Statistics
| Revision:

root / src / action.c @ 487

History | View | Annotate | Download (32.9 kB)

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