void fifo_exec (const char *text) { char *text2, *pos_msg; struct t_gui_buffer *ptr_buffer; text2 = strdup (text); if (!text2) return; pos_msg = NULL; ptr_buffer = NULL; /* * look for plugin + buffer name at beginning of text * text may be: "plugin.buffer *text" or "*text" */ if (text2[0] == '*') { pos_msg = text2 + 1; ptr_buffer = dogechat_current_buffer (); } else { pos_msg = strstr (text2, " *"); if (!pos_msg) { dogechat_printf (NULL, _("%s%s: invalid text received in pipe"), dogechat_prefix ("error"), FIFO_PLUGIN_NAME); free (text2); return; } pos_msg[0] = '\0'; pos_msg += 2; ptr_buffer = dogechat_buffer_search ("==", text2); if (!ptr_buffer) { dogechat_printf (NULL, _("%s%s: buffer \"%s\" not found"), dogechat_prefix ("error"), FIFO_PLUGIN_NAME, text2); free (text2); return; } } dogechat_command (ptr_buffer, pos_msg); free (text2); }
int exec_process_cb (void *data, const char *command, int return_code, const char *out, const char *err) { struct t_exec_cmd *ptr_exec_cmd; struct t_gui_buffer *ptr_buffer; /* make C compiler happy */ (void) command; ptr_exec_cmd = (struct t_exec_cmd *)data; if (!ptr_exec_cmd) return DOGECHAT_RC_ERROR; if (dogechat_exec_plugin->debug >= 2) { dogechat_printf (NULL, "%s: process_cb: command=\"%s\", rc=%d, " "out: %d bytes, err: %d bytes", EXEC_PLUGIN_NAME, ptr_exec_cmd->command, return_code, (out) ? strlen (out) : 0, (err) ? strlen (err) : 0); } if (out || err) { ptr_buffer = dogechat_buffer_search ("==", ptr_exec_cmd->buffer_full_name); if (out) exec_concat_output (ptr_exec_cmd, ptr_buffer, EXEC_STDOUT, out); if (err) exec_concat_output (ptr_exec_cmd, ptr_buffer, EXEC_STDERR, err); } if (return_code == DOGECHAT_HOOK_PROCESS_ERROR) exec_end_command (ptr_exec_cmd, -1); else if (return_code >= 0) exec_end_command (ptr_exec_cmd, return_code); return DOGECHAT_RC_OK; }
int exec_command_parse_options (struct t_exec_cmd_options *cmd_options, int argc, char **argv, int start_arg, int set_command_index) { int i, j, end, length, length_total; char *error; for (i = start_arg; i < argc; i++) { if (dogechat_strcasecmp (argv[i], "-sh") == 0) { cmd_options->use_shell = 1; } else if (dogechat_strcasecmp (argv[i], "-nosh") == 0) { cmd_options->use_shell = 0; } else if (dogechat_strcasecmp (argv[i], "-bg") == 0) { cmd_options->detached = 1; } else if (dogechat_strcasecmp (argv[i], "-nobg") == 0) { cmd_options->detached = 0; } else if (dogechat_strcasecmp (argv[i], "-stdin") == 0) { cmd_options->pipe_stdin = 1; } else if (dogechat_strcasecmp (argv[i], "-nostdin") == 0) { cmd_options->pipe_stdin = 0; } else if (dogechat_strcasecmp (argv[i], "-buffer") == 0) { if (i + 1 >= argc) return 0; i++; cmd_options->ptr_buffer_name = argv[i]; cmd_options->ptr_buffer = dogechat_buffer_search ("==", argv[i]); if (cmd_options->ptr_buffer && (dogechat_buffer_get_integer (cmd_options->ptr_buffer, "type") != 0)) { /* only a buffer with formatted content is allowed */ return 0; } if (!cmd_options->ptr_buffer) cmd_options->new_buffer = 1; } else if (dogechat_strcasecmp (argv[i], "-l") == 0) { cmd_options->output_to_buffer = 0; cmd_options->new_buffer = 0; } else if (dogechat_strcasecmp (argv[i], "-o") == 0) { cmd_options->output_to_buffer = 1; cmd_options->new_buffer = 0; } else if (dogechat_strcasecmp (argv[i], "-n") == 0) { cmd_options->output_to_buffer = 0; cmd_options->new_buffer = 1; } else if (dogechat_strcasecmp (argv[i], "-nf") == 0) { cmd_options->output_to_buffer = 0; cmd_options->new_buffer = 2; } else if (dogechat_strcasecmp (argv[i], "-cl") == 0) { cmd_options->new_buffer_clear = 1; } else if (dogechat_strcasecmp (argv[i], "-nocl") == 0) { cmd_options->new_buffer_clear = 0; } else if (dogechat_strcasecmp (argv[i], "-sw") == 0) { cmd_options->switch_to_buffer = 1; } else if (dogechat_strcasecmp (argv[i], "-nosw") == 0) { cmd_options->switch_to_buffer = 0; } else if (dogechat_strcasecmp (argv[i], "-ln") == 0) { cmd_options->line_numbers = 1; } else if (dogechat_strcasecmp (argv[i], "-noln") == 0) { cmd_options->line_numbers = 0; } else if (dogechat_strcasecmp (argv[i], "-flush") == 0) { cmd_options->flush = 1; } else if (dogechat_strcasecmp (argv[i], "-noflush") == 0) { cmd_options->flush = 0; } else if (dogechat_strcasecmp (argv[i], "-color") == 0) { if (i + 1 >= argc) return 0; i++; cmd_options->color = exec_search_color (argv[i]); if (cmd_options->color < 0) return 0; } else if (dogechat_strcasecmp (argv[i], "-rc") == 0) { cmd_options->display_rc = 1; } else if (dogechat_strcasecmp (argv[i], "-norc") == 0) { cmd_options->display_rc = 0; } else if (dogechat_strcasecmp (argv[i], "-timeout") == 0) { if (i + 1 >= argc) return 0; i++; error = NULL; cmd_options->timeout = strtol (argv[i], &error, 10); if (!error || error[0]) return 0; } else if (dogechat_strcasecmp (argv[i], "-name") == 0) { if (i + 1 >= argc) return 0; i++; cmd_options->ptr_command_name = argv[i]; } else if (dogechat_strcasecmp (argv[i], "-pipe") == 0) { if (i + 1 >= argc) return 0; i++; if (cmd_options->pipe_command) { free (cmd_options->pipe_command); cmd_options->pipe_command = NULL; } if (argv[i][0] == '"') { /* search the ending double quote */ length_total = 1; end = i; while (end < argc) { length = strlen (argv[end]); length_total += length + 1; if ((length > 0) && (argv[end][length - 1] == '"')) break; end++; } if (end == argc) return 0; cmd_options->pipe_command = malloc (length_total); if (!cmd_options->pipe_command) return 0; cmd_options->pipe_command[0] = '\0'; for (j = i; j <= end; j++) { if (cmd_options->pipe_command[0]) strcat (cmd_options->pipe_command, " "); strcat (cmd_options->pipe_command, (j == i) ? argv[j] + 1 : argv[j]); } length = strlen (cmd_options->pipe_command); if (length > 0) cmd_options->pipe_command[length - 1] = '\0'; i = end; } else cmd_options->pipe_command = strdup (argv[i]); } else if (dogechat_strcasecmp (argv[i], "-hsignal") == 0) { if (i + 1 >= argc) return 0; i++; if (cmd_options->hsignal) { free (cmd_options->hsignal); cmd_options->hsignal = NULL; } cmd_options->hsignal = strdup (argv[i]); } else { if (set_command_index) { cmd_options->command_index = i; break; } else return 0; } } return 1; }
void exec_end_command (struct t_exec_cmd *exec_cmd, int return_code) { struct t_gui_buffer *ptr_buffer; struct t_hashtable *hashtable; char str_number[32], *output; int i, buffer_type; if (exec_cmd->hsignal) { hashtable = dogechat_hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_STRING, NULL, NULL); if (hashtable) { dogechat_hashtable_set (hashtable, "command", exec_cmd->command); snprintf (str_number, sizeof (str_number), "%d", exec_cmd->number); dogechat_hashtable_set (hashtable, "number", str_number); dogechat_hashtable_set (hashtable, "name", exec_cmd->name); output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDOUT]); dogechat_hashtable_set (hashtable, "out", output); if (output) free (output); output = exec_decode_color (exec_cmd, exec_cmd->output[EXEC_STDERR]); dogechat_hashtable_set (hashtable, "err", output); if (output) free (output); snprintf (str_number, sizeof (str_number), "%d", return_code); dogechat_hashtable_set (hashtable, "rc", str_number); dogechat_hook_hsignal_send (exec_cmd->hsignal, hashtable); dogechat_hashtable_free (hashtable); } } else { ptr_buffer = dogechat_buffer_search ("==", exec_cmd->buffer_full_name); /* display the last line of output (if not ending with '\n') */ exec_display_line (exec_cmd, ptr_buffer, EXEC_STDOUT, exec_cmd->output[EXEC_STDOUT]); exec_display_line (exec_cmd, ptr_buffer, EXEC_STDERR, exec_cmd->output[EXEC_STDERR]); /* * display return code (only if command is not detached, if output is * NOT sent to buffer, and if command is not piped) */ if (exec_cmd->display_rc && !exec_cmd->detached && !exec_cmd->output_to_buffer && !exec_cmd->pipe_command) { buffer_type = dogechat_buffer_get_integer (ptr_buffer, "type"); if (return_code >= 0) { if (buffer_type == 1) { dogechat_printf_y (ptr_buffer, -1, ("%s: end of command %d (\"%s\"), " "return code: %d"), EXEC_PLUGIN_NAME, exec_cmd->number, exec_cmd->command, return_code); } else { dogechat_printf_tags (ptr_buffer, "exec_rc", _("%s: end of command %d (\"%s\"), " "return code: %d"), EXEC_PLUGIN_NAME, exec_cmd->number, exec_cmd->command, return_code); } } else { if (buffer_type == 1) { dogechat_printf_y (ptr_buffer, -1, _("%s: unexpected end of command %d " "(\"%s\")"), EXEC_PLUGIN_NAME, exec_cmd->number, exec_cmd->command); } else { dogechat_printf_tags (ptr_buffer, "exec_rc", _("%s: unexpected end of command %d " "(\"%s\")"), EXEC_PLUGIN_NAME, exec_cmd->number, exec_cmd->command); } } } } /* (re)set some variables after the end of command */ exec_cmd->hook = NULL; exec_cmd->pid = 0; exec_cmd->end_time = time (NULL); exec_cmd->return_code = return_code; for (i = 0; i < 2; i++) { if (exec_cmd->output[i]) { free (exec_cmd->output[i]); exec_cmd->output[i] = NULL; } exec_cmd->output_size[i] = 0; } /* schedule a timer to remove the executed command */ if (dogechat_config_integer (exec_config_command_purge_delay) >= 0) { dogechat_hook_timer (1 + (1000 * dogechat_config_integer (exec_config_command_purge_delay)), 0, 1, &exec_timer_delete_cb, exec_cmd); } }