Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.57
      1 /*	$NetBSD: main.c,v 1.57 1999/10/01 06:55:45 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.57 1999/10/01 06:55:45 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 
    153 	/*
    154 	 * Get the default socket buffer sizes if we don't already have them.
    155 	 * It doesn't matter which socket we do this to, because on the first
    156 	 * call no socket buffer sizes will have been modified, so we are
    157 	 * guaranteed to get the system defaults.
    158 	 */
    159 	s = socket(AF_INET, SOCK_STREAM, 0);
    160 	if (s == -1)
    161 		err(1, "can't create socket");
    162 	len = sizeof(rcvbuf_size);
    163 	if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size, &len)
    164 	    < 0)
    165 		err(1, "unable to get default rcvbuf size");
    166 	len = sizeof(sndbuf_size);
    167 	if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size, &len)
    168 	    < 0)
    169 		err(1, "unable to get default sndbuf size");
    170 	(void)close(s);
    171 					/* sanity check returned buffer sizes */
    172 	if (rcvbuf_size <= 0)
    173 		rcvbuf_size = 8192;
    174 	if (sndbuf_size <= 0)
    175 		sndbuf_size = 8192;
    176 
    177 	marg_sl = sl_init();
    178 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    179 		tmpdir = _PATH_TMP;
    180 
    181 	/* Set default operation mode based on FTPMODE environment variable */
    182 	if ((cp = getenv("FTPMODE")) != NULL) {
    183 		if (strcmp(cp, "passive") == 0) {
    184 			passivemode = 1;
    185 			activefallback = 0;
    186 		} else if (strcmp(cp, "active") == 0) {
    187 			passivemode = 0;
    188 			activefallback = 0;
    189 		} else if (strcmp(cp, "gate") == 0) {
    190 			gatemode = 1;
    191 		} else if (strcmp(cp, "auto") == 0) {
    192 			passivemode = 1;
    193 			activefallback = 1;
    194 		} else
    195 			warnx("unknown $FTPMODE '%s'; using defaults", cp);
    196 	}
    197 
    198 	if (strcmp(__progname, "pftp") == 0) {
    199 		passivemode = 1;
    200 		activefallback = 0;
    201 	} else if (strcmp(__progname, "gate-ftp") == 0)
    202 		gatemode = 1;
    203 
    204 	gateserver = getenv("FTPSERVER");
    205 	if (gateserver == NULL || *gateserver == '\0')
    206 		gateserver = GATE_SERVER;
    207 	if (gatemode) {
    208 		if (*gateserver == '\0') {
    209 			warnx(
    210 "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
    211 			gatemode = 0;
    212 		}
    213 	}
    214 
    215 	cp = getenv("TERM");
    216 	if (cp == NULL || strcmp(cp, "dumb") == 0)
    217 		dumbterm = 1;
    218 	else
    219 		dumbterm = 0;
    220 	fromatty = isatty(fileno(stdin));
    221 	ttyout = stdout;
    222 	if (isatty(fileno(ttyout))) {
    223 		verbose = 1;		/* verbose if to a tty */
    224 		if (! dumbterm) {
    225 #ifndef NO_EDITCOMPLETE
    226 			if (fromatty)	/* editing mode on if tty is usable */
    227 				editing = 1;
    228 #endif
    229 #ifndef NO_PROGRESS
    230 			if (foregroundproc())
    231 				progress = 1;	/* progress bar on if fg */
    232 #endif
    233 		}
    234 	}
    235 
    236 	while ((ch = getopt(argc, argv, "Aadefgino:pP:r:RtT:vV")) != -1) {
    237 		switch (ch) {
    238 		case 'A':
    239 			activefallback = 0;
    240 			passivemode = 0;
    241 			break;
    242 
    243 		case 'a':
    244 			anonftp = 1;
    245 			break;
    246 
    247 		case 'd':
    248 			options |= SO_DEBUG;
    249 			debug++;
    250 			break;
    251 
    252 		case 'e':
    253 #ifndef NO_EDITCOMPLETE
    254 			editing = 0;
    255 #endif
    256 			break;
    257 
    258 		case 'f':
    259 			flushcache = 1;
    260 			break;
    261 
    262 		case 'g':
    263 			doglob = 0;
    264 			break;
    265 
    266 		case 'i':
    267 			interactive = 0;
    268 			break;
    269 
    270 		case 'n':
    271 			autologin = 0;
    272 			break;
    273 
    274 		case 'o':
    275 			outfile = optarg;
    276 			if (strcmp(outfile, "-") == 0)
    277 				ttyout = stderr;
    278 			break;
    279 
    280 		case 'p':
    281 			passivemode = 1;
    282 			activefallback = 0;
    283 			break;
    284 
    285 		case 'P':
    286 			ftpport = optarg;
    287 			break;
    288 
    289 		case 'r':
    290 			retry_connect = strtol(optarg, &ep, 10);
    291 			if (retry_connect < 1 || *ep != '\0')
    292 				errx(1, "bad retry value: %s", optarg);
    293 			break;
    294 
    295 		case 'R':
    296 			restartautofetch = 1;
    297 			break;
    298 
    299 		case 't':
    300 			trace = 1;
    301 			break;
    302 
    303 		case 'T':
    304 		{
    305 			int targc;
    306 			char *targv[6], *oac;
    307 
    308 				/* look for `dir,max[,incr]' */
    309 			targc = 0;
    310 			targv[targc++] = "-T";
    311 			oac = xstrdup(optarg);
    312 
    313 			while ((cp = strsep(&oac, ",")) != NULL) {
    314 				if (*cp == '\0') {
    315 					warnx("bad throttle value: %s", optarg);
    316 					usage();
    317 					/* NOTREACHED */
    318 				}
    319 				targv[targc++] = cp;
    320 				if (targc >= 5)
    321 					break;
    322 			}
    323 			if (parserate(targc, targv, 1) == -1)
    324 				usage();
    325 			free(oac);
    326 			break;
    327 		}
    328 
    329 		case 'v':
    330 			progress = verbose = 1;
    331 			break;
    332 
    333 		case 'V':
    334 			progress = verbose = 0;
    335 			break;
    336 
    337 		default:
    338 			usage();
    339 		}
    340 	}
    341 			/* set line buffering on ttyout */
    342 	setvbuf(ttyout, NULL, _IOLBF, 0);
    343 	argc -= optind;
    344 	argv += optind;
    345 
    346 	cpend = 0;	/* no pending replies */
    347 	proxy = 0;	/* proxy not active */
    348 	crflag = 1;	/* strip c.r. on ascii gets */
    349 	sendport = -1;	/* not using ports */
    350 	/*
    351 	 * Set up the home directory in case we're globbing.
    352 	 */
    353 	cp = getlogin();
    354 	if (cp != NULL) {
    355 		pw = getpwnam(cp);
    356 	}
    357 	if (pw == NULL)
    358 		pw = getpwuid(getuid());
    359 	if (pw != NULL)
    360 		(void)strlcpy(home, pw->pw_dir, sizeof(home));
    361 	else
    362 		(void)strlcpy(home, "/", sizeof(home));
    363 
    364 	setttywidth(0);
    365 	(void)xsignal(SIGWINCH, setttywidth);
    366 	(void)xsignal(SIGUSR1, crankrate);
    367 	(void)xsignal(SIGUSR2, crankrate);
    368 
    369 #ifdef __GNUC__			/* to shut up gcc warnings */
    370 	(void)&argc;
    371 	(void)&argv;
    372 #endif
    373 
    374 	if (argc > 0) {
    375 		if (strchr(argv[0], ':') != NULL && ! isipv6addr(argv[0])) {
    376 			rval = auto_fetch(argc, argv);
    377 			if (rval >= 0)		/* -1 == connected and cd-ed */
    378 				exit(rval);
    379 		} else {
    380 			char *xargv[5];
    381 
    382 			if (setjmp(toplevel))
    383 				exit(0);
    384 			(void)xsignal(SIGINT, (sig_t)intr);
    385 			(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    386 			xargv[0] = __progname;
    387 			xargv[1] = argv[0];
    388 			xargv[2] = argv[1];
    389 			xargv[3] = argv[2];
    390 			xargv[4] = NULL;
    391 			do {
    392 				setpeer(argc+1, xargv);
    393 				if (!retry_connect)
    394 					break;
    395 				if (!connected) {
    396 					macnum = 0;
    397 					fprintf(ttyout,
    398 					    "Retrying in %d seconds...\n",
    399 					    retry_connect);
    400 					sleep(retry_connect);
    401 				}
    402 			} while (!connected);
    403 			retry_connect = 0; /* connected, stop hiding msgs */
    404 		}
    405 	}
    406 #ifndef NO_EDITCOMPLETE
    407 	controlediting();
    408 #endif /* !NO_EDITCOMPLETE */
    409 	top = setjmp(toplevel) == 0;
    410 	if (top) {
    411 		(void)xsignal(SIGINT, (sig_t)intr);
    412 		(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    413 	}
    414 	for (;;) {
    415 		cmdscanner(top);
    416 		top = 1;
    417 	}
    418 }
    419 
    420 void
    421 intr()
    422 {
    423 
    424 	alarmtimer(0);
    425 	longjmp(toplevel, 1);
    426 }
    427 
    428 void
    429 lostpeer()
    430 {
    431 
    432 	alarmtimer(0);
    433 	if (connected) {
    434 		if (cout != NULL) {
    435 			(void)shutdown(fileno(cout), 1+1);
    436 			(void)fclose(cout);
    437 			cout = NULL;
    438 		}
    439 		if (data >= 0) {
    440 			(void)shutdown(data, 1+1);
    441 			(void)close(data);
    442 			data = -1;
    443 		}
    444 		connected = 0;
    445 	}
    446 	pswitch(1);
    447 	if (connected) {
    448 		if (cout != NULL) {
    449 			(void)shutdown(fileno(cout), 1+1);
    450 			(void)fclose(cout);
    451 			cout = NULL;
    452 		}
    453 		connected = 0;
    454 	}
    455 	proxflag = 0;
    456 	pswitch(0);
    457 }
    458 
    459 /*
    460  * Generate a prompt
    461  */
    462 char *
    463 prompt()
    464 {
    465 	return ("ftp> ");
    466 }
    467 
    468 /*
    469  * Command parser.
    470  */
    471 void
    472 cmdscanner(top)
    473 	int top;
    474 {
    475 	struct cmd *c;
    476 	int num;
    477 
    478 	if (!top
    479 #ifndef NO_EDITCOMPLETE
    480 	    && !editing
    481 #endif /* !NO_EDITCOMPLETE */
    482 	    )
    483 		(void)putc('\n', ttyout);
    484 	for (;;) {
    485 #ifndef NO_EDITCOMPLETE
    486 		if (!editing) {
    487 #endif /* !NO_EDITCOMPLETE */
    488 			if (fromatty) {
    489 				fputs(prompt(), ttyout);
    490 				(void)fflush(ttyout);
    491 			}
    492 			if (fgets(line, sizeof(line), stdin) == NULL)
    493 				quit(0, 0);
    494 			num = strlen(line);
    495 			if (num == 0)
    496 				break;
    497 			if (line[--num] == '\n') {
    498 				if (num == 0)
    499 					break;
    500 				line[num] = '\0';
    501 			} else if (num == sizeof(line) - 2) {
    502 				fputs("sorry, input line too long.\n", ttyout);
    503 				while ((num = getchar()) != '\n' && num != EOF)
    504 					/* void */;
    505 				break;
    506 			} /* else it was a line without a newline */
    507 #ifndef NO_EDITCOMPLETE
    508 		} else {
    509 			const char *buf;
    510 			HistEvent ev;
    511 			cursor_pos = NULL;
    512 
    513 			if ((buf = el_gets(el, &num)) == NULL || num == 0)
    514 				quit(0, 0);
    515 			if (buf[--num] == '\n') {
    516 				if (num == 0)
    517 					break;
    518 			} else if (num >= sizeof(line)) {
    519 				fputs("sorry, input line too long.\n", ttyout);
    520 				break;
    521 			}
    522 			memcpy(line, buf, num);
    523 			line[num] = '\0';
    524 			history(hist, &ev, H_ENTER, buf);
    525 		}
    526 #endif /* !NO_EDITCOMPLETE */
    527 
    528 		makeargv();
    529 		if (margc == 0)
    530 			continue;
    531 		c = getcmd(margv[0]);
    532 		if (c == (struct cmd *)-1) {
    533 			fputs("?Ambiguous command.\n", ttyout);
    534 			continue;
    535 		}
    536 		if (c == NULL) {
    537 #if !defined(NO_EDITCOMPLETE)
    538 			/*
    539 			 * attempt to el_parse() unknown commands.
    540 			 * any command containing a ':' would be parsed
    541 			 * as "[prog:]cmd ...", and will result in a
    542 			 * false positive if prog != "ftp", so treat
    543 			 * such commands as invalid.
    544 			 */
    545 			if (strchr(margv[0], ':') != NULL ||
    546 			    el_parse(el, margc, margv) != 0)
    547 #endif /* !NO_EDITCOMPLETE */
    548 				fputs("?Invalid command.\n", ttyout);
    549 			continue;
    550 		}
    551 		if (c->c_conn && !connected) {
    552 			fputs("Not connected.\n", ttyout);
    553 			continue;
    554 		}
    555 		confirmrest = 0;
    556 		(*c->c_handler)(margc, margv);
    557 		if (bell && c->c_bell)
    558 			(void)putc('\007', ttyout);
    559 		if (c->c_handler != help)
    560 			break;
    561 	}
    562 	(void)xsignal(SIGINT, (sig_t)intr);
    563 	(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    564 }
    565 
    566 struct cmd *
    567 getcmd(name)
    568 	const char *name;
    569 {
    570 	const char *p, *q;
    571 	struct cmd *c, *found;
    572 	int nmatches, longest;
    573 
    574 	if (name == NULL)
    575 		return (0);
    576 
    577 	longest = 0;
    578 	nmatches = 0;
    579 	found = 0;
    580 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    581 		for (q = name; *q == *p++; q++)
    582 			if (*q == 0)		/* exact match? */
    583 				return (c);
    584 		if (!*q) {			/* the name was a prefix */
    585 			if (q - name > longest) {
    586 				longest = q - name;
    587 				nmatches = 1;
    588 				found = c;
    589 			} else if (q - name == longest)
    590 				nmatches++;
    591 		}
    592 	}
    593 	if (nmatches > 1)
    594 		return ((struct cmd *)-1);
    595 	return (found);
    596 }
    597 
    598 /*
    599  * Slice a string up into argc/argv.
    600  */
    601 
    602 int slrflag;
    603 
    604 void
    605 makeargv()
    606 {
    607 	char *argp;
    608 
    609 	stringbase = line;		/* scan from first of buffer */
    610 	argbase = argbuf;		/* store from first of buffer */
    611 	slrflag = 0;
    612 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    613 	for (margc = 0; ; margc++) {
    614 		argp = slurpstring();
    615 		sl_add(marg_sl, argp);
    616 		if (argp == NULL)
    617 			break;
    618 	}
    619 #ifndef NO_EDITCOMPLETE
    620 	if (cursor_pos == line) {
    621 		cursor_argc = 0;
    622 		cursor_argo = 0;
    623 	} else if (cursor_pos != NULL) {
    624 		cursor_argc = margc;
    625 		cursor_argo = strlen(margv[margc-1]);
    626 	}
    627 #endif /* !NO_EDITCOMPLETE */
    628 }
    629 
    630 #ifdef NO_EDITCOMPLETE
    631 #define INC_CHKCURSOR(x)	(x)++
    632 #else  /* !NO_EDITCOMPLETE */
    633 #define INC_CHKCURSOR(x)	{ (x)++ ; \
    634 				if (x == cursor_pos) { \
    635 					cursor_argc = margc; \
    636 					cursor_argo = ap-argbase; \
    637 					cursor_pos = NULL; \
    638 				} }
    639 
    640 #endif /* !NO_EDITCOMPLETE */
    641 
    642 /*
    643  * Parse string into argbuf;
    644  * implemented with FSM to
    645  * handle quoting and strings
    646  */
    647 char *
    648 slurpstring()
    649 {
    650 	int got_one = 0;
    651 	char *sb = stringbase;
    652 	char *ap = argbase;
    653 	char *tmp = argbase;		/* will return this if token found */
    654 
    655 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    656 		switch (slrflag) {	/* and $ as token for macro invoke */
    657 			case 0:
    658 				slrflag++;
    659 				INC_CHKCURSOR(stringbase);
    660 				return ((*sb == '!') ? "!" : "$");
    661 				/* NOTREACHED */
    662 			case 1:
    663 				slrflag++;
    664 				altarg = stringbase;
    665 				break;
    666 			default:
    667 				break;
    668 		}
    669 	}
    670 
    671 S0:
    672 	switch (*sb) {
    673 
    674 	case '\0':
    675 		goto OUT;
    676 
    677 	case ' ':
    678 	case '\t':
    679 		INC_CHKCURSOR(sb);
    680 		goto S0;
    681 
    682 	default:
    683 		switch (slrflag) {
    684 			case 0:
    685 				slrflag++;
    686 				break;
    687 			case 1:
    688 				slrflag++;
    689 				altarg = sb;
    690 				break;
    691 			default:
    692 				break;
    693 		}
    694 		goto S1;
    695 	}
    696 
    697 S1:
    698 	switch (*sb) {
    699 
    700 	case ' ':
    701 	case '\t':
    702 	case '\0':
    703 		goto OUT;	/* end of token */
    704 
    705 	case '\\':
    706 		INC_CHKCURSOR(sb);
    707 		goto S2;	/* slurp next character */
    708 
    709 	case '"':
    710 		INC_CHKCURSOR(sb);
    711 		goto S3;	/* slurp quoted string */
    712 
    713 	default:
    714 		*ap = *sb;	/* add character to token */
    715 		ap++;
    716 		INC_CHKCURSOR(sb);
    717 		got_one = 1;
    718 		goto S1;
    719 	}
    720 
    721 S2:
    722 	switch (*sb) {
    723 
    724 	case '\0':
    725 		goto OUT;
    726 
    727 	default:
    728 		*ap = *sb;
    729 		ap++;
    730 		INC_CHKCURSOR(sb);
    731 		got_one = 1;
    732 		goto S1;
    733 	}
    734 
    735 S3:
    736 	switch (*sb) {
    737 
    738 	case '\0':
    739 		goto OUT;
    740 
    741 	case '"':
    742 		INC_CHKCURSOR(sb);
    743 		goto S1;
    744 
    745 	default:
    746 		*ap = *sb;
    747 		ap++;
    748 		INC_CHKCURSOR(sb);
    749 		got_one = 1;
    750 		goto S3;
    751 	}
    752 
    753 OUT:
    754 	if (got_one)
    755 		*ap++ = '\0';
    756 	argbase = ap;			/* update storage pointer */
    757 	stringbase = sb;		/* update scan pointer */
    758 	if (got_one) {
    759 		return (tmp);
    760 	}
    761 	switch (slrflag) {
    762 		case 0:
    763 			slrflag++;
    764 			break;
    765 		case 1:
    766 			slrflag++;
    767 			altarg = NULL;
    768 			break;
    769 		default:
    770 			break;
    771 	}
    772 	return (NULL);
    773 }
    774 
    775 /*
    776  * Help command.
    777  * Call each command handler with argc == 0 and argv[0] == name.
    778  */
    779 void
    780 help(argc, argv)
    781 	int argc;
    782 	char *argv[];
    783 {
    784 	struct cmd *c;
    785 
    786 	if (argc == 1) {
    787 		StringList *buf;
    788 
    789 		buf = sl_init();
    790 		fprintf(ttyout,
    791 		    "%sommands may be abbreviated.  Commands are:\n\n",
    792 		    proxy ? "Proxy c" : "C");
    793 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
    794 			if (c->c_name && (!proxy || c->c_proxy))
    795 				sl_add(buf, c->c_name);
    796 		list_vertical(buf);
    797 		sl_free(buf, 0);
    798 		return;
    799 	}
    800 
    801 #define HELPINDENT ((int) sizeof("disconnect"))
    802 
    803 	while (--argc > 0) {
    804 		char *arg;
    805 
    806 		arg = *++argv;
    807 		c = getcmd(arg);
    808 		if (c == (struct cmd *)-1)
    809 			fprintf(ttyout, "?Ambiguous help command %s\n", arg);
    810 		else if (c == NULL)
    811 			fprintf(ttyout, "?Invalid help command %s\n", arg);
    812 		else
    813 			fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
    814 				c->c_name, c->c_help);
    815 	}
    816 }
    817 
    818 void
    819 usage()
    820 {
    821 	(void)fprintf(stderr,
    822 "usage: %s [-AadefginpRtvV] [-r retry] [-o outfile] [-P port] [-T dir,max[,inc]\n"
    823 "       [host [port]] [file:///file] [ftp://[user[:pass]@]host[:port]/path[/]]\n"
    824 "       [http://[user[:pass]@]host[:port]/path] [host:path[/]]\n", __progname);
    825 	exit(1);
    826 }
    827