home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* Copyright 1988, 1989 by Chuck Musciano and Harris Corporation */
- /* */
- /* Permission to use, copy, modify, and distribute this software */
- /* and its documentation for any purpose and without fee is */
- /* hereby granted, provided that the above copyright notice */
- /* appear in all copies and that both that copyright notice and */
- /* this permission notice appear in supporting documentation, and */
- /* that the name of Chuck Musciano and Harris Corporation not be */
- /* used in advertising or publicity pertaining to distribution */
- /* of the software without specific, written prior permission. */
- /* Chuck Musciano and Harris Corporation make no representations */
- /* about the suitability of this software for any purpose. It is */
- /* provided "as is" without express or implied warranty. This */
- /* software may not be sold without the prior explicit permission */
- /* of Harris Corporation. */
- /************************************************************************/
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <sys/file.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include <suntool/sunview.h>
- #include <suntool/textsw.h>
- #include <suntool/icon_load.h>
-
- #include "contool.h"
-
- EXPORT char filter_path[256]; /* -c */
- EXPORT int blink_icon = TRUE; /* -i */
- EXPORT int pop_open = FALSE; /* -p */
- EXPORT int beep_amount = BEEP_COUNT; /* -s */
- EXPORT int do_time_stamp = TRUE;
- EXPORT f_ptr filters = NULL;
- EXPORT f_ptr curr_filter = NULL;
-
- PUBLIC Frame confirmer;
-
- PRIVATE char *ct_usage = "usage: contool [-b <file>] [-c <file>] [-d <size>] [-f <file>] [-g <file>] [-l <size>] [-n] [-o <logfile>] [-p] [-r <amt>] [-s <amt>]\n";
-
- PRIVATE Frame bf;
- PRIVATE Frame dialog = NULL;
- PRIVATE Textsw text;
- PRIVATE Icon good, bad, inverse;
- PRIVATE struct pixrect *good_pr, *bad_pr, *inv_pr;
- PRIVATE Menu_item stop_blink;
- PRIVATE Rect open_rect;
-
- PRIVATE char bad_icon[512]; /* -b */
- PRIVATE int delete_amt = TEXT_DELETE_SIZE; /* -d */
- PRIVATE char inv_icon[512]; /* -f */
- PRIVATE char good_icon[512]; /* -g */
- PRIVATE int size_limit = TEXT_SIZE_LIMIT; /* -l */
- PRIVATE int resolution = TS_INTERVAL; /* -r */
-
- PRIVATE int bad_is_up;
- PRIVATE int beep_count;
- PRIVATE int blinking = FALSE;
- PRIVATE int event_in_progress = FALSE;
- PRIVATE int explicit_filters = FALSE;
- PRIVATE int icon_height;
- PRIVATE int icon_width;
- PRIVATE FILE *master = NULL;
- PRIVATE int old_time = 0;
- PRIVATE char *program;
- PRIVATE FILE *slave = NULL;
- PRIVATE FILE *logfile = NULL;
-
- PRIVATE struct itimerval timer = {{0, 500000}, {0, 500000}};
-
- /************************************************************************/
- /* First, some basic console utility routines */
- /************************************************************************/
-
- /************************************************************************/
- PRIVATE acquire_console(path)
-
- char *path;
-
- {
- if (ioctl(fileno(slave), TIOCCONS, NULL) == -1) {
- fprintf(stderr, "%s: could not attach %s to /dev/console\n", program, path);
- exit(1);
- }
- }
-
- /************************************************************************/
- PRIVATE clear_messages()
-
- {
- textsw_reset(text, 0, 0);
- old_time = 0;
- }
-
- /************************************************************************/
- PRIVATE stop_blinking()
-
- {
- notify_set_itimer_func(bf, NULL, ITIMER_REAL, NULL, NULL);
- window_set(bf, FRAME_ICON, good, 0);
- blinking = FALSE;
- menu_set(stop_blink, MENU_INACTIVE, TRUE, 0);
- }
-
- /************************************************************************/
- PRIVATE edit_filters()
-
- { Frame create_dialog_box();
-
- if (dialog == NULL)
- dialog = create_dialog_box(bf);
- update_edit_dialog(filters);
- update_defaults();
- window_set(dialog, WIN_SHOW, TRUE, 0);
- }
-
- /************************************************************************/
- /* Now, filter and regular expression handlers */
- /************************************************************************/
-
- /************************************************************************/
- PRIVATE internal_message(a, b, c, d, e, f)
-
- int a, b, c, d, e, f;
-
- { char buf[512];
-
- sprintf(buf, a, b, c, d, e, f);
- time_stamp();
- write_log(buf);
- do_insertion(buf, strlen(buf));
- }
-
- /************************************************************************/
- PRIVATE internal_error(a, b, c, d, e, f)
-
- int a, b, c, d, e, f;
-
- { char buf[512];
-
- sprintf(buf, a, b, c, d, e, f);
- time_stamp();
- fprintf(stderr, "*** %s: %s\n", program, buf);
- }
-
- /************************************************************************/
- PRIVATE load_filters()
-
- { char *result;
- struct stat sb;
- f_ptr new_filters, f;
- static int load_time = 0;
-
- if (access(filter_path, R_OK) == -1) {
- if (explicit_filters && load_time == 0) {
- internal_error("filter file %s cannot be accessed", filter_path);
- load_time = 1;
- }
- return;
- }
- if (stat(filter_path, &sb) == 0 && sb.st_mtime > load_time)
- if (new_filters = read_filters(filter_path, internal_error)) {
- for (f = new_filters; f->start; f++)
- if (result = compile_exp(f, f->start, f->end)) {
- internal_error(result);
- f->valid = FALSE;
- }
- free_filters(filters);
- filters = new_filters;
- internal_message("*** filters loaded from %s\n", filter_path);
- load_time = sb.st_mtime;
- }
- }
-
- /************************************************************************/
- /* Various event handlers for the console frame */
- /************************************************************************/
-
- /************************************************************************/
- PRIVATE Notify_value blink_proc(me, which)
-
- int *me;
- int which;
-
- {
- if (event_in_progress)
- return(NOTIFY_DONE);
- if (beep_count > 0) {
- window_bell(bf);
- beep_count--;
- }
- if (blinking) {
- if (bad_is_up)
- window_set(bf, FRAME_ICON, inverse, 0);
- else
- window_set(bf, FRAME_ICON, bad, 0);
- bad_is_up = !bad_is_up;
- }
- if (beep_count == 0 && !blinking)
- notify_set_itimer_func(bf, blink_proc, ITIMER_REAL, NULL, NULL);
- return(NOTIFY_DONE);
- }
-
- /************************************************************************/
- PRIVATE Notify_value close_proc(frame, event, arg, type)
-
- Frame frame;
- Event *event;
- Notify_arg arg;
- Notify_event_type type;
-
- { int init_closed, curr_closed, is_resize;
- Notify_value value;
- Rect *temp;
-
- event_in_progress = TRUE;
- init_closed = (int) window_get(frame, FRAME_CLOSED);
- is_resize = (event_id(event) == WIN_RESIZE);
- value = notify_next_event_func(frame, event, arg, type);
- curr_closed = (int) window_get(frame, FRAME_CLOSED);
- if (init_closed != curr_closed)
- if (!curr_closed && blinking) {
- notify_set_itimer_func(bf, blink_proc, ITIMER_REAL, NULL, NULL);
- window_set(bf, FRAME_ICON, good, 0);
- blinking = FALSE;
- menu_set(stop_blink, MENU_INACTIVE, TRUE, 0);
- }
- event_in_progress = FALSE;
- if (is_resize) {
- temp = (Rect *) window_get(frame, FRAME_OPEN_RECT);
- if (temp->r_width <= icon_width && temp->r_height <= icon_height) { /* override spurious resize request */
- window_set(frame, FRAME_OPEN_RECT, &open_rect, FRAME_CLOSED, FALSE, 0);
- notify_set_itimer_func(bf, blink_proc, ITIMER_REAL, NULL, NULL);
- window_set(bf, FRAME_ICON, good, 0);
- blinking = FALSE;
- menu_set(stop_blink, MENU_INACTIVE, TRUE, 0);
- }
- else /* save away new open rect */
- open_rect = *temp;
- }
- return(value);
- }
-
- /************************************************************************/
- PRIVATE Notify_value destroy_proc(frame, status)
-
- Frame frame;
- Destroy_status status;
-
- {
- if (status == DESTROY_CHECKING) {
- if (dialog)
- window_destroy(dialog);
- if (confirmer)
- window_destroy(confirmer);
- textsw_reset(text, 0, 0);
- return(NOTIFY_DONE);
- }
- else
- return(notify_next_destroy_func(frame, status));
- }
-
- /************************************************************************/
- /* Routines which handle capturing and displaying messages */
- /************************************************************************/
-
- /************************************************************************/
- PRIVATE write_log(s)
-
- char *s;
-
- { int t;
- static char hostname[100] = "";
-
- if (logfile) {
- if (*hostname == NULL)
- if (gethostname(hostname, 99) != 0)
- strcpy(hostname, "(unknown)");
- t = time(0);
- fseek(logfile, 0L, 2);
- fprintf(logfile, "%s\t%.16s\t%s", hostname, ctime(&t) + 4, s);
- fflush(logfile);
- }
- }
-
- /************************************************************************/
- PRIVATE do_insertion(buf, len)
-
- char *buf;
- int len;
-
- { int first, last;
-
- while (len > size_limit - ((int) window_get(text, TEXTSW_LENGTH) - TEXT_SIZE_FUZZ)) { /* make some room */
- first = 1;
- last = TEXTSW_INFINITY;
- if (textsw_find_bytes(text, &first, &last, "\n<<<", 4, 0) == -1)
- if (textsw_find_bytes(text, &first, &last, "\n", 1, 0) == -1)
- first = delete_amt;
- textsw_delete(text, 0, first + 1);
- }
- window_set(text, TEXTSW_INSERTION_POINT, TEXTSW_INFINITY, 0);
- textsw_insert(text, buf, len);
- }
-
- /************************************************************************/
- PRIVATE time_stamp()
-
- { int t, pos;
- char buf[5];
-
- t = time(0);
- if (t - old_time >= resolution) {
- window_set(text, TEXTSW_INSERTION_POINT, TEXTSW_INFINITY, 0);
- pos = (int) window_get(text, TEXTSW_LENGTH);
- if (pos != 0) {
- window_get(text, TEXTSW_CONTENTS, pos - 1, buf, 1);
- if (buf[0] != '\n')
- do_insertion("\n", 1);
- }
- do_insertion("\n<<< ", 5);
- do_insertion(ctime(&t), 24);
- do_insertion(" >>>\n", 5);
- old_time = t;
- }
- }
-
- /************************************************************************/
- PRIVATE Notify_value input_func(me, fd)
-
- int *me;
- int fd;
-
- { char old_c, *s, *t;
- f_ptr f;
- int count, do_blink = FALSE, do_open = FALSE;
- static char in_buf[INPUT_BUFFER_SIZE + 2];
-
- while ((count = read(fileno(master), in_buf, INPUT_BUFFER_SIZE)) >= 0) {
- in_buf[count] = '\0';
- while (s = index(in_buf, '\015')) {
- strcpy(s, s + 1);
- count--;
- }
- for (t = in_buf; *t; *s = old_c, t = s) {
- if (s = index(t, '\n')) {
- old_c = *++s;
- *s = '\0';
- }
- else {
- s = t + strlen(t);
- old_c = '\0';
- }
- if (curr_filter == NULL) {
- load_filters();
- for (f = filters; f && f->start; f++)
- if (f->valid && match_exp(f->start_re, f->scircf, t)) {
- if (f->save) {
- do_blink = f->flash;
- beep_count = f->beep;
- do_open = f->open;
- if (f->stamp)
- time_stamp();
- write_log(t);
- do_insertion(t, strlen(t));
- }
- if (f->end) {
- curr_filter = f;
- }
- break;
- }
- if (f == NULL || f->start == NULL) {
- if (do_time_stamp)
- time_stamp();
- write_log(t);
- do_insertion(t, strlen(t));
- do_blink = blink_icon;
- do_open = pop_open;
- beep_count = beep_amount;
- }
- }
- else {
- if (curr_filter->save) {
- if (curr_filter->stamp)
- time_stamp();
- write_log(t);
- do_insertion(t, strlen(t));
- }
- if (match_exp(curr_filter->end_re, curr_filter->ecircf, t))
- curr_filter = NULL;
- }
- }
- }
- window_set(text, TEXTSW_UPDATE_SCROLLBAR, 0);
- if (do_open)
- window_set(bf, FRAME_CLOSED, FALSE, 0);
- if (do_blink)
- if (window_get(bf, FRAME_CLOSED) && !blinking) {
- window_set(bf, FRAME_ICON, bad, WIN_SHOW, TRUE, 0);
- blinking = TRUE;
- bad_is_up = TRUE;
- menu_set(stop_blink, MENU_INACTIVE, FALSE, 0);
- notify_set_itimer_func(bf, blink_proc, ITIMER_REAL, &timer, NULL);
- }
- if (beep_count > 0 || blinking)
- notify_set_itimer_func(bf, blink_proc, ITIMER_REAL, &timer, NULL);
- return(NOTIFY_DONE);
- }
-
- /************************************************************************/
- /* Routines which parse options, create windows, and main() */
- /************************************************************************/
-
- /************************************************************************/
- PRIVATE parse_options(argc, argv)
-
- int *argc;
- char **argv;
-
- { char *s, c;
-
- strcpy(good_icon, ICON_DIRECTORY);
- strcat(good_icon, GOOD_ICON);
- strcpy(bad_icon, ICON_DIRECTORY);
- strcat(bad_icon, BAD_ICON);
- strcpy(inv_icon, ICON_DIRECTORY);
- strcat(inv_icon, INVERSE_ICON);
-
- strcpy(filter_path, getenv("HOME"));
- strcat(filter_path, "/.contool");
-
- while ((c = getopt(argc, argv, "b:c:d:f:g:l:no:pr:s:?", &s)) != EOF)
- switch (c) {
- case 'b' : strcpy(bad_icon, s);
- break;
- case 'c' : strcpy(filter_path, s);
- explicit_filters = TRUE;
- break;
- case 'd' : if (verify(s, "0123456789"))
- delete_amt = atoi(s);
- else {
- fprintf(stderr, "%s: invalid delete amount: %s\n", program, s);
- exit(1);
- }
- break;
- case 'f' : strcpy(inv_icon, s);
- break;
- case 'g' : strcpy(good_icon, s);
- break;
- case 'l' : if (verify(s, "0123456789"))
- window_set(text, TEXTSW_MEMORY_MAXIMUM, (size_limit = atoi(s)) + TEXT_SIZE_FUZZ, 0);
- else {
- fprintf(stderr, "%s: invalid message limit: %s\n", program, s);
- exit(1);
- }
- break;
- case 'n' : blink_icon = FALSE;
- break;
- case 'o': if ((logfile = fopen(s, "a")) == NULL) {
- fprintf(stderr, "%s : can't open logfile: %s\n", program, s);
- exit(1);
- }
- break;
- case 'p' : pop_open = TRUE;
- break;
- case 'r' : if (verify(s, "0123456789"))
- resolution = atoi(s);
- else {
- fprintf(stderr, "%s: invalid timestamp resolution: %s\n", program, s);
- exit(1);
- }
- break;
- case 's' : if (verify(s, "0123456789"))
- beep_amount = atoi(s);
- else {
- fprintf(stderr, "%s: invalid beep count: %s\n", program, s);
- exit(1);
- }
- break;
- case '?' : fprintf(stderr, ct_usage);
- exit(0);
- break;
- default : fprintf(stderr, ct_usage);
- exit(1);
- }
- }
-
- /************************************************************************/
- struct pixrect *load_icon(path, message)
-
- char *path;
- char *message;
-
- { char new_path[512], *real_path;
-
- if (access(path, R_OK) == -1) {
- strcpy(new_path, ICON_DIRECTORY);
- strcat(new_path, path);
- if (access(new_path, R_OK) == -1) {
- sprintf(message, "cannot read icon file %s", path);
- return(NULL);
- }
- real_path = new_path;
- }
- else
- real_path = path;
- return(icon_load_mpr(real_path, message));
- }
-
- /************************************************************************/
- PRIVATE load_icons()
-
- { char msg[IL_ERRORMSG_SIZE];
-
- if ((good_pr = load_icon(good_icon, msg)) == NULL) {
- fprintf(stderr, "%s: %s\n", program, msg);
- exit(1);
- }
- good = icon_create(ICON_IMAGE, good_pr,
- ICON_LABEL, "",
- ICON_WIDTH, good_pr->pr_size.x,
- ICON_HEIGHT, good_pr->pr_size.y,
- 0);
- icon_width = good_pr->pr_size.x;
- icon_height = good_pr->pr_size.y;
- if ((bad_pr = load_icon(bad_icon, msg)) == NULL) {
- fprintf(stderr, "%s: %s\n", program, msg);
- exit(1);
- }
- bad = icon_create(ICON_IMAGE, bad_pr,
- ICON_LABEL, "",
- ICON_WIDTH, bad_pr->pr_size.x,
- ICON_HEIGHT, bad_pr->pr_size.y,
- 0);
- if (bad_pr->pr_size.x > icon_width)
- icon_width = bad_pr->pr_size.x;
- if (bad_pr->pr_size.y > icon_height)
- icon_height = bad_pr->pr_size.y;
- if (*inv_icon == '\0')
- strcpy(inv_icon, bad_icon);
- if ((inv_pr = load_icon(inv_icon, msg)) == NULL) {
- fprintf(stderr, "%s: %s\n", program, msg);
- exit(1);
- }
- inverse = icon_create(ICON_IMAGE, inv_pr,
- ICON_LABEL, "",
- ICON_WIDTH, inv_pr->pr_size.x,
- ICON_HEIGHT, inv_pr->pr_size.y,
- 0);
- if (inv_pr->pr_size.x > icon_width)
- icon_width = inv_pr->pr_size.x;
- if (inv_pr->pr_size.y > icon_height)
- icon_height = inv_pr->pr_size.y;
- window_set(bf, FRAME_ICON, good, 0);
- }
-
- /************************************************************************/
- main(argc, argv)
-
- int argc;
- char **argv;
-
- { char *path;
- int i;
- Menu menu;
-
- program = strsave(argv[0]);
-
- bf = window_create(NULL, FRAME,
- FRAME_ARGC_PTR_ARGV, &argc, argv,
- FRAME_LABEL, TOOL_LABEL,
- 0);
- text = window_create(bf, TEXTSW,
- TEXTSW_DISABLE_CD, TRUE,
- TEXTSW_DISABLE_LOAD, TRUE,
- TEXTSW_AGAIN_RECORDING, FALSE,
- TEXTSW_IGNORE_LIMIT, TEXTSW_INFINITY,
- TEXTSW_HISTORY_LIMIT, 0,
- TEXTSW_MEMORY_MAXIMUM, size_limit + TEXT_SIZE_FUZZ,
- 0);
- open_rect = *((Rect *) window_get(bf, FRAME_OPEN_RECT));
-
- argv = saveargs(argc, argv);
- parse_options(&argc, argv);
- if (argc != 1) {
- fprintf(stderr, ct_usage);
- exit(1);
- }
-
- load_icons();
-
- path = open_psuedo_tty(&master, "r", &slave, "w");
- if (master == NULL) {
- fprintf(stderr, "%s: couldn't open any psuedo-tty\n");
- exit(1);
- }
- if (slave == NULL) {
- fprintf(stderr, "%s: couldn't open slave side of %s\n", program, path);
- exit(1);
- }
-
- i = fcntl(fileno(master), F_GETFL, 0);
- i |= FNDELAY;
- if (fcntl(fileno(master), F_SETFL, i) == -1) {
- fprintf(stderr, "%s: could not force %s to non-blocking i/o\n", program, path);
- exit(1);
- }
-
- acquire_console(path);
-
- stop_blink = menu_create_item(MENU_STRING, "Stop Blinking",
- MENU_INACTIVE, TRUE,
- MENU_ACTION_PROC, stop_blinking,
- 0);
- menu = menu_create(MENU_APPEND_ITEM, stop_blink,
- MENU_ACTION_ITEM, "Become Console", acquire_console,
- MENU_ACTION_ITEM, "Clear Messages", clear_messages,
- MENU_ACTION_ITEM, "Edit Filters", edit_filters,
- MENU_PULLRIGHT_ITEM, "Frame", window_get(bf, WIN_MENU),
- 0);
- window_set(bf, WIN_MENU, menu, 0);
-
- notify_set_input_func(bf, input_func, fileno(master));
- notify_interpose_destroy_func(bf, destroy_proc);
- notify_interpose_event_func(bf, close_proc, NOTIFY_SAFE);
-
- load_filters();
-
- window_main_loop(bf);
- }
-