[BACK]Return to cu.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cu

Annotation of src/usr.bin/cu/cu.c, Revision 1.13

1.13    ! nicm        1: /* $OpenBSD: cu.c,v 1.12 2013/01/17 11:15:22 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:
1.11      halex      22: #include <ctype.h>
1.1       nicm       23: #include <err.h>
                     24: #include <event.h>
                     25: #include <fcntl.h>
                     26: #include <getopt.h>
                     27: #include <paths.h>
                     28: #include <pwd.h>
                     29: #include <signal.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <termios.h>
                     34: #include <unistd.h>
                     35:
                     36: #include "cu.h"
                     37:
                     38: extern char            *__progname;
                     39:
1.6       nicm       40: FILE                   *record_file;
1.1       nicm       41: struct termios          saved_tio;
                     42: struct bufferevent     *input_ev;
                     43: struct bufferevent     *output_ev;
                     44: int                     line_fd;
1.12      nicm       45: struct termios          line_tio;
1.1       nicm       46: struct bufferevent     *line_ev;
                     47: struct event            sigterm_ev;
1.8       nicm       48: struct event            sighup_ev;
1.1       nicm       49: enum {
                     50:        STATE_NONE,
                     51:        STATE_NEWLINE,
                     52:        STATE_TILDE
                     53: } last_state = STATE_NEWLINE;
                     54:
                     55: __dead void    usage(void);
                     56: void           signal_event(int, short, void *);
                     57: void           stream_read(struct bufferevent *, void *);
                     58: void           stream_error(struct bufferevent *, short, void *);
                     59: void           line_read(struct bufferevent *, void *);
                     60: void           line_error(struct bufferevent *, short, void *);
                     61:
                     62: __dead void
                     63: usage(void)
                     64: {
1.7       nicm       65:        fprintf(stderr, "usage: %s [-l line] [-s speed | -speed]\n",
                     66:            __progname);
1.1       nicm       67:        exit(1);
                     68: }
                     69:
                     70: int
                     71: main(int argc, char **argv)
                     72: {
                     73:        const char      *line, *errstr;
                     74:        char            *tmp;
1.11      halex      75:        int              opt, speed, i;
1.1       nicm       76:
                     77:        line = "/dev/cua00";
                     78:        speed = 9600;
                     79:
                     80:        /*
                     81:         * Convert obsolescent -### speed to modern -s### syntax which getopt()
                     82:         * can handle.
                     83:         */
                     84:        for (i = 1; i < argc; i++) {
1.10      dlg        85:                if (strcmp("--", argv[i]) == 0)
                     86:                        break;
                     87:                if (argv[i][0] != '-' || !isdigit(argv[i][1]))
                     88:                        continue;
                     89:
                     90:                if (asprintf(&argv[i], "-s%s", &argv[i][1]) == -1)
                     91:                        errx(1, "speed asprintf");
1.1       nicm       92:        }
                     93:
                     94:        while ((opt = getopt(argc, argv, "l:s:")) != -1) {
                     95:                switch (opt) {
                     96:                case 'l':
                     97:                        line = optarg;
                     98:                        break;
                     99:                case 's':
                    100:                        speed = strtonum(optarg, 0, UINT_MAX, &errstr);
                    101:                        if (errstr != NULL)
                    102:                                errx(1, "speed is %s: %s", errstr, optarg);
                    103:                        break;
                    104:                default:
                    105:                        usage();
                    106:                }
                    107:        }
                    108:        argc -= optind;
                    109:        argv += optind;
                    110:        if (argc != 0)
                    111:                usage();
                    112:
                    113:        if (strchr(line, '/') == NULL) {
                    114:                if (asprintf(&tmp, "%s%s", _PATH_DEV, line) == -1)
                    115:                        err(1, "asprintf");
                    116:                line = tmp;
                    117:        }
                    118:
                    119:        line_fd = open(line, O_RDWR);
                    120:        if (line_fd < 0)
                    121:                err(1, "open(\"%s\")", line);
                    122:        if (ioctl(line_fd, TIOCEXCL) != 0)
                    123:                err(1, "ioctl(TIOCEXCL)");
1.12      nicm      124:        if (tcgetattr(line_fd, &line_tio) != 0)
                    125:                err(1, "tcgetattr");
