1*0a6a1f1dSLionel Sambuc /* $NetBSD: main.c,v 1.123 2015/04/23 23:31:23 lukem Exp $ */
204203a83SThomas Cort
304203a83SThomas Cort /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 1996-2015 The NetBSD Foundation, Inc.
504203a83SThomas Cort * All rights reserved.
604203a83SThomas Cort *
704203a83SThomas Cort * This code is derived from software contributed to The NetBSD Foundation
804203a83SThomas Cort * by Luke Mewburn.
904203a83SThomas Cort *
1004203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
1104203a83SThomas Cort * modification, are permitted provided that the following conditions
1204203a83SThomas Cort * are met:
1304203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
1404203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
1504203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
1604203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
1704203a83SThomas Cort * documentation and/or other materials provided with the distribution.
1804203a83SThomas Cort *
1904203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2004203a83SThomas Cort * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2104203a83SThomas Cort * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2204203a83SThomas Cort * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2304203a83SThomas Cort * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2404203a83SThomas Cort * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2504203a83SThomas Cort * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2604203a83SThomas Cort * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2704203a83SThomas Cort * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2804203a83SThomas Cort * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2904203a83SThomas Cort * POSSIBILITY OF SUCH DAMAGE.
3004203a83SThomas Cort */
3104203a83SThomas Cort
3204203a83SThomas Cort /*
3304203a83SThomas Cort * Copyright (c) 1985, 1989, 1993, 1994
3404203a83SThomas Cort * The Regents of the University of California. All rights reserved.
3504203a83SThomas Cort *
3604203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
3704203a83SThomas Cort * modification, are permitted provided that the following conditions
3804203a83SThomas Cort * are met:
3904203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
4004203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
4104203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
4204203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
4304203a83SThomas Cort * documentation and/or other materials provided with the distribution.
4404203a83SThomas Cort * 3. Neither the name of the University nor the names of its contributors
4504203a83SThomas Cort * may be used to endorse or promote products derived from this software
4604203a83SThomas Cort * without specific prior written permission.
4704203a83SThomas Cort *
4804203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4904203a83SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5004203a83SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5104203a83SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5204203a83SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5304203a83SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5404203a83SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5504203a83SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5604203a83SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5704203a83SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5804203a83SThomas Cort * SUCH DAMAGE.
5904203a83SThomas Cort */
6004203a83SThomas Cort
6104203a83SThomas Cort /*
6204203a83SThomas Cort * Copyright (C) 1997 and 1998 WIDE Project.
6304203a83SThomas Cort * All rights reserved.
6404203a83SThomas Cort *
6504203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
6604203a83SThomas Cort * modification, are permitted provided that the following conditions
6704203a83SThomas Cort * are met:
6804203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
6904203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
7004203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
7104203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
7204203a83SThomas Cort * documentation and/or other materials provided with the distribution.
7304203a83SThomas Cort * 3. Neither the name of the project nor the names of its contributors
7404203a83SThomas Cort * may be used to endorse or promote products derived from this software
7504203a83SThomas Cort * without specific prior written permission.
7604203a83SThomas Cort *
7704203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
7804203a83SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7904203a83SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8004203a83SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
8104203a83SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8204203a83SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8304203a83SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8404203a83SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8504203a83SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8604203a83SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8704203a83SThomas Cort * SUCH DAMAGE.
8804203a83SThomas Cort */
8904203a83SThomas Cort
9004203a83SThomas Cort #include <sys/cdefs.h>
9104203a83SThomas Cort #ifndef lint
9204203a83SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\
9304203a83SThomas Cort The Regents of the University of California. All rights reserved.\
94*0a6a1f1dSLionel Sambuc Copyright 1996-2015 The NetBSD Foundation, Inc. All rights reserved");
9504203a83SThomas Cort #endif /* not lint */
9604203a83SThomas Cort
9704203a83SThomas Cort #ifndef lint
9804203a83SThomas Cort #if 0
9904203a83SThomas Cort static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
10004203a83SThomas Cort #else
101*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: main.c,v 1.123 2015/04/23 23:31:23 lukem Exp $");
10204203a83SThomas Cort #endif
10304203a83SThomas Cort #endif /* not lint */
10404203a83SThomas Cort
10504203a83SThomas Cort /*
10604203a83SThomas Cort * FTP User Program -- Command Interface.
10704203a83SThomas Cort */
10804203a83SThomas Cort #include <sys/types.h>
10904203a83SThomas Cort #include <sys/socket.h>
11004203a83SThomas Cort
11104203a83SThomas Cort #include <err.h>
11204203a83SThomas Cort #include <errno.h>
11304203a83SThomas Cort #include <netdb.h>
11404203a83SThomas Cort #include <paths.h>
11504203a83SThomas Cort #include <pwd.h>
11604203a83SThomas Cort #include <signal.h>
11704203a83SThomas Cort #include <stdio.h>
11804203a83SThomas Cort #include <stdlib.h>
11904203a83SThomas Cort #include <string.h>
12004203a83SThomas Cort #include <time.h>
12104203a83SThomas Cort #include <unistd.h>
12204203a83SThomas Cort #include <locale.h>
12304203a83SThomas Cort
12404203a83SThomas Cort #define GLOBAL /* force GLOBAL decls in ftp_var.h to be declared */
12504203a83SThomas Cort #include "ftp_var.h"
12604203a83SThomas Cort
12704203a83SThomas Cort #define FTP_PROXY "ftp_proxy" /* env var with FTP proxy location */
12804203a83SThomas Cort #define HTTP_PROXY "http_proxy" /* env var with HTTP proxy location */
12904203a83SThomas Cort #define HTTPS_PROXY "https_proxy" /* env var with HTTPS proxy location */
13004203a83SThomas Cort #define NO_PROXY "no_proxy" /* env var with list of non-proxied
13104203a83SThomas Cort * hosts, comma or space separated */
13204203a83SThomas Cort
13304203a83SThomas Cort __dead static void usage(void);
13404203a83SThomas Cort static void setupoption(const char *, const char *, const char *);
13504203a83SThomas Cort
13604203a83SThomas Cort int
main(int volatile argc,char ** volatile argv)13704203a83SThomas Cort main(int volatile argc, char **volatile argv)
13804203a83SThomas Cort {
13904203a83SThomas Cort int ch, rval;
14004203a83SThomas Cort struct passwd *pw;
14104203a83SThomas Cort char *cp, *ep, *anonpass, *upload_path, *src_addr;
14204203a83SThomas Cort const char *anonuser;
14304203a83SThomas Cort int dumbterm, isupload;
14404203a83SThomas Cort size_t len;
14504203a83SThomas Cort
14604203a83SThomas Cort tzset();
14704203a83SThomas Cort setlocale(LC_ALL, "");
14804203a83SThomas Cort setprogname(argv[0]);
14904203a83SThomas Cort
15004203a83SThomas Cort sigint_raised = 0;
15104203a83SThomas Cort
15204203a83SThomas Cort ftpport = "ftp";
15304203a83SThomas Cort httpport = "http";
15404203a83SThomas Cort #ifdef WITH_SSL
15504203a83SThomas Cort httpsport = "https";
15604203a83SThomas Cort #endif
15704203a83SThomas Cort gateport = NULL;
15804203a83SThomas Cort cp = getenv("FTPSERVERPORT");
15904203a83SThomas Cort if (cp != NULL)
16004203a83SThomas Cort gateport = cp;
16104203a83SThomas Cort else
16204203a83SThomas Cort gateport = "ftpgate";
16304203a83SThomas Cort doglob = 1;
16404203a83SThomas Cort interactive = 1;
16504203a83SThomas Cort autologin = 1;
16604203a83SThomas Cort passivemode = 1;
16704203a83SThomas Cort activefallback = 1;
16804203a83SThomas Cort preserve = 1;
16904203a83SThomas Cort verbose = 0;
17004203a83SThomas Cort progress = 0;
17104203a83SThomas Cort gatemode = 0;
17204203a83SThomas Cort data = -1;
17304203a83SThomas Cort outfile = NULL;
17404203a83SThomas Cort restartautofetch = 0;
17504203a83SThomas Cort #ifndef NO_EDITCOMPLETE
17604203a83SThomas Cort editing = 0;
17704203a83SThomas Cort el = NULL;
17804203a83SThomas Cort hist = NULL;
17904203a83SThomas Cort #endif
18004203a83SThomas Cort bytes = 0;
18104203a83SThomas Cort mark = HASHBYTES;
18204203a83SThomas Cort rate_get = 0;
18304203a83SThomas Cort rate_get_incr = DEFAULTINCR;
18404203a83SThomas Cort rate_put = 0;
18504203a83SThomas Cort rate_put_incr = DEFAULTINCR;
18604203a83SThomas Cort #ifdef INET6
18704203a83SThomas Cort epsv4 = 1;
18804203a83SThomas Cort epsv6 = 1;
18904203a83SThomas Cort #else
19004203a83SThomas Cort epsv4 = 0;
19104203a83SThomas Cort epsv6 = 0;
19204203a83SThomas Cort #endif
19304203a83SThomas Cort epsv4bad = 0;
19404203a83SThomas Cort epsv6bad = 0;
19504203a83SThomas Cort src_addr = NULL;
19604203a83SThomas Cort upload_path = NULL;
19704203a83SThomas Cort isupload = 0;
19804203a83SThomas Cort reply_callback = NULL;
19904203a83SThomas Cort #ifdef INET6
20004203a83SThomas Cort family = AF_UNSPEC;
20104203a83SThomas Cort #else
20204203a83SThomas Cort family = AF_INET; /* force AF_INET if no INET6 support */
20304203a83SThomas Cort #endif
20404203a83SThomas Cort
20504203a83SThomas Cort netrc[0] = '\0';
20604203a83SThomas Cort cp = getenv("NETRC");
20704203a83SThomas Cort if (cp != NULL && strlcpy(netrc, cp, sizeof(netrc)) >= sizeof(netrc))
20804203a83SThomas Cort errx(1, "$NETRC `%s': %s", cp, strerror(ENAMETOOLONG));
20904203a83SThomas Cort
21004203a83SThomas Cort marg_sl = ftp_sl_init();
21104203a83SThomas Cort if ((tmpdir = getenv("TMPDIR")) == NULL)
21204203a83SThomas Cort tmpdir = _PATH_TMP;
21304203a83SThomas Cort
21404203a83SThomas Cort /* Set default operation mode based on FTPMODE environment variable */
21504203a83SThomas Cort if ((cp = getenv("FTPMODE")) != NULL) {
21604203a83SThomas Cort if (strcasecmp(cp, "passive") == 0) {
21704203a83SThomas Cort passivemode = 1;
21804203a83SThomas Cort activefallback = 0;
21904203a83SThomas Cort } else if (strcasecmp(cp, "active") == 0) {
22004203a83SThomas Cort passivemode = 0;
22104203a83SThomas Cort activefallback = 0;
22204203a83SThomas Cort } else if (strcasecmp(cp, "gate") == 0) {
22304203a83SThomas Cort gatemode = 1;
22404203a83SThomas Cort } else if (strcasecmp(cp, "auto") == 0) {
22504203a83SThomas Cort passivemode = 1;
22604203a83SThomas Cort activefallback = 1;
22704203a83SThomas Cort } else
22804203a83SThomas Cort warnx("Unknown $FTPMODE `%s'; using defaults", cp);
22904203a83SThomas Cort }
23004203a83SThomas Cort
23104203a83SThomas Cort if (strcmp(getprogname(), "pftp") == 0) {
23204203a83SThomas Cort passivemode = 1;
23304203a83SThomas Cort activefallback = 0;
23404203a83SThomas Cort } else if (strcmp(getprogname(), "gate-ftp") == 0)
23504203a83SThomas Cort gatemode = 1;
23604203a83SThomas Cort
23704203a83SThomas Cort gateserver = getenv("FTPSERVER");
23804203a83SThomas Cort if (gateserver == NULL || *gateserver == '\0')
23904203a83SThomas Cort gateserver = GATE_SERVER;
24004203a83SThomas Cort if (gatemode) {
24104203a83SThomas Cort if (*gateserver == '\0') {
24204203a83SThomas Cort warnx(
24304203a83SThomas Cort "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
24404203a83SThomas Cort gatemode = 0;
24504203a83SThomas Cort }
24604203a83SThomas Cort }
24704203a83SThomas Cort
24804203a83SThomas Cort cp = getenv("TERM");
24904203a83SThomas Cort if (cp == NULL || strcmp(cp, "dumb") == 0)
25004203a83SThomas Cort dumbterm = 1;
25104203a83SThomas Cort else
25204203a83SThomas Cort dumbterm = 0;
25304203a83SThomas Cort fromatty = isatty(fileno(stdin));
25404203a83SThomas Cort ttyout = stdout;
25504203a83SThomas Cort if (isatty(fileno(ttyout))) {
25604203a83SThomas Cort verbose = 1; /* verbose if to a tty */
25704203a83SThomas Cort if (! dumbterm) {
25804203a83SThomas Cort #ifndef NO_EDITCOMPLETE
25904203a83SThomas Cort if (fromatty) /* editing mode on if tty is usable */
26004203a83SThomas Cort editing = 1;
26104203a83SThomas Cort #endif
26204203a83SThomas Cort #ifndef NO_PROGRESS
26304203a83SThomas Cort if (foregroundproc())
26404203a83SThomas Cort progress = 1; /* progress bar on if fg */
26504203a83SThomas Cort #endif
26604203a83SThomas Cort }
26704203a83SThomas Cort }
26804203a83SThomas Cort
269*0a6a1f1dSLionel Sambuc while ((ch = getopt(argc, argv, "46AadefginN:o:pP:q:r:Rs:tT:u:vVx:")) != -1) {
27004203a83SThomas Cort switch (ch) {
27104203a83SThomas Cort case '4':
27204203a83SThomas Cort family = AF_INET;
27304203a83SThomas Cort break;
27404203a83SThomas Cort
27504203a83SThomas Cort case '6':
27604203a83SThomas Cort #ifdef INET6
27704203a83SThomas Cort family = AF_INET6;
27804203a83SThomas Cort #else
27904203a83SThomas Cort warnx("INET6 support is not available; ignoring -6");
28004203a83SThomas Cort #endif
28104203a83SThomas Cort break;
28204203a83SThomas Cort
28304203a83SThomas Cort case 'A':
28404203a83SThomas Cort activefallback = 0;
28504203a83SThomas Cort passivemode = 0;
28604203a83SThomas Cort break;
28704203a83SThomas Cort
28804203a83SThomas Cort case 'a':
28904203a83SThomas Cort anonftp = 1;
29004203a83SThomas Cort break;
29104203a83SThomas Cort
29204203a83SThomas Cort case 'd':
29304203a83SThomas Cort options |= SO_DEBUG;
29404203a83SThomas Cort ftp_debug++;
29504203a83SThomas Cort break;
29604203a83SThomas Cort
29704203a83SThomas Cort case 'e':
29804203a83SThomas Cort #ifndef NO_EDITCOMPLETE
29904203a83SThomas Cort editing = 0;
30004203a83SThomas Cort #endif
30104203a83SThomas Cort break;
30204203a83SThomas Cort
30304203a83SThomas Cort case 'f':
30404203a83SThomas Cort flushcache = 1;
30504203a83SThomas Cort break;
30604203a83SThomas Cort
30704203a83SThomas Cort case 'g':
30804203a83SThomas Cort doglob = 0;
30904203a83SThomas Cort break;
31004203a83SThomas Cort
31104203a83SThomas Cort case 'i':
31204203a83SThomas Cort interactive = 0;
31304203a83SThomas Cort break;
31404203a83SThomas Cort
31504203a83SThomas Cort case 'n':
31604203a83SThomas Cort autologin = 0;
31704203a83SThomas Cort break;
31804203a83SThomas Cort
31904203a83SThomas Cort case 'N':
32004203a83SThomas Cort if (strlcpy(netrc, optarg, sizeof(netrc))
32104203a83SThomas Cort >= sizeof(netrc))
32204203a83SThomas Cort errx(1, "%s: %s", optarg,
32304203a83SThomas Cort strerror(ENAMETOOLONG));
32404203a83SThomas Cort break;
32504203a83SThomas Cort
32604203a83SThomas Cort case 'o':
32704203a83SThomas Cort outfile = optarg;
32804203a83SThomas Cort if (strcmp(outfile, "-") == 0)
32904203a83SThomas Cort ttyout = stderr;
33004203a83SThomas Cort break;
33104203a83SThomas Cort
33204203a83SThomas Cort case 'p':
33304203a83SThomas Cort passivemode = 1;
33404203a83SThomas Cort activefallback = 0;
33504203a83SThomas Cort break;
33604203a83SThomas Cort
33704203a83SThomas Cort case 'P':
33804203a83SThomas Cort ftpport = optarg;
33904203a83SThomas Cort break;
34004203a83SThomas Cort
34104203a83SThomas Cort case 'q':
34204203a83SThomas Cort quit_time = strtol(optarg, &ep, 10);
34304203a83SThomas Cort if (quit_time < 1 || *ep != '\0')
34404203a83SThomas Cort errx(1, "Bad quit value: %s", optarg);
34504203a83SThomas Cort break;
34604203a83SThomas Cort
34704203a83SThomas Cort case 'r':
34804203a83SThomas Cort retry_connect = strtol(optarg, &ep, 10);
34904203a83SThomas Cort if (retry_connect < 1 || *ep != '\0')
35004203a83SThomas Cort errx(1, "Bad retry value: %s", optarg);
35104203a83SThomas Cort break;
35204203a83SThomas Cort
35304203a83SThomas Cort case 'R':
35404203a83SThomas Cort restartautofetch = 1;
35504203a83SThomas Cort break;
35604203a83SThomas Cort
35704203a83SThomas Cort case 's':
35804203a83SThomas Cort src_addr = optarg;
35904203a83SThomas Cort break;
36004203a83SThomas Cort
36104203a83SThomas Cort case 't':
36204203a83SThomas Cort trace = 1;
36304203a83SThomas Cort break;
36404203a83SThomas Cort
36504203a83SThomas Cort case 'T':
36604203a83SThomas Cort {
36704203a83SThomas Cort int targc;
36804203a83SThomas Cort char *targv[6], *oac;
36904203a83SThomas Cort char cmdbuf[MAX_C_NAME];
37004203a83SThomas Cort
37104203a83SThomas Cort /* look for `dir,max[,incr]' */
37204203a83SThomas Cort targc = 0;
37304203a83SThomas Cort (void)strlcpy(cmdbuf, "-T", sizeof(cmdbuf));
37404203a83SThomas Cort targv[targc++] = cmdbuf;
37504203a83SThomas Cort oac = ftp_strdup(optarg);
37604203a83SThomas Cort
37704203a83SThomas Cort while ((cp = strsep(&oac, ",")) != NULL) {
37804203a83SThomas Cort if (*cp == '\0') {
37904203a83SThomas Cort warnx("Bad throttle value `%s'",
38004203a83SThomas Cort optarg);
38104203a83SThomas Cort usage();
38204203a83SThomas Cort /* NOTREACHED */
38304203a83SThomas Cort }
38404203a83SThomas Cort targv[targc++] = cp;
38504203a83SThomas Cort if (targc >= 5)
38604203a83SThomas Cort break;
38704203a83SThomas Cort }
38804203a83SThomas Cort if (parserate(targc, targv, 1) == -1)
38904203a83SThomas Cort usage();
39004203a83SThomas Cort free(oac);
39104203a83SThomas Cort break;
39204203a83SThomas Cort }
39304203a83SThomas Cort
39404203a83SThomas Cort case 'u':
39504203a83SThomas Cort {
39604203a83SThomas Cort isupload = 1;
39704203a83SThomas Cort interactive = 0;
39804203a83SThomas Cort upload_path = ftp_strdup(optarg);
39904203a83SThomas Cort
40004203a83SThomas Cort break;
40104203a83SThomas Cort }
40204203a83SThomas Cort
40304203a83SThomas Cort case 'v':
40404203a83SThomas Cort progress = verbose = 1;
40504203a83SThomas Cort break;
40604203a83SThomas Cort
40704203a83SThomas Cort case 'V':
40804203a83SThomas Cort progress = verbose = 0;
40904203a83SThomas Cort break;
41004203a83SThomas Cort
411*0a6a1f1dSLionel Sambuc case 'x':
412*0a6a1f1dSLionel Sambuc sndbuf_size = strsuftoi(optarg);
413*0a6a1f1dSLionel Sambuc if (sndbuf_size < 1)
414*0a6a1f1dSLionel Sambuc errx(1, "Bad xferbuf value: %s", optarg);
415*0a6a1f1dSLionel Sambuc rcvbuf_size = sndbuf_size;
416*0a6a1f1dSLionel Sambuc break;
417*0a6a1f1dSLionel Sambuc
41804203a83SThomas Cort default:
41904203a83SThomas Cort usage();
42004203a83SThomas Cort }
42104203a83SThomas Cort }
42204203a83SThomas Cort /* set line buffering on ttyout */
42304203a83SThomas Cort setvbuf(ttyout, NULL, _IOLBF, 0);
42404203a83SThomas Cort argc -= optind;
42504203a83SThomas Cort argv += optind;
42604203a83SThomas Cort
42704203a83SThomas Cort cpend = 0; /* no pending replies */
42804203a83SThomas Cort proxy = 0; /* proxy not active */
42904203a83SThomas Cort crflag = 1; /* strip c.r. on ascii gets */
43004203a83SThomas Cort sendport = -1; /* not using ports */
43104203a83SThomas Cort
43204203a83SThomas Cort if (src_addr != NULL) {
43304203a83SThomas Cort struct addrinfo hints;
43404203a83SThomas Cort int error;
43504203a83SThomas Cort
43604203a83SThomas Cort memset(&hints, 0, sizeof(hints));
43704203a83SThomas Cort hints.ai_family = family;
43804203a83SThomas Cort hints.ai_socktype = SOCK_STREAM;
43904203a83SThomas Cort hints.ai_flags = AI_PASSIVE;
44004203a83SThomas Cort error = getaddrinfo(src_addr, NULL, &hints, &bindai);
44104203a83SThomas Cort if (error) {
44204203a83SThomas Cort errx(1, "Can't lookup `%s': %s", src_addr,
44304203a83SThomas Cort (error == EAI_SYSTEM) ? strerror(errno)
44404203a83SThomas Cort : gai_strerror(error));
44504203a83SThomas Cort }
44604203a83SThomas Cort }
44704203a83SThomas Cort
44804203a83SThomas Cort /*
44904203a83SThomas Cort * Cache the user name and home directory.
45004203a83SThomas Cort */
45104203a83SThomas Cort localhome = NULL;
45204203a83SThomas Cort localname = NULL;
45304203a83SThomas Cort anonuser = "anonymous";
45404203a83SThomas Cort cp = getenv("HOME");
45504203a83SThomas Cort if (! EMPTYSTRING(cp))
45604203a83SThomas Cort localhome = ftp_strdup(cp);
45704203a83SThomas Cort pw = NULL;
45804203a83SThomas Cort cp = getlogin();
45904203a83SThomas Cort if (cp != NULL)
46004203a83SThomas Cort pw = getpwnam(cp);
46104203a83SThomas Cort if (pw == NULL)
46204203a83SThomas Cort pw = getpwuid(getuid());
46304203a83SThomas Cort if (pw != NULL) {
46404203a83SThomas Cort if (localhome == NULL && !EMPTYSTRING(pw->pw_dir))
46504203a83SThomas Cort localhome = ftp_strdup(pw->pw_dir);
46604203a83SThomas Cort localname = ftp_strdup(pw->pw_name);
46704203a83SThomas Cort anonuser = localname;
46804203a83SThomas Cort }
46904203a83SThomas Cort if (netrc[0] == '\0' && localhome != NULL) {
47004203a83SThomas Cort if (strlcpy(netrc, localhome, sizeof(netrc)) >= sizeof(netrc) ||
47104203a83SThomas Cort strlcat(netrc, "/.netrc", sizeof(netrc)) >= sizeof(netrc)) {
47204203a83SThomas Cort warnx("%s/.netrc: %s", localhome,
47304203a83SThomas Cort strerror(ENAMETOOLONG));
47404203a83SThomas Cort netrc[0] = '\0';
47504203a83SThomas Cort }
47604203a83SThomas Cort }
47704203a83SThomas Cort if (localhome == NULL)
47804203a83SThomas Cort localhome = ftp_strdup("/");
47904203a83SThomas Cort
48004203a83SThomas Cort /*
48104203a83SThomas Cort * Every anonymous FTP server I've encountered will accept the
48204203a83SThomas Cort * string "username@", and will append the hostname itself. We
48304203a83SThomas Cort * do this by default since many servers are picky about not
48404203a83SThomas Cort * having a FQDN in the anonymous password.
48504203a83SThomas Cort * - thorpej@NetBSD.org
48604203a83SThomas Cort */
48704203a83SThomas Cort len = strlen(anonuser) + 2;
48804203a83SThomas Cort anonpass = ftp_malloc(len);
48904203a83SThomas Cort (void)strlcpy(anonpass, anonuser, len);
49004203a83SThomas Cort (void)strlcat(anonpass, "@", len);
49104203a83SThomas Cort
49204203a83SThomas Cort /*
49304203a83SThomas Cort * set all the defaults for options defined in
49404203a83SThomas Cort * struct option optiontab[] declared in cmdtab.c
49504203a83SThomas Cort */
49604203a83SThomas Cort setupoption("anonpass", getenv("FTPANONPASS"), anonpass);
49704203a83SThomas Cort setupoption("ftp_proxy", getenv(FTP_PROXY), "");
49804203a83SThomas Cort setupoption("http_proxy", getenv(HTTP_PROXY), "");
49904203a83SThomas Cort setupoption("https_proxy", getenv(HTTPS_PROXY), "");
50004203a83SThomas Cort setupoption("no_proxy", getenv(NO_PROXY), "");
50104203a83SThomas Cort setupoption("pager", getenv("PAGER"), DEFAULTPAGER);
50204203a83SThomas Cort setupoption("prompt", getenv("FTPPROMPT"), DEFAULTPROMPT);
50304203a83SThomas Cort setupoption("rprompt", getenv("FTPRPROMPT"), DEFAULTRPROMPT);
50404203a83SThomas Cort
50504203a83SThomas Cort free(anonpass);
50604203a83SThomas Cort
50704203a83SThomas Cort setttywidth(0);
50804203a83SThomas Cort #ifdef SIGINFO
50904203a83SThomas Cort (void)xsignal(SIGINFO, psummary);
51004203a83SThomas Cort #endif
51104203a83SThomas Cort (void)xsignal(SIGQUIT, psummary);
51204203a83SThomas Cort (void)xsignal(SIGUSR1, crankrate);
51304203a83SThomas Cort (void)xsignal(SIGUSR2, crankrate);
51404203a83SThomas Cort (void)xsignal(SIGWINCH, setttywidth);
51504203a83SThomas Cort
51604203a83SThomas Cort if (argc > 0) {
51704203a83SThomas Cort if (isupload) {
51804203a83SThomas Cort rval = auto_put(argc, argv, upload_path);
51904203a83SThomas Cort sigint_or_rval_exit:
52004203a83SThomas Cort if (sigint_raised) {
52104203a83SThomas Cort (void)xsignal(SIGINT, SIG_DFL);
52204203a83SThomas Cort raise(SIGINT);
52304203a83SThomas Cort }
52404203a83SThomas Cort exit(rval);
52504203a83SThomas Cort } else if (strchr(argv[0], ':') != NULL
52604203a83SThomas Cort && ! isipv6addr(argv[0])) {
52704203a83SThomas Cort rval = auto_fetch(argc, argv);
52804203a83SThomas Cort if (rval >= 0) /* -1 == connected and cd-ed */
52904203a83SThomas Cort goto sigint_or_rval_exit;
53004203a83SThomas Cort } else {
53104203a83SThomas Cort char *xargv[4], *uuser, *host;
53204203a83SThomas Cort char cmdbuf[MAXPATHLEN];
53304203a83SThomas Cort
53404203a83SThomas Cort if ((rval = sigsetjmp(toplevel, 1)))
53504203a83SThomas Cort goto sigint_or_rval_exit;
53604203a83SThomas Cort (void)xsignal(SIGINT, intr);
53704203a83SThomas Cort (void)xsignal(SIGPIPE, lostpeer);
53804203a83SThomas Cort uuser = NULL;
53904203a83SThomas Cort host = argv[0];
54004203a83SThomas Cort cp = strchr(host, '@');
54104203a83SThomas Cort if (cp) {
54204203a83SThomas Cort *cp = '\0';
54304203a83SThomas Cort uuser = host;
54404203a83SThomas Cort host = cp + 1;
54504203a83SThomas Cort }
54604203a83SThomas Cort (void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
54704203a83SThomas Cort xargv[0] = cmdbuf;
54804203a83SThomas Cort xargv[1] = host;
54904203a83SThomas Cort xargv[2] = argv[1];
55004203a83SThomas Cort xargv[3] = NULL;
55104203a83SThomas Cort do {
55204203a83SThomas Cort int oautologin;
55304203a83SThomas Cort
55404203a83SThomas Cort oautologin = autologin;
55504203a83SThomas Cort if (uuser != NULL) {
55604203a83SThomas Cort anonftp = 0;
55704203a83SThomas Cort autologin = 0;
55804203a83SThomas Cort }
55904203a83SThomas Cort setpeer(argc+1, xargv);
56004203a83SThomas Cort autologin = oautologin;
56104203a83SThomas Cort if (connected == 1 && uuser != NULL)
56204203a83SThomas Cort (void)ftp_login(host, uuser, NULL);
56304203a83SThomas Cort if (!retry_connect)
56404203a83SThomas Cort break;
56504203a83SThomas Cort if (!connected) {
56604203a83SThomas Cort macnum = 0;
56704203a83SThomas Cort fprintf(ttyout,
56804203a83SThomas Cort "Retrying in %d seconds...\n",
56904203a83SThomas Cort retry_connect);
57004203a83SThomas Cort sleep(retry_connect);
57104203a83SThomas Cort }
57204203a83SThomas Cort } while (!connected);
57304203a83SThomas Cort retry_connect = 0; /* connected, stop hiding msgs */
57404203a83SThomas Cort }
57504203a83SThomas Cort }
57604203a83SThomas Cort if (isupload)
57704203a83SThomas Cort usage();
57804203a83SThomas Cort
57904203a83SThomas Cort #ifndef NO_EDITCOMPLETE
58004203a83SThomas Cort controlediting();
58104203a83SThomas Cort #endif /* !NO_EDITCOMPLETE */
58204203a83SThomas Cort
58304203a83SThomas Cort (void)sigsetjmp(toplevel, 1);
58404203a83SThomas Cort (void)xsignal(SIGINT, intr);
58504203a83SThomas Cort (void)xsignal(SIGPIPE, lostpeer);
58604203a83SThomas Cort for (;;)
58704203a83SThomas Cort cmdscanner();
58804203a83SThomas Cort }
58904203a83SThomas Cort
59004203a83SThomas Cort /*
59104203a83SThomas Cort * Generate a prompt
59204203a83SThomas Cort */
59304203a83SThomas Cort char *
prompt(void)59404203a83SThomas Cort prompt(void)
59504203a83SThomas Cort {
59604203a83SThomas Cort static char **promptopt;
59704203a83SThomas Cort static char buf[MAXPATHLEN];
59804203a83SThomas Cort
59904203a83SThomas Cort if (promptopt == NULL) {
60004203a83SThomas Cort struct option *o;
60104203a83SThomas Cort
60204203a83SThomas Cort o = getoption("prompt");
60304203a83SThomas Cort if (o == NULL)
60404203a83SThomas Cort errx(1, "prompt: no such option `prompt'");
60504203a83SThomas Cort promptopt = &(o->value);
60604203a83SThomas Cort }
60704203a83SThomas Cort formatbuf(buf, sizeof(buf), *promptopt ? *promptopt : DEFAULTPROMPT);
60804203a83SThomas Cort return (buf);
60904203a83SThomas Cort }
61004203a83SThomas Cort
61104203a83SThomas Cort /*
61204203a83SThomas Cort * Generate an rprompt
61304203a83SThomas Cort */
61404203a83SThomas Cort char *
rprompt(void)61504203a83SThomas Cort rprompt(void)
61604203a83SThomas Cort {
61704203a83SThomas Cort static char **rpromptopt;
61804203a83SThomas Cort static char buf[MAXPATHLEN];
61904203a83SThomas Cort
62004203a83SThomas Cort if (rpromptopt == NULL) {
62104203a83SThomas Cort struct option *o;
62204203a83SThomas Cort
62304203a83SThomas Cort o = getoption("rprompt");
62404203a83SThomas Cort if (o == NULL)
62504203a83SThomas Cort errx(1, "rprompt: no such option `rprompt'");
62604203a83SThomas Cort rpromptopt = &(o->value);
62704203a83SThomas Cort }
62804203a83SThomas Cort formatbuf(buf, sizeof(buf), *rpromptopt ? *rpromptopt : DEFAULTRPROMPT);
62904203a83SThomas Cort return (buf);
63004203a83SThomas Cort }
63104203a83SThomas Cort
63204203a83SThomas Cort /*
63304203a83SThomas Cort * Command parser.
63404203a83SThomas Cort */
63504203a83SThomas Cort void
cmdscanner(void)63604203a83SThomas Cort cmdscanner(void)
63704203a83SThomas Cort {
63804203a83SThomas Cort struct cmd *c;
63904203a83SThomas Cort char *p;
64004203a83SThomas Cort #ifndef NO_EDITCOMPLETE
64104203a83SThomas Cort int ch;
64204203a83SThomas Cort size_t num;
64304203a83SThomas Cort #endif
64404203a83SThomas Cort int len;
64504203a83SThomas Cort char cmdbuf[MAX_C_NAME];
64604203a83SThomas Cort
64704203a83SThomas Cort for (;;) {
64804203a83SThomas Cort #ifndef NO_EDITCOMPLETE
64904203a83SThomas Cort if (!editing) {
65004203a83SThomas Cort #endif /* !NO_EDITCOMPLETE */
65104203a83SThomas Cort if (fromatty) {
65204203a83SThomas Cort fputs(prompt(), ttyout);
65304203a83SThomas Cort p = rprompt();
65404203a83SThomas Cort if (*p)
65504203a83SThomas Cort fprintf(ttyout, "%s ", p);
65604203a83SThomas Cort }
65704203a83SThomas Cort (void)fflush(ttyout);
65804203a83SThomas Cort len = get_line(stdin, line, sizeof(line), NULL);
65904203a83SThomas Cort switch (len) {
66004203a83SThomas Cort case -1: /* EOF */
66104203a83SThomas Cort case -2: /* error */
66204203a83SThomas Cort if (fromatty)
66304203a83SThomas Cort putc('\n', ttyout);
66404203a83SThomas Cort quit(0, NULL);
66504203a83SThomas Cort /* NOTREACHED */
66604203a83SThomas Cort case -3: /* too long; try again */
66704203a83SThomas Cort fputs("Sorry, input line is too long.\n",
66804203a83SThomas Cort ttyout);
66904203a83SThomas Cort continue;
67004203a83SThomas Cort case 0: /* empty; try again */
67104203a83SThomas Cort continue;
67204203a83SThomas Cort default: /* all ok */
67304203a83SThomas Cort break;
67404203a83SThomas Cort }
67504203a83SThomas Cort #ifndef NO_EDITCOMPLETE
67604203a83SThomas Cort } else {
67704203a83SThomas Cort const char *buf;
67804203a83SThomas Cort HistEvent ev;
67904203a83SThomas Cort cursor_pos = NULL;
68004203a83SThomas Cort
68104203a83SThomas Cort buf = el_gets(el, &ch);
68204203a83SThomas Cort num = ch;
68304203a83SThomas Cort if (buf == NULL || num == 0) {
68404203a83SThomas Cort if (fromatty)
68504203a83SThomas Cort putc('\n', ttyout);
68604203a83SThomas Cort quit(0, NULL);
68704203a83SThomas Cort }
68804203a83SThomas Cort if (num >= sizeof(line)) {
68904203a83SThomas Cort fputs("Sorry, input line is too long.\n",
69004203a83SThomas Cort ttyout);
69104203a83SThomas Cort break;
69204203a83SThomas Cort }
69304203a83SThomas Cort memcpy(line, buf, num);
69404203a83SThomas Cort if (line[--num] == '\n') {
69504203a83SThomas Cort line[num] = '\0';
69604203a83SThomas Cort if (num == 0)
69704203a83SThomas Cort break;
69804203a83SThomas Cort }
69904203a83SThomas Cort history(hist, &ev, H_ENTER, buf);
70004203a83SThomas Cort }
70104203a83SThomas Cort #endif /* !NO_EDITCOMPLETE */
70204203a83SThomas Cort
70304203a83SThomas Cort makeargv();
70404203a83SThomas Cort if (margc == 0)
70504203a83SThomas Cort continue;
70604203a83SThomas Cort c = getcmd(margv[0]);
70704203a83SThomas Cort if (c == (struct cmd *)-1) {
70804203a83SThomas Cort fputs("?Ambiguous command.\n", ttyout);
70904203a83SThomas Cort continue;
71004203a83SThomas Cort }
71104203a83SThomas Cort if (c == NULL) {
71204203a83SThomas Cort #if !defined(NO_EDITCOMPLETE)
71304203a83SThomas Cort /*
71404203a83SThomas Cort * attempt to el_parse() unknown commands.
71504203a83SThomas Cort * any command containing a ':' would be parsed
71604203a83SThomas Cort * as "[prog:]cmd ...", and will result in a
71704203a83SThomas Cort * false positive if prog != "ftp", so treat
71804203a83SThomas Cort * such commands as invalid.
71904203a83SThomas Cort */
72004203a83SThomas Cort if (strchr(margv[0], ':') != NULL ||
72104203a83SThomas Cort !editing ||
72204203a83SThomas Cort el_parse(el, margc, (void *)margv) != 0)
72304203a83SThomas Cort #endif /* !NO_EDITCOMPLETE */
72404203a83SThomas Cort fputs("?Invalid command.\n", ttyout);
72504203a83SThomas Cort continue;
72604203a83SThomas Cort }
72704203a83SThomas Cort if (c->c_conn && !connected) {
72804203a83SThomas Cort fputs("Not connected.\n", ttyout);
72904203a83SThomas Cort continue;
73004203a83SThomas Cort }
73104203a83SThomas Cort confirmrest = 0;
73204203a83SThomas Cort (void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
73304203a83SThomas Cort margv[0] = cmdbuf;
73404203a83SThomas Cort (*c->c_handler)(margc, margv);
73504203a83SThomas Cort if (bell && c->c_bell)
73604203a83SThomas Cort (void)putc('\007', ttyout);
73704203a83SThomas Cort if (c->c_handler != help)
73804203a83SThomas Cort break;
73904203a83SThomas Cort }
74004203a83SThomas Cort (void)xsignal(SIGINT, intr);
74104203a83SThomas Cort (void)xsignal(SIGPIPE, lostpeer);
74204203a83SThomas Cort }
74304203a83SThomas Cort
74404203a83SThomas Cort struct cmd *
getcmd(const char * name)74504203a83SThomas Cort getcmd(const char *name)
74604203a83SThomas Cort {
74704203a83SThomas Cort const char *p, *q;
74804203a83SThomas Cort struct cmd *c, *found;
74904203a83SThomas Cort int nmatches, longest;
75004203a83SThomas Cort
75104203a83SThomas Cort if (name == NULL)
75204203a83SThomas Cort return (0);
75304203a83SThomas Cort
75404203a83SThomas Cort longest = 0;
75504203a83SThomas Cort nmatches = 0;
75604203a83SThomas Cort found = 0;
75704203a83SThomas Cort for (c = cmdtab; (p = c->c_name) != NULL; c++) {
75804203a83SThomas Cort for (q = name; *q == *p++; q++)
75904203a83SThomas Cort if (*q == 0) /* exact match? */
76004203a83SThomas Cort return (c);
76104203a83SThomas Cort if (!*q) { /* the name was a prefix */
76204203a83SThomas Cort if (q - name > longest) {
76304203a83SThomas Cort longest = q - name;
76404203a83SThomas Cort nmatches = 1;
76504203a83SThomas Cort found = c;
76604203a83SThomas Cort } else if (q - name == longest)
76704203a83SThomas Cort nmatches++;
76804203a83SThomas Cort }
76904203a83SThomas Cort }
77004203a83SThomas Cort if (nmatches > 1)
77104203a83SThomas Cort return ((struct cmd *)-1);
77204203a83SThomas Cort return (found);
77304203a83SThomas Cort }
77404203a83SThomas Cort
77504203a83SThomas Cort /*
77604203a83SThomas Cort * Slice a string up into argc/argv.
77704203a83SThomas Cort */
77804203a83SThomas Cort
77904203a83SThomas Cort int slrflag;
78004203a83SThomas Cort
78104203a83SThomas Cort void
makeargv(void)78204203a83SThomas Cort makeargv(void)
78304203a83SThomas Cort {
78404203a83SThomas Cort char *argp;
78504203a83SThomas Cort
78604203a83SThomas Cort stringbase = line; /* scan from first of buffer */
78704203a83SThomas Cort argbase = argbuf; /* store from first of buffer */
78804203a83SThomas Cort slrflag = 0;
78904203a83SThomas Cort marg_sl->sl_cur = 0; /* reset to start of marg_sl */
79004203a83SThomas Cort for (margc = 0; ; margc++) {
79104203a83SThomas Cort argp = slurpstring();
79204203a83SThomas Cort ftp_sl_add(marg_sl, argp);
79304203a83SThomas Cort if (argp == NULL)
79404203a83SThomas Cort break;
79504203a83SThomas Cort }
79604203a83SThomas Cort #ifndef NO_EDITCOMPLETE
79704203a83SThomas Cort if (cursor_pos == line) {
79804203a83SThomas Cort cursor_argc = 0;
79904203a83SThomas Cort cursor_argo = 0;
80004203a83SThomas Cort } else if (cursor_pos != NULL) {
80104203a83SThomas Cort cursor_argc = margc;
80204203a83SThomas Cort cursor_argo = strlen(margv[margc-1]);
80304203a83SThomas Cort }
80404203a83SThomas Cort #endif /* !NO_EDITCOMPLETE */
80504203a83SThomas Cort }
80604203a83SThomas Cort
80704203a83SThomas Cort #ifdef NO_EDITCOMPLETE
80804203a83SThomas Cort #define INC_CHKCURSOR(x) (x)++
80904203a83SThomas Cort #else /* !NO_EDITCOMPLETE */
81004203a83SThomas Cort #define INC_CHKCURSOR(x) { (x)++ ; \
81104203a83SThomas Cort if (x == cursor_pos) { \
81204203a83SThomas Cort cursor_argc = margc; \
81304203a83SThomas Cort cursor_argo = ap-argbase; \
81404203a83SThomas Cort cursor_pos = NULL; \
81504203a83SThomas Cort } }
81604203a83SThomas Cort
81704203a83SThomas Cort #endif /* !NO_EDITCOMPLETE */
81804203a83SThomas Cort
81904203a83SThomas Cort /*
82004203a83SThomas Cort * Parse string into argbuf;
82104203a83SThomas Cort * implemented with FSM to
82204203a83SThomas Cort * handle quoting and strings
82304203a83SThomas Cort */
82404203a83SThomas Cort char *
slurpstring(void)82504203a83SThomas Cort slurpstring(void)
82604203a83SThomas Cort {
82704203a83SThomas Cort static char bangstr[2] = { '!', '\0' };
82804203a83SThomas Cort static char dollarstr[2] = { '$', '\0' };
82904203a83SThomas Cort int got_one = 0;
83004203a83SThomas Cort char *sb = stringbase;
83104203a83SThomas Cort char *ap = argbase;
83204203a83SThomas Cort char *tmp = argbase; /* will return this if token found */
83304203a83SThomas Cort
83404203a83SThomas Cort if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
83504203a83SThomas Cort switch (slrflag) { /* and $ as token for macro invoke */
83604203a83SThomas Cort case 0:
83704203a83SThomas Cort slrflag++;
83804203a83SThomas Cort INC_CHKCURSOR(stringbase);
83904203a83SThomas Cort return ((*sb == '!') ? bangstr : dollarstr);
84004203a83SThomas Cort /* NOTREACHED */
84104203a83SThomas Cort case 1:
84204203a83SThomas Cort slrflag++;
84304203a83SThomas Cort altarg = stringbase;
84404203a83SThomas Cort break;
84504203a83SThomas Cort default:
84604203a83SThomas Cort break;
84704203a83SThomas Cort }
84804203a83SThomas Cort }
84904203a83SThomas Cort
85004203a83SThomas Cort S0:
85104203a83SThomas Cort switch (*sb) {
85204203a83SThomas Cort
85304203a83SThomas Cort case '\0':
85404203a83SThomas Cort goto OUT;
85504203a83SThomas Cort
85604203a83SThomas Cort case ' ':
85704203a83SThomas Cort case '\t':
85804203a83SThomas Cort INC_CHKCURSOR(sb);
85904203a83SThomas Cort goto S0;
86004203a83SThomas Cort
86104203a83SThomas Cort default:
86204203a83SThomas Cort switch (slrflag) {
86304203a83SThomas Cort case 0:
86404203a83SThomas Cort slrflag++;
86504203a83SThomas Cort break;
86604203a83SThomas Cort case 1:
86704203a83SThomas Cort slrflag++;
86804203a83SThomas Cort altarg = sb;
86904203a83SThomas Cort break;
87004203a83SThomas Cort default:
87104203a83SThomas Cort break;
87204203a83SThomas Cort }
87304203a83SThomas Cort goto S1;
87404203a83SThomas Cort }
87504203a83SThomas Cort
87604203a83SThomas Cort S1:
87704203a83SThomas Cort switch (*sb) {
87804203a83SThomas Cort
87904203a83SThomas Cort case ' ':
88004203a83SThomas Cort case '\t':
88104203a83SThomas Cort case '\0':
88204203a83SThomas Cort goto OUT; /* end of token */
88304203a83SThomas Cort
88404203a83SThomas Cort case '\\':
88504203a83SThomas Cort INC_CHKCURSOR(sb);
88604203a83SThomas Cort goto S2; /* slurp next character */
88704203a83SThomas Cort
88804203a83SThomas Cort case '"':
88904203a83SThomas Cort INC_CHKCURSOR(sb);
89004203a83SThomas Cort goto S3; /* slurp quoted string */
89104203a83SThomas Cort
89204203a83SThomas Cort default:
89304203a83SThomas Cort *ap = *sb; /* add character to token */
89404203a83SThomas Cort ap++;
89504203a83SThomas Cort INC_CHKCURSOR(sb);
89604203a83SThomas Cort got_one = 1;
89704203a83SThomas Cort goto S1;
89804203a83SThomas Cort }
89904203a83SThomas Cort
90004203a83SThomas Cort S2:
90104203a83SThomas Cort switch (*sb) {
90204203a83SThomas Cort
90304203a83SThomas Cort case '\0':
90404203a83SThomas Cort goto OUT;
90504203a83SThomas Cort
90604203a83SThomas Cort default:
90704203a83SThomas Cort *ap = *sb;
90804203a83SThomas Cort ap++;
90904203a83SThomas Cort INC_CHKCURSOR(sb);
91004203a83SThomas Cort got_one = 1;
91104203a83SThomas Cort goto S1;
91204203a83SThomas Cort }
91304203a83SThomas Cort
91404203a83SThomas Cort S3:
91504203a83SThomas Cort switch (*sb) {
91604203a83SThomas Cort
91704203a83SThomas Cort case '\0':
91804203a83SThomas Cort goto OUT;
91904203a83SThomas Cort
92004203a83SThomas Cort case '"':
92104203a83SThomas Cort INC_CHKCURSOR(sb);
92204203a83SThomas Cort goto S1;
92304203a83SThomas Cort
92404203a83SThomas Cort default:
92504203a83SThomas Cort *ap = *sb;
92604203a83SThomas Cort ap++;
92704203a83SThomas Cort INC_CHKCURSOR(sb);
92804203a83SThomas Cort got_one = 1;
92904203a83SThomas Cort goto S3;
93004203a83SThomas Cort }
93104203a83SThomas Cort
93204203a83SThomas Cort OUT:
93304203a83SThomas Cort if (got_one)
93404203a83SThomas Cort *ap++ = '\0';
93504203a83SThomas Cort argbase = ap; /* update storage pointer */
93604203a83SThomas Cort stringbase = sb; /* update scan pointer */
93704203a83SThomas Cort if (got_one) {
93804203a83SThomas Cort return (tmp);
93904203a83SThomas Cort }
94004203a83SThomas Cort switch (slrflag) {
94104203a83SThomas Cort case 0:
94204203a83SThomas Cort slrflag++;
94304203a83SThomas Cort break;
94404203a83SThomas Cort case 1:
94504203a83SThomas Cort slrflag++;
94604203a83SThomas Cort altarg = NULL;
94704203a83SThomas Cort break;
94804203a83SThomas Cort default:
94904203a83SThomas Cort break;
95004203a83SThomas Cort }
95104203a83SThomas Cort return (NULL);
95204203a83SThomas Cort }
95304203a83SThomas Cort
95404203a83SThomas Cort /*
95504203a83SThomas Cort * Help/usage command.
95604203a83SThomas Cort * Call each command handler with argc == 0 and argv[0] == name.
95704203a83SThomas Cort */
95804203a83SThomas Cort void
help(int argc,char * argv[])95904203a83SThomas Cort help(int argc, char *argv[])
96004203a83SThomas Cort {
96104203a83SThomas Cort struct cmd *c;
96204203a83SThomas Cort char *nargv[1], *cmd;
96304203a83SThomas Cort const char *p;
96404203a83SThomas Cort int isusage;
96504203a83SThomas Cort
96604203a83SThomas Cort cmd = argv[0];
96704203a83SThomas Cort isusage = (strcmp(cmd, "usage") == 0);
96804203a83SThomas Cort if (argc == 0 || (isusage && argc == 1)) {
96904203a83SThomas Cort UPRINTF("usage: %s [command [...]]\n", cmd);
97004203a83SThomas Cort return;
97104203a83SThomas Cort }
97204203a83SThomas Cort if (argc == 1) {
97304203a83SThomas Cort StringList *buf;
97404203a83SThomas Cort
97504203a83SThomas Cort buf = ftp_sl_init();
97604203a83SThomas Cort fprintf(ttyout,
97704203a83SThomas Cort "%sommands may be abbreviated. Commands are:\n\n",
97804203a83SThomas Cort proxy ? "Proxy c" : "C");
97904203a83SThomas Cort for (c = cmdtab; (p = c->c_name) != NULL; c++)
98004203a83SThomas Cort if (!proxy || c->c_proxy)
98104203a83SThomas Cort ftp_sl_add(buf, ftp_strdup(p));
98204203a83SThomas Cort list_vertical(buf);
98304203a83SThomas Cort sl_free(buf, 1);
98404203a83SThomas Cort return;
98504203a83SThomas Cort }
98604203a83SThomas Cort
98704203a83SThomas Cort #define HELPINDENT ((int) sizeof("disconnect"))
98804203a83SThomas Cort
98904203a83SThomas Cort while (--argc > 0) {
99004203a83SThomas Cort char *arg;
99104203a83SThomas Cort char cmdbuf[MAX_C_NAME];
99204203a83SThomas Cort
99304203a83SThomas Cort arg = *++argv;
99404203a83SThomas Cort c = getcmd(arg);
99504203a83SThomas Cort if (c == (struct cmd *)-1)
99604203a83SThomas Cort fprintf(ttyout, "?Ambiguous %s command `%s'\n",
99704203a83SThomas Cort cmd, arg);
99804203a83SThomas Cort else if (c == NULL)
99904203a83SThomas Cort fprintf(ttyout, "?Invalid %s command `%s'\n",
100004203a83SThomas Cort cmd, arg);
100104203a83SThomas Cort else {
100204203a83SThomas Cort if (isusage) {
100304203a83SThomas Cort (void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
100404203a83SThomas Cort nargv[0] = cmdbuf;
100504203a83SThomas Cort (*c->c_handler)(0, nargv);
100604203a83SThomas Cort } else
100704203a83SThomas Cort fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
100804203a83SThomas Cort c->c_name, c->c_help);
100904203a83SThomas Cort }
101004203a83SThomas Cort }
101104203a83SThomas Cort }
101204203a83SThomas Cort
101304203a83SThomas Cort struct option *
getoption(const char * name)101404203a83SThomas Cort getoption(const char *name)
101504203a83SThomas Cort {
101604203a83SThomas Cort const char *p;
101704203a83SThomas Cort struct option *c;
101804203a83SThomas Cort
101904203a83SThomas Cort if (name == NULL)
102004203a83SThomas Cort return (NULL);
102104203a83SThomas Cort for (c = optiontab; (p = c->name) != NULL; c++) {
102204203a83SThomas Cort if (strcasecmp(p, name) == 0)
102304203a83SThomas Cort return (c);
102404203a83SThomas Cort }
102504203a83SThomas Cort return (NULL);
102604203a83SThomas Cort }
102704203a83SThomas Cort
102804203a83SThomas Cort char *
getoptionvalue(const char * name)102904203a83SThomas Cort getoptionvalue(const char *name)
103004203a83SThomas Cort {
103104203a83SThomas Cort struct option *c;
103204203a83SThomas Cort
103304203a83SThomas Cort if (name == NULL)
103404203a83SThomas Cort errx(1, "getoptionvalue: invoked with NULL name");
103504203a83SThomas Cort c = getoption(name);
103604203a83SThomas Cort if (c != NULL)
103704203a83SThomas Cort return (c->value);
103804203a83SThomas Cort errx(1, "getoptionvalue: invoked with unknown option `%s'", name);
103904203a83SThomas Cort /* NOTREACHED */
104004203a83SThomas Cort }
104104203a83SThomas Cort
104204203a83SThomas Cort static void
setupoption(const char * name,const char * value,const char * defaultvalue)104304203a83SThomas Cort setupoption(const char *name, const char *value, const char *defaultvalue)
104404203a83SThomas Cort {
104504203a83SThomas Cort set_option(name, value ? value : defaultvalue, 0);
104604203a83SThomas Cort }
104704203a83SThomas Cort
104804203a83SThomas Cort void
usage(void)104904203a83SThomas Cort usage(void)
105004203a83SThomas Cort {
105104203a83SThomas Cort const char *progname = getprogname();
105204203a83SThomas Cort
105304203a83SThomas Cort (void)fprintf(stderr,
105404203a83SThomas Cort "usage: %s [-46AadefginpRtVv] [-N netrc] [-o outfile] [-P port] [-q quittime]\n"
1055*0a6a1f1dSLionel Sambuc " [-r retry] [-s srcaddr] [-T dir,max[,inc]] [-x xferbufsize]\n"
105604203a83SThomas Cort " [[user@]host [port]] [host:path[/]] [file:///file]\n"
105704203a83SThomas Cort " [ftp://[user[:pass]@]host[:port]/path[/]]\n"
105804203a83SThomas Cort " [http://[user[:pass]@]host[:port]/path] [...]\n"
105904203a83SThomas Cort #ifdef WITH_SSL
106004203a83SThomas Cort " [https://[user[:pass]@]host[:port]/path] [...]\n"
106104203a83SThomas Cort #endif
106204203a83SThomas Cort " %s -u URL file [...]\n", progname, progname);
106304203a83SThomas Cort exit(1);
106404203a83SThomas Cort }
1065