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

Annotation of src/usr.bin/tip/tip.c, Revision 1.51

1.51    ! nicm        1: /*     $OpenBSD: tip.c,v 1.50 2010/07/02 05:52:48 nicm Exp $   */
1.5       millert     2: /*     $NetBSD: tip.c,v 1.13 1997/04/20 00:03:05 mellon Exp $  */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.20      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * tip - UNIX link to other systems
                     35:  *  tip [-v] [-speed] system-name
                     36:  * or
1.40      nicm       37:  *  cu phone-number [-s speed] [-l line]
1.1       deraadt    38:  */
1.36      nicm       39:
                     40: #include <sys/types.h>
                     41: #include <sys/socket.h>
                     42:
1.49      nicm       43: #include <util.h>
                     44:
1.1       deraadt    45: #include "tip.h"
                     46:
1.26      moritz     47: static void    intprompt(int);
                     48: static void    tipin(void);
                     49: static int     escape(void);
                     50:
1.8       deraadt    51: int
1.25      deraadt    52: main(int argc, char *argv[])
1.1       deraadt    53: {
1.47      chl        54:        char *sys = NULL;
1.36      nicm       55:        int i, pair[2];
1.32      mcbride    56:
                     57:        /* XXX preserve previous braindamaged behavior */
1.48      nicm       58:        vsetnum(DC, 1);
1.1       deraadt    59:
1.39      nicm       60:        if (strcmp(__progname, "cu") == 0) {
1.1       deraadt    61:                cumode = 1;
                     62:                cumain(argc, argv);
                     63:                goto cucommon;
                     64:        }
                     65:
                     66:        if (argc > 4) {
1.34      sobrado    67:                fprintf(stderr, "usage: tip [-nv] [-speed] [system-name]\n");
1.1       deraadt    68:                exit(1);
                     69:        }
                     70:        if (!isatty(0)) {
1.13      millert    71:                fprintf(stderr, "%s: must be interactive\n", __progname);
1.1       deraadt    72:                exit(1);
                     73:        }
                     74:
                     75:        for (; argc > 1; argv++, argc--) {
                     76:                if (argv[1][0] != '-')
1.27      deraadt    77:                        sys = argv[1];
1.1       deraadt    78:                else switch (argv[1][1]) {
                     79:
                     80:                case 'v':
                     81:                        vflag++;
                     82:                        break;
                     83:
1.9       todd       84:                case 'n':
                     85:                        noesc++;
                     86:                        break;
                     87:
1.1       deraadt    88:                case '0': case '1': case '2': case '3': case '4':
                     89:                case '5': case '6': case '7': case '8': case '9':
1.48      nicm       90:                        vsetnum(BAUDRATE, atoi(&argv[1][1]));
1.1       deraadt    91:                        break;
                     92:
                     93:                default:
1.13      millert    94:                        fprintf(stderr, "%s: %s, unknown option\n", __progname,
                     95:                            argv[1]);
1.1       deraadt    96:                        break;
                     97:                }
                     98:        }
                     99:
                    100:        (void)signal(SIGINT, cleanup);
                    101:        (void)signal(SIGQUIT, cleanup);
                    102:        (void)signal(SIGHUP, cleanup);
                    103:        (void)signal(SIGTERM, cleanup);
1.24      otto      104:        (void)signal(SIGCHLD, SIG_DFL);
1.1       deraadt   105:
1.50      nicm      106:        FD = hunt(sys);
1.1       deraadt   107:        setbuf(stdout, NULL);
                    108:        loginit();
                    109:        vinit();                                /* init variables */
1.12      millert   110:        setparity("none");                      /* set the parity table */
1.1       deraadt   111:
1.48      nicm      112:        if (ttysetup(vgetnum(BAUDRATE))) {
1.49      nicm      113:                fprintf(stderr, "%s: bad baud rate %d\n", __progname,
1.48      nicm      114:                    vgetnum(BAUDRATE));
1.19      millert   115:                (void)uu_unlock(uucplock);
                    116:                exit(3);
                    117:        }
1.40      nicm      118:        con();
1.41      nicm      119:
1.1       deraadt   120: cucommon:
                    121:        /*
                    122:         * From here down the code is shared with
                    123:         * the "cu" version of tip.
                    124:         */
1.10      jason     125:
                    126:        i = fcntl(FD, F_GETFL);
                    127:        if (i == -1) {
                    128:                perror("fcntl");
1.23      deraadt   129:                cleanup(0);
1.10      jason     130:        }
                    131:        i = fcntl(FD, F_SETFL, i & ~O_NONBLOCK);
                    132:        if (i == -1) {
                    133:                perror("fcntl");
1.23      deraadt   134:                cleanup(0);
1.10      jason     135:        }
1.1       deraadt   136:
1.2       deraadt   137:        tcgetattr(0, &defterm);
1.22      deraadt   138:        gotdefterm = 1;
1.2       deraadt   139:        term = defterm;
                    140:        term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
                    141:        term.c_iflag &= ~(INPCK|ICRNL);
                    142:        term.c_oflag &= ~OPOST;
                    143:        term.c_cc[VMIN] = 1;
                    144:        term.c_cc[VTIME] = 0;
                    145:        defchars = term;
                    146:        term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
1.25      deraadt   147:            term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
                    148:            term.c_cc[VLNEXT] = _POSIX_VDISABLE;
1.1       deraadt   149:        raw();
                    150:
                    151:        (void)signal(SIGALRM, timeout);
1.29      deraadt   152:
1.48      nicm      153:        if (vgetnum(LINEDISC) != TTYDISC) {
                    154:                int ld = (int)vgetnum(LINEDISC);
1.29      deraadt   155:                ioctl(FD, TIOCSETD, &ld);
1.48      nicm      156:        }
1.1       deraadt   157:
1.36      nicm      158:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) {
                    159:                (void)uu_unlock(uucplock);
                    160:                err(3, "socketpair");
                    161:        }
                    162:
1.1       deraadt   163:        /*
                    164:         * Everything's set up now:
                    165:         *      connection established (hardwired or dialup)
                    166:         *      line conditioned (baud rate, mode, etc.)
                    167:         *      internal data structures (variables)
                    168:         * so, fork one process for local side and one for remote.
                    169:         */
                    170:        printf(cumode ? "Connected\r\n" : "\07connected\r\n");
1.23      deraadt   171:        tipin_pid = getpid();
1.36      nicm      172:        switch (tipout_pid = fork()) {
                    173:        case -1:
                    174:                (void)uu_unlock(uucplock);
                    175:                err(3, "fork");
                    176:        case 0:
                    177:                close(pair[1]);
                    178:                tipin_fd = pair[0];
                    179:                tipout();
                    180:        default:
                    181:                close(pair[0]);
                    182:                tipout_fd = pair[1];
1.1       deraadt   183:                tipin();
1.36      nicm      184:        }
1.1       deraadt   185:        /*NOTREACHED*/
1.8       deraadt   186:        exit(0);
1.40      nicm      187: }
                    188:
                    189: void
                    190: con(void)
                    191: {
1.48      nicm      192:        if (vgetstr(CONNECT) != NULL)
                    193:                parwrite(FD, vgetstr(CONNECT), size(vgetstr(CONNECT)));
                    194:        logent(vgetstr(HOST), vgetstr(DEVICE), "call completed");
1.1       deraadt   195: }
                    196:
                    197: void
1.23      deraadt   198: cleanup(int signo)
1.1       deraadt   199: {
                    200:        (void)uu_unlock(uucplock);
                    201:        if (odisc)
1.27      deraadt   202:                ioctl(0, TIOCSETD, &odisc);
1.22      deraadt   203:        unraw();
1.23      deraadt   204:        if (signo && tipout_pid) {
                    205:                kill(tipout_pid, signo);
                    206:                wait(NULL);
                    207:        }
1.1       deraadt   208:        exit(0);
                    209: }
                    210:
                    211: /*
                    212:  * put the controlling keyboard into raw mode
                    213:  */
1.8       deraadt   214: void
1.25      deraadt   215: raw(void)
1.1       deraadt   216: {
1.2       deraadt   217:        tcsetattr(0, TCSADRAIN, &term);
1.1       deraadt   218: }
                    219:
                    220:
                    221: /*
                    222:  * return keyboard to normal mode
                    223:  */
