Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.58
      1 /*	$NetBSD: main.c,v 1.58 1999/10/01 08:01:13 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Copyright (C) 1997 and 1998 WIDE Project.
     38  * All rights reserved.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. Neither the name of the project nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 #ifndef lint
     67 __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
     68 	The Regents of the University of California.  All rights reserved.\n");
     69 #endif /* not lint */
     70 
     71 #ifndef lint
     72 #if 0
     73 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 10/9/94";
     74 #else
     75 __RCSID("$NetBSD: main.c,v 1.58 1999/10/01 08:01:13 lukem Exp $");
     76 #endif
     77 #endif /* not lint */
     78 
     79 /*
     80  * FTP User Program -- Command Interface.
     81  */
     82 #include <sys/types.h>
     83 #include <sys/socket.h>
     84 
     85 #include <err.h>
     86 #include <netdb.h>
     87 #include <paths.h>
     88 #include <pwd.h>
     89 #include <stdio.h>
     90 #include <stdlib.h>
     91 #include <string.h>
     92 #include <unistd.h>
     93 
     94 #define	GLOBAL		/* force GLOBAL decls in ftp_var.h to be declared */
     95 #include "ftp_var.h"
     96 
     97 #define FTP_PROXY	"ftp_proxy"	/* env var with FTP proxy location */
     98 #define HTTP_PROXY	"http_proxy"	/* env var with HTTP proxy location */
     99 #define NO_PROXY	"no_proxy"	/* env var with list of non-proxied
    100 					 * hosts, comma or space separated */
    101 
    102 int main __P((int, char **));
    103 
    104 int
    105 main(argc, argv)
    106 	int argc;
    107 	char *argv[];
    108 {
    109 	int ch, top, rval;
    110 	struct passwd *pw = NULL;
    111 	char *cp, *ep;
    112 	int dumbterm, s, len;
    113 
    114 	ftpport = "ftp";
    115 	httpport = "http";
    116 	ftpproxy = getenv(FTP_PROXY);
    117 	httpproxy = getenv(HTTP_PROXY);
    118 	no_proxy = getenv(NO_PROXY);
    119 	gateport = NULL;
    120 	cp = getenv("FTPSERVERPORT");
    121 	if (cp != NULL)
    122 		gateport = cp;
    123 	else
    124 		gateport = "ftpgate";
    125 	doglob = 1;
    126 	interactive = 1;
    127 	autologin = 1;
    128 	passivemode = 1;
    129 	activefallback = 1;
    130 	preserve = 1;
    131 	verbose = 0;
    132 	progress = 0;
    133 	gatemode = 0;
    134 	data = -1;
    135 	outfile = NULL;
    136 	restartautofetch = 0;
    137 #ifndef NO_EDITCOMPLETE
    138 	editing = 0;
    139 	el = NULL;
    140 	hist = NULL;
    141 #endif
    142 	mark = HASHBYTES;
    143 	rate_get = 0;
    144 	rate_get_incr = DEFAULTINCR;
    145 	rate_put = 0;
    146 	rate_put_incr = DEFAULTINCR;
    147 #ifdef INET6
    148 	epsv4 = 1;
    149 #else
    150 	epsv4 = 0;
    151 #endif
    152 	epsv4bad = 0;
    153 
    154 	/*
    155 	 * Get the default socket buffer sizes if we don't already have them.
    156 	 * It doesn't matter which socket we do this to, because on the first
    157 	 * call no socket buffer sizes will have been modified, so we are
    158 	 * guaranteed to get the system defaults.
    159 	 */
    160 	s = socket(AF_INET, SOCK_STREAM, 0);
    161 	if (s == -1)
    162 		err(1, "can't create socket");
    163 	len = sizeof(rcvbuf_size);
    164 	if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size, &len)
    165 	    < 0)
    166 		err(1, "unable to get default rcvbuf size");
    167 	len = sizeof(sndbuf_size);
    168 	if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size, &len)
    169 	    < 0)
    170 		err(1, "unable to get default sndbuf size");
    171 	(void)close(s);
    172 					/* sanity check returned buffer sizes */
    173 	if (rcvbuf_size <= 0)
    174 		rcvbuf_size = 8192;
    175 	if (sndbuf_size <= 0)
    176 		sndbuf_size = 8192;
    177 
    178 	marg_sl = sl_init();
    179 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    180 		tmpdir = _PATH_TMP;
    181 
    182 	/* Set default operation mode based on FTPMODE environment variable */
    183 	if ((cp = getenv("FTPMODE")) != NULL) {
    184 		if (strcmp(cp, "passive") == 0) {
    185 			passivemode = 1;
    186 			activefallback = 0;
    187 		} else if (strcmp(cp, "active") == 0) {
    188 			passivemode = 0;
    189 			activefallback = 0;
    190 		} else if (strcmp(cp, "gate") == 0) {
    191 			gatemode = 1;
    192 		} else if (strcmp(cp, "auto") == 0) {
    193 			passivemode = 1;
    194 			activefallback = 1;
    195 		} else
    196 			warnx("unknown $FTPMODE '%s'; using defaults", cp);
    197 	}
    198 
    199 	if (strcmp(__progname, "pftp") == 0) {
    200 		passivemode = 1;
    201 		activefallback = 0;
    202 	} else if (strcmp(__progname, "gate-ftp") == 0)
    203 		gatemode = 1;
    204 
    205 	gateserver = getenv("FTPSERVER");
    206 	if (gateserver == NULL || *gateserver == '\0')
    207 		gateserver = GATE_SERVER;
    208 	if (gatemode) {
    209 		if (*gateserver == '\0') {
    210 			warnx(
    211 "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
    212 			gatemode = 0;
    213 		}
    214 	}
    215 
    216 	cp = getenv("TERM");
    217 	if (cp == NULL || strcmp(cp, "dumb") == 0)
    218 		dumbterm = 1;
    219 	else
    220 		dumbterm = 0;
    221 	fromatty = isatty(fileno(stdin));
    222 	ttyout = stdout;
    223 	if (isatty(fileno(ttyout))) {
    224 		verbose = 1;		/* verbose if to a tty */
    225 		if (! dumbterm) {
    226 #ifndef NO_EDITCOMPLETE
    227 			if (fromatty)	/* editing mode on if tty is usable */
    228 				editing = 1;
    229 #endif
    230 #ifndef NO_PROGRESS
    231 			if (foregroundproc())
    232 				progress = 1;	/* progress bar on if fg */
    233 #endif
    234 		}
    235 	}
    236 
    237 	while ((ch = getopt(argc, argv, "Aadefgino:pP:r:RtT:vV")) != -1) {
    238 		switch (ch) {
    239 		case 'A':
    240 			activefallback = 0;
    241 			passivemode = 0;
    242 			break;
    243 
    244 		case 'a':
    245 			anonftp = 1;
    246 			break;
    247 
    248 		case 'd':
    249 			options |= SO_DEBUG;
    250 			debug++;
    251 			break;
    252 
    253 		case 'e':
    254 #ifndef NO_EDITCOMPLETE
    255 			editing = 0;
    256 #endif
    257 			break;
    258 
    259 		case 'f':
    260 			flushcache = 1;
    261 			break;
    262 
    263 		case 'g':
    264 			doglob = 0;
    265 			break;
    266 
    267 		case 'i':
    268 			interactive = 0;
    269 			break;
    270 
    271 		case 'n':
    272 			autologin = 0;
    273 			break;
    274 
    275 		case 'o':
    276 			outfile = optarg;
    277 			if (strcmp(outfile, "-") == 0)
    278 				ttyout = stderr;
    279 			break;
    280 
    281 		case 'p':
    282 			passivemode = 1;
    283 			activefallback = 0;
    284 			break;
    285 
    286 		case 'P':
    287 			ftpport = optarg;
    288 			break;
    289 
    290 		case 'r':
    291 			retry_connect = strtol(optarg, &ep, 10);
    292 			if (retry_connect < 1 || *ep != '\0')
    293 				errx(1, "bad retry value: %s", optarg);
    294 			break;
    295 
    296 		case 'R':
    297 			restartautofetch = 1;
    298 			break;
    299 
    300 		case 't':
    301 			trace = 1;
    302 			break;
    303 
    304 		case 'T':
    305 		{
    306 			int targc;
    307 			char *targv[6], *oac;
    308 
    309 				/* look for `dir,max[,incr]' */
    310 			targc = 0;
    311 			targv[targc++] = "-T";
    312 			oac = xstrdup(optarg);
    313 
    314 			while ((cp = strsep(&oac, ",")) != NULL) {
    315 				if (*cp == '\0') {
    316 					warnx("bad throttle value: %s", optarg);
    317 					usage();
    318 					/* NOTREACHED */
    319 				}
    320 				targv[targc++] = cp;
    321 				if (targc >= 5)
    322 					break;
    323 			}
    324 			if (parserate(targc, targv, 1) == -1)
    325 				usage();
    326 			free(oac);
    327 			break;
    328 		}
    329 
    330 		case 'v':
    331 			progress = verbose = 1;
    332 			break;
    333 
    334 		case 'V':
    335 			progress = verbose = 0;
    336 			break;
    337 
    338 		default:
    339 			usage();
    340 		}
    341 	}
    342 			/* set line buffering on ttyout */
    343 	setvbuf(ttyout, NULL, _IOLBF, 0);
    344 	argc -= optind;
    345 	argv += optind;
    346 
    347 	cpend = 0;	/* no pending replies */
    348 	proxy = 0;	/* proxy not active */
    349 	crflag = 1;	/* strip c.r. on ascii gets */
    350 	sendport = -1;	/* not using ports */
    351 	/*
    352 	 * Set up the home directory in case we're globbing.
    353 	 */
    354 	cp = getlogin();
    355 	if (cp != NULL) {
    356 		pw = getpwnam(cp);
    357 	}
    358 	if (pw == NULL)
    359 		pw = getpwuid(getuid());
    360 	if (pw != NULL)
    361 		(void)strlcpy(home, pw->pw_dir, sizeof(home));
    362 	else
    363 		(void)strlcpy(home, "/", sizeof(home));
    364 
    365 	setttywidth(0);
    366 	(void)xsignal(SIGWINCH, setttywidth);
    367 	(void)xsignal(SIGUSR1, crankrate);
    368 	(void)xsignal(SIGUSR2, crankrate);
    369 
    370 #ifdef __GNUC__			/* to shut up gcc warnings */
    371 	(void)&argc;
    372 	(void)&argv;
    373 #endif
    374 
    375 	if (argc > 0) {
    376 		if (strchr(argv[0], ':') != NULL && ! isipv6addr(argv[0])) {
    377 			rval = auto_fetch(argc, argv);
    378 			if (rval >= 0)		/* -1 == connected and cd-ed */
    379 				exit(rval);
    380 		} else {
    381 			char *xargv[5];
    382 
    383 			if (setjmp(toplevel))
    384 				exit(0);
    385 			(void)xsignal(SIGINT, (sig_t)intr);
    386 			(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    387 			xargv[0] = __progname;
    388 			xargv[1] = argv[0];
    389 			xargv[2] = argv[1];
    390 			xargv[3] = argv[2];
    391 			xargv[4] = NULL;
    392 			do {
    393 				setpeer(argc+1, xargv);
    394 				if (!retry_connect)
    395 					break;
    396 				if (!connected) {
    397 					macnum = 0;
    398 					fprintf(ttyout,
    399 					    "Retrying in %d seconds...\n",
    400 					    retry_connect);
    401 					sleep(retry_connect);
    402 				}
    403 			} while (!connected);
    404 			retry_connect = 0; /* connected, stop hiding msgs */
    405 		}
    406 	}
    407 #ifndef NO_EDITCOMPLETE
    408 	controlediting();
    409 #endif /* !NO_EDITCOMPLETE */
    410 	top = setjmp(toplevel) == 0;
    411 	if (top) {
    412 		(void)xsignal(SIGINT, (sig_t)intr);
    413 		(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    414 	}
    415 	for (;;) {
    416 		cmdscanner(top);
    417 		top = 1;
    418 	}
    419 }
    420 
    421 void
    422 intr()
    423 {
    424 
    425 	alarmtimer(0);
    426 	longjmp(toplevel, 1);
    427 }
    428 
    429 void
    430 lostpeer()
    431 {
    432 
    433 	alarmtimer(0);
    434 	if (connected) {
    435 		if (cout != NULL) {
    436 			(void)shutdown(fileno(cout), 1+1);
    437 			(void)fclose(cout);
    438 			cout = NULL;
    439 		}
    440 		if (data >= 0) {
    441 			(void)shutdown(data, 1+1);
    442 			(void)close(data);
    443 			data = -1;
    444 		}
    445 		connected = 0;
    446 	}
    447 	pswitch(1);
    448 	if (connected) {
    449 		if (cout != NULL) {
    450 			(void)shutdown(fileno(cout), 1+1);
    451 			(void)fclose(cout);
    452 			cout = NULL;
    453 		}
    454 		connected = 0;
    455 	}
    456 	proxflag = 0;
    457 	pswitch(0);
    458 }
    459 
    460 /*
    461  * Generate a prompt
    462  */
    463 char *
    464 prompt()
    465 {
    466 	return ("ftp> ");
    467 }
    468 
    469 /*
    470  * Command parser.
    471  */
    472 void
    473 cmdscanner(top)
    474 	int top;
    475 {
    476 	struct cmd *c;
    477 	int num;
    478 
    479 	if (!top
    480 #ifndef NO_EDITCOMPLETE
    481 	    && !editing
    482 #endif /* !NO_EDITCOMPLETE */
    483 	    )
    484 		(void)putc('\n', ttyout);
    485 	for (;;) {
    486 #ifndef NO_EDITCOMPLETE
    487 		if (!editing) {
    488 #endif /* !NO_EDITCOMPLETE */
    489 			if (fromatty) {
    490 				fputs(prompt(), ttyout);
    491 				(void)fflush(ttyout);
    492 			}
    493 			if (fgets(line, sizeof(line), stdin) == NULL)
    494 				quit(0, 0);
    495 			num = strlen(line);
    496 			if (num == 0)
    497 				break;
    498 			if (line[--num] == '\n') {
    499 				if (num == 0)
    500 					break;
    501 				line[num] = '\0';
    502 			} else if (num == sizeof(line) - 2) {
    503 				fputs("sorry, input line too long.\n", ttyout);
    504 				while ((num = getchar()) != '\n' && num != EOF)
    505 					/* void */;
    506 				break;
    507 			} /* else it was a line without a newline */
    508 #ifndef NO_EDITCOMPLETE
    509 		} else {
    510 			const char *buf;
    511 			HistEvent ev;
    512 			cursor_pos = NULL;
    513 
    514 			if ((buf = el_gets(el, &num)) == NULL || num == 0)
    515 				quit(0, 0);
    516 			if (buf[--num] == '\n') {
    517 				if (num == 0)
    518 					break;
    519 			} else if (num >= sizeof(line)) {
    520 				fputs("sorry, input line too long.\n", ttyout);
    521 				break;
    522 			}
    523 			memcpy(line, buf, num);
    524 			line[num] = '\0';
    525 			history(hist, &ev, H_ENTER, buf);
    526 		}
    527 #endif /* !NO_EDITCOMPLETE */
    528 
    529 		makeargv();
    530 		if (margc == 0)
    531 			continue;
    532 		c = getcmd(margv[0]);
    533 		if (c == (struct cmd *)-1) {
    534 			fputs("?Ambiguous command.\n", ttyout);
    535 			continue;
    536 		}
    537 		if (c == NULL) {
    538 #if !defined(NO_EDITCOMPLETE)
    539 			/*
    540 			 * attempt to el_parse() unknown commands.
    541 			 * any command containing a ':' would be parsed
    542 			 * as "[prog:]cmd ...", and will result in a
    543 			 * false positive if prog != "ftp", so treat
    544 			 * such commands as invalid.
    545 			 */
    546 			if (strchr(margv[0], ':') != NULL ||
    547 			    el_parse(el, margc, margv) != 0)
    548 #endif /* !NO_EDITCOMPLETE */
    549 				fputs("?Invalid command.\n", ttyout);
    550 			continue;
    551 		}
    552 		if (c->c_conn && !connected) {
    553 			fputs("Not connected.\n", ttyout);
    554 			continue;
    555 		}
    556 		confirmrest = 0;
    557 		(*c->c_handler)(margc, margv);
    558 		if (bell && c->c_bell)
    559 			(void)putc('\007', ttyout);
    560 		if (c->c_handler != help)
    561 			break;
    562 	}
    563 	(void)xsignal(SIGINT, (sig_t)intr);
    564 	(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    565 }
    566 
    567 struct cmd *
    568 getcmd(name)
    569 	const char *name;
    570 {
    571 	const char *p, *q;
    572 	struct cmd *c, *found;
    573 	int nmatches, longest;
    574 
    575 	if (name == NULL)
    576 		return (0);
    577 
    578 	longest = 0;
    579 	nmatches = 0;
    580 	found = 0;
    581 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    582 		for (q = name; *q == *p++; q++)
    583 			if (*q == 0)		/* exact match? */
    584 				return (c);
    585 		if (!*q) {			/* the name was a prefix */
    586 			if (q - name > longest) {
    587 				longest = q - name;
    588 				nmatches = 1;
    589 				found = c;
    590 			} else if (q - name == longest)
    591 				nmatches++;
    592 		}
    593 	}
    594 	if (nmatches > 1)
    595 		return ((struct cmd *)-1);
    596 	return (found);
    597 }
    598 
    599 /*
    600  * Slice a string up into argc/argv.
    601  */
    602 
    603 int slrflag;
    604 
    605 void
    606 makeargv()
    607 {
    608 	char *argp;
    609 
    610 	stringbase = line;		/* scan from first of buffer */
    611 	argbase = argbuf;		/* store from first of buffer */
    612 	slrflag = 0;
    613 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    614 	for (margc = 0; ; margc++) {
    615 		argp = slurpstring();
    616 		sl_add(marg_sl, argp);
    617 		if (argp == NULL)
    618 			break;
    619 	}
    620 #ifndef NO_EDITCOMPLETE
    621 	if (cursor_pos == line) {
    622 		cursor_argc = 0;
    623 		cursor_argo = 0;
    624 	} else if (cursor_pos != NULL) {
    625 		cursor_argc = margc;
    626 		cursor_argo = strlen(margv[margc-1]);
    627 	}
    628 #endif /* !NO_EDITCOMPLETE */
    629 }
    630 
    631 #ifdef NO_EDITCOMPLETE
    632 #define INC_CHKCURSOR(x)	(x)++
    633 #else  /* !NO_EDITCOMPLETE */
    634 #define INC_CHKCURSOR(x)	{ (x)++ ; \
    635 				if (x == cursor_pos) { \
    636 					cursor_argc = margc; \
    637 					cursor_argo = ap-argbase; \
    638 					cursor_pos = NULL; \
    639 				} }
    640 
    641 #endif /* !NO_EDITCOMPLETE */
    642 
    643 /*
    644  * Parse string into argbuf;
    645  * implemented with FSM to
    646  * handle quoting and strings
    647  */
    648 char *
    649 slurpstring()
    650 {
    651 	int got_one = 0;
    652 	char *sb = stringbase;
    653 	char *ap = argbase;
    654 	char *tmp = argbase;		/* will return this if token found */
    655 
    656 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    657 		switch (slrflag) {	/* and $ as token for macro invoke */
    658 			case 0:
    659 				slrflag++;
    660 				INC_CHKCURSOR(stringbase);
    661 				return ((*sb == '!') ? "!" : "$");
    662 				/* NOTREACHED */
    663 			case 1:
    664 				slrflag++;
    665 				altarg = stringbase;
    666 				break;
    667 			default:
    668 				break;
    669 		}
    670 	}
    671 
    672 S0:
    673 	switch (*sb) {
    674 
    675 	case '\0':
    676 		goto OUT;
    677 
    678 	case ' ':
    679 	case '\t':
    680 		INC_CHKCURSOR(sb);
    681 		goto S0;
    682 
    683 	default:
    684 		switch (slrflag) {
    685 			case 0:
    686 				slrflag++;
    687 				break;
    688 			case 1:
    689 				slrflag++;
    690 				altarg = sb;
    691 				break;
    692 			default:
    693 				break;
    694 		}
    695 		goto S1;
    696 	}
    697 
    698 S1:
    699 	switch (*sb) {
    700 
    701 	case ' ':
    702 	case '\t':
    703 	case '\0':
    704 		goto OUT;	/* end of token */
    705 
    706 	case '\\':
    707 		INC_CHKCURSOR(sb);
    708 		goto S2;	/* slurp next character */
    709 
    710 	case '"':
    711 		INC_CHKCURSOR(sb);
    712 		goto S3;	/* slurp quoted string */
    713 
    714 	default:
    715 		*ap = *sb;	/* add character to token */
    716 		ap++;
    717 		INC_CHKCURSOR(sb);
    718 		got_one = 1;
    719 		goto S1;
    720 	}
    721 
    722 S2:
    723 	switch (*sb) {
    724 
    725 	case '\0':
    726 		goto OUT;
    727 
    728 	default:
    729 		*ap = *sb;
    730 		ap++;
    731 		INC_CHKCURSOR(sb);
    732 		got_one = 1;
    733 		goto S1;
    734 	}
    735 
    736 S3:
    737 	switch (*sb) {
    738 
    739 	case '\0':
    740 		goto OUT;
    741 
    742 	case '"':
    743 		INC_CHKCURSOR(sb);
    744 		goto S1;
    745 
    746 	default:
    747 		*ap = *sb;
    748 		ap++;
    749 		INC_CHKCURSOR(sb);
    750 		got_one = 1;
    751 		goto S3;
    752 	}
    753 
    754 OUT:
    755 	if (got_one)
    756 		*ap++ = '\0';
    757 	argbase = ap;			/* update storage pointer */
    758 	stringbase = sb;		/* update scan pointer */
    759 	if (got_one) {
    760 		return (tmp);
    761 	}
    762 	switch (slrflag) {
    763 		case 0:
    764 			slrflag++;
    765 			break;
    766 		case 1:
    767 			slrflag++;
    768 			altarg = NULL;
    769 			break;
    770 		default:
    771 			break;
    772 	}
    773 	return (NULL);
    774 }
    775 
    776 /*
    777  * Help command.
    778  * Call each command handler with argc == 0 and argv[0] == name.
    779  */
    780 void
    781 help(argc, argv)
    782 	int argc;
    783 	char *argv[];
    784 {
    785 	struct cmd *c;
    786 
    787 	if (argc == 1) {
    788 		StringList *buf;
    789 
    790 		buf = sl_init();
    791 		fprintf(ttyout,
    792 		    "%sommands may be abbreviated.  Commands are:\n\n",
    793 		    proxy ? "Proxy c" : "C");
    794 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
    795 			if (c->c_name && (!proxy || c->c_proxy))
    796 				sl_add(buf, c->c_name);
    797 		list_vertical(buf);
    798 		sl_free(buf, 0);
    799 		return;
    800 	}
    801 
    802 #define HELPINDENT ((int) sizeof("disconnect"))
    803 
    804 	while (--argc > 0) {
    805 		char *arg;
    806 
    807 		arg = *++argv;
    808 		c = getcmd(arg);
    809 		if (c == (struct cmd *)-1)
    810 			fprintf(ttyout, "?Ambiguous help command %s\n", arg);
    811 		else if (c == NULL)
    812 			fprintf(ttyout, "?Invalid help command %s\n", arg);
    813 		else
    814 			fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
    815 				c->c_name, c->c_help);
    816 	}
    817 }
    818 
    819 void
    820 usage()
    821 {
    822 	(void)fprintf(stderr,
    823 "usage: %s [-AadefginpRtvV] [-r retry] [-o outfile] [-P port] [-T dir,max[,inc]\n"
    824 "       [host [port]] [file:///file] [ftp://[user[:pass]@]host[:port]/path[/]]\n"
    825 "       [http://[user[:pass]@]host[:port]/path] [host:path[/]]\n", __progname);
    826 	exit(1);
    827 }
    828