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