diff options
-rw-r--r-- | po/POTFILES.in | 2 | ||||
-rw-r--r-- | po/srcfiles.cmake | 2 | ||||
-rw-r--r-- | src/plugins/exec/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/plugins/exec/Makefile.am | 2 | ||||
-rw-r--r-- | src/plugins/exec/exec-buffer.c | 130 | ||||
-rw-r--r-- | src/plugins/exec/exec-buffer.h | 27 | ||||
-rw-r--r-- | src/plugins/exec/exec-command.c | 106 | ||||
-rw-r--r-- | src/plugins/exec/exec-command.h | 15 | ||||
-rw-r--r-- | src/plugins/exec/exec.c | 33 | ||||
-rw-r--r-- | src/plugins/exec/exec.h | 3 |
10 files changed, 268 insertions, 53 deletions
diff --git a/po/POTFILES.in b/po/POTFILES.in index f3f120ee0..d0dadead2 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -114,6 +114,8 @@ ./src/plugins/aspell/weechat-aspell-speller.h ./src/plugins/charset/charset.c ./src/plugins/exec/exec.c +./src/plugins/exec/exec-buffer.c +./src/plugins/exec/exec-buffer.h ./src/plugins/exec/exec-command.c ./src/plugins/exec/exec-command.h ./src/plugins/exec/exec-completion.c diff --git a/po/srcfiles.cmake b/po/srcfiles.cmake index 2fce5d3d2..886397caa 100644 --- a/po/srcfiles.cmake +++ b/po/srcfiles.cmake @@ -115,6 +115,8 @@ SET(WEECHAT_SOURCES ./src/plugins/aspell/weechat-aspell-speller.h ./src/plugins/charset/charset.c ./src/plugins/exec/exec.c +./src/plugins/exec/exec-buffer.c +./src/plugins/exec/exec-buffer.h ./src/plugins/exec/exec-command.c ./src/plugins/exec/exec-command.h ./src/plugins/exec/exec-completion.c diff --git a/src/plugins/exec/CMakeLists.txt b/src/plugins/exec/CMakeLists.txt index bd86d831f..a5286eabd 100644 --- a/src/plugins/exec/CMakeLists.txt +++ b/src/plugins/exec/CMakeLists.txt @@ -19,6 +19,7 @@ ADD_LIBRARY(exec MODULE exec.c exec.h +exec-buffer.c exec-buffer.h exec-command.c exec-command.h exec-completion.c exec-completion.h exec-config.c exec-config.h) diff --git a/src/plugins/exec/Makefile.am b/src/plugins/exec/Makefile.am index 3fa30374f..cad543311 100644 --- a/src/plugins/exec/Makefile.am +++ b/src/plugins/exec/Makefile.am @@ -25,6 +25,8 @@ lib_LTLIBRARIES = exec.la exec_la_SOURCES = exec.c \ exec.h \ + exec-buffer.c \ + exec-buffer.h \ exec-command.c \ exec-command.h \ exec-completion.c \ diff --git a/src/plugins/exec/exec-buffer.c b/src/plugins/exec/exec-buffer.c new file mode 100644 index 000000000..ce36db0a4 --- /dev/null +++ b/src/plugins/exec/exec-buffer.c @@ -0,0 +1,130 @@ +/* + * exec-buffer.c - buffers with output of commands + * + * Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org> + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "../weechat-plugin.h" +#include "exec.h" +#include "exec-buffer.h" +#include "exec-config.h" + + +/* + * Callback for user data in an exec buffer. + */ + +int +exec_buffer_input_cb (void *data, struct t_gui_buffer *buffer, + const char *input_data) +{ + /* make C compiler happy */ + (void) data; + + /* close buffer */ + if (strcmp (input_data, "q") == 0) + { + weechat_buffer_close (buffer); + return WEECHAT_RC_OK; + } + + return WEECHAT_RC_OK; +} + +/* + * Callback called when an exec buffer is closed. + */ + +int +exec_buffer_close_cb (void *data, struct t_gui_buffer *buffer) +{ + /* make C compiler happy */ + (void) data; + (void) buffer; + + return WEECHAT_RC_OK; +} + +/* + * Restore buffer callbacks (input and close) for buffers created by exec + * plugin. + */ + +void +exec_buffer_set_callbacks () +{ + struct t_infolist *ptr_infolist; + struct t_gui_buffer *ptr_buffer; + const char *plugin_name; + + ptr_infolist = weechat_infolist_get ("buffer", NULL, ""); + if (ptr_infolist) + { + while (weechat_infolist_next (ptr_infolist)) + { + ptr_buffer = weechat_infolist_pointer (ptr_infolist, "pointer"); + plugin_name = weechat_infolist_string (ptr_infolist, "plugin_name"); + if (ptr_buffer && plugin_name + && (strcmp (plugin_name, EXEC_PLUGIN_NAME) == 0)) + { + weechat_buffer_set_pointer (ptr_buffer, "close_callback", + &exec_buffer_close_cb); + weechat_buffer_set_pointer (ptr_buffer, "input_callback", + &exec_buffer_input_cb); + } + } + weechat_infolist_free (ptr_infolist); + } +} + +/* + * Creates a new exec buffer for a command. + */ + +struct t_gui_buffer * +exec_buffer_new (const char *name, int switch_to_buffer) +{ + struct t_gui_buffer *new_buffer; + + new_buffer = weechat_buffer_search (EXEC_PLUGIN_NAME, name); + if (new_buffer) + return new_buffer; + + new_buffer = weechat_buffer_new (name, + &exec_buffer_input_cb, NULL, + &exec_buffer_close_cb, NULL); + + /* failed to create buffer ? then return */ + if (!new_buffer) + return NULL; + + weechat_buffer_set (new_buffer, "title", _("Executed commands")); + weechat_buffer_set (new_buffer, "localvar_set_type", "exec"); + weechat_buffer_set (new_buffer, "localvar_set_no_log", "1"); + weechat_buffer_set (new_buffer, "time_for_each_line", "0"); + weechat_buffer_set (new_buffer, "input_get_unknown_commands", "0"); + + if (switch_to_buffer) + weechat_buffer_set (new_buffer, "display", "1"); + + return new_buffer; +} diff --git a/src/plugins/exec/exec-buffer.h b/src/plugins/exec/exec-buffer.h new file mode 100644 index 000000000..42e519888 --- /dev/null +++ b/src/plugins/exec/exec-buffer.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org> + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __WEECHAT_EXEC_BUFFER_H +#define __WEECHAT_EXEC_BUFFER_H 1 + +extern void exec_buffer_set_callbacks (); +extern struct t_gui_buffer *exec_buffer_new (const char *name, + int switch_to_buffer); + +#endif /* __WEECHAT_EXEC_BUFFER_H */ diff --git a/src/plugins/exec/exec-command.c b/src/plugins/exec/exec-command.c index 779ac2e48..b81e6c181 100644 --- a/src/plugins/exec/exec-command.c +++ b/src/plugins/exec/exec-command.c @@ -26,6 +26,7 @@ #include "../weechat-plugin.h" #include "exec.h" +#include "exec-buffer.h" #include "exec-command.h" #include "exec-config.h" @@ -210,10 +211,22 @@ exec_command_parse_options (struct t_exec_cmd_options *cmd_options, else if (weechat_strcasecmp (argv[i], "-l") == 0) { cmd_options->output_to_buffer = 0; + cmd_options->new_buffer = 0; } else if (weechat_strcasecmp (argv[i], "-o") == 0) { cmd_options->output_to_buffer = 1; + cmd_options->new_buffer = 0; + } + else if (weechat_strcasecmp (argv[i], "-n") == 0) + { + cmd_options->output_to_buffer = 0; + cmd_options->new_buffer = 2; + } + else if (weechat_strcasecmp (argv[i], "-ns") == 0) + { + cmd_options->output_to_buffer = 0; + cmd_options->new_buffer = 1; } else if (weechat_strcasecmp (argv[i], "-timeout") == 0) { @@ -256,11 +269,12 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { int i, length, count; - char *text; + char *text, str_buffer[512]; struct t_exec_cmd *ptr_exec_cmd, *ptr_next_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; @@ -403,6 +417,7 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, cmd_options.pipe_stdin = 0; cmd_options.timeout = 0; cmd_options.output_to_buffer = 0; + cmd_options.new_buffer = 0; cmd_options.ptr_name = NULL; /* parse default options */ @@ -420,8 +435,9 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, if (!exec_command_parse_options (&cmd_options, argc, argv, 1, 1)) return WEECHAT_RC_ERROR; - /* "-bg" and "-o" are incompatible options */ - if (cmd_options.detached && cmd_options.output_to_buffer) + /* options "-bg" and "-o"/"-n" are incompatible */ + if (cmd_options.detached + && (cmd_options.output_to_buffer || cmd_options.new_buffer)) return WEECHAT_RC_ERROR; /* command not found? */ @@ -443,8 +459,6 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, exec_free (new_exec_cmd); return WEECHAT_RC_ERROR; } - - /* run the command in background */ if (cmd_options.use_shell) { /* command will be: sh -c "command arguments..." */ @@ -456,6 +470,40 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, weechat_hashtable_set (process_options, "stdin", "1"); if (cmd_options.detached) weechat_hashtable_set (process_options, "detached", "1"); + + /* set variables in new command (before running the command) */ + new_exec_cmd->name = (cmd_options.ptr_name) ? + strdup (cmd_options.ptr_name) : NULL; + new_exec_cmd->command = strdup (argv_eol[cmd_options.command_index]); + new_exec_cmd->detached = cmd_options.detached; + new_exec_cmd->output_to_buffer = cmd_options.output_to_buffer; + if (cmd_options.new_buffer) + { + /* output in a new buffer */ + if (new_exec_cmd->name) + { + snprintf (str_buffer, sizeof (str_buffer), + "exec.%s", new_exec_cmd->name); + } + else + { + snprintf (str_buffer, sizeof (str_buffer), + "exec.%d", new_exec_cmd->number); + } + new_buffer = exec_buffer_new (str_buffer, (cmd_options.new_buffer > 1)); + if (new_buffer) + { + new_exec_cmd->buffer_full_name = + strdup (weechat_buffer_get_string (new_buffer, "full_name")); + } + } + else + { + new_exec_cmd->buffer_full_name = + strdup (weechat_buffer_get_string (buffer, "full_name")); + } + + /* execute the command */ if (weechat_exec_plugin->debug >= 1) { weechat_printf (NULL, "%s: executing command: \"%s%s%s\"", @@ -471,37 +519,30 @@ exec_command_exec (void *data, struct t_gui_buffer *buffer, int argc, &exec_process_cb, new_exec_cmd); - weechat_hashtable_free (process_options); - - if (!new_exec_cmd->hook) + if (new_exec_cmd->hook) + { + /* get PID of command */ + ptr_infolist = weechat_infolist_get ("hook", new_exec_cmd->hook, NULL); + if (ptr_infolist) + { + if (weechat_infolist_next (ptr_infolist)) + { + new_exec_cmd->pid = weechat_infolist_integer (ptr_infolist, + "child_pid"); + } + weechat_infolist_free (ptr_infolist); + } + } + else { exec_free (new_exec_cmd); weechat_printf (NULL, _("%s%s: failed to run command \"%s\""), weechat_prefix ("error"), EXEC_PLUGIN_NAME, argv_eol[cmd_options.command_index]); - return WEECHAT_RC_OK; } - new_exec_cmd->name = (cmd_options.ptr_name) ? - strdup (cmd_options.ptr_name) : NULL; - new_exec_cmd->command = strdup (argv_eol[cmd_options.command_index]); - new_exec_cmd->detached = cmd_options.detached; - new_exec_cmd->buffer_plugin = strdup (weechat_buffer_get_string (buffer, - "plugin")); - new_exec_cmd->buffer_name = strdup (weechat_buffer_get_string (buffer, - "name")); - new_exec_cmd->output_to_buffer = cmd_options.output_to_buffer; - ptr_infolist = weechat_infolist_get ("hook", new_exec_cmd->hook, NULL); - if (ptr_infolist) - { - if (weechat_infolist_next (ptr_infolist)) - { - new_exec_cmd->pid = weechat_infolist_integer (ptr_infolist, - "child_pid"); - } - weechat_infolist_free (ptr_infolist); - } + weechat_hashtable_free (process_options); return WEECHAT_RC_OK; } @@ -517,7 +558,7 @@ exec_command_init () "exec", N_("execute external commands"), N_("-list" - " || [-sh|-nosh] [-bg|-nobg] [-stdin|-nostdin] [-l|-o] " + " || [-sh|-nosh] [-bg|-nobg] [-stdin|-nostdin] [-l|-o|-n] " "[-timeout <timeout>] [-name <name>] <command>" " || -in <id> <text>" " || -signal <id> <signal>" @@ -531,7 +572,7 @@ exec_command_init () "the command has some unsafe data, for example the content of a " "message from another user)\n" " -bg: run process in background: do not display process output " - "neither return code (not compatible with option -o)\n" + "neither return code (not compatible with option -o/-n)\n" " -nobg: catch process output and display return code (default)\n" " -stdin: create a pipe for sending data to the process (with " "/exec -in)\n" @@ -540,6 +581,9 @@ exec_command_init () "(default)\n" " -o: send output of command to the current buffer " "(not compatible with option -bg)\n" + " -n: display output of command in a new buffer (not compatible " + "with -bg)\n" + " -ns: same as -n, but don't switch to the new buffer\n" "-timeout: set a timeout for the command (in seconds)\n" " -name: set a name for the command (to name it later with /exec)\n" " command: the command to execute\n" @@ -560,7 +604,7 @@ exec_command_init () "Default options can be set in the option " "exec.command.default_options."), "-list" - " || -sh|-nosh|-bg|-nobg|-stdin|-nostdin|-l|-o|-timeout|-name|%*" + " || -sh|-nosh|-bg|-nobg|-stdin|-nostdin|-l|-o|-n|-timeout|-name|%*" " || -in|-signal|-kill %(exec_commands_ids)" " || -killall" " || -set %(exec_commands_ids) stdin|stdin_close|signal" diff --git a/src/plugins/exec/exec-command.h b/src/plugins/exec/exec-command.h index 4132fd853..c62277a80 100644 --- a/src/plugins/exec/exec-command.h +++ b/src/plugins/exec/exec-command.h @@ -22,13 +22,14 @@ struct t_exec_cmd_options { - int command_index; - int use_shell; - int detached; - int pipe_stdin; - int timeout; - int output_to_buffer; - const char *ptr_name; + int command_index; /* index of command in arguments */ + int use_shell; /* 1 to use shell (sh -c "command") */ + int detached; /* 1 if detached (no output) */ + int pipe_stdin; /* 1 to create a pipe for stdin */ + int timeout; /* timeout (in seconds) */ + int output_to_buffer; /* 1 if output is sent to buffer */ + int new_buffer; /* output in a new buffer */ + const char *ptr_name; /* name of command */ }; extern void exec_command_init (); diff --git a/src/plugins/exec/exec.c b/src/plugins/exec/exec.c index 6cc68710a..ec7ebe870 100644 --- a/src/plugins/exec/exec.c +++ b/src/plugins/exec/exec.c @@ -26,6 +26,7 @@ #include "../weechat-plugin.h" #include "exec.h" +#include "exec-buffer.h" #include "exec-command.h" #include "exec-completion.h" #include "exec-config.h" @@ -121,9 +122,8 @@ exec_add () new_exec_cmd->detached = 0; new_exec_cmd->start_time = time (NULL); new_exec_cmd->end_time = 0; - new_exec_cmd->buffer_plugin = NULL; - new_exec_cmd->buffer_name = NULL; new_exec_cmd->output_to_buffer = 0; + new_exec_cmd->buffer_full_name = NULL; new_exec_cmd->stdout_size = 0; new_exec_cmd->stdout = NULL; new_exec_cmd->stderr_size = 0; @@ -241,8 +241,7 @@ exec_end_command (struct t_exec_cmd *exec_cmd, int return_code) { struct t_gui_buffer *ptr_buffer; - ptr_buffer = weechat_buffer_search (exec_cmd->buffer_plugin, - exec_cmd->buffer_name); + ptr_buffer = weechat_buffer_search ("==", exec_cmd->buffer_full_name); /* display stdout/stderr (if output to buffer, the buffer must exist) */ exec_command_display_output (exec_cmd, ptr_buffer, 1); @@ -368,10 +367,8 @@ exec_free (struct t_exec_cmd *exec_cmd) free (exec_cmd->name); if (exec_cmd->command) free (exec_cmd->command); - if (exec_cmd->buffer_plugin) - free (exec_cmd->buffer_plugin); - if (exec_cmd->buffer_name) - free (exec_cmd->buffer_name); + if (exec_cmd->buffer_full_name) + free (exec_cmd->buffer_full_name); if (exec_cmd->stdout) free (exec_cmd->stdout); if (exec_cmd->stderr) @@ -417,9 +414,8 @@ exec_print_log () weechat_log_printf (" detached. . . . . . . . : %d", ptr_exec_cmd->detached); weechat_log_printf (" start_time. . . . . . . : %ld", ptr_exec_cmd->start_time); weechat_log_printf (" end_time. . . . . . . . : %ld", ptr_exec_cmd->end_time); - weechat_log_printf (" buffer_plugin . . . . . : '%s'", ptr_exec_cmd->buffer_plugin); - weechat_log_printf (" buffer_name . . . . . . : '%s'", ptr_exec_cmd->buffer_name); weechat_log_printf (" output_to_buffer. . . . : %d", ptr_exec_cmd->output_to_buffer); + weechat_log_printf (" buffer_full_name. . . . : '%s'", ptr_exec_cmd->buffer_full_name); weechat_log_printf (" stdout_size . . . . . . : %d", ptr_exec_cmd->stdout_size); weechat_log_printf (" stdout. . . . . . . . . : '%s'", ptr_exec_cmd->stdout); weechat_log_printf (" stderr_size . . . . . . : %d", ptr_exec_cmd->stderr_size); @@ -467,9 +463,7 @@ exec_debug_dump_cb (void *data, const char *signal, const char *type_data, int weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) { - /* make C compiler happy */ - (void) argc; - (void) argv; + int i, upgrading; weechat_plugin = plugin; @@ -486,6 +480,19 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) /* hook completions */ exec_completion_init (); + /* look at arguments */ + upgrading = 0; + for (i = 0; i < argc; i++) + { + if (weechat_strcasecmp (argv[i], "--upgrade") == 0) + { + upgrading = 1; + } + } + + if (upgrading) + exec_buffer_set_callbacks (); + return WEECHAT_RC_OK; } diff --git a/src/plugins/exec/exec.h b/src/plugins/exec/exec.h index 6d7c4f5fa..0b5d2f94c 100644 --- a/src/plugins/exec/exec.h +++ b/src/plugins/exec/exec.h @@ -38,9 +38,8 @@ struct t_exec_cmd time_t end_time; /* end time */ /* buffer */ - char *buffer_plugin; /* buffer plugin (where cmd is exec) */ - char *buffer_name; /* buffer name (where cmd is exec) */ int output_to_buffer; /* 1 if output is sent to buffer */ + char *buffer_full_name; /* buffer where output is displayed */ /* command output */ int stdout_size; /* number of bytes in stdout */ |