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

Annotation of src/usr.bin/tip/cmds.c, Revision 1.20

1.20    ! otto        1: /*     $OpenBSD: cmds.c,v 1.19 2004/05/26 18:17:58 deraadt Exp $       */
1.5       millert     2: /*     $NetBSD: cmds.c,v 1.7 1997/02/11 09:24:03 mrg 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.16      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: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "@(#)cmds.c     8.1 (Berkeley) 6/6/93";
                     36: #endif
1.20    ! otto       37: static const char rcsid[] = "$OpenBSD: cmds.c,v 1.19 2004/05/26 18:17:58 deraadt Exp $";
1.1       deraadt    38: #endif /* not lint */
                     39:
                     40: #include "tip.h"
                     41: #include "pathnames.h"
                     42:
1.11      millert    43: #include <vis.h>
                     44:
1.1       deraadt    45: /*
                     46:  * tip
                     47:  *
                     48:  * miscellaneous commands
                     49:  */
                     50:
                     51: int    quant[] = { 60, 60, 24 };
                     52:
                     53: char   null = '\0';
                     54: char   *sep[] = { "second", "minute", "hour" };
                     55: static char *argv[10];         /* argument vector for take and put */
                     56:
                     57: void   timeout();              /* timeout function called on alarm */
                     58: void   stopsnd();              /* SIGINT handler during file transfers */
                     59: void   intcopy();              /* interrupt routine for file transfers */
                     60:
                     61: /*
                     62:  * FTP - remote ==> local
                     63:  *  get a file from the remote host
                     64:  */
1.7       deraadt    65: void
1.19      deraadt    66: getfl(char c)
1.1       deraadt    67: {
                     68:        char buf[256], *cp, *expand();
                     69:
                     70:        putchar(c);
                     71:        /*
                     72:         * get the UNIX receiving file's name
                     73:         */
1.6       millert    74:        if (prompt("Local file name? ", copyname, sizeof(copyname)))
1.1       deraadt    75:                return;
                     76:        cp = expand(copyname);
                     77:        if ((sfd = creat(cp, 0666)) < 0) {
                     78:                printf("\r\n%s: cannot creat\r\n", copyname);
                     79:                return;
                     80:        }
                     81:
                     82:        /*
                     83:         * collect parameters
                     84:         */
1.6       millert    85:        if (prompt("List command for remote system? ", buf, sizeof(buf))) {
1.1       deraadt    86:                unlink(copyname);
                     87:                return;
                     88:        }
                     89:        transfer(buf, sfd, value(EOFREAD));
                     90: }
                     91:
                     92: /*
                     93:  * Cu-like take command
                     94:  */
1.7       deraadt    95: void
1.19      deraadt    96: cu_take(char cc)
1.1       deraadt    97: {
                     98:        int fd, argc;
                     99:        char line[BUFSIZ], *expand(), *cp;
                    100:
1.6       millert   101:        if (prompt("[take] ", copyname, sizeof(copyname)))
1.1       deraadt   102:                return;
1.7       deraadt   103:        if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 ||
                    104:            argc > 2) {
1.1       deraadt   105:                printf("usage: <take> from [to]\r\n");
                    106:                return;
                    107:        }
                    108:        if (argc == 1)
                    109:                argv[1] = argv[0];
                    110:        cp = expand(argv[1]);
                    111:        if ((fd = creat(cp, 0666)) < 0) {
                    112:                printf("\r\n%s: cannot create\r\n", argv[1]);
                    113:                return;
                    114:        }
1.5       millert   115:        (void)snprintf(line, sizeof(line), "cat %s;echo \01", argv[0]);
1.1       deraadt   116:        transfer(line, fd, "\01");
                    117: }
                    118:
                    119: static jmp_buf intbuf;
1.7       deraadt   120:
1.1       deraadt   121: /*
                    122:  * Bulk transfer routine --
                    123:  *  used by getfl(), cu_take(), and pipefile()
                    124:  */
1.7       deraadt   125: void
1.1       deraadt   126: transfer(buf, fd, eofchars)
                    127:        char *buf, *eofchars;
