1*3a184c67SAntonio Huete Jimenez /* $NetBSD: main.c,v 1.22 2021/04/25 07:50:37 lukem Exp $ */
2*3a184c67SAntonio Huete Jimenez /* from NetBSD: main.c,v 1.127 2020/07/18 03:00:37 lukem Exp */
36cdfca03SJohn Marino
46cdfca03SJohn Marino /*-
56cdfca03SJohn Marino * Copyright (c) 1996-2015 The NetBSD Foundation, Inc.
66cdfca03SJohn Marino * All rights reserved.
76cdfca03SJohn Marino *
86cdfca03SJohn Marino * This code is derived from software contributed to The NetBSD Foundation
96cdfca03SJohn Marino * by Luke Mewburn.
106cdfca03SJohn Marino *
116cdfca03SJohn Marino * Redistribution and use in source and binary forms, with or without
126cdfca03SJohn Marino * modification, are permitted provided that the following conditions
136cdfca03SJohn Marino * are met:
146cdfca03SJohn Marino * 1. Redistributions of source code must retain the above copyright
156cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer.
166cdfca03SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
176cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer in the
186cdfca03SJohn Marino * documentation and/or other materials provided with the distribution.
196cdfca03SJohn Marino *
206cdfca03SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
216cdfca03SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
226cdfca03SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
236cdfca03SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
246cdfca03SJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256cdfca03SJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
266cdfca03SJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
276cdfca03SJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286cdfca03SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
296cdfca03SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
306cdfca03SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
316cdfca03SJohn Marino */
326cdfca03SJohn Marino
336cdfca03SJohn Marino /*
346cdfca03SJohn Marino * Copyright (c) 1985, 1989, 1993, 1994
356cdfca03SJohn Marino * The Regents of the University of California. All rights reserved.
366cdfca03SJohn Marino *
376cdfca03SJohn Marino * Redistribution and use in source and binary forms, with or without
386cdfca03SJohn Marino * modification, are permitted provided that the following conditions
396cdfca03SJohn Marino * are met:
406cdfca03SJohn Marino * 1. Redistributions of source code must retain the above copyright
416cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer.
426cdfca03SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
436cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer in the
446cdfca03SJohn Marino * documentation and/or other materials provided with the distribution.
456cdfca03SJohn Marino * 3. Neither the name of the University nor the names of its contributors
466cdfca03SJohn Marino * may be used to endorse or promote products derived from this software
476cdfca03SJohn Marino * without specific prior written permission.
486cdfca03SJohn Marino *
496cdfca03SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
506cdfca03SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
516cdfca03SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
526cdfca03SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
536cdfca03SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
546cdfca03SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
556cdfca03SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
566cdfca03SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
576cdfca03SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
586cdfca03SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
596cdfca03SJohn Marino * SUCH DAMAGE.
606cdfca03SJohn Marino */
616cdfca03SJohn Marino
626cdfca03SJohn Marino /*
636cdfca03SJohn Marino * Copyright (C) 1997 and 1998 WIDE Project.
646cdfca03SJohn Marino * All rights reserved.
656cdfca03SJohn Marino *
666cdfca03SJohn Marino * Redistribution and use in source and binary forms, with or without
676cdfca03SJohn Marino * modification, are permitted provided that the following conditions
686cdfca03SJohn Marino * are met:
696cdfca03SJohn Marino * 1. Redistributions of source code must retain the above copyright
706cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer.
716cdfca03SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
726cdfca03SJohn Marino * notice, this list of conditions and the following disclaimer in the
736cdfca03SJohn Marino * documentation and/or other materials provided with the distribution.
746cdfca03SJohn Marino * 3. Neither the name of the project nor the names of its contributors
756cdfca03SJohn Marino * may be used to endorse or promote products derived from this software
766cdfca03SJohn Marino * without specific prior written permission.
776cdfca03SJohn Marino *
786cdfca03SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
796cdfca03SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
806cdfca03SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
816cdfca03SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
826cdfca03SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
836cdfca03SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
846cdfca03SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
856cdfca03SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
866cdfca03SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
876cdfca03SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
886cdfca03SJohn Marino * SUCH DAMAGE.
896cdfca03SJohn Marino */
906cdfca03SJohn Marino
916cdfca03SJohn Marino #include "tnftp.h"
926cdfca03SJohn Marino
936cdfca03SJohn Marino #if 0 /* tnftp */
946cdfca03SJohn Marino
956cdfca03SJohn Marino #include <sys/cdefs.h>
966cdfca03SJohn Marino #ifndef lint
976cdfca03SJohn Marino __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\
986cdfca03SJohn Marino The Regents of the University of California. All rights reserved.\
996cdfca03SJohn Marino Copyright 1996-2015 The NetBSD Foundation, Inc. All rights reserved");
1006cdfca03SJohn Marino #endif /* not lint */
1016cdfca03SJohn Marino
1026cdfca03SJohn Marino #ifndef lint
1036cdfca03SJohn Marino #if 0
1046cdfca03SJohn Marino static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
1056cdfca03SJohn Marino #else
106*3a184c67SAntonio Huete Jimenez __RCSID(" NetBSD: main.c,v 1.127 2020/07/18 03:00:37 lukem Exp ");
1076cdfca03SJohn Marino #endif
1086cdfca03SJohn Marino #endif /* not lint */
1096cdfca03SJohn Marino
1106cdfca03SJohn Marino /*
1116cdfca03SJohn Marino * FTP User Program -- Command Interface.
1126cdfca03SJohn Marino */
1136cdfca03SJohn Marino #include <sys/types.h>
1146cdfca03SJohn Marino #include <sys/socket.h>
1156cdfca03SJohn Marino
1166cdfca03SJohn Marino #include <err.h>
1176cdfca03SJohn Marino #include <errno.h>
1186cdfca03SJohn Marino #include <netdb.h>
1196cdfca03SJohn Marino #include <paths.h>
1206cdfca03SJohn Marino #include <pwd.h>
1216cdfca03SJohn Marino #include <signal.h>
1226cdfca03SJohn Marino #include <stdio.h>
1236cdfca03SJohn Marino #include <stdlib.h>
1246cdfca03SJohn Marino #include <string.h>
1256cdfca03SJohn Marino #include <time.h>
1266cdfca03SJohn Marino #include <unistd.h>
1276cdfca03SJohn Marino #include <locale.h>
1286cdfca03SJohn Marino
1296cdfca03SJohn Marino #endif /* tnftp */
1306cdfca03SJohn Marino
1316cdfca03SJohn Marino #define GLOBAL /* force GLOBAL decls in ftp_var.h to be declared */
1326cdfca03SJohn Marino #include "ftp_var.h"
1336cdfca03SJohn Marino
1346cdfca03SJohn Marino #define FTP_PROXY "ftp_proxy" /* env var with FTP proxy location */
1356cdfca03SJohn Marino #define HTTP_PROXY "http_proxy" /* env var with HTTP proxy location */
1366cdfca03SJohn Marino #define HTTPS_PROXY "https_proxy" /* env var with HTTPS proxy location */
1376cdfca03SJohn Marino #define NO_PROXY "no_proxy" /* env var with list of non-proxied
1386cdfca03SJohn Marino * hosts, comma or space separated */
1396cdfca03SJohn Marino
140*3a184c67SAntonio Huete Jimenez static int usage(void);
141*3a184c67SAntonio Huete Jimenez static int usage_help(void);
1426cdfca03SJohn Marino static void setupoption(const char *, const char *, const char *);
1436cdfca03SJohn Marino
1446cdfca03SJohn Marino int
main(int volatile argc,char ** volatile argv)1456cdfca03SJohn Marino main(int volatile argc, char **volatile argv)
1466cdfca03SJohn Marino {
1476cdfca03SJohn Marino int ch, rval;
1486cdfca03SJohn Marino struct passwd *pw;
1496cdfca03SJohn Marino char *cp, *ep, *anonpass, *upload_path, *src_addr;
1506cdfca03SJohn Marino const char *anonuser;
1516cdfca03SJohn Marino int dumbterm, isupload;
1526cdfca03SJohn Marino size_t len;
1536cdfca03SJohn Marino
1546cdfca03SJohn Marino tzset();
1556cdfca03SJohn Marino #if defined(HAVE_SETLOCALE)
1566cdfca03SJohn Marino setlocale(LC_ALL, "");
1576cdfca03SJohn Marino #endif
1586cdfca03SJohn Marino setprogname(argv[0]);
1596cdfca03SJohn Marino
1606cdfca03SJohn Marino sigint_raised = 0;
1616cdfca03SJohn Marino
1626cdfca03SJohn Marino ftpport = "ftp";
1636cdfca03SJohn Marino httpport = "http";
1646cdfca03SJohn Marino #ifdef WITH_SSL
1656cdfca03SJohn Marino httpsport = "https";
1666cdfca03SJohn Marino #endif
1676cdfca03SJohn Marino gateport = NULL;
1686cdfca03SJohn Marino cp = getenv("FTPSERVERPORT");
1696cdfca03SJohn Marino if (cp != NULL)
1706cdfca03SJohn Marino gateport = cp;
1716cdfca03SJohn Marino else
1726cdfca03SJohn Marino gateport = "ftpgate";
1736cdfca03SJohn Marino doglob = 1;
1746cdfca03SJohn Marino interactive = 1;
1756cdfca03SJohn Marino autologin = 1;
1766cdfca03SJohn Marino passivemode = 1;
1776cdfca03SJohn Marino activefallback = 1;
1786cdfca03SJohn Marino preserve = 1;
1796cdfca03SJohn Marino verbose = 0;
1806cdfca03SJohn Marino progress = 0;
1816cdfca03SJohn Marino gatemode = 0;
1826cdfca03SJohn Marino data = -1;
1836cdfca03SJohn Marino outfile = NULL;
1846cdfca03SJohn Marino restartautofetch = 0;
1856cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
1866cdfca03SJohn Marino editing = 0;
1876cdfca03SJohn Marino el = NULL;
1886cdfca03SJohn Marino hist = NULL;
1896cdfca03SJohn Marino #endif
1906cdfca03SJohn Marino bytes = 0;
1916cdfca03SJohn Marino mark = HASHBYTES;
1926cdfca03SJohn Marino rate_get = 0;
1936cdfca03SJohn Marino rate_get_incr = DEFAULTINCR;
1946cdfca03SJohn Marino rate_put = 0;
1956cdfca03SJohn Marino rate_put_incr = DEFAULTINCR;
1966cdfca03SJohn Marino #ifdef INET6
1976cdfca03SJohn Marino epsv4 = 1;
1986cdfca03SJohn Marino epsv6 = 1;
1996cdfca03SJohn Marino #else
2006cdfca03SJohn Marino epsv4 = 0;
2016cdfca03SJohn Marino epsv6 = 0;
2026cdfca03SJohn Marino #endif
2036cdfca03SJohn Marino epsv4bad = 0;
2046cdfca03SJohn Marino epsv6bad = 0;
2056cdfca03SJohn Marino src_addr = NULL;
2066cdfca03SJohn Marino upload_path = NULL;
2076cdfca03SJohn Marino isupload = 0;
2086cdfca03SJohn Marino reply_callback = NULL;
2096cdfca03SJohn Marino #ifdef INET6
2106cdfca03SJohn Marino family = AF_UNSPEC;
2116cdfca03SJohn Marino #else
2126cdfca03SJohn Marino family = AF_INET; /* force AF_INET if no INET6 support */
2136cdfca03SJohn Marino #endif
2146cdfca03SJohn Marino
2156cdfca03SJohn Marino netrc[0] = '\0';
2166cdfca03SJohn Marino cp = getenv("NETRC");
2176cdfca03SJohn Marino if (cp != NULL && strlcpy(netrc, cp, sizeof(netrc)) >= sizeof(netrc))
2186cdfca03SJohn Marino errx(1, "$NETRC `%s': %s", cp, strerror(ENAMETOOLONG));
2196cdfca03SJohn Marino
2206cdfca03SJohn Marino marg_sl = ftp_sl_init();
2216cdfca03SJohn Marino if ((tmpdir = getenv("TMPDIR")) == NULL)
2226cdfca03SJohn Marino tmpdir = _PATH_TMP;
2236cdfca03SJohn Marino
2246cdfca03SJohn Marino /* Set default operation mode based on FTPMODE environment variable */
2256cdfca03SJohn Marino if ((cp = getenv("FTPMODE")) != NULL) {
2266cdfca03SJohn Marino if (strcasecmp(cp, "passive") == 0) {
2276cdfca03SJohn Marino passivemode = 1;
2286cdfca03SJohn Marino activefallback = 0;
2296cdfca03SJohn Marino } else if (strcasecmp(cp, "active") == 0) {
2306cdfca03SJohn Marino passivemode = 0;
2316cdfca03SJohn Marino activefallback = 0;
2326cdfca03SJohn Marino } else if (strcasecmp(cp, "gate") == 0) {
2336cdfca03SJohn Marino gatemode = 1;
2346cdfca03SJohn Marino } else if (strcasecmp(cp, "auto") == 0) {
2356cdfca03SJohn Marino passivemode = 1;
2366cdfca03SJohn Marino activefallback = 1;
2376cdfca03SJohn Marino } else
2386cdfca03SJohn Marino warnx("Unknown $FTPMODE `%s'; using defaults", cp);
2396cdfca03SJohn Marino }
2406cdfca03SJohn Marino
2416cdfca03SJohn Marino if (strcmp(getprogname(), "pftp") == 0) {
2426cdfca03SJohn Marino passivemode = 1;
2436cdfca03SJohn Marino activefallback = 0;
2446cdfca03SJohn Marino } else if (strcmp(getprogname(), "gate-ftp") == 0)
2456cdfca03SJohn Marino gatemode = 1;
2466cdfca03SJohn Marino
2476cdfca03SJohn Marino gateserver = getenv("FTPSERVER");
2486cdfca03SJohn Marino if (gateserver == NULL || *gateserver == '\0')
2496cdfca03SJohn Marino gateserver = GATE_SERVER;
2506cdfca03SJohn Marino if (gatemode) {
2516cdfca03SJohn Marino if (*gateserver == '\0') {
2526cdfca03SJohn Marino warnx(
2536cdfca03SJohn Marino "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
2546cdfca03SJohn Marino gatemode = 0;
2556cdfca03SJohn Marino }
2566cdfca03SJohn Marino }
2576cdfca03SJohn Marino
2586cdfca03SJohn Marino cp = getenv("TERM");
2596cdfca03SJohn Marino if (cp == NULL || strcmp(cp, "dumb") == 0)
2606cdfca03SJohn Marino dumbterm = 1;
2616cdfca03SJohn Marino else
2626cdfca03SJohn Marino dumbterm = 0;
2636cdfca03SJohn Marino fromatty = isatty(fileno(stdin));
2646cdfca03SJohn Marino ttyout = stdout;
2656cdfca03SJohn Marino if (isatty(fileno(ttyout))) {
2666cdfca03SJohn Marino verbose = 1; /* verbose if to a tty */
2676cdfca03SJohn Marino if (! dumbterm) {
2686cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
2696cdfca03SJohn Marino if (fromatty) /* editing mode on if tty is usable */
2706cdfca03SJohn Marino editing = 1;
2716cdfca03SJohn Marino #endif
2726cdfca03SJohn Marino #ifndef NO_PROGRESS
2736cdfca03SJohn Marino if (foregroundproc())
2746cdfca03SJohn Marino progress = 1; /* progress bar on if fg */
2756cdfca03SJohn Marino #endif
2766cdfca03SJohn Marino }
2776cdfca03SJohn Marino }
2786cdfca03SJohn Marino
279*3a184c67SAntonio Huete Jimenez while ((ch = getopt(argc, argv, "?46AadefginN:o:pP:q:r:Rs:tT:u:vVx:")) != -1) {
2806cdfca03SJohn Marino switch (ch) {
2816cdfca03SJohn Marino case '4':
2826cdfca03SJohn Marino family = AF_INET;
2836cdfca03SJohn Marino break;
2846cdfca03SJohn Marino
2856cdfca03SJohn Marino case '6':
2866cdfca03SJohn Marino #ifdef INET6
2876cdfca03SJohn Marino family = AF_INET6;
2886cdfca03SJohn Marino #else
2896cdfca03SJohn Marino warnx("INET6 support is not available; ignoring -6");
2906cdfca03SJohn Marino #endif
2916cdfca03SJohn Marino break;
2926cdfca03SJohn Marino
2936cdfca03SJohn Marino case 'A':
2946cdfca03SJohn Marino activefallback = 0;
2956cdfca03SJohn Marino passivemode = 0;
2966cdfca03SJohn Marino break;
2976cdfca03SJohn Marino
2986cdfca03SJohn Marino case 'a':
2996cdfca03SJohn Marino anonftp = 1;
3006cdfca03SJohn Marino break;
3016cdfca03SJohn Marino
3026cdfca03SJohn Marino case 'd':
3036cdfca03SJohn Marino options |= SO_DEBUG;
3046cdfca03SJohn Marino ftp_debug++;
3056cdfca03SJohn Marino break;
3066cdfca03SJohn Marino
3076cdfca03SJohn Marino case 'e':
3086cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
3096cdfca03SJohn Marino editing = 0;
3106cdfca03SJohn Marino #endif
3116cdfca03SJohn Marino break;
3126cdfca03SJohn Marino
3136cdfca03SJohn Marino case 'f':
3146cdfca03SJohn Marino flushcache = 1;
3156cdfca03SJohn Marino break;
3166cdfca03SJohn Marino
3176cdfca03SJohn Marino case 'g':
3186cdfca03SJohn Marino doglob = 0;
3196cdfca03SJohn Marino break;
3206cdfca03SJohn Marino
3216cdfca03SJohn Marino case 'i':
3226cdfca03SJohn Marino interactive = 0;
3236cdfca03SJohn Marino break;
3246cdfca03SJohn Marino
3256cdfca03SJohn Marino case 'n':
3266cdfca03SJohn Marino autologin = 0;
3276cdfca03SJohn Marino break;
3286cdfca03SJohn Marino
3296cdfca03SJohn Marino case 'N':
3306cdfca03SJohn Marino if (strlcpy(netrc, optarg, sizeof(netrc))
3316cdfca03SJohn Marino >= sizeof(netrc))
3326cdfca03SJohn Marino errx(1, "%s: %s", optarg,
3336cdfca03SJohn Marino strerror(ENAMETOOLONG));
3346cdfca03SJohn Marino break;
3356cdfca03SJohn Marino
3366cdfca03SJohn Marino case 'o':
337*3a184c67SAntonio Huete Jimenez outfile = ftp_strdup(optarg);
3386cdfca03SJohn Marino if (strcmp(outfile, "-") == 0)
3396cdfca03SJohn Marino ttyout = stderr;
3406cdfca03SJohn Marino break;
3416cdfca03SJohn Marino
3426cdfca03SJohn Marino case 'p':
3436cdfca03SJohn Marino passivemode = 1;
3446cdfca03SJohn Marino activefallback = 0;
3456cdfca03SJohn Marino break;
3466cdfca03SJohn Marino
3476cdfca03SJohn Marino case 'P':
3486cdfca03SJohn Marino ftpport = optarg;
3496cdfca03SJohn Marino break;
3506cdfca03SJohn Marino
3516cdfca03SJohn Marino case 'q':
3526cdfca03SJohn Marino quit_time = strtol(optarg, &ep, 10);
3536cdfca03SJohn Marino if (quit_time < 1 || *ep != '\0')
3546cdfca03SJohn Marino errx(1, "Bad quit value: %s", optarg);
3556cdfca03SJohn Marino break;
3566cdfca03SJohn Marino
3576cdfca03SJohn Marino case 'r':
3586cdfca03SJohn Marino retry_connect = strtol(optarg, &ep, 10);
3596cdfca03SJohn Marino if (retry_connect < 1 || *ep != '\0')
3606cdfca03SJohn Marino errx(1, "Bad retry value: %s", optarg);
3616cdfca03SJohn Marino break;
3626cdfca03SJohn Marino
3636cdfca03SJohn Marino case 'R':
3646cdfca03SJohn Marino restartautofetch = 1;
3656cdfca03SJohn Marino break;
3666cdfca03SJohn Marino
3676cdfca03SJohn Marino case 's':
3686cdfca03SJohn Marino src_addr = optarg;
3696cdfca03SJohn Marino break;
3706cdfca03SJohn Marino
3716cdfca03SJohn Marino case 't':
3726cdfca03SJohn Marino trace = 1;
3736cdfca03SJohn Marino break;
3746cdfca03SJohn Marino
3756cdfca03SJohn Marino case 'T':
3766cdfca03SJohn Marino {
3776cdfca03SJohn Marino int targc;
3786cdfca03SJohn Marino char *targv[6], *oac;
3796cdfca03SJohn Marino char cmdbuf[MAX_C_NAME];
3806cdfca03SJohn Marino
3816cdfca03SJohn Marino /* look for `dir,max[,incr]' */
3826cdfca03SJohn Marino targc = 0;
3836cdfca03SJohn Marino (void)strlcpy(cmdbuf, "-T", sizeof(cmdbuf));
3846cdfca03SJohn Marino targv[targc++] = cmdbuf;
3856cdfca03SJohn Marino oac = ftp_strdup(optarg);
3866cdfca03SJohn Marino
3876cdfca03SJohn Marino while ((cp = strsep(&oac, ",")) != NULL) {
3886cdfca03SJohn Marino if (*cp == '\0') {
3896cdfca03SJohn Marino warnx("Bad throttle value `%s'",
3906cdfca03SJohn Marino optarg);
391*3a184c67SAntonio Huete Jimenez return usage();
3926cdfca03SJohn Marino }
3936cdfca03SJohn Marino targv[targc++] = cp;
3946cdfca03SJohn Marino if (targc >= 5)
3956cdfca03SJohn Marino break;
3966cdfca03SJohn Marino }
397*3a184c67SAntonio Huete Jimenez if (parserate(targc, targv, 1) == -1) {
398*3a184c67SAntonio Huete Jimenez return usage();
399*3a184c67SAntonio Huete Jimenez }
4006cdfca03SJohn Marino free(oac);
4016cdfca03SJohn Marino break;
4026cdfca03SJohn Marino }
4036cdfca03SJohn Marino
4046cdfca03SJohn Marino case 'u':
4056cdfca03SJohn Marino {
4066cdfca03SJohn Marino isupload = 1;
4076cdfca03SJohn Marino interactive = 0;
4086cdfca03SJohn Marino upload_path = ftp_strdup(optarg);
4096cdfca03SJohn Marino
4106cdfca03SJohn Marino break;
4116cdfca03SJohn Marino }
4126cdfca03SJohn Marino
4136cdfca03SJohn Marino case 'v':
4146cdfca03SJohn Marino progress = verbose = 1;
4156cdfca03SJohn Marino break;
4166cdfca03SJohn Marino
4176cdfca03SJohn Marino case 'V':
4186cdfca03SJohn Marino progress = verbose = 0;
4196cdfca03SJohn Marino break;
4206cdfca03SJohn Marino
4216cdfca03SJohn Marino case 'x':
4226cdfca03SJohn Marino sndbuf_size = strsuftoi(optarg);
4236cdfca03SJohn Marino if (sndbuf_size < 1)
4246cdfca03SJohn Marino errx(1, "Bad xferbuf value: %s", optarg);
4256cdfca03SJohn Marino rcvbuf_size = sndbuf_size;
4266cdfca03SJohn Marino break;
4276cdfca03SJohn Marino
428*3a184c67SAntonio Huete Jimenez case '?':
429*3a184c67SAntonio Huete Jimenez if (optopt == '?') {
430*3a184c67SAntonio Huete Jimenez return usage_help();
431*3a184c67SAntonio Huete Jimenez }
432*3a184c67SAntonio Huete Jimenez return usage();
433*3a184c67SAntonio Huete Jimenez
4346cdfca03SJohn Marino default:
435*3a184c67SAntonio Huete Jimenez errx(1, "unimplemented option -%c", ch);
4366cdfca03SJohn Marino }
4376cdfca03SJohn Marino }
4386cdfca03SJohn Marino /* set line buffering on ttyout */
4396cdfca03SJohn Marino setvbuf(ttyout, NULL, _IOLBF, 0);
4406cdfca03SJohn Marino argc -= optind;
4416cdfca03SJohn Marino argv += optind;
4426cdfca03SJohn Marino
4436cdfca03SJohn Marino cpend = 0; /* no pending replies */
4446cdfca03SJohn Marino proxy = 0; /* proxy not active */
4456cdfca03SJohn Marino crflag = 1; /* strip c.r. on ascii gets */
4466cdfca03SJohn Marino sendport = -1; /* not using ports */
4476cdfca03SJohn Marino
4486cdfca03SJohn Marino if (src_addr != NULL) {
4496cdfca03SJohn Marino struct addrinfo hints;
4506cdfca03SJohn Marino int error;
4516cdfca03SJohn Marino
4526cdfca03SJohn Marino memset(&hints, 0, sizeof(hints));
4536cdfca03SJohn Marino hints.ai_family = family;
4546cdfca03SJohn Marino hints.ai_socktype = SOCK_STREAM;
4556cdfca03SJohn Marino hints.ai_flags = AI_PASSIVE;
4566cdfca03SJohn Marino error = getaddrinfo(src_addr, NULL, &hints, &bindai);
4576cdfca03SJohn Marino if (error) {
4586cdfca03SJohn Marino errx(1, "Can't lookup `%s': %s", src_addr,
4596cdfca03SJohn Marino (error == EAI_SYSTEM) ? strerror(errno)
4606cdfca03SJohn Marino : gai_strerror(error));
4616cdfca03SJohn Marino }
4626cdfca03SJohn Marino }
4636cdfca03SJohn Marino
4646cdfca03SJohn Marino /*
4656cdfca03SJohn Marino * Cache the user name and home directory.
4666cdfca03SJohn Marino */
4676cdfca03SJohn Marino localhome = NULL;
4686cdfca03SJohn Marino localname = NULL;
4696cdfca03SJohn Marino anonuser = "anonymous";
4706cdfca03SJohn Marino cp = getenv("HOME");
4716cdfca03SJohn Marino if (! EMPTYSTRING(cp))
4726cdfca03SJohn Marino localhome = ftp_strdup(cp);
4736cdfca03SJohn Marino pw = NULL;
4746cdfca03SJohn Marino cp = getlogin();
4756cdfca03SJohn Marino if (cp != NULL)
4766cdfca03SJohn Marino pw = getpwnam(cp);
4776cdfca03SJohn Marino if (pw == NULL)
4786cdfca03SJohn Marino pw = getpwuid(getuid());
4796cdfca03SJohn Marino if (pw != NULL) {
4806cdfca03SJohn Marino if (localhome == NULL && !EMPTYSTRING(pw->pw_dir))
4816cdfca03SJohn Marino localhome = ftp_strdup(pw->pw_dir);
4826cdfca03SJohn Marino localname = ftp_strdup(pw->pw_name);
4836cdfca03SJohn Marino }
4846cdfca03SJohn Marino if (netrc[0] == '\0' && localhome != NULL) {
4856cdfca03SJohn Marino if (strlcpy(netrc, localhome, sizeof(netrc)) >= sizeof(netrc) ||
4866cdfca03SJohn Marino strlcat(netrc, "/.netrc", sizeof(netrc)) >= sizeof(netrc)) {
4876cdfca03SJohn Marino warnx("%s/.netrc: %s", localhome,
4886cdfca03SJohn Marino strerror(ENAMETOOLONG));
4896cdfca03SJohn Marino netrc[0] = '\0';
4906cdfca03SJohn Marino }
4916cdfca03SJohn Marino }
4926cdfca03SJohn Marino if (localhome == NULL)
4936cdfca03SJohn Marino localhome = ftp_strdup("/");
4946cdfca03SJohn Marino
4956cdfca03SJohn Marino /*
4966cdfca03SJohn Marino * Every anonymous FTP server I've encountered will accept the
4976cdfca03SJohn Marino * string "username@", and will append the hostname itself. We
4986cdfca03SJohn Marino * do this by default since many servers are picky about not
4996cdfca03SJohn Marino * having a FQDN in the anonymous password.
5006cdfca03SJohn Marino * - thorpej@NetBSD.org
5016cdfca03SJohn Marino */
5026cdfca03SJohn Marino len = strlen(anonuser) + 2;
5036cdfca03SJohn Marino anonpass = ftp_malloc(len);
5046cdfca03SJohn Marino (void)strlcpy(anonpass, anonuser, len);
5056cdfca03SJohn Marino (void)strlcat(anonpass, "@", len);
5066cdfca03SJohn Marino
5076cdfca03SJohn Marino /*
5086cdfca03SJohn Marino * set all the defaults for options defined in
5096cdfca03SJohn Marino * struct option optiontab[] declared in cmdtab.c
5106cdfca03SJohn Marino */
5116cdfca03SJohn Marino setupoption("anonpass", getenv("FTPANONPASS"), anonpass);
5126cdfca03SJohn Marino setupoption("ftp_proxy", getenv(FTP_PROXY), "");
5136cdfca03SJohn Marino setupoption("http_proxy", getenv(HTTP_PROXY), "");
5146cdfca03SJohn Marino setupoption("https_proxy", getenv(HTTPS_PROXY), "");
5156cdfca03SJohn Marino setupoption("no_proxy", getenv(NO_PROXY), "");
5166cdfca03SJohn Marino setupoption("pager", getenv("PAGER"), DEFAULTPAGER);
5176cdfca03SJohn Marino setupoption("prompt", getenv("FTPPROMPT"), DEFAULTPROMPT);
5186cdfca03SJohn Marino setupoption("rprompt", getenv("FTPRPROMPT"), DEFAULTRPROMPT);
5196cdfca03SJohn Marino
5206cdfca03SJohn Marino free(anonpass);
5216cdfca03SJohn Marino
5226cdfca03SJohn Marino setttywidth(0);
5236cdfca03SJohn Marino #ifdef SIGINFO
5246cdfca03SJohn Marino (void)xsignal(SIGINFO, psummary);
5256cdfca03SJohn Marino #endif
5266cdfca03SJohn Marino (void)xsignal(SIGQUIT, psummary);
5276cdfca03SJohn Marino (void)xsignal(SIGUSR1, crankrate);
5286cdfca03SJohn Marino (void)xsignal(SIGUSR2, crankrate);
5296cdfca03SJohn Marino (void)xsignal(SIGWINCH, setttywidth);
5306cdfca03SJohn Marino
5316cdfca03SJohn Marino if (argc > 0) {
5326cdfca03SJohn Marino if (isupload) {
5336cdfca03SJohn Marino rval = auto_put(argc, argv, upload_path);
5346cdfca03SJohn Marino sigint_or_rval_exit:
5356cdfca03SJohn Marino if (sigint_raised) {
5366cdfca03SJohn Marino (void)xsignal(SIGINT, SIG_DFL);
5376cdfca03SJohn Marino raise(SIGINT);
5386cdfca03SJohn Marino }
5396cdfca03SJohn Marino exit(rval);
5406cdfca03SJohn Marino } else if (strchr(argv[0], ':') != NULL
5416cdfca03SJohn Marino && ! isipv6addr(argv[0])) {
5426cdfca03SJohn Marino rval = auto_fetch(argc, argv);
5436cdfca03SJohn Marino if (rval >= 0) /* -1 == connected and cd-ed */
5446cdfca03SJohn Marino goto sigint_or_rval_exit;
5456cdfca03SJohn Marino } else {
5466cdfca03SJohn Marino char *xargv[4], *uuser, *host;
5476cdfca03SJohn Marino char cmdbuf[MAXPATHLEN];
5486cdfca03SJohn Marino
5496cdfca03SJohn Marino if ((rval = sigsetjmp(toplevel, 1)))
5506cdfca03SJohn Marino goto sigint_or_rval_exit;
5516cdfca03SJohn Marino (void)xsignal(SIGINT, intr);
5526cdfca03SJohn Marino (void)xsignal(SIGPIPE, lostpeer);
5536cdfca03SJohn Marino uuser = NULL;
5546cdfca03SJohn Marino host = argv[0];
5556cdfca03SJohn Marino cp = strchr(host, '@');
5566cdfca03SJohn Marino if (cp) {
5576cdfca03SJohn Marino *cp = '\0';
5586cdfca03SJohn Marino uuser = host;
5596cdfca03SJohn Marino host = cp + 1;
5606cdfca03SJohn Marino }
5616cdfca03SJohn Marino (void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
5626cdfca03SJohn Marino xargv[0] = cmdbuf;
5636cdfca03SJohn Marino xargv[1] = host;
5646cdfca03SJohn Marino xargv[2] = argv[1];
5656cdfca03SJohn Marino xargv[3] = NULL;
5666cdfca03SJohn Marino do {
5676cdfca03SJohn Marino int oautologin;
5686cdfca03SJohn Marino
5696cdfca03SJohn Marino oautologin = autologin;
5706cdfca03SJohn Marino if (uuser != NULL) {
5716cdfca03SJohn Marino anonftp = 0;
5726cdfca03SJohn Marino autologin = 0;
5736cdfca03SJohn Marino }
5746cdfca03SJohn Marino setpeer(argc+1, xargv);
5756cdfca03SJohn Marino autologin = oautologin;
5766cdfca03SJohn Marino if (connected == 1 && uuser != NULL)
5776cdfca03SJohn Marino (void)ftp_login(host, uuser, NULL);
5786cdfca03SJohn Marino if (!retry_connect)
5796cdfca03SJohn Marino break;
5806cdfca03SJohn Marino if (!connected) {
5816cdfca03SJohn Marino macnum = 0;
5826cdfca03SJohn Marino fprintf(ttyout,
5836cdfca03SJohn Marino "Retrying in %d seconds...\n",
5846cdfca03SJohn Marino retry_connect);
5856cdfca03SJohn Marino sleep(retry_connect);
5866cdfca03SJohn Marino }
5876cdfca03SJohn Marino } while (!connected);
5886cdfca03SJohn Marino retry_connect = 0; /* connected, stop hiding msgs */
5896cdfca03SJohn Marino }
5906cdfca03SJohn Marino }
591*3a184c67SAntonio Huete Jimenez if (isupload) {
592*3a184c67SAntonio Huete Jimenez return usage();
593*3a184c67SAntonio Huete Jimenez }
5946cdfca03SJohn Marino
5956cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
5966cdfca03SJohn Marino controlediting();
5976cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
5986cdfca03SJohn Marino
5996cdfca03SJohn Marino (void)sigsetjmp(toplevel, 1);
6006cdfca03SJohn Marino (void)xsignal(SIGINT, intr);
6016cdfca03SJohn Marino (void)xsignal(SIGPIPE, lostpeer);
6026cdfca03SJohn Marino for (;;)
6036cdfca03SJohn Marino cmdscanner();
6046cdfca03SJohn Marino }
6056cdfca03SJohn Marino
6066cdfca03SJohn Marino /*
6076cdfca03SJohn Marino * Generate a prompt
6086cdfca03SJohn Marino */
6096cdfca03SJohn Marino char *
prompt(void)6106cdfca03SJohn Marino prompt(void)
6116cdfca03SJohn Marino {
6126cdfca03SJohn Marino static char **promptopt;
6136cdfca03SJohn Marino static char buf[MAXPATHLEN];
6146cdfca03SJohn Marino
6156cdfca03SJohn Marino if (promptopt == NULL) {
6166cdfca03SJohn Marino struct option *o;
6176cdfca03SJohn Marino
6186cdfca03SJohn Marino o = getoption("prompt");
6196cdfca03SJohn Marino if (o == NULL)
6206cdfca03SJohn Marino errx(1, "prompt: no such option `prompt'");
6216cdfca03SJohn Marino promptopt = &(o->value);
6226cdfca03SJohn Marino }
6236cdfca03SJohn Marino formatbuf(buf, sizeof(buf), *promptopt ? *promptopt : DEFAULTPROMPT);
6246cdfca03SJohn Marino return (buf);
6256cdfca03SJohn Marino }
6266cdfca03SJohn Marino
6276cdfca03SJohn Marino /*
6286cdfca03SJohn Marino * Generate an rprompt
6296cdfca03SJohn Marino */
6306cdfca03SJohn Marino char *
rprompt(void)6316cdfca03SJohn Marino rprompt(void)
6326cdfca03SJohn Marino {
6336cdfca03SJohn Marino static char **rpromptopt;
6346cdfca03SJohn Marino static char buf[MAXPATHLEN];
6356cdfca03SJohn Marino
6366cdfca03SJohn Marino if (rpromptopt == NULL) {
6376cdfca03SJohn Marino struct option *o;
6386cdfca03SJohn Marino
6396cdfca03SJohn Marino o = getoption("rprompt");
6406cdfca03SJohn Marino if (o == NULL)
6416cdfca03SJohn Marino errx(1, "rprompt: no such option `rprompt'");
6426cdfca03SJohn Marino rpromptopt = &(o->value);
6436cdfca03SJohn Marino }
6446cdfca03SJohn Marino formatbuf(buf, sizeof(buf), *rpromptopt ? *rpromptopt : DEFAULTRPROMPT);
6456cdfca03SJohn Marino return (buf);
6466cdfca03SJohn Marino }
6476cdfca03SJohn Marino
6486cdfca03SJohn Marino /*
6496cdfca03SJohn Marino * Command parser.
6506cdfca03SJohn Marino */
6516cdfca03SJohn Marino void
cmdscanner(void)6526cdfca03SJohn Marino cmdscanner(void)
6536cdfca03SJohn Marino {
6546cdfca03SJohn Marino struct cmd *c;
6556cdfca03SJohn Marino char *p;
6566cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
6576cdfca03SJohn Marino int ch;
6586cdfca03SJohn Marino size_t num;
6596cdfca03SJohn Marino #endif
6606cdfca03SJohn Marino int len;
6616cdfca03SJohn Marino char cmdbuf[MAX_C_NAME];
6626cdfca03SJohn Marino
6636cdfca03SJohn Marino for (;;) {
6646cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
6656cdfca03SJohn Marino if (!editing) {
6666cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
6676cdfca03SJohn Marino if (fromatty) {
6686cdfca03SJohn Marino fputs(prompt(), ttyout);
6696cdfca03SJohn Marino p = rprompt();
6706cdfca03SJohn Marino if (*p)
6716cdfca03SJohn Marino fprintf(ttyout, "%s ", p);
6726cdfca03SJohn Marino }
6736cdfca03SJohn Marino (void)fflush(ttyout);
6746cdfca03SJohn Marino len = get_line(stdin, line, sizeof(line), NULL);
6756cdfca03SJohn Marino switch (len) {
6766cdfca03SJohn Marino case -1: /* EOF */
6776cdfca03SJohn Marino case -2: /* error */
6786cdfca03SJohn Marino if (fromatty)
6796cdfca03SJohn Marino putc('\n', ttyout);
680*3a184c67SAntonio Huete Jimenez justquit();
6816cdfca03SJohn Marino /* NOTREACHED */
6826cdfca03SJohn Marino case -3: /* too long; try again */
6836cdfca03SJohn Marino fputs("Sorry, input line is too long.\n",
6846cdfca03SJohn Marino ttyout);
6856cdfca03SJohn Marino continue;
6866cdfca03SJohn Marino case 0: /* empty; try again */
6876cdfca03SJohn Marino continue;
6886cdfca03SJohn Marino default: /* all ok */
6896cdfca03SJohn Marino break;
6906cdfca03SJohn Marino }
6916cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
6926cdfca03SJohn Marino } else {
6936cdfca03SJohn Marino const char *buf;
6946cdfca03SJohn Marino HistEvent ev;
6956cdfca03SJohn Marino cursor_pos = NULL;
6966cdfca03SJohn Marino
6976cdfca03SJohn Marino buf = el_gets(el, &ch);
6986cdfca03SJohn Marino num = ch;
6996cdfca03SJohn Marino if (buf == NULL || num == 0) {
7006cdfca03SJohn Marino if (fromatty)
7016cdfca03SJohn Marino putc('\n', ttyout);
702*3a184c67SAntonio Huete Jimenez justquit();
7036cdfca03SJohn Marino }
7046cdfca03SJohn Marino if (num >= sizeof(line)) {
7056cdfca03SJohn Marino fputs("Sorry, input line is too long.\n",
7066cdfca03SJohn Marino ttyout);
7076cdfca03SJohn Marino break;
7086cdfca03SJohn Marino }
7096cdfca03SJohn Marino memcpy(line, buf, num);
7106cdfca03SJohn Marino if (line[--num] == '\n') {
7116cdfca03SJohn Marino line[num] = '\0';
7126cdfca03SJohn Marino if (num == 0)
7136cdfca03SJohn Marino break;
7146cdfca03SJohn Marino }
7156cdfca03SJohn Marino history(hist, &ev, H_ENTER, buf);
7166cdfca03SJohn Marino }
7176cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
7186cdfca03SJohn Marino
7196cdfca03SJohn Marino makeargv();
7206cdfca03SJohn Marino if (margc == 0)
7216cdfca03SJohn Marino continue;
7226cdfca03SJohn Marino c = getcmd(margv[0]);
7236cdfca03SJohn Marino if (c == (struct cmd *)-1) {
7246cdfca03SJohn Marino fputs("?Ambiguous command.\n", ttyout);
7256cdfca03SJohn Marino continue;
7266cdfca03SJohn Marino }
7276cdfca03SJohn Marino if (c == NULL) {
7286cdfca03SJohn Marino #if !defined(NO_EDITCOMPLETE)
7296cdfca03SJohn Marino /*
7306cdfca03SJohn Marino * attempt to el_parse() unknown commands.
7316cdfca03SJohn Marino * any command containing a ':' would be parsed
7326cdfca03SJohn Marino * as "[prog:]cmd ...", and will result in a
7336cdfca03SJohn Marino * false positive if prog != "ftp", so treat
7346cdfca03SJohn Marino * such commands as invalid.
7356cdfca03SJohn Marino */
7366cdfca03SJohn Marino if (strchr(margv[0], ':') != NULL ||
7376cdfca03SJohn Marino !editing ||
7386cdfca03SJohn Marino el_parse(el, margc, (void *)margv) != 0)
7396cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
7406cdfca03SJohn Marino fputs("?Invalid command.\n", ttyout);
7416cdfca03SJohn Marino continue;
7426cdfca03SJohn Marino }
7436cdfca03SJohn Marino if (c->c_conn && !connected) {
7446cdfca03SJohn Marino fputs("Not connected.\n", ttyout);
7456cdfca03SJohn Marino continue;
7466cdfca03SJohn Marino }
7476cdfca03SJohn Marino confirmrest = 0;
7486cdfca03SJohn Marino (void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
7496cdfca03SJohn Marino margv[0] = cmdbuf;
7506cdfca03SJohn Marino (*c->c_handler)(margc, margv);
7516cdfca03SJohn Marino if (bell && c->c_bell)
7526cdfca03SJohn Marino (void)putc('\007', ttyout);
7536cdfca03SJohn Marino if (c->c_handler != help)
7546cdfca03SJohn Marino break;
7556cdfca03SJohn Marino }
7566cdfca03SJohn Marino (void)xsignal(SIGINT, intr);
7576cdfca03SJohn Marino (void)xsignal(SIGPIPE, lostpeer);
7586cdfca03SJohn Marino }
7596cdfca03SJohn Marino
7606cdfca03SJohn Marino struct cmd *
getcmd(const char * name)7616cdfca03SJohn Marino getcmd(const char *name)
7626cdfca03SJohn Marino {
7636cdfca03SJohn Marino const char *p, *q;
7646cdfca03SJohn Marino struct cmd *c, *found;
7656cdfca03SJohn Marino int nmatches, longest;
7666cdfca03SJohn Marino
7676cdfca03SJohn Marino if (name == NULL)
7686cdfca03SJohn Marino return (0);
7696cdfca03SJohn Marino
7706cdfca03SJohn Marino longest = 0;
7716cdfca03SJohn Marino nmatches = 0;
7726cdfca03SJohn Marino found = 0;
7736cdfca03SJohn Marino for (c = cmdtab; (p = c->c_name) != NULL; c++) {
7746cdfca03SJohn Marino for (q = name; *q == *p++; q++)
7756cdfca03SJohn Marino if (*q == 0) /* exact match? */
7766cdfca03SJohn Marino return (c);
7776cdfca03SJohn Marino if (!*q) { /* the name was a prefix */
7786cdfca03SJohn Marino if (q - name > longest) {
7796cdfca03SJohn Marino longest = q - name;
7806cdfca03SJohn Marino nmatches = 1;
7816cdfca03SJohn Marino found = c;
7826cdfca03SJohn Marino } else if (q - name == longest)
7836cdfca03SJohn Marino nmatches++;
7846cdfca03SJohn Marino }
7856cdfca03SJohn Marino }
7866cdfca03SJohn Marino if (nmatches > 1)
7876cdfca03SJohn Marino return ((struct cmd *)-1);
7886cdfca03SJohn Marino return (found);
7896cdfca03SJohn Marino }
7906cdfca03SJohn Marino
7916cdfca03SJohn Marino /*
7926cdfca03SJohn Marino * Slice a string up into argc/argv.
7936cdfca03SJohn Marino */
7946cdfca03SJohn Marino
7956cdfca03SJohn Marino int slrflag;
7966cdfca03SJohn Marino
7976cdfca03SJohn Marino void
makeargv(void)7986cdfca03SJohn Marino makeargv(void)
7996cdfca03SJohn Marino {
8006cdfca03SJohn Marino char *argp;
8016cdfca03SJohn Marino
8026cdfca03SJohn Marino stringbase = line; /* scan from first of buffer */
8036cdfca03SJohn Marino argbase = argbuf; /* store from first of buffer */
8046cdfca03SJohn Marino slrflag = 0;
8056cdfca03SJohn Marino marg_sl->sl_cur = 0; /* reset to start of marg_sl */
8066cdfca03SJohn Marino for (margc = 0; ; margc++) {
8076cdfca03SJohn Marino argp = slurpstring();
8086cdfca03SJohn Marino ftp_sl_add(marg_sl, argp);
8096cdfca03SJohn Marino if (argp == NULL)
8106cdfca03SJohn Marino break;
8116cdfca03SJohn Marino }
8126cdfca03SJohn Marino #ifndef NO_EDITCOMPLETE
8136cdfca03SJohn Marino if (cursor_pos == line) {
8146cdfca03SJohn Marino cursor_argc = 0;
8156cdfca03SJohn Marino cursor_argo = 0;
8166cdfca03SJohn Marino } else if (cursor_pos != NULL) {
8176cdfca03SJohn Marino cursor_argc = margc;
8186cdfca03SJohn Marino cursor_argo = strlen(margv[margc-1]);
8196cdfca03SJohn Marino }
8206cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
8216cdfca03SJohn Marino }
8226cdfca03SJohn Marino
8236cdfca03SJohn Marino #ifdef NO_EDITCOMPLETE
8246cdfca03SJohn Marino #define INC_CHKCURSOR(x) (x)++
8256cdfca03SJohn Marino #else /* !NO_EDITCOMPLETE */
8266cdfca03SJohn Marino #define INC_CHKCURSOR(x) { (x)++ ; \
8276cdfca03SJohn Marino if (x == cursor_pos) { \
8286cdfca03SJohn Marino cursor_argc = margc; \
8296cdfca03SJohn Marino cursor_argo = ap-argbase; \
8306cdfca03SJohn Marino cursor_pos = NULL; \
8316cdfca03SJohn Marino } }
8326cdfca03SJohn Marino
8336cdfca03SJohn Marino #endif /* !NO_EDITCOMPLETE */
8346cdfca03SJohn Marino
8356cdfca03SJohn Marino /*
8366cdfca03SJohn Marino * Parse string into argbuf;
8376cdfca03SJohn Marino * implemented with FSM to
8386cdfca03SJohn Marino * handle quoting and strings
8396cdfca03SJohn Marino */
8406cdfca03SJohn Marino char *
slurpstring(void)8416cdfca03SJohn Marino slurpstring(void)
8426cdfca03SJohn Marino {
8436cdfca03SJohn Marino static char bangstr[2] = { '!', '\0' };
8446cdfca03SJohn Marino static char dollarstr[2] = { '$', '\0' };
8456cdfca03SJohn Marino int got_one = 0;
8466cdfca03SJohn Marino char *sb = stringbase;
8476cdfca03SJohn Marino char *ap = argbase;
8486cdfca03SJohn Marino char *tmp = argbase; /* will return this if token found */
8496cdfca03SJohn Marino
8506cdfca03SJohn Marino if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
8516cdfca03SJohn Marino switch (slrflag) { /* and $ as token for macro invoke */
8526cdfca03SJohn Marino case 0:
8536cdfca03SJohn Marino slrflag++;
8546cdfca03SJohn Marino INC_CHKCURSOR(stringbase);
8556cdfca03SJohn Marino return ((*sb == '!') ? bangstr : dollarstr);
8566cdfca03SJohn Marino case 1:
8576cdfca03SJohn Marino slrflag++;
8586cdfca03SJohn Marino altarg = stringbase;
8596cdfca03SJohn Marino break;
8606cdfca03SJohn Marino default:
8616cdfca03SJohn Marino break;
8626cdfca03SJohn Marino }
8636cdfca03SJohn Marino }
8646cdfca03SJohn Marino
8656cdfca03SJohn Marino S0:
8666cdfca03SJohn Marino switch (*sb) {
8676cdfca03SJohn Marino
8686cdfca03SJohn Marino case '\0':
8696cdfca03SJohn Marino goto OUT;
8706cdfca03SJohn Marino
8716cdfca03SJohn Marino case ' ':
8726cdfca03SJohn Marino case '\t':
8736cdfca03SJohn Marino INC_CHKCURSOR(sb);
8746cdfca03SJohn Marino goto S0;
8756cdfca03SJohn Marino
8766cdfca03SJohn Marino default:
8776cdfca03SJohn Marino switch (slrflag) {
8786cdfca03SJohn Marino case 0:
8796cdfca03SJohn Marino slrflag++;
8806cdfca03SJohn Marino break;
8816cdfca03SJohn Marino case 1:
8826cdfca03SJohn Marino slrflag++;
8836cdfca03SJohn Marino altarg = sb;
8846cdfca03SJohn Marino break;
8856cdfca03SJohn Marino default:
8866cdfca03SJohn Marino break;
8876cdfca03SJohn Marino }
8886cdfca03SJohn Marino goto S1;
8896cdfca03SJohn Marino }
8906cdfca03SJohn Marino
8916cdfca03SJohn Marino S1:
8926cdfca03SJohn Marino switch (*sb) {
8936cdfca03SJohn Marino
8946cdfca03SJohn Marino case ' ':
8956cdfca03SJohn Marino case '\t':
8966cdfca03SJohn Marino case '\0':
8976cdfca03SJohn Marino goto OUT; /* end of token */
8986cdfca03SJohn Marino
8996cdfca03SJohn Marino case '\\':
9006cdfca03SJohn Marino INC_CHKCURSOR(sb);
9016cdfca03SJohn Marino goto S2; /* slurp next character */
9026cdfca03SJohn Marino
9036cdfca03SJohn Marino case '"':
9046cdfca03SJohn Marino INC_CHKCURSOR(sb);
9056cdfca03SJohn Marino goto S3; /* slurp quoted string */
9066cdfca03SJohn Marino
9076cdfca03SJohn Marino default:
9086cdfca03SJohn Marino *ap = *sb; /* add character to token */
9096cdfca03SJohn Marino ap++;
9106cdfca03SJohn Marino INC_CHKCURSOR(sb);
9116cdfca03SJohn Marino got_one = 1;
9126cdfca03SJohn Marino goto S1;
9136cdfca03SJohn Marino }
9146cdfca03SJohn Marino
9156cdfca03SJohn Marino S2:
9166cdfca03SJohn Marino switch (*sb) {
9176cdfca03SJohn Marino
9186cdfca03SJohn Marino case '\0':
9196cdfca03SJohn Marino goto OUT;
9206cdfca03SJohn Marino
9216cdfca03SJohn Marino default:
9226cdfca03SJohn Marino *ap = *sb;
9236cdfca03SJohn Marino ap++;
9246cdfca03SJohn Marino INC_CHKCURSOR(sb);
9256cdfca03SJohn Marino got_one = 1;
9266cdfca03SJohn Marino goto S1;
9276cdfca03SJohn Marino }
9286cdfca03SJohn Marino
9296cdfca03SJohn Marino S3:
9306cdfca03SJohn Marino switch (*sb) {
9316cdfca03SJohn Marino
9326cdfca03SJohn Marino case '\0':
9336cdfca03SJohn Marino goto OUT;
9346cdfca03SJohn Marino
9356cdfca03SJohn Marino case '"':
9366cdfca03SJohn Marino INC_CHKCURSOR(sb);
9376cdfca03SJohn Marino goto S1;
9386cdfca03SJohn Marino
9396cdfca03SJohn Marino default:
9406cdfca03SJohn Marino *ap = *sb;
9416cdfca03SJohn Marino ap++;
9426cdfca03SJohn Marino INC_CHKCURSOR(sb);
9436cdfca03SJohn Marino got_one = 1;
9446cdfca03SJohn Marino goto S3;
9456cdfca03SJohn Marino }
9466cdfca03SJohn Marino
9476cdfca03SJohn Marino OUT:
9486cdfca03SJohn Marino if (got_one)
9496cdfca03SJohn Marino *ap++ = '\0';
9506cdfca03SJohn Marino argbase = ap; /* update storage pointer */
9516cdfca03SJohn Marino stringbase = sb; /* update scan pointer */
9526cdfca03SJohn Marino if (got_one) {
9536cdfca03SJohn Marino return (tmp);
9546cdfca03SJohn Marino }
9556cdfca03SJohn Marino switch (slrflag) {
9566cdfca03SJohn Marino case 0:
9576cdfca03SJohn Marino slrflag++;
9586cdfca03SJohn Marino break;
9596cdfca03SJohn Marino case 1:
9606cdfca03SJohn Marino slrflag++;
9616cdfca03SJohn Marino altarg = NULL;
9626cdfca03SJohn Marino break;
9636cdfca03SJohn Marino default:
9646cdfca03SJohn Marino break;
9656cdfca03SJohn Marino }
9666cdfca03SJohn Marino return (NULL);
9676cdfca03SJohn Marino }
9686cdfca03SJohn Marino
9696cdfca03SJohn Marino /*
9706cdfca03SJohn Marino * Help/usage command.
9716cdfca03SJohn Marino * Call each command handler with argc == 0 and argv[0] == name.
9726cdfca03SJohn Marino */
9736cdfca03SJohn Marino void
help(int argc,char * argv[])9746cdfca03SJohn Marino help(int argc, char *argv[])
9756cdfca03SJohn Marino {
9766cdfca03SJohn Marino struct cmd *c;
9776cdfca03SJohn Marino char *nargv[1], *cmd;
9786cdfca03SJohn Marino const char *p;
9796cdfca03SJohn Marino int isusage;
9806cdfca03SJohn Marino
9816cdfca03SJohn Marino cmd = argv[0];
9826cdfca03SJohn Marino isusage = (strcmp(cmd, "usage") == 0);
9836cdfca03SJohn Marino if (argc == 0 || (isusage && argc == 1)) {
984*3a184c67SAntonio Huete Jimenez UPRINTF("usage: %s [command ...]\n", cmd);
9856cdfca03SJohn Marino return;
9866cdfca03SJohn Marino }
9876cdfca03SJohn Marino if (argc == 1) {
9886cdfca03SJohn Marino StringList *buf;
9896cdfca03SJohn Marino
9906cdfca03SJohn Marino buf = ftp_sl_init();
9916cdfca03SJohn Marino fprintf(ttyout,
9926cdfca03SJohn Marino "%sommands may be abbreviated. Commands are:\n\n",
9936cdfca03SJohn Marino proxy ? "Proxy c" : "C");
9946cdfca03SJohn Marino for (c = cmdtab; (p = c->c_name) != NULL; c++)
9956cdfca03SJohn Marino if (!proxy || c->c_proxy)
9966cdfca03SJohn Marino ftp_sl_add(buf, ftp_strdup(p));
9976cdfca03SJohn Marino list_vertical(buf);
9986cdfca03SJohn Marino sl_free(buf, 1);
9996cdfca03SJohn Marino return;
10006cdfca03SJohn Marino }
10016cdfca03SJohn Marino
10026cdfca03SJohn Marino #define HELPINDENT ((int) sizeof("disconnect"))
10036cdfca03SJohn Marino
10046cdfca03SJohn Marino while (--argc > 0) {
10056cdfca03SJohn Marino char *arg;
10066cdfca03SJohn Marino char cmdbuf[MAX_C_NAME];
10076cdfca03SJohn Marino
10086cdfca03SJohn Marino arg = *++argv;
10096cdfca03SJohn Marino c = getcmd(arg);
10106cdfca03SJohn Marino if (c == (struct cmd *)-1)
10116cdfca03SJohn Marino fprintf(ttyout, "?Ambiguous %s command `%s'\n",
10126cdfca03SJohn Marino cmd, arg);
10136cdfca03SJohn Marino else if (c == NULL)
10146cdfca03SJohn Marino fprintf(ttyout, "?Invalid %s command `%s'\n",
10156cdfca03SJohn Marino cmd, arg);
10166cdfca03SJohn Marino else {
10176cdfca03SJohn Marino if (isusage) {
10186cdfca03SJohn Marino (void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
10196cdfca03SJohn Marino nargv[0] = cmdbuf;
10206cdfca03SJohn Marino (*c->c_handler)(0, nargv);
10216cdfca03SJohn Marino } else
10226cdfca03SJohn Marino fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
10236cdfca03SJohn Marino c->c_name, c->c_help);
10246cdfca03SJohn Marino }
10256cdfca03SJohn Marino }
10266cdfca03SJohn Marino }
10276cdfca03SJohn Marino
10286cdfca03SJohn Marino struct option *
getoption(const char * name)10296cdfca03SJohn Marino getoption(const char *name)
10306cdfca03SJohn Marino {
10316cdfca03SJohn Marino const char *p;
10326cdfca03SJohn Marino struct option *c;
10336cdfca03SJohn Marino
10346cdfca03SJohn Marino if (name == NULL)
10356cdfca03SJohn Marino return (NULL);
10366cdfca03SJohn Marino for (c = optiontab; (p = c->name) != NULL; c++) {
10376cdfca03SJohn Marino if (strcasecmp(p, name) == 0)
10386cdfca03SJohn Marino return (c);
10396cdfca03SJohn Marino }
10406cdfca03SJohn Marino return (NULL);
10416cdfca03SJohn Marino }
10426cdfca03SJohn Marino
10436cdfca03SJohn Marino char *
getoptionvalue(const char * name)10446cdfca03SJohn Marino getoptionvalue(const char *name)
10456cdfca03SJohn Marino {
10466cdfca03SJohn Marino struct option *c;
10476cdfca03SJohn Marino
10486cdfca03SJohn Marino if (name == NULL)
10496cdfca03SJohn Marino errx(1, "getoptionvalue: invoked with NULL name");
10506cdfca03SJohn Marino c = getoption(name);
10516cdfca03SJohn Marino if (c != NULL)
10526cdfca03SJohn Marino return (c->value);
10536cdfca03SJohn Marino errx(1, "getoptionvalue: invoked with unknown option `%s'", name);
10546cdfca03SJohn Marino /* NOTREACHED */
10556cdfca03SJohn Marino }
10566cdfca03SJohn Marino
10576cdfca03SJohn Marino static void
setupoption(const char * name,const char * value,const char * defaultvalue)10586cdfca03SJohn Marino setupoption(const char *name, const char *value, const char *defaultvalue)
10596cdfca03SJohn Marino {
10606cdfca03SJohn Marino set_option(name, value ? value : defaultvalue, 0);
10616cdfca03SJohn Marino }
10626cdfca03SJohn Marino
1063*3a184c67SAntonio Huete Jimenez static void
synopsis(FILE * stream)1064*3a184c67SAntonio Huete Jimenez synopsis(FILE * stream)
10656cdfca03SJohn Marino {
10666cdfca03SJohn Marino const char * progname = getprogname();
10676cdfca03SJohn Marino
1068*3a184c67SAntonio Huete Jimenez fprintf(stream,
1069*3a184c67SAntonio Huete Jimenez "usage: %s [-46AadefginpRtVv] [-N NETRC] [-o OUTPUT] [-P PORT] [-q QUITTIME]\n"
1070*3a184c67SAntonio Huete Jimenez " [-r RETRY] [-s SRCADDR] [-T DIR,MAX[,INC]] [-x XFERSIZE]\n"
1071*3a184c67SAntonio Huete Jimenez " [[USER@]HOST [PORT]]\n"
1072*3a184c67SAntonio Huete Jimenez " [[USER@]HOST:[PATH][/]]\n"
1073*3a184c67SAntonio Huete Jimenez " [file:///PATH]\n"
1074*3a184c67SAntonio Huete Jimenez " [ftp://[USER[:PASSWORD]@]HOST[:PORT]/PATH[/][;type=TYPE]]\n"
1075*3a184c67SAntonio Huete Jimenez " [http://[USER[:PASSWORD]@]HOST[:PORT]/PATH]\n"
10766cdfca03SJohn Marino #ifdef WITH_SSL
1077*3a184c67SAntonio Huete Jimenez " [https://[USER[:PASSWORD]@]HOST[:PORT]/PATH]\n"
10786cdfca03SJohn Marino #endif
1079*3a184c67SAntonio Huete Jimenez " ...\n"
1080*3a184c67SAntonio Huete Jimenez " %s -u URL FILE ...\n"
1081*3a184c67SAntonio Huete Jimenez " %s -?\n",
1082*3a184c67SAntonio Huete Jimenez progname, progname, progname);
1083*3a184c67SAntonio Huete Jimenez }
1084*3a184c67SAntonio Huete Jimenez
1085*3a184c67SAntonio Huete Jimenez static int
usage_help(void)1086*3a184c67SAntonio Huete Jimenez usage_help(void)
1087*3a184c67SAntonio Huete Jimenez {
1088*3a184c67SAntonio Huete Jimenez synopsis(stdout);
1089*3a184c67SAntonio Huete Jimenez #ifndef NO_USAGE
1090*3a184c67SAntonio Huete Jimenez printf(
1091*3a184c67SAntonio Huete Jimenez " -4 Only use IPv4 addresses\n"
1092*3a184c67SAntonio Huete Jimenez " -6 Only use IPv6 addresses\n"
1093*3a184c67SAntonio Huete Jimenez " -A Force active mode\n"
1094*3a184c67SAntonio Huete Jimenez " -a Use anonymous login\n"
1095*3a184c67SAntonio Huete Jimenez " -d Enable debugging\n"
1096*3a184c67SAntonio Huete Jimenez " -e Disable command-line editing\n"
1097*3a184c67SAntonio Huete Jimenez " -f Force cache reload for FTP or HTTP proxy transfers\n"
1098*3a184c67SAntonio Huete Jimenez " -g Disable file name globbing\n"
1099*3a184c67SAntonio Huete Jimenez " -i Disable interactive prompt during multiple file transfers\n"
1100*3a184c67SAntonio Huete Jimenez " -N NETRC Use NETRC instead of ~/.netrc\n"
1101*3a184c67SAntonio Huete Jimenez " -n Disable auto-login\n"
1102*3a184c67SAntonio Huete Jimenez " -o OUTPUT Save auto-fetched files to OUTPUT\n"
1103*3a184c67SAntonio Huete Jimenez " -P PORT Use port PORT\n"
1104*3a184c67SAntonio Huete Jimenez " -p Force passive mode\n"
1105*3a184c67SAntonio Huete Jimenez " -q QUITTIME Quit if connection stalls for QUITTIME seconds\n"
1106*3a184c67SAntonio Huete Jimenez " -R Restart non-proxy auto-fetch\n"
1107*3a184c67SAntonio Huete Jimenez " -r RETRY Retry failed connection attempts after RETRY seconds\n"
1108*3a184c67SAntonio Huete Jimenez " -s SRCADDR Use source address SRCADDR\n"
1109*3a184c67SAntonio Huete Jimenez " -t Enable packet tracing\n"
1110*3a184c67SAntonio Huete Jimenez " -T DIR,MAX[,INC]\n"
1111*3a184c67SAntonio Huete Jimenez " Set maximum transfer rate for direction DIR to MAX bytes/s,\n"
1112*3a184c67SAntonio Huete Jimenez " with optional increment INC bytes/s\n"
1113*3a184c67SAntonio Huete Jimenez " -u URL URL to upload file arguments to\n"
1114*3a184c67SAntonio Huete Jimenez " -V Disable verbose and progress\n"
1115*3a184c67SAntonio Huete Jimenez " -v Enable verbose and progress\n"
1116*3a184c67SAntonio Huete Jimenez " -x XFERSIZE Set socket send and receive size to XFERSIZE\n"
1117*3a184c67SAntonio Huete Jimenez " -? Display this help and exit\n"
1118*3a184c67SAntonio Huete Jimenez );
1119*3a184c67SAntonio Huete Jimenez #endif
1120*3a184c67SAntonio Huete Jimenez return EXIT_SUCCESS;
1121*3a184c67SAntonio Huete Jimenez }
1122*3a184c67SAntonio Huete Jimenez
1123*3a184c67SAntonio Huete Jimenez static int
usage(void)1124*3a184c67SAntonio Huete Jimenez usage(void)
1125*3a184c67SAntonio Huete Jimenez {
1126*3a184c67SAntonio Huete Jimenez synopsis(stderr);
1127*3a184c67SAntonio Huete Jimenez return EXIT_FAILURE;
11286cdfca03SJohn Marino }
1129