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