Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.63
      1 /*	$NetBSD: main.c,v 1.63 1999/10/12 06:05:01 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.63 1999/10/12 06:05:01 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, top, 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 	top = sigsetjmp(toplevel, 1) == 0;
    453 	if (top) {
    454 		(void)xsignal(SIGINT, intr);
    455 		(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    456 	}
    457 	for (;;) {
    458 		cmdscanner(top);
    459 		top = 1;
    460 	}
    461 }
    462 
    463 void
    464 intr(dummy)
    465 	int dummy;
    466 {
    467 
    468 	alarmtimer(0);
    469 	if (editing)		/* ugly hack to cleanup after editline */
    470 		write(fileno(ttyout), "\n", 1);
    471 	siglongjmp(toplevel, 1);
    472 }
    473 
    474 void
    475 lostpeer()
    476 {
    477 	int oerrno = errno;
    478 
    479 	alarmtimer(0);
    480 	if (connected) {
    481 		if (cout != NULL) {
    482 			(void)shutdown(fileno(cout), 1+1);
    483 			(void)fclose(cout);
    484 			cout = NULL;
    485 		}
    486 		if (data >= 0) {
    487 			(void)shutdown(data, 1+1);
    488 			(void)close(data);
    489 			data = -1;
    490 		}
    491 		connected = 0;
    492 	}
    493 	pswitch(1);
    494 	if (connected) {
    495 		if (cout != NULL) {
    496 			(void)shutdown(fileno(cout), 1+1);
    497 			(void)fclose(cout);
    498 			cout = NULL;
    499 		}
    500 		connected = 0;
    501 	}
    502 	proxflag = 0;
    503 	pswitch(0);
    504 	errno = oerrno;
    505 }
    506 
    507 /*
    508  * Generate a prompt
    509  */
    510 char *
    511 prompt()
    512 {
    513 	return ("ftp> ");
    514 }
    515 
    516 /*
    517  * Command parser.
    518  */
    519 void
    520 cmdscanner(top)
    521 	int top;
    522 {
    523 	struct cmd *c;
    524 	int num;
    525 
    526 	if (!top
    527 #ifndef NO_EDITCOMPLETE
    528 	    && !editing
    529 #endif /* !NO_EDITCOMPLETE */
    530 	    )
    531 		(void)putc('\n', ttyout);
    532 	for (;;) {
    533 #ifndef NO_EDITCOMPLETE
    534 		if (!editing) {
    535 #endif /* !NO_EDITCOMPLETE */
    536 			if (fromatty) {
    537 				fputs(prompt(), ttyout);
    538 				(void)fflush(ttyout);
    539 			}
    540 			if (fgets(line, sizeof(line), stdin) == NULL) {
    541 				if (fromatty)
    542 					putc('\n', ttyout);
    543 				quit(0, 0);
    544 			}
    545 			num = strlen(line);
    546 			if (num == 0)
    547 				break;
    548 			if (line[--num] == '\n') {
    549 				if (num == 0)
    550 					break;
    551 				line[num] = '\0';
    552 			} else if (num == sizeof(line) - 2) {
    553 				fputs("sorry, input line too long.\n", ttyout);
    554 				while ((num = getchar()) != '\n' && num != EOF)
    555 					/* void */;
    556 				break;
    557 			} /* else it was a line without a newline */
    558 #ifndef NO_EDITCOMPLETE
    559 		} else {
    560 			const char *buf;
    561 			HistEvent ev;
    562 			cursor_pos = NULL;
    563 
    564 			if ((buf = el_gets(el, &num)) == NULL || num == 0) {
    565 				if (fromatty)
    566 					putc('\n', ttyout);
    567 				quit(0, 0);
    568 			}
    569 			if (buf[--num] == '\n') {
    570 				if (num == 0)
    571 					break;
    572 			} else if (num >= sizeof(line)) {
    573 				fputs("sorry, input line too long.\n", ttyout);
    574 				break;
    575 			}
    576 			memcpy(line, buf, num);
    577 			line[num] = '\0';
    578 			history(hist, &ev, H_ENTER, buf);
    579 		}
    580 #endif /* !NO_EDITCOMPLETE */
    581 
    582 		makeargv();
    583 		if (margc == 0)
    584 			continue;
    585 		c = getcmd(margv[0]);
    586 		if (c == (struct cmd *)-1) {
    587 			fputs("?Ambiguous command.\n", ttyout);
    588 			continue;
    589 		}
    590 		if (c == NULL) {
    591 #if !defined(NO_EDITCOMPLETE)
    592 			/*
    593 			 * attempt to el_parse() unknown commands.
    594 			 * any command containing a ':' would be parsed
    595 			 * as "[prog:]cmd ...", and will result in a
    596 			 * false positive if prog != "ftp", so treat
    597 			 * such commands as invalid.
    598 			 */
    599 			if (strchr(margv[0], ':') != NULL ||
    600 			    el_parse(el, margc, margv) != 0)
    601 #endif /* !NO_EDITCOMPLETE */
    602 				fputs("?Invalid command.\n", ttyout);
    603 			continue;
    604 		}
    605 		if (c->c_conn && !connected) {
    606 			fputs("Not connected.\n", ttyout);
    607 			continue;
    608 		}
    609 		confirmrest = 0;
    610 		(*c->c_handler)(margc, margv);
    611 		if (bell && c->c_bell)
    612 			(void)putc('\007', ttyout);
    613 		if (c->c_handler != help)
    614 			break;
    615 	}
    616 	(void)xsignal(SIGINT, intr);
    617 	(void)xsignal(SIGPIPE, (sig_t)lostpeer);
    618 }
    619 
    620 struct cmd *
    621 getcmd(name)
    622 	const char *name;
    623 {
    624 	const char *p, *q;
    625 	struct cmd *c, *found;
    626 	int nmatches, longest;
    627 
    628 	if (name == NULL)
    629 		return (0);
    630 
    631 	longest = 0;
    632 	nmatches = 0;
    633 	found = 0;
    634 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    635 		for (q = name; *q == *p++; q++)
    636 			if (*q == 0)		/* exact match? */
    637 				return (c);
    638 		if (!*q) {			/* the name was a prefix */
    639 			if (q - name > longest) {
    640 				longest = q - name;
    641 				nmatches = 1;
    642 				found = c;
    643 			} else if (q - name == longest)
    644 				nmatches++;
    645 		}
    646 	}
    647 	if (nmatches > 1)
    648 		return ((struct cmd *)-1);
    649 	return (found);
    650 }
    651 
    652 /*
    653  * Slice a string up into argc/argv.
    654  */
    655 
    656 int slrflag;
    657 
    658 void
    659 makeargv()
    660 {
    661 	char *argp;
    662 
    663 	stringbase = line;		/* scan from first of buffer */
    664 	argbase = argbuf;		/* store from first of buffer */
    665 	slrflag = 0;
    666 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    667 	for (margc = 0; ; margc++) {
    668 		argp = slurpstring();
    669 		sl_add(marg_sl, argp);
    670 		if (argp == NULL)
    671 			break;
    672 	}
    673 #ifndef NO_EDITCOMPLETE
    674 	if (cursor_pos == line) {
    675 		cursor_argc = 0;
    676 		cursor_argo = 0;
    677 	} else if (cursor_pos != NULL) {
    678 		cursor_argc = margc;
    679 		cursor_argo = strlen(margv[margc-1]);
    680 	}
    681 #endif /* !NO_EDITCOMPLETE */
    682 }
    683 
    684 #ifdef NO_EDITCOMPLETE
    685 #define INC_CHKCURSOR(x)	(x)++
    686 #else  /* !NO_EDITCOMPLETE */
    687 #define INC_CHKCURSOR(x)	{ (x)++ ; \
    688 				if (x == cursor_pos) { \
    689 					cursor_argc = margc; \
    690 					cursor_argo = ap-argbase; \
    691 					cursor_pos = NULL; \
    692 				} }
    693 
    694 #endif /* !NO_EDITCOMPLETE */
    695 
    696 /*
    697  * Parse string into argbuf;
    698  * implemented with FSM to
    699  * handle quoting and strings
    700  */
    701 char *
    702 slurpstring()
    703 {
    704 	int got_one = 0;
    705 	char *sb = stringbase;
    706 	char *ap = argbase;
    707 	char *tmp = argbase;		/* will return this if token found */
    708 
    709 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    710 		switch (slrflag) {	/* and $ as token for macro invoke */
    711 			case 0:
    712 				slrflag++;
    713 				INC_CHKCURSOR(stringbase);
    714 				return ((*sb == '!') ? "!" : "$");
    715 				/* NOTREACHED */
    716 			case 1:
    717 				slrflag++;
    718 				altarg = stringbase;
    719 				break;
    720 			default:
    721 				break;
    722 		}
    723 	}
    724 
    725 S0:
    726 	switch (*sb) {
    727 
    728 	case '\0':
    729 		goto OUT;
    730 
    731 	case ' ':
    732 	case '\t':
    733 		INC_CHKCURSOR(sb);
    734 		goto S0;
    735 
    736 	default:
    737 		switch (slrflag) {
    738 			case 0:
    739 				slrflag++;
    740 				break;
    741 			case 1:
    742 				slrflag++;
    743 				altarg = sb;
    744 				break;
    745 			default:
    746 				break;
    747 		}
    748 		goto S1;
    749 	}
    750 
    751 S1:
    752 	switch (*sb) {
    753 
    754 	case ' ':
    755 	case '\t':
    756 	case '\0':
    757 		goto OUT;	/* end of token */
    758 
    759 	case '\\':
    760 		INC_CHKCURSOR(sb);
    761 		goto S2;	/* slurp next character */
    762 
    763 	case '"':
    764 		INC_CHKCURSOR(sb);
    765 		goto S3;	/* slurp quoted string */
    766 
    767 	default:
    768 		*ap = *sb;	/* add character to token */
    769 		ap++;
    770 		INC_CHKCURSOR(sb);
    771 		got_one = 1;
    772 		goto S1;
    773 	}
    774 
    775 S2:
    776 	switch (*sb) {
    777 
    778 	case '\0':
    779 		goto OUT;
    780 
    781 	default:
    782 		*ap = *sb;
    783 		ap++;
    784 		INC_CHKCURSOR(sb);
    785 		got_one = 1;
    786 		goto S1;
    787 	}
    788 
    789 S3:
    790 	switch (*sb) {
    791 
    792 	case '\0':
    793 		goto OUT;
    794 
    795 	case '"':
    796 		INC_CHKCURSOR(sb);
    797 		goto S1;
    798 
    799 	default:
    800 		*ap = *sb;
    801 		ap++;
    802 		INC_CHKCURSOR(sb);
    803 		got_one = 1;
    804 		goto S3;
    805 	}
    806 
    807 OUT:
    808 	if (got_one)
    809 		*ap++ = '\0';
    810 	argbase = ap;			/* update storage pointer */
    811 	stringbase = sb;		/* update scan pointer */
    812 	if (got_one) {
    813 		return (tmp);
    814 	}
    815 	switch (slrflag) {
    816 		case 0:
    817 			slrflag++;
    818 			break;
    819 		case 1:
    820 			slrflag++;
    821 			altarg = NULL;
    822 			break;
    823 		default:
    824 			break;
    825 	}
    826 	return (NULL);
    827 }
    828 
    829 /*
    830  * Help command.
    831  * Call each command handler with argc == 0 and argv[0] == name.
    832  */
    833 void
    834 help(argc, argv)
    835 	int argc;
    836 	char *argv[];
    837 {
    838 	struct cmd *c;
    839 
    840 	if (argc == 1) {
    841 		StringList *buf;
    842 
    843 		buf = sl_init();
    844 		fprintf(ttyout,
    845 		    "%sommands may be abbreviated.  Commands are:\n\n",
    846 		    proxy ? "Proxy c" : "C");
    847 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
    848 			if (c->c_name && (!proxy || c->c_proxy))
    849 				sl_add(buf, c->c_name);
    850 		list_vertical(buf);
    851 		sl_free(buf, 0);
    852 		return;
    853 	}
    854 
    855 #define HELPINDENT ((int) sizeof("disconnect"))
    856 
    857 	while (--argc > 0) {
    858 		char *arg;
    859 
    860 		arg = *++argv;
    861 		c = getcmd(arg);
    862 		if (c == (struct cmd *)-1)
    863 			fprintf(ttyout, "?Ambiguous help command %s\n", arg);
    864 		else if (c == NULL)
    865 			fprintf(ttyout, "?Invalid help command %s\n", arg);
    866 		else
    867 			fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
    868 				c->c_name, c->c_help);
    869 	}
    870 }
    871 
    872 void
    873 usage()
    874 {
    875 	(void)fprintf(stderr,
    876 "usage: %s [-AadefginpRtvV] [-r retry] [-o outfile] [-P port] [-T dir,max[,inc]\n"
    877 "       [host [port]] [file:///file] [ftp://[user[:pass]@]host[:port]/path[/]]\n"
    878 "       [http://[user[:pass]@]host[:port]/path] [host:path[/]]\n", __progname);
    879 	exit(1);
    880 }
    881