88a946e6638db41623cc52bf8007223f00777f9b
[claws.git] / src / action.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
4  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #ifdef GDK_WINDOWING_X11
31 #  include <gdk/gdkx.h>
32 #endif /* GDK_WINDOWING_X11 */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #if HAVE_SYS_WAIT_H
39 #  include <sys/wait.h>
40 #endif
41 #include <signal.h>
42 #include <unistd.h>
43
44 #include "utils.h"
45 #include "gtkutils.h"
46 #include "manage_window.h"
47 #include "mainwindow.h"
48 #include "prefs_common.h"
49 #include "alertpanel.h"
50 #include "inputdialog.h"
51 #include "action.h"
52 #include "compose.h"
53 #include "procmsg.h"
54 #include "textview.h"
55 #include "matcher_parser.h" /* CLAWS */
56 #include "filtering.h"
57
58 typedef struct _Children                Children;
59 typedef struct _ChildInfo               ChildInfo;
60 typedef struct _UserStringDialog        UserStringDialog;
61
62 struct _Children
63 {
64         GtkWidget       *dialog;
65         GtkWidget       *text;
66         GtkWidget       *input_entry;
67         GtkWidget       *input_hbox;
68         GtkWidget       *progress_bar;
69         GtkWidget       *abort_btn;
70         GtkWidget       *close_btn;
71         GtkWidget       *scrolledwin;
72
73         gchar           *action;
74         ActionType       action_type;
75         GSList          *list;
76         gint             nb;
77         gint             initial_nb;
78         gint             open_in;
79         gboolean         output;
80
81         GtkWidget       *msg_text;
82
83         gboolean         is_selection;
84 };
85
86 struct _ChildInfo
87 {
88         Children        *children;
89         gchar           *cmd;
90         pid_t            pid;
91         gint             chld_in;
92         gint             chld_out;
93         gint             chld_err;
94         gint             chld_status;
95         gint             tag_in;
96         gint             tag_out;
97         gint             tag_err;
98         gint             tag_status;
99         gint             new_out;
100
101         GString         *output;
102         void (*callback)(void *data);
103         void *data;
104 };
105
106 static void action_update_menu          (GtkItemFactory *ifactory,
107                                          gchar          *branch_path,
108                                          gpointer        callback,
109                                          gpointer        data);
110 static void compose_actions_execute_cb  (Compose        *compose,
111                                          guint           action_nb,
112                                          GtkWidget      *widget);
113 static void mainwin_actions_execute_cb  (MainWindow     *mainwin,
114                                          guint           action_nb,
115                                          GtkWidget      *widget);
116 static void msgview_actions_execute_cb  (MessageView    *msgview,
117                                          guint           action_nb,
118                                          GtkWidget      *widget);
119 static void message_actions_execute     (MessageView    *msgview,
120                                          guint           action_nb,
121                                          GSList         *msg_list);
122
123 static gboolean execute_filtering_actions(gchar         *action, 
124                                           GSList        *msglist);
125
126 static gboolean execute_actions         (gchar          *action, 
127                                          GSList         *msg_list, 
128                                          GtkWidget      *text,
129                                          gint            body_pos,
130                                          MimeInfo       *partinfo, 
131                                          void (*callback)(void *data),
132                                          void *data);
133
134 static gchar *parse_action_cmd          (gchar          *action,
135                                          MsgInfo        *msginfo,
136                                          GSList         *msg_list,
137                                          MimeInfo       *partinfo,
138                                          const gchar    *user_str,
139                                          const gchar    *user_hidden_str,
140                                          const gchar    *sel_str);
141 static gboolean parse_append_filename   (GString        *cmd,
142                                          MsgInfo        *msginfo);
143
144 static gboolean parse_append_msgpart    (GString        *cmd,
145                                          MsgInfo        *msginfo,
146                                          MimeInfo       *partinfo);
147
148 static ChildInfo *fork_child            (gchar          *cmd,
149                                          const gchar    *msg_str,
150                                          Children       *children);
151
152 static gint wait_for_children           (Children       *children);
153
154 static void free_children               (Children       *children);
155
156 static void childinfo_close_pipes       (ChildInfo      *child_info);
157
158 static void create_io_dialog            (Children       *children);
159 static void update_io_dialog            (Children       *children);
160
161 static void hide_io_dialog_cb           (GtkWidget      *widget,
162                                          gpointer        data);
163 static gint io_dialog_key_pressed_cb    (GtkWidget      *widget,
164                                          GdkEventKey    *event,
165                                          gpointer        data);
166
167 static void catch_output                (gpointer                data,
168                                          gint                    source,
169                                          GdkInputCondition       cond);
170 static void catch_input                 (gpointer                data, 
171                                          gint                    source,
172                                          GdkInputCondition       cond);
173 static void catch_status                (gpointer                data,
174                                          gint                    source,
175                                          GdkInputCondition       cond);
176
177 static gchar *get_user_string           (const gchar    *action,
178                                          ActionType      type);
179
180
181 ActionType action_get_type(const gchar *action_str)
182 {
183         const gchar *p;
184         ActionType action_type = ACTION_NONE;
185
186         g_return_val_if_fail(action_str,  ACTION_ERROR);
187         g_return_val_if_fail(*action_str, ACTION_ERROR);
188
189         p = action_str;
190
191         if (p[0] == '|') {
192                 action_type |= ACTION_PIPE_IN;
193                 p++;
194         } else if (p[0] == '>') {
195                 action_type |= ACTION_USER_IN;
196                 p++;
197         } else if (p[0] == '*') {
198                 action_type |= ACTION_USER_HIDDEN_IN;
199                 p++;
200         }
201
202         if (p[0] == '\0')
203                 return ACTION_ERROR;
204
205         while (*p && action_type != ACTION_ERROR) {
206                 if (p[0] == '%' && p[1]) {
207                         switch (p[1]) {
208                         case 'a':
209                                 /* CLAWS: filtering action is a mutually exclusive
210                                  * action. we can enable others if needed later. we
211                                  * add ACTION_SINGLE | ACTION_MULTIPLE so it will
212                                  * only be executed from the main window toolbar */
213                                 if (p[2] == 's')  /* source messages */
214                                         action_type = ACTION_FILTERING_ACTION 
215                                                     | ACTION_SINGLE 
216                                                     | ACTION_MULTIPLE;
217                                 break;
218                         case 'f':
219                                 action_type |= ACTION_SINGLE;
220                                 break;
221                         case 'F':
222                                 action_type |= ACTION_MULTIPLE;
223                                 break;
224                         case 'p':
225                                 action_type |= ACTION_SINGLE;
226                                 break;
227                         case 's':
228                                 action_type |= ACTION_SELECTION_STR;
229                                 break;
230                         case 'u':
231                                 action_type |= ACTION_USER_STR;
232                                 break;
233                         case 'h':
234                                 action_type |= ACTION_USER_HIDDEN_STR;
235                                 break;
236                         default:
237                                 action_type = ACTION_ERROR;
238                                 break;
239                         }
240                 } else if (p[0] == '|') {
241                         if (p[1] == '\0')
242                                 action_type |= ACTION_PIPE_OUT;
243                 } else if (p[0] == '>') {
244                         if (p[1] == '\0')
245                                 action_type |= ACTION_INSERT;
246                 } else if (p[0] == '&') {
247                         if (p[1] == '\0')
248                                 action_type |= ACTION_ASYNC;
249                 }
250                 p++;
251         }
252
253         return action_type;
254 }
255
256 static gchar *parse_action_cmd(gchar *action, MsgInfo *msginfo,
257                                GSList *msg_list, MimeInfo *partinfo,
258                                const gchar *user_str,
259                                const gchar *user_hidden_str,
260                                const gchar *sel_str)
261 {
262         GString *cmd;
263         gchar *p;
264         GSList *cur;
265         
266         p = action;
267         
268         if (p[0] == '|' || p[0] == '>' || p[0] == '*')
269                 p++;
270
271         cmd = g_string_sized_new(strlen(action));
272
273         while (p[0] &&
274                !((p[0] == '|' || p[0] == '>' || p[0] == '&') && !p[1])) {
275                 if (p[0] == '%' && p[1]) {
276                         switch (p[1]) {
277                         case 'f':
278                                 if (!parse_append_filename(cmd, msginfo)) {
279                                         g_string_free(cmd, TRUE);
280                                         return NULL;
281                                 }
282                                 p++;
283                                 break;
284                         case 'F':
285                                 for (cur = msg_list; cur != NULL;
286                                      cur = cur->next) {
287                                         MsgInfo *msg = (MsgInfo *)cur->data;
288
289                                         if (!parse_append_filename(cmd, msg)) {
290                                                 g_string_free(cmd, TRUE);
291                                                 return NULL;
292                                         }
293                                         if (cur->next)
294                                                 g_string_append_c(cmd, ' ');
295                                 }
296                                 p++;
297                                 break;
298                         case 'p':
299                                 if (!parse_append_msgpart(cmd, msginfo,
300                                                           partinfo)) {
301                                         g_string_free(cmd, TRUE);
302                                         return NULL;
303                                 }
304                                 p++;
305                                 break;
306                         case 's':
307                                 if (sel_str)
308                                         g_string_append(cmd, sel_str);
309                                 p++;
310                                 break;
311                         case 'u':
312                                 if (user_str)
313                                         g_string_append(cmd, user_str);
314                                 p++;
315                                 break;
316                         case 'h':
317                                 if (user_hidden_str)
318                                         g_string_append(cmd, user_hidden_str);
319                                 p++;
320                                 break;
321                         default:
322                                 g_string_append_c(cmd, p[0]);
323                                 g_string_append_c(cmd, p[1]);
324                                 p++;
325                         }
326                 } else {
327                         g_string_append_c(cmd, p[0]);
328                 }
329                 p++;
330         }
331         if (cmd->len == 0) {
332                 g_string_free(cmd, TRUE);
333                 return NULL;
334         }
335
336         p = cmd->str;
337         g_string_free(cmd, FALSE);
338         return p;
339 }
340
341 static gboolean parse_append_filename(GString *cmd, MsgInfo *msginfo)
342 {
343         gchar *filename;
344         gchar *p, *q;
345         gchar escape_ch[] = "\\ ";
346
347         g_return_val_if_fail(msginfo, FALSE);
348
349         filename = procmsg_get_message_file(msginfo);
350
351         if (!filename) {
352                 alertpanel_error(_("Could not get message file %d"),
353                                  msginfo->msgnum);
354                 return FALSE;
355         }
356
357         p = filename;
358         while ((q = strpbrk(p, "$\"`'\\ \t*?[]&|;<>()!#~")) != NULL) {
359                 escape_ch[1] = *q;
360                 *q = '\0';
361                 g_string_append(cmd, p);
362                 g_string_append(cmd, escape_ch);
363                 p = q + 1;
364         }
365         g_string_append(cmd, p);
366
367         g_free(filename);
368
369         return TRUE;
370 }
371
372 static gboolean parse_append_msgpart(GString *cmd, MsgInfo *msginfo,
373                                      MimeInfo *partinfo)
374 {
375         gboolean single_part = FALSE;
376         gchar *filename;
377         gchar *part_filename;
378         gint ret;
379
380         if (!partinfo) {
381                 partinfo = procmime_scan_message(msginfo);
382                 if (!partinfo) {
383                         alertpanel_error(_("Could not get message part."));
384                         return FALSE;
385                 }
386
387                 single_part = TRUE;
388         }
389
390         filename = procmsg_get_message_file_path(msginfo);
391         part_filename = procmime_get_tmp_file_name(partinfo);
392
393         ret = procmime_get_part(part_filename, partinfo);
394
395         if (single_part)
396                 procmime_mimeinfo_free_all(partinfo);
397         g_free(filename);
398
399         if (ret < 0) {
400                 alertpanel_error(_("Can't get part of multipart message"));
401                 g_free(part_filename);
402                 return FALSE;
403         }
404
405         g_string_append(cmd, part_filename);
406
407         g_free(part_filename);
408
409         return TRUE;
410 }
411
412 void actions_execute(gpointer data, 
413                      guint action_nb,
414                      GtkWidget *widget,
415                      gint source)
416 {
417         if (source == TOOLBAR_MAIN) 
418                 mainwin_actions_execute_cb((MainWindow*)data, action_nb, widget);
419         else if (source == TOOLBAR_COMPOSE)
420                 compose_actions_execute_cb((Compose*)data, action_nb, widget);
421         else if (source == TOOLBAR_MSGVIEW)
422                 msgview_actions_execute_cb((MessageView*)data, action_nb, widget);      
423 }
424
425 void action_update_mainwin_menu(GtkItemFactory *ifactory,
426                                 gchar *branch_path,
427                                 MainWindow *mainwin)
428 {
429         action_update_menu(ifactory, branch_path,
430                            mainwin_actions_execute_cb, mainwin);
431 }
432
433 void action_update_msgview_menu(GtkItemFactory *ifactory,
434                                 gchar *branch_path,
435                                 MessageView *msgview)
436 {
437         action_update_menu(ifactory, branch_path,
438                            msgview_actions_execute_cb, msgview);
439 }
440
441 void action_update_compose_menu(GtkItemFactory *ifactory, 
442                                 gchar *branch_path,
443                                 Compose *compose)
444 {
445         action_update_menu(ifactory, branch_path,
446                            compose_actions_execute_cb, compose);
447 }
448
449 static void action_update_menu(GtkItemFactory *ifactory, 
450                                gchar *branch_path,
451                                gpointer callback, gpointer data)
452 {
453         GtkWidget *menuitem;
454         gchar *menu_path;
455         GSList *cur;
456         gchar *action, *action_p;
457         GList *amenu;
458         GtkItemFactoryEntry ifentry = {NULL, NULL, NULL, 0, "<Branch>"};
459
460         ifentry.path = branch_path;
461         menuitem = gtk_item_factory_get_widget(ifactory, branch_path);
462         g_return_if_fail(menuitem != NULL);
463
464         amenu = GTK_MENU_SHELL(menuitem)->children;
465         while (amenu != NULL) {
466                 GList *alist = amenu->next;
467                 gtk_widget_destroy(GTK_WIDGET(amenu->data));
468                 amenu = alist;
469         }
470
471         ifentry.accelerator     = NULL;
472         ifentry.callback_action = 0;
473         ifentry.callback        = callback;
474         ifentry.item_type       = NULL;
475
476         for (cur = prefs_common.actions_list; cur; cur = cur->next) {
477                 action   = g_strdup((gchar *)cur->data);
478                 action_p = strstr(action, ": ");
479                 if (action_p && action_p[2] &&
480                     (action_get_type(&action_p[2]) != ACTION_ERROR) &&
481                     (action[0] != '/')) {
482                         action_p[0] = '\0';
483                         menu_path = g_strdup_printf("%s/%s", branch_path,
484                                                     action);
485                         ifentry.path = menu_path;
486                         gtk_item_factory_create_item(ifactory, &ifentry, data,
487                                                      1);
488                         g_free(menu_path);
489                 }
490                 g_free(action);
491                 ifentry.callback_action++;
492         }
493 }
494
495 static void compose_actions_execute_cb(Compose *compose, guint action_nb,
496                                        GtkWidget *widget)
497 {
498         gchar *buf, *action;
499         ActionType action_type;
500
501         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
502
503         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
504         g_return_if_fail(buf != NULL);
505         action = strstr(buf, ": ");
506         g_return_if_fail(action != NULL);
507
508         /* Point to the beginning of the command-line */
509         action += 2;
510
511         action_type = action_get_type(action);
512         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE)) {
513                 alertpanel_warning
514                         (_("The selected action cannot be used in the compose window\n"
515                            "because it contains %%f, %%F, %%as or %%p."));
516                 return;
517         }
518
519         execute_actions(action, NULL, compose->text, 0, NULL, 
520                 compose_action_cb, compose);
521 }
522
523 static void mainwin_actions_execute_cb(MainWindow *mainwin, guint action_nb,
524                                        GtkWidget *widget)
525 {
526         GSList *msg_list;
527
528         msg_list = summary_get_selected_msg_list(mainwin->summaryview);
529         message_actions_execute(mainwin->messageview, action_nb, msg_list);
530         g_slist_free(msg_list);
531 }
532
533 static void msgview_actions_execute_cb(MessageView *msgview, guint action_nb,
534                                        GtkWidget *widget)
535 {
536         GSList *msg_list = NULL;
537
538         if (msgview->msginfo)
539                 msg_list = g_slist_append(msg_list, msgview->msginfo);
540         message_actions_execute(msgview, action_nb, msg_list);
541         g_slist_free(msg_list);
542 }
543
544 static void message_actions_execute(MessageView *msgview, guint action_nb,
545                                     GSList *msg_list)
546 {
547         TextView *textview;
548         MimeInfo *partinfo;
549         gchar *buf;
550         gchar *action;
551         GtkWidget *text = NULL;
552         guint body_pos = 0;
553         ActionType action_type;
554         
555         g_return_if_fail(action_nb < g_slist_length(prefs_common.actions_list));
556
557         buf = (gchar *)g_slist_nth_data(prefs_common.actions_list, action_nb);
558
559         g_return_if_fail(buf);
560         g_return_if_fail((action = strstr(buf, ": ")));
561
562         /* Point to the beginning of the command-line */
563         action += 2;
564
565         textview = messageview_get_current_textview(msgview);
566         if (textview) {
567                 text     = textview->text;
568                 body_pos = textview->body_pos;
569         }
570         partinfo = messageview_get_selected_mime_part(msgview);
571
572         /* this command will alter the message text */
573         action_type = action_get_type(action);
574         if (action_type & (ACTION_PIPE_OUT | ACTION_INSERT))
575                 msgview->filtered = TRUE;
576
577         if (action_type & ACTION_FILTERING_ACTION) 
578                 /* CLAWS: most of the above code is not necessary for applying
579                  * filtering */
580                 execute_filtering_actions(action, msg_list);
581         else
582                 execute_actions(action, msg_list, text, body_pos, partinfo,
583                         NULL, NULL);
584 }
585
586 static gboolean execute_filtering_actions(gchar *action, GSList *msglist)
587 {
588         GSList *action_list, *p;
589         const gchar *sbegin, *send;
590         gchar *action_string;
591         SummaryView *summaryview = NULL;
592         MainWindow *mainwin = NULL;
593
594         if (mainwindow_get_mainwindow()) {
595                 summaryview = mainwindow_get_mainwindow()->summaryview;
596                 mainwin = mainwindow_get_mainwindow();
597         }
598
599         if (NULL == (sbegin = strstr2(action, "%as{")))
600                 return FALSE;
601         sbegin += sizeof "%as{" - 1;
602         if (NULL == (send = strrchr(sbegin, '}')))
603                 return FALSE;
604         action_string = g_strndup(sbegin, send - sbegin);
605         
606         action_list = matcher_parser_get_action_list(action_string);
607         g_free(action_string);
608         if (action_list == NULL) return FALSE;
609         
610         /* apply actions on each message info */
611         for (p = msglist; p && p->data; p = g_slist_next(p)) {
612                 filteringaction_apply_action_list(action_list, (MsgInfo *) p->data);
613                 
614         }
615
616         if (summaryview) {
617                 summary_lock(summaryview);                              
618                 main_window_cursor_wait(mainwin);               
619                 gtk_clist_freeze(GTK_CLIST(summaryview->ctree));        
620                 folder_item_update_freeze();                            
621         }
622
623         filtering_move_and_copy_msgs(msglist);
624
625         if (summaryview) {
626                 folder_item_update_thaw();                              
627                 gtk_clist_thaw(GTK_CLIST(summaryview->ctree));          
628                 main_window_cursor_normal(mainwin);     
629                 summary_unlock(summaryview);                            
630         }
631         for (p = action_list; p; p = g_slist_next(p))
632                 if (p->data) filteringaction_free(p->data);     
633         g_slist_free(action_list);              
634         return TRUE;    
635 }
636
637 static gboolean execute_actions(gchar *action, GSList *msg_list,
638                                 GtkWidget *text,
639                                 gint body_pos, MimeInfo *partinfo,
640                                 void (*callback)(void *data), void *data)
641 {
642         GSList *children_list = NULL;
643         gint is_ok  = TRUE;
644         gint msg_list_len;
645         Children *children;
646         ChildInfo *child_info;
647         ActionType action_type;
648         MsgInfo *msginfo;
649         gchar *cmd;
650         gchar *sel_str = NULL;
651         gchar *msg_str = NULL;
652         gchar *user_str = NULL;
653         gchar *user_hidden_str = NULL;
654         GtkTextIter start_iter, end_iter;
655         gboolean is_selection = FALSE;
656
657         g_return_val_if_fail(action && *action, FALSE);
658
659         action_type = action_get_type(action);
660
661         if (action_type == ACTION_ERROR)
662                 return FALSE;         /* ERR: syntax error */
663
664         if (action_type & (ACTION_SINGLE | ACTION_MULTIPLE) && !msg_list)
665                 return FALSE;         /* ERR: file command without selection */
666
667         msg_list_len = g_slist_length(msg_list);
668
669         if (action_type & (ACTION_PIPE_OUT | ACTION_PIPE_IN | ACTION_INSERT)) {
670                 if (msg_list_len > 1)
671                         return FALSE; /* ERR: pipe + multiple selection */
672                 if (!text)
673                         return FALSE; /* ERR: pipe and no displayed text */
674         }
675
676         if (action_type & ACTION_SELECTION_STR) {
677                 if (!text)
678                         return FALSE; /* ERR: selection string but no text */
679         }
680
681         if (text) {
682                 GtkTextBuffer *textbuf;
683
684                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
685                 is_selection = gtk_text_buffer_get_selection_bounds
686                         (textbuf, &start_iter, &end_iter);
687                 if (!is_selection) {
688                         gtk_text_buffer_get_iter_at_offset
689                                 (textbuf, &start_iter, body_pos);
690                         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
691                 }
692                 msg_str = gtk_text_buffer_get_text
693                         (textbuf, &start_iter, &end_iter, FALSE);
694                 if (is_selection)
695                         sel_str = g_strdup(msg_str);
696         }
697
698         if (action_type & ACTION_USER_STR) {
699                 if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
700                         g_free(msg_str);
701                         g_free(sel_str);
702                         return FALSE;
703                 }
704         }
705
706         if (action_type & ACTION_USER_HIDDEN_STR) {
707                 if (!(user_hidden_str =
708                         get_user_string(action, ACTION_USER_HIDDEN_STR))) {
709                         g_free(msg_str);
710                         g_free(sel_str);
711                         g_free(user_str);
712                         return FALSE;
713                 }
714         }
715
716         if (text && (action_type & ACTION_PIPE_OUT)) {
717                 GtkTextBuffer *textbuf;
718                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
719                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
720         }
721
722         children = g_new0(Children, 1);
723
724         children->action      = g_strdup(action);
725         children->action_type = action_type;
726         children->msg_text    = text;
727         children->is_selection = is_selection;
728
729         if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
730             ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
731                 children->open_in = 1;
732
733         if (action_type & ACTION_SINGLE) {
734                 GSList *cur;
735
736                 for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
737                         msginfo = (MsgInfo *)cur->data;
738                         if (!msginfo) {
739                                 is_ok  = FALSE; /* ERR: msginfo missing */
740                                 break;
741                         }
742                         cmd = parse_action_cmd(action, msginfo, msg_list,
743                                                partinfo, user_str,
744                                                user_hidden_str, sel_str);
745                         if (!cmd) {
746                                 debug_print("Action command error\n");
747                                 is_ok  = FALSE; /* ERR: incorrect command */
748                                 break;
749                         }
750                         if ((child_info = fork_child(cmd, msg_str, children))) {
751                                 children_list = g_slist_append(children_list,
752                                                                child_info);
753                         }
754                         g_free(cmd);
755                 }
756         } else {
757                 cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
758                                        user_str, user_hidden_str, sel_str);
759                 if (cmd) {
760                         if ((child_info = fork_child(cmd, msg_str, children))) {
761                                 children_list = g_slist_append(children_list,
762                                                                child_info);
763                         }
764                         g_free(cmd);
765                 } else
766                         is_ok  = FALSE;         /* ERR: incorrect command */
767         }
768
769         g_free(msg_str);
770         g_free(sel_str);
771         g_free(user_str);
772         g_free(user_hidden_str);
773
774         if (!children_list) {
775                  /* If not waiting for children, return */
776                 free_children(children);
777         } else {
778                 GSList *cur;
779
780                 children->list        = children_list;
781                 children->nb          = g_slist_length(children_list);
782                 children->initial_nb  = children->nb;
783
784                 for (cur = children_list; cur; cur = cur->next) {
785                         child_info = (ChildInfo *) cur->data;
786                         child_info->callback = callback;
787                         child_info->data = data;
788                         child_info->tag_status = 
789                                 gdk_input_add(child_info->chld_status,
790                                               GDK_INPUT_READ,
791                                               catch_status, child_info);
792                 }
793
794                 create_io_dialog(children);
795         }
796         return is_ok;
797 }
798
799 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
800                              Children *children)
801 {
802 #ifdef G_OS_UNIX
803         gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
804         gchar *cmdline[4], *ret_str;
805         pid_t pid, gch_pid;
806         ChildInfo *child_info;
807         gint sync;
808         gssize by_written = 0, by_read = 0;
809
810         sync = !(children->action_type & ACTION_ASYNC);
811
812         chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
813                 = chld_err[1] = chld_status[0] = chld_status[1] = -1;
814
815         if (sync) {
816                 if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
817                     pipe(chld_err)) {
818                         alertpanel_error(_("Command could not be started. "
819                                            "Pipe creation failed.\n%s"),
820                                         g_strerror(errno));
821                         /* Closing fd = -1 fails silently */
822                         close(chld_in[0]);
823                         close(chld_in[1]);
824                         close(chld_out[0]);
825                         close(chld_out[1]);
826                         close(chld_err[0]);
827                         close(chld_err[1]);
828                         close(chld_status[0]);
829                         close(chld_status[1]);
830                         return NULL; /* Pipe error */
831                 }
832         }
833
834         debug_print("Forking child and grandchild.\n");
835         debug_print("Executing: /bin/sh -c %s\n", cmd);
836
837         pid = fork();
838         if (pid == 0) { /* Child */
839                 if (setpgid(0, 0))
840                         perror("setpgid");
841
842 #ifdef GDK_WINDOWING_X11
843                 close(ConnectionNumber(gdk_display));
844 #endif /* GDK_WINDOWING_X11 */
845
846                 gch_pid = fork();
847
848                 if (gch_pid == 0) {
849                         if (setpgid(0, getppid()))
850                                 perror("setpgid");
851
852                         if (sync) {
853                                 if (children->action_type &
854                                     (ACTION_PIPE_IN |
855                                      ACTION_USER_IN |
856                                      ACTION_USER_HIDDEN_IN)) {
857                                         close(fileno(stdin));
858                                         dup  (chld_in[0]);
859                                 }
860                                 close(chld_in[0]);
861                                 close(chld_in[1]);
862
863                                 close(fileno(stdout));
864                                 dup  (chld_out[1]);
865                                 close(chld_out[0]);
866                                 close(chld_out[1]);
867
868                                 close(fileno(stderr));
869                                 dup  (chld_err[1]);
870                                 close(chld_err[0]);
871                                 close(chld_err[1]);
872                         }
873
874                         cmdline[0] = "sh";
875                         cmdline[1] = "-c";
876                         ret_str = g_locale_from_utf8(cmd, strlen(cmd),
877                                                      &by_read, &by_written,
878                                                      NULL);
879                         if (ret_str && by_written)
880                                 cmdline[2] = ret_str;
881                         else
882                                 cmdline[2] = cmd;
883                         cmdline[3] = NULL;
884                         execvp("/bin/sh", cmdline);
885
886                         perror("execvp");
887                         g_free(ret_str);
888                         _exit(1);
889                 } else if (gch_pid < (pid_t) 0) { /* Fork error */
890                         if (sync)
891                                 write(chld_status[1], "1\n", 2);
892                         perror("fork");
893                         _exit(1);
894                 } else { /* Child */
895                         if (sync) {
896                                 close(chld_in[0]);
897                                 close(chld_in[1]);
898                                 close(chld_out[0]);
899                                 close(chld_out[1]);
900                                 close(chld_err[0]);
901                                 close(chld_err[1]);
902                                 close(chld_status[0]);
903
904                                 debug_print("Child: Waiting for grandchild\n");
905                                 waitpid(gch_pid, NULL, 0);
906                                 debug_print("Child: grandchild ended\n");
907                                 write(chld_status[1], "0\n", 2);
908                                 close(chld_status[1]);
909                         }
910                         _exit(0);
911                 }
912         } else if (pid < 0) { /* Fork error */
913                 alertpanel_error(_("Could not fork to execute the following "
914                                    "command:\n%s\n%s"),
915                                  cmd, g_strerror(errno));
916                 return NULL; 
917         }
918
919         /* Parent */
920
921         if (!sync) {
922                 waitpid(pid, NULL, 0);
923                 return NULL;
924         }
925
926         close(chld_in[0]);
927         if (!(children->action_type &
928               (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
929                 close(chld_in[1]);
930         close(chld_out[1]);
931         close(chld_err[1]);
932         close(chld_status[1]);
933
934         child_info = g_new0(ChildInfo, 1);
935
936         child_info->children    = children;
937
938         child_info->pid         = pid;
939         child_info->cmd         = g_strdup(cmd);
940         child_info->new_out     = FALSE;
941         child_info->output      = g_string_new(NULL);
942         child_info->chld_in     =
943                 (children->action_type &
944                  (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
945                         ? chld_in [1] : -1;
946         child_info->chld_out    = chld_out[0];
947         child_info->chld_err    = chld_err[0];
948         child_info->chld_status = chld_status[0];
949         child_info->tag_in      = -1;
950         child_info->tag_out     = gdk_input_add(chld_out[0], GDK_INPUT_READ,
951                                                 catch_output, child_info);
952         child_info->tag_err     = gdk_input_add(chld_err[0], GDK_INPUT_READ,
953                                                 catch_output, child_info);
954
955         if (!(children->action_type &
956               (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
957                 return child_info;
958
959         if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
960                 ret_str = g_locale_from_utf8(msg_str, strlen(msg_str),
961                                              &by_read, &by_written, NULL);
962                 if (ret_str && by_written) {
963                         write(chld_in[1], ret_str, strlen(ret_str));
964                         g_free(ret_str);
965                 } else
966                         write(chld_in[1], msg_str, strlen(msg_str));
967                 if (!(children->action_type &
968                       (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
969                         close(chld_in[1]);
970                 child_info->chld_in = -1; /* No more input */
971         }
972
973         return child_info;
974 #else
975         return NULL;
976 #endif /* G_OS_UNIX */
977 }
978
979 static void kill_children_cb(GtkWidget *widget, gpointer data)
980 {
981 #ifdef G_OS_UNIX
982         GSList *cur;
983         Children *children = (Children *) data;
984         ChildInfo *child_info;
985
986         for (cur = children->list; cur; cur = cur->next) {
987                 child_info = (ChildInfo *)(cur->data);
988                 debug_print("Killing child group id %d\n", child_info->pid);
989                 if (child_info->pid && kill(-child_info->pid, SIGTERM) < 0)
990                         perror("kill");
991         }
992 #endif /* G_OS_UNIX */
993 }
994
995 static gint wait_for_children(Children *children)
996 {
997         gboolean new_output;
998         ChildInfo *child_info;
999         GSList *cur;
1000         gint nb = children->nb;
1001
1002         children->nb = 0;
1003
1004         cur = children->list;
1005         new_output = FALSE;
1006         while (cur) {
1007                 child_info = (ChildInfo *)cur->data;
1008                 if (child_info->pid)
1009                         children->nb++;
1010                 new_output |= child_info->new_out;
1011                 cur = cur->next;
1012         }
1013
1014         children->output |= new_output;
1015
1016         if (new_output || (children->dialog && (nb != children->nb)))
1017                 update_io_dialog(children);
1018
1019         if (children->nb)
1020                 return FALSE;
1021
1022         if (!children->dialog) {
1023                 free_children(children);
1024         } else if (!children->output) {
1025                 gtk_widget_destroy(children->dialog);
1026         }
1027
1028         return FALSE;
1029 }
1030
1031 static void send_input(GtkWidget *w, gpointer data)
1032 {
1033         Children *children = (Children *) data;
1034         ChildInfo *child_info = (ChildInfo *) children->list->data;
1035
1036         child_info->tag_in = gdk_input_add(child_info->chld_in,
1037                                            GDK_INPUT_WRITE,
1038                                            catch_input, children);
1039         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1040 }
1041
1042 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
1043 {
1044         hide_io_dialog_cb(w, data);
1045         return TRUE;
1046 }
1047
1048 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
1049 {
1050
1051         Children *children = (Children *)data;
1052
1053         if (!children->nb) {
1054                 g_signal_handlers_disconnect_matched
1055                         (G_OBJECT(children->dialog), G_SIGNAL_MATCH_DATA,
1056                          0, 0, NULL, NULL, children);
1057                 gtk_widget_destroy(children->dialog);
1058                 free_children(children);
1059         }
1060 }
1061
1062 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
1063                                      gpointer data)
1064 {
1065         if (event && event->keyval == GDK_Escape)
1066                 hide_io_dialog_cb(widget, data);
1067         return TRUE;
1068 }
1069
1070 static void childinfo_close_pipes(ChildInfo *child_info)
1071 {
1072         /* stdout and stderr pipes are guaranteed to be removed by
1073          * their handler, but in case where we receive child exit notification
1074          * before grand-child's pipes closing signals, we check them and close
1075          * them if necessary
1076          */
1077         if (child_info->tag_in > 0)
1078                 gdk_input_remove(child_info->tag_in);
1079         if (child_info->tag_out > 0)
1080                 gdk_input_remove(child_info->tag_out);
1081         if (child_info->tag_err > 0)
1082                 gdk_input_remove(child_info->tag_err);
1083
1084         if (child_info->chld_in >= 0)
1085                 close(child_info->chld_in);
1086         if (child_info->chld_out >= 0)
1087                 close(child_info->chld_out);
1088         if (child_info->chld_err >= 0)
1089                 close(child_info->chld_err);
1090
1091         close(child_info->chld_status);
1092 }
1093
1094 static void free_children(Children *children)
1095 {
1096         ChildInfo *child_info;
1097         void (*callback)(void *data) = NULL;
1098         void *data = NULL;
1099
1100         debug_print("Freeing children data %p\n", children);
1101
1102         g_free(children->action);
1103         while (children->list != NULL) {
1104                 child_info = (ChildInfo *)children->list->data;
1105                 g_free(child_info->cmd);
1106                 g_string_free(child_info->output, TRUE);
1107                 children->list = g_slist_remove(children->list, child_info);
1108                 callback = child_info->callback;
1109                 data = child_info->data;
1110                 g_free(child_info);
1111         }
1112
1113         if (callback)
1114                 callback(data);
1115         
1116         g_free(children);
1117 }
1118
1119 static void update_io_dialog(Children *children)
1120 {
1121         GSList *cur;
1122
1123         debug_print("Updating actions input/output dialog.\n");
1124
1125         if (children->progress_bar) {
1126                 gchar *text;
1127                 
1128                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(children->progress_bar),
1129                                               (gdouble) (children->initial_nb - children->nb) /
1130                                               (gdouble) children->initial_nb);
1131                 text = g_strdup_printf("%s %d/%d", _("Completed"), 
1132                                        children->initial_nb - children->nb,
1133                                        children->initial_nb);
1134                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(children->progress_bar), text);
1135                 g_free(text);
1136         }                                             
1137
1138         if (!children->nb) {
1139                 gtk_widget_set_sensitive(children->abort_btn, FALSE);
1140                 gtk_widget_set_sensitive(children->close_btn, TRUE);
1141                 if (children->input_hbox)
1142                         gtk_widget_set_sensitive(children->input_hbox, FALSE);
1143                 gtk_widget_grab_focus(children->close_btn);
1144                 g_signal_connect(G_OBJECT(children->dialog),
1145                                  "key_press_event",
1146                                  G_CALLBACK(io_dialog_key_pressed_cb),
1147                                  children);
1148         }
1149
1150         if (children->output) {
1151                 GtkWidget *text = children->text;
1152                 GtkTextBuffer *textbuf;
1153                 GtkTextIter iter, start_iter, end_iter;
1154                 gchar *caption;
1155                 ChildInfo *child_info;
1156
1157                 gtk_widget_show(children->scrolledwin);
1158                 textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1159                 gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1160                 gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1161                 gtk_text_buffer_get_start_iter(textbuf, &iter);
1162
1163                 for (cur = children->list; cur; cur = cur->next) {
1164                         child_info = (ChildInfo *)cur->data;
1165                         if (child_info->pid)
1166                                 caption = g_strdup_printf
1167                                         (_("--- Running: %s\n"),
1168                                          child_info->cmd);
1169                         else
1170                                 caption = g_strdup_printf
1171                                         (_("--- Ended: %s\n"),
1172                                          child_info->cmd);
1173
1174                         gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1175                         gtk_text_buffer_insert(textbuf, &iter,
1176                                                child_info->output->str, -1);
1177                         g_free(caption);
1178                         child_info->new_out = FALSE;
1179                 }
1180         }
1181 }
1182
1183 static void create_io_dialog(Children *children)
1184 {
1185         GtkWidget *dialog;
1186         GtkWidget *vbox;
1187         GtkWidget *entry = NULL;
1188         GtkWidget *input_hbox = NULL;
1189         GtkWidget *send_button;
1190         GtkWidget *label;
1191         GtkWidget *text;
1192         GtkWidget *scrolledwin;
1193         GtkWidget *hbox;
1194         GtkWidget *progress_bar = NULL;
1195         GtkWidget *abort_button;
1196         GtkWidget *close_button;
1197
1198         debug_print("Creating action IO dialog\n");
1199
1200         dialog = gtk_dialog_new();
1201         gtk_container_set_border_width
1202                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1203         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1204         gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1205         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1206         manage_window_set_transient(GTK_WINDOW(dialog));
1207         g_signal_connect(G_OBJECT(dialog), "delete_event",
1208                          G_CALLBACK(delete_io_dialog_cb), children);
1209         g_signal_connect(G_OBJECT(dialog), "destroy",
1210                          G_CALLBACK(hide_io_dialog_cb),
1211                          children);
1212
1213         vbox = gtk_vbox_new(FALSE, 8);
1214         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1215         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1216         gtk_widget_show(vbox);
1217
1218         label = gtk_label_new(children->action);
1219         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1220         gtk_widget_show(label);
1221
1222         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1223         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1224                                        GTK_POLICY_AUTOMATIC,
1225                                        GTK_POLICY_AUTOMATIC);
1226         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1227                                             GTK_SHADOW_IN);
1228         gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1229         gtk_widget_set_size_request(scrolledwin, 560, 200);
1230         gtk_widget_hide(scrolledwin);
1231
1232         text = gtk_text_view_new();
1233
1234         if (prefs_common.textfont) {
1235                 PangoFontDescription *font_desc;
1236                 font_desc = pango_font_description_from_string
1237                         (prefs_common.textfont);
1238                 if (font_desc) {
1239                         gtk_widget_modify_font(text, font_desc);
1240                         pango_font_description_free(font_desc);
1241                 }
1242         }
1243
1244         gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1245         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1246         gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1247         gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1248         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1249         gtk_widget_show(text);
1250
1251         if (children->open_in) {
1252                 input_hbox = gtk_hbox_new(FALSE, 8);
1253                 gtk_widget_show(input_hbox);
1254
1255                 entry = gtk_entry_new();
1256                 gtk_widget_set_size_request(entry, 320, -1);
1257                 g_signal_connect(G_OBJECT(entry), "activate",
1258                                  G_CALLBACK(send_input), children);
1259                 gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1260                 if (children->action_type & ACTION_USER_HIDDEN_IN)
1261                         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1262                 gtk_widget_show(entry);
1263
1264                 send_button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
1265                 g_signal_connect(G_OBJECT(send_button), "clicked",
1266                                  G_CALLBACK(send_input), children);
1267                 gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1268                                    FALSE, 0);
1269                 gtk_widget_show(send_button);
1270
1271                 gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1272                 gtk_widget_grab_focus(entry);
1273         }
1274
1275         if (children->initial_nb > 1) {
1276                 gchar * text;
1277                 
1278                 progress_bar = gtk_progress_bar_new();
1279                 gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress_bar),
1280                                 GTK_PROGRESS_LEFT_TO_RIGHT);
1281                 text = g_strdup_printf("%s 0/%d\n", _("Completed"), 
1282                                        children->initial_nb);
1283                 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar),
1284                                           text);
1285                 g_free(text);
1286                 gtk_box_pack_start(GTK_BOX(vbox), progress_bar, FALSE, FALSE, 0);
1287                 gtk_widget_show(progress_bar);
1288         }
1289
1290         gtkut_stock_button_set_create(&hbox, &abort_button, GTK_STOCK_STOP,
1291                                       &close_button, GTK_STOCK_CLOSE, NULL, NULL);
1292         g_signal_connect(G_OBJECT(abort_button), "clicked",
1293                          G_CALLBACK(kill_children_cb), children);
1294         g_signal_connect(G_OBJECT(close_button), "clicked",
1295                          G_CALLBACK(hide_io_dialog_cb), children);
1296         gtk_widget_show(hbox);
1297
1298         if (children->nb)
1299                 gtk_widget_set_sensitive(close_button, FALSE);
1300
1301         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1302
1303         children->dialog       = dialog;
1304         children->scrolledwin  = scrolledwin;
1305         children->text         = text;
1306         children->input_hbox   = children->open_in ? input_hbox : NULL;
1307         children->input_entry  = children->open_in ? entry : NULL;
1308         children->progress_bar = progress_bar;
1309         children->abort_btn    = abort_button;
1310         children->close_btn    = close_button;
1311
1312         gtk_widget_show(dialog);
1313 }
1314
1315 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1316 {
1317         ChildInfo *child_info = (ChildInfo *)data;
1318         gchar buf;
1319         gint c;
1320
1321         gdk_input_remove(child_info->tag_status);
1322
1323         c = read(source, &buf, 1);
1324         debug_print("Child returned %c\n", buf);
1325
1326 #ifdef G_OS_UNIX
1327         waitpid(-child_info->pid, NULL, 0);
1328 #endif
1329         childinfo_close_pipes(child_info);
1330         child_info->pid = 0;
1331
1332         wait_for_children(child_info->children);
1333 }
1334         
1335 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1336 {
1337         Children *children = (Children *)data;
1338         ChildInfo *child_info = (ChildInfo *)children->list->data;
1339         gchar *input, *ret_str;
1340         gint c, count, len;
1341         gssize by_read = 0, by_written = 0;
1342
1343         debug_print("Sending input to grand child.\n");
1344         if (!(cond && GDK_INPUT_WRITE))
1345                 return;
1346
1347         gdk_input_remove(child_info->tag_in);
1348         child_info->tag_in = -1;
1349
1350         input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1351                                        0, -1);
1352         ret_str = g_locale_from_utf8(input, strlen(input), &by_read,
1353                                      &by_written, NULL);
1354         if (ret_str && by_written) {
1355                 g_free(input);
1356                 input = ret_str;
1357         }
1358
1359         len = strlen(input);
1360         count = 0;
1361
1362         do {
1363                 c = write(child_info->chld_in, input + count, len - count);
1364                 if (c >= 0)
1365                         count += c;
1366         } while (c >= 0 && count < len);
1367
1368         if (c >= 0)
1369                 write(child_info->chld_in, "\n", 2);
1370
1371         g_free(input);
1372
1373         gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1374         gtk_widget_set_sensitive(children->input_hbox, TRUE);
1375         close(child_info->chld_in);
1376         child_info->chld_in = -1;
1377         debug_print("Input to grand child sent.\n");
1378 }
1379
1380 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1381 {
1382         ChildInfo *child_info = (ChildInfo *)data;
1383         gint c;
1384         gchar buf[BUFFSIZE];
1385
1386         debug_print("Catching grand child's output.\n");
1387         if (child_info->children->action_type &
1388             (ACTION_PIPE_OUT | ACTION_INSERT)
1389             && source == child_info->chld_out) {
1390                 GtkTextView *text =
1391                         GTK_TEXT_VIEW(child_info->children->msg_text);
1392                 GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1393                 GtkTextIter iter;
1394                 GtkTextMark *mark;
1395                 gint ins_pos;
1396
1397                 mark = gtk_text_buffer_get_insert(textbuf);
1398                 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1399                 ins_pos = gtk_text_iter_get_offset(&iter);
1400
1401                 while (TRUE) {
1402                         gsize bytes_read = 0, bytes_written = 0;
1403                         gchar *ret_str;
1404
1405                         c = read(source, buf, sizeof(buf) - 1);
1406                         if (c == 0)
1407                                 break;
1408
1409                         ret_str = g_locale_to_utf8
1410                                 (buf, c, &bytes_read, &bytes_written, NULL);
1411                         if (ret_str && bytes_written > 0) {
1412                                 gtk_text_buffer_insert
1413                                         (textbuf, &iter, ret_str,
1414                                          bytes_written);
1415                                 g_free(ret_str);
1416                         } else
1417                                 gtk_text_buffer_insert(textbuf, &iter, buf, c);
1418                 }
1419
1420                 if (child_info->children->is_selection) {
1421                         GtkTextIter ins;
1422
1423                         gtk_text_buffer_get_iter_at_offset
1424                                 (textbuf, &ins, ins_pos);
1425                         gtk_text_buffer_select_range(textbuf, &ins, &iter);
1426                 }
1427         } else {
1428                 c = read(source, buf, sizeof(buf) - 1);
1429                 if (c > 0) {
1430                         gsize bytes_read = 0, bytes_written = 0;
1431                         gchar *ret_str;
1432
1433                         ret_str = g_locale_to_utf8
1434                                 (buf, c, &bytes_read, &bytes_written, NULL);
1435                         if (ret_str && bytes_written > 0) {
1436                                 g_string_append_len
1437                                         (child_info->output, ret_str,
1438                                          bytes_written);
1439                                 g_free(ret_str);
1440                         } else
1441                                 g_string_append_len(child_info->output, buf, c);
1442
1443                         child_info->new_out = TRUE;
1444                 }
1445         }
1446         if (c == 0) {
1447                 if (source == child_info->chld_out) {
1448                         gdk_input_remove(child_info->tag_out);
1449                         child_info->tag_out = -1;
1450                         close(child_info->chld_out);
1451                         child_info->chld_out = -1;
1452                 } else {
1453                         gdk_input_remove(child_info->tag_err);
1454                         child_info->tag_err = -1;
1455                         close(child_info->chld_err);
1456                         child_info->chld_err = -1;
1457                 }
1458         }
1459         
1460         wait_for_children(child_info->children);
1461 }
1462
1463 static gchar *get_user_string(const gchar *action, ActionType type)
1464 {
1465         gchar *message;
1466         gchar *user_str = NULL;
1467
1468         switch (type) {
1469         case ACTION_USER_HIDDEN_STR:
1470                 message = g_strdup_printf
1471                         (_("Enter the argument for the following action:\n"
1472                            "('%%h' will be replaced with the argument)\n"
1473                            "  %s"),
1474                          action);
1475                 user_str = input_dialog_with_invisible
1476                         (_("Action's hidden user argument"), message, NULL);
1477                 break;
1478         case ACTION_USER_STR:
1479                 message = g_strdup_printf
1480                         (_("Enter the argument for the following action:\n"
1481                            "('%%u' will be replaced with the argument)\n"
1482                            "  %s"),
1483                          action);
1484                 user_str = input_dialog
1485                         (_("Action's user argument"), message, NULL);
1486                 break;
1487         default:
1488                 g_warning("Unsupported action type %d", type);
1489         }
1490
1491         return user_str;
1492 }