xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/ftp/main.c (revision 2321:cd490d070379)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2321Ssp149894  * Common Development and Distribution License (the "License").
6*2321Ssp149894  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2321Ssp149894  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  *	Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  *	The Regents of the University of California
320Sstevel@tonic-gate  *	All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  *	contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * FTP User Program -- Command Interface.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate #define	EXTERN
450Sstevel@tonic-gate #include	"ftp_var.h"
460Sstevel@tonic-gate #include	<deflt.h>	/* macros that make using libcmd easier */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static void usage(void);
490Sstevel@tonic-gate static void timeout_sig(int sig);
500Sstevel@tonic-gate static void cmdscanner(int top);
510Sstevel@tonic-gate static void intr(int sig);
520Sstevel@tonic-gate static char *slurpstring(void);
530Sstevel@tonic-gate extern	int use_eprt;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate boolean_t ls_invokes_NLST = B_TRUE;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <gssapi/gssapi.h>
580Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
59*2321Ssp149894 #define	GETOPT_STR	"dginpstvET:axfm:"
60*2321Ssp149894 #define	USAGE_STR	"[-adfginpstvx] [-m mech] [-T timeout] " \
610Sstevel@tonic-gate 			"[hostname [port]]"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int
main(int argc,char * argv[])640Sstevel@tonic-gate main(int argc, char *argv[])
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	char *cp;
670Sstevel@tonic-gate 	int c, top;
680Sstevel@tonic-gate 	struct passwd *pw = NULL;
690Sstevel@tonic-gate 	char homedir[MAXPATHLEN];
700Sstevel@tonic-gate 	char *temp_string = NULL;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	buf = (char *)memalign(getpagesize(), FTPBUFSIZ);
750Sstevel@tonic-gate 	if (buf == NULL) {
760Sstevel@tonic-gate 		(void) fprintf(stderr, "ftp: memory allocation failed\n");
770Sstevel@tonic-gate 		return (1);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	timeoutms = timeout = 0;
810Sstevel@tonic-gate 	doglob = 1;
820Sstevel@tonic-gate 	interactive = 1;
830Sstevel@tonic-gate 	autologin = 1;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	autoauth = 0;
86*2321Ssp149894 	/* by default SYST command will be sent to determine system type */
87*2321Ssp149894 	skipsyst = 0;
880Sstevel@tonic-gate 	fflag = 0;
890Sstevel@tonic-gate 	autoencrypt = 0;
900Sstevel@tonic-gate 	goteof = 0;
910Sstevel@tonic-gate 	mechstr[0] = '\0';
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	sendport = -1;	/* tri-state variable. start out in "automatic" mode. */
940Sstevel@tonic-gate 	passivemode = 0;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	while ((c = getopt(argc, argv, GETOPT_STR)) != EOF) {
970Sstevel@tonic-gate 		switch (c) {
980Sstevel@tonic-gate 		case 'd':
990Sstevel@tonic-gate 			options |= SO_DEBUG;
1000Sstevel@tonic-gate 			debug++;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		case 'g':
1040Sstevel@tonic-gate 			doglob = 0;
1050Sstevel@tonic-gate 			break;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		case 'i':
1080Sstevel@tonic-gate 			interactive = 0;
1090Sstevel@tonic-gate 			break;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		case 'n':
1120Sstevel@tonic-gate 			autologin = 0;
1130Sstevel@tonic-gate 			break;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		case 'p':
1160Sstevel@tonic-gate 			passivemode = 1;
1170Sstevel@tonic-gate 			break;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		case 't':
1200Sstevel@tonic-gate 			trace++;
1210Sstevel@tonic-gate 			break;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		case 'v':
1240Sstevel@tonic-gate 			verbose++;
1250Sstevel@tonic-gate 			break;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 		/* undocumented option: allows testing of EPRT */
1280Sstevel@tonic-gate 		case 'E':
1290Sstevel@tonic-gate 			use_eprt = 1;
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		case 'T':
1330Sstevel@tonic-gate 			if (!isdigit(*optarg)) {
1340Sstevel@tonic-gate 				(void) fprintf(stderr,
1350Sstevel@tonic-gate 					"ftp: bad timeout: \"%s\"\n", optarg);
1360Sstevel@tonic-gate 				break;
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 			timeout = atoi(optarg);
1390Sstevel@tonic-gate 			timeoutms = timeout * MILLISEC;
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		case 'a':
1430Sstevel@tonic-gate 			autoauth = 1;
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		case 'f':
1470Sstevel@tonic-gate 			autoauth = 1;
1480Sstevel@tonic-gate 			fflag = 1;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		case 'm':
1520Sstevel@tonic-gate 			autoauth = 1;
1530Sstevel@tonic-gate 			call(setmech, "ftp", optarg, 0);
1540Sstevel@tonic-gate 			if (code != 0)
1550Sstevel@tonic-gate 				exit(1);
1560Sstevel@tonic-gate 			break;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		case 'x':
1590Sstevel@tonic-gate 			autoauth = 1;
1600Sstevel@tonic-gate 			autoencrypt = 1;
1610Sstevel@tonic-gate 			break;
1620Sstevel@tonic-gate 
163*2321Ssp149894 		case 's':
164*2321Ssp149894 			skipsyst = 1;
165*2321Ssp149894 			break;
166*2321Ssp149894 
1670Sstevel@tonic-gate 		case '?':
1680Sstevel@tonic-gate 		default:
1690Sstevel@tonic-gate 			usage();
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	argc -= optind;
1730Sstevel@tonic-gate 	argv += optind;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (argc > 2)
1760Sstevel@tonic-gate 		usage();
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	fromatty = isatty(fileno(stdin));
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Scan env, then DEFAULTFTPFILE
1810Sstevel@tonic-gate 	 * for FTP_LS_SENDS_NLST
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	temp_string = getenv("FTP_LS_SENDS_NLST");
1840Sstevel@tonic-gate 	if (temp_string == NULL) {	/* env var not set */
1850Sstevel@tonic-gate 		if (defopen(DEFAULTFTPFILE) == 0) {
1860Sstevel@tonic-gate 			/*
1870Sstevel@tonic-gate 			 * turn off case sensitivity
1880Sstevel@tonic-gate 			 */
1890Sstevel@tonic-gate 			int flags = defcntl(DC_GETFLAGS, 0);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 			TURNOFF(flags, DC_CASE);
1920Sstevel@tonic-gate 			(void) defcntl(DC_SETFLAGS, flags);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 			temp_string = defread("FTP_LS_SENDS_NLST=");
1950Sstevel@tonic-gate 			(void) defopen(NULL);	/* close default file */
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	if (temp_string != NULL &&
1990Sstevel@tonic-gate 	    strncasecmp(temp_string, "n", 1) == 0)
2000Sstevel@tonic-gate 		ls_invokes_NLST = B_FALSE;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Set up defaults for FTP.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	(void) strcpy(typename, "ascii"), type = TYPE_A;
2060Sstevel@tonic-gate 	(void) strcpy(formname, "non-print"), form = FORM_N;
2070Sstevel@tonic-gate 	(void) strcpy(modename, "stream"), mode = MODE_S;
2080Sstevel@tonic-gate 	(void) strcpy(structname, "file"), stru = STRU_F;
2090Sstevel@tonic-gate 	(void) strcpy(bytename, "8"), bytesize = 8;
2100Sstevel@tonic-gate 	if (fromatty)
2110Sstevel@tonic-gate 		verbose++;
2120Sstevel@tonic-gate 	cpend = 0;	/* no pending replies */
2130Sstevel@tonic-gate 	proxy = 0;	/* proxy not active */
2140Sstevel@tonic-gate 	crflag = 1;	/* strip c.r. on ascii gets */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (mechstr[0] == '\0') {
2170Sstevel@tonic-gate 		strlcpy(mechstr, FTP_DEF_MECH, MECH_SZ);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/*
2210Sstevel@tonic-gate 	 * Set up the home directory in case we're globbing.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 	cp = getlogin();
2240Sstevel@tonic-gate 	if (cp != NULL) {
2250Sstevel@tonic-gate 		pw = getpwnam(cp);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	if (pw == NULL)
2280Sstevel@tonic-gate 		pw = getpwuid(getuid());
2290Sstevel@tonic-gate 	if (pw != NULL) {
2300Sstevel@tonic-gate 		home = homedir;
2310Sstevel@tonic-gate 		(void) strcpy(home, pw->pw_dir);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	if (setjmp(timeralarm)) {
2340Sstevel@tonic-gate 		(void) fflush(stdout);
2350Sstevel@tonic-gate 		(void) printf("Connection timeout\n");
2360Sstevel@tonic-gate 		exit(1);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	(void) signal(SIGALRM, timeout_sig);
2390Sstevel@tonic-gate 	reset_timer();
2400Sstevel@tonic-gate 	if (argc > 0) {
2410Sstevel@tonic-gate 		int nargc = 0;
2420Sstevel@tonic-gate 		char *nargv[4];
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		if (setjmp(toplevel))
2450Sstevel@tonic-gate 			return (0);
2460Sstevel@tonic-gate 		(void) signal(SIGINT, intr);
2470Sstevel@tonic-gate 		(void) signal(SIGPIPE, lostpeer);
2480Sstevel@tonic-gate 		nargv[nargc++] = "ftp";
2490Sstevel@tonic-gate 		nargv[nargc++] = argv[0];		/* hostname */
2500Sstevel@tonic-gate 		if (argc > 1)
2510Sstevel@tonic-gate 			nargv[nargc++] = argv[1];	/* port */
2520Sstevel@tonic-gate 		nargv[nargc] = NULL;
2530Sstevel@tonic-gate 		setpeer(nargc, nargv);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 	top = setjmp(toplevel) == 0;
2560Sstevel@tonic-gate 	if (top) {
2570Sstevel@tonic-gate 		(void) signal(SIGINT, intr);
2580Sstevel@tonic-gate 		(void) signal(SIGPIPE, lostpeer);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	for (;;) {
2620Sstevel@tonic-gate 		cmdscanner(top);
2630Sstevel@tonic-gate 		top = 1;
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static void
usage(void)2680Sstevel@tonic-gate usage(void)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: ftp %s\n", USAGE_STR);
2710Sstevel@tonic-gate 	exit(1);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate void
reset_timer()2750Sstevel@tonic-gate reset_timer()
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	/* The test is just to reduce syscalls if timeouts aren't used */
2780Sstevel@tonic-gate 	if (timeout)
2790Sstevel@tonic-gate 		alarm(timeout);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate void
stop_timer()2830Sstevel@tonic-gate stop_timer()
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	if (timeout)
2860Sstevel@tonic-gate 		alarm(0);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*ARGSUSED*/
2900Sstevel@tonic-gate static void
timeout_sig(int sig)2910Sstevel@tonic-gate timeout_sig(int sig)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	longjmp(timeralarm, 1);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*ARGSUSED*/
2970Sstevel@tonic-gate static void
intr(int sig)2980Sstevel@tonic-gate intr(int sig)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	longjmp(toplevel, 1);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*ARGSUSED*/
3040Sstevel@tonic-gate void
lostpeer(int sig)3050Sstevel@tonic-gate lostpeer(int sig)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	extern FILE *ctrl_out;
3080Sstevel@tonic-gate 	extern int data;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (connected) {
3110Sstevel@tonic-gate 		if (ctrl_out != NULL) {
3120Sstevel@tonic-gate 			(void) shutdown(fileno(ctrl_out), 1+1);
3130Sstevel@tonic-gate 			(void) fclose(ctrl_out);
3140Sstevel@tonic-gate 			ctrl_out = NULL;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 		if (data >= 0) {
3170Sstevel@tonic-gate 			(void) shutdown(data, 1+1);
3180Sstevel@tonic-gate 			(void) close(data);
3190Sstevel@tonic-gate 			data = -1;
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 		connected = 0;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		auth_type = AUTHTYPE_NONE;
3240Sstevel@tonic-gate 		clevel = dlevel = PROT_C;
3250Sstevel@tonic-gate 		goteof = 0;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 	pswitch(1);
3280Sstevel@tonic-gate 	if (connected) {
3290Sstevel@tonic-gate 		if (ctrl_out != NULL) {
3300Sstevel@tonic-gate 			(void) shutdown(fileno(ctrl_out), 1+1);
3310Sstevel@tonic-gate 			(void) fclose(ctrl_out);
3320Sstevel@tonic-gate 			ctrl_out = NULL;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		connected = 0;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 		auth_type = AUTHTYPE_NONE;
3370Sstevel@tonic-gate 		clevel = dlevel = PROT_C;
3380Sstevel@tonic-gate 		goteof = 0;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	proxflag = 0;
3410Sstevel@tonic-gate 	pswitch(0);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * Command parser.
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate static void
cmdscanner(int top)3480Sstevel@tonic-gate cmdscanner(int top)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	struct cmd *c;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (!top)
3530Sstevel@tonic-gate 		(void) putchar('\n');
3540Sstevel@tonic-gate 	for (;;) {
3550Sstevel@tonic-gate 		stop_timer();
3560Sstevel@tonic-gate 		if (fromatty) {
3570Sstevel@tonic-gate 			(void) printf("ftp> ");
3580Sstevel@tonic-gate 			(void) fflush(stdout);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == 0) {
3610Sstevel@tonic-gate 			if (feof(stdin) || ferror(stdin))
3620Sstevel@tonic-gate 				quit(0, NULL);
3630Sstevel@tonic-gate 			break;
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		if (line[0] == 0)
3660Sstevel@tonic-gate 			break;
3670Sstevel@tonic-gate 		/* If not all, just discard rest of line */
3680Sstevel@tonic-gate 		if (line[strlen(line)-1] != '\n') {
3690Sstevel@tonic-gate 			while (fgetc(stdin) != '\n' && !feof(stdin) &&
3700Sstevel@tonic-gate 			    !ferror(stdin))
3710Sstevel@tonic-gate 				;
3720Sstevel@tonic-gate 			(void) printf("Line too long\n");
3730Sstevel@tonic-gate 			continue;
3740Sstevel@tonic-gate 		} else
3750Sstevel@tonic-gate 			line[strlen(line)-1] = 0;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		makeargv();
3780Sstevel@tonic-gate 		if (margc == 0) {
3790Sstevel@tonic-gate 			continue;
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 		c = getcmd(margv[0]);
3820Sstevel@tonic-gate 		if (c == (struct cmd *)-1) {
3830Sstevel@tonic-gate 			(void) printf("?Ambiguous command\n");
3840Sstevel@tonic-gate 			continue;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		if (c == 0) {
3870Sstevel@tonic-gate 			(void) printf("?Invalid command\n");
3880Sstevel@tonic-gate 			continue;
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 		if (c->c_conn && !connected) {
3910Sstevel@tonic-gate 			(void) printf("Not connected.\n");
3920Sstevel@tonic-gate 			continue;
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		reset_timer();
3950Sstevel@tonic-gate 		(*c->c_handler)(margc, margv);
3960Sstevel@tonic-gate #ifndef CTRL
3970Sstevel@tonic-gate #define	CTRL(c) ((c)&037)
3980Sstevel@tonic-gate #endif
3990Sstevel@tonic-gate 		stop_timer();
4000Sstevel@tonic-gate 		if (bell && c->c_bell)
4010Sstevel@tonic-gate 			(void) putchar(CTRL('g'));
4020Sstevel@tonic-gate 		if (c->c_handler != help)
4030Sstevel@tonic-gate 			break;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	(void) signal(SIGINT, intr);
4060Sstevel@tonic-gate 	(void) signal(SIGPIPE, lostpeer);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate struct cmd *
getcmd(char * name)4100Sstevel@tonic-gate getcmd(char *name)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	char *p, *q;
4130Sstevel@tonic-gate 	struct cmd *c, *found;
4140Sstevel@tonic-gate 	int nmatches, longest;
4150Sstevel@tonic-gate 	extern struct cmd cmdtab[];
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (name == NULL)
4180Sstevel@tonic-gate 		return (0);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	longest = 0;
4210Sstevel@tonic-gate 	nmatches = 0;
4220Sstevel@tonic-gate 	found = 0;
4230Sstevel@tonic-gate 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
4240Sstevel@tonic-gate 		for (q = name; *q == *p++; q++)
4250Sstevel@tonic-gate 			if (*q == 0)		/* exact match? */
4260Sstevel@tonic-gate 				return (c);
4270Sstevel@tonic-gate 		if (!*q) {			/* the name was a prefix */
4280Sstevel@tonic-gate 			if (q - name > longest) {
4290Sstevel@tonic-gate 				longest = q - name;
4300Sstevel@tonic-gate 				nmatches = 1;
4310Sstevel@tonic-gate 				found = c;
4320Sstevel@tonic-gate 			} else if (q - name == longest)
4330Sstevel@tonic-gate 				nmatches++;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 	if (nmatches > 1)
4370Sstevel@tonic-gate 		return ((struct cmd *)-1);
4380Sstevel@tonic-gate 	return (found);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate  * Slice a string up into argc/argv.
4430Sstevel@tonic-gate  */
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static int slrflag;
4460Sstevel@tonic-gate #define	MARGV_INC	20
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate void
makeargv(void)4490Sstevel@tonic-gate makeargv(void)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	char **argp;
4520Sstevel@tonic-gate 	static int margv_size;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	margc = 0;
4550Sstevel@tonic-gate 	stringbase = line;		/* scan from first of buffer */
4560Sstevel@tonic-gate 	argbase = argbuf;		/* store from first of buffer */
4570Sstevel@tonic-gate 	slrflag = 0;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (!margv) {
4600Sstevel@tonic-gate 		margv_size = MARGV_INC;
4610Sstevel@tonic-gate 		if ((margv = malloc(margv_size * sizeof (char *))) == NULL)
4620Sstevel@tonic-gate 			fatal("Out of memory");
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	argp = margv;
4650Sstevel@tonic-gate 	while (*argp++ = slurpstring()) {
4660Sstevel@tonic-gate 		margc++;
4670Sstevel@tonic-gate 		if (margc == margv_size) {
4680Sstevel@tonic-gate 			margv_size += MARGV_INC;
4690Sstevel@tonic-gate 			if ((margv = realloc(margv,
4700Sstevel@tonic-gate 			    margv_size * sizeof (char *))) == NULL)
4710Sstevel@tonic-gate 				fatal("Out of memory");
4720Sstevel@tonic-gate 			argp = margv + margc;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate  * Parse string into argbuf;
4790Sstevel@tonic-gate  * implemented with FSM to
4800Sstevel@tonic-gate  * handle quoting and strings
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate static char *
slurpstring(void)4830Sstevel@tonic-gate slurpstring(void)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	int got_one = 0;
4860Sstevel@tonic-gate 	char *sb = stringbase;
4870Sstevel@tonic-gate 	char *ap = argbase;
4880Sstevel@tonic-gate 	char *tmp = argbase;		/* will return this if token found */
4890Sstevel@tonic-gate 	int	len;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
4920Sstevel@tonic-gate 		switch (slrflag) {	/* and $ as token for macro invoke */
4930Sstevel@tonic-gate 			case 0:
4940Sstevel@tonic-gate 				slrflag++;
4950Sstevel@tonic-gate 				stringbase++;
4960Sstevel@tonic-gate 				return ((*sb == '!') ? "!" : "$");
4970Sstevel@tonic-gate 			case 1:
4980Sstevel@tonic-gate 				slrflag++;
4990Sstevel@tonic-gate 				altarg = stringbase;
5000Sstevel@tonic-gate 				break;
5010Sstevel@tonic-gate 			default:
5020Sstevel@tonic-gate 				break;
5030Sstevel@tonic-gate 		}
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate S0:
5070Sstevel@tonic-gate 	switch (*sb) {
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	case '\0':
5100Sstevel@tonic-gate 		goto OUT;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	case ' ':
5130Sstevel@tonic-gate 	case '\t':
5140Sstevel@tonic-gate 		sb++; goto S0;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	default:
5170Sstevel@tonic-gate 		switch (slrflag) {
5180Sstevel@tonic-gate 			case 0:
5190Sstevel@tonic-gate 				slrflag++;
5200Sstevel@tonic-gate 				break;
5210Sstevel@tonic-gate 			case 1:
5220Sstevel@tonic-gate 				slrflag++;
5230Sstevel@tonic-gate 				altarg = sb;
5240Sstevel@tonic-gate 				break;
5250Sstevel@tonic-gate 			default:
5260Sstevel@tonic-gate 				break;
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 		goto S1;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate S1:
5320Sstevel@tonic-gate 	switch (*sb) {
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	case ' ':
5350Sstevel@tonic-gate 	case '\t':
5360Sstevel@tonic-gate 	case '\0':
5370Sstevel@tonic-gate 		goto OUT;	/* end of token */
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	case '\\':
5400Sstevel@tonic-gate 		sb++; goto S2;	/* slurp next character */
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	case '"':
5430Sstevel@tonic-gate 		sb++; goto S3;	/* slurp quoted string */
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	default:
5460Sstevel@tonic-gate 		if ((len = mblen(sb, MB_CUR_MAX)) <= 0)
5470Sstevel@tonic-gate 			len = 1;
5480Sstevel@tonic-gate 		memcpy(ap, sb, len);
5490Sstevel@tonic-gate 		ap += len;
5500Sstevel@tonic-gate 		sb += len;
5510Sstevel@tonic-gate 		got_one = 1;
5520Sstevel@tonic-gate 		goto S1;
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate S2:
5560Sstevel@tonic-gate 	switch (*sb) {
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	case '\0':
5590Sstevel@tonic-gate 		goto OUT;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	default:
5620Sstevel@tonic-gate 		if ((len = mblen(sb, MB_CUR_MAX)) <= 0)
5630Sstevel@tonic-gate 			len = 1;
5640Sstevel@tonic-gate 		memcpy(ap, sb, len);
5650Sstevel@tonic-gate 		ap += len;
5660Sstevel@tonic-gate 		sb += len;
5670Sstevel@tonic-gate 		got_one = 1;
5680Sstevel@tonic-gate 		goto S1;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate S3:
5720Sstevel@tonic-gate 	switch (*sb) {
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	case '\0':
5750Sstevel@tonic-gate 		goto OUT;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	case '"':
5780Sstevel@tonic-gate 		sb++; goto S1;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	default:
5810Sstevel@tonic-gate 		if ((len = mblen(sb, MB_CUR_MAX)) <= 0)
5820Sstevel@tonic-gate 			len = 1;
5830Sstevel@tonic-gate 		memcpy(ap, sb, len);
5840Sstevel@tonic-gate 		ap += len;
5850Sstevel@tonic-gate 		sb += len;
5860Sstevel@tonic-gate 		got_one = 1;
5870Sstevel@tonic-gate 		goto S3;
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate OUT:
5910Sstevel@tonic-gate 	if (got_one)
5920Sstevel@tonic-gate 		*ap++ = '\0';
5930Sstevel@tonic-gate 	argbase = ap;			/* update storage pointer */
5940Sstevel@tonic-gate 	stringbase = sb;		/* update scan pointer */
5950Sstevel@tonic-gate 	if (got_one) {
5960Sstevel@tonic-gate 		return (tmp);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 	switch (slrflag) {
5990Sstevel@tonic-gate 		case 0:
6000Sstevel@tonic-gate 			slrflag++;
6010Sstevel@tonic-gate 			break;
6020Sstevel@tonic-gate 		case 1:
6030Sstevel@tonic-gate 			slrflag++;
6040Sstevel@tonic-gate 			altarg = (char *)0;
6050Sstevel@tonic-gate 			break;
6060Sstevel@tonic-gate 		default:
6070Sstevel@tonic-gate 			break;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	return ((char *)0);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate #define	HELPINDENT (sizeof ("directory"))
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate  * Help command.
6160Sstevel@tonic-gate  * Call each command handler with argc == 0 and argv[0] == name.
6170Sstevel@tonic-gate  */
6180Sstevel@tonic-gate void
help(int argc,char * argv[])6190Sstevel@tonic-gate help(int argc, char *argv[])
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	struct cmd *c;
6220Sstevel@tonic-gate 	extern struct cmd cmdtab[];
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (argc == 1) {
6250Sstevel@tonic-gate 		int i, j, w, k;
6260Sstevel@tonic-gate 		int columns, width = 0, lines;
6270Sstevel@tonic-gate 		extern int NCMDS;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		(void) printf(
6300Sstevel@tonic-gate 			"Commands may be abbreviated.  Commands are:\n\n");
6310Sstevel@tonic-gate 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
6320Sstevel@tonic-gate 			int len = strlen(c->c_name);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 			if (len > width)
6350Sstevel@tonic-gate 				width = len;
6360Sstevel@tonic-gate 		}
6370Sstevel@tonic-gate 		width = (width + 8) &~ 7;
6380Sstevel@tonic-gate 		columns = 80 / width;
6390Sstevel@tonic-gate 		if (columns == 0)
6400Sstevel@tonic-gate 			columns = 1;
6410Sstevel@tonic-gate 		lines = (NCMDS + columns - 1) / columns;
6420Sstevel@tonic-gate 		for (i = 0; i < lines; i++) {
6430Sstevel@tonic-gate 			for (j = 0; j < columns; j++) {
6440Sstevel@tonic-gate 				c = cmdtab + j * lines + i;
6450Sstevel@tonic-gate 				if (c->c_name && (!proxy || c->c_proxy)) {
6460Sstevel@tonic-gate 					(void) printf("%s", c->c_name);
6470Sstevel@tonic-gate 				} else if (c->c_name) {
6480Sstevel@tonic-gate 					for (k = 0; k < strlen(c->c_name);
6490Sstevel@tonic-gate 					    k++) {
6500Sstevel@tonic-gate 						(void) putchar(' ');
6510Sstevel@tonic-gate 					}
6520Sstevel@tonic-gate 				}
6530Sstevel@tonic-gate 				if (c + lines >= &cmdtab[NCMDS]) {
6540Sstevel@tonic-gate 					(void) printf("\n");
6550Sstevel@tonic-gate 					break;
6560Sstevel@tonic-gate 				}
6570Sstevel@tonic-gate 				w = strlen(c->c_name);
6580Sstevel@tonic-gate 				while (w < width) {
6590Sstevel@tonic-gate 					w = (w + 8) &~ 7;
6600Sstevel@tonic-gate 					(void) putchar('\t');
6610Sstevel@tonic-gate 				}
6620Sstevel@tonic-gate 			}
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 		return;
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 	while (--argc > 0) {
6670Sstevel@tonic-gate 		char *arg;
6680Sstevel@tonic-gate 		arg = *++argv;
6690Sstevel@tonic-gate 		c = getcmd(arg);
6700Sstevel@tonic-gate 		if (c == (struct cmd *)-1)
6710Sstevel@tonic-gate 			(void) printf("?Ambiguous help command %s\n", arg);
6720Sstevel@tonic-gate 		else if (c == (struct cmd *)0)
6730Sstevel@tonic-gate 			(void) printf("?Invalid help command %s\n", arg);
6740Sstevel@tonic-gate 		else
6750Sstevel@tonic-gate 			(void) printf("%-*s\t%s\n", HELPINDENT,
6760Sstevel@tonic-gate 				c->c_name, c->c_help);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate  * Call routine with argc, argv set from args (terminated by 0).
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate void
call(void (* routine)(int argc,char * argv[]),...)6840Sstevel@tonic-gate call(void (*routine)(int argc, char *argv[]), ...)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	va_list ap;
6870Sstevel@tonic-gate 	char *argv[10];
6880Sstevel@tonic-gate 	int argc = 0;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	va_start(ap, routine);
6910Sstevel@tonic-gate 	while ((argv[argc] = va_arg(ap, char *)) != (char *)0)
6920Sstevel@tonic-gate 		argc++;
6930Sstevel@tonic-gate 	va_end(ap);
6940Sstevel@tonic-gate 	(*routine)(argc, argv);
6950Sstevel@tonic-gate }
696