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

Annotation of src/usr.bin/ftp/main.c, Revision 1.75

1.75    ! deraadt     1: /*     $OpenBSD: main.c,v 1.74 2009/04/27 08:32:15 sobrado Exp $       */
1.36      millert     2: /*     $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $  */
1.5       deraadt     3:
1.1       deraadt     4: /*
1.44      itojun      5:  * Copyright (C) 1997 and 1998 WIDE Project.
                      6:  * 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. Neither the name of the project nor the names of its contributors
                     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 PROJECT 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 PROJECT 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: /*
1.1       deraadt    34:  * Copyright (c) 1985, 1989, 1993, 1994
                     35:  *     The Regents of the University of California.  All rights reserved.
                     36:  *
                     37:  * Redistribution and use in source and binary forms, with or without
                     38:  * modification, are permitted provided that the following conditions
                     39:  * are met:
                     40:  * 1. Redistributions of source code must retain the above copyright
                     41:  *    notice, this list of conditions and the following disclaimer.
                     42:  * 2. Redistributions in binary form must reproduce the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer in the
                     44:  *    documentation and/or other materials provided with the distribution.
1.53      millert    45:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     50:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     51:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     52:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     53:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     54:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     55:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     56:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  */
                     61:
                     62: /*
                     63:  * FTP User Program -- Command Interface.
                     64:  */
                     65: #include <sys/types.h>
                     66: #include <sys/socket.h>
                     67:
1.18      millert    68: #include <ctype.h>
1.1       deraadt    69: #include <err.h>
                     70: #include <netdb.h>
                     71: #include <pwd.h>
                     72: #include <stdio.h>
1.39      deraadt    73: #include <errno.h>
1.18      millert    74: #include <stdlib.h>
1.1       deraadt    75: #include <string.h>
                     76: #include <unistd.h>
                     77:
                     78: #include "ftp_var.h"
                     79:
1.49      deraadt    80: int family = PF_UNSPEC;
                     81:
