diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2014-03-13 07:38:16 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2014-03-13 07:38:16 +0100 |
commit | 571a7a5dbef307489b496840fcdd07ba44ee1128 (patch) | |
tree | 69003c7f1000cf74ca95e3397c202858fa711492 /src | |
parent | ed6ea18c30176ca0e961a2535837d54adeeef2ec (diff) | |
download | weechat-571a7a5dbef307489b496840fcdd07ba44ee1128.zip |
exec: execute commands from input of exec buffers
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/exec/exec-buffer.c | 15 | ||||
-rw-r--r-- | src/plugins/exec/exec-command.c | 354 | ||||
-rw-r--r-- | src/plugins/exec/exec-command.h | 3 |
3 files changed, 207 insertions, 165 deletions
diff --git a/src/plugins/exec/exec-buffer.c b/src/plugins/exec/exec-buffer.c index ce36db0a4..9a220785e 100644 --- a/src/plugins/exec/exec-buffer.c +++ b/src/plugins/exec/exec-buffer.c @@ -26,6 +26,7 @@ #include "../weechat-plugin.h" #include "exec.h" #include "exec-buffer.h" +#include "exec-command.h" #include "exec-config.h" @@ -37,6 +38,9 @@ int exec_buffer_input_cb (void *data, struct t_gui_buffer *buffer, const char *input_data) { + char **argv, **argv_eol; + int argc; + /* make C compiler happy */ (void) data; @@ -47,6 +51,17 @@ exec_buffer_input_cb (void *data, struct t_gui_buffer *buffer, return WEECHAT_RC_OK; } + argv = weechat_string_split (input_data, " ", 0, 0, &argc); + argv_eol = weechat_string_split (input_data, " ", 1, 0, NULL); + + if (argv && argv_eol) + exec_command_run (buffer, argc, argv, argv_eol, 0); + + if (argv) + weechat_string_free_split (argv); + if (argv_eol) + weechat_string_free_split (argv_eol); + return WEECHAT_RC_OK; } diff --git a/src/plugins/exec/exec-command.c b/src/plugins/exec/exec-command.c index 97f318f01..b54904ab1 100644 --- a/src/plugins/exec/exec-command.c +++ b/src/plugins/exec/exec-command.c @@ -288,179 +288,24 @@ exec_command_parse_options (struct t_exec_cmd_options *cmd_options, } /* - * Callback for command "/exec": manage executed commands. + * Runs a command. + * + * Returns: + * WEECHAT_RC_OK: command run successfully + * WEECHAT_RC_ERROR: error running command */ int -exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, - char **argv, char **argv_eol) +exec_command_run (struct t_gui_buffer *buffer, + int argc, char **argv, char **argv_eol, int start_arg) { - int i, length, count; - char *text, str_buffer[512]; - struct t_exec_cmd *ptr_exec_cmd, *ptr_next_exec_cmd, *new_exec_cmd; + char str_buffer[512]; + struct t_exec_cmd *new_exec_cmd; struct t_exec_cmd_options cmd_options; struct t_hashtable *process_options; struct t_infolist *ptr_infolist; struct t_gui_buffer *new_buffer; - /* make C compiler happy */ - (void) data; - (void) buffer; - - /* list running commands */ - if ((argc == 1) - || ((argc == 2) && (weechat_strcasecmp (argv[1], "-list") == 0))) - { - exec_command_list (); - return WEECHAT_RC_OK; - } - - /* send text to a running process */ - if (weechat_strcasecmp (argv[1], "-in") == 0) - { - if (argc < 4) - return WEECHAT_RC_ERROR; - ptr_exec_cmd = exec_command_search_running_id (argv[2]); - if (ptr_exec_cmd && ptr_exec_cmd->hook) - { - length = strlen (argv_eol[3]) + 1 + 1; - text = malloc (length); - if (text) - { - snprintf (text, length, "%s\n", argv_eol[3]); - weechat_hook_set (ptr_exec_cmd->hook, "stdin", text); - free (text); - } - } - return WEECHAT_RC_OK; - } - - /* send text to a running process (if given), then close stdin */ - if (weechat_strcasecmp (argv[1], "-inclose") == 0) - { - if (argc < 3) - return WEECHAT_RC_ERROR; - ptr_exec_cmd = exec_command_search_running_id (argv[2]); - if (ptr_exec_cmd && ptr_exec_cmd->hook) - { - if (argc > 3) - { - length = strlen (argv_eol[3]) + 1 + 1; - text = malloc (length); - if (text) - { - snprintf (text, length, "%s\n", argv_eol[3]); - weechat_hook_set (ptr_exec_cmd->hook, "stdin", text); - free (text); - } - } - weechat_hook_set (ptr_exec_cmd->hook, "stdin_close", "1"); - } - return WEECHAT_RC_OK; - } - - /* send a signal to a running process */ - if (weechat_strcasecmp (argv[1], "-signal") == 0) - { - if (argc < 4) - return WEECHAT_RC_ERROR; - ptr_exec_cmd = exec_command_search_running_id (argv[2]); - if (ptr_exec_cmd) - weechat_hook_set (ptr_exec_cmd->hook, "signal", argv[3]); - return WEECHAT_RC_OK; - } - - /* send a KILL signal to a running process */ - if (weechat_strcasecmp (argv[1], "-kill") == 0) - { - if (argc < 3) - return WEECHAT_RC_ERROR; - ptr_exec_cmd = exec_command_search_running_id (argv[2]); - if (ptr_exec_cmd) - weechat_hook_set (ptr_exec_cmd->hook, "signal", "kill"); - return WEECHAT_RC_OK; - } - - /* send a KILL signal to all running processes */ - if (weechat_strcasecmp (argv[1], "-killall") == 0) - { - for (ptr_exec_cmd = exec_cmds; ptr_exec_cmd; - ptr_exec_cmd = ptr_exec_cmd->next_cmd) - { - if (ptr_exec_cmd->hook) - { - weechat_hook_set (ptr_exec_cmd->hook, "signal", "kill"); - } - } - return WEECHAT_RC_OK; - } - - /* set a hook property */ - if (weechat_strcasecmp (argv[1], "-set") == 0) - { - if (argc < 5) - return WEECHAT_RC_ERROR; - ptr_exec_cmd = exec_command_search_running_id (argv[2]); - if (ptr_exec_cmd) - weechat_hook_set (ptr_exec_cmd->hook, argv[3], argv_eol[4]); - return WEECHAT_RC_OK; - } - - /* delete terminated command(s) */ - if (weechat_strcasecmp (argv[1], "-del") == 0) - { - if (argc < 3) - return WEECHAT_RC_ERROR; - if (weechat_strcasecmp (argv[2], "-all") == 0) - { - count = 0; - ptr_exec_cmd = exec_cmds; - while (ptr_exec_cmd) - { - ptr_next_exec_cmd = ptr_exec_cmd->next_cmd; - if (!ptr_exec_cmd->hook) - { - exec_free (ptr_exec_cmd); - count++; - } - ptr_exec_cmd = ptr_next_exec_cmd; - } - weechat_printf (NULL, _("%d commands removed"), count); - } - else - { - for (i = 2; i < argc; i++) - { - ptr_exec_cmd = exec_search_by_id (argv[i]); - if (ptr_exec_cmd) - { - if (ptr_exec_cmd->hook) - { - weechat_printf (NULL, - _("%s%s: command with id \"%s\" is still " - "running"), - weechat_prefix ("error"), EXEC_PLUGIN_NAME, - argv[i]); - } - else - { - exec_free (ptr_exec_cmd); - weechat_printf (NULL, - _("Command \"%s\" removed"), argv[i]); - } - } - else - { - weechat_printf (NULL, - _("%s%s: command id \"%s\" not found"), - weechat_prefix ("error"), EXEC_PLUGIN_NAME, - argv[i]); - } - } - } - return WEECHAT_RC_OK; - } - /* parse command options */ cmd_options.command_index = -1; cmd_options.use_shell = 1; @@ -487,7 +332,7 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, weechat_prefix ("error"), EXEC_PLUGIN_NAME); return WEECHAT_RC_ERROR; } - if (!exec_command_parse_options (&cmd_options, argc, argv, 1, 1)) + if (!exec_command_parse_options (&cmd_options, argc, argv, start_arg, 1)) return WEECHAT_RC_ERROR; /* options "-bg" and "-o"/"-n" are incompatible */ @@ -576,6 +421,12 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, if (cmd_options.switch_to_buffer) weechat_buffer_set (cmd_options.ptr_buffer, "display", "1"); } + if (cmd_options.ptr_buffer + && (strcmp (weechat_buffer_get_string (cmd_options.ptr_buffer, "plugin"), + EXEC_PLUGIN_NAME) == 0)) + { + cmd_options.new_buffer = 1; + } new_exec_cmd->line_numbers = (cmd_options.line_numbers < 0) ? cmd_options.new_buffer : cmd_options.line_numbers; @@ -624,6 +475,179 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, } /* + * Callback for command "/exec": manage executed commands. + */ + +int +exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, + char **argv, char **argv_eol) +{ + int i, length, count; + char *text; + struct t_exec_cmd *ptr_exec_cmd, *ptr_next_exec_cmd; + + /* make C compiler happy */ + (void) data; + (void) buffer; + + /* list running commands */ + if ((argc == 1) + || ((argc == 2) && (weechat_strcasecmp (argv[1], "-list") == 0))) + { + exec_command_list (); + return WEECHAT_RC_OK; + } + + /* send text to a running process */ + if (weechat_strcasecmp (argv[1], "-in") == 0) + { + if (argc < 4) + return WEECHAT_RC_ERROR; + ptr_exec_cmd = exec_command_search_running_id (argv[2]); + if (ptr_exec_cmd && ptr_exec_cmd->hook) + { + length = strlen (argv_eol[3]) + 1 + 1; + text = malloc (length); + if (text) + { + snprintf (text, length, "%s\n", argv_eol[3]); + weechat_hook_set (ptr_exec_cmd->hook, "stdin", text); + free (text); + } + } + return WEECHAT_RC_OK; + } + + /* send text to a running process (if given), then close stdin */ + if (weechat_strcasecmp (argv[1], "-inclose") == 0) + { + if (argc < 3) + return WEECHAT_RC_ERROR; + ptr_exec_cmd = exec_command_search_running_id (argv[2]); + if (ptr_exec_cmd && ptr_exec_cmd->hook) + { + if (argc > 3) + { + length = strlen (argv_eol[3]) + 1 + 1; + text = malloc (length); + if (text) + { + snprintf (text, length, "%s\n", argv_eol[3]); + weechat_hook_set (ptr_exec_cmd->hook, "stdin", text); + free (text); + } + } + weechat_hook_set (ptr_exec_cmd->hook, "stdin_close", "1"); + } + return WEECHAT_RC_OK; + } + + /* send a signal to a running process */ + if (weechat_strcasecmp (argv[1], "-signal") == 0) + { + if (argc < 4) + return WEECHAT_RC_ERROR; + ptr_exec_cmd = exec_command_search_running_id (argv[2]); + if (ptr_exec_cmd) + weechat_hook_set (ptr_exec_cmd->hook, "signal", argv[3]); + return WEECHAT_RC_OK; + } + + /* send a KILL signal to a running process */ + if (weechat_strcasecmp (argv[1], "-kill") == 0) + { + if (argc < 3) + return WEECHAT_RC_ERROR; + ptr_exec_cmd = exec_command_search_running_id (argv[2]); + if (ptr_exec_cmd) + weechat_hook_set (ptr_exec_cmd->hook, "signal", "kill"); + return WEECHAT_RC_OK; + } + + /* send a KILL signal to all running processes */ + if (weechat_strcasecmp (argv[1], "-killall") == 0) + { + for (ptr_exec_cmd = exec_cmds; ptr_exec_cmd; + ptr_exec_cmd = ptr_exec_cmd->next_cmd) + { + if (ptr_exec_cmd->hook) + { + weechat_hook_set (ptr_exec_cmd->hook, "signal", "kill"); + } + } + return WEECHAT_RC_OK; + } + + /* set a hook property */ + if (weechat_strcasecmp (argv[1], "-set") == 0) + { + if (argc < 5) + return WEECHAT_RC_ERROR; + ptr_exec_cmd = exec_command_search_running_id (argv[2]); + if (ptr_exec_cmd) + weechat_hook_set (ptr_exec_cmd->hook, argv[3], argv_eol[4]); + return WEECHAT_RC_OK; + } + + /* delete terminated command(s) */ + if (weechat_strcasecmp (argv[1], "-del") == 0) + { + if (argc < 3) + return WEECHAT_RC_ERROR; + if (weechat_strcasecmp (argv[2], "-all") == 0) + { + count = 0; + ptr_exec_cmd = exec_cmds; + while (ptr_exec_cmd) + { + ptr_next_exec_cmd = ptr_exec_cmd->next_cmd; + if (!ptr_exec_cmd->hook) + { + exec_free (ptr_exec_cmd); + count++; + } + ptr_exec_cmd = ptr_next_exec_cmd; + } + weechat_printf (NULL, _("%d commands removed"), count); + } + else + { + for (i = 2; i < argc; i++) + { + ptr_exec_cmd = exec_search_by_id (argv[i]); + if (ptr_exec_cmd) + { + if (ptr_exec_cmd->hook) + { + weechat_printf (NULL, + _("%s%s: command with id \"%s\" is still " + "running"), + weechat_prefix ("error"), EXEC_PLUGIN_NAME, + argv[i]); + } + else + { + exec_free (ptr_exec_cmd); + weechat_printf (NULL, + _("Command \"%s\" removed"), argv[i]); + } + } + else + { + weechat_printf (NULL, + _("%s%s: command id \"%s\" not found"), + weechat_prefix ("error"), EXEC_PLUGIN_NAME, + argv[i]); + } + } + } + return WEECHAT_RC_OK; + } + + return exec_command_run (buffer, argc, argv, argv_eol, 1); +} + +/* * Hooks exec commands. */ diff --git a/src/plugins/exec/exec-command.h b/src/plugins/exec/exec-command.h index 680d60be3..e0cfb7618 100644 --- a/src/plugins/exec/exec-command.h +++ b/src/plugins/exec/exec-command.h @@ -36,6 +36,9 @@ struct t_exec_cmd_options const char *ptr_command_name; /* name of command */ }; +extern int exec_command_run (struct t_gui_buffer *buffer, + int argc, char **argv, char **argv_eol, + int start_arg); extern void exec_command_init (); #endif /* __WEECHAT_EXEC_COMMAND_H */ |