2 * Copyright (c) 1995 Ugen J.S.Antsilevich
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
7 * Redistribution in binary form may occur without any restrictions.
8 * Obviously, it would be nice if you gave credit where credit is due
9 * but requiring it would be too onerous.
11 * This software is provided ``AS IS'' without any warranties of any kind.
17 static const char rcsid[] =
21 #include <sys/param.h>
22 #include <sys/fcntl.h>
23 #include <sys/filio.h>
24 #include <sys/snoop.h>
26 #include <sys/linker.h>
27 #include <sys/module.h>
42 #define MSG_INIT "Snoop started."
43 #define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
44 #define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
45 #define MSG_CHANGE "Snoop device change by user request."
46 #define MSG_NOWRITE "Snoop device change due to write failure."
49 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
52 #define CHR_SWITCH 24 /* Ctrl+X */
53 #define CHR_CLEAR 23 /* Ctrl+V */
55 static void clear(void);
56 static void timestamp(const char *);
57 static void set_tty(void);
58 static void unset_tty(void);
59 static void fatal(int, const char *);
60 static int open_snp(void);
61 static void cleanup(int);
62 static void usage(void) __dead2;
63 static void setup_scr(void);
64 static void attach_snp(void);
65 static void detach_snp(void);
66 static void set_dev(const char *);
67 static void ask_dev(char *, const char *);
69 int opt_reconn_close = 0;
70 int opt_reconn_oflow = 0;
71 int opt_interactive = 1;
72 int opt_timestamp = 0;
74 int opt_no_switch = 0;
75 const char *opt_snpdev;
77 char dev_name[DEV_NAME_LEN];
80 int std_in = 0, std_out = 1;
85 char tbuf[1024], gbuf[1024];
92 tputs(gbuf, 1, putchar);
97 timestamp(const char *buf)
102 printf("\n---------------------------------------------\n");
104 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
105 printf("%s\n", btmp);
107 printf("---------------------------------------------\n");
116 tcgetattr (std_in, &otty);
118 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
119 ntty.c_lflag &= ~ECHO;
121 ntty.c_lflag &= ~FLUSHO;
124 ntty.c_lflag &= ~PENDIN;
127 ntty.c_lflag &= ~IEXTEN;
129 ntty.c_cc[VMIN] = 1; /* minimum of one character */
130 ntty.c_cc[VTIME] = 0; /* timeout value */
132 ntty.c_cc[VINTR] = 07; /* ^G */
133 ntty.c_cc[VQUIT] = 07; /* ^G */
134 tcsetattr (std_in, TCSANOW, &ntty);
140 tcsetattr (std_in, TCSANOW, &otty);
145 fatal(int error, const char *buf)
149 errx(error, "fatal: %s", buf);
157 char snp[] = {_PATH_DEV "snpX"};
166 if (opt_snpdev == NULL)
167 for (c = '0'; c <= '9'; c++) {
169 if ((f = open(snp, mode)) < 0)
174 if ((f = open(opt_snpdev, mode)) != -1)
176 fatal(EX_OSFILE, "cannot open snoop device");
182 cleanup(int signo __unused)
185 timestamp("Logging Exited.");
195 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
202 char *cbuf = gbuf, *term;
203 if (!opt_interactive)
205 if ((term = getenv("TERM")))
206 if (tgetent(tbuf, term) == 1)
207 if (tgetstr("cl", &cbuf))
219 ioctl(snp_io, SNPSTTY, &dev);
225 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
226 fatal(EX_UNAVAILABLE, "cannot attach to tty");
228 timestamp("Logging Started.");
233 set_dev(const char *name)
235 char buf[DEV_NAME_LEN];
238 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
239 snprintf(buf, sizeof buf, "%s", name);
241 if (strlen(name) == 2)
242 sprintf(buf, "%s%s", _PATH_TTY, name);
244 sprintf(buf, "%s%s", _PATH_DEV, name);
247 if (*name == '\0' || stat(buf, &sb) < 0)
248 fatal(EX_DATAERR, "bad device name");
250 if ((sb.st_mode & S_IFMT) != S_IFCHR)
251 fatal(EX_DATAERR, "must be a character device");
253 snp_tty = sb.st_rdev;
258 ask_dev(char *dbuf, const char *msg)
260 char buf[DEV_NAME_LEN];
269 printf("Enter device name [%s]:", dbuf);
271 printf("Enter device name:");
273 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
275 if (buf[len - 1] == '\n')
277 if (buf[0] != '\0' && buf[0] != ' ')
286 main(int ac, char *av[])
289 size_t nread, b_size = MIN_SIZE;
290 char ch, *buf, chb[READB_LEN];
293 (void) setlocale(LC_TIME, "");
301 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
307 opt_reconn_close = 1;
313 opt_reconn_oflow = 1;
329 if (modfind("snp") == -1)
330 if (kldload("snp") == -1 || modfind("snp") == -1)
331 warn("snp module not available");
333 signal(SIGINT, cleanup);
338 if (*(av += optind) == NULL) {
339 if (opt_interactive && !opt_no_switch)
340 ask_dev(dev_name, MSG_INIT);
342 fatal(EX_DATAERR, "no device name given");
344 strncpy(dev_name, *av, DEV_NAME_LEN);
348 if (!(buf = (char *) malloc(b_size)))
349 fatal(EX_UNAVAILABLE, "malloc failed");
355 FD_SET(std_in, &fd_s);
356 FD_SET(snp_io, &fd_s);
357 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
358 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
360 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
361 fatal(EX_OSERR, "ioctl(FIONREAD)");
362 if (nread > READB_LEN)
364 rv = read(std_in, chb, nread);
365 if (rv == -1 || (unsigned)rv != nread)
366 fatal(EX_IOERR, "read (stdin) failed");
373 if (!opt_no_switch) {
375 ask_dev(dev_name, MSG_CHANGE);
381 rv = write(snp_io, chb, nread);
382 if (rv == -1 || (unsigned)rv != nread) {
387 ask_dev(dev_name, MSG_NOWRITE);
394 if (!FD_ISSET(snp_io, &fd_s))
397 if ((res = ioctl(snp_io, FIONREAD, &idata)) != 0)
398 fatal(EX_OSERR, "ioctl(FIONREAD)");
402 if (opt_reconn_oflow)
404 else if (opt_interactive && !opt_no_switch) {
405 ask_dev(dev_name, MSG_OFLOW);
411 if (opt_reconn_close)
413 else if (opt_interactive && !opt_no_switch) {
414 ask_dev(dev_name, MSG_CLOSED);
419 nread = (unsigned)idata;
420 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
422 if (!(buf = (char *) malloc(b_size / 2)))
423 fatal(EX_UNAVAILABLE, "malloc failed");
426 if (nread > b_size) {
427 b_size = (nread % 2) ? (nread + 1) : (nread);
429 if (!(buf = (char *) malloc(b_size)))
430 fatal(EX_UNAVAILABLE, "malloc failed");
432 rv = read(snp_io, buf, nread);
433 if (rv == -1 || (unsigned)rv != nread)
434 fatal(EX_IOERR, "read failed");
435 rv = write(std_out, buf, nread);
436 if (rv == -1 || (unsigned)rv != nread)
437 fatal(EX_IOERR, "write failed");