From 0291d6b7a1548f31892554c0554a7f51a421dad0 Mon Sep 17 00:00:00 2001 From: Michael Jennings Date: Wed, 9 May 2001 03:14:18 +0000 Subject: [PATCH] Tue May 8 19:53:56 PDT 2001 Michael Jennings You can now create key, mouse button, menu item, and buttonbar button bindings which trigger the parsing of a "script." I've thrown in a few functions already to replace some icky escape sequences; there will be many more functions forthcoming, although I don't know how many of them (if any) will make it in prior to release. The usage is pretty simple. Where before you might've said: action string "\e]6;70\a" you would now say: action script "exit();" You can call as many functions as you like per script. Use a semi-colon (';') to separate each function call. If you only call one function, the ';' is optional. If you aren't passing parameters to the function, the parentheses are also optional. Parameters are separated by commas and/or whitespace. Valid functions currently are: die(): die() is a synonym for exit(). exec(): exec() is a synonym for spawn(). exit(): Exit Eterm. Takes an optional exit code or message. quit(): quit() is a synonym for exit(). save(): Save the config. Specify "theme" to save the theme config. Also takes an optional path & filename. search(): Search the scrollback buffer for a string. spawn(): Spawns a sub-program. Defaults to "Eterm". You will need the libast dated today to build and run this Eterm. You will also need to update your themes. SVN revision: 4748 --- ChangeLog | 37 ++++++++++++ src/script.c | 115 ++++++++++++++++++++++++++++++++++-- src/script.h | 14 +++++ src/term.c | 6 +- themes/Eterm/menus.cfg | 12 ++-- themes/Eterm/theme.cfg.in | 6 +- themes/auto/menus.cfg | 12 ++-- themes/auto/theme.cfg.in | 6 +- themes/cEterm/menus.cfg | 12 ++-- themes/cEterm/theme.cfg.in | 6 +- themes/chooser/menus.cfg | 12 ++-- themes/chooser/theme.cfg.in | 6 +- themes/emacs/menus.cfg | 6 +- themes/emacs/theme.cfg.in | 6 +- themes/irc/menus.cfg | 12 ++-- themes/irc/theme.cfg.in | 4 +- themes/mutt/menus.cfg | 6 +- themes/mutt/theme.cfg.in | 6 +- themes/trans/menus.cfg | 12 ++-- themes/trans/theme.cfg.in | 6 +- 20 files changed, 232 insertions(+), 70 deletions(-) diff --git a/ChangeLog b/ChangeLog index 894226f..216f5cc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4089,3 +4089,40 @@ Fri May 4 23:56:12 PDT 2001 Michael Jennings backgrounds. ------------------------------------------------------------------------------- +Tue May 8 19:53:56 PDT 2001 Michael Jennings + + You can now create key, mouse button, menu item, and buttonbar button + bindings which trigger the parsing of a "script." I've thrown in a + few functions already to replace some icky escape sequences; there + will be many more functions forthcoming, although I don't know how + many of them (if any) will make it in prior to release. + + The usage is pretty simple. Where before you might've said: + + action string "\e]6;70\a" + + you would now say: + + action script "exit();" + + You can call as many functions as you like per script. Use a + semi-colon (';') to separate each function call. If you only call + one function, the ';' is optional. If you aren't passing parameters + to the function, the parentheses are also optional. Parameters are + separated by commas and/or whitespace. + + Valid functions currently are: + + die(): die() is a synonym for exit(). + exec(): exec() is a synonym for spawn(). + exit(): Exit Eterm. Takes an optional exit code or message. + quit(): quit() is a synonym for exit(). + save(): Save the config. Specify "theme" to save the theme + config. Also takes an optional path & filename. + search(): Search the scrollback buffer for a string. + spawn(): Spawns a sub-program. Defaults to "Eterm". + + You will need the libast dated today to build and run this Eterm. + You will also need to update your themes. + +------------------------------------------------------------------------------- diff --git a/src/script.c b/src/script.c index f804db0..563923f 100644 --- a/src/script.c +++ b/src/script.c @@ -34,11 +34,26 @@ static const char cvs_ident[] = "$Id$"; #include #include "command.h" -#include "startup.h" #include "options.h" #include "pixmap.h" -#include "system.h" +#include "screen.h" #include "script.h" +#include "startup.h" +#include "system.h" + +static eterm_script_handler_t script_handlers[] = +{ + { "die", script_handler_exit }, + { "exec", script_handler_spawn }, + { "exit", script_handler_exit }, + { "quit", script_handler_exit }, + { "save", script_handler_save }, + { "search", script_handler_search }, + { "spawn", script_handler_spawn }, + + { "nop", script_handler_nop } +}; +static size_t handler_count = sizeof(script_handlers) / sizeof(eterm_script_handler_t); #if 0 void @@ -104,6 +119,81 @@ eterm_handle_winop(char *action) } #endif +/********* HANDLERS **********/ +void +script_handler_exit(char **params) +{ + unsigned char code = 0; + char *tmp; + + if (params && *params) { + if (isdigit(params[0][0]) || (params[0][0] == '-' && isdigit(params[0][1]))) { + code = (unsigned char) atoi(params[0]); + } else { + tmp = join(" ", params); + printf("Exiting: %s\n", tmp); + FREE(tmp); + } + } + exit(code); +} + +void +script_handler_save(char **params) +{ + if (params && *params) { + if (!strcasecmp(params[0], "theme")) { + save_config(params[1], SAVE_THEME_CONFIG); + } else { + save_config(params[0], SAVE_USER_CONFIG); + } + } else { + save_config(NULL, SAVE_USER_CONFIG); + } +} + +void +script_handler_search(char **params) +{ + scr_search_scrollback(params ? params[0] : NULL); +} + +void +script_handler_spawn(char **params) +{ + char *tmp; + + if (params && *params) { + tmp = join(" ", params); + system_no_wait(tmp); + FREE(tmp); + } else { + system_no_wait("Eterm"); + } +} + +void +script_handler_nop(char **params) +{ + USE_VAR(params); +} + +/********* ENGINE *********/ +eterm_script_handler_t * +script_find_handler(const char *name) +{ + register unsigned long i; + + for (i = 0; i < handler_count; i++) { + /* Small optimization. Only call strcasecmp() if the first letter matches. */ + if ((tolower(name[0]) == tolower(script_handlers[i].name[0])) + && !strcasecmp(name, script_handlers[i].name)) { + return &script_handlers[i]; + } + } + return NULL; +} + void script_parse(char *s) { @@ -112,6 +202,7 @@ script_parse(char *s) register unsigned long i; char *func_name, *params, *tmp; size_t len; + eterm_script_handler_t *func; REQUIRE(s != NULL); @@ -137,25 +228,37 @@ script_parse(char *s) func_name[len] = 0; } else { print_error("Error in script \"%s\": Missing function name before \"%s\".\n", s, params); + free_array((void **) token_list, 0); return; } } else { func_name = STRDUP(pstr); } - if (func_name) { - chomp(func_name); + if (!func_name) { + free_array((void **) token_list, 0); + return; } if (params) { params++; - if ((tmp = strchr(params, ')')) != NULL) { + if ((tmp = strrchr(params, ')')) != NULL) { *tmp = 0; } else { print_error("Error in script \"%s\": Missing closing parentheses for \"%s\".\n", s, token_list[i]); + free_array((void **) token_list, 0); return; } param_list = split(", \t", params); } - D_SCRIPT(("Calling function %s with parameters: %s\n", NONULL(func_name), NONULL(params))); + D_SCRIPT(("Calling function %s with parameters: %s\n", func_name, NONULL(params))); + if ((func = script_find_handler(func_name)) != NULL) { + (func->handler)(param_list); + } else { + print_error("Error in script \"%s\": No such function \"%s\".\n", s, func_name); + } } + if (params) { + free_array((void **) param_list, 0); + } + free_array((void **) token_list, 0); } diff --git a/src/script.h b/src/script.h index 0277cef..5fc0c9b 100644 --- a/src/script.h +++ b/src/script.h @@ -31,12 +31,26 @@ /************ Macros and Definitions ************/ /************ Structures ************/ +typedef void (*eterm_script_handler_function_t)(char **); +typedef struct { + char *name; + eterm_script_handler_function_t handler; +} eterm_script_handler_t; /************ Variables ************/ /************ Function Prototypes ************/ _XFUNCPROTOBEGIN +/* Handlers */ +extern void script_handler_exit(char **); +extern void script_handler_save(char **); +extern void script_handler_search(char **); +extern void script_handler_spawn(char **); +extern void script_handler_nop(char **); + +/* Engine */ +extern eterm_script_handler_t *script_find_handler(const char *); extern void script_parse(char *); _XFUNCPROTOEND diff --git a/src/term.c b/src/term.c index 41edbc4..4bffcd1 100644 --- a/src/term.c +++ b/src/term.c @@ -1841,7 +1841,7 @@ xterm_seq(int op, const char *str) 30-39 Foreground/Text Color Configuration 40-49 Background Color Configuration 50-69 Window/Window Manager Configuration/Interaction - 70-79 Internal Eterm Operations + 70+ Internal Eterm Operations */ switch (eterm_seq_op) { #ifdef PIXMAP_SUPPORT @@ -2269,6 +2269,7 @@ xterm_seq(int op, const char *str) XSendEvent(Xdisplay, Xroot, False, SubstructureNotifyMask, (XEvent *) & xev); } break; +#if 0 case 70: /* Exit Eterm */ exit(0); @@ -2287,6 +2288,7 @@ xterm_seq(int op, const char *str) save_config(NULL, SAVE_USER_CONFIG); } break; +#endif case 72: /* Search scrollback buffer for a string. NULL to clear. */ nstr = (char *) strsep(&tnstr, ";"); @@ -2296,6 +2298,7 @@ xterm_seq(int op, const char *str) scr_search_scrollback(NULL); } break; +#if 0 case 73: /* Spawn a subprogram */ nstr = (char *) strsep(&tnstr, ";"); @@ -2303,6 +2306,7 @@ xterm_seq(int op, const char *str) system_no_wait(nstr); } break; +#endif case 80: /* Set debugging level */ nstr = (char *) strsep(&tnstr, ";"); diff --git a/themes/Eterm/menus.cfg b/themes/Eterm/menus.cfg index 99f73ee..b81766a 100644 --- a/themes/Eterm/menus.cfg +++ b/themes/Eterm/menus.cfg @@ -879,11 +879,11 @@ begin menu separator begin menuitem text "Save Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end @@ -905,7 +905,7 @@ begin menu - begin menuitem text "New Window..." - action string "\e]6;73;Eterm\a" + action script "spawn(Eterm)" end begin menuitem text "Version" @@ -918,15 +918,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/Eterm/theme.cfg.in b/themes/Eterm/theme.cfg.in index 9f52a6c..969c37c 100644 --- a/themes/Eterm/theme.cfg.in +++ b/themes/Eterm/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -304,8 +304,8 @@ begin main button Font action menu Font button Background action menu Background button Terminal action menu Terminal - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/auto/menus.cfg b/themes/auto/menus.cfg index 99f73ee..b81766a 100644 --- a/themes/auto/menus.cfg +++ b/themes/auto/menus.cfg @@ -879,11 +879,11 @@ begin menu separator begin menuitem text "Save Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end @@ -905,7 +905,7 @@ begin menu - begin menuitem text "New Window..." - action string "\e]6;73;Eterm\a" + action script "spawn(Eterm)" end begin menuitem text "Version" @@ -918,15 +918,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/auto/theme.cfg.in b/themes/auto/theme.cfg.in index 9bf58b2..e34442f 100644 --- a/themes/auto/theme.cfg.in +++ b/themes/auto/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -304,8 +304,8 @@ begin main button Font action menu Font button Background action menu Background button Terminal action menu Terminal - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/cEterm/menus.cfg b/themes/cEterm/menus.cfg index 99f73ee..b81766a 100644 --- a/themes/cEterm/menus.cfg +++ b/themes/cEterm/menus.cfg @@ -879,11 +879,11 @@ begin menu separator begin menuitem text "Save Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end @@ -905,7 +905,7 @@ begin menu - begin menuitem text "New Window..." - action string "\e]6;73;Eterm\a" + action script "spawn(Eterm)" end begin menuitem text "Version" @@ -918,15 +918,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/cEterm/theme.cfg.in b/themes/cEterm/theme.cfg.in index 230ba64..8501b6f 100644 --- a/themes/cEterm/theme.cfg.in +++ b/themes/cEterm/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -303,8 +303,8 @@ begin main button Font action menu Font button Background action menu Background button Terminal action menu Terminal - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/chooser/menus.cfg b/themes/chooser/menus.cfg index 539b9fb..bb38c7d 100644 --- a/themes/chooser/menus.cfg +++ b/themes/chooser/menus.cfg @@ -12,6 +12,10 @@ define(SUBMENU_ITEM, `begin menuitem text "$1" action submenu "$1" end') +define(SCRIPT_ITEM, `begin menuitem + text "$1" + action script "$2" + end') define(FONT_ITEM, `begin menuitem text "$1" action string "\e]50;#$2" @@ -979,11 +983,11 @@ begin menu SUBMENU_ITEM(Background) SUBMENU_ITEM(Terminal) - - STRING_ITEM(`New Eterm Window', `\e]6;73;Eterm\a') + SCRIPT_ITEM(`New Eterm Window', `spawn(Eterm)') STRING_ITEM(`Version', `\e[8n') STRING_ITEM(`Status', `\e[9n') separator - STRING_ITEM(`Save User Settings...', `\e]6;71\a') - STRING_ITEM(`Save Theme Settings...', `\e]6;71;theme\a') - STRING_ITEM(`Exit', `\e]6;70\a') + SCRIPT_ITEM(`Save User Settings...', `save') + SCRIPT_ITEM(`Save Theme Settings...', `save(theme)') + SCRIPT_ITEM(`Exit', `exit') end diff --git a/themes/chooser/theme.cfg.in b/themes/chooser/theme.cfg.in index 2fdea5e..851f36f 100644 --- a/themes/chooser/theme.cfg.in +++ b/themes/chooser/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -305,8 +305,8 @@ begin main button rlogin action menu rlogin button telnet action menu telnet button ftp action menu ftp - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/emacs/menus.cfg b/themes/emacs/menus.cfg index 488f5ad..3d07d37 100644 --- a/themes/emacs/menus.cfg +++ b/themes/emacs/menus.cfg @@ -1128,15 +1128,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/emacs/theme.cfg.in b/themes/emacs/theme.cfg.in index b7d2e76..a2fb477 100644 --- a/themes/emacs/theme.cfg.in +++ b/themes/emacs/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -306,8 +306,8 @@ begin main button Edit action menu Edit button Search action menu Search button Help action menu Help - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/irc/menus.cfg b/themes/irc/menus.cfg index 99f73ee..b81766a 100644 --- a/themes/irc/menus.cfg +++ b/themes/irc/menus.cfg @@ -879,11 +879,11 @@ begin menu separator begin menuitem text "Save Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end @@ -905,7 +905,7 @@ begin menu - begin menuitem text "New Window..." - action string "\e]6;73;Eterm\a" + action script "spawn(Eterm)" end begin menuitem text "Version" @@ -918,15 +918,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/irc/theme.cfg.in b/themes/irc/theme.cfg.in index 1e67561..ddd91ed 100644 --- a/themes/irc/theme.cfg.in +++ b/themes/irc/theme.cfg.in @@ -306,8 +306,8 @@ begin main button Font action menu Font button Background action menu Background button Terminal action menu Terminal - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/mutt/menus.cfg b/themes/mutt/menus.cfg index ea9b084..780a5f1 100644 --- a/themes/mutt/menus.cfg +++ b/themes/mutt/menus.cfg @@ -1062,15 +1062,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/mutt/theme.cfg.in b/themes/mutt/theme.cfg.in index 4d6683c..532499d 100644 --- a/themes/mutt/theme.cfg.in +++ b/themes/mutt/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -302,8 +302,8 @@ begin main button Eterm action menu Eterm button Messages action menu Messages button Mailbox action menu Mailbox - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options. diff --git a/themes/trans/menus.cfg b/themes/trans/menus.cfg index 99f73ee..b81766a 100644 --- a/themes/trans/menus.cfg +++ b/themes/trans/menus.cfg @@ -879,11 +879,11 @@ begin menu separator begin menuitem text "Save Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end @@ -905,7 +905,7 @@ begin menu - begin menuitem text "New Window..." - action string "\e]6;73;Eterm\a" + action script "spawn(Eterm)" end begin menuitem text "Version" @@ -918,15 +918,15 @@ begin menu separator begin menuitem text "Save User Settings..." - action string "\e]6;71\a" + action script "save()" end begin menuitem text "Save Theme Settings..." - action string "\e]6;71;theme\a" + action script "save(theme)" end begin menuitem text "Exit" - action string "\e]6;70\a" + action script "exit" end end diff --git a/themes/trans/theme.cfg.in b/themes/trans/theme.cfg.in index 09baad2..97a327b 100644 --- a/themes/trans/theme.cfg.in +++ b/themes/trans/theme.cfg.in @@ -19,7 +19,7 @@ begin main begin color # Foreground, background, cursor, scrollbar, pointer colors - foreground white + foreground #aaaaaa background black cursor #ffff00 cursor_text #880000 @@ -304,8 +304,8 @@ begin main button Font action menu Font button Background action menu Background button Terminal action menu Terminal - rbutton icon help.png action string "\e]6;73;Eterm -e man Eterm\a" - rbutton icon exit.png action string "\e]6;70\a" + rbutton icon help.png action script "spawn(Eterm -e man Eterm)" + rbutton icon exit.png action script "exit" end button_bar # The XIM support options.