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."
47 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
50 #define CHR_SWITCH 24 /* Ctrl+X */
51 #define CHR_CLEAR 23 /* Ctrl+V */
53 static void clear(void);
54 static void timestamp(const char *);
55 static void set_tty(void);
56 static void unset_tty(void);
57 static void fatal(int, const char *);
58 static int open_snp(void);
59 static void cleanup(int);
60 static void usage(void) __dead2;
61 static void setup_scr(void);
62 static void attach_snp(void);
63 static void detach_snp(void);
64 static void set_dev(const char *);
65 static void ask_dev(char *, const char *);
67 int opt_reconn_close = 0;
68 int opt_reconn_oflow = 0;
69 int opt_interactive = 1;
70 int opt_timestamp = 0;
72 int opt_no_switch = 0;
73 const char *opt_snpdev;
75 char dev_name[DEV_NAME_LEN];
77 int std_in = 0, std_out = 1;
81 char tbuf[1024], gbuf[1024];
88 tputs(gbuf, 1, putchar);
93 timestamp(const char *buf)
99 printf("\n---------------------------------------------\n");
101 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
102 printf("%s\n", btmp);
104 printf("---------------------------------------------\n");
113 tcgetattr(std_in, &otty);
115 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
116 ntty.c_lflag &= ~ECHO;
118 ntty.c_lflag &= ~FLUSHO;
121 ntty.c_lflag &= ~PENDIN;
124 ntty.c_lflag &= ~IEXTEN;
126 ntty.c_cc[VMIN] = 1; /* minimum of one character */
127 ntty.c_cc[VTIME] = 0; /* timeout value */
129 ntty.c_cc[VINTR] = 07; /* ^G */
130 ntty.c_cc[VQUIT] = 07; /* ^G */
131 tcsetattr(std_in, TCSANOW, &ntty);
138 tcsetattr(std_in, TCSANOW, &otty);
142 fatal(int error, const char *buf)
147 errx(error, "fatal: %s", buf);
162 if (opt_snpdev == NULL)
163 f = open(_PATH_DEV "snp", mode);
165 f = open(opt_snpdev, mode);
167 fatal(EX_OSFILE, "cannot open snoop device");
173 cleanup(int signo __unused)
177 timestamp("Logging Exited.");
187 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
194 char *cbuf = gbuf, *term;
196 if (!opt_interactive)
198 if ((term = getenv("TERM")))
199 if (tgetent(tbuf, term) == 1)
200 if (tgetstr("cl", &cbuf))
212 ioctl(snp_io, SNPSTTY, &fd);
220 snp_tty = open(dev_name, O_RDONLY | O_NONBLOCK);
222 fatal(EX_DATAERR, "can't open device");
223 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
224 fatal(EX_UNAVAILABLE, "cannot attach to tty");
227 timestamp("Logging Started.");
231 set_dev(const char *name)
233 char buf[DEV_NAME_LEN];
236 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
237 snprintf(buf, sizeof buf, "%s", name);
239 if (strlen(name) == 2)
240 sprintf(buf, "%s%s", _PATH_TTY, name);
242 sprintf(buf, "%s%s", _PATH_DEV, name);
245 if (*name == '\0' || stat(buf, &sb) < 0)
246 fatal(EX_DATAERR, "bad device name");
248 if ((sb.st_mode & S_IFMT) != S_IFCHR)
249 fatal(EX_DATAERR, "must be a character device");
251 strncpy(dev_name, buf, DEV_NAME_LEN);
257 ask_dev(char *dbuf, const char *msg)
259 char buf[DEV_NAME_LEN];
268 printf("Enter device name [%s]:", dbuf);
270 printf("Enter device name:");
272 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
274 if (buf[len - 1] == '\n')
276 if (buf[0] != '\0' && buf[0] != ' ')
285 main(int ac, char *av[])
287 int ch, res, rv, nread;
288 size_t b_size = MIN_SIZE;
289 char *buf, chb[READB_LEN];
292 (void) setlocale(LC_TIME, "");
299 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
305 opt_reconn_close = 1;
311 opt_reconn_oflow = 1;
327 if (modfind("snp") == -1)
328 if (kldload("snp") == -1 || modfind("snp") == -1)
329 warn("snp module not available");
331 signal(SIGINT, cleanup);
336 if (*(av += optind) == NULL) {
337 if (opt_interactive && !opt_no_switch)
338 ask_dev(dev_name, MSG_INIT);
340 fatal(EX_DATAERR, "no device name given");
342 strncpy(dev_name, *av, DEV_NAME_LEN);
346 if (!(buf = (char *) malloc(b_size)))
347 fatal(EX_UNAVAILABLE, "malloc failed");
353 FD_SET(std_in, &fd_s);
354 FD_SET(snp_io, &fd_s);
355 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
356 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
358 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
359 fatal(EX_OSERR, "ioctl(FIONREAD)");
360 if (nread > READB_LEN)
362 rv = read(std_in, chb, nread);
363 if (rv == -1 || rv != nread)
364 fatal(EX_IOERR, "read (stdin) failed");
371 if (!opt_no_switch) {
373 ask_dev(dev_name, MSG_CHANGE);
379 rv = write(snp_io, chb, nread);
380 if (rv == -1 || rv != nread) {
385 ask_dev(dev_name, MSG_NOWRITE);
392 if (!FD_ISSET(snp_io, &fd_s))
395 if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
396 fatal(EX_OSERR, "ioctl(FIONREAD)");
400 if (opt_reconn_oflow)
402 else if (opt_interactive && !opt_no_switch) {
403 ask_dev(dev_name, MSG_OFLOW);
410 if (opt_reconn_close)
412 else if (opt_interactive && !opt_no_switch) {
413 ask_dev(dev_name, MSG_CLOSED);
419 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
421 if (!(buf = (char *) malloc(b_size / 2)))
422 fatal(EX_UNAVAILABLE, "malloc failed");
425 if (nread > b_size) {
426 b_size = (nread % 2) ? (nread + 1) : (nread);
428 if (!(buf = (char *) malloc(b_size)))
429 fatal(EX_UNAVAILABLE, "malloc failed");
431 rv = read(snp_io, buf, nread);
432 if (rv == -1 || rv != nread)
433 fatal(EX_IOERR, "read failed");
434 rv = write(std_out, buf, nread);
435 if (rv == -1 || rv != nread)
436 fatal(EX_IOERR, "write failed");