1.1       deraadt    82: int
1.54      deraadt    83: main(volatile int argc, char *argv[])
1.1       deraadt    84: {
1.34      millert    85:        int ch, top, rval;
1.1       deraadt    86:        struct passwd *pw = NULL;
1.44      itojun     87:        char *cp, homedir[MAXPATHLEN];
1.38      millert    88:        char *outfile = NULL;
1.62      tedu       89:        const char *errstr;
1.28      millert    90:        int dumb_terminal = 0;
1.1       deraadt    91:
1.44      itojun     92:        ftpport = "ftp";
                     93:        httpport = "http";
1.61      deraadt    94: #ifndef SMALL
                     95:        httpsport = "https";
1.69      martynas   96: #endif /* !SMALL */
1.44      itojun     97:        gateport = getenv("FTPSERVERPORT");
1.51      millert    98:        if (gateport == NULL || *gateport == '\0')
1.44      itojun     99:                gateport = "ftpgate";
1.1       deraadt   100:        doglob = 1;
                    101:        interactive = 1;
                    102:        autologin = 1;
1.37      millert   103:        passivemode = 1;
                    104:        activefallback = 1;
1.17      millert   105:        preserve = 1;
                    106:        verbose = 0;
                    107:        progress = 0;
1.36      millert   108:        gatemode = 0;
1.25      millert   109: #ifndef SMALL
1.22      millert   110:        editing = 0;
1.28      millert   111:        el = NULL;
                    112:        hist = NULL;
1.63      pyr       113:        cookiefile = NULL;
1.67      martynas  114:        resume = 0;
1.69      martynas  115: #endif /* !SMALL */
1.8       kstailey  116:        mark = HASHBYTES;
1.17      millert   117:        marg_sl = sl_init();
1.47      itojun    118: #ifdef INET6
                    119:        epsv4 = 1;
                    120: #else
                    121:        epsv4 = 0;
                    122: #endif
                    123:        epsv4bad = 0;
1.1       deraadt   124:
1.37      millert   125:        /* Set default operation mode based on FTPMODE environment variable */
1.51      millert   126:        if ((cp = getenv("FTPMODE")) != NULL && *cp != '\0') {
1.37      millert   127:                if (strcmp(cp, "passive") == 0) {
                    128:                        passivemode = 1;
                    129:                        activefallback = 0;
                    130:                } else if (strcmp(cp, "active") == 0) {
                    131:                        passivemode = 0;
                    132:                        activefallback = 0;
                    133:                } else if (strcmp(cp, "gate") == 0) {
                    134:                        gatemode = 1;
                    135:                } else if (strcmp(cp, "auto") == 0) {
                    136:                        passivemode = 1;
                    137:                        activefallback = 1;
                    138:                } else
                    139:                        warnx("unknown FTPMODE: %s.  Using defaults", cp);
                    140:        }
                    141:
                    142:        if (strcmp(__progname, "gate-ftp") == 0)
1.36      millert   143:                gatemode = 1;
                    144:        gateserver = getenv("FTPSERVER");
                    145:        if (gateserver == NULL || *gateserver == '\0')
                    146:                gateserver = GATE_SERVER;
                    147:        if (gatemode) {
                    148:                if (*gateserver == '\0') {
                    149:                        warnx(
1.37      millert   150: "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
1.36      millert   151:                        gatemode = 0;
                    152:                }
                    153:        }
1.17      millert   154:
1.31      millert   155:        cp = getenv("TERM");
1.51      millert   156:        dumb_terminal = (cp == NULL || *cp == '\0' || !strcmp(cp, "dumb") ||
1.31      millert   157:            !strcmp(cp, "emacs") || !strcmp(cp, "su"));
1.17      millert   158:        fromatty = isatty(fileno(stdin));
1.22      millert   159:        if (fromatty) {
1.17      millert   160:                verbose = 1;            /* verbose if from a tty */
1.25      millert   161: #ifndef SMALL
1.28      millert   162:                if (!dumb_terminal)
1.34      millert   163:                        editing = 1;    /* editing mode on if tty is usable */
1.69      martynas  164: #endif /* !SMALL */
1.22      millert   165:        }
1.30      deraadt   166:
                    167:        ttyout = stdout;
1.33      millert   168:        if (isatty(fileno(ttyout)) && !dumb_terminal && foregroundproc())
1.34      millert   169:                progress = 1;           /* progress bar on if tty is usable */
1.32      deraadt   170:
1.63      pyr       171: #ifndef SMALL
                    172:        cookiefile = getenv("http_cookies");
1.69      martynas  173: #endif /* !SMALL */
1.63      pyr       174:
1.73      martynas  175:        while ((ch = getopt(argc, argv, "46AaCc:dEegik:mno:pP:r:tvV")) != -1) {
1.1       deraadt   176:                switch (ch) {
1.49      deraadt   177:                case '4':
                    178:                        family = PF_INET;
                    179:                        break;
                    180:                case '6':
                    181:                        family = PF_INET6;
                    182:                        break;
1.37      millert   183:                case 'A':
                    184:                        activefallback = 0;
                    185:                        passivemode = 0;
                    186:                        break;
                    187:
1.17      millert   188:                case 'a':
                    189:                        anonftp = 1;
                    190:                        break;
                    191:
1.67      martynas  192:                case 'C':
                    193: #ifndef SMALL
                    194:                        resume = 1;
1.69      martynas  195: #endif /* !SMALL */
1.67      martynas  196:                        break;
                    197:
1.63      pyr       198:                case 'c':
                    199: #ifndef SMALL
                    200:                        cookiefile = optarg;
1.69      martynas  201: #endif /* !SMALL */
1.63      pyr       202:                        break;
                    203:
1.1       deraadt   204:                case 'd':
1.70      martynas  205: #ifndef SMALL
1.1       deraadt   206:                        options |= SO_DEBUG;
                    207:                        debug++;
1.70      martynas  208: #endif /* !SMALL */
1.1       deraadt   209:                        break;
1.17      millert   210:
1.59      fgsch     211:                case 'E':
                    212:                        epsv4 = 0;
                    213:                        break;
                    214:
1.28      millert   215:                case 'e':
                    216: #ifndef SMALL
1.22      millert   217:                        editing = 0;
1.69      martynas  218: #endif /* !SMALL */
1.21      kstailey  219:                        break;
                    220:
1.1       deraadt   221:                case 'g':
                    222:                        doglob = 0;
                    223:                        break;
                    224:
                    225:                case 'i':
                    226:                        interactive = 0;
1.41      millert   227:                        break;
                    228:
1.65      espie     229:                case 'k':
                    230:                        keep_alive_timeout = strtonum(optarg, 0, INT_MAX,
                    231:                            &errstr);
                    232:                        if (errstr != NULL) {
                    233:                                warnx("keep alive amount is %s: %s", errstr,
                    234:                                        optarg);
                    235:                                usage();
                    236:                        }
                    237:                        break;
1.41      millert   238:                case 'm':
1.42      millert   239:                        progress = -1;
1.1       deraadt   240:                        break;
                    241:
                    242:                case 'n':
                    243:                        autologin = 0;
                    244:                        break;
                    245:
1.38      millert   246:                case 'o':
                    247:                        outfile = optarg;
                    248:                        if (strcmp(outfile, "-") == 0)
                    249:                                ttyout = stderr;
                    250:                        break;
                    251:
1.3       deraadt   252:                case 'p':
1.17      millert   253:                        passivemode = 1;
1.37      millert   254:                        activefallback = 0;
1.3       deraadt   255:                        break;
                    256:
1.17      millert   257:                case 'P':
1.44      itojun    258:                        ftpport = optarg;
1.7       mickey    259:                        break;
                    260:
1.18      millert   261:                case 'r':
1.62      tedu      262:                        retry_connect = strtonum(optarg, 0, INT_MAX, &errstr);
1.65      espie     263:                        if (errstr != NULL) {
                    264:                                warnx("retry amount is %s: %s", errstr,
1.62      tedu      265:                                        optarg);
1.65      espie     266:                                usage();
                    267:                        }
1.18      millert   268:                        break;
                    269:
1.1       deraadt   270:                case 't':
1.17      millert   271:                        trace = 1;
1.1       deraadt   272:                        break;
                    273:
                    274:                case 'v':
1.17      millert   275:                        verbose = 1;
                    276:                        break;
                    277:
                    278:                case 'V':
                    279:                        verbose = 0;
1.1       deraadt   280:                        break;
                    281:
                    282:                default:
1.17      millert   283:                        usage();
1.1       deraadt   284:                }
                    285:        }
                    286:        argc -= optind;
                    287:        argv += optind;
1.63      pyr       288:
                    289: #ifndef SMALL
                    290:        cookie_load();
1.69      martynas  291: #endif /* !SMALL */
1.1       deraadt   292:
                    293:        cpend = 0;      /* no pending replies */
                    294:        proxy = 0;      /* proxy not active */
                    295:        crflag = 1;     /* strip c.r. on ascii gets */
                    296:        sendport = -1;  /* not using ports */
                    297:        /*
                    298:         * Set up the home directory in case we're globbing.
                    299:         */
                    300:        cp = getlogin();
                    301:        if (cp != NULL) {
                    302:                pw = getpwnam(cp);
                    303:        }
                    304:        if (pw == NULL)
                    305:                pw = getpwuid(getuid());
                    306:        if (pw != NULL) {
1.52      deraadt   307:                (void)strlcpy(homedir, pw->pw_dir, sizeof homedir);
1.1       deraadt   308:                home = homedir;
1.3       deraadt   309:        }
1.9       michaels  310:
1.17      millert   311:        setttywidth(0);
1.18      millert   312:        (void)signal(SIGWINCH, setttywidth);
1.34      millert   313:
1.17      millert   314:        if (argc > 0) {
1.44      itojun    315:                if (isurl(argv[0])) {
1.17      millert   316:                        anonftp = 1;    /* Handle "automatic" transfers. */
1.38      millert   317:                        rval = auto_fetch(argc, argv, outfile);
1.17      millert   318:                        if (rval >= 0)          /* -1 == connected and cd-ed */
                    319:                                exit(rval);
                    320:                } else {
1.3       deraadt   321:                        char *xargv[5];
                    322:
                    323:                        if (setjmp(toplevel))
                    324:                                exit(0);
1.20      millert   325:                        (void)signal(SIGINT, (sig_t)intr);
                    326:                        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.3       deraadt   327:                        xargv[0] = __progname;
1.17      millert   328:                        xargv[1] = argv[0];
                    329:                        xargv[2] = argv[1];
                    330:                        xargv[3] = argv[2];
                    331:                        xargv[4] = NULL;
1.18      millert   332:                        do {
                    333:                                setpeer(argc+1, xargv);
                    334:                                if (!retry_connect)
                    335:                                        break;
                    336:                                if (!connected) {
                    337:                                        macnum = 0;
1.30      deraadt   338:                                        fputs("Retrying...\n", ttyout);
1.18      millert   339:                                        sleep(retry_connect);
                    340:                                }
                    341:                        } while (!connected);
1.35      mickey    342:                        retry_connect = 0; /* connected, stop hiding msgs */
1.3       deraadt   343:                }
1.1       deraadt   344:        }
1.28      millert   345: #ifndef SMALL
                    346:        controlediting();
                    347: #endif /* !SMALL */
1.1       deraadt   348:        top = setjmp(toplevel) == 0;
                    349:        if (top) {
1.20      millert   350:                (void)signal(SIGINT, (sig_t)intr);
                    351:                (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       deraadt   352:        }
                    353:        for (;;) {
                    354:                cmdscanner(top);
                    355:                top = 1;
                    356:        }
                    357: }
                    358:
                    359: void
1.58      deraadt   360: intr(void)
1.1       deraadt   361: {
                    362:
1.17      millert   363:        alarmtimer(0);
1.1       deraadt   364:        longjmp(toplevel, 1);
                    365: }
                    366:
                    367: void
1.58      deraadt   368: lostpeer(void)
1.1       deraadt   369: {
1.39      deraadt   370:        int save_errno = errno;
1.1       deraadt   371:
1.17      millert   372:        alarmtimer(0);
1.1       deraadt   373:        if (connected) {
                    374:                if (cout != NULL) {
1.66      moritz    375:                        (void)shutdown(fileno(cout), SHUT_RDWR);
1.18      millert   376:                        (void)fclose(cout);
1.1       deraadt   377:                        cout = NULL;
                    378:                }
                    379:                if (data >= 0) {
1.66      moritz    380:                        (void)shutdown(data, SHUT_RDWR);
1.18      millert   381:                        (void)close(data);
1.1       deraadt   382:                        data = -1;
                    383:                }
                    384:                connected = 0;
                    385:        }
                    386:        pswitch(1);
                    387:        if (connected) {
                    388:                if (cout != NULL) {
1.66      moritz    389:                        (void)shutdown(fileno(cout), SHUT_RDWR);
1.18      millert   390:                        (void)fclose(cout);
1.1       deraadt   391:                        cout = NULL;
                    392:                }
                    393:                connected = 0;
                    394:        }
                    395:        proxflag = 0;
                    396:        pswitch(0);
1.39      deraadt   397:        errno = save_errno;
1.1       deraadt   398: }
                    399:
                    400: /*
1.17      millert   401:  * Generate a prompt
                    402:  */
1.1       deraadt   403: char *
1.58      deraadt   404: prompt(void)
1.1       deraadt   405: {
1.17      millert   406:        return ("ftp> ");
1.1       deraadt   407: }
                    408:
                    409: /*
                    410:  * Command parser.
                    411:  */
                    412: void
1.58      deraadt   413: cmdscanner(int top)
1.1       deraadt   414: {
                    415:        struct cmd *c;
1.17      millert   416:        int num;
1.55      otto      417: #ifndef SMALL
                    418:        HistEvent hev;
1.69      martynas  419: #endif /* !SMALL */
1.1       deraadt   420:
1.17      millert   421:        if (!top
1.25      millert   422: #ifndef SMALL
1.17      millert   423:            && !editing
1.25      millert   424: #endif /* !SMALL */
1.17      millert   425:            )
1.30      deraadt   426:                (void)putc('\n', ttyout);
1.1       deraadt   427:        for (;;) {
1.25      millert   428: #ifndef SMALL
1.17      millert   429:                if (!editing) {
1.25      millert   430: #endif /* !SMALL */
1.17      millert   431:                        if (fromatty) {
1.30      deraadt   432:                                fputs(prompt(), ttyout);
                    433:                                (void)fflush(ttyout);
1.17      millert   434:                        }
                    435:                        if (fgets(line, sizeof(line), stdin) == NULL)
                    436:                                quit(0, 0);
                    437:                        num = strlen(line);
                    438:                        if (num == 0)
                    439:                                break;
                    440:                        if (line[--num] == '\n') {
                    441:                                if (num == 0)
                    442:                                        break;
                    443:                                line[num] = '\0';
                    444:                        } else if (num == sizeof(line) - 2) {
1.30      deraadt   445:                                fputs("sorry, input line too long.\n", ttyout);
1.17      millert   446:                                while ((num = getchar()) != '\n' && num != EOF)
                    447:                                        /* void */;
                    448:                                break;
                    449:                        } /* else it was a line without a newline */
1.25      millert   450: #ifndef SMALL
1.17      millert   451:                } else {
                    452:                        const char *buf;
                    453:                        cursor_pos = NULL;
                    454:
                    455:                        if ((buf = el_gets(el, &num)) == NULL || num == 0)
                    456:                                quit(0, 0);
1.46      fgsch     457:                        if (buf[--num] == '\n') {
1.17      millert   458:                                if (num == 0)
                    459:                                        break;
1.46      fgsch     460:                        }
                    461:                        if (num >= sizeof(line)) {
1.30      deraadt   462:                                fputs("sorry, input line too long.\n", ttyout);
1.1       deraadt   463:                                break;
1.17      millert   464:                        }
1.34      millert   465:                        memcpy(line, buf, (size_t)num);
1.17      millert   466:                        line[num] = '\0';
1.55      otto      467:                        history(hist, &hev, H_ENTER, buf);
1.17      millert   468:                }
1.25      millert   469: #endif /* !SMALL */
1.17      millert   470:
1.1       deraadt   471:                makeargv();
1.17      millert   472:                if (margc == 0)
1.1       deraadt   473:                        continue;
                    474:                c = getcmd(margv[0]);
                    475:                if (c == (struct cmd *)-1) {
1.30      deraadt   476:                        fputs("?Ambiguous command.\n", ttyout);
1.1       deraadt   477:                        continue;
                    478:                }
                    479:                if (c == 0) {
1.25      millert   480: #ifndef SMALL
1.24      millert   481:                        /*
                    482:                         * Give editline(3) a shot at unknown commands.
                    483:                         * XXX - bogus commands with a colon in
                    484:                         *       them will not elicit an error.
                    485:                         */
1.45      markus    486:                        if (editing &&
1.55      otto      487:                            el_parse(el, margc, (const char **)margv) != 0)
1.25      millert   488: #endif /* !SMALL */
1.30      deraadt   489:                                fputs("?Invalid command.\n", ttyout);
1.1       deraadt   490:                        continue;
                    491:                }
                    492:                if (c->c_conn && !connected) {
1.30      deraadt   493:                        fputs("Not connected.\n", ttyout);
1.1       deraadt   494:                        continue;
                    495:                }
1.17      millert   496:                confirmrest = 0;
1.1       deraadt   497:                (*c->c_handler)(margc, margv);
                    498:                if (bell && c->c_bell)
1.30      deraadt   499:                        (void)putc('\007', ttyout);
1.1       deraadt   500:                if (c->c_handler != help)
                    501:                        break;
                    502:        }
1.20      millert   503:        (void)signal(SIGINT, (sig_t)intr);
                    504:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       deraadt   505: }
                    506:
                    507: struct cmd *
1.58      deraadt   508: getcmd(const char *name)
1.1       deraadt   509: {
1.17      millert   510:        const char *p, *q;
1.1       deraadt   511:        struct cmd *c, *found;
                    512:        int nmatches, longest;
1.2       deraadt   513:
                    514:        if (name == NULL)
                    515:                return (0);
1.1       deraadt   516:
                    517:        longest = 0;
                    518:        nmatches = 0;
                    519:        found = 0;
1.17      millert   520:        for (c = cmdtab; (p = c->c_name) != NULL; c++) {
1.1       deraadt   521:                for (q = name; *q == *p++; q++)
                    522:                        if (*q == 0)            /* exact match? */
                    523:                                return (c);
                    524:                if (!*q) {                      /* the name was a prefix */
                    525:                        if (q - name > longest) {
                    526:                                longest = q - name;
                    527:                                nmatches = 1;
                    528:                                found = c;
                    529:                        } else if (q - name == longest)
                    530:                                nmatches++;
                    531:                }
                    532:        }
                    533:        if (nmatches > 1)
                    534:                return ((struct cmd *)-1);
                    535:        return (found);
                    536: }
                    537:
                    538: /*
                    539:  * Slice a string up into argc/argv.
                    540:  */
                    541:
                    542: int slrflag;
                    543:
                    544: void
1.58      deraadt   545: makeargv(void)
1.1       deraadt   546: {
1.17      millert   547:        char *argp;
1.1       deraadt   548:
                    549:        stringbase = line;              /* scan from first of buffer */
                    550:        argbase = argbuf;               /* store from first of buffer */
                    551:        slrflag = 0;
1.17      millert   552:        marg_sl->sl_cur = 0;            /* reset to start of marg_sl */
1.1       deraadt   553:        for (margc = 0; ; margc++) {
1.17      millert   554:                argp = slurpstring();
                    555:                sl_add(marg_sl, argp);
                    556:                if (argp == NULL)
1.1       deraadt   557:                        break;
                    558:        }
1.25      millert   559: #ifndef SMALL
1.17      millert   560:        if (cursor_pos == line) {
                    561:                cursor_argc = 0;
                    562:                cursor_argo = 0;
                    563:        } else if (cursor_pos != NULL) {
                    564:                cursor_argc = margc;
                    565:                cursor_argo = strlen(margv[margc-1]);
                    566:        }
1.25      millert   567: #endif /* !SMALL */
1.17      millert   568: }
1.1       deraadt   569:
1.25      millert   570: #ifdef SMALL
1.17      millert   571: #define INC_CHKCURSOR(x)       (x)++
1.69      martynas  572: #else  /* SMALL */
1.17      millert   573: #define INC_CHKCURSOR(x)       { (x)++ ; \
                    574:                                if (x == cursor_pos) { \
                    575:                                        cursor_argc = margc; \
                    576:                                        cursor_argo = ap-argbase; \
                    577:                                        cursor_pos = NULL; \
                    578:                                } }
                    579:
1.69      martynas  580: #endif /* SMALL */
1.1       deraadt   581:
                    582: /*
                    583:  * Parse string into argbuf;
                    584:  * implemented with FSM to
                    585:  * handle quoting and strings
                    586:  */
                    587: char *
1.58      deraadt   588: slurpstring(void)
1.1       deraadt   589: {
                    590:        int got_one = 0;
                    591:        char *sb = stringbase;
                    592:        char *ap = argbase;
                    593:        char *tmp = argbase;            /* will return this if token found */
                    594:
                    595:        if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
                    596:                switch (slrflag) {      /* and $ as token for macro invoke */
                    597:                        case 0:
                    598:                                slrflag++;
1.17      millert   599:                                INC_CHKCURSOR(stringbase);
1.1       deraadt   600:                                return ((*sb == '!') ? "!" : "$");
                    601:                                /* NOTREACHED */
                    602:                        case 1:
                    603:                                slrflag++;
                    604:                                altarg = stringbase;
                    605:                                break;
                    606:                        default:
                    607:                                break;
                    608:                }
                    609:        }
                    610:
                    611: S0:
                    612:        switch (*sb) {
                    613:
                    614:        case '\0':
                    615:                goto OUT;
                    616:
                    617:        case ' ':
                    618:        case '\t':
1.17      millert   619:                INC_CHKCURSOR(sb);
                    620:                goto S0;
1.1       deraadt   621:
                    622:        default:
                    623:                switch (slrflag) {
                    624:                        case 0:
                    625:                                slrflag++;
                    626:                                break;
                    627:                        case 1:
                    628:                                slrflag++;
                    629:                                altarg = sb;
                    630:                                break;
                    631:                        default:
                    632:                                break;
                    633:                }
                    634:                goto S1;
                    635:        }
                    636:
                    637: S1:
                    638:        switch (*sb) {
                    639:
                    640:        case ' ':
                    641:        case '\t':
                    642:        case '\0':
                    643:                goto OUT;       /* end of token */
                    644:
                    645:        case '\\':
1.17      millert   646:                INC_CHKCURSOR(sb);
                    647:                goto S2;        /* slurp next character */
1.1       deraadt   648:
                    649:        case '"':
1.17      millert   650:                INC_CHKCURSOR(sb);
                    651:                goto S3;        /* slurp quoted string */
1.1       deraadt   652:
                    653:        default:
1.17      millert   654:                *ap = *sb;      /* add character to token */
                    655:                ap++;
                    656:                INC_CHKCURSOR(sb);
1.1       deraadt   657:                got_one = 1;
                    658:                goto S1;
                    659:        }
                    660:
                    661: S2:
                    662:        switch (*sb) {
                    663:
                    664:        case '\0':
                    665:                goto OUT;
                    666:
                    667:        default:
1.17      millert   668:                *ap = *sb;
                    669:                ap++;
                    670:                INC_CHKCURSOR(sb);
1.1       deraadt   671:                got_one = 1;
                    672:                goto S1;
                    673:        }
                    674:
                    675: S3:
                    676:        switch (*sb) {
                    677:
                    678:        case '\0':
                    679:                goto OUT;
                    680:
                    681:        case '"':
1.17      millert   682:                INC_CHKCURSOR(sb);
                    683:                goto S1;
1.1       deraadt   684:
                    685:        default:
1.17      millert   686:                *ap = *sb;
                    687:                ap++;
                    688:                INC_CHKCURSOR(sb);
1.1       deraadt   689:                got_one = 1;
                    690:                goto S3;
                    691:        }
                    692:
                    693: OUT:
                    694:        if (got_one)
                    695:                *ap++ = '\0';
                    696:        argbase = ap;                   /* update storage pointer */
                    697:        stringbase = sb;                /* update scan pointer */
                    698:        if (got_one) {
                    699:                return (tmp);
                    700:        }
                    701:        switch (slrflag) {
                    702:                case 0:
                    703:                        slrflag++;
                    704:                        break;
                    705:                case 1:
                    706:                        slrflag++;
                    707:                        altarg = (char *) 0;
                    708:                        break;
                    709:                default:
                    710:                        break;
                    711:        }
                    712:        return ((char *)0);
                    713: }
                    714:
                    715: /*
                    716:  * Help command.
                    717:  * Call each command handler with argc == 0 and argv[0] == name.
                    718:  */
                    719: void
