3 * $Id: xcatch.c,v 1.1 1998/12/15 23:46:50 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.1 1998/12/15 23:46:50 mdw
33 * New program: captures input and puts it in a window.
37 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/report.h>
56 #include <mLib/quis.h>
58 #include <mgLib/cancel.h>
59 #include <mgLib/msg.h>
61 /*----- Inportant state ---------------------------------------------------*/
63 static unsigned int flags;
70 GtkWidget *textbox = 0;
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- The window's closed --- */
77 static void killwin(GtkWidget *w, gpointer p)
85 /* --- Some input has arrived --- */
87 static void ready(gpointer data, gint fd, GdkInputCondition c)
92 /* --- Read the next buffer of data --- */
94 if (!(c & GDK_INPUT_READ))
96 count = read(fd, buf, sizeof(buf));
98 /* --- Decide what to do --- */
101 msg(":~OK", "error reading data: %s", strerror(errno));
114 /* --- If there's no output window, create one --- */
121 win = gtk_dialog_new();
122 gtk_signal_connect(GTK_OBJECT(win), "destroy",
123 GTK_SIGNAL_FUNC(killwin), 0);
125 t = gtk_table_new(2, 2, 0);
126 gtk_container_border_width(GTK_CONTAINER(t), 8);
127 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), t, 1, 1, 0);
130 textbox = gtk_text_new(0, 0);
131 gtk_table_attach(GTK_TABLE(t), textbox, 0, 1, 0, 1,
132 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
133 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
135 gtk_text_set_editable(GTK_TEXT(textbox), 0);
136 gtk_widget_set_usize(textbox, 500, 100);
137 gtk_widget_show(textbox);
139 w = gtk_vscrollbar_new(GTK_TEXT(textbox)->vadj);
140 gtk_table_attach(GTK_TABLE(t), w, 1, 2, 0, 1,
141 0, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
144 w = gtk_hscrollbar_new(GTK_TEXT(textbox)->hadj);
145 gtk_table_attach(GTK_TABLE(t), w, 0, 1, 1, 2,
146 GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0, 0);
149 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
150 w = gtk_button_new_with_label("Dismiss");
151 gtk_signal_connect_object(GTK_OBJECT(w), "clicked",
152 GTK_SIGNAL_FUNC(gtk_object_destroy),
154 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
155 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
156 gtk_widget_grab_default(w);
157 cancel(GTK_WINDOW(win), w);
160 gtk_widget_show(win);
163 /* --- Append the new text --- */
165 gtk_text_insert(GTK_TEXT(textbox), font, 0, 0, buf, count);
168 /* --- Signal handler --- */
170 static void reap(int sig)
172 while (waitpid(-1, 0, WNOHANG) > 0)
176 /* --- Main program --- */
178 static void version(FILE *fp)
180 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
183 static void usage(FILE *fp)
185 fprintf(fp, "Usage: %s [-f file] [-F font] [command [args...]]\n", QUIS);
188 int main(int argc, char *argv[])
194 gtk_init(&argc, &argv);
197 static struct option opt[] = {
198 { "help", 0, 0, 'h' },
199 { "usage", 0, 0, 'u' },
200 { "version", 0, 0, 'v' },
201 { "file", gFlag_argReq, 0, 'f' },
202 { "font", gFlag_argReq, 0, 'F' },
205 int i = mdwopt(argc, argv, "huvf:F:", opt, 0, 0, 0);
217 "Catches input from a pipe or other source, and captures it in a window.\n"
218 "Nothing is displayed if there's no input.\n"
220 "Options provided:\n"
222 "-h, --help Display this help text\n"
223 "-u, --usage Display a quick usage summary\n"
224 "-v, --version Display the version number\n"
225 "-f, --file=FILE Read input from the named file\n"
226 "-F, --font=FONT Display output in the named font\n",
239 if ((fd = open(optarg, O_RDONLY)) < 0) {
240 die(1, "couldn't open file: %s", strerror(errno));
245 font = gdk_font_load(optarg);
253 if (flags & f_bogus) {
266 /* --- Set up a signal handler --- */
268 sa.sa_handler = reap;
269 sigemptyset(&sa.sa_mask);
271 sigaction(SIGCHLD, &sa, 0);
273 /* --- Start a child program --- */
276 die(1, "couldn't open pipe: %s", strerror(errno));
279 die(1, "couldn't fork: %s", strerror(errno));
284 if (pfd[1] != STDOUT_FILENO)
285 dup2(pfd[1], STDOUT_FILENO);
286 if (pfd[1] != STDERR_FILENO)
287 dup2(pfd[1], STDERR_FILENO);
288 if (pfd[1] != STDOUT_FILENO && pfd[1] != STDERR_FILENO)
290 execvp(argv[optind], argv + optind);
293 dstr_putf(&d, "%s: couldn't run `%s': %s\n",
294 QUIS, argv[optind], strerror(errno));
295 write(STDERR_FILENO, d.buf, d.len);
303 gdk_input_add(fd, GDK_INPUT_READ, ready, 0);
308 /*----- That's all, folks -------------------------------------------------*/