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.
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
19 #include <sys/param.h>
20 #include <sys/fcntl.h>
21 #include <sys/filio.h>
22 #include <sys/snoop.h>
24 #include <sys/linker.h>
25 #include <sys/module.h>
41 #define MSG_INIT "Snoop started."
42 #define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
43 #define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
44 #define MSG_CHANGE "Snoop device change by user request."
45 #define MSG_NOWRITE "Snoop device change due to write failure."
48 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
51 #define CHR_SWITCH 24 /* Ctrl+X */
52 #define CHR_CLEAR 23 /* Ctrl+V */
54 static void clear(void);
55 static void timestamp(const char *);
56 static void set_tty(void);
57 static void unset_tty(void);
58 static void fatal(int, const char *);
59 static int open_snp(void);
60 static void cleanup(int);
61 static void usage(void) __dead2;
62 static void setup_scr(void);
63 static void attach_snp(void);
64 static void detach_snp(void);
65 static void set_dev(const char *);
66 static void ask_dev(char *, const char *);
68 int opt_reconn_close = 0;
69 int opt_reconn_oflow = 0;
70 int opt_interactive = 1;
71 int opt_timestamp = 0;
73 int opt_no_switch = 0;
74 const char *opt_snpdev;
76 char dev_name[DEV_NAME_LEN];
78 int std_in = 0, std_out = 1;
83 char tbuf[1024], gbuf[1024];
90 tputs(gbuf, 1, putchar);
95 timestamp(const char *buf)
100 printf("\n---------------------------------------------\n");
102 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
103 printf("%s\n", btmp);
105 printf("---------------------------------------------\n");
114 tcgetattr (std_in, &otty);
116 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
117 ntty.c_lflag &= ~ECHO;
119 ntty.c_lflag &= ~FLUSHO;
122 ntty.c_lflag &= ~PENDIN;
125 ntty.c_lflag &= ~IEXTEN;
127 ntty.c_cc[VMIN] = 1; /* minimum of one character */
128 ntty.c_cc[VTIME] = 0; /* timeout value */
130 ntty.c_cc[VINTR] = 07; /* ^G */
131 ntty.c_cc[VQUIT] = 07; /* ^G */
132 tcsetattr (std_in, TCSANOW, &ntty);
138 tcsetattr (std_in, TCSANOW, &otty);
143 fatal(int error, const char *buf)
147 errx(error, "fatal: %s", buf);
155 char snp[] = {_PATH_DEV "snpXXX"};
158 pos = strlen(snp) - 3;
164 if (opt_snpdev == NULL)
165 for (c = 0; c <= 999; c++) {
166 snprintf(snp+pos, 4, "%d", c);
167 if ((f = open(snp, mode)) < 0) {
170 err(1, "open %s", snp);
175 if ((f = open(opt_snpdev, mode)) != -1)
177 fatal(EX_OSFILE, "cannot open snoop device");
183 cleanup(int signo __unused)
186 timestamp("Logging Exited.");
196 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
203 char *cbuf = gbuf, *term;
204 if (!opt_interactive)
206 if ((term = getenv("TERM")))
207 if (tgetent(tbuf, term) == 1)
208 if (tgetstr("cl", &cbuf))
220 ioctl(snp_io, SNPSTTY, &fd);
228 snp_tty = open(dev_name, O_RDONLY | O_NONBLOCK);
230 fatal(EX_DATAERR, "can't open device");
231 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
232 fatal(EX_UNAVAILABLE, "cannot attach to tty");
235 timestamp("Logging Started.");
240 set_dev(const char *name)
242 char buf[DEV_NAME_LEN];
245 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
246 snprintf(buf, sizeof buf, "%s", name);
248 if (strlen(name) == 2)
249 sprintf(buf, "%s%s", _PATH_TTY, name);
251 sprintf(buf, "%s%s", _PATH_DEV, name);
254 if (*name == '\0' || stat(buf, &sb) < 0)
255 fatal(EX_DATAERR, "bad device name");
257 if ((sb.st_mode & S_IFMT) != S_IFCHR)
258 fatal(EX_DATAERR, "must be a character device");
260 strncpy(dev_name, buf, DEV_NAME_LEN);
266 ask_dev(char *dbuf, const char *msg)
268 char buf[DEV_NAME_LEN];
277 printf("Enter device name [%s]:", dbuf);
279 printf("Enter device name:");
281 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
283 if (buf[len - 1] == '\n')
285 if (buf[0] != '\0' && buf[0] != ' ')
294 main(int ac, char *av[])
296 int ch, res, rv, nread;
297 size_t b_size = MIN_SIZE;
298 char *buf, chb[READB_LEN];
301 (void) setlocale(LC_TIME, "");
309 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
315 opt_reconn_close = 1;
321 opt_reconn_oflow = 1;
337 if (modfind("snp") == -1)
338 if (kldload("snp") == -1 || modfind("snp") == -1)
339 warn("snp module not available");
341 signal(SIGINT, cleanup);
346 if (*(av += optind) == NULL) {
347 if (opt_interactive && !opt_no_switch)
348 ask_dev(dev_name, MSG_INIT);
350 fatal(EX_DATAERR, "no device name given");
352 strncpy(dev_name, *av, DEV_NAME_LEN);
356 if (!(buf = (char *) malloc(b_size)))
357 fatal(EX_UNAVAILABLE, "malloc failed");
363 FD_SET(std_in, &fd_s);
364 FD_SET(snp_io, &fd_s);
365 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
366 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
368 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
369 fatal(EX_OSERR, "ioctl(FIONREAD)");
370 if (nread > READB_LEN)
372 rv = read(std_in, chb, nread);
373 if (rv == -1 || rv != nread)
374 fatal(EX_IOERR, "read (stdin) failed");
381 if (!opt_no_switch) {
383 ask_dev(dev_name, MSG_CHANGE);
389 rv = write(snp_io, chb, nread);
390 if (rv == -1 || rv != nread) {
395 ask_dev(dev_name, MSG_NOWRITE);
402 if (!FD_ISSET(snp_io, &fd_s))
405 if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
406 fatal(EX_OSERR, "ioctl(FIONREAD)");
410 if (opt_reconn_oflow)
412 else if (opt_interactive && !opt_no_switch) {
413 ask_dev(dev_name, MSG_OFLOW);
420 if (opt_reconn_close)
422 else if (opt_interactive && !opt_no_switch) {
423 ask_dev(dev_name, MSG_CLOSED);
429 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
431 if (!(buf = (char *) malloc(b_size / 2)))
432 fatal(EX_UNAVAILABLE, "malloc failed");
435 if (nread > b_size) {
436 b_size = (nread % 2) ? (nread + 1) : (nread);
438 if (!(buf = (char *) malloc(b_size)))
439 fatal(EX_UNAVAILABLE, "malloc failed");
441 rv = read(snp_io, buf, nread);
442 if (rv == -1 || rv != nread)
443 fatal(EX_IOERR, "read failed");
444 rv = write(std_out, buf, nread);
445 if (rv == -1 || rv != nread)
446 fatal(EX_IOERR, "write failed");