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