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