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