1.7       deraadt   128:        int fd;
1.1       deraadt   129: {
1.13      millert   130:        int ct;
1.1       deraadt   131:        char c, buffer[BUFSIZ];
1.7       deraadt   132:        char *p = buffer;
1.13      millert   133:        int cnt, eof;
1.1       deraadt   134:        time_t start;
                    135:        sig_t f;
                    136:        char r;
                    137:
1.8       deraadt   138:        parwrite(FD, buf, size(buf));
1.1       deraadt   139:        quit = 0;
1.19      deraadt   140:        kill(tipout_pid, SIGIOT);
1.1       deraadt   141:        read(repdes[0], (char *)&ccc, 1);  /* Wait until read process stops */
                    142:
                    143:        /*
                    144:         * finish command
                    145:         */
                    146:        r = '\r';
1.8       deraadt   147:        parwrite(FD, &r, 1);
1.1       deraadt   148:        do
                    149:                read(FD, &c, 1);
1.2       deraadt   150:        while ((c&STRIP_PAR) != '\n');
                    151:        tcsetattr(0, TCSAFLUSH, &defchars);
1.1       deraadt   152:
                    153:        (void) setjmp(intbuf);
                    154:        f = signal(SIGINT, intcopy);
                    155:        start = time(0);
                    156:        for (ct = 0; !quit;) {
                    157:                eof = read(FD, &c, 1) <= 0;
1.2       deraadt   158:                c &= STRIP_PAR;
1.1       deraadt   159:                if (quit)
                    160:                        continue;
                    161:                if (eof || any(c, eofchars))
                    162:                        break;
                    163:                if (c == 0)
                    164:                        continue;       /* ignore nulls */
                    165:                if (c == '\r')
                    166:                        continue;
                    167:                *p++ = c;
                    168:
                    169:                if (c == '\n' && boolean(value(VERBOSE)))
                    170:                        printf("\r%d", ++ct);
                    171:                if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) {
                    172:                        if (write(fd, buffer, cnt) != cnt) {
                    173:                                printf("\r\nwrite error\r\n");
                    174:                                quit = 1;
                    175:                        }
                    176:                        p = buffer;
                    177:                }
                    178:        }
1.7       deraadt   179:        if ((cnt = (p-buffer)))
1.1       deraadt   180:                if (write(fd, buffer, cnt) != cnt)
                    181:                        printf("\r\nwrite error\r\n");
                    182:
                    183:        if (boolean(value(VERBOSE)))
                    184:                prtime(" lines transferred in ", time(0)-start);
1.2       deraadt   185:        tcsetattr(0, TCSAFLUSH, &term);
1.1       deraadt   186:        write(fildes[1], (char *)&ccc, 1);
                    187:        signal(SIGINT, f);
                    188:        close(fd);
                    189: }
                    190:
                    191: /*
                    192:  * FTP - remote ==> local process
                    193:  *   send remote input to local process via pipe
                    194:  */
1.7       deraadt   195: void
1.1       deraadt   196: pipefile()
                    197: {
1.15      mpech     198:        int pdes[2];
1.1       deraadt   199:        char buf[256];
                    200:        int status, p;
1.15      mpech     201:        pid_t cpid;
1.1       deraadt   202:
1.6       millert   203:        if (prompt("Local command? ", buf, sizeof(buf)))
1.1       deraadt   204:                return;
                    205:
                    206:        if (pipe(pdes)) {
                    207:                printf("can't establish pipe\r\n");
                    208:                return;
                    209:        }
                    210:
                    211:        if ((cpid = fork()) < 0) {
                    212:                printf("can't fork!\r\n");
                    213:                return;
                    214:        } else if (cpid) {
1.6       millert   215:                if (prompt("List command for remote system? ", buf, sizeof(buf))) {
1.1       deraadt   216:                        close(pdes[0]), close(pdes[1]);
                    217:                        kill (cpid, SIGKILL);
                    218:                } else {
                    219:                        close(pdes[0]);
                    220:                        signal(SIGPIPE, intcopy);
                    221:                        transfer(buf, pdes[1], value(EOFREAD));
                    222:                        signal(SIGPIPE, SIG_DFL);
                    223:                        while ((p = wait(&status)) > 0 && p != cpid)
                    224:                                ;
                    225:                }
                    226:        } else {
1.13      millert   227:                int f;
1.1       deraadt   228:
                    229:                dup2(pdes[0], 0);
                    230:                close(pdes[0]);
                    231:                for (f = 3; f < 20; f++)
                    232:                        close(f);
                    233:                execute(buf);
                    234:                printf("can't execl!\r\n");
                    235:                exit(0);
                    236:        }
                    237: }
                    238:
                    239: /*
                    240:  * Interrupt service routine for FTP
                    241:  */
                    242: void
                    243: stopsnd()
                    244: {
                    245:
                    246:        stop = 1;
                    247:        signal(SIGINT, SIG_IGN);
                    248: }
                    249:
                    250: /*
                    251:  * FTP - local ==> remote
                    252:  *  send local file to remote host
                    253:  *  terminate transmission with pseudo EOF sequence
                    254:  */
