2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto & The Sylpheed Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
34 #include <sys/types.h>
42 #include "manage_window.h"
43 #include "mainwindow.h"
44 #include "prefs_common.h"
45 #include "alertpanel.h"
46 #include "inputdialog.h"
53 typedef struct _Children Children;
54 typedef struct _ChildInfo ChildInfo;
55 typedef struct _UserStringDialog UserStringDialog;
61 GtkWidget *input_entry;
62 GtkWidget *input_hbox;
65 GtkWidget *scrolledwin;
68 ActionType action_type;
96 static void action_update_menu (GtkItemFactory *ifactory,
100 static void compose_actions_execute_cb (Compose *compose,
103 static void mainwin_actions_execute_cb (MainWindow *mainwin,
106 static void msgview_actions_execute_cb (MessageView *msgview,
109 static void message_actions_execute (MessageView *msgview,
113 static gboolean execute_actions (gchar *action,
120 static gchar *parse_action_cmd (gchar *action,
124 const gchar *user_str,
125 const gchar *user_hidden_str,
126 const gchar *sel_str);
127 static gboolean parse_append_filename (GString *cmd,
130 static gboolean parse_append_msgpart (GString *cmd,
134 static ChildInfo *fork_child (gchar *cmd,
135 const gchar *msg_str,
138 static gint wait_for_children (Children *children);
140 static void free_children (Children *children);
142 static void childinfo_close_pipes (ChildInfo *child_info);
144 static void create_io_dialog (Children *children);
145 static void update_io_dialog (Children *children);
147 static void hide_io_dialog_cb (GtkWidget *widget,
149 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
153 static void catch_output (gpointer data,
155 GdkInputCondition cond);
156 static void catch_input (gpointer data,
158 GdkInputCondition cond);
159 static void catch_status (gpointer data,
161 GdkInputCondition cond);
163 static gchar *get_user_string (const gchar *action,
167 ActionType action_get_type(const gchar *action_str)
170 ActionType action_type = ACTION_NONE;
172 g_return_val_if_fail(action_str, ACTION_ERROR);
173 g_return_val_if_fail(*action_str, ACTION_ERROR);
178 action_type |= ACTION_PIPE_IN;
180 } else if (p[0] == '>') {
181 action_type |= ACTION_USER_IN;
183 } else if (p[0] == '*') {
184 action_type |= ACTION_USER_HIDDEN_IN;
191 while (*p && action_type != ACTION_ERROR) {
195 action_type |= ACTION_SINGLE;
198 action_type |= ACTION_MULTIPLE;
201 action_type |= ACTION_SINGLE;
204 action_type |= ACTION_SELECTION_STR;
207 action_type |= ACTION_USER_STR;
210 action_type |= ACTION_USER_HIDDEN_STR;
213 action_type = ACTION_ERROR;
216 } else if (p[0] == '|') {
218 action_type |= ACTION_PIPE_OUT;
219 } else if (p[0] == '>') {
221 action_type |= ACTION_INSERT;
222 } else if (p[0] == '&') {
224 action_type |= ACTION_ASYNC;
232 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
233 GSList *msg_list, MimeInfo *partinfo,
234 const gchar *user_str,
235 const gchar *user_hidden_str,
236 const gchar *sel_str)
244 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
247 cmd = g_string_sized_new(strlen(action));
250 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
251 if (p[0] == '%' && p[1]) {
254 if (!parse_append_filename(cmd, msginfo)) {
255 g_string_free(cmd, TRUE);
261 for (cur = msg_list; cur != NULL;
263 MsgInfo *msg = (MsgInfo *)cur->data;
265 if (!parse_append_filename(cmd, msg)) {
266 g_string_free(cmd, TRUE);
270 g_string_append_c(cmd, ' ');
275 if (!parse_append_msgpart(cmd, msginfo,
277 g_string_free(cmd, TRUE);
284 g_string_append(cmd, sel_str);
289 g_string_append(cmd, user_str);
294 g_string_append(cmd, user_hidden_str);
298 g_string_append_c(cmd, p[0]);
299 g_string_append_c(cmd, p[1]);
303 g_string_append_c(cmd, p[0]);
308 g_string_free(cmd, TRUE);
313 g_string_free(cmd, FALSE);
317 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
321 g_return_val_if_fail(msginfo, FALSE);
323 filename = procmsg_get_message_file(msginfo);
326 g_string_append(cmd, filename);
329 alertpanel_error(_("Could not get message file %d"),
337 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
340 gboolean single_part = FALSE;
342 gchar *part_filename;
346 partinfo = procmime_scan_message(msginfo);
348 alertpanel_error(_("Could not get message part."));
355 filename = procmsg_get_message_file_path(msginfo);
356 part_filename = procmime_get_tmp_file_name(partinfo);
358 ret = procmime_get_part(part_filename, filename, partinfo);
361 procmime_mimeinfo_free_all(partinfo);
365 alertpanel_error(_("Can't get part of multipart message"));
366 g_free(part_filename);
370 g_string_append(cmd, part_filename);
372 g_free(part_filename);
377 void actions_execute(gpointer data,
382 if (source == TOOLBAR_MAIN)
383 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
384 else if (source == TOOLBAR_COMPOSE)
385 compose_actions_execute_cb((Compose*)data, action_nb, widget);
386 else if (source == TOOLBAR_MSGVIEW)
387 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
390 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
392 action_update_menu(ifactory, "/Tools/Actions",
393 mainwin_actions_execute_cb, mainwin);
396 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
398 action_update_menu(ifactory, "/Tools/Actions",
399 msgview_actions_execute_cb, msgview);
402 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
404 action_update_menu(ifactory, "/Tools/Actions",
405 compose_actions_execute_cb, compose);
408 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
409 gpointer callback, gpointer data)
414 gchar *action, *action_p;
416 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
418 ifentry.path = branch_path;
419 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
420 g_return_if_fail(menuitem != NULL);
422 amenu = GTK_MENU_SHELL(menuitem)->children;
423 while (amenu != NULL) {
424 GList *alist = amenu->next;
425 gtk_widget_destroy(GTK_WIDGET(amenu->data));
429 ifentry.accelerator = NULL;
430 ifentry.callback_action = 0;
431 ifentry.callback = callback;
432 ifentry.item_type = NULL;
434 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
435 action = g_strdup((gchar *)cur->data);
436 action_p = strstr(action, ": ");
437 if (action_p && action_p[2] &&
438 action_get_type(&action_p[2]) != ACTION_ERROR) {
440 menu_path = g_strdup_printf("%s/%s", branch_path,
442 ifentry.path = menu_path;
443 gtk_item_factory_create_item(ifactory, &ifentry, data,
448 ifentry.callback_action++;
452 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
456 ActionType action_type;
458 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
460 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
461 g_return_if_fail(buf != NULL);
462 action = strstr(buf, ": ");
463 g_return_if_fail(action != NULL);
465 /* Point to the beginning of the command-line */
468 action_type = action_get_type(action);
469 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
471 (_("The selected action cannot be used in the compose window\n"
472 "because it contains %%f, %%F or %%p."));
476 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
479 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
484 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
485 message_actions_execute(mainwin->messageview, action_nb, msg_list);
486 g_slist_free(msg_list);
489 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
492 GSList *msg_list = NULL;
494 if (msgview->msginfo)
495 msg_list = g_slist_append(msg_list, msgview->msginfo);
496 message_actions_execute(msgview, action_nb, msg_list);
497 g_slist_free(msg_list);
500 static void message_actions_execute(MessageView *msgview, guint action_nb,
507 GtkWidget *text = NULL;
508 GdkFont *msgfont = NULL;
511 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
513 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
515 g_return_if_fail(buf);
516 g_return_if_fail(action = strstr(buf, ": "));
518 /* Point to the beginning of the command-line */
521 textview = messageview_get_current_textview(msgview);
523 text = textview->text;
524 msgfont = textview->msgfont;
525 body_pos = textview->body_pos;
527 partinfo = messageview_get_selected_mime_part(msgview);
529 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
532 static gboolean execute_actions(gchar *action, GSList *msg_list,
533 GtkWidget *text, GdkFont *msgfont,
534 gint body_pos, MimeInfo *partinfo)
536 GSList *children_list = NULL;
540 ChildInfo *child_info;
541 ActionType action_type;
544 guint start = 0, end = 0;
545 gchar *sel_str = NULL;
546 gchar *msg_str = NULL;
547 gchar *user_str = NULL;
548 gchar *user_hidden_str = NULL;
550 g_return_val_if_fail(action && *action, FALSE);
552 action_type = action_get_type(action);
554 if (action_type == ACTION_ERROR)
555 return FALSE; /* ERR: syntax error */
557 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
558 return FALSE; /* ERR: file command without selection */
560 msg_list_len = g_slist_length(msg_list);
562 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
563 if (msg_list_len > 1)
564 return FALSE; /* ERR: pipe + multiple selection */
566 return FALSE; /* ERR: pipe and no displayed text */
569 if (action_type & ACTION_SELECTION_STR) {
571 return FALSE; /* ERR: selection string but no text */
575 if (GTK_EDITABLE(text)->has_selection) {
576 start = GTK_EDITABLE(text)->selection_start_pos;
577 end = GTK_EDITABLE(text)->selection_end_pos;
587 end = gtk_stext_get_length(GTK_STEXT(text));
588 msg_str = gtk_editable_get_chars
589 (GTK_EDITABLE(text), start, end);
591 sel_str = gtk_editable_get_chars
592 (GTK_EDITABLE(text), start, end);
593 msg_str = g_strdup(sel_str);
597 end = gtk_stext_get_length(GTK_STEXT(text));
598 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
603 if (action_type & ACTION_USER_STR) {
604 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
611 if (action_type & ACTION_USER_HIDDEN_STR) {
612 if (!(user_hidden_str =
613 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
621 if (action_type & ACTION_PIPE_OUT) {
622 gtk_stext_freeze(GTK_STEXT(text));
623 gtk_stext_set_point(GTK_STEXT(text), start);
624 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
625 gtk_stext_thaw(GTK_STEXT(text));
628 children = g_new0(Children, 1);
630 children->action = g_strdup(action);
631 children->action_type = action_type;
632 children->msg_text = text;
633 children->msgfont = msgfont;
635 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
636 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
637 children->open_in = 1;
639 if (action_type & ACTION_SINGLE) {
642 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
643 msginfo = (MsgInfo *)cur->data;
645 is_ok = FALSE; /* ERR: msginfo missing */
648 cmd = parse_action_cmd(action, msginfo, msg_list,
650 user_hidden_str, sel_str);
652 debug_print("Action command error\n");
653 is_ok = FALSE; /* ERR: incorrect command */
656 if ((child_info = fork_child(cmd, msg_str, children))) {
657 children_list = g_slist_append(children_list,
663 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
664 user_str, user_hidden_str, sel_str);
666 if ((child_info = fork_child(cmd, msg_str, children))) {
667 children_list = g_slist_append(children_list,
672 is_ok = FALSE; /* ERR: incorrect command */
678 g_free(user_hidden_str);
680 if (!children_list) {
681 /* If not waiting for children, return */
682 free_children(children);
686 children->list = children_list;
687 children->nb = g_slist_length(children_list);
689 for (cur = children_list; cur; cur = cur->next) {
690 child_info = (ChildInfo *) cur->data;
691 child_info->tag_status =
692 gdk_input_add(child_info->chld_status,
694 catch_status, child_info);
697 create_io_dialog(children);
703 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
706 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
709 ChildInfo *child_info;
712 sync = !(children->action_type & ACTION_ASYNC);
714 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
715 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
718 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
720 alertpanel_error(_("Command could not be started. "
721 "Pipe creation failed.\n%s"),
723 /* Closing fd = -1 fails silently */
730 close(chld_status[0]);
731 close(chld_status[1]);
732 return NULL; /* Pipe error */
736 debug_print("Forking child and grandchild.\n");
739 if (pid == 0) { /* Child */
743 close(ConnectionNumber(gdk_display));
748 if (setpgid(0, getppid()))
752 if (children->action_type &
755 ACTION_USER_HIDDEN_IN)) {
756 close(fileno(stdin));
762 close(fileno(stdout));
767 close(fileno(stderr));
777 execvp("/bin/sh", cmdline);
781 } else if (gch_pid < (pid_t) 0) { /* Fork error */
783 write(chld_status[1], "1\n", 2);
794 close(chld_status[0]);
796 debug_print("Child: Waiting for grandchild\n");
797 waitpid(gch_pid, NULL, 0);
798 debug_print("Child: grandchild ended\n");
799 write(chld_status[1], "0\n", 2);
800 close(chld_status[1]);
804 } else if (pid < 0) { /* Fork error */
805 alertpanel_error(_("Could not fork to execute the following "
807 cmd, g_strerror(errno));
814 waitpid(pid, NULL, 0);
819 if (!(children->action_type &
820 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
824 close(chld_status[1]);
826 child_info = g_new0(ChildInfo, 1);
828 child_info->children = children;
830 child_info->pid = pid;
831 child_info->cmd = g_strdup(cmd);
832 child_info->new_out = FALSE;
833 child_info->output = g_string_new(NULL);
834 child_info->chld_in =
835 (children->action_type &
836 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
838 child_info->chld_out = chld_out[0];
839 child_info->chld_err = chld_err[0];
840 child_info->chld_status = chld_status[0];
841 child_info->tag_in = -1;
842 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
843 catch_output, child_info);
844 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
845 catch_output, child_info);
847 if (!(children->action_type &
848 (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
851 if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
852 write(chld_in[1], msg_str, strlen(msg_str));
853 if (!(children->action_type &
854 (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
856 child_info->chld_in = -1; /* No more input */
862 static void kill_children_cb(GtkWidget *widget, gpointer data)
865 Children *children = (Children *) data;
866 ChildInfo *child_info;
868 for (cur = children->list; cur; cur = cur->next) {
869 child_info = (ChildInfo *)(cur->data);
870 debug_print("Killing child group id %d\n", child_info->pid);
871 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
876 static gint wait_for_children(Children *children)
879 ChildInfo *child_info;
881 gint nb = children->nb;
885 cur = children->list;
888 child_info = (ChildInfo *)cur->data;
891 new_output |= child_info->new_out;
895 children->output |= new_output;
897 if (new_output || (children->dialog && (nb != children->nb)))
898 update_io_dialog(children);
903 if (!children->dialog) {
904 free_children(children);
905 } else if (!children->output) {
906 gtk_widget_destroy(children->dialog);
912 static void send_input(GtkWidget *w, gpointer data)
914 Children *children = (Children *) data;
915 ChildInfo *child_info = (ChildInfo *) children->list->data;
917 child_info->tag_in = gdk_input_add(child_info->chld_in,
919 catch_input, children);
920 gtk_widget_set_sensitive(children->input_hbox, FALSE);
923 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
925 hide_io_dialog_cb(w, data);
929 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
932 Children *children = (Children *)data;
935 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
937 gtk_widget_destroy(children->dialog);
938 free_children(children);
942 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
945 if (event && event->keyval == GDK_Escape)
946 hide_io_dialog_cb(widget, data);
950 static void childinfo_close_pipes(ChildInfo *child_info)
952 if (child_info->tag_in > 0)
953 gdk_input_remove(child_info->tag_in);
954 gdk_input_remove(child_info->tag_out);
955 gdk_input_remove(child_info->tag_err);
957 if (child_info->chld_in >= 0)
958 close(child_info->chld_in);
959 close(child_info->chld_out);
960 close(child_info->chld_err);
961 close(child_info->chld_status);
964 static void free_children(Children *children)
966 ChildInfo *child_info;
968 debug_print("Freeing children data %p\n", children);
970 g_free(children->action);
971 while (children->list != NULL) {
972 child_info = (ChildInfo *)children->list->data;
973 g_free(child_info->cmd);
974 g_string_free(child_info->output, TRUE);
975 children->list = g_slist_remove(children->list, child_info);
981 static void update_io_dialog(Children *children)
985 debug_print("Updating actions input/output dialog.\n");
988 gtk_widget_set_sensitive(children->abort_btn, FALSE);
989 gtk_widget_set_sensitive(children->close_btn, TRUE);
990 if (children->input_hbox)
991 gtk_widget_set_sensitive(children->input_hbox, FALSE);
992 gtk_widget_grab_focus(children->close_btn);
993 gtk_signal_connect(GTK_OBJECT(children->dialog),
995 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
999 if (children->output) {
1000 GtkWidget *text = children->text;
1002 ChildInfo *child_info;
1004 gtk_widget_show(children->scrolledwin);
1005 gtk_text_freeze(GTK_TEXT(text));
1006 gtk_text_set_point(GTK_TEXT(text), 0);
1007 gtk_text_forward_delete(GTK_TEXT(text),
1008 gtk_text_get_length(GTK_TEXT(text)));
1009 for (cur = children->list; cur; cur = cur->next) {
1010 child_info = (ChildInfo *)cur->data;
1011 if (child_info->pid)
1012 caption = g_strdup_printf
1013 (_("--- Running: %s\n"),
1016 caption = g_strdup_printf
1017 (_("--- Ended: %s\n"),
1020 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1022 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1023 child_info->output->str, -1);
1025 child_info->new_out = FALSE;
1027 gtk_text_thaw(GTK_TEXT(text));
1031 static void create_io_dialog(Children *children)
1035 GtkWidget *entry = NULL;
1036 GtkWidget *input_hbox = NULL;
1037 GtkWidget *send_button;
1040 GtkWidget *scrolledwin;
1042 GtkWidget *abort_button;
1043 GtkWidget *close_button;
1045 debug_print("Creating action IO dialog\n");
1047 dialog = gtk_dialog_new();
1048 gtk_container_set_border_width
1049 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1050 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1051 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1052 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1053 manage_window_set_transient(GTK_WINDOW(dialog));
1054 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1055 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1056 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1057 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1060 vbox = gtk_vbox_new(FALSE, 8);
1061 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1062 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1063 gtk_widget_show(vbox);
1065 label = gtk_label_new(children->action);
1066 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1067 gtk_widget_show(label);
1069 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1070 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1071 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1072 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1073 gtk_widget_set_usize(scrolledwin, 480, 200);
1074 gtk_widget_hide(scrolledwin);
1076 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1077 (GTK_SCROLLED_WINDOW(scrolledwin)),
1078 gtk_scrolled_window_get_vadjustment
1079 (GTK_SCROLLED_WINDOW(scrolledwin)));
1080 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1081 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1082 gtk_widget_show(text);
1084 if (children->open_in) {
1085 input_hbox = gtk_hbox_new(FALSE, 8);
1086 gtk_widget_show(input_hbox);
1088 entry = gtk_entry_new();
1089 gtk_widget_set_usize(entry, 320, -1);
1090 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1091 GTK_SIGNAL_FUNC(send_input), children);
1092 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1093 if (children->action_type & ACTION_USER_HIDDEN_IN)
1094 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1095 gtk_widget_show(entry);
1097 send_button = gtk_button_new_with_label(_(" Send "));
1098 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1099 GTK_SIGNAL_FUNC(send_input), children);
1100 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1102 gtk_widget_show(send_button);
1104 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1105 gtk_widget_grab_focus(entry);
1108 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1109 &close_button, _("Close"), NULL, NULL);
1110 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1111 GTK_SIGNAL_FUNC(kill_children_cb), children);
1112 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1113 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1114 gtk_widget_show(hbox);
1117 gtk_widget_set_sensitive(close_button, FALSE);
1119 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1121 children->dialog = dialog;
1122 children->scrolledwin = scrolledwin;
1123 children->text = text;
1124 children->input_hbox = children->open_in ? input_hbox : NULL;
1125 children->input_entry = children->open_in ? entry : NULL;
1126 children->abort_btn = abort_button;
1127 children->close_btn = close_button;
1129 gtk_widget_show(dialog);
1132 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1134 ChildInfo *child_info = (ChildInfo *)data;
1138 gdk_input_remove(child_info->tag_status);
1140 c = read(source, &buf, 1);
1141 debug_print("Child returned %c\n", buf);
1143 waitpid(-child_info->pid, NULL, 0);
1144 childinfo_close_pipes(child_info);
1145 child_info->pid = 0;
1147 wait_for_children(child_info->children);
1150 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1152 Children *children = (Children *)data;
1153 ChildInfo *child_info = (ChildInfo *)children->list->data;
1157 debug_print("Sending input to grand child.\n");
1158 if (!(cond && GDK_INPUT_WRITE))
1161 gdk_input_remove(child_info->tag_in);
1162 child_info->tag_in = -1;
1164 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1166 len = strlen(input);
1170 c = write(child_info->chld_in, input + count, len - count);
1173 } while (c >= 0 && count < len);
1176 write(child_info->chld_in, "\n", 2);
1180 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1181 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1182 close(child_info->chld_in);
1183 child_info->chld_in = -1;
1184 debug_print("Input to grand child sent.\n");
1187 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1189 ChildInfo *child_info = (ChildInfo *)data;
1191 gchar buf[BUFFSIZE];
1193 debug_print("Catching grand child's output.\n");
1194 if (child_info->children->action_type &
1195 (ACTION_PIPE_OUT | ACTION_INSERT)
1196 && source == child_info->chld_out) {
1197 gboolean is_selection = FALSE;
1198 GtkWidget *text = child_info->children->msg_text;
1200 if (GTK_EDITABLE(text)->has_selection)
1201 is_selection = TRUE;
1202 gtk_stext_freeze(GTK_STEXT(text));
1204 c = read(source, buf, sizeof(buf) - 1);
1207 gtk_stext_insert(GTK_STEXT(text),
1208 child_info->children->msgfont,
1209 NULL, NULL, buf, c);
1212 /* Using the select_region draws things. Should not.
1213 * so we just change selection position and
1214 * defere drawing when thawing. Hack?
1216 GTK_EDITABLE(text)->selection_end_pos =
1217 gtk_stext_get_point(GTK_STEXT(text));
1219 gtk_stext_thaw(GTK_STEXT(text));
1221 c = read(source, buf, sizeof(buf) - 1);
1222 for (i = 0; i < c; i++)
1223 g_string_append_c(child_info->output, buf[i]);
1225 child_info->new_out = TRUE;
1227 wait_for_children(child_info->children);
1230 static gchar *get_user_string(const gchar *action, ActionType type)
1233 gchar *user_str = NULL;
1236 case ACTION_USER_HIDDEN_STR:
1237 message = g_strdup_printf
1238 (_("Enter the argument for the following action:\n"
1239 "(`%%h' will be replaced with the argument)\n"
1242 user_str = input_dialog_with_invisible
1243 (_("Action's hidden user argument"), message, NULL);
1245 case ACTION_USER_STR:
1246 message = g_strdup_printf
1247 (_("Enter the argument for the following action:\n"
1248 "(`%%u' will be replaced with the argument)\n"
1251 user_str = input_dialog
1252 (_("Action's user argument"), message, NULL);
1255 g_warning("Unsupported action type %d", type);