1.58      deraadt   720: help(int argc, char *argv[])
1.1       deraadt   721: {
                    722:        struct cmd *c;
                    723:
                    724:        if (argc == 1) {
1.17      millert   725:                StringList *buf;
1.1       deraadt   726:
1.17      millert   727:                buf = sl_init();
1.30      deraadt   728:                fprintf(ttyout, "%sommands may be abbreviated.  Commands are:\n\n",
1.17      millert   729:                    proxy ? "Proxy c" : "C");
                    730:                for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
                    731:                        if (c->c_name && (!proxy || c->c_proxy))
                    732:                                sl_add(buf, c->c_name);
                    733:                list_vertical(buf);
                    734:                sl_free(buf, 0);
1.1       deraadt   735:                return;
                    736:        }
1.17      millert   737:
1.18      millert   738: #define HELPINDENT ((int) sizeof("disconnect"))
1.17      millert   739:
1.1       deraadt   740:        while (--argc > 0) {
                    741:                char *arg;
1.17      millert   742:
1.1       deraadt   743:                arg = *++argv;
                    744:                c = getcmd(arg);
                    745:                if (c == (struct cmd *)-1)
1.30      deraadt   746:                        fprintf(ttyout, "?Ambiguous help command %s\n", arg);
1.1       deraadt   747:                else if (c == (struct cmd *)0)
1.30      deraadt   748:                        fprintf(ttyout, "?Invalid help command %s\n", arg);
1.1       deraadt   749:                else
1.30      deraadt   750:                        fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
1.1       deraadt   751:                                c->c_name, c->c_help);
                    752:        }