1.7       deraadt   255: void
1.1       deraadt   256: sendfile(cc)
                    257:        char cc;
                    258: {
                    259:        FILE *fd;
                    260:        char *fnamex;
                    261:        char *expand();
                    262:
                    263:        putchar(cc);
                    264:        /*
                    265:         * get file name
                    266:         */
1.6       millert   267:        if (prompt("Local file name? ", fname, sizeof(fname)))
1.1       deraadt   268:                return;
                    269:
                    270:        /*
                    271:         * look up file
                    272:         */
                    273:        fnamex = expand(fname);
                    274:        if ((fd = fopen(fnamex, "r")) == NULL) {
                    275:                printf("%s: cannot open\r\n", fname);
                    276:                return;
                    277:        }
                    278:        transmit(fd, value(EOFWRITE), NULL);
1.2       deraadt   279:        if (!boolean(value(ECHOCHECK)))
                    280:                tcdrain(FD);
1.1       deraadt   281: }
                    282:
                    283: /*
                    284:  * Bulk transfer routine to remote host --
                    285:  *   used by sendfile() and cu_put()
                    286:  */
1.7       deraadt   287: void
1.1       deraadt   288: transmit(fd, eofchars, command)
                    289:        FILE *fd;
                    290:        char *eofchars, *command;
                    291: {
                    292:        char *pc, lastc;
                    293:        int c, ccount, lcount;
                    294:        time_t start_t, stop_t;
                    295:        sig_t f;
                    296:
1.19      deraadt   297:        kill(tipout_pid, SIGIOT);       /* put TIPOUT into a wait state */
1.1       deraadt   298:        stop = 0;
                    299:        f = signal(SIGINT, stopsnd);
1.2       deraadt   300:        tcsetattr(0, TCSAFLUSH, &defchars);
1.1       deraadt   301:        read(repdes[0], (char *)&ccc, 1);
                    302:        if (command != NULL) {
                    303:                for (pc = command; *pc; pc++)
                    304:                        send(*pc);
                    305:                if (boolean(value(ECHOCHECK)))
                    306:                        read(FD, (char *)&c, 1);        /* trailing \n */
                    307:                else {
1.2       deraadt   308:                        tcdrain(FD);
1.1       deraadt   309:                        sleep(5); /* wait for remote stty to take effect */
                    310:                }
                    311:        }
                    312:        lcount = 0;
                    313:        lastc = '\0';
                    314:        start_t = time(0);
                    315:        while (1) {
                    316:                ccount = 0;
                    317:                do {
                    318:                        c = getc(fd);
                    319:                        if (stop)
                    320:                                goto out;
                    321:                        if (c == EOF)
                    322:                                goto out;
                    323:                        if (c == 0177 && !boolean(value(RAWFTP)))
                    324:                                continue;
                    325:                        lastc = c;
                    326:                        if (c < 040) {
                    327:                                if (c == '\n') {
                    328:                                        if (!boolean(value(RAWFTP)))
                    329:                                                c = '\r';
                    330:                                }
                    331:                                else if (c == '\t') {
                    332:                                        if (!boolean(value(RAWFTP))) {
                    333:                                                if (boolean(value(TABEXPAND))) {
                    334:                                                        send(' ');
                    335:                                                        while ((++ccount % 8) != 0)
                    336:                                                                send(' ');
                    337:                                                        continue;
                    338:                                                }
                    339:                                        }
                    340:                                } else
                    341:                                        if (!boolean(value(RAWFTP)))
                    342:                                                continue;
                    343:                        }
                    344:                        send(c);
                    345:                } while (c != '\r' && !boolean(value(RAWFTP)));
                    346:                if (boolean(value(VERBOSE)))
                    347:                        printf("\r%d", ++lcount);
                    348:                if (boolean(value(ECHOCHECK))) {
                    349:                        timedout = 0;
                    350:                        alarm((long)value(ETIMEOUT));
                    351:                        do {    /* wait for prompt */
                    352:                                read(FD, (char *)&c, 1);
                    353:                                if (timedout || stop) {
                    354:                                        if (timedout)
                    355:                                                printf("\r\ntimed out at eol\r\n");
                    356:                                        alarm(0);
                    357:                                        goto out;
                    358:                                }
1.2       deraadt   359:                        } while ((c&STRIP_PAR) != character(value(PROMPT)));
1.1       deraadt   360:                        alarm(0);
                    361:                }
                    362:        }
                    363: out:
                    364:        if (lastc != '\n' && !boolean(value(RAWFTP)))
                    365:                send('\r');
                    366:        if (eofchars) {
                    367:                for (pc = eofchars; *pc; pc++)
                    368:                        send(*pc);
                    369:        }
                    370:        stop_t = time(0);
                    371:        fclose(fd);
                    372:        signal(SIGINT, f);
1.10      deraadt   373:        if (boolean(value(VERBOSE))) {
1.1       deraadt   374:                if (boolean(value(RAWFTP)))
                    375:                        prtime(" chars transferred in ", stop_t-start_t);
                    376:                else
                    377:                        prtime(" lines transferred in ", stop_t-start_t);
1.10      deraadt   378:        }
1.1       deraadt   379:        write(fildes[1], (char *)&ccc, 1);
1.2       deraadt   380:        tcsetattr(0, TCSAFLUSH, &term);
1.1       deraadt   381: }
                    382:
                    383: /*
                    384:  * Cu-like put command
                    385:  */
