[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.7

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