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