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

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