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