1.17      millert   753: }
                    754:
                    755: void
1.58      deraadt   756: usage(void)
1.17      millert   757: {
                    758:        (void)fprintf(stderr,
1.70      martynas  759:            "usage: %s [-46Aa"
                    760: #ifndef SMALL
                    761:            "d"
                    762: #endif /* !SMALL */
1.74      sobrado   763:            "EegimnptVv] [-k seconds] [-P port] [-r seconds] [host [port]]\n"
1.70      martynas  764:            "       %s "
                    765: #ifndef SMALL
                    766:            "[-C] "
                    767: #endif /* !SMALL */
                    768:            "[-o output] "
1.68      jmc       769:            "ftp://[user:password@]host[:port]/file[/]\n"
1.70      martynas  770:            "       %s "
                    771: #ifndef SMALL
                    772:            "[-C] [-c cookie] "
                    773: #endif /* !SMALL */
                    774:            "[-o output] "
1.68      jmc       775:            "http://host[:port]/file\n"
1.61      deraadt   776: #ifndef SMALL
1.68      jmc       777:            "       %s [-C] [-c cookie] [-o output] "
                    778:            "https://host[:port]/file\n"
1.69      martynas  779: #endif /* !SMALL */
1.70      martynas  780:            "       %s "
                    781: #ifndef SMALL
                    782:            "[-C] "
                    783: #endif /* !SMALL */
1.71      sobrado   784:            "[-o output] host:/file[/]\n",
1.61      deraadt   785: #ifndef SMALL
                    786:            __progname, __progname, __progname, __progname, __progname);
1.69      martynas  787: #else /* !SMALL */
1.17      millert   788:            __progname, __progname, __progname, __progname);
1.69      martynas  789: #endif /* !SMALL */
1.17      millert   790:        exit(1);
1.1       deraadt   791: }