1.7       deraadt   386: void
1.1       deraadt   387: cu_put(cc)
                    388:        char cc;
                    389: {
                    390:        FILE *fd;
                    391:        char line[BUFSIZ];
                    392:        int argc;
                    393:        char *expand();
                    394:        char *copynamex;
                    395:
1.6       millert   396:        if (prompt("[put] ", copyname, sizeof(copyname)))
1.1       deraadt   397:                return;
1.7       deraadt   398:        if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 ||
                    399:            argc > 2) {
1.1       deraadt   400:                printf("usage: <put> from [to]\r\n");
                    401:                return;
                    402:        }
                    403:        if (argc == 1)
                    404:                argv[1] = argv[0];
                    405:        copynamex = expand(argv[0]);
                    406:        if ((fd = fopen(copynamex, "r")) == NULL) {
                    407:                printf("%s: cannot open\r\n", copynamex);
                    408:                return;
                    409:        }
                    410:        if (boolean(value(ECHOCHECK)))
1.5       millert   411:                (void)snprintf(line, sizeof(line), "cat>%s\r", argv[1]);
1.1       deraadt   412:        else
1.5       millert   413:                (void)snprintf(line, sizeof(line),
                    414:                    "stty -echo;cat>%s;stty echo\r", argv[1]);
1.1       deraadt   415:        transmit(fd, "\04", line);
                    416: }
                    417:
                    418: /*
                    419:  * FTP - send single character
                    420:  *  wait for echo & handle timeout
                    421:  */
1.7       deraadt   422: void
1.19      deraadt   423: send(int c)
1.1       deraadt   424: {
                    425:        char cc;
                    426:        int retry = 0;
                    427:
                    428:        cc = c;
1.8       deraadt   429:        parwrite(FD, &cc, 1);
1.1       deraadt   430:        if (number(value(CDELAY)) > 0 && c != '\r')
1.18      deraadt   431:                usleep(number(value(CDELAY)));
1.1       deraadt   432:        if (!boolean(value(ECHOCHECK))) {
                    433:                if (number(value(LDELAY)) > 0 && c == '\r')
1.18      deraadt   434:                        usleep(number(value(LDELAY)));
1.1       deraadt   435:                return;
                    436:        }
                    437: tryagain:
                    438:        timedout = 0;
                    439:        alarm((long)value(ETIMEOUT));
                    440:        read(FD, &cc, 1);
                    441:        alarm(0);
                    442:        if (timedout) {
                    443:                printf("\r\ntimeout error (%s)\r\n", ctrl(c));
                    444:                if (retry++ > 3)
                    445:                        return;
1.8       deraadt   446:                parwrite(FD, &null, 1); /* poke it */
1.1       deraadt   447:                goto tryagain;
                    448:        }
                    449: }
                    450:
                    451: void
                    452: timeout()
                    453: {
                    454:        signal(SIGALRM, timeout);
                    455:        timedout = 1;
                    456: }
                    457:
                    458: /*
                    459:  * Stolen from consh() -- puts a remote file on the output of a local command.
                    460:  *     Identical to consh() except for where stdout goes.
                    461:  */
