1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2015 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 
action_get_type(const gchar * action_str)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 
parse_action_cmd(gchar * action,MsgInfo * msginfo,GSList * msg_list,MimeInfo * partinfo,const gchar * user_str,const gchar * user_hidden_str,const gchar * sel_str)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 
parse_append_filename(GString * cmd,MsgInfo * msginfo)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 
parse_append_msgpart(GString * cmd,MsgInfo * msginfo,MimeInfo * partinfo)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 
action_update_mainwin_menu(GtkItemFactory * ifactory,MainWindow * mainwin)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 
action_update_msgview_menu(GtkItemFactory * ifactory,MessageView * msgview)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 
action_update_compose_menu(GtkItemFactory * ifactory,Compose * compose)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 
action_update_menu(GtkItemFactory * ifactory,gchar * branch_path,gpointer callback,gpointer data)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 
compose_actions_execute_cb(Compose * compose,guint action_nb,GtkWidget * widget)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 
mainwin_actions_execute_cb(MainWindow * mainwin,guint action_nb,GtkWidget * widget)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 
msgview_actions_execute_cb(MessageView * msgview,guint action_nb,GtkWidget * widget)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 
message_actions_execute(MessageView * msgview,guint action_nb,GSList * msg_list)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 
execute_actions(gchar * action,GSList * msg_list,GtkWidget * text,gint body_pos,MimeInfo * partinfo)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 			if (!(action_type & ACTION_INSERT))
585 				gtk_text_buffer_place_cursor
586 					(textbuf, &start_iter);
587 		}
588 		msg_str = gtk_text_buffer_get_text
589 			(textbuf, &start_iter, &end_iter, FALSE);
590 		if (is_selection)
591 			sel_str = g_strdup(msg_str);
592 	}
593 
594 	if (action_type & ACTION_USER_STR) {
595 		if (!(user_str = get_user_string(action, ACTION_USER_STR))) {
596 			g_free(msg_str);
597 			g_free(sel_str);
598 			return FALSE;
599 		}
600 	}
601 
602 	if (action_type & ACTION_USER_HIDDEN_STR) {
603 		if (!(user_hidden_str =
604 			get_user_string(action, ACTION_USER_HIDDEN_STR))) {
605 			g_free(msg_str);
606 			g_free(sel_str);
607 			g_free(user_str);
608 			return FALSE;
609 		}
610 	}
611 
612 	if (text && (action_type & ACTION_PIPE_OUT)) {
613 		GtkTextBuffer *textbuf;
614 		textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
615 		gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
616 	}
617 
618 	children = g_new0(Children, 1);
619 
620 	children->action       = g_strdup(action);
621 	children->action_type  = action_type;
622 	children->msg_text     = text;
623 	children->is_selection = is_selection;
624 
625 	if ((action_type & (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)) &&
626 	    ((action_type & ACTION_SINGLE) == 0 || msg_list_len == 1))
627 		children->open_in = 1;
628 
629 	if (action_type & ACTION_SINGLE) {
630 		GSList *cur;
631 
632 		for (cur = msg_list; cur && is_ok == TRUE; cur = cur->next) {
633 			msginfo = (MsgInfo *)cur->data;
634 			if (!msginfo) {
635 				is_ok  = FALSE; /* ERR: msginfo missing */
636 				break;
637 			}
638 			cmd = parse_action_cmd(action, msginfo, msg_list,
639 					       partinfo, user_str,
640 					       user_hidden_str, sel_str);
641 			if (!cmd) {
642 				debug_print("Action command error\n");
643 				is_ok  = FALSE; /* ERR: incorrect command */
644 				break;
645 			}
646 			if ((child_info = fork_child(cmd, msg_str, children))) {
647 				children_list = g_slist_append(children_list,
648 							       child_info);
649 			}
650 			g_free(cmd);
651 		}
652 	} else {
653 		cmd = parse_action_cmd(action, NULL, msg_list, partinfo,
654 				       user_str, user_hidden_str, sel_str);
655 		if (cmd) {
656 			if ((child_info = fork_child(cmd, msg_str, children))) {
657 				children_list = g_slist_append(children_list,
658 							       child_info);
659 			}
660 			g_free(cmd);
661 		} else
662 			is_ok  = FALSE;         /* ERR: incorrect command */
663 	}
664 
665 	g_free(msg_str);
666 	g_free(sel_str);
667 	g_free(user_str);
668 	g_free(user_hidden_str);
669 
670 	if (!children_list) {
671 		/* If not waiting for children, return */
672 		free_children(children);
673 	} else {
674 		GSList *cur;
675 
676 		children->list	      = children_list;
677 		children->nb	      = g_slist_length(children_list);
678 
679 		create_io_dialog(children);
680 
681 		for (cur = children_list; cur; cur = cur->next) {
682 			child_info = (ChildInfo *) cur->data;
683 			child_info->tag_status =
684 				gdk_input_add(child_info->chld_status,
685 					      GDK_INPUT_READ,
686 					      catch_status, child_info);
687 		}
688 	}
689 
690 	return is_ok;
691 }
692 
fork_child(gchar * cmd,const gchar * msg_str,Children * children)693 static ChildInfo *fork_child(gchar *cmd, const gchar *msg_str,
694 			     Children *children)
695 {
696 #ifdef G_OS_UNIX
697 	gint chld_in[2], chld_out[2], chld_err[2], chld_status[2];
698 	gchar *cmdline[4];
699 	pid_t pid, gch_pid;
700 	ChildInfo *child_info;
701 	gint sync;
702 
703 	sync = !(children->action_type & ACTION_ASYNC);
704 
705 	chld_in[0] = chld_in[1] = chld_out[0] = chld_out[1] = chld_err[0]
706 		= chld_err[1] = chld_status[0] = chld_status[1] = -1;
707 
708 	if (sync) {
709 		if (pipe(chld_status) || pipe(chld_in) || pipe(chld_out) ||
710 		    pipe(chld_err)) {
711 			alertpanel_error(_("Command could not be started. "
712 					   "Pipe creation failed.\n%s"),
713 					g_strerror(errno));
714 			/* Closing fd = -1 fails silently */
715 			close(chld_in[0]);
716 			close(chld_in[1]);
717 			close(chld_out[0]);
718 			close(chld_out[1]);
719 			close(chld_err[0]);
720 			close(chld_err[1]);
721 			close(chld_status[0]);
722 			close(chld_status[1]);
723 			return NULL; /* Pipe error */
724 		}
725 	}
726 
727 	debug_print("Forking child and grandchild in %s mode.\n", sync ? "sync" : "async");
728 	debug_print("Executing: /bin/sh -c %s\n", cmd);
729 
730 	pid = fork();
731 	if (pid == 0) { /* Child */
732 		struct sigaction sa;
733 
734 		if (setpgid(0, 0))
735 			perror("setpgid");
736 
737 		/* reset signal handlers */
738 		memset(&sa, 0, sizeof(sa));
739 		sa.sa_handler = SIG_DFL;
740 		sigaction(SIGHUP, &sa, NULL);
741 		sigaction(SIGINT, &sa, NULL);
742 		sigaction(SIGTERM, &sa, NULL);
743 		sigaction(SIGQUIT, &sa, NULL);
744 		sigaction(SIGPIPE, &sa, NULL);
745 
746 #ifdef GDK_WINDOWING_X11
747 		close(ConnectionNumber(gdk_display));
748 #endif /* GDK_WINDOWING_X11 */
749 
750 		gch_pid = fork();
751 
752 		if (gch_pid == 0) {
753 			if (setpgid(0, getppid()))
754 				perror("setpgid");
755 
756 			if (sync) {
757 				if (children->action_type &
758 				    (ACTION_PIPE_IN |
759 				     ACTION_USER_IN |
760 				     ACTION_USER_HIDDEN_IN)) {
761 					close(fileno(stdin));
762 					dup  (chld_in[0]);
763 				}
764 				close(chld_in[0]);
765 				close(chld_in[1]);
766 
767 				close(fileno(stdout));
768 				dup  (chld_out[1]);
769 				close(chld_out[0]);
770 				close(chld_out[1]);
771 
772 				close(fileno(stderr));
773 				dup  (chld_err[1]);
774 				close(chld_err[0]);
775 				close(chld_err[1]);
776 			}
777 
778 			cmdline[0] = "sh";
779 			cmdline[1] = "-c";
780 			cmdline[2] = cmd;
781 			cmdline[3] = NULL;
782 			execvp("/bin/sh", cmdline);
783 
784 			perror("execvp");
785 			_exit(1);
786 		} else if (gch_pid < (pid_t) 0) { /* Fork error */
787 			if (sync)
788 				write(chld_status[1], "1\n", 2);
789 			perror("fork");
790 			_exit(1);
791 		} else { /* Child */
792 			if (sync) {
793 				close(chld_in[0]);
794 				close(chld_in[1]);
795 				close(chld_out[0]);
796 				close(chld_out[1]);
797 				close(chld_err[0]);
798 				close(chld_err[1]);
799 				close(chld_status[0]);
800 
801 				debug_print("Child: Waiting for grandchild (PID: %d)\n", gch_pid);
802 				waitpid(gch_pid, NULL, 0);
803 				debug_print("Child: grandchild ended (PID: %d)\n", gch_pid);
804 				write(chld_status[1], "0\n", 2);
805 				close(chld_status[1]);
806 			}
807 			_exit(0);
808 		}
809 	} else if (pid < 0) { /* Fork error */
810 		alertpanel_error(_("Could not fork to execute the following "
811 				   "command:\n%s\n%s"),
812 				 cmd, g_strerror(errno));
813 		return NULL;
814 	}
815 
816 	/* Parent */
817 
818 	if (!sync) {
819 		waitpid(pid, NULL, 0);
820 		return NULL;
821 	}
822 
823 	close(chld_in[0]);
824 	if (!(children->action_type &
825 	      (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
826 		close(chld_in[1]);
827 	close(chld_out[1]);
828 	close(chld_err[1]);
829 	close(chld_status[1]);
830 
831 	child_info = g_new0(ChildInfo, 1);
832 
833 	child_info->children    = children;
834 
835 	child_info->pid         = pid;
836 	child_info->cmd         = g_strdup(cmd);
837 	child_info->new_out     = FALSE;
838 	child_info->output      = g_string_new(NULL);
839 	child_info->chld_in     =
840 		(children->action_type &
841 		 (ACTION_PIPE_IN | ACTION_USER_IN | ACTION_USER_HIDDEN_IN))
842 			? chld_in [1] : -1;
843 	child_info->chld_out    = chld_out[0];
844 	child_info->chld_err    = chld_err[0];
845 	child_info->chld_status = chld_status[0];
846 	child_info->tag_in      = -1;
847 	child_info->tag_out     = gdk_input_add(chld_out[0], GDK_INPUT_READ,
848 						catch_output, child_info);
849 	child_info->tag_err     = gdk_input_add(chld_err[0], GDK_INPUT_READ,
850 						catch_output, child_info);
851 
852 	debug_print("Monitoring child (PID: %d)\n", pid);
853 
854 	if (!(children->action_type &
855 	      (ACTION_PIPE_IN | ACTION_PIPE_OUT | ACTION_INSERT)))
856 		return child_info;
857 
858 	if ((children->action_type & ACTION_PIPE_IN) && msg_str) {
859 		write(chld_in[1], msg_str, strlen(msg_str));
860 		if (!(children->action_type &
861 		      (ACTION_USER_IN | ACTION_USER_HIDDEN_IN)))
862 			close(chld_in[1]);
863 		child_info->chld_in = -1; /* No more input */
864 	}
865 
866 	return child_info;
867 #else
868 	return NULL;
869 #endif /* G_OS_UNIX */
870 }
871 
kill_children_cb(GtkWidget * widget,gpointer data)872 static void kill_children_cb(GtkWidget *widget, gpointer data)
873 {
874 #ifdef G_OS_UNIX
875 	GSList *cur;
876 	Children *children = (Children *) data;
877 	ChildInfo *child_info;
878 
879 	for (cur = children->list; cur; cur = cur->next) {
880 		child_info = (ChildInfo *)(cur->data);
881 		debug_print("Killing child: %d\n", child_info->pid);
882 		if (child_info->pid && kill(child_info->pid, SIGTERM) < 0)
883 			perror("kill");
884 	}
885 #endif /* G_OS_UNIX */
886 }
887 
wait_for_children(Children * children)888 static gint wait_for_children(Children *children)
889 {
890 	gboolean new_output;
891 	ChildInfo *child_info;
892 	GSList *cur;
893 	gint nb = children->nb;
894 
895 	debug_print("wait_for_children (%p)\n", children);
896 
897 	children->nb = 0;
898 
899 	cur = children->list;
900 	new_output = FALSE;
901 	while (cur) {
902 		child_info = (ChildInfo *)cur->data;
903 		if (child_info->pid)
904 			children->nb++;
905 		new_output |= child_info->new_out;
906 		cur = cur->next;
907 	}
908 
909 	children->output |= new_output;
910 
911 	if (new_output || (children->dialog && (nb != children->nb)))
912 		update_io_dialog(children);
913 
914 	if (children->nb)
915 		return FALSE;
916 
917 	if (!children->dialog) {
918 		/* free_children(children); */
919 	} else if (!children->output) {
920 		gtk_widget_destroy(children->dialog);
921 		children->dialog = NULL;
922 	}
923 
924 	return FALSE;
925 }
926 
send_input(GtkWidget * w,gpointer data)927 static void send_input(GtkWidget *w, gpointer data)
928 {
929 	Children *children = (Children *) data;
930 	ChildInfo *child_info = (ChildInfo *) children->list->data;
931 
932 	child_info->tag_in = gdk_input_add(child_info->chld_in,
933 					   GDK_INPUT_WRITE,
934 					   catch_input, children);
935 	gtk_widget_set_sensitive(children->input_hbox, FALSE);
936 }
937 
delete_io_dialog_cb(GtkWidget * w,GdkEvent * e,gpointer data)938 static gint delete_io_dialog_cb(GtkWidget *w, GdkEvent *e, gpointer data)
939 {
940 	hide_io_dialog_cb(w, data);
941 	return TRUE;
942 }
943 
hide_io_dialog_cb(GtkWidget * w,gpointer data)944 static void hide_io_dialog_cb(GtkWidget *w, gpointer data)
945 {
946 
947 	Children *children = (Children *)data;
948 
949 	if (!children->nb) {
950 		g_signal_handlers_disconnect_matched
951 			(G_OBJECT(children->dialog),
952 			 (GSignalMatchType)G_SIGNAL_MATCH_DATA,
953 			 0, 0, NULL, NULL, children);
954 		gtk_widget_destroy(children->dialog);
955 		free_children(children);
956 	}
957 }
958 
io_dialog_key_pressed_cb(GtkWidget * widget,GdkEventKey * event,gpointer data)959 static gint io_dialog_key_pressed_cb(GtkWidget *widget, GdkEventKey *event,
960 				     gpointer data)
961 {
962 	if (event && event->keyval == GDK_Escape)
963 		hide_io_dialog_cb(widget, data);
964 	return TRUE;
965 }
966 
childinfo_close_pipes(ChildInfo * child_info)967 static void childinfo_close_pipes(ChildInfo *child_info)
968 {
969 	/* stdout and stderr pipes are guaranteed to be removed by
970 	 * their handler, but in case where we receive child exit notification
971 	 * before grand-child's pipes closing signals, we check them and close
972 	 * them if necessary
973 	 */
974 	if (child_info->tag_in > 0)
975 		gdk_input_remove(child_info->tag_in);
976 	if (child_info->tag_out > 0)
977 		gdk_input_remove(child_info->tag_out);
978 	if (child_info->tag_err > 0)
979 		gdk_input_remove(child_info->tag_err);
980 
981 	if (child_info->chld_in >= 0)
982 		close(child_info->chld_in);
983 	if (child_info->chld_out >= 0)
984 		close(child_info->chld_out);
985 	if (child_info->chld_err >= 0)
986 		close(child_info->chld_err);
987 
988 	close(child_info->chld_status);
989 }
990 
free_children(Children * children)991 static void free_children(Children *children)
992 {
993 	ChildInfo *child_info;
994 
995 	debug_print("Freeing children data %p\n", children);
996 
997 	g_free(children->action);
998 	while (children->list != NULL) {
999 		child_info = (ChildInfo *)children->list->data;
1000 #ifdef G_OS_UNIX
1001 		if (child_info->pid != 0) {
1002 			debug_print("free_children: waiting PID %d\n", child_info->pid);
1003 			if (waitpid(child_info->pid, NULL, 0)
1004 			    != child_info->pid) {
1005 				perror("waitpid (free_children)");
1006 			}
1007 		}
1008 #endif
1009 		g_free(child_info->cmd);
1010 		g_string_free(child_info->output, TRUE);
1011 		children->list = g_slist_remove(children->list, child_info);
1012 		g_free(child_info);
1013 	}
1014 	g_free(children);
1015 }
1016 
update_io_dialog(Children * children)1017 static void update_io_dialog(Children *children)
1018 {
1019 	GSList *cur;
1020 
1021 	debug_print("Updating actions input/output dialog.\n");
1022 
1023 	if (!children->nb) {
1024 		gtk_widget_set_sensitive(children->abort_btn, FALSE);
1025 		gtk_widget_set_sensitive(children->close_btn, TRUE);
1026 		if (children->input_hbox)
1027 			gtk_widget_set_sensitive(children->input_hbox, FALSE);
1028 		gtk_widget_grab_focus(children->close_btn);
1029 		g_signal_connect(G_OBJECT(children->dialog),
1030 				 "key_press_event",
1031 				 G_CALLBACK(io_dialog_key_pressed_cb),
1032 				 children);
1033 	}
1034 
1035 	if (children->output) {
1036 		GtkWidget *text = children->text;
1037 		GtkTextBuffer *textbuf;
1038 		GtkTextIter iter, start_iter, end_iter;
1039 		gchar *caption;
1040 		ChildInfo *child_info;
1041 
1042 		gtk_widget_show(children->scrolledwin);
1043 		textbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
1044 		gtk_text_buffer_get_bounds(textbuf, &start_iter, &end_iter);
1045 		gtk_text_buffer_delete(textbuf, &start_iter, &end_iter);
1046 		gtk_text_buffer_get_start_iter(textbuf, &iter);
1047 
1048 		for (cur = children->list; cur; cur = cur->next) {
1049 			child_info = (ChildInfo *)cur->data;
1050 			if (child_info->pid)
1051 				caption = g_strdup_printf
1052 					(_("--- Running: %s\n"),
1053 					 child_info->cmd);
1054 			else
1055 				caption = g_strdup_printf
1056 					(_("--- Ended: %s\n"),
1057 					 child_info->cmd);
1058 
1059 			gtk_text_buffer_insert(textbuf, &iter, caption, -1);
1060 			gtk_text_buffer_insert(textbuf, &iter,
1061 					       child_info->output->str, -1);
1062 			g_free(caption);
1063 			child_info->new_out = FALSE;
1064 		}
1065 	}
1066 }
1067 
create_io_dialog(Children * children)1068 static void create_io_dialog(Children *children)
1069 {
1070 	GtkWidget *dialog;
1071 	GtkWidget *vbox;
1072 	GtkWidget *entry = NULL;
1073 	GtkWidget *input_hbox = NULL;
1074 	GtkWidget *send_button;
1075 	GtkWidget *label;
1076 	GtkWidget *text;
1077 	GtkWidget *scrolledwin;
1078 	GtkWidget *hbox;
1079 	GtkWidget *abort_button;
1080 	GtkWidget *close_button;
1081 
1082 	debug_print("Creating action IO dialog\n");
1083 
1084 	dialog = gtk_dialog_new();
1085 	gtk_container_set_border_width
1086 		(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
1087 	gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
1088 	gtk_window_set_title(GTK_WINDOW(dialog), _("Action's input/output"));
1089 	gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1090 	manage_window_set_transient(GTK_WINDOW(dialog));
1091 	g_signal_connect(G_OBJECT(dialog), "delete_event",
1092 			 G_CALLBACK(delete_io_dialog_cb), children);
1093 	g_signal_connect(G_OBJECT(dialog), "destroy",
1094 			 G_CALLBACK(hide_io_dialog_cb),
1095 			 children);
1096 
1097 	vbox = gtk_vbox_new(FALSE, 8);
1098 	gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
1099 	gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1100 	gtk_widget_show(vbox);
1101 
1102 	label = gtk_label_new(children->action);
1103 	gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1104 	gtk_widget_show(label);
1105 
1106 	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1107 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1108 				       GTK_POLICY_AUTOMATIC,
1109 				       GTK_POLICY_AUTOMATIC);
1110 	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1111 					    GTK_SHADOW_IN);
1112 	gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
1113 	gtk_widget_set_size_request(scrolledwin,
1114 				    560 * gtkut_get_dpi_multiplier(),
1115 				    200 * gtkut_get_dpi_multiplier());
1116 	gtk_widget_hide(scrolledwin);
1117 
1118 	text = gtk_text_view_new();
1119 
1120 	if (prefs_common.textfont) {
1121 		PangoFontDescription *font_desc;
1122 		font_desc = pango_font_description_from_string
1123 			(prefs_common.textfont);
1124 		if (font_desc) {
1125 			gtk_widget_modify_font(text, font_desc);
1126 			pango_font_description_free(font_desc);
1127 		}
1128 	}
1129 
1130 	gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
1131 	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
1132 	gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
1133 	gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
1134 	gtk_container_add(GTK_CONTAINER(scrolledwin), text);
1135 	gtk_widget_show(text);
1136 
1137 	if (children->open_in) {
1138 		input_hbox = gtk_hbox_new(FALSE, 8);
1139 		gtk_widget_show(input_hbox);
1140 
1141 		entry = gtk_entry_new();
1142 		gtk_widget_set_size_request
1143 			(entry, 320 * gtkut_get_dpi_multiplier(), -1);
1144 		g_signal_connect(G_OBJECT(entry), "activate",
1145 				 G_CALLBACK(send_input), children);
1146 		gtk_box_pack_start(GTK_BOX(input_hbox), entry, TRUE, TRUE, 0);
1147 		if (children->action_type & ACTION_USER_HIDDEN_IN)
1148 			gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
1149 		gtk_widget_show(entry);
1150 
1151 		send_button = gtk_button_new_with_label(_(" Send "));
1152 		g_signal_connect(G_OBJECT(send_button), "clicked",
1153 				 G_CALLBACK(send_input), children);
1154 		gtk_box_pack_start(GTK_BOX(input_hbox), send_button, FALSE,
1155 				   FALSE, 0);
1156 		gtk_widget_show(send_button);
1157 
1158 		gtk_box_pack_start(GTK_BOX(vbox), input_hbox, FALSE, FALSE, 0);
1159 		gtk_widget_grab_focus(entry);
1160 	}
1161 
1162 	gtkut_stock_button_set_create(&hbox, &abort_button, _("Abort"),
1163 				      &close_button, GTK_STOCK_CLOSE,
1164 				      NULL, NULL);
1165 	g_signal_connect(G_OBJECT(abort_button), "clicked",
1166 			 G_CALLBACK(kill_children_cb), children);
1167 	g_signal_connect(G_OBJECT(close_button), "clicked",
1168 			 G_CALLBACK(hide_io_dialog_cb), children);
1169 	gtk_widget_show(hbox);
1170 
1171 	if (children->nb)
1172 		gtk_widget_set_sensitive(close_button, FALSE);
1173 
1174 	gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1175 
1176 	children->dialog      = dialog;
1177 	children->scrolledwin = scrolledwin;
1178 	children->text        = text;
1179 	children->input_hbox  = children->open_in ? input_hbox : NULL;
1180 	children->input_entry = children->open_in ? entry : NULL;
1181 	children->abort_btn   = abort_button;
1182 	children->close_btn   = close_button;
1183 
1184 	gtk_widget_show(dialog);
1185 }
1186 
catch_status(gpointer data,gint source,GdkInputCondition cond)1187 static void catch_status(gpointer data, gint source, GdkInputCondition cond)
1188 {
1189 	ChildInfo *child_info = (ChildInfo *)data;
1190 	gchar buf;
1191 	gint c;
1192 
1193 	debug_print("Catching child status (child PID: %d).\n", child_info->pid);
1194 
1195 	gdk_threads_enter();
1196 
1197 	gdk_input_remove(child_info->tag_status);
1198 
1199 	c = read(source, &buf, 1);
1200 	if (c == 1)
1201 		debug_print("Child (PID: %d) returned %c\n", child_info->pid, buf);
1202 	else
1203 		debug_print("Could not get child (PID: %d) status\n", child_info->pid);
1204 
1205 #ifdef G_OS_UNIX
1206 	waitpid(child_info->pid, NULL, 0);
1207 #endif
1208 	childinfo_close_pipes(child_info);
1209 	child_info->pid = 0;
1210 
1211 	wait_for_children(child_info->children);
1212 
1213 	gdk_threads_leave();
1214 }
1215 
catch_input(gpointer data,gint source,GdkInputCondition cond)1216 static void catch_input(gpointer data, gint source, GdkInputCondition cond)
1217 {
1218 	Children *children = (Children *)data;
1219 	ChildInfo *child_info = (ChildInfo *)children->list->data;
1220 	gchar *input;
1221 	gint c, count, len;
1222 
1223 	debug_print("Sending input to grand child.\n");
1224 	if (!(cond && GDK_INPUT_WRITE))
1225 		return;
1226 
1227 	gdk_threads_enter();
1228 
1229 	gdk_input_remove(child_info->tag_in);
1230 	child_info->tag_in = -1;
1231 
1232 	input = gtk_editable_get_chars(GTK_EDITABLE(children->input_entry),
1233 				       0, -1);
1234 	len = strlen(input);
1235 	count = 0;
1236 
1237 	do {
1238 		c = write(child_info->chld_in, input + count, len - count);
1239 		if (c >= 0)
1240 			count += c;
1241 	} while (c >= 0 && count < len);
1242 
1243 	if (c >= 0)
1244 		write(child_info->chld_in, "\n", 2);
1245 
1246 	g_free(input);
1247 
1248 	gtk_entry_set_text(GTK_ENTRY(children->input_entry), "");
1249 	gtk_widget_set_sensitive(children->input_hbox, TRUE);
1250 	close(child_info->chld_in);
1251 	child_info->chld_in = -1;
1252 	debug_print("Input to grand child sent.\n");
1253 
1254 	gdk_threads_leave();
1255 }
1256 
catch_output(gpointer data,gint source,GdkInputCondition cond)1257 static void catch_output(gpointer data, gint source, GdkInputCondition cond)
1258 {
1259 	ChildInfo *child_info = (ChildInfo *)data;
1260 	gint c;
1261 	gchar buf[BUFFSIZE];
1262 
1263 	gdk_threads_enter();
1264 
1265 	debug_print("Catching grand child's output (child PID: %d).\n", child_info->pid);
1266 	if (child_info->children->action_type &
1267 	    (ACTION_PIPE_OUT | ACTION_INSERT)
1268 	    && source == child_info->chld_out) {
1269 		GtkTextView *text =
1270 			GTK_TEXT_VIEW(child_info->children->msg_text);
1271 		GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
1272 		GtkTextIter iter;
1273 		GtkTextMark *mark;
1274 		gint ins_pos;
1275 
1276 		mark = gtk_text_buffer_get_insert(textbuf);
1277 		gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1278 		ins_pos = gtk_text_iter_get_offset(&iter);
1279 
1280 		while (TRUE) {
1281 			gsize bytes_read = 0, bytes_written = 0;
1282 			gchar *ret_str;
1283 
1284 			c = read(source, buf, sizeof(buf) - 1);
1285 			if (c == 0)
1286 				break;
1287 
1288 			ret_str = g_locale_to_utf8
1289 				(buf, c, &bytes_read, &bytes_written, NULL);
1290 			if (ret_str && bytes_written > 0) {
1291 				gtk_text_buffer_insert
1292 					(textbuf, &iter, ret_str,
1293 					 bytes_written);
1294 				g_free(ret_str);
1295 			} else
1296 				gtk_text_buffer_insert(textbuf, &iter, buf, c);
1297 		}
1298 
1299 		if (child_info->children->is_selection) {
1300 			GtkTextIter ins;
1301 
1302 			gtk_text_buffer_get_iter_at_offset
1303 				(textbuf, &ins, ins_pos);
1304 			gtk_text_buffer_select_range(textbuf, &ins, &iter);
1305 		}
1306 	} else {
1307 		c = read(source, buf, sizeof(buf) - 1);
1308 		if (c > 0) {
1309 			gsize bytes_read = 0, bytes_written = 0;
1310 			gchar *ret_str;
1311 
1312 			ret_str = g_locale_to_utf8
1313 				(buf, c, &bytes_read, &bytes_written, NULL);
1314 			if (ret_str && bytes_written > 0) {
1315 				g_string_append_len
1316 					(child_info->output, ret_str,
1317 					 bytes_written);
1318 				g_free(ret_str);
1319 			} else
1320 				g_string_append_len(child_info->output, buf, c);
1321 
1322 			child_info->new_out = TRUE;
1323 		}
1324 	}
1325 	if (c == 0) {
1326 		if (source == child_info->chld_out) {
1327 			gdk_input_remove(child_info->tag_out);
1328 			child_info->tag_out = -1;
1329 			close(child_info->chld_out);
1330 			child_info->chld_out = -1;
1331 		} else {
1332 			gdk_input_remove(child_info->tag_err);
1333 			child_info->tag_err = -1;
1334 			close(child_info->chld_err);
1335 			child_info->chld_err = -1;
1336 		}
1337 	}
1338 
1339 	wait_for_children(child_info->children);
1340 
1341 	gdk_threads_leave();
1342 }
1343 
get_user_string(const gchar * action,ActionType type)1344 static gchar *get_user_string(const gchar *action, ActionType type)
1345 {
1346 	gchar *message;
1347 	gchar *user_str = NULL;
1348 
1349 	switch (type) {
1350 	case ACTION_USER_HIDDEN_STR:
1351 		message = g_strdup_printf
1352 			(_("Enter the argument for the following action:\n"
1353 			   "(`%%h' will be replaced with the argument)\n"
1354 			   "  %s"),
1355 			 action);
1356 		user_str = input_dialog_with_invisible
1357 			(_("Action's hidden user argument"), message, NULL);
1358 		break;
1359 	case ACTION_USER_STR:
1360 		message = g_strdup_printf
1361 			(_("Enter the argument for the following action:\n"
1362 			   "(`%%u' will be replaced with the argument)\n"
1363 			   "  %s"),
1364 			 action);
1365 		user_str = input_dialog
1366 			(_("Action's user argument"), message, NULL);
1367 		break;
1368 	default:
1369 		g_warning("Unsupported action type %d", type);
1370 	}
1371 
1372 	return user_str;
1373 }
1374