1.8       deraadt   224: void
1.25      deraadt   225: unraw(void)
1.1       deraadt   226: {
1.22      deraadt   227:        if (gotdefterm)
                    228:                tcsetattr(0, TCSADRAIN, &defterm);
1.1       deraadt   229: }
                    230:
                    231: static jmp_buf promptbuf;
                    232:
                    233: /*
                    234:  * Print string ``s'', then read a string
                    235:  *  in from the terminal.  Handles signals & allows use of
                    236:  *  normal erase and kill characters.
                    237:  */
1.8       deraadt   238: int
1.25      deraadt   239: prompt(char *s, char *p, size_t sz)
1.1       deraadt   240: {
1.15      millert   241:        int c;
                    242:        char *b = p;
1.1       deraadt   243:        sig_t oint, oquit;
                    244:
                    245:        stoprompt = 0;
                    246:        oint = signal(SIGINT, intprompt);
                    247:        oquit = signal(SIGQUIT, SIG_IGN);
                    248:        unraw();
                    249:        printf("%s", s);
                    250:        if (setjmp(promptbuf) == 0)
1.6       millert   251:                while ((c = getchar()) != EOF && (*p = c) != '\n' && --sz > 0)
1.1       deraadt   252:                        p++;
                    253:        *p = '\0';
                    254:
                    255:        raw();
                    256:        (void)signal(SIGINT, oint);
                    257:        (void)signal(SIGQUIT, oquit);
                    258:        return (stoprompt || p == b);
                    259: }
                    260:
                    261: /*
                    262:  * Interrupt service routine during prompting
                    263:  */
1.27      deraadt   264: /*ARGSUSED*/
1.26      moritz    265: static void
                    266: intprompt(int signo)
1.1       deraadt   267: {
                    268:        (void)signal(SIGINT, SIG_IGN);
                    269:        stoprompt = 1;
                    270:        printf("\r\n");
                    271:        longjmp(promptbuf, 1);
                    272: }
                    273:
                    274: /*
                    275:  * ****TIPIN   TIPIN****
                    276:  */
1.26      moritz    277: static void
1.25      deraadt   278: tipin(void)
1.1       deraadt   279: {
1.27      deraadt   280:        int bol = 1;
1.16      deraadt   281:        int gch;
1.17      deraadt   282:        char ch;
1.1       deraadt   283:
                    284:        /*
                    285:         * Kinda klugey here...
                    286:         *   check for scripting being turned on from the .tiprc file,
                    287:         *   but be careful about just using setscript(), as we may
                    288:         *   send a SIGEMT before tipout has a chance to set up catching
                    289:         *   it; so wait a second, then setscript()
                    290:         */
1.48      nicm      291:        if (vgetnum(SCRIPT)) {
1.1       deraadt   292:                sleep(1);
                    293:                setscript();
                    294:        }
                    295:
                    296:        while (1) {
1.2       deraadt   297:                gch = getchar()&STRIP_PAR;
1.16      deraadt   298:                /* XXX does not check for EOF */
1.48      nicm      299:                if (gch == vgetnum(ESCAPE) && bol) {
1.9       todd      300:                        if (!noesc) {
                    301:                                if (!(gch = escape()))
                    302:                                        continue;
                    303:                        }
1.48      nicm      304:                } else if (!cumode && gch == vgetnum(RAISECHAR)) {
                    305:                        vsetnum(RAISE, !vgetnum(RAISE));
1.1       deraadt   306:                        continue;
                    307:                } else if (gch == '\r') {
                    308:                        bol = 1;
1.17      deraadt   309:                        ch = gch;
                    310:                        parwrite(FD, &ch, 1);
1.48      nicm      311:                        if (vgetnum(HALFDUPLEX))
1.1       deraadt   312:                                printf("\r\n");
                    313:                        continue;
1.48      nicm      314:                } else if (!cumode && gch == vgetnum(FORCE))
                    315:                        gch = getchar() & STRIP_PAR;
                    316:                bol = any(gch, vgetstr(EOL));
                    317:                if (vgetnum(RAISE) && islower(gch))
1.1       deraadt   318:                        gch = toupper(gch);
1.17      deraadt   319:                ch = gch;
                    320:                parwrite(FD, &ch, 1);
1.48      nicm      321:                if (vgetnum(HALFDUPLEX))
1.17      deraadt   322:                        printf("%c", ch);
1.1       deraadt   323:        }
                    324: }
                    325:
                    326: extern esctable_t etable[];
                    327:
                    328: /*
                    329:  * Escape handler --
                    330:  *  called on recognition of ``escapec'' at the beginning of a line
                    331:  */
