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