1.2       nicm      126:        if (set_line(speed) != 0)
1.4       nicm      127:                err(1, "tcsetattr");
1.1       nicm      128:
                    129:        if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
                    130:                err(1, "tcgetattr");
                    131:
                    132:        event_init();
1.3       nicm      133:
1.1       nicm      134:        signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
                    135:        signal_add(&sigterm_ev, NULL);
1.8       nicm      136:        signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
                    137:        signal_add(&sighup_ev, NULL);
1.3       nicm      138:        if (signal(SIGINT, SIG_IGN) == SIG_ERR)
                    139:                err(1, "signal");
                    140:        if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
                    141:                err(1, "signal");
1.1       nicm      142:
1.4       nicm      143:        set_termios(); /* after this use cu_err and friends */
1.1       nicm      144:
                    145:        /* stdin and stdout get separate events */
                    146:        input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
                    147:            stream_error, NULL);
                    148:        bufferevent_enable(input_ev, EV_READ);
                    149:        output_ev = bufferevent_new(STDOUT_FILENO, NULL, NULL, stream_error,
                    150:            NULL);
                    151:        bufferevent_enable(output_ev, EV_WRITE);
                    152:
                    153:        line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
                    154:            NULL);
                    155:        bufferevent_enable(line_ev, EV_READ|EV_WRITE);
                    156:
1.13    ! nicm      157:        printf("Connected (speed %d)\r\n", speed);
1.1       nicm      158:        event_dispatch();
                    159:
1.5       nicm      160:        restore_termios();
1.1       nicm      161:        printf("\r\n[EOT]\n");
                    162:
                    163:        exit(0);
                    164: }
                    165:
                    166: void
                    167: signal_event(int fd, short events, void *data)
                    168: {
1.5       nicm      169:        restore_termios();
1.1       nicm      170:        printf("\r\n[SIG%s]\n", sys_signame[fd]);
                    171:
                    172:        exit(0);
                    173: }
                    174:
                    175: void
                    176: set_termios(void)
                    177: {
                    178:        struct termios tio;
                    179:
                    180:        if (!isatty(STDIN_FILENO))
                    181:                return;
                    182:
                    183:        memcpy(&tio, &saved_tio, sizeof(tio));
                    184:        tio.c_lflag &= ~(ICANON|IEXTEN|ECHO);
                    185:        tio.c_iflag &= ~(INPCK|ICRNL);
                    186:        tio.c_oflag &= ~OPOST;
                    187:        tio.c_cc[VMIN] = 1;
                    188:        tio.c_cc[VTIME] = 0;
                    189:        tio.c_cc[VDISCARD] = _POSIX_VDISABLE;
                    190:        tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
                    191:        tio.c_cc[VINTR] = _POSIX_VDISABLE;
                    192:        tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
                    193:        tio.c_cc[VQUIT] = _POSIX_VDISABLE;
                    194:        tio.c_cc[VSUSP] = _POSIX_VDISABLE;
                    195:        if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
1.4       nicm      196:                cu_err(1, "tcsetattr");
1.1       nicm      197: }
                    198:
                    199: void
                    200: restore_termios(void)
                    201: {
1.4       nicm      202:        if (isatty(STDIN_FILENO))
                    203:                tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
1.2       nicm      204: }
                    205:
                    206: int
                    207: set_line(int speed)
                    208: {
                    209:        struct termios   tio;
                    210:
1.12      nicm      211:        memcpy(&tio, &line_tio, sizeof(tio));
                    212:        tio.c_iflag &= ~(ISTRIP|ICRNL);
                    213:        tio.c_oflag &= ~OPOST;
                    214:        tio.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
                    215:        tio.c_cflag &= ~(CSIZE|PARENB);
                    216:        tio.c_cflag |= CREAD|CS8|CLOCAL;
1.2       nicm      217:        tio.c_cc[VMIN] = 1;
                    218:        tio.c_cc[VTIME] = 0;
                    219:        cfsetspeed(&tio, speed);
1.4       nicm      220:        if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
1.2       nicm      221:                return (-1);
                    222:        return (0);
1.1       nicm      223: }
                    224:
                    225: void
                    226: stream_read(struct bufferevent *bufev, void *data)
                    227: {
                    228:        char    *new_data, *ptr;
                    229:        size_t   new_size;
                    230:        int      state_change;
                    231:
                    232:        new_data = EVBUFFER_DATA(input_ev->input);
                    233:        new_size = EVBUFFER_LENGTH(input_ev->input);
                    234:        if (new_size == 0)
                    235:                return;
                    236:
                    237:        state_change = isatty(STDIN_FILENO);
                    238:        for (ptr = new_data; ptr < new_data + new_size; ptr++) {
                    239:                switch (last_state) {
                    240:                case STATE_NONE:
                    241:                        if (state_change && *ptr == '\r')
                    242:                                last_state = STATE_NEWLINE;
                    243:                        break;
                    244:                case STATE_NEWLINE:
                    245:                        if (state_change && *ptr == '~') {
                    246:                                last_state = STATE_TILDE;
                    247:                                continue;
                    248:                        }
                    249:                        if (*ptr != '\r')
                    250:                                last_state = STATE_NONE;
                    251:                        break;
                    252:                case STATE_TILDE:
                    253:                        do_command(*ptr);
                    254:                        last_state = STATE_NEWLINE;
                    255:                        continue;
                    256:                }
                    257:
                    258:                bufferevent_write(line_ev, ptr, 1);
                    259:        }
                    260:
                    261:        evbuffer_drain(input_ev->input, new_size);
                    262: }
                    263:
                    264: void
                    265: stream_error(struct bufferevent *bufev, short what, void *data)
                    266: {
                    267:        event_loopexit(NULL);
                    268: }
                    269:
                    270: void
                    271: line_read(struct bufferevent *bufev, void *data)
                    272: {
                    273:        char    *new_data;
                    274:        size_t   new_size;
                    275:
                    276:        new_data = EVBUFFER_DATA(line_ev->input);
                    277:        new_size = EVBUFFER_LENGTH(line_ev->input);
                    278:        if (new_size == 0)
                    279:                return;
                    280:
1.6       nicm      281:        if (record_file != NULL)
                    282:                fwrite(new_data, 1, new_size, record_file);
1.1       nicm      283:        bufferevent_write(output_ev, new_data, new_size);
                    284:
                    285:        evbuffer_drain(line_ev->input, new_size);
                    286: }
                    287:
                    288: void
                    289: line_error(struct bufferevent *bufev, short what, void *data)
                    290: {
                    291:        event_loopexit(NULL);
                    292: }
                    293:
                    294: /* Expands tildes in the file name. Based on code from ssh/misc.c. */
                    295: char *
                    296: tilde_expand(const char *filename1)
                    297: {
                    298:        const char      *filename, *path;
                    299:        char             user[128], ret[MAXPATHLEN], *out;
                    300:        struct passwd   *pw;
                    301:        u_int            len, slash;
                    302:
                    303:        if (*filename1 != '~')
                    304:                goto no_change;
                    305:        filename = filename1 + 1;
                    306:
                    307:        path = strchr(filename, '/');
                    308:        if (path != NULL && path > filename) {          /* ~user/path */
                    309:                slash = path - filename;
                    310:                if (slash > sizeof(user) - 1)
                    311:                        goto no_change;
                    312:                memcpy(user, filename, slash);
                    313:                user[slash] = '\0';
                    314:                if ((pw = getpwnam(user)) == NULL)
                    315:                        goto no_change;
                    316:        } else if ((pw = getpwuid(getuid())) == NULL)   /* ~/path */
                    317:                goto no_change;
                    318:
                    319:        if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
                    320:                goto no_change;
                    321:
                    322:        /* Make sure directory has a trailing '/' */
                    323:        len = strlen(pw->pw_dir);
                    324:        if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
                    325:            strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
                    326:                goto no_change;
                    327:
                    328:        /* Skip leading '/' from specified path */
                    329:        if (path != NULL)
                    330:                filename = path + 1;
                    331:        if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
                    332:                goto no_change;
                    333:
                    334:        out = strdup(ret);
                    335:        if (out == NULL)
1.4       nicm      336:                cu_err(1, "strdup");
1.1       nicm      337:        return (out);
                    338:
                    339: no_change:
                    340:        out = strdup(filename1);
                    341:        if (out == NULL)
1.4       nicm      342:                cu_err(1, "strdup");
1.1       nicm      343:        return (out);
                    344: }