Statistics
| Revision:

root / src / action.c @ 487

History | View | Annotate | Download (32.9 kB)

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