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

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

1.77    ! cgd         1: /*     $NetBSD: main.c,v 1.76 2001/01/09 22:25:55 jdolecek Exp $       */
1.59      lukem       2:
                      3: /*-
1.70      lukem       4:  * Copyright (c) 1996-2000 The NetBSD Foundation, Inc.
1.59      lukem       5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Luke Mewburn.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the NetBSD
                     21:  *     Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
1.45      itojun     38:
                     39: /*
1.55      lukem      40:  * Copyright (c) 1985, 1989, 1993, 1994
                     41:  *     The Regents of the University of California.  All rights reserved.
                     42:  *
1.45      itojun     43:  * Redistribution and use in source and binary forms, with or without
                     44:  * modification, are permitted provided that the following conditions
                     45:  * are met:
                     46:  * 1. Redistributions of source code must retain the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer.
                     48:  * 2. Redistributions in binary form must reproduce the above copyright
                     49:  *    notice, this list of conditions and the following disclaimer in the
                     50:  *    documentation and/or other materials provided with the distribution.
1.55      lukem      51:  * 3. All advertising materials mentioning features or use of this software
                     52:  *    must display the following acknowledgement:
                     53:  *     This product includes software developed by the University of
                     54:  *     California, Berkeley and its contributors.
                     55:  * 4. Neither the name of the University nor the names of its contributors
1.45      itojun     56:  *    may be used to endorse or promote products derived from this software
                     57:  *    without specific prior written permission.
1.55      lukem      58:  *
                     59:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1.45      itojun     60:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     61:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.55      lukem      62:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1.45      itojun     63:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     64:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     65:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     66:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     67:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     68:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     69:  * SUCH DAMAGE.
                     70:  */
1.12      lukem      71:
1.1       cgd        72: /*
1.55      lukem      73:  * Copyright (C) 1997 and 1998 WIDE Project.
                     74:  * All rights reserved.
                     75:  *
1.1       cgd        76:  * Redistribution and use in source and binary forms, with or without
                     77:  * modification, are permitted provided that the following conditions
                     78:  * are met:
                     79:  * 1. Redistributions of source code must retain the above copyright
                     80:  *    notice, this list of conditions and the following disclaimer.
                     81:  * 2. Redistributions in binary form must reproduce the above copyright
                     82:  *    notice, this list of conditions and the following disclaimer in the
                     83:  *    documentation and/or other materials provided with the distribution.
1.55      lukem      84:  * 3. Neither the name of the project nor the names of its contributors
1.1       cgd        85:  *    may be used to endorse or promote products derived from this software
                     86:  *    without specific prior written permission.
1.55      lukem      87:  *
                     88:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1.1       cgd        89:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     90:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.55      lukem      91:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
1.1       cgd        92:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     93:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     94:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     95:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     96:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     97:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     98:  * SUCH DAMAGE.
                     99:  */
                    100:
1.23      lukem     101: #include <sys/cdefs.h>
1.1       cgd       102: #ifndef lint
1.23      lukem     103: __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
                    104:        The Regents of the University of California.  All rights reserved.\n");
1.1       cgd       105: #endif /* not lint */
                    106:
                    107: #ifndef lint
1.8       tls       108: #if 0
                    109: static char sccsid[] = "@(#)main.c     8.6 (Berkeley) 10/9/94";
                    110: #else
1.77    ! cgd       111: __RCSID("$NetBSD: main.c,v 1.76 2001/01/09 22:25:55 jdolecek Exp $");
1.8       tls       112: #endif
1.1       cgd       113: #endif /* not lint */
                    114:
                    115: /*
                    116:  * FTP User Program -- Command Interface.
                    117:  */
1.3       cgd       118: #include <sys/types.h>
1.1       cgd       119: #include <sys/socket.h>
                    120:
1.3       cgd       121: #include <err.h>
1.61      lukem     122: #include <errno.h>
1.1       cgd       123: #include <netdb.h>
1.56      lukem     124: #include <paths.h>
1.1       cgd       125: #include <pwd.h>
1.3       cgd       126: #include <stdio.h>
1.22      lukem     127: #include <stdlib.h>
1.9       cgd       128: #include <string.h>
1.3       cgd       129: #include <unistd.h>
1.76      jdolecek  130: #include <locale.h>
1.1       cgd       131:
1.57      lukem     132: #define        GLOBAL          /* force GLOBAL decls in ftp_var.h to be declared */
1.3       cgd       133: #include "ftp_var.h"
1.1       cgd       134:
1.65      lukem     135: #define        FTP_PROXY       "ftp_proxy"     /* env var with FTP proxy location */
                    136: #define        HTTP_PROXY      "http_proxy"    /* env var with HTTP proxy location */
                    137: #define        NO_PROXY        "no_proxy"      /* env var with list of non-proxied
1.35      lukem     138:                                         * hosts, comma or space separated */
                    139:
1.70      lukem     140: static void    setupoption(char *, char *, char *);
                    141: int            main(int, char *[]);
