3 * $Id: xcatch.c,v 1.3 1998/12/20 17:19:16 mdw Exp $
5 * Catch input and trap it in an X window
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Edgeware X tools collection.
14 * X tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * X tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with X tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1998/12/20 17:19:16 mdw
33 * Return exit status of child process, rather than always returning
36 * Revision 1.2 1998/12/16 00:10:58 mdw
37 * Fix tabbing in help text.
39 * Revision 1.1 1998/12/15 23:46:50 mdw
40 * New program: captures input and puts it in a window.
44 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
60 #include <mLib/dstr.h>
61 #include <mLib/mdwopt.h>
62 #include <mLib/report.h>
63 #include <mLib/quis.h>
65 #include <mgLib/cancel.h>
66 #include <mgLib/msg.h>
68 /*----- Inportant state ---------------------------------------------------*/
70 static unsigned int flags
;
77 static GtkWidget
*textbox
= 0;
80 static pid_t kid
= -1;
83 /*----- Main code ---------------------------------------------------------*/
85 /* --- The window's closed --- */
87 static void killwin(GtkWidget
*w
, gpointer p
)
95 /* --- Some input has arrived --- */
97 static void ready(gpointer data
, gint fd
, GdkInputCondition c
)
102 /* --- Read the next buffer of data --- */
104 if (!(c
& GDK_INPUT_READ
))
106 count
= read(fd
, buf
, sizeof(buf
));
108 /* --- Decide what to do --- */
111 msg(":~OK", "error reading data: %s", strerror(errno
));
124 /* --- If there's no output window, create one --- */
131 win
= gtk_dialog_new();
132 gtk_signal_connect(GTK_OBJECT(win
), "destroy",
133 GTK_SIGNAL_FUNC(killwin
), 0);
135 t
= gtk_table_new(2, 2, 0);
136 gtk_container_border_width(GTK_CONTAINER(t
), 8);
137 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win
)->vbox
), t
, 1, 1, 0);
140 textbox
= gtk_text_new(0, 0);
141 gtk_table_attach(GTK_TABLE(t
), textbox
, 0, 1, 0, 1,
142 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
143 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
,
145 gtk_text_set_editable(GTK_TEXT(textbox
), 0);
146 gtk_widget_set_usize(textbox
, 500, 100);
147 gtk_widget_show(textbox
);
149 w
= gtk_vscrollbar_new(GTK_TEXT(textbox
)->vadj
);
150 gtk_table_attach(GTK_TABLE(t
), w
, 1, 2, 0, 1,
151 0, GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
, 0, 0);
154 w
= gtk_hscrollbar_new(GTK_TEXT(textbox
)->hadj
);
155 gtk_table_attach(GTK_TABLE(t
), w
, 0, 1, 1, 2,
156 GTK_EXPAND
| GTK_SHRINK
| GTK_FILL
, 0, 0, 0);
159 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win
)->action_area
), 0);
160 w
= gtk_button_new_with_label("Dismiss");
161 gtk_signal_connect_object(GTK_OBJECT(w
), "clicked",
162 GTK_SIGNAL_FUNC(gtk_object_destroy
),
164 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win
)->action_area
), w
, 0, 0, 0);
165 GTK_WIDGET_SET_FLAGS(w
, GTK_CAN_DEFAULT
);
166 gtk_widget_grab_default(w
);
167 cancel(GTK_WINDOW(win
), w
);
170 gtk_widget_show(win
);
173 /* --- Append the new text --- */
175 gtk_text_insert(GTK_TEXT(textbox
), font
, 0, 0, buf
, count
);
178 /* --- Signal handler --- */
180 static void reap(int sig
)
186 k
= waitpid(-1, &s
, WNOHANG
);
191 status
= WEXITSTATUS(s
);
198 /* --- Main program --- */
200 static void version(FILE *fp
)
202 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
205 static void usage(FILE *fp
)
207 fprintf(fp
, "Usage: %s [-f file] [-F font] [command [args...]]\n", QUIS
);
210 int main(int argc
, char *argv
[])
216 gtk_init(&argc
, &argv
);
219 static struct option opt
[] = {
220 { "help", 0, 0, 'h' },
221 { "usage", 0, 0, 'u' },
222 { "version", 0, 0, 'v' },
223 { "file", gFlag_argReq
, 0, 'f' },
224 { "font", gFlag_argReq
, 0, 'F' },
227 int i
= mdwopt(argc
, argv
, "huvf:F:", opt
, 0, 0, 0);
239 "Catches input from a pipe or other source, and captures it in a window.\n"
240 "Nothing is displayed if there's no input.\n"
242 "Options provided:\n"
244 "-h, --help Display this help text\n"
245 "-u, --usage Display a quick usage summary\n"
246 "-v, --version Display the version number\n"
247 "-f, --file=FILE\t Read input from the named file\n"
248 "-F, --font=FONT\t Display output in the named font\n",
261 if ((fd
= open(optarg
, O_RDONLY
)) < 0) {
262 die(1, "couldn't open file: %s", strerror(errno
));
267 font
= gdk_font_load(optarg
);
275 if (flags
& f_bogus
) {
287 /* --- Set up a signal handler --- */
289 sa
.sa_handler
= reap
;
290 sigemptyset(&sa
.sa_mask
);
292 sigaction(SIGCHLD
, &sa
, 0);
294 /* --- Start a child program --- */
297 die(1, "couldn't open pipe: %s", strerror(errno
));
300 die(1, "couldn't fork: %s", strerror(errno
));
305 if (pfd
[1] != STDOUT_FILENO
)
306 dup2(pfd
[1], STDOUT_FILENO
);
307 if (pfd
[1] != STDERR_FILENO
)
308 dup2(pfd
[1], STDERR_FILENO
);
309 if (pfd
[1] != STDOUT_FILENO
&& pfd
[1] != STDERR_FILENO
)
311 execvp(argv
[optind
], argv
+ optind
);
314 dstr_putf(&d
, "%s: couldn't run `%s': %s\n",
315 QUIS
, argv
[optind
], strerror(errno
));
316 write(STDERR_FILENO
, d
.buf
, d
.len
);
324 gdk_input_add(fd
, GDK_INPUT_READ
, ready
, 0);
329 /*----- That's all, folks -------------------------------------------------*/