1.7       deraadt   462: void
1.1       deraadt   463: pipeout(c)
                    464: {
                    465:        char buf[256];
1.15      mpech     466:        int status, p;
                    467:        pid_t cpid;
1.7       deraadt   468:        time_t start = time(NULL);
1.1       deraadt   469:
                    470:        putchar(c);
1.6       millert   471:        if (prompt("Local command? ", buf, sizeof(buf)))
1.1       deraadt   472:                return;
1.19      deraadt   473:        kill(tipout_pid, SIGIOT);       /* put TIPOUT into a wait state */
1.1       deraadt   474:        signal(SIGINT, SIG_IGN);
                    475:        signal(SIGQUIT, SIG_IGN);
1.2       deraadt   476:        tcsetattr(0, TCSAFLUSH, &defchars);
1.1       deraadt   477:        read(repdes[0], (char *)&ccc, 1);
                    478:        /*
                    479:         * Set up file descriptors in the child and
                    480:         *  let it go...
                    481:         */
                    482:        if ((cpid = fork()) < 0)
                    483:                printf("can't fork!\r\n");
                    484:        else if (cpid) {
1.7       deraadt   485:                start = time(NULL);
1.1       deraadt   486:                while ((p = wait(&status)) > 0 && p != cpid)
                    487:                        ;
                    488:        } else {
1.13      millert   489:                int i;
1.1       deraadt   490:
                    491:                dup2(FD, 1);
                    492:                for (i = 3; i < 20; i++)
                    493:                        close(i);
                    494:                signal(SIGINT, SIG_DFL);
                    495:                signal(SIGQUIT, SIG_DFL);
                    496:                execute(buf);
                    497:                printf("can't find `%s'\r\n", buf);
                    498:                exit(0);
                    499:        }
                    500:        if (boolean(value(VERBOSE)))
                    501:                prtime("away for ", time(0)-start);
                    502:        write(fildes[1], (char *)&ccc, 1);
1.2       deraadt   503:        tcsetattr(0, TCSAFLUSH, &term);
1.1       deraadt   504:        signal(SIGINT, SIG_DFL);
                    505:        signal(SIGQUIT, SIG_DFL);
                    506: }
                    507:
                    508: #ifdef CONNECT
                    509: /*
                    510:  * Fork a program with:
                    511:  *  0 <-> remote tty in
                    512:  *  1 <-> remote tty out
1.20    ! otto      513:  *  2 <-> local tty stderr
1.1       deraadt   514:  */
1.7       deraadt   515: void
1.19      deraadt   516: consh(int c)
1.1       deraadt   517: {
                    518:        char buf[256];
1.15      mpech     519:        int status, p;
                    520:        pid_t cpid;
1.7       deraadt   521:        time_t start = time(NULL);
1.1       deraadt   522:
                    523:        putchar(c);
1.6       millert   524:        if (prompt("Local command? ", buf, sizeof(buf)))
1.1       deraadt   525:                return;
1.19      deraadt   526:        kill(tipout_pid, SIGIOT);       /* put TIPOUT into a wait state */
1.1       deraadt   527:        signal(SIGINT, SIG_IGN);
                    528:        signal(SIGQUIT, SIG_IGN);
1.2       deraadt   529:        tcsetattr(0, TCSAFLUSH, &defchars);
1.1       deraadt   530:        read(repdes[0], (char *)&ccc, 1);
                    531:        /*
                    532:         * Set up file descriptors in the child and
                    533:         *  let it go...
                    534:         */
                    535:        if ((cpid = fork()) < 0)
                    536:                printf("can't fork!\r\n");
                    537:        else if (cpid) {
                    538:                start = time(0);
                    539:                while ((p = wait(&status)) > 0 && p != cpid)
                    540:                        ;
                    541:        } else {
1.13      millert   542:                int i;
1.1       deraadt   543:
                    544:                dup2(FD, 0);
                    545:                dup2(3, 1);
1.20    ! otto      546:                closefrom(3);
1.1       deraadt   547:                signal(SIGINT, SIG_DFL);
                    548:                signal(SIGQUIT, SIG_DFL);
                    549:                execute(buf);
                    550:                printf("can't find `%s'\r\n", buf);
                    551:                exit(0);
                    552:        }
                    553:        if (boolean(value(VERBOSE)))
                    554:                prtime("away for ", time(0)-start);
                    555:        write(fildes[1], (char *)&ccc, 1);
1.2       deraadt   556:        tcsetattr(0, TCSAFLUSH, &term);
1.1       deraadt   557:        signal(SIGINT, SIG_DFL);
                    558:        signal(SIGQUIT, SIG_DFL);
                    559: }
                    560: #endif
                    561:
                    562: /*
                    563:  * Escape to local shell
                    564:  */
1.7       deraadt   565: void
1.19      deraadt   566: shell(void)
1.1       deraadt   567: {
1.15      mpech     568:        int status;
1.1       deraadt   569:        char *cp;
1.15      mpech     570:        pid_t shpid;
1.1       deraadt   571:
                    572:        printf("[sh]\r\n");
                    573:        signal(SIGINT, SIG_IGN);
                    574:        signal(SIGQUIT, SIG_IGN);
                    575:        unraw();
1.7       deraadt   576:        if ((shpid = fork())) {
1.1       deraadt   577:                while (shpid != wait(&status));
                    578:                raw();
                    579:                printf("\r\n!\r\n");
                    580:                signal(SIGINT, SIG_DFL);
                    581:                signal(SIGQUIT, SIG_DFL);
                    582:                return;
                    583:        } else {
                    584:                signal(SIGQUIT, SIG_DFL);
                    585:                signal(SIGINT, SIG_DFL);
1.4       millert   586:                if ((cp = strrchr(value(SHELL), '/')) == NULL)
1.1       deraadt   587:                        cp = value(SHELL);
                    588:                else
                    589:                        cp++;
                    590:                shell_uid();
1.9       deraadt   591:                execl(value(SHELL), cp, (char *)NULL);
1.1       deraadt   592:                printf("\r\ncan't execl!\r\n");
                    593:                exit(1);
                    594:        }
                    595: }
                    596:
                    597: /*
                    598:  * TIPIN portion of scripting
                    599:  *   initiate the conversation with TIPOUT
                    600:  */