1.26      moritz    332: static int
1.25      deraadt   333: escape(void)
1.1       deraadt   334: {
1.16      deraadt   335:        int gch;
1.15      millert   336:        esctable_t *p;
1.48      nicm      337:        char c = vgetnum(ESCAPE);
1.1       deraadt   338:
1.2       deraadt   339:        gch = (getchar()&STRIP_PAR);
1.16      deraadt   340:        /* XXX does not check for EOF */
1.1       deraadt   341:        for (p = etable; p->e_char; p++)
                    342:                if (p->e_char == gch) {
                    343:                        printf("%s", ctrl(c));
                    344:                        (*p->e_func)(gch);
                    345:                        return (0);
                    346:                }
                    347:        /* ESCAPE ESCAPE forces ESCAPE */
                    348:        if (c != gch)
1.11      deraadt   349:                parwrite(FD, &c, 1);
1.1       deraadt   350:        return (gch);
                    351: }
                    352:
1.8       deraadt   353: int
1.25      deraadt   354: any(int cc, char *p)
1.1       deraadt   355: {
1.8       deraadt   356:        char c = cc;
1.1       deraadt   357:        while (p && *p)
                    358:                if (*p++ == c)
                    359:                        return (1);
                    360:        return (0);
                    361: }
                    362:
1.28      deraadt   363: size_t
1.25      deraadt   364: size(char *s)
1.1       deraadt   365: {
1.28      deraadt   366:        size_t i = 0;
1.1       deraadt   367:
                    368:        while (s && *s++)
                    369:                i++;
                    370:        return (i);
                    371: }
                    372:
                    373: char *
1.25      deraadt   374: interp(char *s)
1.1       deraadt   375: {
                    376:        static char buf[256];
1.15      millert   377:        char *p = buf, c, *q;
1.1       deraadt   378:
1.8       deraadt   379:        while ((c = *s++)) {
1.1       deraadt   380:                for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
                    381:                        if (*q++ == c) {
                    382:                                *p++ = '\\'; *p++ = *q;
                    383:                                goto next;
                    384:                        }
                    385:                if (c < 040) {
                    386:                        *p++ = '^'; *p++ = c + 'A'-1;
                    387:                } else if (c == 0177) {
                    388:                        *p++ = '^'; *p++ = '?';
                    389:                } else
                    390:                        *p++ = c;
                    391:        next:
                    392:                ;
                    393:        }
                    394:        *p = '\0';
                    395:        return (buf);
                    396: }
                    397:
                    398: char *
1.25      deraadt   399: ctrl(char c)
1.1       deraadt   400: {
                    401:        static char s[3];
                    402:
                    403:        if (c < 040 || c == 0177) {
                    404:                s[0] = '^';
                    405:                s[1] = c == 0177 ? '?' : c+'A'-1;
                    406:                s[2] = '\0';
                    407:        } else {
                    408:                s[0] = c;
                    409:                s[1] = '\0';
                    410:        }
                    411:        return (s);
                    412: }
                    413:
                    414: /*
                    415:  * Help command
                    416:  */
1.8       deraadt   417: void
1.26      moritz    418: help(int c)
1.1       deraadt   419: {
1.15      millert   420:        esctable_t *p;
1.1       deraadt   421:
                    422:        printf("%c\r\n", c);
                    423:        for (p = etable; p->e_char; p++) {
1.48      nicm      424:                printf("%2s", ctrl(vgetnum(ESCAPE)));
1.37      nicm      425:                printf("%-2s     %s\r\n", ctrl(p->e_char), p->e_help);
1.1       deraadt   426:        }
                    427: }
                    428:
                    429: /*
                    430:  * Set up the "remote" tty's state
                    431:  */
