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 #include <sys/param.h>
18 #include <sys/fcntl.h>
19 #include <sys/filio.h>
20 #include <sys/snoop.h>
22 #include <sys/linker.h>
23 #include <sys/module.h>
39 #define MSG_INIT "Snoop started."
40 #define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
41 #define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
42 #define MSG_CHANGE "Snoop device change by user request."
43 #define MSG_NOWRITE "Snoop device change due to write failure."
45 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
48 #define CHR_SWITCH 24 /* Ctrl+X */
49 #define CHR_CLEAR 23 /* Ctrl+V */
51 static void clear(void);
52 static void timestamp(const char *);
53 static void set_tty(void);
54 static void unset_tty(void);
55 static void fatal(int, const char *);
56 static int open_snp(void);
57 static void cleanup(int);
58 static void usage(void) __dead2;
59 static void setup_scr(void);
60 static void attach_snp(void);
61 static void detach_snp(void);
62 static void set_dev(const char *);
63 static void ask_dev(char *, const char *);
65 int opt_reconn_close = 0;
66 int opt_reconn_oflow = 0;
67 int opt_interactive = 1;
68 int opt_timestamp = 0;
70 int opt_no_switch = 0;
71 const char *opt_snpdev;
73 char dev_name[DEV_NAME_LEN];
75 int std_in = 0, std_out = 1;
79 char tbuf[1024], gbuf[1024];
86 tputs(gbuf, 1, putchar);
91 timestamp(const char *buf)
97 printf("\n---------------------------------------------\n");
99 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
100 printf("%s\n", btmp);
102 printf("---------------------------------------------\n");
112 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
113 ntty.c_lflag &= ~ECHO;
115 ntty.c_lflag &= ~FLUSHO;
118 ntty.c_lflag &= ~PENDIN;
121 ntty.c_lflag &= ~IEXTEN;
123 ntty.c_cc[VMIN] = 1; /* minimum of one character */
124 ntty.c_cc[VTIME] = 0; /* timeout value */
126 ntty.c_cc[VINTR] = 07; /* ^G */
127 ntty.c_cc[VQUIT] = 07; /* ^G */
128 tcsetattr(std_in, TCSANOW, &ntty);
135 tcsetattr(std_in, TCSANOW, &otty);
139 fatal(int error, const char *buf)
144 errx(error, "fatal: %s", buf);
159 if (opt_snpdev == NULL)
160 f = open(_PATH_DEV "snp", mode);
162 f = open(opt_snpdev, mode);
164 fatal(EX_OSFILE, "cannot open snoop device");
170 cleanup(int signo __unused)
174 timestamp("Logging Exited.");
184 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
191 char *cbuf = gbuf, *term;
193 if (!opt_interactive)
195 if ((term = getenv("TERM")))
196 if (tgetent(tbuf, term) == 1)
197 if (tgetstr("cl", &cbuf))
209 ioctl(snp_io, SNPSTTY, &fd);
217 snp_tty = open(dev_name, O_RDONLY | O_NONBLOCK);
219 fatal(EX_DATAERR, "can't open device");
220 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
221 fatal(EX_UNAVAILABLE, "cannot attach to tty");
224 timestamp("Logging Started.");
228 set_dev(const char *name)
230 char buf[DEV_NAME_LEN];
233 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
234 snprintf(buf, sizeof buf, "%s", name);
236 if (strlen(name) == 2)
237 sprintf(buf, "%s%s", _PATH_TTY, name);
239 sprintf(buf, "%s%s", _PATH_DEV, name);
242 if (*name == '\0' || stat(buf, &sb) < 0)
243 fatal(EX_DATAERR, "bad device name");
245 if ((sb.st_mode & S_IFMT) != S_IFCHR)
246 fatal(EX_DATAERR, "must be a character device");
248 strlcpy(dev_name, buf, sizeof(dev_name));
254 ask_dev(char *dbuf, const char *msg)
256 char buf[DEV_NAME_LEN];
265 printf("Enter device name [%s]:", dbuf);
267 printf("Enter device name:");
269 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
271 if (buf[len - 1] == '\n')
273 if (buf[0] != '\0' && buf[0] != ' ')
282 main(int ac, char *av[])
284 int ch, res, rv, nread;
285 size_t b_size = MIN_SIZE;
286 char *buf, chb[READB_LEN];
289 (void) setlocale(LC_TIME, "");
296 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
302 opt_reconn_close = 1;
308 opt_reconn_oflow = 1;
324 tcgetattr(std_in, &otty);
326 if (modfind("snp") == -1)
327 if (kldload("snp") == -1 || modfind("snp") == -1)
328 warn("snp module not available");
330 signal(SIGINT, cleanup);
335 if (*(av += optind) == NULL) {
336 if (opt_interactive && !opt_no_switch)
337 ask_dev(dev_name, MSG_INIT);
339 fatal(EX_DATAERR, "no device name given");
341 strlcpy(dev_name, *av, sizeof(dev_name));
345 if (!(buf = (char *) malloc(b_size)))
346 fatal(EX_UNAVAILABLE, "malloc failed");
352 FD_SET(std_in, &fd_s);
353 FD_SET(snp_io, &fd_s);
354 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
355 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
357 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
358 fatal(EX_OSERR, "ioctl(FIONREAD)");
359 if (nread > READB_LEN)
361 rv = read(std_in, chb, nread);
362 if (rv == -1 || rv != nread)
363 fatal(EX_IOERR, "read (stdin) failed");
370 if (!opt_no_switch) {
372 ask_dev(dev_name, MSG_CHANGE);
378 rv = write(snp_io, chb, nread);
379 if (rv == -1 || rv != nread) {
384 ask_dev(dev_name, MSG_NOWRITE);
391 if (!FD_ISSET(snp_io, &fd_s))
394 if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
395 fatal(EX_OSERR, "ioctl(FIONREAD)");
399 if (opt_reconn_oflow)
401 else if (opt_interactive && !opt_no_switch) {
402 ask_dev(dev_name, MSG_OFLOW);
409 if (opt_reconn_close)
411 else if (opt_interactive && !opt_no_switch) {
412 ask_dev(dev_name, MSG_CLOSED);
418 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
420 if (!(buf = (char *) malloc(b_size / 2)))
421 fatal(EX_UNAVAILABLE, "malloc failed");
424 if (nread > b_size) {
425 b_size = (nread % 2) ? (nread + 1) : (nread);
427 if (!(buf = (char *) malloc(b_size)))
428 fatal(EX_UNAVAILABLE, "malloc failed");
430 rv = read(snp_io, buf, nread);
431 if (rv == -1 || rv != nread)
432 fatal(EX_IOERR, "read failed");
433 rv = write(std_out, buf, nread);
434 if (rv == -1 || rv != nread)
435 fatal(EX_IOERR, "write failed");