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