1.7       deraadt   601: void
1.1       deraadt   602: setscript()
                    603: {
                    604:        char c;
                    605:        /*
                    606:         * enable TIPOUT side for dialogue
                    607:         */
1.19      deraadt   608:        kill(tipout_pid, SIGEMT);
1.1       deraadt   609:        if (boolean(value(SCRIPT)))
                    610:                write(fildes[1], value(RECORD), size(value(RECORD)));
                    611:        write(fildes[1], "\n", 1);
                    612:        /*
                    613:         * wait for TIPOUT to finish
                    614:         */
                    615:        read(repdes[0], &c, 1);
                    616:        if (c == 'n')
                    617:                printf("can't create %s\r\n", value(RECORD));
                    618: }
                    619:
                    620: /*
                    621:  * Change current working directory of
                    622:  *   local portion of tip
                    623:  */
1.7       deraadt   624: void
1.1       deraadt   625: chdirectory()
                    626: {
1.6       millert   627:        char dirname[PATH_MAX];
1.13      millert   628:        char *cp = dirname;
1.1       deraadt   629:
1.6       millert   630:        if (prompt("[cd] ", dirname, sizeof(dirname))) {
1.1       deraadt   631:                if (stoprompt)
                    632:                        return;
                    633:                cp = value(HOME);
                    634:        }
                    635:        if (chdir(cp) < 0)
                    636:                printf("%s: bad directory\r\n", cp);
                    637:        printf("!\r\n");
                    638: }
                    639:
1.7       deraadt   640: void
1.1       deraadt   641: tipabort(msg)
                    642:        char *msg;
                    643: {
                    644:
1.19      deraadt   645:        kill(tipout_pid, SIGTERM);
1.1       deraadt   646:        disconnect(msg);
                    647:        if (msg != NOSTR)
                    648:                printf("\r\n%s", msg);
                    649:        printf("\r\n[EOT]\r\n");
                    650:        daemon_uid();
                    651:        (void)uu_unlock(uucplock);
                    652:        unraw();
                    653:        exit(0);
                    654: }
                    655:
1.7       deraadt   656: void
1.1       deraadt   657: finish()
                    658: {
                    659:        char *dismsg;
                    660:
                    661:        if ((dismsg = value(DISCONNECT)) != NOSTR) {
                    662:                write(FD, dismsg, strlen(dismsg));
                    663:                sleep(5);
                    664:        }
                    665:        tipabort(NOSTR);
                    666: }
                    667:
                    668: void
                    669: intcopy()
                    670: {
                    671:        raw();
                    672:        quit = 1;
                    673:        longjmp(intbuf, 1);
                    674: }
                    675:
1.7       deraadt   676: void
1.1       deraadt   677: execute(s)
                    678:        char *s;
                    679: {
1.13      millert   680:        char *cp;
1.1       deraadt   681:
1.4       millert   682:        if ((cp = strrchr(value(SHELL), '/')) == NULL)
1.1       deraadt   683:                cp = value(SHELL);
                    684:        else
                    685:                cp++;
                    686:        shell_uid();
1.9       deraadt   687:        execl(value(SHELL), cp, "-c", s, (char *)NULL);
1.1       deraadt   688: }
                    689:
1.7       deraadt   690: int
                    691: args(buf, a, num)
1.1       deraadt   692:        char *buf, *a[];
1.7       deraadt   693:        int num;
1.1       deraadt   694: {
1.13      millert   695:        char *p = buf, *start;
                    696:        char **parg = a;
                    697:        int n = 0;
1.1       deraadt   698:
                    699:        do {
                    700:                while (*p && (*p == ' ' || *p == '\t'))
                    701:                        p++;
                    702:                start = p;
                    703:                if (*p)
                    704:                        *parg = p;
                    705:                while (*p && (*p != ' ' && *p != '\t'))
                    706:                        p++;
                    707:                if (p != start)
                    708:                        parg++, n++;
                    709:                if (*p)
                    710:                        *p++ = '\0';
1.7       deraadt   711:        } while (*p && n < num);
1.1       deraadt   712:
                    713:        return(n);
                    714: }
                    715:
1.7       deraadt   716: void
1.1       deraadt   717: prtime(s, a)
                    718:        char *s;
                    719:        time_t a;
                    720: {
1.13      millert   721:        int i;
1.1       deraadt   722:        int nums[3];
                    723:
                    724:        for (i = 0; i < 3; i++) {
                    725:                nums[i] = (int)(a % quant[i]);
                    726:                a /= quant[i];
                    727:        }
                    728:        printf("%s", s);
                    729:        while (--i >= 0)
1.14      hugh      730:                if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
1.1       deraadt   731:                        printf("%d %s%c ", nums[i], sep[i],
                    732:                                nums[i] == 1 ? '\0' : 's');
                    733:        printf("\r\n!\r\n");
                    734: }
                    735:
1.7       deraadt   736: void
1.1       deraadt   737: variable()
                    738: {
                    739:        char    buf[256];
                    740:
1.6       millert   741:        if (prompt("[set] ", buf, sizeof(buf)))
1.1       deraadt   742:                return;
                    743:        vlex(buf);
                    744:        if (vtable[BEAUTIFY].v_access&CHANGED) {
                    745:                vtable[BEAUTIFY].v_access &= ~CHANGED;
1.19      deraadt   746:                kill(tipout_pid, SIGSYS);
1.1       deraadt   747:        }
                    748:        if (vtable[SCRIPT].v_access&CHANGED) {
                    749:                vtable[SCRIPT].v_access &= ~CHANGED;
                    750:                setscript();
                    751:                /*
                    752:                 * So that "set record=blah script" doesn't
                    753:                 *  cause two transactions to occur.
                    754:                 */
                    755:                if (vtable[RECORD].v_access&CHANGED)
                    756:                        vtable[RECORD].v_access &= ~CHANGED;
                    757:        }
                    758:        if (vtable[RECORD].v_access&CHANGED) {
                    759:                vtable[RECORD].v_access &= ~CHANGED;
                    760:                if (boolean(value(SCRIPT)))
                    761:                        setscript();
                    762:        }
                    763:        if (vtable[TAND].v_access&CHANGED) {
                    764:                vtable[TAND].v_access &= ~CHANGED;
                    765:                if (boolean(value(TAND)))
                    766:                        tandem("on");
                    767:                else
                    768:                        tandem("off");
                    769:        }
                    770:        if (vtable[LECHO].v_access&CHANGED) {
                    771:                vtable[LECHO].v_access &= ~CHANGED;
                    772:                HD = boolean(value(LECHO));
                    773:        }
                    774:        if (vtable[PARITY].v_access&CHANGED) {
                    775:                vtable[PARITY].v_access &= ~CHANGED;
1.7       deraadt   776:                setparity(NOSTR);
1.1       deraadt   777:        }
1.17      millert   778:        if (vtable[HARDWAREFLOW].v_access&CHANGED) {
                    779:                vtable[HARDWAREFLOW].v_access &= ~CHANGED;
                    780:                if (boolean(value(HARDWAREFLOW)))
                    781:                        hardwareflow("on");
                    782:                else
                    783:                        hardwareflow("off");
                    784:        }
1.11      millert   785: }
                    786:
                    787: void
                    788: listvariables()
                    789: {
                    790:        value_t *p;
                    791:        char buf[BUFSIZ];
                    792:
                    793:        puts("v\r");
                    794:        for (p = vtable; p->v_name; p++) {
                    795:                fputs(p->v_name, stdout);
                    796:                switch (p->v_type&TMASK) {
                    797:                case STRING:
                    798:                        if (p->v_value) {
                    799:                                strnvis(buf, p->v_value, sizeof(buf),
                    800:                                    VIS_WHITE|VIS_OCTAL);
                    801:                                printf(" %s", buf);
                    802:                        }
                    803:                        putchar('\r');
                    804:                        putchar('\n');
                    805:                        break;
                    806:                case NUMBER:
1.12      pvalchev  807:                        printf(" %ld\r\n", number(p->v_value));
1.11      millert   808:                        break;
                    809:                case BOOL:
                    810:                        printf(" %s\r\n",
1.17      millert   811:                            !boolean(p->v_value) ? "false" : "true");
1.11      millert   812:                        break;
                    813:                case CHAR:
                    814:                        vis(buf, character(p->v_value), VIS_WHITE|VIS_OCTAL, 0);
                    815:                        printf(" %s\r\n", buf);
                    816:                        break;
                    817:                }
                    818:         }
1.1       deraadt   819: }
                    820:
                    821: /*
                    822:  * Turn tandem mode on or off for remote tty.
                    823:  */
