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