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