2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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;
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,
136 const gchar *msg_str,
139 static gint wait_for_children (Children *children);
141 static void free_children (Children *children);
143 static void childinfo_close_pipes (ChildInfo *child_info);
145 static void create_io_dialog (Children *children);
146 static void update_io_dialog (Children *children);
148 static void hide_io_dialog_cb (GtkWidget *widget,
150 static gint io_dialog_key_pressed_cb (GtkWidget *widget,
154 static void catch_output (gpointer data,
156 GdkInputCondition cond);
157 static void catch_input (gpointer data,
159 GdkInputCondition cond);
160 static void catch_status (gpointer data,
162 GdkInputCondition cond);
164 static gchar *get_user_string (const gchar *action,
168 ActionType action_get_type(const gchar *action_str)
171 ActionType action_type = ACTION_NONE;
173 g_return_val_if_fail(action_str, ACTION_ERROR);
174 g_return_val_if_fail(*action_str, ACTION_ERROR);
179 action_type |= ACTION_PIPE_IN;
181 } else if (p[0] == '>') {
182 action_type |= ACTION_USER_IN;
184 } else if (p[0] == '*') {
185 action_type |= ACTION_USER_HIDDEN_IN;
192 while (*p && action_type != ACTION_ERROR) {
196 action_type |= ACTION_SINGLE;
199 action_type |= ACTION_MULTIPLE;
202 action_type |= ACTION_SINGLE;
205 action_type |= ACTION_SELECTION_STR;
208 action_type |= ACTION_USER_STR;
211 action_type |= ACTION_USER_HIDDEN_STR;
214 action_type = ACTION_ERROR;
217 } else if (p[0] == '|') {
219 action_type |= ACTION_PIPE_OUT;
220 } else if (p[0] == '>') {
222 action_type |= ACTION_INSERT;
223 } else if (p[0] == '&') {
225 action_type |= ACTION_ASYNC;
233 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
234 GSList *msg_list, MimeInfo *partinfo,
235 const gchar *user_str,
236 const gchar *user_hidden_str,
237 const gchar *sel_str)
245 if (p[0] == '|' || p[0] == '>' || p[0] == '*')
248 cmd = g_string_sized_new(strlen(action));
251 !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
252 if (p[0] == '%' && p[1]) {
255 if (!parse_append_filename(cmd, msginfo)) {
256 g_string_free(cmd, TRUE);
262 for (cur = msg_list; cur != NULL;
264 MsgInfo *msg = (MsgInfo *)cur->data;
266 if (!parse_append_filename(cmd, msg)) {
267 g_string_free(cmd, TRUE);
271 g_string_append_c(cmd, ' ');
276 if (!parse_append_msgpart(cmd, msginfo,
278 g_string_free(cmd, TRUE);
285 g_string_append(cmd, sel_str);
290 g_string_append(cmd, user_str);
295 g_string_append(cmd, user_hidden_str);
299 g_string_append_c(cmd, p[0]);
300 g_string_append_c(cmd, p[1]);
304 g_string_append_c(cmd, p[0]);
309 g_string_free(cmd, TRUE);
314 g_string_free(cmd, FALSE);
318 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
322 g_return_val_if_fail(msginfo, FALSE);
324 filename = procmsg_get_message_file(msginfo);
327 g_string_append(cmd, filename);
330 alertpanel_error(_("Could not get message file %d"),
338 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
341 gboolean single_part = FALSE;
343 gchar *part_filename;
349 if ((fp = procmsg_open_message_decrypted(msginfo, &partinfo))
351 alertpanel_error(_("Could not get message file."));
355 if ((fp = procmsg_open_message(msginfo)) == NULL) {
356 alertpanel_error(_("Could not get message file."));
359 partinfo = procmime_scan_mime_header(fp);
363 procmime_mimeinfo_free_all(partinfo);
364 alertpanel_error(_("Could not get message part."));
371 filename = procmsg_get_message_file_path(msginfo);
372 part_filename = procmime_get_tmp_file_name(partinfo);
374 ret = procmime_get_part(part_filename, filename, partinfo);
377 procmime_mimeinfo_free_all(partinfo);
381 alertpanel_error(_("Can't get part of multipart message"));
382 g_free(part_filename);
386 g_string_append(cmd, part_filename);
388 g_free(part_filename);
393 void actions_execute(gpointer data,
398 if (source == TOOLBAR_MAIN)
399 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
400 else if (source == TOOLBAR_COMPOSE)
401 compose_actions_execute_cb((Compose*)data, action_nb, widget);
402 else if (source == TOOLBAR_MSGVIEW)
403 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);
406 void action_update_mainwin_menu(GtkItemFactory *ifactory, MainWindow *mainwin)
408 action_update_menu(ifactory, "/Tools/Actions",
409 mainwin_actions_execute_cb, mainwin);
412 void action_update_msgview_menu(GtkItemFactory *ifactory, MessageView *msgview)
414 action_update_menu(ifactory, "/Tools/Actions",
415 msgview_actions_execute_cb, msgview);
418 void action_update_compose_menu(GtkItemFactory *ifactory, Compose *compose)
420 action_update_menu(ifactory, "/Tools/Actions",
421 compose_actions_execute_cb, compose);
424 static void action_update_menu(GtkItemFactory *ifactory, gchar *branch_path,
425 gpointer callback, gpointer data)
430 gchar *action, *action_p;
432 GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
434 ifentry.path = branch_path;
435 menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
436 g_return_if_fail(menuitem != NULL);
438 amenu = GTK_MENU_SHELL(menuitem)->children;
439 while (amenu != NULL) {
440 GList *alist = amenu->next;
441 gtk_widget_destroy(GTK_WIDGET(amenu->data));
445 ifentry.accelerator = NULL;
446 ifentry.callback_action = 0;
447 ifentry.callback = callback;
448 ifentry.item_type = NULL;
450 for (cur = prefs_common.actions_list; cur; cur = cur->next) {
451 action = g_strdup((gchar *)cur->data);
452 action_p = strstr(action, ": ");
453 if (action_p && action_p[2] &&
454 action_get_type(&action_p[2]) != ACTION_ERROR) {
456 menu_path = g_strdup_printf("%s/%s", branch_path,
458 ifentry.path = menu_path;
459 gtk_item_factory_create_item(ifactory, &ifentry, data,
464 ifentry.callback_action++;
468 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
474 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
476 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
477 g_return_if_fail(buf != NULL);
478 action = strstr(buf, ": ");
479 g_return_if_fail(action != NULL);
481 /* Point to the beginning of the command-line */
484 action_type = action_get_type(action);
485 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
487 (_("The selected action cannot be used in the compose window\n"
488 "because it contains %%f, %%F or %%p."));
492 execute_actions(action, NULL, compose->text, NULL, 0, NULL);
495 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
500 msg_list = summary_get_selected_msg_list(mainwin->summaryview);
501 message_actions_execute(mainwin->messageview, action_nb, msg_list);
502 g_slist_free(msg_list);
505 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
508 GSList *msg_list = NULL;
510 if (msgview->msginfo)
511 msg_list = g_slist_append(msg_list, msgview->msginfo);
512 message_actions_execute(msgview, action_nb, msg_list);
513 g_slist_free(msg_list);
516 static void message_actions_execute(MessageView *msgview, guint action_nb,
523 GtkWidget *text = NULL;
524 GdkFont *msgfont = NULL;
527 g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
529 buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
531 g_return_if_fail(buf);
532 g_return_if_fail(action = strstr(buf, ": "));
534 /* Point to the beginning of the command-line */
537 textview = messageview_get_current_textview(msgview);
539 text = textview->text;
540 msgfont = textview->msgfont;
541 body_pos = textview->body_pos;
543 partinfo = messageview_get_selected_mime_part(msgview);
545 execute_actions(action, msg_list, text, msgfont, body_pos, partinfo);
548 static gboolean execute_actions(gchar *action, GSList *msg_list,
549 GtkWidget *text, GdkFont *msgfont,
550 gint body_pos, MimeInfo *partinfo)
552 GSList *children_list = NULL;
556 ChildInfo *child_info;
560 guint start = 0, end = 0;
561 gchar *sel_str = NULL;
562 gchar *msg_str = NULL;
563 gchar *user_str = NULL;
564 gchar *user_hidden_str = NULL;
566 g_return_val_if_fail(action && *action, FALSE);
568 action_type = action_get_type(action);
570 if (action_type == ACTION_ERROR)
571 return FALSE; /* ERR: syntax error */
573 if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
574 return FALSE; /* ERR: file command without selection */
576 msg_list_len = g_slist_length(msg_list);
578 if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
579 if (msg_list_len > 1)
580 return FALSE; /* ERR: pipe + multiple selection */
582 return FALSE; /* ERR: pipe and no displayed text */
585 if (action_type & ACTION_SELECTION_STR) {
587 return FALSE; /* ERR: selection string but no text */
591 start = GTK_EDITABLE(text)->selection_start_pos;
592 end = GTK_EDITABLE(text)->selection_end_pos;
602 end = gtk_stext_get_length(GTK_STEXT(text));
603 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
606 sel_str = gtk_editable_get_chars(GTK_EDITABLE(text),
608 msg_str = g_strdup(sel_str);
612 end = gtk_stext_get_length(GTK_STEXT(text));
613 msg_str = gtk_editable_get_chars(GTK_EDITABLE(text),
617 if (action_type & ACTION_USER_STR) {
618 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
625 if (action_type & ACTION_USER_HIDDEN_STR) {
626 if (!(user_hidden_str =
627 get_user_string(action, ACTION_USER_HIDDEN_STR))) {
635 if (action_type & ACTION_PIPE_OUT) {
636 gtk_stext_freeze(GTK_STEXT(text));
637 gtk_stext_set_point(GTK_STEXT(text), start);
638 gtk_stext_forward_delete(GTK_STEXT(text), end - start);
639 gtk_stext_thaw(GTK_STEXT(text));
642 children = g_new0(Children, 1);
644 children->msg_text = text;
645 children->msgfont = msgfont;
647 if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
648 ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
649 children->open_in = 1;
651 if (action_type & ACTION_SINGLE) {
654 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
655 msginfo = (MsgInfo *)cur->data;
657 is_ok = FALSE; /* ERR: msginfo missing */
660 cmd = parse_action_cmd(action, msginfo, msg_list,
662 user_hidden_str, sel_str);
664 debug_print("Action command error\n");
665 is_ok = FALSE; /* ERR: incorrect command */
668 if ((child_info = fork_child(cmd, action_type, msg_str,
670 children_list = g_slist_append(children_list,
676 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
677 user_str, user_hidden_str, sel_str);
679 if ((child_info = fork_child(cmd, action_type, msg_str,
681 children_list = g_slist_append(children_list,
686 is_ok = FALSE; /* ERR: incorrect command */
692 g_free(user_hidden_str);
694 if (!children_list) {
695 /* If not waiting for children, return */
700 children->action = g_strdup(action);
701 children->dialog = NULL;
702 children->list = children_list;
703 children->nb = g_slist_length(children_list);
705 for (cur = children_list; cur; cur = cur->next) {
706 child_info = (ChildInfo *) cur->data;
707 child_info->tag_status =
708 gdk_input_add(child_info->chld_status,
710 catch_status, child_info);
713 create_io_dialog(children);
719 static ChildInfo *fork_child(gchar *cmd, gint action_type,
720 const gchar *msg_str, Children *children)
722 gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
725 ChildInfo *child_info;
728 sync = !(action_type & ACTION_ASYNC);
730 chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
731 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
734 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
736 alertpanel_error(_("Command could not be started. "
737 "Pipe creation failed.\n%s"),
739 /* Closing fd = -1 fails silently */
746 close(chld_status[0]);
747 close(chld_status[1]);
748 return NULL; /* Pipe error */
752 debug_print("Forking child and grandchild.\n");
755 if (pid == 0) { /* Child */
759 close(ConnectionNumber(gdk_display));
764 if (setpgid(0, getppid()))
771 ACTION_USER_HIDDEN_IN)) {
772 close(fileno(stdin));
778 close(fileno(stdout));
783 close(fileno(stderr));
793 execvp("/bin/sh", cmdline);
797 } else if (gch_pid < (pid_t) 0) { /* Fork error */
799 write(chld_status[1], "1\n", 2);
810 close(chld_status[0]);
812 debug_print("Child: Waiting for grandchild\n");
813 waitpid(gch_pid, NULL, 0);
814 debug_print("Child: grandchild ended\n");
815 write(chld_status[1], "0\n", 2);
816 close(chld_status[1]);
820 } else if (pid < 0) { /* Fork error */
821 alertpanel_error(_("Could not fork to execute the following "
823 cmd, g_strerror(errno));
830 waitpid(pid, NULL, 0);
836 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
840 close(chld_status[1]);
842 child_info = g_new0(ChildInfo, 1);
844 child_info->children = children;
846 child_info->pid = pid;
847 child_info->cmd = g_strdup(cmd);
848 child_info->type = action_type;
849 child_info->new_out = FALSE;
850 child_info->output = g_string_new(NULL);
851 child_info->chld_in =
853 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
855 child_info->chld_out = chld_out[0];
856 child_info->chld_err = chld_err[0];
857 child_info->chld_status = chld_status[0];
858 child_info->tag_in = -1;
859 child_info->tag_out = gdk_input_add(chld_out[0], GDK_INPUT_READ,
860 catch_output, child_info);
861 child_info->tag_err = gdk_input_add(chld_err[0], GDK_INPUT_READ,
862 catch_output, child_info);
864 if (!(action_type & (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
867 if ((action_type & ACTION_PIPE_IN) && msg_str) {
868 write(chld_in[1], msg_str, strlen(msg_str));
869 if (!(action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
871 child_info->chld_in = -1; /* No more input */
877 static void kill_children_cb(GtkWidget *widget, gpointer data)
880 Children *children = (Children *) data;
881 ChildInfo *child_info;
883 for (cur = children->list; cur; cur = cur->next) {
884 child_info = (ChildInfo *)(cur->data);
885 debug_print("Killing child group id %d\n", child_info->pid);
886 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
891 static gint wait_for_children(Children *children)
894 ChildInfo *child_info;
896 gint nb = children->nb;
900 cur = children->list;
903 child_info = (ChildInfo *)cur->data;
906 new_output |= child_info->new_out;
910 children->output |= new_output;
912 if (new_output || (children->dialog && (nb != children->nb)))
913 update_io_dialog(children);
918 if (!children->dialog) {
919 free_children(children);
920 } else if (!children->output) {
921 gtk_widget_destroy(children->dialog);
927 static void send_input(GtkWidget *w, gpointer data)
929 Children *children = (Children *) data;
930 ChildInfo *child_info = (ChildInfo *) children->list->data;
932 child_info->tag_in = gdk_input_add(child_info->chld_in,
934 catch_input, children);
935 gtk_widget_set_sensitive(children->input_hbox, FALSE);
938 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
940 hide_io_dialog_cb(w, data);
944 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
947 Children *children = (Children *)data;
950 gtk_signal_disconnect_by_data(GTK_OBJECT(children->dialog),
952 gtk_widget_destroy(children->dialog);
953 free_children(children);
957 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
960 if (event && event->keyval == GDK_Escape)
961 hide_io_dialog_cb(widget, data);
965 static void childinfo_close_pipes(ChildInfo *child_info)
967 if (child_info->tag_in > 0)
968 gdk_input_remove(child_info->tag_in);
969 gdk_input_remove(child_info->tag_out);
970 gdk_input_remove(child_info->tag_err);
972 if (child_info->chld_in >= 0)
973 close(child_info->chld_in);
974 close(child_info->chld_out);
975 close(child_info->chld_err);
976 close(child_info->chld_status);
979 static void free_children(Children *children)
982 ChildInfo *child_info;
984 debug_print("Freeing children data %p\n", children);
986 g_free(children->action);
987 for (cur = children->list; cur;) {
988 child_info = (ChildInfo *)cur->data;
989 g_free(child_info->cmd);
990 g_string_free(child_info->output, TRUE);
991 children->list = g_slist_remove(children->list, child_info);
993 cur = children->list;
998 static void update_io_dialog(Children *children)
1002 debug_print("Updating actions input/output dialog.\n");
1004 if (!children->nb) {
1005 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1006 gtk_widget_set_sensitive(children->close_btn, TRUE);
1007 if (children->input_hbox)
1008 gtk_widget_set_sensitive(children->input_hbox, FALSE);
1009 gtk_widget_grab_focus(children->close_btn);
1010 gtk_signal_connect(GTK_OBJECT(children->dialog),
1012 GTK_SIGNAL_FUNC(io_dialog_key_pressed_cb),
1016 if (children->output) {
1017 GtkWidget *text = children->text;
1019 ChildInfo *child_info;
1021 gtk_widget_show(children->scrolledwin);
1022 gtk_text_freeze(GTK_TEXT(text));
1023 gtk_text_set_point(GTK_TEXT(text), 0);
1024 gtk_text_forward_delete(GTK_TEXT(text),
1025 gtk_text_get_length(GTK_TEXT(text)));
1026 for (cur = children->list; cur; cur = cur->next) {
1027 child_info = (ChildInfo *)cur->data;
1028 if (child_info->pid)
1029 caption = g_strdup_printf
1030 (_("--- Running: %s\n"),
1033 caption = g_strdup_printf
1034 (_("--- Ended: %s\n"),
1037 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1039 gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL,
1040 child_info->output->str, -1);
1042 child_info->new_out = FALSE;
1044 gtk_text_thaw(GTK_TEXT(text));
1048 static void create_io_dialog(Children *children)
1052 GtkWidget *entry = NULL;
1053 GtkWidget *input_hbox = NULL;
1054 GtkWidget *send_button;
1057 GtkWidget *scrolledwin;
1059 GtkWidget *abort_button;
1060 GtkWidget *close_button;
1062 debug_print("Creating action IO dialog\n");
1064 dialog = gtk_dialog_new();
1065 gtk_container_set_border_width
1066 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1067 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1068 gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1069 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1070 manage_window_set_transient(GTK_WINDOW(dialog));
1071 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
1072 GTK_SIGNAL_FUNC(delete_io_dialog_cb), children);
1073 gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
1074 GTK_SIGNAL_FUNC(hide_io_dialog_cb),
1077 vbox = gtk_vbox_new(FALSE, 8);
1078 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1079 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1080 gtk_widget_show(vbox);
1082 label = gtk_label_new(children->action);
1083 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1084 gtk_widget_show(label);
1086 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1087 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1088 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1089 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1090 gtk_widget_set_usize(scrolledwin, 480, 200);
1091 gtk_widget_hide(scrolledwin);
1093 text = gtk_text_new(gtk_scrolled_window_get_hadjustment
1094 (GTK_SCROLLED_WINDOW(scrolledwin)),
1095 gtk_scrolled_window_get_vadjustment
1096 (GTK_SCROLLED_WINDOW(scrolledwin)));
1097 gtk_text_set_editable(GTK_TEXT(text), FALSE);
1098 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1099 gtk_widget_show(text);
1101 if (children->open_in) {
1102 input_hbox = gtk_hbox_new(FALSE, 8);
1103 gtk_widget_show(input_hbox);
1105 entry = gtk_entry_new();
1106 gtk_widget_set_usize(entry, 320, -1);
1107 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1108 GTK_SIGNAL_FUNC(send_input), children);
1109 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1110 if (children->open_in & ACTION_USER_HIDDEN_IN)
1111 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1112 gtk_widget_show(entry);
1114 send_button = gtk_button_new_with_label(_(" Send "));
1115 gtk_signal_connect(GTK_OBJECT(send_button), "clicked",
1116 GTK_SIGNAL_FUNC(send_input), children);
1117 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1119 gtk_widget_show(send_button);
1121 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1122 gtk_widget_grab_focus(entry);
1125 gtkut_button_set_create(&hbox, &abort_button, _("Abort"),
1126 &close_button, _("Close"), NULL, NULL);
1127 gtk_signal_connect(GTK_OBJECT(abort_button), "clicked",
1128 GTK_SIGNAL_FUNC(kill_children_cb), children);
1129 gtk_signal_connect(GTK_OBJECT(close_button), "clicked",
1130 GTK_SIGNAL_FUNC(hide_io_dialog_cb), children);
1131 gtk_widget_show(hbox);
1134 gtk_widget_set_sensitive(close_button, FALSE);
1136 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1138 children->dialog = dialog;
1139 children->scrolledwin = scrolledwin;
1140 children->text = text;
1141 children->input_hbox = children->open_in ? input_hbox : NULL;
1142 children->input_entry = children->open_in ? entry : NULL;
1143 children->abort_btn = abort_button;
1144 children->close_btn = close_button;
1146 gtk_widget_show(dialog);
1149 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1151 ChildInfo *child_info = (ChildInfo *)data;
1155 gdk_input_remove(child_info->tag_status);
1157 c = read(source, &buf, 1);
1158 debug_print("Child returned %c\n", buf);
1160 waitpid(-child_info->pid, NULL, 0);
1161 childinfo_close_pipes(child_info);
1162 child_info->pid = 0;
1164 wait_for_children(child_info->children);
1167 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1169 Children *children = (Children *)data;
1170 ChildInfo *child_info = (ChildInfo *)children->list->data;
1174 debug_print("Sending input to grand child.\n");
1175 if (!(cond && GDK_INPUT_WRITE))
1178 gdk_input_remove(child_info->tag_in);
1179 child_info->tag_in = -1;
1181 input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1183 len = strlen(input);
1187 c = write(child_info->chld_in, input + count, len - count);
1190 } while (c >= 0 && count < len);
1193 write(child_info->chld_in, "\n", 2);
1197 gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1198 gtk_widget_set_sensitive(children->input_hbox, TRUE);
1199 close(child_info->chld_in);
1200 child_info->chld_in = -1;
1201 debug_print("Input to grand child sent.\n");
1204 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1206 ChildInfo *child_info = (ChildInfo *)data;
1208 gchar buf[BUFFSIZE];
1210 debug_print("Catching grand child's output.\n");
1211 if (child_info->type & (ACTION_PIPE_OUT | ACTION_INSERT)
1212 && source == child_info->chld_out) {
1213 gboolean is_selection = FALSE;
1214 GtkWidget *text = child_info->children->msg_text;
1216 if (GTK_EDITABLE(text)->has_selection)
1217 is_selection = TRUE;
1218 gtk_stext_freeze(GTK_STEXT(text));
1220 c = read(source, buf, sizeof(buf) - 1);
1223 gtk_stext_insert(GTK_STEXT(text),
1224 child_info->children->msgfont,
1225 NULL, NULL, buf, c);
1228 /* Using the select_region draws things. Should not.
1229 * so we just change selection position and
1230 * defere drawing when thawing. Hack?
1232 GTK_EDITABLE(text)->selection_end_pos =
1233 gtk_stext_get_point(GTK_STEXT(text));
1235 gtk_stext_thaw(GTK_STEXT(text));
1237 c = read(source, buf, sizeof(buf) - 1);
1238 for (i = 0; i < c; i++)
1239 g_string_append_c(child_info->output, buf[i]);
1241 child_info->new_out = TRUE;
1243 wait_for_children(child_info->children);
1246 static gchar *get_user_string(const gchar *action, ActionType type)
1249 gchar *user_str = NULL;
1252 case ACTION_USER_HIDDEN_STR:
1253 message = g_strdup_printf
1254 (_("Enter the argument for the following action:\n"
1255 "(`%%h' will be replaced with the argument)\n"
1258 user_str = input_dialog_with_invisible
1259 (_("Action's hidden user argument"), message, NULL);
1261 case ACTION_USER_STR:
1262 message = g_strdup_printf
1263 (_("Enter the argument for the following action:\n"
1264 "(`%%u' will be replaced with the argument)\n"
1267 user_str = input_dialog
1268 (_("Action's user argument"), message, NULL);
1271 g_warning("Unsupported action type %d", type);