1.7       deraadt   824: void
1.1       deraadt   825: tandem(option)
                    826:        char *option;
                    827: {
1.2       deraadt   828:        struct termios  rmtty;
1.1       deraadt   829:
1.2       deraadt   830:        tcgetattr(FD, &rmtty);
                    831:        if (strcmp(option, "on") == 0) {
                    832:                rmtty.c_iflag |= IXOFF;
                    833:                term.c_iflag |= IXOFF;
1.1       deraadt   834:        } else {
1.2       deraadt   835:                rmtty.c_iflag &= ~IXOFF;
                    836:                term.c_iflag &= ~IXOFF;
1.1       deraadt   837:        }
1.2       deraadt   838:        tcsetattr(FD, TCSADRAIN, &rmtty);
                    839:        tcsetattr(0, TCSADRAIN, &term);
1.17      millert   840: }
                    841:
                    842: /*
                    843:  * Turn hardware flow control on or off for remote tty.
                    844:  */
                    845: void
                    846: hardwareflow(option)
                    847:        char *option;
                    848: {
                    849:        struct termios  rmtty;
                    850:
                    851:        tcgetattr(FD, &rmtty);
                    852:        if (strcmp(option, "on") == 0)
                    853:                rmtty.c_iflag |= CRTSCTS;
                    854:        else
                    855:                rmtty.c_iflag &= ~CRTSCTS;
                    856:        tcsetattr(FD, TCSADRAIN, &rmtty);
1.1       deraadt   857: }
                    858:
                    859: /*
                    860:  * Send a break.
                    861:  */
1.7       deraadt   862: void
1.1       deraadt   863: genbrk()
                    864: {
                    865:
                    866:        ioctl(FD, TIOCSBRK, NULL);
                    867:        sleep(1);
                    868:        ioctl(FD, TIOCCBRK, NULL);
                    869: }
                    870:
                    871: /*
                    872:  * Suspend tip
                    873:  */
1.7       deraadt   874: void
1.1       deraadt   875: suspend(c)
                    876:        char c;
                    877: {
                    878:
                    879:        unraw();
                    880:        kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
                    881:        raw();
                    882: }
                    883:
                    884: /*
                    885:  *     expand a file name if it includes shell meta characters
                    886:  */
                    887:
                    888: char *
                    889: expand(name)
                    890:        char name[];
                    891: {
                    892:        static char xname[BUFSIZ];
                    893:        char cmdbuf[BUFSIZ];
1.15      mpech     894:        int l;
1.13      millert   895:        char *cp, *Shell;
1.7       deraadt   896:        int s, pivec[2];
1.15      mpech     897:        pid_t pid;
1.1       deraadt   898:
                    899:        if (!anyof(name, "~{[*?$`'\"\\"))
                    900:                return(name);
                    901:        /* sigint = signal(SIGINT, SIG_IGN); */
                    902:        if (pipe(pivec) < 0) {
                    903:                perror("pipe");
                    904:                /* signal(SIGINT, sigint) */
                    905:                return(name);
                    906:        }
1.5       millert   907:        (void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
1.1       deraadt   908:        if ((pid = vfork()) == 0) {
                    909:                Shell = value(SHELL);
                    910:                if (Shell == NOSTR)
                    911:                        Shell = _PATH_BSHELL;
                    912:                close(pivec[0]);
                    913:                close(1);
                    914:                dup(pivec[1]);
                    915:                close(pivec[1]);
                    916:                close(2);
                    917:                shell_uid();
1.9       deraadt   918:                execl(Shell, Shell, "-c", cmdbuf, (char *)NULL);
1.1       deraadt   919:                _exit(1);
                    920:        }
                    921:        if (pid == -1) {
                    922:                perror("fork");
                    923:                close(pivec[0]);
                    924:                close(pivec[1]);
                    925:                return(NOSTR);
                    926:        }
                    927:        close(pivec[1]);
                    928:        l = read(pivec[0], xname, BUFSIZ);
                    929:        close(pivec[0]);
                    930:        while (wait(&s) != pid);
                    931:                ;
                    932:        s &= 0377;
                    933:        if (s != 0 && s != SIGPIPE) {
                    934:                fprintf(stderr, "\"Echo\" failed\n");
                    935:                return(NOSTR);
                    936:        }
                    937:        if (l < 0) {
                    938:                perror("read");
                    939:                return(NOSTR);
                    940:        }
                    941:        if (l == 0) {
                    942:                fprintf(stderr, "\"%s\": No match\n", name);
                    943:                return(NOSTR);
                    944:        }
                    945:        if (l == BUFSIZ) {
                    946:                fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
                    947:                return(NOSTR);
                    948:        }
                    949:        xname[l] = 0;
                    950:        for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
                    951:                ;
                    952:        *++cp = '\0';
                    953:        return(xname);
                    954: }
                    955:
                    956: /*
                    957:  * Are any of the characters in the two strings the same?
                    958:  */
1.7       deraadt   959: int
1.1       deraadt   960: anyof(s1, s2)
1.13      millert   961:        char *s1, *s2;
1.1       deraadt   962: {
1.13      millert   963:        int c;
1.1       deraadt   964:
1.7       deraadt   965:        while ((c = *s1++))
1.1       deraadt   966:                if (any(c, s2))
                    967:                        return(1);
                    968:        return(0);
                    969: }