1.23      lukem     142:
1.3       cgd       143: int
1.70      lukem     144: main(int argc, char *argv[])
1.1       cgd       145: {
1.64      lukem     146:        int ch, rval;
1.74      lukem     147:        struct passwd *pw;
1.71      lukem     148:        char *cp, *ep, *anonuser, *anonpass, *upload_path;
                    149:        int dumbterm, s, len, isupload;
1.76      jdolecek  150:
                    151:        setlocale(LC_ALL, "");
1.1       cgd       152:
1.45      itojun    153:        ftpport = "ftp";
                    154:        httpport = "http";
                    155:        gateport = NULL;
1.24      lukem     156:        cp = getenv("FTPSERVERPORT");
1.45      itojun    157:        if (cp != NULL)
                    158:                gateport = cp;
                    159:        else
                    160:                gateport = "ftpgate";
1.1       cgd       161:        doglob = 1;
                    162:        interactive = 1;
                    163:        autologin = 1;
1.31      lukem     164:        passivemode = 1;
                    165:        activefallback = 1;
1.13      lukem     166:        preserve = 1;
1.17      lukem     167:        verbose = 0;
                    168:        progress = 0;
1.24      lukem     169:        gatemode = 0;
1.57      lukem     170:        data = -1;
1.39      lukem     171:        outfile = NULL;
                    172:        restartautofetch = 0;
1.43      cgd       173: #ifndef NO_EDITCOMPLETE
1.18      lukem     174:        editing = 0;
1.21      lukem     175:        el = NULL;
                    176:        hist = NULL;
1.18      lukem     177: #endif
1.60      lukem     178:        bytes = 0;
1.12      lukem     179:        mark = HASHBYTES;
1.44      lukem     180:        rate_get = 0;
                    181:        rate_get_incr = DEFAULTINCR;
                    182:        rate_put = 0;
                    183:        rate_put_incr = DEFAULTINCR;
1.49      lukem     184: #ifdef INET6
1.47      itojun    185:        epsv4 = 1;
1.49      lukem     186: #else
                    187:        epsv4 = 0;
                    188: #endif
1.58      lukem     189:        epsv4bad = 0;
1.71      lukem     190:        upload_path = NULL;
                    191:        isupload = 0;
1.73      lukem     192:        reply_callback = NULL;
1.50      lukem     193:
1.74      lukem     194:        netrc[0] = '\0';
                    195:        cp = getenv("NETRC");
                    196:        if (cp != NULL && strlcpy(netrc, cp, sizeof(netrc)) >= sizeof(netrc))
                    197:                errx(1, "$NETRC `%s': %s", cp, strerror(ENAMETOOLONG));
                    198:
1.50      lukem     199:        /*
                    200:         * Get the default socket buffer sizes if we don't already have them.
                    201:         * It doesn't matter which socket we do this to, because on the first
                    202:         * call no socket buffer sizes will have been modified, so we are
                    203:         * guaranteed to get the system defaults.
                    204:         */
                    205:        s = socket(AF_INET, SOCK_STREAM, 0);
                    206:        if (s == -1)
                    207:                err(1, "can't create socket");
1.51      lukem     208:        len = sizeof(rcvbuf_size);
                    209:        if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size, &len)
                    210:            < 0)
                    211:                err(1, "unable to get default rcvbuf size");
1.50      lukem     212:        len = sizeof(sndbuf_size);
                    213:        if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size, &len)
                    214:            < 0)
                    215:                err(1, "unable to get default sndbuf size");
1.51      lukem     216:        (void)close(s);
                    217:                                        /* sanity check returned buffer sizes */
                    218:        if (rcvbuf_size <= 0)
                    219:                rcvbuf_size = 8192;
                    220:        if (sndbuf_size <= 0)
                    221:                sndbuf_size = 8192;
1.44      lukem     222:
1.69      lukem     223:        marg_sl = xsl_init();
1.25      lukem     224:        if ((tmpdir = getenv("TMPDIR")) == NULL)
                    225:                tmpdir = _PATH_TMP;
1.3       cgd       226:
1.31      lukem     227:        /* Set default operation mode based on FTPMODE environment variable */
                    228:        if ((cp = getenv("FTPMODE")) != NULL) {
1.65      lukem     229:                if (strcasecmp(cp, "passive") == 0) {
1.31      lukem     230:                        passivemode = 1;
                    231:                        activefallback = 0;
1.65      lukem     232:                } else if (strcasecmp(cp, "active") == 0) {
1.31      lukem     233:                        passivemode = 0;
                    234:                        activefallback = 0;
1.65      lukem     235:                } else if (strcasecmp(cp, "gate") == 0) {
1.31      lukem     236:                        gatemode = 1;
1.65      lukem     237:                } else if (strcasecmp(cp, "auto") == 0) {
1.31      lukem     238:                        passivemode = 1;
                    239:                        activefallback = 1;
                    240:                } else
                    241:                        warnx("unknown $FTPMODE '%s'; using defaults", cp);
                    242:        }
                    243:
