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