Home | History | Annotate | Line # | Download | only in tftp
main.c revision 1.27
      1 /*	$NetBSD: main.c,v 1.27 2011/09/16 15:39:30 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #if 0
     37 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
     38 #else
     39 __RCSID("$NetBSD: main.c,v 1.27 2011/09/16 15:39:30 joerg Exp $");
     40 #endif
     41 #endif /* not lint */
     42 
     43 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
     44 
     45 /*
     46  * TFTP User Program -- Command Interface.
     47  */
     48 #include <sys/types.h>
     49 #include <sys/socket.h>
     50 
     51 #include <netinet/in.h>
     52 
     53 #include <arpa/inet.h>
     54 #include <arpa/tftp.h>
     55 
     56 #include <ctype.h>
     57 #include <fcntl.h>
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <netdb.h>
     61 #include <setjmp.h>
     62 #include <signal.h>
     63 #include <stdio.h>
     64 #include <stdlib.h>
     65 #include <string.h>
     66 #include <unistd.h>
     67 
     68 #include "extern.h"
     69 
     70 #define	TIMEOUT		5		/* secs between rexmt's */
     71 #define	LBUFLEN		200		/* size of input buffer */
     72 
     73 struct	sockaddr_storage peeraddr;
     74 int	f;
     75 int	mf;
     76 int	trace;
     77 int	verbose;
     78 int	tsize=0;
     79 int	tout=0;
     80 size_t	def_blksize=SEGSIZE;
     81 size_t	blksize=SEGSIZE;
     82 in_addr_t	mcaddr = INADDR_NONE;
     83 uint16_t	mcport;
     84 ushort	mcmasterslave;
     85 int	connected;
     86 char	mode[32];
     87 char	line[LBUFLEN];
     88 int	margc;
     89 char	*margv[20];
     90 const	char *prompt = "tftp";
     91 jmp_buf	toplevel;
     92 
     93 void	get __P((int, char **));
     94 void	help __P((int, char **));
     95 void	modecmd __P((int, char **));
     96 void	put __P((int, char **));
     97 __dead static void	quit __P((int, char **));
     98 void	setascii __P((int, char **));
     99 void	setbinary __P((int, char **));
    100 void	setpeer0 __P((const char *, const char *));
    101 void	setpeer __P((int, char **));
    102 void	setrexmt __P((int, char **));
    103 void	settimeout __P((int, char **));
    104 void	settrace __P((int, char **));
    105 void	setverbose __P((int, char **));
    106 void	setblksize __P((int, char **));
    107 void	settsize __P((int, char **));
    108 void	settimeoutopt __P((int, char **));
    109 void	status __P((int, char **));
    110 char	*tail __P((char *));
    111 __dead static void	intr __P((int));
    112 const	struct cmd *getcmd __P((char *));
    113 
    114 static __dead void command __P((void));
    115 
    116 static void getusage __P((char *));
    117 static void makeargv __P((void));
    118 static void putusage __P((char *));
    119 static void settftpmode __P((const char *));
    120 
    121 #define HELPINDENT (sizeof("connect"))
    122 
    123 struct cmd {
    124 	const char *name;
    125 	const char *help;
    126 	void	(*handler) __P((int, char **));
    127 };
    128 
    129 const char vhelp[] = "toggle verbose mode";
    130 const char thelp[] = "toggle packet tracing";
    131 const char tshelp[] = "toggle extended tsize option";
    132 const char tohelp[] = "toggle extended timeout option";
    133 const char blhelp[] = "set an alternative blocksize (def. 512)";
    134 const char chelp[] = "connect to remote tftp";
    135 const char qhelp[] = "exit tftp";
    136 const char hhelp[] = "print help information";
    137 const char shelp[] = "send file";
    138 const char rhelp[] = "receive file";
    139 const char mhelp[] = "set file transfer mode";
    140 const char sthelp[] = "show current status";
    141 const char xhelp[] = "set per-packet retransmission timeout";
    142 const char ihelp[] = "set total retransmission timeout";
    143 const char ashelp[] = "set mode to netascii";
    144 const char bnhelp[] = "set mode to octet";
    145 
    146 const struct cmd cmdtab[] = {
    147 	{ "connect",	chelp,		setpeer },
    148 	{ "mode",       mhelp,          modecmd },
    149 	{ "put",	shelp,		put },
    150 	{ "get",	rhelp,		get },
    151 	{ "quit",	qhelp,		quit },
    152 	{ "verbose",	vhelp,		setverbose },
    153 	{ "blksize",	blhelp,		setblksize },
    154 	{ "tsize",	tshelp,		settsize },
    155 	{ "trace",	thelp,		settrace },
    156 	{ "status",	sthelp,		status },
    157 	{ "binary",     bnhelp,         setbinary },
    158 	{ "ascii",      ashelp,         setascii },
    159 	{ "rexmt",	xhelp,		setrexmt },
    160 	{ "timeout",	ihelp,		settimeout },
    161 	{ "tout",	tohelp,		settimeoutopt },
    162 	{ "?",		hhelp,		help },
    163 	{ .name = NULL }
    164 };
    165 
    166 int
    167 main(argc, argv)
    168 	int argc;
    169 	char *argv[];
    170 {
    171 	int	c;
    172 
    173 	f = mf = -1;
    174 	(void)strlcpy(mode, "netascii", sizeof(mode));
    175 	(void)signal(SIGINT, intr);
    176 
    177 	setprogname(argv[0]);
    178 	while ((c = getopt(argc, argv, "e")) != -1) {
    179 		switch (c) {
    180 		case 'e':
    181 			blksize = MAXSEGSIZE;
    182 			(void)strlcpy(mode, "octet", sizeof(mode));
    183 			tsize = 1;
    184 			tout = 1;
    185 			break;
    186 		default:
    187 			(void)printf("usage: %s [-e] host-name [port]\n",
    188 				getprogname());
    189 			exit(1);
    190 		}
    191 	}
    192 	argc -= optind;
    193 	argv += optind;
    194 
    195 	if (argc >= 1) {
    196 		if (setjmp(toplevel) != 0)
    197 			exit(0);
    198 		argc++;
    199 		argv--;
    200 		setpeer(argc, argv);
    201 	}
    202 	if (setjmp(toplevel) != 0)
    203 		(void)putchar('\n');
    204 	command();
    205 	return (0);
    206 }
    207 
    208 char    hostname[100];
    209 
    210 void
    211 setpeer0(host, port)
    212 	const char *host;
    213 	const char *port;
    214 {
    215 	struct addrinfo hints, *res0, *res;
    216 	int error, soopt;
    217 	struct sockaddr_storage ss;
    218 	const char *cause = "unknown";
    219 
    220 	if (connected) {
    221 		(void)close(f);
    222 		f = -1;
    223 	}
    224 	connected = 0;
    225 
    226 	(void)memset(&hints, 0, sizeof(hints));
    227 	hints.ai_family = PF_UNSPEC;
    228 	hints.ai_socktype = SOCK_DGRAM;
    229 	hints.ai_protocol = IPPROTO_UDP;
    230 	hints.ai_flags = AI_CANONNAME;
    231 	if (!port)
    232 		port = "tftp";
    233 	error = getaddrinfo(host, port, &hints, &res0);
    234 	if (error) {
    235 		warnx("%s", gai_strerror(error));
    236 		return;
    237 	}
    238 
    239 	for (res = res0; res; res = res->ai_next) {
    240 		if (res->ai_addrlen > sizeof(peeraddr))
    241 			continue;
    242 		f = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    243 		if (f < 0) {
    244 			cause = "socket";
    245 			continue;
    246 		}
    247 
    248 		(void)memset(&ss, 0, sizeof(ss));
    249 		ss.ss_family = res->ai_family;
    250 		ss.ss_len = res->ai_addrlen;
    251 		if (bind(f, (struct sockaddr *)(void *)&ss,
    252 		    (socklen_t)ss.ss_len) < 0) {
    253 			cause = "bind";
    254 			(void)close(f);
    255 			f = -1;
    256 			continue;
    257 		}
    258 
    259 		break;
    260 	}
    261 
    262 	if (f >= 0) {
    263 		soopt = 65536;
    264 		if (setsockopt(f, SOL_SOCKET, SO_SNDBUF, &soopt, sizeof(soopt))
    265 		    < 0) {
    266 			(void)close(f);
    267 			f = -1;
    268 			cause = "setsockopt SNDBUF";
    269 		}
    270 		else if (setsockopt(f, SOL_SOCKET, SO_RCVBUF, &soopt, sizeof(soopt))
    271 		    < 0) {
    272 			(void)close(f);
    273 			f = -1;
    274 			cause = "setsockopt RCVBUF";
    275 		}
    276 	}
    277 
    278 	if (f < 0 || res == NULL)
    279 		warn("%s", cause);
    280 	else {
    281 		/* res->ai_addr <= sizeof(peeraddr) is guaranteed */
    282 		(void)memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
    283 		if (res->ai_canonname) {
    284 			(void)strlcpy(hostname, res->ai_canonname,
    285 			    sizeof(hostname));
    286 		} else
    287 			(void)strlcpy(hostname, host, sizeof(hostname));
    288 		connected = 1;
    289 	}
    290 
    291 	freeaddrinfo(res0);
    292 }
    293 
    294 void
    295 setpeer(argc, argv)
    296 	int argc;
    297 	char *argv[];
    298 {
    299 
    300 	if (argc < 2) {
    301 		(void)strlcpy(line, "Connect ", sizeof(line));
    302 		(void)printf("(to) ");
    303 		(void)fgets(&line[strlen(line)], (int)(LBUFLEN-strlen(line)), stdin);
    304 		makeargv();
    305 		argc = margc;
    306 		argv = margv;
    307 	}
    308 	if ((argc < 2) || (argc > 3)) {
    309 		(void)printf("usage: %s [-e] host-name [port]\n", getprogname());
    310 		return;
    311 	}
    312 	if (argc == 2)
    313 		setpeer0(argv[1], NULL);
    314 	else
    315 		setpeer0(argv[1], argv[2]);
    316 }
    317 
    318 struct	modes {
    319 	const char *m_name;
    320 	const char *m_mode;
    321 } modes[] = {
    322 	{ "ascii",	"netascii" },
    323 	{ "netascii",   "netascii" },
    324 	{ "binary",     "octet" },
    325 	{ "image",      "octet" },
    326 	{ "octet",     "octet" },
    327 /*      { "mail",       "mail" },       */
    328 	{ 0,		0 }
    329 };
    330 
    331 void
    332 modecmd(argc, argv)
    333 	int argc;
    334 	char *argv[];
    335 {
    336 	struct modes *p;
    337 	const char *sep;
    338 
    339 	if (argc < 2) {
    340 		(void)printf("Using %s mode to transfer files.\n", mode);
    341 		return;
    342 	}
    343 	if (argc == 2) {
    344 		for (p = modes; p->m_name; p++)
    345 			if (strcmp(argv[1], p->m_name) == 0)
    346 				break;
    347 		if (p->m_name) {
    348 			settftpmode(p->m_mode);
    349 			return;
    350 		}
    351 		(void)printf("%s: unknown mode\n", argv[1]);
    352 		/* drop through and print usage message */
    353 	}
    354 
    355 	(void)printf("usage: %s [", argv[0]);
    356 	sep = " ";
    357 	for (p = modes; p->m_name; p++) {
    358 		(void)printf("%s%s", sep, p->m_name);
    359 		if (*sep == ' ')
    360 			sep = " | ";
    361 	}
    362 	(void)printf(" ]\n");
    363 	return;
    364 }
    365 
    366 void
    367 /*ARGSUSED*/
    368 setbinary(argc, argv)
    369 	int argc;
    370 	char *argv[];
    371 {
    372 
    373 	settftpmode("octet");
    374 }
    375 
    376 void
    377 /*ARGSUSED*/
    378 setascii(argc, argv)
    379 	int argc;
    380 	char *argv[];
    381 {
    382 
    383 	settftpmode("netascii");
    384 }
    385 
    386 static void
    387 settftpmode(newmode)
    388 	const char *newmode;
    389 {
    390 	(void)strlcpy(mode, newmode, sizeof(mode));
    391 	if (verbose)
    392 		(void)printf("mode set to %s\n", mode);
    393 }
    394 
    395 
    396 /*
    397  * Send file(s).
    398  */
    399 void
    400 put(argc, argv)
    401 	int argc;
    402 	char *argv[];
    403 {
    404 	int fd;
    405 	int n;
    406 	char *targ, *p;
    407 
    408 	if (argc < 2) {
    409 		(void)strlcpy(line, "send ", sizeof(line));
    410 		(void)printf("(file) ");
    411 		(void)fgets(&line[strlen(line)], (int)(LBUFLEN-strlen(line)), stdin);
    412 		makeargv();
    413 		argc = margc;
    414 		argv = margv;
    415 	}
    416 	if (argc < 2) {
    417 		putusage(argv[0]);
    418 		return;
    419 	}
    420 	targ = argv[argc - 1];
    421 	if (strrchr(argv[argc - 1], ':')) {
    422 		char *cp;
    423 
    424 		for (n = 1; n < argc - 1; n++)
    425 			if (strchr(argv[n], ':')) {
    426 				putusage(argv[0]);
    427 				return;
    428 			}
    429 		cp = argv[argc - 1];
    430 		targ = strrchr(cp, ':');
    431 		*targ++ = 0;
    432 		if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') {
    433 			cp[strlen(cp) - 1] = '\0';
    434 			cp++;
    435 		}
    436 		setpeer0(cp, NULL);
    437 	}
    438 	if (!connected) {
    439 		(void)printf("No target machine specified.\n");
    440 		return;
    441 	}
    442 	if (argc < 4) {
    443 		char *cp = argc == 2 ? tail(targ) : argv[1];
    444 		fd = open(cp, O_RDONLY);
    445 		if (fd < 0) {
    446 			warn("%s", cp);
    447 			return;
    448 		}
    449 		if (verbose)
    450 			(void)printf("putting %s to %s:%s [%s]\n",
    451 				cp, hostname, targ, mode);
    452 		sendfile(fd, targ, mode);
    453 		return;
    454 	}
    455 				/* this assumes the target is a directory */
    456 				/* on a remote unix system.  hmmmm.  */
    457 	p = strchr(targ, '\0');
    458 	*p++ = '/';
    459 	for (n = 1; n < argc - 1; n++) {
    460 		(void)strcpy(p, tail(argv[n]));
    461 		fd = open(argv[n], O_RDONLY);
    462 		if (fd < 0) {
    463 			warn("%s", argv[n]);
    464 			continue;
    465 		}
    466 		if (verbose)
    467 			(void)printf("putting %s to %s:%s [%s]\n",
    468 				argv[n], hostname, targ, mode);
    469 		sendfile(fd, targ, mode);
    470 	}
    471 }
    472 
    473 static void
    474 putusage(s)
    475 	char *s;
    476 {
    477 	(void)printf("usage: %s file ... host:target, or\n", s);
    478 	(void)printf("       %s file ... target (when already connected)\n", s);
    479 }
    480 
    481 /*
    482  * Receive file(s).
    483  */
    484 void
    485 get(argc, argv)
    486 	int argc;
    487 	char *argv[];
    488 {
    489 	int fd;
    490 	int n;
    491 	char *p;
    492 	char *src;
    493 
    494 	if (argc < 2) {
    495 		(void)strlcpy(line, "get ", sizeof(line));
    496 		(void)printf("(files) ");
    497 		(void)fgets(&line[strlen(line)], (int)(LBUFLEN-strlen(line)), stdin);
    498 		makeargv();
    499 		argc = margc;
    500 		argv = margv;
    501 	}
    502 	if (argc < 2) {
    503 		getusage(argv[0]);
    504 		return;
    505 	}
    506 	if (!connected) {
    507 		for (n = 1; n < argc ; n++)
    508 			if (strrchr(argv[n], ':') == 0) {
    509 				getusage(argv[0]);
    510 				return;
    511 			}
    512 	}
    513 	for (n = 1; n < argc ; n++) {
    514 		src = strrchr(argv[n], ':');
    515 		if (src == NULL)
    516 			src = argv[n];
    517 		else {
    518 			char *cp;
    519 			*src++ = 0;
    520 			cp = argv[n];
    521 			if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') {
    522 				cp[strlen(cp) - 1] = '\0';
    523 				cp++;
    524 			}
    525 			setpeer0(cp, NULL);
    526 			if (!connected)
    527 				continue;
    528 		}
    529 		if (argc < 4) {
    530 			char *cp = argc == 3 ? argv[2] : tail(src);
    531 			fd = creat(cp, 0644);
    532 			if (fd < 0) {
    533 				warn("%s", cp);
    534 				return;
    535 			}
    536 			if (verbose)
    537 				(void)printf("getting from %s:%s to %s [%s]\n",
    538 					hostname, src, cp, mode);
    539 			recvfile(fd, src, mode);
    540 			break;
    541 		}
    542 		p = tail(src);         /* new .. jdg */
    543 		fd = creat(p, 0644);
    544 		if (fd < 0) {
    545 			warn("%s", p);
    546 			continue;
    547 		}
    548 		if (verbose)
    549 			(void)printf("getting from %s:%s to %s [%s]\n",
    550 				hostname, src, p, mode);
    551 		recvfile(fd, src, mode);
    552 	}
    553 }
    554 
    555 static void
    556 getusage(s)
    557 	char *s;
    558 {
    559 	(void)printf("usage: %s host:file host:file ... file, or\n", s);
    560 	(void)printf("       %s file file ... file if connected\n", s);
    561 }
    562 
    563 void
    564 setblksize(argc, argv)
    565 	int argc;
    566 	char *argv[];
    567 {
    568 	int t;
    569 
    570 	if (argc < 2) {
    571 		(void)strlcpy(line, "blksize ", sizeof(line));
    572 		(void)printf("(blksize) ");
    573 		(void)fgets(&line[strlen(line)], (int)(LBUFLEN-strlen(line)), stdin);
    574 		makeargv();
    575 		argc = margc;
    576 		argv = margv;
    577 	}
    578 	if (argc != 2) {
    579 		(void)printf("usage: %s value\n", argv[0]);
    580 		return;
    581 	}
    582 	t = atoi(argv[1]);
    583 	if (t < 8 || t > 65464)
    584 		(void)printf("%s: bad value\n", argv[1]);
    585 	else
    586 		blksize = t;
    587 }
    588 
    589 unsigned int	def_rexmtval = TIMEOUT;
    590 unsigned int	rexmtval = TIMEOUT;
    591 
    592 void
    593 setrexmt(argc, argv)
    594 	int argc;
    595 	char *argv[];
    596 {
    597 	int t;
    598 
    599 	if (argc < 2) {
    600 		(void)strlcpy(line, "Rexmt-timeout ", sizeof(line));
    601 		(void)printf("(value) ");
    602 		(void)fgets(&line[strlen(line)], (int)(LBUFLEN-strlen(line)), stdin);
    603 		makeargv();
    604 		argc = margc;
    605 		argv = margv;
    606 	}
    607 	if (argc != 2) {
    608 		(void)printf("usage: %s value\n", argv[0]);
    609 		return;
    610 	}
    611 	t = atoi(argv[1]);
    612 	if (t < 0)
    613 		(void)printf("%s: bad value\n", argv[1]);
    614 	else
    615 		rexmtval = t;
    616 }
    617 
    618 int	maxtimeout = 5 * TIMEOUT;
    619 
    620 void
    621 settimeout(argc, argv)
    622 	int argc;
    623 	char *argv[];
    624 {
    625 	int t;
    626 
    627 	if (argc < 2) {
    628 		(void)strlcpy(line, "Maximum-timeout ", sizeof(line));
    629 		(void)printf("(value) ");
    630 		(void)fgets(&line[strlen(line)], (int)(LBUFLEN-strlen(line)), stdin);
    631 		makeargv();
    632 		argc = margc;
    633 		argv = margv;
    634 	}
    635 	if (argc != 2) {
    636 		(void)printf("usage: %s value\n", argv[0]);
    637 		return;
    638 	}
    639 	t = atoi(argv[1]);
    640 	if (t < 0)
    641 		(void)printf("%s: bad value\n", argv[1]);
    642 	else
    643 		maxtimeout = t;
    644 }
    645 
    646 void
    647 /*ARGSUSED*/
    648 status(argc, argv)
    649 	int argc;
    650 	char *argv[];
    651 {
    652 	if (connected)
    653 		(void)printf("Connected to %s.\n", hostname);
    654 	else
    655 		(void)printf("Not connected.\n");
    656 	(void)printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
    657 		verbose ? "on" : "off", trace ? "on" : "off");
    658 	(void)printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
    659 		rexmtval, maxtimeout);
    660 }
    661 
    662 void
    663 /*ARGSUSED*/
    664 intr(dummy)
    665 	int dummy;
    666 {
    667 
    668 	(void)signal(SIGALRM, SIG_IGN);
    669 	(void)alarm(0);
    670 	longjmp(toplevel, -1);
    671 }
    672 
    673 char *
    674 tail(filename)
    675 	char *filename;
    676 {
    677 	char *s;
    678 
    679 	while (*filename) {
    680 		s = strrchr(filename, '/');
    681 		if (s == NULL)
    682 			break;
    683 		if (s[1])
    684 			return (s + 1);
    685 		*s = '\0';
    686 	}
    687 	return (filename);
    688 }
    689 
    690 /*
    691  * Command parser.
    692  */
    693 static __dead void
    694 command()
    695 {
    696 	const struct cmd *c;
    697 
    698 	for (;;) {
    699 		(void)printf("%s> ", prompt);
    700 		if (fgets(line, LBUFLEN, stdin) == 0) {
    701 			if (feof(stdin)) {
    702 				exit(0);
    703 			} else {
    704 				continue;
    705 			}
    706 		}
    707 		if ((line[0] == 0) || (line[0] == '\n'))
    708 			continue;
    709 		makeargv();
    710 		if (margc == 0)
    711 			continue;
    712 		c = getcmd(margv[0]);
    713 		if (c == (struct cmd *)-1) {
    714 			(void)printf("?Ambiguous command\n");
    715 			continue;
    716 		}
    717 		if (c == 0) {
    718 			(void)printf("?Invalid command\n");
    719 			continue;
    720 		}
    721 		(*c->handler)(margc, margv);
    722 	}
    723 }
    724 
    725 const struct cmd *
    726 getcmd(name)
    727 	char *name;
    728 {
    729 	const char *p, *q;
    730 	const struct cmd *c, *found;
    731 	int nmatches, longest;
    732 
    733 	longest = 0;
    734 	nmatches = 0;
    735 	found = 0;
    736 	for (c = cmdtab; (p = c->name) != NULL; c++) {
    737 		for (q = name; *q == *p++; q++)
    738 			if (*q == 0)		/* exact match? */
    739 				return (c);
    740 		if (!*q) {			/* the name was a prefix */
    741 			if (q - name > longest) {
    742 				longest = q - name;
    743 				nmatches = 1;
    744 				found = c;
    745 			} else if (q - name == longest)
    746 				nmatches++;
    747 		}
    748 	}
    749 	if (nmatches > 1)
    750 		return ((struct cmd *)-1);
    751 	return (found);
    752 }
    753 
    754 /*
    755  * Slice a string up into argc/argv.
    756  */
    757 static void
    758 makeargv()
    759 {
    760 	char *cp;
    761 	char **argp = margv;
    762 
    763 	margc = 0;
    764 	for (cp = line; *cp;) {
    765 		while (isspace((unsigned char)*cp))
    766 			cp++;
    767 		if (*cp == '\0')
    768 			break;
    769 		*argp++ = cp;
    770 		margc += 1;
    771 		while (*cp != '\0' && !isspace((unsigned char)*cp))
    772 			cp++;
    773 		if (*cp == '\0')
    774 			break;
    775 		*cp++ = '\0';
    776 	}
    777 	*argp++ = 0;
    778 }
    779 
    780 void
    781 /*ARGSUSED*/
    782 quit(argc, argv)
    783 	int argc;
    784 	char *argv[];
    785 {
    786 
    787 	exit(0);
    788 }
    789 
    790 /*
    791  * Help command.
    792  */
    793 void
    794 help(argc, argv)
    795 	int argc;
    796 	char *argv[];
    797 {
    798 	const struct cmd *c;
    799 
    800 	if (argc == 1) {
    801 		(void)printf("Commands may be abbreviated.  Commands are:\n\n");
    802 		for (c = cmdtab; c->name; c++)
    803 			(void)printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
    804 		return;
    805 	}
    806 	while (--argc > 0) {
    807 		char *arg;
    808 		arg = *++argv;
    809 		c = getcmd(arg);
    810 		if (c == (struct cmd *)-1)
    811 			(void)printf("?Ambiguous help command %s\n", arg);
    812 		else if (c == (struct cmd *)0)
    813 			(void)printf("?Invalid help command %s\n", arg);
    814 		else
    815 			(void)printf("%s\n", c->help);
    816 	}
    817 }
    818 
    819 void
    820 /*ARGSUSED*/
    821 settrace(argc, argv)
    822 	int argc;
    823 	char **argv;
    824 {
    825 	trace = !trace;
    826 	(void)printf("Packet tracing %s.\n", trace ? "on" : "off");
    827 }
    828 
    829 void
    830 /*ARGSUSED*/
    831 setverbose(argc, argv)
    832 	int argc;
    833 	char **argv;
    834 {
    835 	verbose = !verbose;
    836 	(void)printf("Verbose mode %s.\n", verbose ? "on" : "off");
    837 }
    838 
    839 void
    840 /*ARGSUSED*/
    841 settsize(argc, argv)
    842 	int argc;
    843 	char **argv;
    844 {
    845 	tsize = !tsize;
    846 	(void)printf("Tsize mode %s.\n", tsize ? "on" : "off");
    847 }
    848 
    849 void
    850 /*ARGSUSED*/
    851 settimeoutopt(argc, argv)
    852 	int argc;
    853 	char **argv;
    854 {
    855 	tout = !tout;
    856 	(void)printf("Timeout option %s.\n", tout ? "on" : "off");
    857 }
    858