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