1.77    ! cgd       244:        if (strcmp(getprogname(), "pftp") == 0) {
1.15      lukem     245:                passivemode = 1;
1.31      lukem     246:                activefallback = 0;
1.77    ! cgd       247:        } else if (strcmp(getprogname(), "gate-ftp") == 0)
1.24      lukem     248:                gatemode = 1;
                    249:
                    250:        gateserver = getenv("FTPSERVER");
                    251:        if (gateserver == NULL || *gateserver == '\0')
                    252:                gateserver = GATE_SERVER;
                    253:        if (gatemode) {
                    254:                if (*gateserver == '\0') {
                    255:                        warnx(
1.42      lukem     256: "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
1.24      lukem     257:                        gatemode = 0;
                    258:                }
                    259:        }
1.15      lukem     260:
1.22      lukem     261:        cp = getenv("TERM");
                    262:        if (cp == NULL || strcmp(cp, "dumb") == 0)
                    263:                dumbterm = 1;
                    264:        else
                    265:                dumbterm = 0;
1.17      lukem     266:        fromatty = isatty(fileno(stdin));
1.37      lukem     267:        ttyout = stdout;
                    268:        if (isatty(fileno(ttyout))) {
1.41      lukem     269:                verbose = 1;            /* verbose if to a tty */
1.37      lukem     270:                if (! dumbterm) {
1.43      cgd       271: #ifndef NO_EDITCOMPLETE
1.41      lukem     272:                        if (fromatty)   /* editing mode on if tty is usable */
                    273:                                editing = 1;
1.43      cgd       274: #endif
                    275: #ifndef NO_PROGRESS
1.37      lukem     276:                        if (foregroundproc())
                    277:                                progress = 1;   /* progress bar on if fg */
1.43      cgd       278: #endif
1.37      lukem     279:                }
1.18      lukem     280:        }
1.17      lukem     281:
1.74      lukem     282:        while ((ch = getopt(argc, argv, "AadefginN:o:pP:r:RtT:u:vV")) != -1) {
1.6       mycroft   283:                switch (ch) {
1.31      lukem     284:                case 'A':
                    285:                        activefallback = 0;
                    286:                        passivemode = 0;
                    287:                        break;
                    288:
1.12      lukem     289:                case 'a':
                    290:                        anonftp = 1;
                    291:                        break;
                    292:
1.3       cgd       293:                case 'd':
                    294:                        options |= SO_DEBUG;
                    295:                        debug++;
                    296:                        break;
1.12      lukem     297:
1.18      lukem     298:                case 'e':
1.43      cgd       299: #ifndef NO_EDITCOMPLETE
1.18      lukem     300:                        editing = 0;
                    301: #endif
                    302:                        break;
                    303:
1.36      lukem     304:                case 'f':
                    305:                        flushcache = 1;
                    306:                        break;
                    307:
1.3       cgd       308:                case 'g':
                    309:                        doglob = 0;
                    310:                        break;
1.1       cgd       311:
1.3       cgd       312:                case 'i':
                    313:                        interactive = 0;
                    314:                        break;
1.1       cgd       315:
1.3       cgd       316:                case 'n':
                    317:                        autologin = 0;
                    318:                        break;
1.1       cgd       319:
1.74      lukem     320:                case 'N':
                    321:                        if (strlcpy(netrc, optarg, sizeof(netrc))
                    322:                            >= sizeof(netrc))
                    323:                                errx(1, "%s: %s", optarg,
                    324:                                    strerror(ENAMETOOLONG));
                    325:                        break;
                    326:
1.31      lukem     327:                case 'o':
                    328:                        outfile = optarg;
                    329:                        if (strcmp(outfile, "-") == 0)
                    330:                                ttyout = stderr;
                    331:                        break;
                    332:
1.12      lukem     333:                case 'p':
                    334:                        passivemode = 1;
1.31      lukem     335:                        activefallback = 0;
1.12      lukem     336:                        break;
                    337:
                    338:                case 'P':
1.45      itojun    339:                        ftpport = optarg;
1.12      lukem     340:                        break;
                    341:
1.31      lukem     342:                case 'r':
                    343:                        retry_connect = strtol(optarg, &ep, 10);
1.39      lukem     344:                        if (retry_connect < 1 || *ep != '\0')
1.31      lukem     345:                                errx(1, "bad retry value: %s", optarg);
                    346:                        break;
                    347:
1.39      lukem     348:                case 'R':
                    349:                        restartautofetch = 1;
                    350:                        break;
                    351:
1.3       cgd       352:                case 't':
1.17      lukem     353:                        trace = 1;
1.3       cgd       354:                        break;
1.1       cgd       355:
1.44      lukem     356:                case 'T':
                    357:                {
                    358:                        int targc;
                    359:                        char *targv[6], *oac;
                    360:
                    361:                                /* look for `dir,max[,incr]' */
                    362:                        targc = 0;
                    363:                        targv[targc++] = "-T";
                    364:                        oac = xstrdup(optarg);
                    365:
                    366:                        while ((cp = strsep(&oac, ",")) != NULL) {
                    367:                                if (*cp == '\0') {
                    368:                                        warnx("bad throttle value: %s", optarg);
                    369:                                        usage();
                    370:                                        /* NOTREACHED */
                    371:                                }
                    372:                                targv[targc++] = cp;
                    373:                                if (targc >= 5)
                    374:                                        break;
                    375:                        }
                    376:                        if (parserate(targc, targv, 1) == -1)
                    377:                                usage();
                    378:                        free(oac);
                    379:                        break;
                    380:                }
                    381:
1.71      lukem     382:                case 'u':
                    383:                {
                    384:                        isupload = 1;
                    385:                        interactive = 0;
                    386:                        upload_path = xstrdup(optarg);
                    387:
                    388:                        break;
                    389:                }
                    390:
1.3       cgd       391:                case 'v':
1.37      lukem     392:                        progress = verbose = 1;
1.17      lukem     393:                        break;
                    394:
                    395:                case 'V':
1.37      lukem     396:                        progress = verbose = 0;
1.3       cgd       397:                        break;
1.1       cgd       398:
1.3       cgd       399:                default:
1.14      lukem     400:                        usage();
1.3       cgd       401:                }
1.1       cgd       402:        }
1.37      lukem     403:                        /* set line buffering on ttyout */
                    404:        setvbuf(ttyout, NULL, _IOLBF, 0);
1.3       cgd       405:        argc -= optind;
                    406:        argv += optind;
                    407:
1.1       cgd       408:        cpend = 0;      /* no pending replies */
                    409:        proxy = 0;      /* proxy not active */
                    410:        crflag = 1;     /* strip c.r. on ascii gets */
                    411:        sendport = -1;  /* not using ports */
1.74      lukem     412:
1.1       cgd       413:        /*
1.74      lukem     414:         * Cache the user name and home directory.
1.1       cgd       415:         */
1.74      lukem     416:        localhome = NULL;
                    417:        localname = NULL;
                    418:        anonuser = "anonymous";
                    419:        cp = getenv("HOME");
                    420:        if (! EMPTYSTRING(cp))
                    421:                localhome = xstrdup(cp);
                    422:        pw = NULL;
1.1       cgd       423:        cp = getlogin();
1.65      lukem     424:        if (cp != NULL)
1.1       cgd       425:                pw = getpwnam(cp);
                    426:        if (pw == NULL)
                    427:                pw = getpwuid(getuid());
1.65      lukem     428:        if (pw != NULL) {
1.74      lukem     429:                if (localhome == NULL && !EMPTYSTRING(pw->pw_dir))
                    430:                        localhome = xstrdup(pw->pw_dir);
                    431:                localname = xstrdup(pw->pw_name);
                    432:                anonuser = localname;
                    433:        }
                    434:        if (netrc[0] == '\0' && localhome != NULL) {
                    435:                if (strlcpy(netrc, localhome, sizeof(netrc)) >= sizeof(netrc) ||
                    436:                    strlcat(netrc, "/.netrc", sizeof(netrc)) >= sizeof(netrc)) {
                    437:                        warnx("%s/.netrc: %s", localhome,
                    438:                            strerror(ENAMETOOLONG));
                    439:                        netrc[0] = '\0';
                    440:                }
1.65      lukem     441:        }
1.74      lukem     442:        if (localhome == NULL)
                    443:                localhome = xstrdup("/");
1.65      lukem     444:
                    445:        /*
                    446:         * Every anonymous FTP server I've encountered will accept the
                    447:         * string "username@", and will append the hostname itself. We
                    448:         * do this by default since many servers are picky about not
                    449:         * having a FQDN in the anonymous password.
                    450:         * - thorpej@netbsd.org
                    451:         */
                    452:        len = strlen(anonuser) + 2;
                    453:        anonpass = xmalloc(len);
                    454:        (void)strlcpy(anonpass, anonuser, len);
                    455:        (void)strlcat(anonpass, "@",      len);
                    456:
1.67      lukem     457:                        /*
                    458:                         * set all the defaults for options defined in
                    459:                         * struct option optiontab[]  declared in cmdtab.c
                    460:                         */
1.65      lukem     461:        setupoption("anonpass",         getenv("FTPANONPASS"),  anonpass);
1.66      lukem     462:        setupoption("ftp_proxy",        getenv(FTP_PROXY),      "");
                    463:        setupoption("http_proxy",       getenv(HTTP_PROXY),     "");
                    464:        setupoption("no_proxy",         getenv(NO_PROXY),       "");
                    465:        setupoption("pager",            getenv("PAGER"),        DEFAULTPAGER);
1.67      lukem     466:        setupoption("prompt",           getenv("FTPPROMPT"),    DEFAULTPROMPT);
                    467:        setupoption("rprompt",          getenv("FTPRPROMPT"),   DEFAULTRPROMPT);
1.66      lukem     468:
                    469:        free(anonpass);
1.12      lukem     470:
1.17      lukem     471:        setttywidth(0);
1.60      lukem     472: #ifdef SIGINFO
                    473:        (void)xsignal(SIGINFO, psummary);
                    474: #endif
                    475:        (void)xsignal(SIGQUIT, psummary);
1.44      lukem     476:        (void)xsignal(SIGUSR1, crankrate);
                    477:        (void)xsignal(SIGUSR2, crankrate);
1.60      lukem     478:        (void)xsignal(SIGWINCH, setttywidth);
1.23      lukem     479:
1.31      lukem     480: #ifdef __GNUC__                        /* to shut up gcc warnings */
1.23      lukem     481:        (void)&argc;
                    482:        (void)&argv;
                    483: #endif
1.17      lukem     484:
1.1       cgd       485:        if (argc > 0) {
1.71      lukem     486:                if (isupload) {
                    487:                        rval = auto_put(argc, argv, upload_path);
                    488:                        exit(rval);
                    489:                } else if (strchr(argv[0], ':') != NULL
                    490:                            && ! isipv6addr(argv[0])) {
1.39      lukem     491:                        rval = auto_fetch(argc, argv);
1.16      lukem     492:                        if (rval >= 0)          /* -1 == connected and cd-ed */
                    493:                                exit(rval);
                    494:                } else {
1.68      lukem     495:                        char *xargv[4], *user, *host;
1.3       cgd       496:
1.61      lukem     497:                        if (sigsetjmp(toplevel, 1))
1.16      lukem     498:                                exit(0);
1.61      lukem     499:                        (void)xsignal(SIGINT, intr);
1.65      lukem     500:                        (void)xsignal(SIGPIPE, lostpeer);
1.68      lukem     501:                        user = NULL;
                    502:                        host = argv[0];
                    503:                        cp = strchr(host, '@');
                    504:                        if (cp) {
                    505:                                *cp = '\0';
                    506:                                user = host;
                    507:                                host = cp + 1;
                    508:                        }
1.77    ! cgd       509:                        /* XXX discards const */
        !           510:                        xargv[0] = (char *)getprogname();
1.68      lukem     511:                        xargv[1] = host;
1.16      lukem     512:                        xargv[2] = argv[1];
1.68      lukem     513:                        xargv[3] = NULL;
1.31      lukem     514:                        do {
1.68      lukem     515:                                int oautologin;
                    516:
                    517:                                oautologin = autologin;
                    518:                                if (user != NULL) {
                    519:                                        anonftp = 0;
                    520:                                        autologin = 0;
                    521:                                }
1.31      lukem     522:                                setpeer(argc+1, xargv);
1.68      lukem     523:                                autologin = oautologin;
                    524:                                if (connected == 1 && user != NULL)
                    525:                                        (void)ftp_login(host, user, NULL);
1.31      lukem     526:                                if (!retry_connect)
                    527:                                        break;
                    528:                                if (!connected) {
                    529:                                        macnum = 0;
                    530:                                        fprintf(ttyout,
                    531:                                            "Retrying in %d seconds...\n",
                    532:                                            retry_connect);
                    533:                                        sleep(retry_connect);
                    534:                                }
                    535:                        } while (!connected);
                    536:                        retry_connect = 0; /* connected, stop hiding msgs */
1.16      lukem     537:                }
1.1       cgd       538:        }
1.71      lukem     539:        if (isupload)
                    540:                usage();
                    541:
1.43      cgd       542: #ifndef NO_EDITCOMPLETE
1.21      lukem     543:        controlediting();
1.43      cgd       544: #endif /* !NO_EDITCOMPLETE */
1.64      lukem     545:
                    546:        (void)sigsetjmp(toplevel, 1);
                    547:        (void)xsignal(SIGINT, intr);
1.65      lukem     548:        (void)xsignal(SIGPIPE, lostpeer);
1.64      lukem     549:        for (;;)
                    550:                cmdscanner();
1.1       cgd       551: }
                    552:
