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