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