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