1.3       cgd       553: /*
1.16      lukem     554:  * Generate a prompt
                    555:  */
1.3       cgd       556: char *
1.70      lukem     557: prompt(void)
1.1       cgd       558: {
1.66      lukem     559:        static char     **prompt;
                    560:        static char       buf[MAXPATHLEN];
                    561:
                    562:        if (prompt == NULL) {
                    563:                struct option *o;
                    564:
                    565:                o = getoption("prompt");
                    566:                if (o == NULL)
                    567:                        errx(1, "no such option `prompt'");
                    568:                prompt = &(o->value);
                    569:        }
1.67      lukem     570:        formatbuf(buf, sizeof(buf), *prompt ? *prompt : DEFAULTPROMPT);
                    571:        return (buf);
                    572: }
                    573:
                    574: /*
                    575:  * Generate an rprompt
                    576:  */
                    577: char *
1.70      lukem     578: rprompt(void)
1.67      lukem     579: {
                    580:        static char     **rprompt;
                    581:        static char       buf[MAXPATHLEN];
                    582:
                    583:        if (rprompt == NULL) {
                    584:                struct option *o;
1.66      lukem     585:
1.67      lukem     586:                o = getoption("rprompt");
                    587:                if (o == NULL)
                    588:                        errx(1, "no such option `rprompt'");
                    589:                rprompt = &(o->value);
1.66      lukem     590:        }
1.67      lukem     591:        formatbuf(buf, sizeof(buf), *rprompt ? *rprompt : DEFAULTRPROMPT);
1.66      lukem     592:        return (buf);
1.1       cgd       593: }
1.3       cgd       594:
1.1       cgd       595: /*
                    596:  * Command parser.
                    597:  */
