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