3 * $Id: xcatch.c,v 1.4 1999/03/24 22:23:57 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.4 1999/03/24 22:23:57 mdw
33 * Improve display for large files. Keep newly added material in view if
34 * scrolled to bottom of window.
36 * Revision 1.3 1998/12/20 17:19:16 mdw
37 * Return exit status of child process, rather than always returning
40 * Revision 1.2 1998/12/16 00:10:58 mdw
41 * Fix tabbing in help text.
43 * Revision 1.1 1998/12/15 23:46:50 mdw
44 * New program: captures input and puts it in a window.
48 /*----- Header files ------------------------------------------------------*/
56 #include <sys/types.h>
64 #include <mLib/dstr.h>
65 #include <mLib/mdwopt.h>
66 #include <mLib/report.h>
67 #include <mLib/quis.h>
69 #include <mgLib/cancel.h>
70 #include <mgLib/msg.h>
72 /*----- Inportant state ---------------------------------------------------*/
74 static unsigned int flags;
81 static GtkWidget *textbox = 0;
84 static pid_t kid = -1;
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- The window's closed --- */
91 static void killwin(GtkWidget *w, gpointer p)
99 /* --- Some input has arrived --- */
101 static void ready(gpointer data, gint fd, GdkInputCondition c)
108 /* --- If not ready to read then go away --- */
110 if (!(c & GDK_INPUT_READ))
113 /* --- Decide whether to scroll the window --- */
116 t = GTK_TEXT(textbox);
118 if (va->value + va->page_size < va->upper)
123 /* --- Read data into the buffer --- *
125 * This is a bit of a mess.
129 int r = read(fd, buf, sizeof(buf));
131 /* --- The read failed --- *
133 * Maybe there's no more data to read. In this case, we get
134 * @EWOULDBLOCK@, indicating it's time to stop and do something else.
135 * Otherwise something serious has happened.
139 if (errno == EWOULDBLOCK)
141 msg(":~OK", "error reading data: %s", strerror(errno));
145 /* --- End of file --- *
147 * If the box is closed, then exit quiety; otherwise wait for it to
160 /* --- If there's no output window, create one --- */
167 win = gtk_dialog_new();
168 gtk_signal_connect(GTK_OBJECT(win), "destroy",
169 GTK_SIGNAL_FUNC(killwin), 0);
171 tbl = gtk_table_new(2, 2, 0);
172 gtk_container_border_width(GTK_CONTAINER(tbl), 8);
173 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), tbl, 1, 1, 0);
174 gtk_widget_show(tbl);
176 textbox = gtk_text_new(0, 0);
177 t = GTK_TEXT(textbox);
179 gtk_table_attach(GTK_TABLE(tbl), textbox, 0, 1, 0, 1,
180 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
181 GTK_EXPAND | GTK_SHRINK | GTK_FILL,
183 gtk_text_set_editable(t, 0);
184 gtk_widget_set_usize(textbox, 500, 300);
186 gtk_widget_show(textbox);
188 w = gtk_vscrollbar_new(va);
189 gtk_table_attach(GTK_TABLE(tbl), w, 1, 2, 0, 1,
190 0, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
193 w = gtk_hscrollbar_new(t->hadj);
194 gtk_table_attach(GTK_TABLE(tbl), w, 0, 1, 1, 2,
195 GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0, 0);
198 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
199 w = gtk_button_new_with_label("Dismiss");
200 gtk_signal_connect_object(GTK_OBJECT(w), "clicked",
201 GTK_SIGNAL_FUNC(gtk_object_destroy),
203 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
204 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
205 gtk_widget_grab_default(w);
206 cancel(GTK_WINDOW(win), w);
209 gtk_widget_show(win);
212 gtk_text_insert(t, font, 0, 0, buf, r);
218 gtk_adjustment_set_value(va, va->upper - va->page_size);
222 /* --- Signal handler --- */
224 static void reap(int sig)
230 k = waitpid(-1, &s, WNOHANG);
235 status = WEXITSTATUS(s);
242 /* --- Main program --- */
244 static void version(FILE *fp)
246 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
249 static void usage(FILE *fp)
251 fprintf(fp, "Usage: %s [-f file] [-F font] [command [args...]]\n", QUIS);
254 int main(int argc, char *argv[])
260 gtk_init(&argc, &argv);
263 static struct option opt[] = {
264 { "help", 0, 0, 'h' },
265 { "usage", 0, 0, 'u' },
266 { "version", 0, 0, 'v' },
267 { "file", gFlag_argReq, 0, 'f' },
268 { "font", gFlag_argReq, 0, 'F' },
271 int i = mdwopt(argc, argv, "huvf:F:", opt, 0, 0, 0);
283 "Catches input from a pipe or other source, and captures it in a window.\n"
284 "Nothing is displayed if there's no input.\n"
286 "Options provided:\n"
288 "-h, --help Display this help text\n"
289 "-u, --usage Display a quick usage summary\n"
290 "-v, --version Display the version number\n"
291 "-f, --file=FILE\t Read input from the named file\n"
292 "-F, --font=FONT\t Display output in the named font\n",
305 if ((fd = open(optarg, O_RDONLY)) < 0) {
306 die(1, "couldn't open file: %s", strerror(errno));
311 font = gdk_font_load(optarg);
319 if (flags & f_bogus) {
331 /* --- Set up a signal handler --- */
333 sa.sa_handler = reap;
334 sigemptyset(&sa.sa_mask);
336 sigaction(SIGCHLD, &sa, 0);
338 /* --- Start a child program --- */
341 die(1, "couldn't open pipe: %s", strerror(errno));
344 die(1, "couldn't fork: %s", strerror(errno));
349 if (pfd[1] != STDOUT_FILENO)
350 dup2(pfd[1], STDOUT_FILENO);
351 if (pfd[1] != STDERR_FILENO)
352 dup2(pfd[1], STDERR_FILENO);
353 if (pfd[1] != STDOUT_FILENO && pfd[1] != STDERR_FILENO)
355 execvp(argv[optind], argv + optind);
358 dstr_putf(&d, "%s: couldn't run `%s': %s\n",
359 QUIS, argv[optind], strerror(errno));
360 write(STDERR_FILENO, d.buf, d.len);
369 int f = fcntl(fd, F_GETFL);
370 fcntl(fd, F_SETFL, f | O_NONBLOCK);
373 gdk_input_add(fd, GDK_INPUT_READ, ready, 0);
378 /*----- That's all, folks -------------------------------------------------*/