xref: /freebsd/contrib/tnftp/src/main.c (revision 91019ea7d45385dd73bc853691375c169bd5941d)
1cc361f65SGavin Atkinson /*	$NetBSD: main.c,v 1.17 2009/11/15 10:12:37 lukem Exp $	*/
2cc361f65SGavin Atkinson /*	from	NetBSD: main.c,v 1.117 2009/07/13 19:05:41 roy Exp	*/
3f982db4aSGavin Atkinson 
4f982db4aSGavin Atkinson /*-
5cc361f65SGavin Atkinson  * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
6f982db4aSGavin Atkinson  * All rights reserved.
7f982db4aSGavin Atkinson  *
8f982db4aSGavin Atkinson  * This code is derived from software contributed to The NetBSD Foundation
9f982db4aSGavin Atkinson  * by Luke Mewburn.
10f982db4aSGavin Atkinson  *
11f982db4aSGavin Atkinson  * Redistribution and use in source and binary forms, with or without
12f982db4aSGavin Atkinson  * modification, are permitted provided that the following conditions
13f982db4aSGavin Atkinson  * are met:
14f982db4aSGavin Atkinson  * 1. Redistributions of source code must retain the above copyright
15f982db4aSGavin Atkinson  *    notice, this list of conditions and the following disclaimer.
16f982db4aSGavin Atkinson  * 2. Redistributions in binary form must reproduce the above copyright
17f982db4aSGavin Atkinson  *    notice, this list of conditions and the following disclaimer in the
18f982db4aSGavin Atkinson  *    documentation and/or other materials provided with the distribution.
19f982db4aSGavin Atkinson  *
20f982db4aSGavin Atkinson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21f982db4aSGavin Atkinson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22f982db4aSGavin Atkinson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23f982db4aSGavin Atkinson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24f982db4aSGavin Atkinson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f982db4aSGavin Atkinson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f982db4aSGavin Atkinson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f982db4aSGavin Atkinson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f982db4aSGavin Atkinson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f982db4aSGavin Atkinson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30f982db4aSGavin Atkinson  * POSSIBILITY OF SUCH DAMAGE.
31f982db4aSGavin Atkinson  */
32f982db4aSGavin Atkinson 
33f982db4aSGavin Atkinson /*
34f982db4aSGavin Atkinson  * Copyright (c) 1985, 1989, 1993, 1994
35f982db4aSGavin Atkinson  *	The Regents of the University of California.  All rights reserved.
36f982db4aSGavin Atkinson  *
37f982db4aSGavin Atkinson  * Redistribution and use in source and binary forms, with or without
38f982db4aSGavin Atkinson  * modification, are permitted provided that the following conditions
39f982db4aSGavin Atkinson  * are met:
40f982db4aSGavin Atkinson  * 1. Redistributions of source code must retain the above copyright
41f982db4aSGavin Atkinson  *    notice, this list of conditions and the following disclaimer.
42f982db4aSGavin Atkinson  * 2. Redistributions in binary form must reproduce the above copyright
43f982db4aSGavin Atkinson  *    notice, this list of conditions and the following disclaimer in the
44f982db4aSGavin Atkinson  *    documentation and/or other materials provided with the distribution.
45f982db4aSGavin Atkinson  * 3. Neither the name of the University nor the names of its contributors
46f982db4aSGavin Atkinson  *    may be used to endorse or promote products derived from this software
47f982db4aSGavin Atkinson  *    without specific prior written permission.
48f982db4aSGavin Atkinson  *
49f982db4aSGavin Atkinson  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50f982db4aSGavin Atkinson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51f982db4aSGavin Atkinson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52f982db4aSGavin Atkinson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53f982db4aSGavin Atkinson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54f982db4aSGavin Atkinson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55f982db4aSGavin Atkinson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56f982db4aSGavin Atkinson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57f982db4aSGavin Atkinson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58f982db4aSGavin Atkinson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59f982db4aSGavin Atkinson  * SUCH DAMAGE.
60f982db4aSGavin Atkinson  */
61f982db4aSGavin Atkinson 
62f982db4aSGavin Atkinson /*
63f982db4aSGavin Atkinson  * Copyright (C) 1997 and 1998 WIDE Project.
64f982db4aSGavin Atkinson  * All rights reserved.
65f982db4aSGavin Atkinson  *
66f982db4aSGavin Atkinson  * Redistribution and use in source and binary forms, with or without
67f982db4aSGavin Atkinson  * modification, are permitted provided that the following conditions
68f982db4aSGavin Atkinson  * are met:
69f982db4aSGavin Atkinson  * 1. Redistributions of source code must retain the above copyright
70f982db4aSGavin Atkinson  *    notice, this list of conditions and the following disclaimer.
71f982db4aSGavin Atkinson  * 2. Redistributions in binary form must reproduce the above copyright
72f982db4aSGavin Atkinson  *    notice, this list of conditions and the following disclaimer in the
73f982db4aSGavin Atkinson  *    documentation and/or other materials provided with the distribution.
74f982db4aSGavin Atkinson  * 3. Neither the name of the project nor the names of its contributors
75f982db4aSGavin Atkinson  *    may be used to endorse or promote products derived from this software
76f982db4aSGavin Atkinson  *    without specific prior written permission.
77f982db4aSGavin Atkinson  *
78f982db4aSGavin Atkinson  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
79f982db4aSGavin Atkinson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80f982db4aSGavin Atkinson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81f982db4aSGavin Atkinson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
82f982db4aSGavin Atkinson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83f982db4aSGavin Atkinson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84f982db4aSGavin Atkinson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85f982db4aSGavin Atkinson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86f982db4aSGavin Atkinson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87f982db4aSGavin Atkinson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88f982db4aSGavin Atkinson  * SUCH DAMAGE.
89f982db4aSGavin Atkinson  */
90f982db4aSGavin Atkinson 
91cc361f65SGavin Atkinson #include "tnftp.h"
92cc361f65SGavin Atkinson 
93cc361f65SGavin Atkinson #if 0	/* tnftp */
94cc361f65SGavin Atkinson 
95f982db4aSGavin Atkinson #include <sys/cdefs.h>
96f982db4aSGavin Atkinson #ifndef lint
97cc361f65SGavin Atkinson __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\
98cc361f65SGavin Atkinson  The Regents of the University of California.  All rights reserved.\
99cc361f65SGavin Atkinson   Copyright 1996-2008 The NetBSD Foundation, Inc.  All rights reserved");
100f982db4aSGavin Atkinson #endif /* not lint */
101f982db4aSGavin Atkinson 
102f982db4aSGavin Atkinson #ifndef lint
103f982db4aSGavin Atkinson #if 0
104f982db4aSGavin Atkinson static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 10/9/94";
105f982db4aSGavin Atkinson #else
106cc361f65SGavin Atkinson __RCSID(" NetBSD: main.c,v 1.117 2009/07/13 19:05:41 roy Exp  ");
107f982db4aSGavin Atkinson #endif
108f982db4aSGavin Atkinson #endif /* not lint */
109f982db4aSGavin Atkinson 
110f982db4aSGavin Atkinson /*
111f982db4aSGavin Atkinson  * FTP User Program -- Command Interface.
112f982db4aSGavin Atkinson  */
113f982db4aSGavin Atkinson #include <sys/types.h>
114f982db4aSGavin Atkinson #include <sys/socket.h>
115f982db4aSGavin Atkinson 
116f982db4aSGavin Atkinson #include <err.h>
117f982db4aSGavin Atkinson #include <errno.h>
118f982db4aSGavin Atkinson #include <netdb.h>
119f982db4aSGavin Atkinson #include <paths.h>
120f982db4aSGavin Atkinson #include <pwd.h>
121f982db4aSGavin Atkinson #include <signal.h>
122f982db4aSGavin Atkinson #include <stdio.h>
123f982db4aSGavin Atkinson #include <stdlib.h>
124f982db4aSGavin Atkinson #include <string.h>
125cc361f65SGavin Atkinson #include <time.h>
126f982db4aSGavin Atkinson #include <unistd.h>
127f982db4aSGavin Atkinson #include <locale.h>
128f982db4aSGavin Atkinson 
129cc361f65SGavin Atkinson #endif	/* tnftp */
130*43092b7dSHiroki Sato #ifdef __FreeBSD__
131*43092b7dSHiroki Sato #include <sys/sysctl.h>
132*43092b7dSHiroki Sato #endif
133cc361f65SGavin Atkinson 
134f982db4aSGavin Atkinson #define	GLOBAL		/* force GLOBAL decls in ftp_var.h to be declared */
135f982db4aSGavin Atkinson #include "ftp_var.h"
136f982db4aSGavin Atkinson 
137f982db4aSGavin Atkinson #define	FTP_PROXY	"ftp_proxy"	/* env var with FTP proxy location */
138f982db4aSGavin Atkinson #define	HTTP_PROXY	"http_proxy"	/* env var with HTTP proxy location */
139f982db4aSGavin Atkinson #define	NO_PROXY	"no_proxy"	/* env var with list of non-proxied
140f982db4aSGavin Atkinson 					 * hosts, comma or space separated */
141f982db4aSGavin Atkinson 
142cc361f65SGavin Atkinson static void	setupoption(const char *, const char *, const char *);
143f982db4aSGavin Atkinson int		main(int, char *[]);
144f982db4aSGavin Atkinson 
145f982db4aSGavin Atkinson int
main(int volatile argc,char ** volatile argv)146cc361f65SGavin Atkinson main(int volatile argc, char **volatile argv)
147f982db4aSGavin Atkinson {
148f982db4aSGavin Atkinson 	int ch, rval;
149f982db4aSGavin Atkinson 	struct passwd *pw;
150cc361f65SGavin Atkinson 	char *cp, *ep, *anonpass, *upload_path, *src_addr;
151cc361f65SGavin Atkinson 	const char *anonuser;
15249e49bdbSGavin Atkinson 	int dumbterm, isupload;
153cc361f65SGavin Atkinson 	size_t len;
154f982db4aSGavin Atkinson 
155cc361f65SGavin Atkinson 	tzset();
156cc361f65SGavin Atkinson #if 0	/* tnftp */	/* XXX */
157f982db4aSGavin Atkinson 	setlocale(LC_ALL, "");
158cc361f65SGavin Atkinson #endif	/* tnftp */
159f982db4aSGavin Atkinson 	setprogname(argv[0]);
160f982db4aSGavin Atkinson 
161f982db4aSGavin Atkinson 	sigint_raised = 0;
162f982db4aSGavin Atkinson 
163f982db4aSGavin Atkinson 	ftpport = "ftp";
164f982db4aSGavin Atkinson 	httpport = "http";
165f982db4aSGavin Atkinson 	gateport = NULL;
166f982db4aSGavin Atkinson 	cp = getenv("FTPSERVERPORT");
167f982db4aSGavin Atkinson 	if (cp != NULL)
168f982db4aSGavin Atkinson 		gateport = cp;
169f982db4aSGavin Atkinson 	else
170f982db4aSGavin Atkinson 		gateport = "ftpgate";
171f982db4aSGavin Atkinson 	doglob = 1;
172f982db4aSGavin Atkinson 	interactive = 1;
173f982db4aSGavin Atkinson 	autologin = 1;
174f982db4aSGavin Atkinson 	passivemode = 1;
175f982db4aSGavin Atkinson 	activefallback = 1;
176f982db4aSGavin Atkinson 	preserve = 1;
177f982db4aSGavin Atkinson 	verbose = 0;
178f982db4aSGavin Atkinson 	progress = 0;
179f982db4aSGavin Atkinson 	gatemode = 0;
180f982db4aSGavin Atkinson 	data = -1;
181f982db4aSGavin Atkinson 	outfile = NULL;
182f982db4aSGavin Atkinson 	restartautofetch = 0;
183f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
184f982db4aSGavin Atkinson 	editing = 0;
185f982db4aSGavin Atkinson 	el = NULL;
186f982db4aSGavin Atkinson 	hist = NULL;
187f982db4aSGavin Atkinson #endif
188f982db4aSGavin Atkinson 	bytes = 0;
189f982db4aSGavin Atkinson 	mark = HASHBYTES;
190f982db4aSGavin Atkinson 	rate_get = 0;
191f982db4aSGavin Atkinson 	rate_get_incr = DEFAULTINCR;
192f982db4aSGavin Atkinson 	rate_put = 0;
193f982db4aSGavin Atkinson 	rate_put_incr = DEFAULTINCR;
194f982db4aSGavin Atkinson #ifdef INET6
195f982db4aSGavin Atkinson 	epsv4 = 1;
196cc361f65SGavin Atkinson 	epsv6 = 1;
197f982db4aSGavin Atkinson #else
198f982db4aSGavin Atkinson 	epsv4 = 0;
199cc361f65SGavin Atkinson 	epsv6 = 0;
200f982db4aSGavin Atkinson #endif
201f982db4aSGavin Atkinson 	epsv4bad = 0;
202cc361f65SGavin Atkinson 	epsv6bad = 0;
203cc361f65SGavin Atkinson 	src_addr = NULL;
204f982db4aSGavin Atkinson 	upload_path = NULL;
205f982db4aSGavin Atkinson 	isupload = 0;
206f982db4aSGavin Atkinson 	reply_callback = NULL;
207cc361f65SGavin Atkinson #ifdef INET6
208f982db4aSGavin Atkinson 	family = AF_UNSPEC;
209cc361f65SGavin Atkinson #else
210cc361f65SGavin Atkinson 	family = AF_INET;	/* force AF_INET if no INET6 support */
211cc361f65SGavin Atkinson #endif
212f982db4aSGavin Atkinson 
213f982db4aSGavin Atkinson 	netrc[0] = '\0';
214f982db4aSGavin Atkinson 	cp = getenv("NETRC");
215f982db4aSGavin Atkinson 	if (cp != NULL && strlcpy(netrc, cp, sizeof(netrc)) >= sizeof(netrc))
216f982db4aSGavin Atkinson 		errx(1, "$NETRC `%s': %s", cp, strerror(ENAMETOOLONG));
217f982db4aSGavin Atkinson 
218cc361f65SGavin Atkinson 	marg_sl = ftp_sl_init();
219f982db4aSGavin Atkinson 	if ((tmpdir = getenv("TMPDIR")) == NULL)
220f982db4aSGavin Atkinson 		tmpdir = _PATH_TMP;
221f982db4aSGavin Atkinson 
222f982db4aSGavin Atkinson 	/* Set default operation mode based on FTPMODE environment variable */
223f982db4aSGavin Atkinson 	if ((cp = getenv("FTPMODE")) != NULL) {
224f982db4aSGavin Atkinson 		if (strcasecmp(cp, "passive") == 0) {
225f982db4aSGavin Atkinson 			passivemode = 1;
226f982db4aSGavin Atkinson 			activefallback = 0;
227f982db4aSGavin Atkinson 		} else if (strcasecmp(cp, "active") == 0) {
228f982db4aSGavin Atkinson 			passivemode = 0;
229f982db4aSGavin Atkinson 			activefallback = 0;
230f982db4aSGavin Atkinson 		} else if (strcasecmp(cp, "gate") == 0) {
231f982db4aSGavin Atkinson 			gatemode = 1;
232f982db4aSGavin Atkinson 		} else if (strcasecmp(cp, "auto") == 0) {
233f982db4aSGavin Atkinson 			passivemode = 1;
234f982db4aSGavin Atkinson 			activefallback = 1;
235f982db4aSGavin Atkinson 		} else
236cc361f65SGavin Atkinson 			warnx("Unknown $FTPMODE `%s'; using defaults", cp);
237f982db4aSGavin Atkinson 	}
238f982db4aSGavin Atkinson 
239f982db4aSGavin Atkinson 	if (strcmp(getprogname(), "pftp") == 0) {
240f982db4aSGavin Atkinson 		passivemode = 1;
241f982db4aSGavin Atkinson 		activefallback = 0;
242f982db4aSGavin Atkinson 	} else if (strcmp(getprogname(), "gate-ftp") == 0)
243f982db4aSGavin Atkinson 		gatemode = 1;
244f982db4aSGavin Atkinson 
245f982db4aSGavin Atkinson 	gateserver = getenv("FTPSERVER");
246f982db4aSGavin Atkinson 	if (gateserver == NULL || *gateserver == '\0')
247f982db4aSGavin Atkinson 		gateserver = GATE_SERVER;
248f982db4aSGavin Atkinson 	if (gatemode) {
249f982db4aSGavin Atkinson 		if (*gateserver == '\0') {
250f982db4aSGavin Atkinson 			warnx(
251f982db4aSGavin Atkinson "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
252f982db4aSGavin Atkinson 			gatemode = 0;
253f982db4aSGavin Atkinson 		}
254f982db4aSGavin Atkinson 	}
255f982db4aSGavin Atkinson 
256f982db4aSGavin Atkinson 	cp = getenv("TERM");
257f982db4aSGavin Atkinson 	if (cp == NULL || strcmp(cp, "dumb") == 0)
258f982db4aSGavin Atkinson 		dumbterm = 1;
259f982db4aSGavin Atkinson 	else
260f982db4aSGavin Atkinson 		dumbterm = 0;
261f982db4aSGavin Atkinson 	fromatty = isatty(fileno(stdin));
262f982db4aSGavin Atkinson 	ttyout = stdout;
263f982db4aSGavin Atkinson 	if (isatty(fileno(ttyout))) {
264f982db4aSGavin Atkinson 		verbose = 1;		/* verbose if to a tty */
265f982db4aSGavin Atkinson 		if (! dumbterm) {
266f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
267f982db4aSGavin Atkinson 			if (fromatty)	/* editing mode on if tty is usable */
268f982db4aSGavin Atkinson 				editing = 1;
269f982db4aSGavin Atkinson #endif
270f982db4aSGavin Atkinson #ifndef NO_PROGRESS
271f982db4aSGavin Atkinson 			if (foregroundproc())
272f982db4aSGavin Atkinson 				progress = 1;	/* progress bar on if fg */
273f982db4aSGavin Atkinson #endif
274f982db4aSGavin Atkinson 		}
275f982db4aSGavin Atkinson 	}
276f982db4aSGavin Atkinson 
277cc361f65SGavin Atkinson 	while ((ch = getopt(argc, argv, "46AadefginN:o:pP:q:r:Rs:tT:u:vV")) != -1) {
278f982db4aSGavin Atkinson 		switch (ch) {
279f982db4aSGavin Atkinson 		case '4':
280f982db4aSGavin Atkinson 			family = AF_INET;
281f982db4aSGavin Atkinson 			break;
282f982db4aSGavin Atkinson 
283f982db4aSGavin Atkinson 		case '6':
284f982db4aSGavin Atkinson #ifdef INET6
285f982db4aSGavin Atkinson 			family = AF_INET6;
286f982db4aSGavin Atkinson #else
287f982db4aSGavin Atkinson 			warnx("INET6 support is not available; ignoring -6");
288f982db4aSGavin Atkinson #endif
289f982db4aSGavin Atkinson 			break;
290f982db4aSGavin Atkinson 
291f982db4aSGavin Atkinson 		case 'A':
292f982db4aSGavin Atkinson 			activefallback = 0;
293f982db4aSGavin Atkinson 			passivemode = 0;
294f982db4aSGavin Atkinson 			break;
295f982db4aSGavin Atkinson 
296f982db4aSGavin Atkinson 		case 'a':
297f982db4aSGavin Atkinson 			anonftp = 1;
298f982db4aSGavin Atkinson 			break;
299f982db4aSGavin Atkinson 
300f982db4aSGavin Atkinson 		case 'd':
301f982db4aSGavin Atkinson 			options |= SO_DEBUG;
302cc361f65SGavin Atkinson 			ftp_debug++;
303f982db4aSGavin Atkinson 			break;
304f982db4aSGavin Atkinson 
305f982db4aSGavin Atkinson 		case 'e':
306f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
307f982db4aSGavin Atkinson 			editing = 0;
308f982db4aSGavin Atkinson #endif
309f982db4aSGavin Atkinson 			break;
310f982db4aSGavin Atkinson 
311f982db4aSGavin Atkinson 		case 'f':
312f982db4aSGavin Atkinson 			flushcache = 1;
313f982db4aSGavin Atkinson 			break;
314f982db4aSGavin Atkinson 
315f982db4aSGavin Atkinson 		case 'g':
316f982db4aSGavin Atkinson 			doglob = 0;
317f982db4aSGavin Atkinson 			break;
318f982db4aSGavin Atkinson 
319f982db4aSGavin Atkinson 		case 'i':
320f982db4aSGavin Atkinson 			interactive = 0;
321f982db4aSGavin Atkinson 			break;
322f982db4aSGavin Atkinson 
323f982db4aSGavin Atkinson 		case 'n':
324f982db4aSGavin Atkinson 			autologin = 0;
325f982db4aSGavin Atkinson 			break;
326f982db4aSGavin Atkinson 
327f982db4aSGavin Atkinson 		case 'N':
328f982db4aSGavin Atkinson 			if (strlcpy(netrc, optarg, sizeof(netrc))
329f982db4aSGavin Atkinson 			    >= sizeof(netrc))
330f982db4aSGavin Atkinson 				errx(1, "%s: %s", optarg,
331f982db4aSGavin Atkinson 				    strerror(ENAMETOOLONG));
332f982db4aSGavin Atkinson 			break;
333f982db4aSGavin Atkinson 
334f982db4aSGavin Atkinson 		case 'o':
335f982db4aSGavin Atkinson 			outfile = optarg;
336f982db4aSGavin Atkinson 			if (strcmp(outfile, "-") == 0)
337f982db4aSGavin Atkinson 				ttyout = stderr;
338f982db4aSGavin Atkinson 			break;
339f982db4aSGavin Atkinson 
340f982db4aSGavin Atkinson 		case 'p':
341f982db4aSGavin Atkinson 			passivemode = 1;
342f982db4aSGavin Atkinson 			activefallback = 0;
343f982db4aSGavin Atkinson 			break;
344f982db4aSGavin Atkinson 
345f982db4aSGavin Atkinson 		case 'P':
346f982db4aSGavin Atkinson 			ftpport = optarg;
347f982db4aSGavin Atkinson 			break;
348f982db4aSGavin Atkinson 
349f982db4aSGavin Atkinson 		case 'q':
350f982db4aSGavin Atkinson 			quit_time = strtol(optarg, &ep, 10);
351f982db4aSGavin Atkinson 			if (quit_time < 1 || *ep != '\0')
352cc361f65SGavin Atkinson 				errx(1, "Bad quit value: %s", optarg);
353f982db4aSGavin Atkinson 			break;
354f982db4aSGavin Atkinson 
355f982db4aSGavin Atkinson 		case 'r':
356f982db4aSGavin Atkinson 			retry_connect = strtol(optarg, &ep, 10);
357f982db4aSGavin Atkinson 			if (retry_connect < 1 || *ep != '\0')
358cc361f65SGavin Atkinson 				errx(1, "Bad retry value: %s", optarg);
359f982db4aSGavin Atkinson 			break;
360f982db4aSGavin Atkinson 
361f982db4aSGavin Atkinson 		case 'R':
362f982db4aSGavin Atkinson 			restartautofetch = 1;
363f982db4aSGavin Atkinson 			break;
364f982db4aSGavin Atkinson 
365cc361f65SGavin Atkinson 		case 's':
366cc361f65SGavin Atkinson 			src_addr = optarg;
367cc361f65SGavin Atkinson 			break;
368cc361f65SGavin Atkinson 
369f982db4aSGavin Atkinson 		case 't':
370f982db4aSGavin Atkinson 			trace = 1;
371f982db4aSGavin Atkinson 			break;
372f982db4aSGavin Atkinson 
373f982db4aSGavin Atkinson 		case 'T':
374f982db4aSGavin Atkinson 		{
375f982db4aSGavin Atkinson 			int targc;
376f982db4aSGavin Atkinson 			char *targv[6], *oac;
377cc361f65SGavin Atkinson 			char cmdbuf[MAX_C_NAME];
378f982db4aSGavin Atkinson 
379f982db4aSGavin Atkinson 				/* look for `dir,max[,incr]' */
380f982db4aSGavin Atkinson 			targc = 0;
381cc361f65SGavin Atkinson 			(void)strlcpy(cmdbuf, "-T", sizeof(cmdbuf));
382cc361f65SGavin Atkinson 			targv[targc++] = cmdbuf;
383cc361f65SGavin Atkinson 			oac = ftp_strdup(optarg);
384f982db4aSGavin Atkinson 
385f982db4aSGavin Atkinson 			while ((cp = strsep(&oac, ",")) != NULL) {
386f982db4aSGavin Atkinson 				if (*cp == '\0') {
387cc361f65SGavin Atkinson 					warnx("Bad throttle value `%s'",
388cc361f65SGavin Atkinson 					    optarg);
389f982db4aSGavin Atkinson 					usage();
390f982db4aSGavin Atkinson 					/* NOTREACHED */
391f982db4aSGavin Atkinson 				}
392f982db4aSGavin Atkinson 				targv[targc++] = cp;
393f982db4aSGavin Atkinson 				if (targc >= 5)
394f982db4aSGavin Atkinson 					break;
395f982db4aSGavin Atkinson 			}
396f982db4aSGavin Atkinson 			if (parserate(targc, targv, 1) == -1)
397f982db4aSGavin Atkinson 				usage();
398f982db4aSGavin Atkinson 			free(oac);
399f982db4aSGavin Atkinson 			break;
400f982db4aSGavin Atkinson 		}
401f982db4aSGavin Atkinson 
402f982db4aSGavin Atkinson 		case 'u':
403f982db4aSGavin Atkinson 		{
404f982db4aSGavin Atkinson 			isupload = 1;
405f982db4aSGavin Atkinson 			interactive = 0;
406cc361f65SGavin Atkinson 			upload_path = ftp_strdup(optarg);
407f982db4aSGavin Atkinson 
408f982db4aSGavin Atkinson 			break;
409f982db4aSGavin Atkinson 		}
410f982db4aSGavin Atkinson 
411f982db4aSGavin Atkinson 		case 'v':
412f982db4aSGavin Atkinson 			progress = verbose = 1;
413f982db4aSGavin Atkinson 			break;
414f982db4aSGavin Atkinson 
415f982db4aSGavin Atkinson 		case 'V':
416f982db4aSGavin Atkinson 			progress = verbose = 0;
417f982db4aSGavin Atkinson 			break;
418f982db4aSGavin Atkinson 
419f982db4aSGavin Atkinson 		default:
420f982db4aSGavin Atkinson 			usage();
421f982db4aSGavin Atkinson 		}
422f982db4aSGavin Atkinson 	}
423f982db4aSGavin Atkinson 			/* set line buffering on ttyout */
424f982db4aSGavin Atkinson 	setvbuf(ttyout, NULL, _IOLBF, 0);
425f982db4aSGavin Atkinson 	argc -= optind;
426f982db4aSGavin Atkinson 	argv += optind;
427f982db4aSGavin Atkinson 
428f982db4aSGavin Atkinson 	cpend = 0;	/* no pending replies */
429f982db4aSGavin Atkinson 	proxy = 0;	/* proxy not active */
430f982db4aSGavin Atkinson 	crflag = 1;	/* strip c.r. on ascii gets */
431f982db4aSGavin Atkinson 	sendport = -1;	/* not using ports */
432f982db4aSGavin Atkinson 
433cc361f65SGavin Atkinson 	if (src_addr != NULL) {
434cc361f65SGavin Atkinson 		struct addrinfo hints;
435cc361f65SGavin Atkinson 		int error;
436cc361f65SGavin Atkinson 
437cc361f65SGavin Atkinson 		memset(&hints, 0, sizeof(hints));
438cc361f65SGavin Atkinson 		hints.ai_family = family;
439cc361f65SGavin Atkinson 		hints.ai_socktype = SOCK_STREAM;
440cc361f65SGavin Atkinson 		hints.ai_flags = AI_PASSIVE;
441cc361f65SGavin Atkinson 		error = getaddrinfo(src_addr, NULL, &hints, &bindai);
442cc361f65SGavin Atkinson 		if (error) {
443cc361f65SGavin Atkinson 		    	errx(1, "Can't lookup `%s': %s", src_addr,
444cc361f65SGavin Atkinson 			    (error == EAI_SYSTEM) ? strerror(errno)
445cc361f65SGavin Atkinson 						  : gai_strerror(error));
446cc361f65SGavin Atkinson 		}
447cc361f65SGavin Atkinson 	}
448cc361f65SGavin Atkinson 
449f982db4aSGavin Atkinson 	/*
450f982db4aSGavin Atkinson 	 * Cache the user name and home directory.
451f982db4aSGavin Atkinson 	 */
452f982db4aSGavin Atkinson 	localhome = NULL;
453f982db4aSGavin Atkinson 	localname = NULL;
454f982db4aSGavin Atkinson 	anonuser = "anonymous";
455f982db4aSGavin Atkinson 	cp = getenv("HOME");
456f982db4aSGavin Atkinson 	if (! EMPTYSTRING(cp))
457cc361f65SGavin Atkinson 		localhome = ftp_strdup(cp);
458f982db4aSGavin Atkinson 	pw = NULL;
459f982db4aSGavin Atkinson 	cp = getlogin();
460f982db4aSGavin Atkinson 	if (cp != NULL)
461f982db4aSGavin Atkinson 		pw = getpwnam(cp);
462f982db4aSGavin Atkinson 	if (pw == NULL)
463f982db4aSGavin Atkinson 		pw = getpwuid(getuid());
464f982db4aSGavin Atkinson 	if (pw != NULL) {
465f982db4aSGavin Atkinson 		if (localhome == NULL && !EMPTYSTRING(pw->pw_dir))
466cc361f65SGavin Atkinson 			localhome = ftp_strdup(pw->pw_dir);
467cc361f65SGavin Atkinson 		localname = ftp_strdup(pw->pw_name);
468f982db4aSGavin Atkinson 		anonuser = localname;
469f982db4aSGavin Atkinson 	}
470f982db4aSGavin Atkinson 	if (netrc[0] == '\0' && localhome != NULL) {
471f982db4aSGavin Atkinson 		if (strlcpy(netrc, localhome, sizeof(netrc)) >= sizeof(netrc) ||
472f982db4aSGavin Atkinson 		    strlcat(netrc, "/.netrc", sizeof(netrc)) >= sizeof(netrc)) {
473f982db4aSGavin Atkinson 			warnx("%s/.netrc: %s", localhome,
474f982db4aSGavin Atkinson 			    strerror(ENAMETOOLONG));
475f982db4aSGavin Atkinson 			netrc[0] = '\0';
476f982db4aSGavin Atkinson 		}
477f982db4aSGavin Atkinson 	}
478f982db4aSGavin Atkinson 	if (localhome == NULL)
479cc361f65SGavin Atkinson 		localhome = ftp_strdup("/");
480f982db4aSGavin Atkinson 
481f982db4aSGavin Atkinson 	/*
482f982db4aSGavin Atkinson 	 * Every anonymous FTP server I've encountered will accept the
483f982db4aSGavin Atkinson 	 * string "username@", and will append the hostname itself. We
484f982db4aSGavin Atkinson 	 * do this by default since many servers are picky about not
485f982db4aSGavin Atkinson 	 * having a FQDN in the anonymous password.
486f982db4aSGavin Atkinson 	 * - thorpej@NetBSD.org
487f982db4aSGavin Atkinson 	 */
488f982db4aSGavin Atkinson 	len = strlen(anonuser) + 2;
489cc361f65SGavin Atkinson 	anonpass = ftp_malloc(len);
490f982db4aSGavin Atkinson 	(void)strlcpy(anonpass, anonuser, len);
491f982db4aSGavin Atkinson 	(void)strlcat(anonpass, "@",	  len);
492f982db4aSGavin Atkinson 
493f982db4aSGavin Atkinson 			/*
494f982db4aSGavin Atkinson 			 * set all the defaults for options defined in
495f982db4aSGavin Atkinson 			 * struct option optiontab[]  declared in cmdtab.c
496f982db4aSGavin Atkinson 			 */
497f982db4aSGavin Atkinson 	setupoption("anonpass",		getenv("FTPANONPASS"),	anonpass);
498f982db4aSGavin Atkinson 	setupoption("ftp_proxy",	getenv(FTP_PROXY),	"");
499f982db4aSGavin Atkinson 	setupoption("http_proxy",	getenv(HTTP_PROXY),	"");
500f982db4aSGavin Atkinson 	setupoption("no_proxy",		getenv(NO_PROXY),	"");
501f982db4aSGavin Atkinson 	setupoption("pager",		getenv("PAGER"),	DEFAULTPAGER);
502f982db4aSGavin Atkinson 	setupoption("prompt",		getenv("FTPPROMPT"),	DEFAULTPROMPT);
503f982db4aSGavin Atkinson 	setupoption("rprompt",		getenv("FTPRPROMPT"),	DEFAULTRPROMPT);
504f982db4aSGavin Atkinson 
505f982db4aSGavin Atkinson 	free(anonpass);
506f982db4aSGavin Atkinson 
507f982db4aSGavin Atkinson 	setttywidth(0);
508f982db4aSGavin Atkinson #ifdef SIGINFO
509f982db4aSGavin Atkinson 	(void)xsignal(SIGINFO, psummary);
510f982db4aSGavin Atkinson #endif
511f982db4aSGavin Atkinson 	(void)xsignal(SIGQUIT, psummary);
512f982db4aSGavin Atkinson 	(void)xsignal(SIGUSR1, crankrate);
513f982db4aSGavin Atkinson 	(void)xsignal(SIGUSR2, crankrate);
514f982db4aSGavin Atkinson 	(void)xsignal(SIGWINCH, setttywidth);
515f982db4aSGavin Atkinson 
516*43092b7dSHiroki Sato 	auto_rcvbuf = ((sysctlbyname("net.inet.tcp.recvbuf_auto",
517*43092b7dSHiroki Sato 	    &auto_rcvbuf, &(size_t []){[0] = sizeof(int)}[0], NULL, 0) == 0) &&
518*43092b7dSHiroki Sato 	    auto_rcvbuf == 1);
519*43092b7dSHiroki Sato 	auto_sndbuf = ((sysctlbyname("net.inet.tcp.sendbuf_auto",
520*43092b7dSHiroki Sato 	    &auto_sndbuf, &(size_t []){[0] = sizeof(int)}[0], NULL, 0) == 0) &&
521*43092b7dSHiroki Sato 	    auto_sndbuf == 1);
522*43092b7dSHiroki Sato 
523f982db4aSGavin Atkinson 	if (argc > 0) {
524f982db4aSGavin Atkinson 		if (isupload) {
525f982db4aSGavin Atkinson 			rval = auto_put(argc, argv, upload_path);
526f982db4aSGavin Atkinson  sigint_or_rval_exit:
527f982db4aSGavin Atkinson 			if (sigint_raised) {
528f982db4aSGavin Atkinson 				(void)xsignal(SIGINT, SIG_DFL);
529f982db4aSGavin Atkinson 				raise(SIGINT);
530f982db4aSGavin Atkinson 			}
531f982db4aSGavin Atkinson 			exit(rval);
532f982db4aSGavin Atkinson 		} else if (strchr(argv[0], ':') != NULL
533f982db4aSGavin Atkinson 			    && ! isipv6addr(argv[0])) {
534f982db4aSGavin Atkinson 			rval = auto_fetch(argc, argv);
535f982db4aSGavin Atkinson 			if (rval >= 0)		/* -1 == connected and cd-ed */
536f982db4aSGavin Atkinson 				goto sigint_or_rval_exit;
537f982db4aSGavin Atkinson 		} else {
538cc361f65SGavin Atkinson 			char *xargv[4], *uuser, *host;
539cc361f65SGavin Atkinson 			char cmdbuf[MAXPATHLEN];
540f982db4aSGavin Atkinson 
541f982db4aSGavin Atkinson 			if ((rval = sigsetjmp(toplevel, 1)))
542f982db4aSGavin Atkinson 				goto sigint_or_rval_exit;
543f982db4aSGavin Atkinson 			(void)xsignal(SIGINT, intr);
544f982db4aSGavin Atkinson 			(void)xsignal(SIGPIPE, lostpeer);
545cc361f65SGavin Atkinson 			uuser = NULL;
546f982db4aSGavin Atkinson 			host = argv[0];
547f982db4aSGavin Atkinson 			cp = strchr(host, '@');
548f982db4aSGavin Atkinson 			if (cp) {
549f982db4aSGavin Atkinson 				*cp = '\0';
550cc361f65SGavin Atkinson 				uuser = host;
551f982db4aSGavin Atkinson 				host = cp + 1;
552f982db4aSGavin Atkinson 			}
553cc361f65SGavin Atkinson 			(void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
554cc361f65SGavin Atkinson 			xargv[0] = cmdbuf;
555f982db4aSGavin Atkinson 			xargv[1] = host;
556f982db4aSGavin Atkinson 			xargv[2] = argv[1];
557f982db4aSGavin Atkinson 			xargv[3] = NULL;
558f982db4aSGavin Atkinson 			do {
559f982db4aSGavin Atkinson 				int oautologin;
560f982db4aSGavin Atkinson 
561f982db4aSGavin Atkinson 				oautologin = autologin;
562cc361f65SGavin Atkinson 				if (uuser != NULL) {
563f982db4aSGavin Atkinson 					anonftp = 0;
564f982db4aSGavin Atkinson 					autologin = 0;
565f982db4aSGavin Atkinson 				}
566f982db4aSGavin Atkinson 				setpeer(argc+1, xargv);
567f982db4aSGavin Atkinson 				autologin = oautologin;
568cc361f65SGavin Atkinson 				if (connected == 1 && uuser != NULL)
569cc361f65SGavin Atkinson 					(void)ftp_login(host, uuser, NULL);
570f982db4aSGavin Atkinson 				if (!retry_connect)
571f982db4aSGavin Atkinson 					break;
572f982db4aSGavin Atkinson 				if (!connected) {
573f982db4aSGavin Atkinson 					macnum = 0;
574f982db4aSGavin Atkinson 					fprintf(ttyout,
575f982db4aSGavin Atkinson 					    "Retrying in %d seconds...\n",
576f982db4aSGavin Atkinson 					    retry_connect);
577f982db4aSGavin Atkinson 					sleep(retry_connect);
578f982db4aSGavin Atkinson 				}
579f982db4aSGavin Atkinson 			} while (!connected);
580f982db4aSGavin Atkinson 			retry_connect = 0; /* connected, stop hiding msgs */
581f982db4aSGavin Atkinson 		}
582f982db4aSGavin Atkinson 	}
583f982db4aSGavin Atkinson 	if (isupload)
584f982db4aSGavin Atkinson 		usage();
585f982db4aSGavin Atkinson 
586f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
587f982db4aSGavin Atkinson 	controlediting();
588f982db4aSGavin Atkinson #endif /* !NO_EDITCOMPLETE */
589f982db4aSGavin Atkinson 
590f982db4aSGavin Atkinson 	(void)sigsetjmp(toplevel, 1);
591f982db4aSGavin Atkinson 	(void)xsignal(SIGINT, intr);
592f982db4aSGavin Atkinson 	(void)xsignal(SIGPIPE, lostpeer);
593f982db4aSGavin Atkinson 	for (;;)
594f982db4aSGavin Atkinson 		cmdscanner();
595f982db4aSGavin Atkinson }
596f982db4aSGavin Atkinson 
597f982db4aSGavin Atkinson /*
598f982db4aSGavin Atkinson  * Generate a prompt
599f982db4aSGavin Atkinson  */
600f982db4aSGavin Atkinson char *
prompt(void)601f982db4aSGavin Atkinson prompt(void)
602f982db4aSGavin Atkinson {
603cc361f65SGavin Atkinson 	static char	**promptopt;
604f982db4aSGavin Atkinson 	static char	  buf[MAXPATHLEN];
605f982db4aSGavin Atkinson 
606cc361f65SGavin Atkinson 	if (promptopt == NULL) {
607f982db4aSGavin Atkinson 		struct option *o;
608f982db4aSGavin Atkinson 
609f982db4aSGavin Atkinson 		o = getoption("prompt");
610f982db4aSGavin Atkinson 		if (o == NULL)
611cc361f65SGavin Atkinson 			errx(1, "prompt: no such option `prompt'");
612cc361f65SGavin Atkinson 		promptopt = &(o->value);
613f982db4aSGavin Atkinson 	}
614cc361f65SGavin Atkinson 	formatbuf(buf, sizeof(buf), *promptopt ? *promptopt : DEFAULTPROMPT);
615f982db4aSGavin Atkinson 	return (buf);
616f982db4aSGavin Atkinson }
617f982db4aSGavin Atkinson 
618f982db4aSGavin Atkinson /*
619f982db4aSGavin Atkinson  * Generate an rprompt
620f982db4aSGavin Atkinson  */
621f982db4aSGavin Atkinson char *
rprompt(void)622f982db4aSGavin Atkinson rprompt(void)
623f982db4aSGavin Atkinson {
624cc361f65SGavin Atkinson 	static char	**rpromptopt;
625f982db4aSGavin Atkinson 	static char	  buf[MAXPATHLEN];
626f982db4aSGavin Atkinson 
627cc361f65SGavin Atkinson 	if (rpromptopt == NULL) {
628f982db4aSGavin Atkinson 		struct option *o;
629f982db4aSGavin Atkinson 
630f982db4aSGavin Atkinson 		o = getoption("rprompt");
631f982db4aSGavin Atkinson 		if (o == NULL)
632cc361f65SGavin Atkinson 			errx(1, "rprompt: no such option `rprompt'");
633cc361f65SGavin Atkinson 		rpromptopt = &(o->value);
634f982db4aSGavin Atkinson 	}
635cc361f65SGavin Atkinson 	formatbuf(buf, sizeof(buf), *rpromptopt ? *rpromptopt : DEFAULTRPROMPT);
636f982db4aSGavin Atkinson 	return (buf);
637f982db4aSGavin Atkinson }
638f982db4aSGavin Atkinson 
639f982db4aSGavin Atkinson /*
640f982db4aSGavin Atkinson  * Command parser.
641f982db4aSGavin Atkinson  */
642f982db4aSGavin Atkinson void
cmdscanner(void)643f982db4aSGavin Atkinson cmdscanner(void)
644f982db4aSGavin Atkinson {
645f982db4aSGavin Atkinson 	struct cmd	*c;
646f982db4aSGavin Atkinson 	char		*p;
647cc361f65SGavin Atkinson #ifndef NO_EDITCOMPLETE
648cc361f65SGavin Atkinson 	int		 ch;
649cc361f65SGavin Atkinson 	size_t		 num;
650cc361f65SGavin Atkinson #endif
651cc361f65SGavin Atkinson 	int		 len;
652cc361f65SGavin Atkinson 	char		 cmdbuf[MAX_C_NAME];
653f982db4aSGavin Atkinson 
654f982db4aSGavin Atkinson 	for (;;) {
655f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
656f982db4aSGavin Atkinson 		if (!editing) {
657f982db4aSGavin Atkinson #endif /* !NO_EDITCOMPLETE */
658f982db4aSGavin Atkinson 			if (fromatty) {
659f982db4aSGavin Atkinson 				fputs(prompt(), ttyout);
660f982db4aSGavin Atkinson 				p = rprompt();
661f982db4aSGavin Atkinson 				if (*p)
662f982db4aSGavin Atkinson 					fprintf(ttyout, "%s ", p);
663f982db4aSGavin Atkinson 			}
664cc361f65SGavin Atkinson 			(void)fflush(ttyout);
665cc361f65SGavin Atkinson 			len = get_line(stdin, line, sizeof(line), NULL);
666cc361f65SGavin Atkinson 			switch (len) {
667cc361f65SGavin Atkinson 			case -1:	/* EOF */
668cc361f65SGavin Atkinson 			case -2:	/* error */
669f982db4aSGavin Atkinson 				if (fromatty)
670f982db4aSGavin Atkinson 					putc('\n', ttyout);
671f982db4aSGavin Atkinson 				quit(0, NULL);
672cc361f65SGavin Atkinson 				/* NOTREACHED */
673cc361f65SGavin Atkinson 			case -3:	/* too long; try again */
674f982db4aSGavin Atkinson 				fputs("Sorry, input line is too long.\n",
675f982db4aSGavin Atkinson 				    ttyout);
676cc361f65SGavin Atkinson 				continue;
677cc361f65SGavin Atkinson 			case 0:		/* empty; try again */
678cc361f65SGavin Atkinson 				continue;
679cc361f65SGavin Atkinson 			default:	/* all ok */
680f982db4aSGavin Atkinson 				break;
681cc361f65SGavin Atkinson 			}
682f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
683f982db4aSGavin Atkinson 		} else {
684f982db4aSGavin Atkinson 			const char *buf;
685f982db4aSGavin Atkinson 			HistEvent ev;
686f982db4aSGavin Atkinson 			cursor_pos = NULL;
687f982db4aSGavin Atkinson 
688cc361f65SGavin Atkinson 			buf = el_gets(el, &ch);
689cc361f65SGavin Atkinson 			num = ch;
690f982db4aSGavin Atkinson 			if (buf == NULL || num == 0) {
691f982db4aSGavin Atkinson 				if (fromatty)
692f982db4aSGavin Atkinson 					putc('\n', ttyout);
693f982db4aSGavin Atkinson 				quit(0, NULL);
694f982db4aSGavin Atkinson 			}
695f982db4aSGavin Atkinson 			if (num >= sizeof(line)) {
696f982db4aSGavin Atkinson 				fputs("Sorry, input line is too long.\n",
697f982db4aSGavin Atkinson 				    ttyout);
698f982db4aSGavin Atkinson 				break;
699f982db4aSGavin Atkinson 			}
700f982db4aSGavin Atkinson 			memcpy(line, buf, num);
701f982db4aSGavin Atkinson 			if (line[--num] == '\n') {
702f982db4aSGavin Atkinson 				line[num] = '\0';
703f982db4aSGavin Atkinson 				if (num == 0)
704f982db4aSGavin Atkinson 					break;
705f982db4aSGavin Atkinson 			}
706f982db4aSGavin Atkinson 			history(hist, &ev, H_ENTER, buf);
707f982db4aSGavin Atkinson 		}
708f982db4aSGavin Atkinson #endif /* !NO_EDITCOMPLETE */
709f982db4aSGavin Atkinson 
710f982db4aSGavin Atkinson 		makeargv();
711f982db4aSGavin Atkinson 		if (margc == 0)
712f982db4aSGavin Atkinson 			continue;
713f982db4aSGavin Atkinson 		c = getcmd(margv[0]);
714f982db4aSGavin Atkinson 		if (c == (struct cmd *)-1) {
715f982db4aSGavin Atkinson 			fputs("?Ambiguous command.\n", ttyout);
716f982db4aSGavin Atkinson 			continue;
717f982db4aSGavin Atkinson 		}
718f982db4aSGavin Atkinson 		if (c == NULL) {
719f982db4aSGavin Atkinson #if !defined(NO_EDITCOMPLETE)
720f982db4aSGavin Atkinson 			/*
721f982db4aSGavin Atkinson 			 * attempt to el_parse() unknown commands.
722f982db4aSGavin Atkinson 			 * any command containing a ':' would be parsed
723f982db4aSGavin Atkinson 			 * as "[prog:]cmd ...", and will result in a
724f982db4aSGavin Atkinson 			 * false positive if prog != "ftp", so treat
725f982db4aSGavin Atkinson 			 * such commands as invalid.
726f982db4aSGavin Atkinson 			 */
727f982db4aSGavin Atkinson 			if (strchr(margv[0], ':') != NULL ||
728f982db4aSGavin Atkinson 			    !editing ||
729f982db4aSGavin Atkinson 			    el_parse(el, margc, (const char **)margv) != 0)
730f982db4aSGavin Atkinson #endif /* !NO_EDITCOMPLETE */
731f982db4aSGavin Atkinson 				fputs("?Invalid command.\n", ttyout);
732f982db4aSGavin Atkinson 			continue;
733f982db4aSGavin Atkinson 		}
734f982db4aSGavin Atkinson 		if (c->c_conn && !connected) {
735f982db4aSGavin Atkinson 			fputs("Not connected.\n", ttyout);
736f982db4aSGavin Atkinson 			continue;
737f982db4aSGavin Atkinson 		}
738f982db4aSGavin Atkinson 		confirmrest = 0;
739cc361f65SGavin Atkinson 		(void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
740cc361f65SGavin Atkinson 		margv[0] = cmdbuf;
741f982db4aSGavin Atkinson 		(*c->c_handler)(margc, margv);
742f982db4aSGavin Atkinson 		if (bell && c->c_bell)
743f982db4aSGavin Atkinson 			(void)putc('\007', ttyout);
744f982db4aSGavin Atkinson 		if (c->c_handler != help)
745f982db4aSGavin Atkinson 			break;
746f982db4aSGavin Atkinson 	}
747f982db4aSGavin Atkinson 	(void)xsignal(SIGINT, intr);
748f982db4aSGavin Atkinson 	(void)xsignal(SIGPIPE, lostpeer);
749f982db4aSGavin Atkinson }
750f982db4aSGavin Atkinson 
751f982db4aSGavin Atkinson struct cmd *
getcmd(const char * name)752f982db4aSGavin Atkinson getcmd(const char *name)
753f982db4aSGavin Atkinson {
754f982db4aSGavin Atkinson 	const char *p, *q;
755f982db4aSGavin Atkinson 	struct cmd *c, *found;
756f982db4aSGavin Atkinson 	int nmatches, longest;
757f982db4aSGavin Atkinson 
758f982db4aSGavin Atkinson 	if (name == NULL)
759f982db4aSGavin Atkinson 		return (0);
760f982db4aSGavin Atkinson 
761f982db4aSGavin Atkinson 	longest = 0;
762f982db4aSGavin Atkinson 	nmatches = 0;
763f982db4aSGavin Atkinson 	found = 0;
764f982db4aSGavin Atkinson 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
765f982db4aSGavin Atkinson 		for (q = name; *q == *p++; q++)
766f982db4aSGavin Atkinson 			if (*q == 0)		/* exact match? */
767f982db4aSGavin Atkinson 				return (c);
768f982db4aSGavin Atkinson 		if (!*q) {			/* the name was a prefix */
769f982db4aSGavin Atkinson 			if (q - name > longest) {
770f982db4aSGavin Atkinson 				longest = q - name;
771f982db4aSGavin Atkinson 				nmatches = 1;
772f982db4aSGavin Atkinson 				found = c;
773f982db4aSGavin Atkinson 			} else if (q - name == longest)
774f982db4aSGavin Atkinson 				nmatches++;
775f982db4aSGavin Atkinson 		}
776f982db4aSGavin Atkinson 	}
777f982db4aSGavin Atkinson 	if (nmatches > 1)
778f982db4aSGavin Atkinson 		return ((struct cmd *)-1);
779f982db4aSGavin Atkinson 	return (found);
780f982db4aSGavin Atkinson }
781f982db4aSGavin Atkinson 
782f982db4aSGavin Atkinson /*
783f982db4aSGavin Atkinson  * Slice a string up into argc/argv.
784f982db4aSGavin Atkinson  */
785f982db4aSGavin Atkinson 
786f982db4aSGavin Atkinson int slrflag;
787f982db4aSGavin Atkinson 
788f982db4aSGavin Atkinson void
makeargv(void)789f982db4aSGavin Atkinson makeargv(void)
790f982db4aSGavin Atkinson {
791f982db4aSGavin Atkinson 	char *argp;
792f982db4aSGavin Atkinson 
793f982db4aSGavin Atkinson 	stringbase = line;		/* scan from first of buffer */
794f982db4aSGavin Atkinson 	argbase = argbuf;		/* store from first of buffer */
795f982db4aSGavin Atkinson 	slrflag = 0;
796f982db4aSGavin Atkinson 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
797f982db4aSGavin Atkinson 	for (margc = 0; ; margc++) {
798f982db4aSGavin Atkinson 		argp = slurpstring();
799cc361f65SGavin Atkinson 		ftp_sl_add(marg_sl, argp);
800f982db4aSGavin Atkinson 		if (argp == NULL)
801f982db4aSGavin Atkinson 			break;
802f982db4aSGavin Atkinson 	}
803f982db4aSGavin Atkinson #ifndef NO_EDITCOMPLETE
804f982db4aSGavin Atkinson 	if (cursor_pos == line) {
805f982db4aSGavin Atkinson 		cursor_argc = 0;
806f982db4aSGavin Atkinson 		cursor_argo = 0;
807f982db4aSGavin Atkinson 	} else if (cursor_pos != NULL) {
808f982db4aSGavin Atkinson 		cursor_argc = margc;
809f982db4aSGavin Atkinson 		cursor_argo = strlen(margv[margc-1]);
810f982db4aSGavin Atkinson 	}
811f982db4aSGavin Atkinson #endif /* !NO_EDITCOMPLETE */
812f982db4aSGavin Atkinson }
813f982db4aSGavin Atkinson 
814f982db4aSGavin Atkinson #ifdef NO_EDITCOMPLETE
815f982db4aSGavin Atkinson #define	INC_CHKCURSOR(x)	(x)++
816f982db4aSGavin Atkinson #else  /* !NO_EDITCOMPLETE */
817f982db4aSGavin Atkinson #define	INC_CHKCURSOR(x)	{ (x)++ ; \
818f982db4aSGavin Atkinson 				if (x == cursor_pos) { \
819f982db4aSGavin Atkinson 					cursor_argc = margc; \
820f982db4aSGavin Atkinson 					cursor_argo = ap-argbase; \
821f982db4aSGavin Atkinson 					cursor_pos = NULL; \
822f982db4aSGavin Atkinson 				} }
823f982db4aSGavin Atkinson 
824f982db4aSGavin Atkinson #endif /* !NO_EDITCOMPLETE */
825f982db4aSGavin Atkinson 
826f982db4aSGavin Atkinson /*
827f982db4aSGavin Atkinson  * Parse string into argbuf;
828f982db4aSGavin Atkinson  * implemented with FSM to
829f982db4aSGavin Atkinson  * handle quoting and strings
830f982db4aSGavin Atkinson  */
831f982db4aSGavin Atkinson char *
slurpstring(void)832f982db4aSGavin Atkinson slurpstring(void)
833f982db4aSGavin Atkinson {
834cc361f65SGavin Atkinson 	static char bangstr[2] = { '!', '\0' };
835cc361f65SGavin Atkinson 	static char dollarstr[2] = { '$', '\0' };
836f982db4aSGavin Atkinson 	int got_one = 0;
837f982db4aSGavin Atkinson 	char *sb = stringbase;
838f982db4aSGavin Atkinson 	char *ap = argbase;
839f982db4aSGavin Atkinson 	char *tmp = argbase;		/* will return this if token found */
840f982db4aSGavin Atkinson 
841f982db4aSGavin Atkinson 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
842f982db4aSGavin Atkinson 		switch (slrflag) {	/* and $ as token for macro invoke */
843f982db4aSGavin Atkinson 			case 0:
844f982db4aSGavin Atkinson 				slrflag++;
845f982db4aSGavin Atkinson 				INC_CHKCURSOR(stringbase);
846cc361f65SGavin Atkinson 				return ((*sb == '!') ? bangstr : dollarstr);
847f982db4aSGavin Atkinson 				/* NOTREACHED */
848f982db4aSGavin Atkinson 			case 1:
849f982db4aSGavin Atkinson 				slrflag++;
850f982db4aSGavin Atkinson 				altarg = stringbase;
851f982db4aSGavin Atkinson 				break;
852f982db4aSGavin Atkinson 			default:
853f982db4aSGavin Atkinson 				break;
854f982db4aSGavin Atkinson 		}
855f982db4aSGavin Atkinson 	}
856f982db4aSGavin Atkinson 
857f982db4aSGavin Atkinson S0:
858f982db4aSGavin Atkinson 	switch (*sb) {
859f982db4aSGavin Atkinson 
860f982db4aSGavin Atkinson 	case '\0':
861f982db4aSGavin Atkinson 		goto OUT;
862f982db4aSGavin Atkinson 
863f982db4aSGavin Atkinson 	case ' ':
864f982db4aSGavin Atkinson 	case '\t':
865f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
866f982db4aSGavin Atkinson 		goto S0;
867f982db4aSGavin Atkinson 
868f982db4aSGavin Atkinson 	default:
869f982db4aSGavin Atkinson 		switch (slrflag) {
870f982db4aSGavin Atkinson 			case 0:
871f982db4aSGavin Atkinson 				slrflag++;
872f982db4aSGavin Atkinson 				break;
873f982db4aSGavin Atkinson 			case 1:
874f982db4aSGavin Atkinson 				slrflag++;
875f982db4aSGavin Atkinson 				altarg = sb;
876f982db4aSGavin Atkinson 				break;
877f982db4aSGavin Atkinson 			default:
878f982db4aSGavin Atkinson 				break;
879f982db4aSGavin Atkinson 		}
880f982db4aSGavin Atkinson 		goto S1;
881f982db4aSGavin Atkinson 	}
882f982db4aSGavin Atkinson 
883f982db4aSGavin Atkinson S1:
884f982db4aSGavin Atkinson 	switch (*sb) {
885f982db4aSGavin Atkinson 
886f982db4aSGavin Atkinson 	case ' ':
887f982db4aSGavin Atkinson 	case '\t':
888f982db4aSGavin Atkinson 	case '\0':
889f982db4aSGavin Atkinson 		goto OUT;	/* end of token */
890f982db4aSGavin Atkinson 
891f982db4aSGavin Atkinson 	case '\\':
892f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
893f982db4aSGavin Atkinson 		goto S2;	/* slurp next character */
894f982db4aSGavin Atkinson 
895f982db4aSGavin Atkinson 	case '"':
896f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
897f982db4aSGavin Atkinson 		goto S3;	/* slurp quoted string */
898f982db4aSGavin Atkinson 
899f982db4aSGavin Atkinson 	default:
900f982db4aSGavin Atkinson 		*ap = *sb;	/* add character to token */
901f982db4aSGavin Atkinson 		ap++;
902f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
903f982db4aSGavin Atkinson 		got_one = 1;
904f982db4aSGavin Atkinson 		goto S1;
905f982db4aSGavin Atkinson 	}
906f982db4aSGavin Atkinson 
907f982db4aSGavin Atkinson S2:
908f982db4aSGavin Atkinson 	switch (*sb) {
909f982db4aSGavin Atkinson 
910f982db4aSGavin Atkinson 	case '\0':
911f982db4aSGavin Atkinson 		goto OUT;
912f982db4aSGavin Atkinson 
913f982db4aSGavin Atkinson 	default:
914f982db4aSGavin Atkinson 		*ap = *sb;
915f982db4aSGavin Atkinson 		ap++;
916f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
917f982db4aSGavin Atkinson 		got_one = 1;
918f982db4aSGavin Atkinson 		goto S1;
919f982db4aSGavin Atkinson 	}
920f982db4aSGavin Atkinson 
921f982db4aSGavin Atkinson S3:
922f982db4aSGavin Atkinson 	switch (*sb) {
923f982db4aSGavin Atkinson 
924f982db4aSGavin Atkinson 	case '\0':
925f982db4aSGavin Atkinson 		goto OUT;
926f982db4aSGavin Atkinson 
927f982db4aSGavin Atkinson 	case '"':
928f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
929f982db4aSGavin Atkinson 		goto S1;
930f982db4aSGavin Atkinson 
931f982db4aSGavin Atkinson 	default:
932f982db4aSGavin Atkinson 		*ap = *sb;
933f982db4aSGavin Atkinson 		ap++;
934f982db4aSGavin Atkinson 		INC_CHKCURSOR(sb);
935f982db4aSGavin Atkinson 		got_one = 1;
936f982db4aSGavin Atkinson 		goto S3;
937f982db4aSGavin Atkinson 	}
938f982db4aSGavin Atkinson 
939f982db4aSGavin Atkinson OUT:
940f982db4aSGavin Atkinson 	if (got_one)
941f982db4aSGavin Atkinson 		*ap++ = '\0';
942f982db4aSGavin Atkinson 	argbase = ap;			/* update storage pointer */
943f982db4aSGavin Atkinson 	stringbase = sb;		/* update scan pointer */
944f982db4aSGavin Atkinson 	if (got_one) {
945f982db4aSGavin Atkinson 		return (tmp);
946f982db4aSGavin Atkinson 	}
947f982db4aSGavin Atkinson 	switch (slrflag) {
948f982db4aSGavin Atkinson 		case 0:
949f982db4aSGavin Atkinson 			slrflag++;
950f982db4aSGavin Atkinson 			break;
951f982db4aSGavin Atkinson 		case 1:
952f982db4aSGavin Atkinson 			slrflag++;
953f982db4aSGavin Atkinson 			altarg = NULL;
954f982db4aSGavin Atkinson 			break;
955f982db4aSGavin Atkinson 		default:
956f982db4aSGavin Atkinson 			break;
957f982db4aSGavin Atkinson 	}
958f982db4aSGavin Atkinson 	return (NULL);
959f982db4aSGavin Atkinson }
960f982db4aSGavin Atkinson 
961f982db4aSGavin Atkinson /*
962f982db4aSGavin Atkinson  * Help/usage command.
963f982db4aSGavin Atkinson  * Call each command handler with argc == 0 and argv[0] == name.
964f982db4aSGavin Atkinson  */
965f982db4aSGavin Atkinson void
help(int argc,char * argv[])966f982db4aSGavin Atkinson help(int argc, char *argv[])
967f982db4aSGavin Atkinson {
968f982db4aSGavin Atkinson 	struct cmd *c;
969cc361f65SGavin Atkinson 	char *nargv[1], *cmd;
970cc361f65SGavin Atkinson 	const char *p;
971f982db4aSGavin Atkinson 	int isusage;
972f982db4aSGavin Atkinson 
973f982db4aSGavin Atkinson 	cmd = argv[0];
974f982db4aSGavin Atkinson 	isusage = (strcmp(cmd, "usage") == 0);
975f982db4aSGavin Atkinson 	if (argc == 0 || (isusage && argc == 1)) {
976cc361f65SGavin Atkinson 		UPRINTF("usage: %s [command [...]]\n", cmd);
977f982db4aSGavin Atkinson 		return;
978f982db4aSGavin Atkinson 	}
979f982db4aSGavin Atkinson 	if (argc == 1) {
980f982db4aSGavin Atkinson 		StringList *buf;
981f982db4aSGavin Atkinson 
982cc361f65SGavin Atkinson 		buf = ftp_sl_init();
983f982db4aSGavin Atkinson 		fprintf(ttyout,
984f982db4aSGavin Atkinson 		    "%sommands may be abbreviated.  Commands are:\n\n",
985f982db4aSGavin Atkinson 		    proxy ? "Proxy c" : "C");
986f982db4aSGavin Atkinson 		for (c = cmdtab; (p = c->c_name) != NULL; c++)
987f982db4aSGavin Atkinson 			if (!proxy || c->c_proxy)
988cc361f65SGavin Atkinson 				ftp_sl_add(buf, ftp_strdup(p));
989f982db4aSGavin Atkinson 		list_vertical(buf);
990cc361f65SGavin Atkinson 		sl_free(buf, 1);
991f982db4aSGavin Atkinson 		return;
992f982db4aSGavin Atkinson 	}
993f982db4aSGavin Atkinson 
994f982db4aSGavin Atkinson #define	HELPINDENT ((int) sizeof("disconnect"))
995f982db4aSGavin Atkinson 
996f982db4aSGavin Atkinson 	while (--argc > 0) {
997f982db4aSGavin Atkinson 		char *arg;
998cc361f65SGavin Atkinson 		char cmdbuf[MAX_C_NAME];
999f982db4aSGavin Atkinson 
1000f982db4aSGavin Atkinson 		arg = *++argv;
1001f982db4aSGavin Atkinson 		c = getcmd(arg);
1002f982db4aSGavin Atkinson 		if (c == (struct cmd *)-1)
1003f982db4aSGavin Atkinson 			fprintf(ttyout, "?Ambiguous %s command `%s'\n",
1004f982db4aSGavin Atkinson 			    cmd, arg);
1005f982db4aSGavin Atkinson 		else if (c == NULL)
1006f982db4aSGavin Atkinson 			fprintf(ttyout, "?Invalid %s command `%s'\n",
1007f982db4aSGavin Atkinson 			    cmd, arg);
1008f982db4aSGavin Atkinson 		else {
1009f982db4aSGavin Atkinson 			if (isusage) {
1010cc361f65SGavin Atkinson 				(void)strlcpy(cmdbuf, c->c_name, sizeof(cmdbuf));
1011cc361f65SGavin Atkinson 				nargv[0] = cmdbuf;
1012f982db4aSGavin Atkinson 				(*c->c_handler)(0, nargv);
1013f982db4aSGavin Atkinson 			} else
1014f982db4aSGavin Atkinson 				fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
1015f982db4aSGavin Atkinson 				    c->c_name, c->c_help);
1016f982db4aSGavin Atkinson 		}
1017f982db4aSGavin Atkinson 	}
1018f982db4aSGavin Atkinson }
1019f982db4aSGavin Atkinson 
1020f982db4aSGavin Atkinson struct option *
getoption(const char * name)1021f982db4aSGavin Atkinson getoption(const char *name)
1022f982db4aSGavin Atkinson {
1023f982db4aSGavin Atkinson 	const char *p;
1024f982db4aSGavin Atkinson 	struct option *c;
1025f982db4aSGavin Atkinson 
1026f982db4aSGavin Atkinson 	if (name == NULL)
1027f982db4aSGavin Atkinson 		return (NULL);
1028f982db4aSGavin Atkinson 	for (c = optiontab; (p = c->name) != NULL; c++) {
1029f982db4aSGavin Atkinson 		if (strcasecmp(p, name) == 0)
1030f982db4aSGavin Atkinson 			return (c);
1031f982db4aSGavin Atkinson 	}
1032f982db4aSGavin Atkinson 	return (NULL);
1033f982db4aSGavin Atkinson }
1034f982db4aSGavin Atkinson 
1035f982db4aSGavin Atkinson char *
getoptionvalue(const char * name)1036f982db4aSGavin Atkinson getoptionvalue(const char *name)
1037f982db4aSGavin Atkinson {
1038f982db4aSGavin Atkinson 	struct option *c;
1039f982db4aSGavin Atkinson 
1040f982db4aSGavin Atkinson 	if (name == NULL)
1041cc361f65SGavin Atkinson 		errx(1, "getoptionvalue: invoked with NULL name");
1042f982db4aSGavin Atkinson 	c = getoption(name);
1043f982db4aSGavin Atkinson 	if (c != NULL)
1044f982db4aSGavin Atkinson 		return (c->value);
1045cc361f65SGavin Atkinson 	errx(1, "getoptionvalue: invoked with unknown option `%s'", name);
1046f982db4aSGavin Atkinson 	/* NOTREACHED */
1047f982db4aSGavin Atkinson }
1048f982db4aSGavin Atkinson 
1049f982db4aSGavin Atkinson static void
setupoption(const char * name,const char * value,const char * defaultvalue)1050cc361f65SGavin Atkinson setupoption(const char *name, const char *value, const char *defaultvalue)
1051f982db4aSGavin Atkinson {
1052cc361f65SGavin Atkinson 	set_option(name, value ? value : defaultvalue, 0);
1053f982db4aSGavin Atkinson }
1054f982db4aSGavin Atkinson 
1055f982db4aSGavin Atkinson void
usage(void)1056f982db4aSGavin Atkinson usage(void)
1057f982db4aSGavin Atkinson {
1058f982db4aSGavin Atkinson 	const char *progname = getprogname();
1059f982db4aSGavin Atkinson 
1060f982db4aSGavin Atkinson 	(void)fprintf(stderr,
1061cc361f65SGavin Atkinson "usage: %s [-46AadefginpRtVv] [-N netrc] [-o outfile] [-P port] [-q quittime]\n"
1062cc361f65SGavin Atkinson "           [-r retry] [-s srcaddr] [-T dir,max[,inc]]\n"
1063cc361f65SGavin Atkinson "           [[user@]host [port]] [host:path[/]] [file:///file]\n"
1064cc361f65SGavin Atkinson "           [ftp://[user[:pass]@]host[:port]/path[/]]\n"
1065f982db4aSGavin Atkinson "           [http://[user[:pass]@]host[:port]/path] [...]\n"
1066f982db4aSGavin Atkinson "       %s -u URL file [...]\n", progname, progname);
1067f982db4aSGavin Atkinson 	exit(1);
1068f982db4aSGavin Atkinson }
1069