1.19      millert   432: int
1.25      deraadt   433: ttysetup(int speed)
1.1       deraadt   434: {
1.2       deraadt   435:        struct termios  cntrl;
1.1       deraadt   436:
1.19      millert   437:        if (tcgetattr(FD, &cntrl))
                    438:                return (-1);
                    439:        cfsetspeed(&cntrl, speed);
1.2       deraadt   440:        cntrl.c_cflag &= ~(CSIZE|PARENB);
                    441:        cntrl.c_cflag |= CS8;
1.48      nicm      442:        if (vgetnum(DC))
1.5       millert   443:                cntrl.c_cflag |= CLOCAL;
1.48      nicm      444:        if (vgetnum(HARDWAREFLOW))
1.25      deraadt   445:                cntrl.c_cflag |= CRTSCTS;
1.2       deraadt   446:        cntrl.c_iflag &= ~(ISTRIP|ICRNL);
                    447:        cntrl.c_oflag &= ~OPOST;
                    448:        cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
                    449:        cntrl.c_cc[VMIN] = 1;
                    450:        cntrl.c_cc[VTIME] = 0;
1.48      nicm      451:        if (vgetnum(TAND))
1.2       deraadt   452:                cntrl.c_iflag |= IXOFF;
1.19      millert   453:        return (tcsetattr(FD, TCSAFLUSH, &cntrl));
1.1       deraadt   454: }
                    455:
                    456: static char partab[0200];
                    457:
                    458: /*
                    459:  * Do a write to the remote machine with the correct parity.
                    460:  * We are doing 8 bit wide output, so we just generate a character
                    461:  * with the right parity and output it.
                    462:  */
1.8       deraadt   463: void
1.27      deraadt   464: parwrite(int fd, char *buf, size_t n)
1.1       deraadt   465: {
1.15      millert   466:        int i;
                    467:        char *bp;
1.1       deraadt   468:
                    469:        bp = buf;
                    470:        if (bits8 == 0)
                    471:                for (i = 0; i < n; i++) {
                    472:                        *bp = partab[(*bp) & 0177];
                    473:                        bp++;
                    474:                }
                    475:        if (write(fd, buf, n) < 0) {
                    476:                if (errno == EIO)
                    477:                        tipabort("Lost carrier.");
                    478:                /* this is questionable */
1.48      nicm      479:                abort();;//
1.1       deraadt   480:                perror("write");
                    481:        }
                    482: }
                    483:
                    484: /*
                    485:  * Build a parity table with appropriate high-order bit.
                    486:  */
1.8       deraadt   487: void
1.25      deraadt   488: setparity(char *defparity)
1.1       deraadt   489: {
1.15      millert   490:        int i, flip, clr, set;
1.1       deraadt   491:        char *parity;
1.4       millert   492:        extern const unsigned char evenpartab[];
1.1       deraadt   493:
1.48      nicm      494:        if (vgetstr(PARITY) == NULL)
                    495:                vsetstr(PARITY, defparity);
                    496:        parity = vgetstr(PARITY);
1.39      nicm      497:        if (strcmp(parity, "none") == 0) {
1.1       deraadt   498:                bits8 = 1;
                    499:                return;
                    500:        }
                    501:        bits8 = 0;
                    502:        flip = 0;
                    503:        clr = 0377;
                    504:        set = 0;
1.39      nicm      505:        if (strcmp(parity, "odd") == 0)
1.1       deraadt   506:                flip = 0200;                    /* reverse bit 7 */
1.39      nicm      507:        else if (strcmp(parity, "zero") == 0)
1.1       deraadt   508:                clr = 0177;                     /* turn off bit 7 */
1.39      nicm      509:        else if (strcmp(parity, "one") == 0)
1.1       deraadt   510:                set = 0200;                     /* turn on bit 7 */
1.39      nicm      511:        else if (strcmp(parity, "even") != 0) {
1.1       deraadt   512:                (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
                    513:                (void) fflush(stderr);
                    514:        }
                    515:        for (i = 0; i < 0200; i++)
1.18      hugh      516:                partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
1.1       deraadt   517: }