Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.54
      1 /*	$NetBSD: main.c,v 1.54 1999/09/30 12:18:04 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.54 1999/09/30 12:18:04 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 char home[MAXPATHLEN] = "/";
    102 
    103 int main __P((int, char **));
    104 
    105 int
    106 main(argc, argv)
    107 	int argc;
    108 	char *argv[];
    109 {
    110 	int ch, top, rval;
    111 	struct passwd *pw = NULL;
    112 	char *cp, *ep;
    113 	int dumbterm, s, len;
    114 
    115 	ftpport = "ftp";
    116 	httpport = "http";
    117 	ftpproxy = getenv(FTP_PROXY);
    118 	httpproxy = getenv(HTTP_PROXY);
    119 	no_proxy = getenv(NO_PROXY);
    120 	gateport = NULL;
    121 	cp = getenv("FTPSERVERPORT");
    122 	if (cp != NULL)
    123 		gateport = cp;
    124 	else
    125 		gateport = "ftpgate";
    126 	doglob = 1;
    127 	interactive = 1;
    128 	autologin = 1;
    129 	passivemode = 1;
    130 	activefallback = 1;
    131 	preserve = 1;
    132 	verbose = 0;
    133 	progress = 0;
    134 	gatemode = 0;
    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 
    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