1.3       cgd       598: void
1.70      lukem     599: cmdscanner(void)
1.1       cgd       600: {
1.67      lukem     601:        struct cmd      *c;
                    602:        char            *p;
                    603:        int              num;
1.1       cgd       604:
                    605:        for (;;) {
1.43      cgd       606: #ifndef NO_EDITCOMPLETE
1.16      lukem     607:                if (!editing) {
1.43      cgd       608: #endif /* !NO_EDITCOMPLETE */
1.16      lukem     609:                        if (fromatty) {
1.31      lukem     610:                                fputs(prompt(), ttyout);
1.67      lukem     611:                                p = rprompt();
                    612:                                if (*p)
                    613:                                        fprintf(ttyout, "%s ", p);
1.31      lukem     614:                                (void)fflush(ttyout);
1.16      lukem     615:                        }
1.63      lukem     616:                        if (fgets(line, sizeof(line), stdin) == NULL) {
                    617:                                if (fromatty)
                    618:                                        putc('\n', ttyout);
1.73      lukem     619:                                quit(0, NULL);
1.63      lukem     620:                        }
1.16      lukem     621:                        num = strlen(line);
                    622:                        if (num == 0)
                    623:                                break;
                    624:                        if (line[--num] == '\n') {
                    625:                                if (num == 0)
                    626:                                        break;
                    627:                                line[num] = '\0';
                    628:                        } else if (num == sizeof(line) - 2) {
1.31      lukem     629:                                fputs("sorry, input line too long.\n", ttyout);
1.16      lukem     630:                                while ((num = getchar()) != '\n' && num != EOF)
                    631:                                        /* void */;
                    632:                                break;
                    633:                        } /* else it was a line without a newline */
1.43      cgd       634: #ifndef NO_EDITCOMPLETE
1.17      lukem     635:                } else {
1.16      lukem     636:                        const char *buf;
1.26      christos  637:                        HistEvent ev;
1.16      lukem     638:                        cursor_pos = NULL;
                    639:
1.63      lukem     640:                        if ((buf = el_gets(el, &num)) == NULL || num == 0) {
                    641:                                if (fromatty)
                    642:                                        putc('\n', ttyout);
1.73      lukem     643:                                quit(0, NULL);
1.63      lukem     644:                        }
1.40      lukem     645:                        if (buf[--num] == '\n') {
1.16      lukem     646:                                if (num == 0)
                    647:                                        break;
                    648:                        } else if (num >= sizeof(line)) {
1.31      lukem     649:                                fputs("sorry, input line too long.\n", ttyout);
1.1       cgd       650:                                break;
1.16      lukem     651:                        }
                    652:                        memcpy(line, buf, num);
                    653:                        line[num] = '\0';
1.26      christos  654:                        history(hist, &ev, H_ENTER, buf);
1.16      lukem     655:                }
1.43      cgd       656: #endif /* !NO_EDITCOMPLETE */
1.16      lukem     657:
1.1       cgd       658:                makeargv();
1.16      lukem     659:                if (margc == 0)
1.1       cgd       660:                        continue;
                    661:                c = getcmd(margv[0]);
                    662:                if (c == (struct cmd *)-1) {
1.31      lukem     663:                        fputs("?Ambiguous command.\n", ttyout);
1.1       cgd       664:                        continue;
                    665:                }
1.30      lukem     666:                if (c == NULL) {
1.43      cgd       667: #if !defined(NO_EDITCOMPLETE)
1.30      lukem     668:                        /*
                    669:                         * attempt to el_parse() unknown commands.
                    670:                         * any command containing a ':' would be parsed
                    671:                         * as "[prog:]cmd ...", and will result in a
                    672:                         * false positive if prog != "ftp", so treat
                    673:                         * such commands as invalid.
                    674:                         */
1.38      lukem     675:                        if (strchr(margv[0], ':') != NULL ||
1.30      lukem     676:                            el_parse(el, margc, margv) != 0)
1.43      cgd       677: #endif /* !NO_EDITCOMPLETE */
1.31      lukem     678:                                fputs("?Invalid command.\n", ttyout);
1.1       cgd       679:                        continue;
                    680:                }
                    681:                if (c->c_conn && !connected) {
1.31      lukem     682:                        fputs("Not connected.\n", ttyout);
1.1       cgd       683:                        continue;
                    684:                }
1.13      lukem     685:                confirmrest = 0;
1.75      lukem     686:                margv[0] = c->c_name;
1.1       cgd       687:                (*c->c_handler)(margc, margv);
                    688:                if (bell && c->c_bell)
1.31      lukem     689:                        (void)putc('\007', ttyout);
1.1       cgd       690:                if (c->c_handler != help)
                    691:                        break;
                    692:        }
1.61      lukem     693:        (void)xsignal(SIGINT, intr);
1.65      lukem     694:        (void)xsignal(SIGPIPE, lostpeer);
1.1       cgd       695: }
                    696:
                    697: struct cmd *
