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