Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.32
      1  1.32     lukem /*	$NetBSD: main.c,v 1.32 1998/08/03 01:49:26 lukem Exp $	*/
      2  1.12     lukem 
      3   1.1       cgd /*
      4   1.3       cgd  * Copyright (c) 1985, 1989, 1993, 1994
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.23     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.23     lukem __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
     39  1.23     lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.8       tls #if 0
     44   1.8       tls static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 10/9/94";
     45   1.8       tls #else
     46  1.32     lukem __RCSID("$NetBSD: main.c,v 1.32 1998/08/03 01:49:26 lukem Exp $");
     47   1.8       tls #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd /*
     51   1.1       cgd  * FTP User Program -- Command Interface.
     52   1.1       cgd  */
     53   1.3       cgd #include <sys/types.h>
     54   1.1       cgd #include <sys/socket.h>
     55   1.1       cgd 
     56   1.3       cgd #include <err.h>
     57   1.1       cgd #include <netdb.h>
     58   1.1       cgd #include <pwd.h>
     59   1.3       cgd #include <stdio.h>
     60  1.22     lukem #include <stdlib.h>
     61   1.9       cgd #include <string.h>
     62   1.3       cgd #include <unistd.h>
     63   1.1       cgd 
     64   1.3       cgd #include "ftp_var.h"
     65  1.25     lukem #include "pathnames.h"
     66   1.1       cgd 
     67  1.23     lukem int main __P((int, char **));
     68  1.23     lukem 
     69   1.3       cgd int
     70   1.1       cgd main(argc, argv)
     71   1.3       cgd 	int argc;
     72   1.1       cgd 	char *argv[];
     73   1.1       cgd {
     74  1.16     lukem 	struct servent *sp;
     75  1.23     lukem 	int ch, top, rval;
     76  1.23     lukem 	long port;
     77   1.1       cgd 	struct passwd *pw = NULL;
     78  1.24     lukem 	char *cp, *ep, homedir[MAXPATHLEN];
     79  1.31     lukem 	char *outfile = NULL;
     80  1.22     lukem 	int dumbterm;
     81   1.1       cgd 
     82   1.1       cgd 	sp = getservbyname("ftp", "tcp");
     83   1.3       cgd 	if (sp == 0)
     84  1.16     lukem 		ftpport = htons(FTP_PORT);	/* good fallback */
     85  1.16     lukem 	else
     86  1.16     lukem 		ftpport = sp->s_port;
     87  1.16     lukem 	sp = getservbyname("http", "tcp");
     88  1.16     lukem 	if (sp == 0)
     89  1.16     lukem 		httpport = htons(HTTP_PORT);	/* good fallback */
     90  1.16     lukem 	else
     91  1.16     lukem 		httpport = sp->s_port;
     92  1.24     lukem 	gateport = 0;
     93  1.24     lukem 	cp = getenv("FTPSERVERPORT");
     94  1.24     lukem 	if (cp != NULL) {
     95  1.24     lukem 		port = strtol(cp, &ep, 10);
     96  1.29     lukem 		if (port < 1 || port > MAX_IN_PORT_T || *ep != '\0')
     97  1.31     lukem 			warnx("bad $FTPSERVERPORT port number: %s (ignored)",
     98  1.24     lukem 			    cp);
     99  1.24     lukem 		else
    100  1.24     lukem 			gateport = htons(port);
    101  1.24     lukem 	}
    102  1.24     lukem 	if (gateport == 0) {
    103  1.24     lukem 		sp = getservbyname("ftpgate", "tcp");
    104  1.24     lukem 		if (sp == 0)
    105  1.24     lukem 			gateport = htons(GATE_PORT);
    106  1.24     lukem 		else
    107  1.24     lukem 			gateport = sp->s_port;
    108  1.24     lukem 	}
    109   1.1       cgd 	doglob = 1;
    110   1.1       cgd 	interactive = 1;
    111   1.1       cgd 	autologin = 1;
    112  1.31     lukem 	passivemode = 1;
    113  1.31     lukem 	activefallback = 1;
    114  1.13     lukem 	preserve = 1;
    115  1.17     lukem 	verbose = 0;
    116  1.17     lukem 	progress = 0;
    117  1.24     lukem 	gatemode = 0;
    118  1.19  christos #ifndef SMALL
    119  1.18     lukem 	editing = 0;
    120  1.21     lukem 	el = NULL;
    121  1.21     lukem 	hist = NULL;
    122  1.18     lukem #endif
    123  1.12     lukem 	mark = HASHBYTES;
    124  1.16     lukem 	marg_sl = sl_init();
    125  1.25     lukem 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    126  1.25     lukem 		tmpdir = _PATH_TMP;
    127   1.3       cgd 
    128  1.31     lukem 	/* Set default operation mode based on FTPMODE environment variable */
    129  1.31     lukem 	if ((cp = getenv("FTPMODE")) != NULL) {
    130  1.31     lukem 		if (strcmp(cp, "passive") == 0) {
    131  1.31     lukem 			passivemode = 1;
    132  1.31     lukem 			activefallback = 0;
    133  1.31     lukem 		} else if (strcmp(cp, "active") == 0) {
    134  1.31     lukem 			passivemode = 0;
    135  1.31     lukem 			activefallback = 0;
    136  1.31     lukem 		} else if (strcmp(cp, "gate") == 0) {
    137  1.31     lukem 			gatemode = 1;
    138  1.31     lukem 		} else if (strcmp(cp, "auto") == 0) {
    139  1.31     lukem 			passivemode = 1;
    140  1.31     lukem 			activefallback = 1;
    141  1.31     lukem 		} else
    142  1.31     lukem 			warnx("unknown $FTPMODE '%s'; using defaults", cp);
    143  1.31     lukem 	}
    144  1.31     lukem 
    145  1.32     lukem 	if (strcmp(__progname, "pftp") == 0) {
    146  1.15     lukem 		passivemode = 1;
    147  1.31     lukem 		activefallback = 0;
    148  1.32     lukem 	} else if (strcmp(__progname, "gate-ftp") == 0)
    149  1.24     lukem 		gatemode = 1;
    150  1.24     lukem 
    151  1.24     lukem 	gateserver = getenv("FTPSERVER");
    152  1.24     lukem 	if (gateserver == NULL || *gateserver == '\0')
    153  1.24     lukem 		gateserver = GATE_SERVER;
    154  1.24     lukem 	if (gatemode) {
    155  1.24     lukem 		if (*gateserver == '\0') {
    156  1.24     lukem 			warnx(
    157  1.31     lukem "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
    158  1.24     lukem 			gatemode = 0;
    159  1.24     lukem 		}
    160  1.24     lukem 	}
    161  1.15     lukem 
    162  1.22     lukem 	cp = getenv("TERM");
    163  1.22     lukem 	if (cp == NULL || strcmp(cp, "dumb") == 0)
    164  1.22     lukem 		dumbterm = 1;
    165  1.22     lukem 	else
    166  1.22     lukem 		dumbterm = 0;
    167  1.17     lukem 	fromatty = isatty(fileno(stdin));
    168  1.18     lukem 	if (fromatty) {
    169  1.17     lukem 		verbose = 1;		/* verbose if from a tty */
    170  1.19  christos #ifndef SMALL
    171  1.22     lukem 		if (! dumbterm)
    172  1.22     lukem 			editing = 1;	/* editing mode on if tty is usable */
    173  1.18     lukem #endif
    174  1.18     lukem 	}
    175  1.31     lukem 	ttyout = stdout;
    176  1.27       gwr #ifndef SMALL
    177  1.31     lukem 	if (isatty(fileno(ttyout)) && !dumbterm && foregroundproc())
    178  1.22     lukem 		progress = 1;		/* progress bar on if tty is usable */
    179  1.27       gwr #endif
    180  1.17     lukem 
    181  1.31     lukem 	while ((ch = getopt(argc, argv, "Aadegino:pP:r:tvV")) != -1) {
    182   1.6   mycroft 		switch (ch) {
    183  1.31     lukem 		case 'A':
    184  1.31     lukem 			activefallback = 0;
    185  1.31     lukem 			passivemode = 0;
    186  1.31     lukem 			break;
    187  1.31     lukem 
    188  1.12     lukem 		case 'a':
    189  1.12     lukem 			anonftp = 1;
    190  1.12     lukem 			break;
    191  1.12     lukem 
    192   1.3       cgd 		case 'd':
    193   1.3       cgd 			options |= SO_DEBUG;
    194   1.3       cgd 			debug++;
    195   1.3       cgd 			break;
    196  1.12     lukem 
    197  1.18     lukem 		case 'e':
    198  1.19  christos #ifndef SMALL
    199  1.18     lukem 			editing = 0;
    200  1.18     lukem #endif
    201  1.18     lukem 			break;
    202  1.18     lukem 
    203   1.3       cgd 		case 'g':
    204   1.3       cgd 			doglob = 0;
    205   1.3       cgd 			break;
    206   1.1       cgd 
    207   1.3       cgd 		case 'i':
    208   1.3       cgd 			interactive = 0;
    209   1.3       cgd 			break;
    210   1.1       cgd 
    211   1.3       cgd 		case 'n':
    212   1.3       cgd 			autologin = 0;
    213   1.3       cgd 			break;
    214   1.1       cgd 
    215  1.31     lukem 		case 'o':
    216  1.31     lukem 			outfile = optarg;
    217  1.31     lukem 			if (strcmp(outfile, "-") == 0)
    218  1.31     lukem 				ttyout = stderr;
    219  1.31     lukem 			break;
    220  1.31     lukem 
    221  1.12     lukem 		case 'p':
    222  1.12     lukem 			passivemode = 1;
    223  1.31     lukem 			activefallback = 0;
    224  1.12     lukem 			break;
    225  1.12     lukem 
    226  1.12     lukem 		case 'P':
    227  1.24     lukem 			port = strtol(optarg, &ep, 10);
    228  1.29     lukem 			if (port < 1 || port > MAX_IN_PORT_T || *ep != '\0')
    229  1.18     lukem 				warnx("bad port number: %s (ignored)", optarg);
    230  1.16     lukem 			else
    231  1.28     lukem 				ftpport = htons((in_port_t)port);
    232  1.12     lukem 			break;
    233  1.12     lukem 
    234  1.31     lukem 		case 'r':
    235  1.31     lukem 			retry_connect = strtol(optarg, &ep, 10);
    236  1.31     lukem 			if (retry_connect < 1 || retry_connect > MAX_IN_PORT_T
    237  1.31     lukem 			    || *ep != '\0')
    238  1.31     lukem 				errx(1, "bad retry value: %s", optarg);
    239  1.31     lukem 			break;
    240  1.31     lukem 
    241   1.3       cgd 		case 't':
    242  1.17     lukem 			trace = 1;
    243   1.3       cgd 			break;
    244   1.1       cgd 
    245   1.3       cgd 		case 'v':
    246  1.17     lukem 			verbose = 1;
    247  1.17     lukem 			break;
    248  1.17     lukem 
    249  1.17     lukem 		case 'V':
    250  1.17     lukem 			verbose = 0;
    251   1.3       cgd 			break;
    252   1.1       cgd 
    253   1.3       cgd 		default:
    254  1.14     lukem 			usage();
    255   1.3       cgd 		}
    256   1.1       cgd 	}
    257   1.3       cgd 	argc -= optind;
    258   1.3       cgd 	argv += optind;
    259   1.3       cgd 
    260   1.1       cgd 	cpend = 0;	/* no pending replies */
    261   1.1       cgd 	proxy = 0;	/* proxy not active */
    262   1.1       cgd 	crflag = 1;	/* strip c.r. on ascii gets */
    263   1.1       cgd 	sendport = -1;	/* not using ports */
    264   1.1       cgd 	/*
    265   1.1       cgd 	 * Set up the home directory in case we're globbing.
    266   1.1       cgd 	 */
    267   1.1       cgd 	cp = getlogin();
    268   1.1       cgd 	if (cp != NULL) {
    269   1.1       cgd 		pw = getpwnam(cp);
    270   1.1       cgd 	}
    271   1.1       cgd 	if (pw == NULL)
    272   1.1       cgd 		pw = getpwuid(getuid());
    273   1.1       cgd 	if (pw != NULL) {
    274   1.1       cgd 		home = homedir;
    275  1.18     lukem 		(void)strcpy(home, pw->pw_dir);
    276   1.1       cgd 	}
    277  1.12     lukem 
    278  1.17     lukem 	setttywidth(0);
    279  1.18     lukem 	(void)signal(SIGWINCH, setttywidth);
    280  1.23     lukem 
    281  1.31     lukem #ifdef __GNUC__			/* to shut up gcc warnings */
    282  1.23     lukem 	(void)&argc;
    283  1.23     lukem 	(void)&argv;
    284  1.23     lukem #endif
    285  1.17     lukem 
    286   1.1       cgd 	if (argc > 0) {
    287  1.16     lukem 		if (strchr(argv[0], ':') != NULL) {
    288  1.16     lukem 			anonftp = 1;	/* Handle "automatic" transfers. */
    289  1.31     lukem 			rval = auto_fetch(argc, argv, outfile);
    290  1.16     lukem 			if (rval >= 0)		/* -1 == connected and cd-ed */
    291  1.16     lukem 				exit(rval);
    292  1.16     lukem 		} else {
    293  1.16     lukem 			char *xargv[5];
    294   1.3       cgd 
    295  1.16     lukem 			if (setjmp(toplevel))
    296  1.16     lukem 				exit(0);
    297  1.18     lukem 			(void)signal(SIGINT, (sig_t)intr);
    298  1.18     lukem 			(void)signal(SIGPIPE, (sig_t)lostpeer);
    299  1.16     lukem 			xargv[0] = __progname;
    300  1.16     lukem 			xargv[1] = argv[0];
    301  1.16     lukem 			xargv[2] = argv[1];
    302  1.16     lukem 			xargv[3] = argv[2];
    303  1.16     lukem 			xargv[4] = NULL;
    304  1.31     lukem 			do {
    305  1.31     lukem 				setpeer(argc+1, xargv);
    306  1.31     lukem 				if (!retry_connect)
    307  1.31     lukem 					break;
    308  1.31     lukem 				if (!connected) {
    309  1.31     lukem 					macnum = 0;
    310  1.31     lukem 					fprintf(ttyout,
    311  1.31     lukem 					    "Retrying in %d seconds...\n",
    312  1.31     lukem 					    retry_connect);
    313  1.31     lukem 					sleep(retry_connect);
    314  1.31     lukem 				}
    315  1.31     lukem 			} while (!connected);
    316  1.31     lukem 			retry_connect = 0; /* connected, stop hiding msgs */
    317  1.16     lukem 		}
    318   1.1       cgd 	}
    319  1.21     lukem #ifndef SMALL
    320  1.21     lukem 	controlediting();
    321  1.21     lukem #endif /* !SMALL */
    322   1.1       cgd 	top = setjmp(toplevel) == 0;
    323   1.1       cgd 	if (top) {
    324  1.18     lukem 		(void)signal(SIGINT, (sig_t)intr);
    325  1.18     lukem 		(void)signal(SIGPIPE, (sig_t)lostpeer);
    326   1.1       cgd 	}
    327   1.1       cgd 	for (;;) {
    328   1.1       cgd 		cmdscanner(top);
    329   1.1       cgd 		top = 1;
    330   1.1       cgd 	}
    331   1.1       cgd }
    332   1.1       cgd 
    333   1.1       cgd void
    334   1.1       cgd intr()
    335   1.1       cgd {
    336   1.1       cgd 
    337  1.17     lukem 	alarmtimer(0);
    338   1.1       cgd 	longjmp(toplevel, 1);
    339   1.1       cgd }
    340   1.1       cgd 
    341   1.1       cgd void
    342   1.1       cgd lostpeer()
    343   1.1       cgd {
    344   1.1       cgd 
    345  1.17     lukem 	alarmtimer(0);
    346   1.1       cgd 	if (connected) {
    347   1.1       cgd 		if (cout != NULL) {
    348  1.18     lukem 			(void)shutdown(fileno(cout), 1+1);
    349  1.18     lukem 			(void)fclose(cout);
    350   1.1       cgd 			cout = NULL;
    351   1.1       cgd 		}
    352   1.1       cgd 		if (data >= 0) {
    353  1.18     lukem 			(void)shutdown(data, 1+1);
    354  1.18     lukem 			(void)close(data);
    355   1.1       cgd 			data = -1;
    356   1.1       cgd 		}
    357   1.1       cgd 		connected = 0;
    358   1.1       cgd 	}
    359   1.1       cgd 	pswitch(1);
    360   1.1       cgd 	if (connected) {
    361   1.1       cgd 		if (cout != NULL) {
    362  1.18     lukem 			(void)shutdown(fileno(cout), 1+1);
    363  1.18     lukem 			(void)fclose(cout);
    364   1.1       cgd 			cout = NULL;
    365   1.1       cgd 		}
    366   1.1       cgd 		connected = 0;
    367   1.1       cgd 	}
    368   1.1       cgd 	proxflag = 0;
    369   1.1       cgd 	pswitch(0);
    370   1.1       cgd }
    371   1.1       cgd 
    372   1.3       cgd /*
    373  1.16     lukem  * Generate a prompt
    374  1.16     lukem  */
    375   1.3       cgd char *
    376  1.16     lukem prompt()
    377   1.1       cgd {
    378  1.16     lukem 	return ("ftp> ");
    379   1.1       cgd }
    380   1.3       cgd 
    381   1.1       cgd /*
    382   1.1       cgd  * Command parser.
    383   1.1       cgd  */
    384   1.3       cgd void
    385   1.1       cgd cmdscanner(top)
    386   1.1       cgd 	int top;
    387   1.1       cgd {
    388   1.3       cgd 	struct cmd *c;
    389  1.16     lukem 	int num;
    390   1.1       cgd 
    391  1.16     lukem 	if (!top
    392  1.19  christos #ifndef SMALL
    393  1.16     lukem 	    && !editing
    394  1.19  christos #endif /* !SMALL */
    395  1.16     lukem 	    )
    396  1.31     lukem 		(void)putc('\n', ttyout);
    397   1.1       cgd 	for (;;) {
    398  1.19  christos #ifndef SMALL
    399  1.16     lukem 		if (!editing) {
    400  1.19  christos #endif /* !SMALL */
    401  1.16     lukem 			if (fromatty) {
    402  1.31     lukem 				fputs(prompt(), ttyout);
    403  1.31     lukem 				(void)fflush(ttyout);
    404  1.16     lukem 			}
    405  1.16     lukem 			if (fgets(line, sizeof(line), stdin) == NULL)
    406  1.16     lukem 				quit(0, 0);
    407  1.16     lukem 			num = strlen(line);
    408  1.16     lukem 			if (num == 0)
    409  1.16     lukem 				break;
    410  1.16     lukem 			if (line[--num] == '\n') {
    411  1.16     lukem 				if (num == 0)
    412  1.16     lukem 					break;
    413  1.16     lukem 				line[num] = '\0';
    414  1.16     lukem 			} else if (num == sizeof(line) - 2) {
    415  1.31     lukem 				fputs("sorry, input line too long.\n", ttyout);
    416  1.16     lukem 				while ((num = getchar()) != '\n' && num != EOF)
    417  1.16     lukem 					/* void */;
    418  1.16     lukem 				break;
    419  1.16     lukem 			} /* else it was a line without a newline */
    420  1.19  christos #ifndef SMALL
    421  1.17     lukem 		} else {
    422  1.16     lukem 			const char *buf;
    423  1.26  christos 			HistEvent ev;
    424  1.16     lukem 			cursor_pos = NULL;
    425  1.16     lukem 
    426  1.16     lukem 			if ((buf = el_gets(el, &num)) == NULL || num == 0)
    427  1.17     lukem 				quit(0, 0);
    428  1.16     lukem 			if (line[--num] == '\n') {
    429  1.16     lukem 				if (num == 0)
    430  1.16     lukem 					break;
    431  1.16     lukem 			} else if (num >= sizeof(line)) {
    432  1.31     lukem 				fputs("sorry, input line too long.\n", ttyout);
    433   1.1       cgd 				break;
    434  1.16     lukem 			}
    435  1.16     lukem 			memcpy(line, buf, num);
    436  1.16     lukem 			line[num] = '\0';
    437  1.26  christos 			history(hist, &ev, H_ENTER, buf);
    438  1.16     lukem 		}
    439  1.19  christos #endif /* !SMALL */
    440  1.16     lukem 
    441   1.1       cgd 		makeargv();
    442  1.16     lukem 		if (margc == 0)
    443   1.1       cgd 			continue;
    444   1.1       cgd 		c = getcmd(margv[0]);
    445   1.1       cgd 		if (c == (struct cmd *)-1) {
    446  1.31     lukem 			fputs("?Ambiguous command.\n", ttyout);
    447   1.1       cgd 			continue;
    448   1.1       cgd 		}
    449  1.30     lukem 		if (c == NULL) {
    450  1.30     lukem #if !defined(SMALL)
    451  1.30     lukem 			/*
    452  1.30     lukem 			 * attempt to el_parse() unknown commands.
    453  1.30     lukem 			 * any command containing a ':' would be parsed
    454  1.30     lukem 			 * as "[prog:]cmd ...", and will result in a
    455  1.30     lukem 			 * false positive if prog != "ftp", so treat
    456  1.30     lukem 			 * such commands as invalid.
    457  1.30     lukem 			 */
    458  1.30     lukem 			if (strchr(margv[0], ':') != NULL ||
    459  1.30     lukem 			    el_parse(el, margc, margv) != 0)
    460  1.30     lukem #endif /* !SMALL */
    461  1.31     lukem 				fputs("?Invalid command.\n", ttyout);
    462   1.1       cgd 			continue;
    463   1.1       cgd 		}
    464   1.1       cgd 		if (c->c_conn && !connected) {
    465  1.31     lukem 			fputs("Not connected.\n", ttyout);
    466   1.1       cgd 			continue;
    467   1.1       cgd 		}
    468  1.13     lukem 		confirmrest = 0;
    469   1.1       cgd 		(*c->c_handler)(margc, margv);
    470   1.1       cgd 		if (bell && c->c_bell)
    471  1.31     lukem 			(void)putc('\007', ttyout);
    472   1.1       cgd 		if (c->c_handler != help)
    473   1.1       cgd 			break;
    474   1.1       cgd 	}
    475  1.18     lukem 	(void)signal(SIGINT, (sig_t)intr);
    476  1.18     lukem 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    477   1.1       cgd }
    478   1.1       cgd 
    479   1.1       cgd struct cmd *
    480   1.1       cgd getcmd(name)
    481  1.13     lukem 	const char *name;
    482   1.1       cgd {
    483  1.13     lukem 	const char *p, *q;
    484   1.3       cgd 	struct cmd *c, *found;
    485   1.3       cgd 	int nmatches, longest;
    486  1.11        pk 
    487  1.11        pk 	if (name == NULL)
    488  1.11        pk 		return (0);
    489   1.1       cgd 
    490   1.1       cgd 	longest = 0;
    491   1.1       cgd 	nmatches = 0;
    492   1.1       cgd 	found = 0;
    493  1.12     lukem 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    494   1.1       cgd 		for (q = name; *q == *p++; q++)
    495   1.1       cgd 			if (*q == 0)		/* exact match? */
    496   1.1       cgd 				return (c);
    497   1.1       cgd 		if (!*q) {			/* the name was a prefix */
    498   1.1       cgd 			if (q - name > longest) {
    499   1.1       cgd 				longest = q - name;
    500   1.1       cgd 				nmatches = 1;
    501   1.1       cgd 				found = c;
    502   1.1       cgd 			} else if (q - name == longest)
    503   1.1       cgd 				nmatches++;
    504   1.1       cgd 		}
    505   1.1       cgd 	}
    506   1.1       cgd 	if (nmatches > 1)
    507   1.1       cgd 		return ((struct cmd *)-1);
    508   1.1       cgd 	return (found);
    509   1.1       cgd }
    510   1.1       cgd 
    511   1.1       cgd /*
    512   1.1       cgd  * Slice a string up into argc/argv.
    513   1.1       cgd  */
    514   1.1       cgd 
    515   1.1       cgd int slrflag;
    516   1.1       cgd 
    517   1.3       cgd void
    518   1.1       cgd makeargv()
    519   1.1       cgd {
    520  1.16     lukem 	char *argp;
    521   1.1       cgd 
    522   1.1       cgd 	stringbase = line;		/* scan from first of buffer */
    523   1.1       cgd 	argbase = argbuf;		/* store from first of buffer */
    524   1.1       cgd 	slrflag = 0;
    525  1.16     lukem 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    526  1.10        pk 	for (margc = 0; ; margc++) {
    527  1.16     lukem 		argp = slurpstring();
    528  1.16     lukem 		sl_add(marg_sl, argp);
    529  1.16     lukem 		if (argp == NULL)
    530  1.10        pk 			break;
    531  1.10        pk 	}
    532  1.19  christos #ifndef SMALL
    533  1.16     lukem 	if (cursor_pos == line) {
    534  1.16     lukem 		cursor_argc = 0;
    535  1.16     lukem 		cursor_argo = 0;
    536  1.16     lukem 	} else if (cursor_pos != NULL) {
    537  1.16     lukem 		cursor_argc = margc;
    538  1.16     lukem 		cursor_argo = strlen(margv[margc-1]);
    539  1.16     lukem 	}
    540  1.19  christos #endif /* !SMALL */
    541  1.16     lukem }
    542  1.10        pk 
    543  1.19  christos #ifdef SMALL
    544  1.16     lukem #define INC_CHKCURSOR(x)	(x)++
    545  1.19  christos #else  /* !SMALL */
    546  1.16     lukem #define INC_CHKCURSOR(x)	{ (x)++ ; \
    547  1.16     lukem 				if (x == cursor_pos) { \
    548  1.16     lukem 					cursor_argc = margc; \
    549  1.16     lukem 					cursor_argo = ap-argbase; \
    550  1.16     lukem 					cursor_pos = NULL; \
    551  1.16     lukem 				} }
    552  1.16     lukem 
    553  1.19  christos #endif /* !SMALL */
    554   1.1       cgd 
    555   1.1       cgd /*
    556   1.1       cgd  * Parse string into argbuf;
    557   1.1       cgd  * implemented with FSM to
    558   1.1       cgd  * handle quoting and strings
    559   1.1       cgd  */
    560   1.1       cgd char *
    561   1.1       cgd slurpstring()
    562   1.1       cgd {
    563   1.1       cgd 	int got_one = 0;
    564   1.3       cgd 	char *sb = stringbase;
    565   1.3       cgd 	char *ap = argbase;
    566   1.1       cgd 	char *tmp = argbase;		/* will return this if token found */
    567   1.1       cgd 
    568   1.1       cgd 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    569   1.1       cgd 		switch (slrflag) {	/* and $ as token for macro invoke */
    570   1.1       cgd 			case 0:
    571   1.1       cgd 				slrflag++;
    572  1.16     lukem 				INC_CHKCURSOR(stringbase);
    573   1.1       cgd 				return ((*sb == '!') ? "!" : "$");
    574   1.1       cgd 				/* NOTREACHED */
    575   1.1       cgd 			case 1:
    576   1.1       cgd 				slrflag++;
    577   1.1       cgd 				altarg = stringbase;
    578   1.1       cgd 				break;
    579   1.1       cgd 			default:
    580   1.1       cgd 				break;
    581   1.1       cgd 		}
    582   1.1       cgd 	}
    583   1.1       cgd 
    584   1.1       cgd S0:
    585   1.1       cgd 	switch (*sb) {
    586   1.1       cgd 
    587   1.1       cgd 	case '\0':
    588   1.1       cgd 		goto OUT;
    589   1.1       cgd 
    590   1.1       cgd 	case ' ':
    591   1.1       cgd 	case '\t':
    592  1.16     lukem 		INC_CHKCURSOR(sb);
    593  1.16     lukem 		goto S0;
    594   1.1       cgd 
    595   1.1       cgd 	default:
    596   1.1       cgd 		switch (slrflag) {
    597   1.1       cgd 			case 0:
    598   1.1       cgd 				slrflag++;
    599   1.1       cgd 				break;
    600   1.1       cgd 			case 1:
    601   1.1       cgd 				slrflag++;
    602   1.1       cgd 				altarg = sb;
    603   1.1       cgd 				break;
    604   1.1       cgd 			default:
    605   1.1       cgd 				break;
    606   1.1       cgd 		}
    607   1.1       cgd 		goto S1;
    608   1.1       cgd 	}
    609   1.1       cgd 
    610   1.1       cgd S1:
    611   1.1       cgd 	switch (*sb) {
    612   1.1       cgd 
    613   1.1       cgd 	case ' ':
    614   1.1       cgd 	case '\t':
    615   1.1       cgd 	case '\0':
    616   1.1       cgd 		goto OUT;	/* end of token */
    617   1.1       cgd 
    618   1.1       cgd 	case '\\':
    619  1.16     lukem 		INC_CHKCURSOR(sb);
    620  1.16     lukem 		goto S2;	/* slurp next character */
    621   1.1       cgd 
    622   1.1       cgd 	case '"':
    623  1.16     lukem 		INC_CHKCURSOR(sb);
    624  1.16     lukem 		goto S3;	/* slurp quoted string */
    625   1.1       cgd 
    626   1.1       cgd 	default:
    627  1.16     lukem 		*ap = *sb;	/* add character to token */
    628  1.16     lukem 		ap++;
    629  1.16     lukem 		INC_CHKCURSOR(sb);
    630   1.1       cgd 		got_one = 1;
    631   1.1       cgd 		goto S1;
    632   1.1       cgd 	}
    633   1.1       cgd 
    634   1.1       cgd S2:
    635   1.1       cgd 	switch (*sb) {
    636   1.1       cgd 
    637   1.1       cgd 	case '\0':
    638   1.1       cgd 		goto OUT;
    639   1.1       cgd 
    640   1.1       cgd 	default:
    641  1.16     lukem 		*ap = *sb;
    642  1.16     lukem 		ap++;
    643  1.16     lukem 		INC_CHKCURSOR(sb);
    644   1.1       cgd 		got_one = 1;
    645   1.1       cgd 		goto S1;
    646   1.1       cgd 	}
    647   1.1       cgd 
    648   1.1       cgd S3:
    649   1.1       cgd 	switch (*sb) {
    650   1.1       cgd 
    651   1.1       cgd 	case '\0':
    652   1.1       cgd 		goto OUT;
    653   1.1       cgd 
    654   1.1       cgd 	case '"':
    655  1.16     lukem 		INC_CHKCURSOR(sb);
    656  1.16     lukem 		goto S1;
    657   1.1       cgd 
    658   1.1       cgd 	default:
    659  1.16     lukem 		*ap = *sb;
    660  1.16     lukem 		ap++;
    661  1.16     lukem 		INC_CHKCURSOR(sb);
    662   1.1       cgd 		got_one = 1;
    663   1.1       cgd 		goto S3;
    664   1.1       cgd 	}
    665   1.1       cgd 
    666   1.1       cgd OUT:
    667   1.1       cgd 	if (got_one)
    668   1.1       cgd 		*ap++ = '\0';
    669   1.1       cgd 	argbase = ap;			/* update storage pointer */
    670   1.1       cgd 	stringbase = sb;		/* update scan pointer */
    671   1.1       cgd 	if (got_one) {
    672   1.3       cgd 		return (tmp);
    673   1.1       cgd 	}
    674   1.1       cgd 	switch (slrflag) {
    675   1.1       cgd 		case 0:
    676   1.1       cgd 			slrflag++;
    677   1.1       cgd 			break;
    678   1.1       cgd 		case 1:
    679   1.1       cgd 			slrflag++;
    680  1.28     lukem 			altarg = NULL;
    681   1.1       cgd 			break;
    682   1.1       cgd 		default:
    683   1.1       cgd 			break;
    684   1.1       cgd 	}
    685  1.28     lukem 	return (NULL);
    686   1.1       cgd }
    687   1.1       cgd 
    688   1.1       cgd /*
    689   1.1       cgd  * Help command.
    690   1.1       cgd  * Call each command handler with argc == 0 and argv[0] == name.
    691   1.1       cgd  */
    692   1.3       cgd void
    693   1.1       cgd help(argc, argv)
    694   1.1       cgd 	int argc;
    695   1.1       cgd 	char *argv[];
    696   1.1       cgd {
    697   1.3       cgd 	struct cmd *c;
    698   1.1       cgd 
    699   1.1       cgd 	if (argc == 1) {
    700  1.16     lukem 		StringList *buf;
    701   1.1       cgd 
    702  1.16     lukem 		buf = sl_init();
    703  1.31     lukem 		fprintf(ttyout,
    704  1.31     lukem 		    "%sommands may be abbreviated.  Commands are:\n\n",
    705  1.16     lukem 		    proxy ? "Proxy c" : "C");
    706  1.16     lukem 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
    707  1.16     lukem 			if (c->c_name && (!proxy || c->c_proxy))
    708  1.16     lukem 				sl_add(buf, c->c_name);
    709  1.16     lukem 		list_vertical(buf);
    710  1.16     lukem 		sl_free(buf, 0);
    711   1.1       cgd 		return;
    712   1.1       cgd 	}
    713  1.16     lukem 
    714  1.18     lukem #define HELPINDENT ((int) sizeof("disconnect"))
    715  1.16     lukem 
    716   1.1       cgd 	while (--argc > 0) {
    717   1.3       cgd 		char *arg;
    718  1.16     lukem 
    719   1.1       cgd 		arg = *++argv;
    720   1.1       cgd 		c = getcmd(arg);
    721   1.1       cgd 		if (c == (struct cmd *)-1)
    722  1.31     lukem 			fprintf(ttyout, "?Ambiguous help command %s\n", arg);
    723  1.28     lukem 		else if (c == NULL)
    724  1.31     lukem 			fprintf(ttyout, "?Invalid help command %s\n", arg);
    725   1.1       cgd 		else
    726  1.31     lukem 			fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
    727   1.1       cgd 				c->c_name, c->c_help);
    728   1.1       cgd 	}
    729  1.12     lukem }
    730  1.12     lukem 
    731  1.14     lukem void
    732  1.14     lukem usage()
    733  1.14     lukem {
    734  1.14     lukem 	(void)fprintf(stderr,
    735  1.31     lukem 	    "usage: %s [-AadeginptvV] [-r retry] [-P port] [host [port]]\n"
    736  1.31     lukem 	    "       %s [-o outfile] host:path[/]\n"
    737  1.31     lukem 	    "       %s [-o outfile] ftp://host[:port]/path[/]\n"
    738  1.31     lukem 	    "       %s [-o outfile] http://host[:port]/file\n",
    739  1.16     lukem 	    __progname, __progname, __progname, __progname);
    740  1.14     lukem 	exit(1);
    741   1.1       cgd }
    742