1.70      lukem     698: getcmd(const char *name)
1.1       cgd       699: {
1.13      lukem     700:        const char *p, *q;
1.3       cgd       701:        struct cmd *c, *found;
                    702:        int nmatches, longest;
1.11      pk        703:
                    704:        if (name == NULL)
                    705:                return (0);
1.1       cgd       706:
                    707:        longest = 0;
                    708:        nmatches = 0;
                    709:        found = 0;
1.12      lukem     710:        for (c = cmdtab; (p = c->c_name) != NULL; c++) {
1.1       cgd       711:                for (q = name; *q == *p++; q++)
                    712:                        if (*q == 0)            /* exact match? */
                    713:                                return (c);
                    714:                if (!*q) {                      /* the name was a prefix */
                    715:                        if (q - name > longest) {
                    716:                                longest = q - name;
                    717:                                nmatches = 1;
                    718:                                found = c;
                    719:                        } else if (q - name == longest)
                    720:                                nmatches++;
                    721:                }
                    722:        }
                    723:        if (nmatches > 1)
                    724:                return ((struct cmd *)-1);
                    725:        return (found);
                    726: }
                    727:
                    728: /*
                    729:  * Slice a string up into argc/argv.
                    730:  */
                    731:
                    732: int slrflag;
                    733:
1.3       cgd       734: void
1.70      lukem     735: makeargv(void)
1.1       cgd       736: {
1.16      lukem     737:        char *argp;
1.1       cgd       738:
                    739:        stringbase = line;              /* scan from first of buffer */
                    740:        argbase = argbuf;               /* store from first of buffer */
                    741:        slrflag = 0;
1.16      lukem     742:        marg_sl->sl_cur = 0;            /* reset to start of marg_sl */
1.10      pk        743:        for (margc = 0; ; margc++) {
1.16      lukem     744:                argp = slurpstring();
1.69      lukem     745:                xsl_add(marg_sl, argp);
1.16      lukem     746:                if (argp == NULL)
1.10      pk        747:                        break;
                    748:        }
1.43      cgd       749: #ifndef NO_EDITCOMPLETE
1.16      lukem     750:        if (cursor_pos == line) {
                    751:                cursor_argc = 0;
                    752:                cursor_argo = 0;
                    753:        } else if (cursor_pos != NULL) {
                    754:                cursor_argc = margc;
                    755:                cursor_argo = strlen(margv[margc-1]);
                    756:        }
1.43      cgd       757: #endif /* !NO_EDITCOMPLETE */
1.16      lukem     758: }
1.10      pk        759:
1.43      cgd       760: #ifdef NO_EDITCOMPLETE
1.65      lukem     761: #define        INC_CHKCURSOR(x)        (x)++
1.43      cgd       762: #else  /* !NO_EDITCOMPLETE */
1.65      lukem     763: #define        INC_CHKCURSOR(x)        { (x)++ ; \
1.16      lukem     764:                                if (x == cursor_pos) { \
                    765:                                        cursor_argc = margc; \
                    766:                                        cursor_argo = ap-argbase; \
                    767:                                        cursor_pos = NULL; \
                    768:                                } }
1.38      lukem     769:
1.43      cgd       770: #endif /* !NO_EDITCOMPLETE */
1.1       cgd       771:
                    772: /*
                    773:  * Parse string into argbuf;
                    774:  * implemented with FSM to
                    775:  * handle quoting and strings
                    776:  */
                    777: char *
1.70      lukem     778: slurpstring(void)
1.1       cgd       779: {
                    780:        int got_one = 0;
1.3       cgd       781:        char *sb = stringbase;
                    782:        char *ap = argbase;
1.1       cgd       783:        char *tmp = argbase;            /* will return this if token found */
                    784:
                    785:        if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
                    786:                switch (slrflag) {      /* and $ as token for macro invoke */
                    787:                        case 0:
                    788:                                slrflag++;
1.16      lukem     789:                                INC_CHKCURSOR(stringbase);
1.1       cgd       790:                                return ((*sb == '!') ? "!" : "$");
                    791:                                /* NOTREACHED */
                    792:                        case 1:
                    793:                                slrflag++;
                    794:                                altarg = stringbase;
                    795:                                break;
                    796:                        default:
                    797:                                break;
                    798:                }
                    799:        }
                    800:
                    801: S0:
                    802:        switch (*sb) {
                    803:
                    804:        case '\0':
                    805:                goto OUT;
                    806:
                    807:        case ' ':
                    808:        case '\t':
1.16      lukem     809:                INC_CHKCURSOR(sb);
                    810:                goto S0;
1.1       cgd       811:
                    812:        default:
                    813:                switch (slrflag) {
                    814:                        case 0:
                    815:                                slrflag++;
                    816:                                break;
                    817:                        case 1:
                    818:                                slrflag++;
                    819:                                altarg = sb;
                    820:                                break;
                    821:                        default:
                    822:                                break;
                    823:                }
                    824:                goto S1;
                    825:        }
                    826:
                    827: S1:
                    828:        switch (*sb) {
                    829:
                    830:        case ' ':
                    831:        case '\t':
                    832:        case '\0':
                    833:                goto OUT;       /* end of token */
                    834:
                    835:        case '\\':
1.16      lukem     836:                INC_CHKCURSOR(sb);
                    837:                goto S2;        /* slurp next character */
1.1       cgd       838:
                    839:        case '"':
1.16      lukem     840:                INC_CHKCURSOR(sb);
                    841:                goto S3;        /* slurp quoted string */
1.1       cgd       842:
                    843:        default:
1.16      lukem     844:                *ap = *sb;      /* add character to token */
                    845:                ap++;
                    846:                INC_CHKCURSOR(sb);
1.1       cgd       847:                got_one = 1;
                    848:                goto S1;
                    849:        }
                    850:
                    851: S2:
                    852:        switch (*sb) {
                    853:
                    854:        case '\0':
                    855:                goto OUT;
                    856:
                    857:        default:
1.16      lukem     858:                *ap = *sb;
                    859:                ap++;
                    860:                INC_CHKCURSOR(sb);
1.1       cgd       861:                got_one = 1;
                    862:                goto S1;
                    863:        }
                    864:
                    865: S3:
                    866:        switch (*sb) {
                    867:
                    868:        case '\0':
                    869:                goto OUT;
                    870:
                    871:        case '"':
1.16      lukem     872:                INC_CHKCURSOR(sb);
                    873:                goto S1;
1.1       cgd       874:
                    875:        default:
1.16      lukem     876:                *ap = *sb;
                    877:                ap++;
                    878:                INC_CHKCURSOR(sb);
1.1       cgd       879:                got_one = 1;
                    880:                goto S3;
                    881:        }
                    882:
                    883: OUT:
                    884:        if (got_one)
                    885:                *ap++ = '\0';
                    886:        argbase = ap;                   /* update storage pointer */
                    887:        stringbase = sb;                /* update scan pointer */
                    888:        if (got_one) {
1.3       cgd       889:                return (tmp);
1.1       cgd       890:        }
                    891:        switch (slrflag) {
                    892:                case 0:
                    893:                        slrflag++;
                    894:                        break;
                    895:                case 1:
                    896:                        slrflag++;
1.28      lukem     897:                        altarg = NULL;
1.1       cgd       898:                        break;
                    899:                default:
                    900:                        break;
                    901:        }
