Annotation of src/usr.bin/cu/cu.c, Revision 1.9
1.9 ! nicm 1: /* $OpenBSD: cu.c,v 1.8 2012/07/12 13:50:02 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/param.h>
20: #include <sys/ioctl.h>
21:
22: #include <err.h>
23: #include <event.h>
24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <paths.h>
27: #include <pwd.h>
28: #include <signal.h>
29: #include <stdio.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <termios.h>
33: #include <unistd.h>
34:
35: #include "cu.h"
36:
37: extern char *__progname;
38:
1.6 nicm 39: FILE *record_file;
1.1 nicm 40: struct termios saved_tio;
41: struct bufferevent *input_ev;
42: struct bufferevent *output_ev;
43: int line_fd;
44: struct bufferevent *line_ev;
45: struct event sigterm_ev;
1.8 nicm 46: struct event sighup_ev;
1.1 nicm 47: enum {
48: STATE_NONE,
49: STATE_NEWLINE,
50: STATE_TILDE
51: } last_state = STATE_NEWLINE;
52:
53: __dead void usage(void);
54: void signal_event(int, short, void *);
55: void stream_read(struct bufferevent *, void *);
56: void stream_error(struct bufferevent *, short, void *);
57: void line_read(struct bufferevent *, void *);
58: void line_error(struct bufferevent *, short, void *);
59:
60: __dead void
61: usage(void)
62: {
1.7 nicm 63: fprintf(stderr, "usage: %s [-l line] [-s speed | -speed]\n",
64: __progname);
1.1 nicm 65: exit(1);
66: }
67:
68: int
69: main(int argc, char **argv)
70: {
71: const char *line, *errstr;
72: char *tmp;
73: int opt, speed, i, ch;
74: static char sbuf[12];
75:
76: line = "/dev/cua00";
77: speed = 9600;
78:
79: /*
80: * Convert obsolescent -### speed to modern -s### syntax which getopt()
81: * can handle.
82: */
83: for (i = 1; i < argc; i++) {
84: if (argv[i][0] == '-') {
85: switch (argv[i][1]) {
86: case '0': case '1': case '2': case '3': case '4':
87: case '5': case '6': case '7': case '8': case '9':
88: ch = snprintf(sbuf, sizeof(sbuf), "-s%s",
89: &argv[i][1]);
90: if (ch <= 0 || ch >= (int)sizeof(sbuf)) {
91: errx(1, "invalid speed: %s",
92: &argv[i][1]);
93: }
94: argv[i] = sbuf;
95: break;
96: case '-':
97: /* if we get "--" stop processing args */
98: if (argv[i][2] == '\0')
99: goto getopt;
100: break;
101: }
102: }
103: }
104:
105: getopt:
106: while ((opt = getopt(argc, argv, "l:s:")) != -1) {
107: switch (opt) {
108: case 'l':
109: line = optarg;
110: break;
111: case 's':
112: speed = strtonum(optarg, 0, UINT_MAX, &errstr);
113: if (errstr != NULL)
114: errx(1, "speed is %s: %s", errstr, optarg);
115: break;
116: default:
117: usage();
118: }
119: }
120: argc -= optind;
121: argv += optind;
122: if (argc != 0)
123: usage();
124:
125: if (strchr(line, '/') == NULL) {
126: if (asprintf(&tmp, "%s%s", _PATH_DEV, line) == -1)
127: err(1, "asprintf");
128: line = tmp;
129: }
130:
131: line_fd = open(line, O_RDWR);
132: if (line_fd < 0)
133: err(1, "open(\"%s\")", line);
134: if (ioctl(line_fd, TIOCEXCL) != 0)
135: err(1, "ioctl(TIOCEXCL)");
136:
1.2 nicm 137: if (set_line(speed) != 0)
1.4 nicm 138: err(1, "tcsetattr");
1.1 nicm 139:
140: if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
141: err(1, "tcgetattr");
142:
143: event_init();
1.3 nicm 144:
1.1 nicm 145: signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
146: signal_add(&sigterm_ev, NULL);
1.8 nicm 147: signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
148: signal_add(&sighup_ev, NULL);
1.3 nicm 149: if (signal(SIGINT, SIG_IGN) == SIG_ERR)
150: err(1, "signal");
151: if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
152: err(1, "signal");
1.1 nicm 153:
1.4 nicm 154: set_termios(); /* after this use cu_err and friends */
1.1 nicm 155:
156: /* stdin and stdout get separate events */
157: input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
158: stream_error, NULL);
159: bufferevent_enable(input_ev, EV_READ);
160: output_ev = bufferevent_new(STDOUT_FILENO, NULL, NULL, stream_error,
161: NULL);
162: bufferevent_enable(output_ev, EV_WRITE);
163:
164: line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
165: NULL);
166: bufferevent_enable(line_ev, EV_READ|EV_WRITE);
167:
168: printf("Connected (speed %u)\r\n", speed);
169: event_dispatch();
170:
1.5 nicm 171: restore_termios();
1.1 nicm 172: printf("\r\n[EOT]\n");
173:
174: exit(0);
175: }
176:
177: void
178: signal_event(int fd, short events, void *data)
179: {
1.5 nicm 180: restore_termios();
1.1 nicm 181: printf("\r\n[SIG%s]\n", sys_signame[fd]);
182:
183: exit(0);
184: }
185:
186: void
187: set_termios(void)
188: {
189: struct termios tio;
190:
191: if (!isatty(STDIN_FILENO))
192: return;
193:
194: memcpy(&tio, &saved_tio, sizeof(tio));
195: tio.c_lflag &= ~(ICANON|IEXTEN|ECHO);
196: tio.c_iflag &= ~(INPCK|ICRNL);
197: tio.c_oflag &= ~OPOST;
198: tio.c_cc[VMIN] = 1;
199: tio.c_cc[VTIME] = 0;
200: tio.c_cc[VDISCARD] = _POSIX_VDISABLE;
201: tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
202: tio.c_cc[VINTR] = _POSIX_VDISABLE;
203: tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
204: tio.c_cc[VQUIT] = _POSIX_VDISABLE;
205: tio.c_cc[VSUSP] = _POSIX_VDISABLE;
206: if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
1.4 nicm 207: cu_err(1, "tcsetattr");
1.1 nicm 208: }
209:
210: void
211: restore_termios(void)
212: {
1.4 nicm 213: if (isatty(STDIN_FILENO))
214: tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
1.2 nicm 215: }
216:
217: int
218: set_line(int speed)
219: {
220: struct termios tio;
221:
222: cfmakeraw(&tio);
223: tio.c_iflag = 0;
224: tio.c_oflag = 0;
225: tio.c_lflag = 0;
1.9 ! nicm 226: tio.c_cflag = CREAD|CS8|CLOCAL;
1.2 nicm 227: tio.c_cc[VMIN] = 1;
228: tio.c_cc[VTIME] = 0;
229: cfsetspeed(&tio, speed);
1.4 nicm 230: if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
1.2 nicm 231: return (-1);
232: return (0);
1.1 nicm 233: }
234:
235: void
236: stream_read(struct bufferevent *bufev, void *data)
237: {
238: char *new_data, *ptr;
239: size_t new_size;
240: int state_change;
241:
242: new_data = EVBUFFER_DATA(input_ev->input);
243: new_size = EVBUFFER_LENGTH(input_ev->input);
244: if (new_size == 0)
245: return;
246:
247: state_change = isatty(STDIN_FILENO);
248: for (ptr = new_data; ptr < new_data + new_size; ptr++) {
249: switch (last_state) {
250: case STATE_NONE:
251: if (state_change && *ptr == '\r')
252: last_state = STATE_NEWLINE;
253: break;
254: case STATE_NEWLINE:
255: if (state_change && *ptr == '~') {
256: last_state = STATE_TILDE;
257: continue;
258: }
259: if (*ptr != '\r')
260: last_state = STATE_NONE;
261: break;
262: case STATE_TILDE:
263: do_command(*ptr);
264: last_state = STATE_NEWLINE;
265: continue;
266: }
267:
268: bufferevent_write(line_ev, ptr, 1);
269: }
270:
271: evbuffer_drain(input_ev->input, new_size);
272: }
273:
274: void
275: stream_error(struct bufferevent *bufev, short what, void *data)
276: {
277: event_loopexit(NULL);
278: }
279:
280: void
281: line_read(struct bufferevent *bufev, void *data)
282: {
283: char *new_data;
284: size_t new_size;
285:
286: new_data = EVBUFFER_DATA(line_ev->input);
287: new_size = EVBUFFER_LENGTH(line_ev->input);
288: if (new_size == 0)
289: return;
290:
1.6 nicm 291: if (record_file != NULL)
292: fwrite(new_data, 1, new_size, record_file);
1.1 nicm 293: bufferevent_write(output_ev, new_data, new_size);
294:
295: evbuffer_drain(line_ev->input, new_size);
296: }
297:
298: void
299: line_error(struct bufferevent *bufev, short what, void *data)
300: {
301: event_loopexit(NULL);
302: }
303:
304: /* Expands tildes in the file name. Based on code from ssh/misc.c. */
305: char *
306: tilde_expand(const char *filename1)
307: {
308: const char *filename, *path;
309: char user[128], ret[MAXPATHLEN], *out;
310: struct passwd *pw;
311: u_int len, slash;
312:
313: if (*filename1 != '~')
314: goto no_change;
315: filename = filename1 + 1;
316:
317: path = strchr(filename, '/');
318: if (path != NULL && path > filename) { /* ~user/path */
319: slash = path - filename;
320: if (slash > sizeof(user) - 1)
321: goto no_change;
322: memcpy(user, filename, slash);
323: user[slash] = '\0';
324: if ((pw = getpwnam(user)) == NULL)
325: goto no_change;
326: } else if ((pw = getpwuid(getuid())) == NULL) /* ~/path */
327: goto no_change;
328:
329: if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
330: goto no_change;
331:
332: /* Make sure directory has a trailing '/' */
333: len = strlen(pw->pw_dir);
334: if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
335: strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
336: goto no_change;
337:
338: /* Skip leading '/' from specified path */
339: if (path != NULL)
340: filename = path + 1;
341: if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
342: goto no_change;
343:
344: out = strdup(ret);
345: if (out == NULL)
1.4 nicm 346: cu_err(1, "strdup");
1.1 nicm 347: return (out);
348:
349: no_change:
350: out = strdup(filename1);
351: if (out == NULL)
1.4 nicm 352: cu_err(1, "strdup");
1.1 nicm 353: return (out);
354: }