1.28      lukem     902:        return (NULL);
1.1       cgd       903: }
                    904:
                    905: /*
1.65      lukem     906:  * Help/usage command.
1.1       cgd       907:  * Call each command handler with argc == 0 and argv[0] == name.
                    908:  */
1.3       cgd       909: void
1.70      lukem     910: help(int argc, char *argv[])
1.1       cgd       911: {
1.3       cgd       912:        struct cmd *c;
1.67      lukem     913:        char *nargv[1], *p, *cmd;
1.65      lukem     914:        int isusage;
1.1       cgd       915:
1.67      lukem     916:        cmd = argv[0];
                    917:        isusage = (strcmp(cmd, "usage") == 0);
1.65      lukem     918:        if (argc == 0 || (isusage && argc == 1)) {
1.67      lukem     919:                fprintf(ttyout, "usage: %s [command [...]]\n", cmd);
1.65      lukem     920:                return;
                    921:        }
1.1       cgd       922:        if (argc == 1) {
1.16      lukem     923:                StringList *buf;
1.1       cgd       924:
1.69      lukem     925:                buf = xsl_init();
1.31      lukem     926:                fprintf(ttyout,
                    927:                    "%sommands may be abbreviated.  Commands are:\n\n",
1.16      lukem     928:                    proxy ? "Proxy c" : "C");
1.65      lukem     929:                for (c = cmdtab; (p = c->c_name) != NULL; c++)
                    930:                        if (!proxy || c->c_proxy)
1.69      lukem     931:                                xsl_add(buf, p);
1.16      lukem     932:                list_vertical(buf);
                    933:                sl_free(buf, 0);
1.1       cgd       934:                return;
                    935:        }
1.16      lukem     936:
1.65      lukem     937: #define        HELPINDENT ((int) sizeof("disconnect"))
1.16      lukem     938:
1.1       cgd       939:        while (--argc > 0) {
1.3       cgd       940:                char *arg;
1.16      lukem     941:
1.1       cgd       942:                arg = *++argv;
                    943:                c = getcmd(arg);
                    944:                if (c == (struct cmd *)-1)
1.65      lukem     945:                        fprintf(ttyout, "?Ambiguous %s command `%s'\n",
1.67      lukem     946:                            cmd, arg);
1.28      lukem     947:                else if (c == NULL)
1.65      lukem     948:                        fprintf(ttyout, "?Invalid %s command `%s'\n",
1.67      lukem     949:                            cmd, arg);
1.65      lukem     950:                else {
                    951:                        if (isusage) {
1.75      lukem     952:                                nargv[0] = c->c_name;
1.65      lukem     953:                                (*c->c_handler)(0, nargv);
                    954:                        } else
                    955:                                fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
                    956:                                    c->c_name, c->c_help);
                    957:                }
1.1       cgd       958:        }
1.65      lukem     959: }
                    960:
                    961: struct option *
1.70      lukem     962: getoption(const char *name)
1.65      lukem     963: {
                    964:        const char *p;
                    965:        struct option *c;
                    966:
                    967:        if (name == NULL)
                    968:                return (NULL);
                    969:        for (c = optiontab; (p = c->name) != NULL; c++) {
                    970:                if (strcasecmp(p, name) == 0)
                    971:                        return (c);
                    972:        }
                    973:        return (NULL);
                    974: }
                    975:
                    976: char *
1.70      lukem     977: getoptionvalue(const char *name)
1.65      lukem     978: {
                    979:        struct option *c;
                    980:
                    981:        if (name == NULL)
                    982:                errx(1, "getoptionvalue() invoked with NULL name");
1.66      lukem     983:        c = getoption(name);
                    984:        if (c != NULL)
                    985:                return (c->value);
1.65      lukem     986:        errx(1, "getoptionvalue() invoked with unknown option `%s'", name);
1.72      lukem     987:        /* NOTREACHED */
1.65      lukem     988: }
                    989:
                    990: static void
1.70      lukem     991: setupoption(char *name, char *value, char *defaultvalue)
1.65      lukem     992: {
                    993:        char *nargv[3];
                    994:        int overbose;
                    995:
                    996:        nargv[0] = "setupoption()";
                    997:        nargv[1] = name;
                    998:        nargv[2] = (value ? value : defaultvalue);
                    999:        overbose = verbose;
                   1000:        verbose = 0;
                   1001:        setoption(3, nargv);
                   1002:        verbose = overbose;
1.12      lukem    1003: }
                   1004:
1.14      lukem    1005: void
1.70      lukem    1006: usage(void)
1.14      lukem    1007: {
1.77    ! cgd      1008:        const char *progname = getprogname();
        !          1009:
1.14      lukem    1010:        (void)fprintf(stderr,
1.74      lukem    1011: "usage: %s [-AadefginpRtvV] [-N netrc] [-o outfile] [-P port] [-r retry]\n"
1.71      lukem    1012: "           [-T dir,max[,inc][[user@]host [port]]] [host:path[/]]\n"
                   1013: "           [file:///file] [ftp://[user[:pass]@]host[:port]/path[/]]\n"
                   1014: "           [http://[user[:pass]@]host[:port]/path] [...]\n"
1.77    ! cgd      1015: "       %s -u url file [...]\n", progname, progname);
1.14      lukem    1016:        exit(1);
1.1       cgd      1017: }

CVSweb <webmaster@jp.NetBSD.org>