main.c revision 1.67 1 1.67 lukem /* $NetBSD: main.c,v 1.67 1999/11/12 02:50:38 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.67 lukem __RCSID("$NetBSD: main.c,v 1.67 1999/11/12 02:50:38 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.65 lukem #define FTP_PROXY "ftp_proxy" /* env var with FTP proxy location */
135 1.65 lukem #define HTTP_PROXY "http_proxy" /* env var with HTTP proxy location */
136 1.65 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.65 lukem static void setupoption __P((char *, char *, char *));
140 1.65 lukem int main __P((int, char **));
141 1.23 lukem
142 1.3 cgd int
143 1.1 cgd main(argc, argv)
144 1.3 cgd int argc;
145 1.1 cgd char *argv[];
146 1.1 cgd {
147 1.64 lukem int ch, rval;
148 1.1 cgd struct passwd *pw = NULL;
149 1.65 lukem char *cp, *ep, *anonuser, *anonpass;
150 1.50 lukem int dumbterm, s, len;
151 1.1 cgd
152 1.45 itojun ftpport = "ftp";
153 1.45 itojun httpport = "http";
154 1.45 itojun gateport = NULL;
155 1.24 lukem cp = getenv("FTPSERVERPORT");
156 1.45 itojun if (cp != NULL)
157 1.45 itojun gateport = cp;
158 1.45 itojun else
159 1.45 itojun gateport = "ftpgate";
160 1.1 cgd doglob = 1;
161 1.1 cgd interactive = 1;
162 1.1 cgd autologin = 1;
163 1.31 lukem passivemode = 1;
164 1.31 lukem activefallback = 1;
165 1.13 lukem preserve = 1;
166 1.17 lukem verbose = 0;
167 1.17 lukem progress = 0;
168 1.24 lukem gatemode = 0;
169 1.57 lukem data = -1;
170 1.39 lukem outfile = NULL;
171 1.39 lukem restartautofetch = 0;
172 1.43 cgd #ifndef NO_EDITCOMPLETE
173 1.18 lukem editing = 0;
174 1.21 lukem el = NULL;
175 1.21 lukem hist = NULL;
176 1.18 lukem #endif
177 1.60 lukem bytes = 0;
178 1.12 lukem mark = HASHBYTES;
179 1.44 lukem rate_get = 0;
180 1.44 lukem rate_get_incr = DEFAULTINCR;
181 1.44 lukem rate_put = 0;
182 1.44 lukem rate_put_incr = DEFAULTINCR;
183 1.49 lukem #ifdef INET6
184 1.47 itojun epsv4 = 1;
185 1.49 lukem #else
186 1.49 lukem epsv4 = 0;
187 1.49 lukem #endif
188 1.58 lukem epsv4bad = 0;
189 1.50 lukem
190 1.50 lukem /*
191 1.50 lukem * Get the default socket buffer sizes if we don't already have them.
192 1.50 lukem * It doesn't matter which socket we do this to, because on the first
193 1.50 lukem * call no socket buffer sizes will have been modified, so we are
194 1.50 lukem * guaranteed to get the system defaults.
195 1.50 lukem */
196 1.50 lukem s = socket(AF_INET, SOCK_STREAM, 0);
197 1.50 lukem if (s == -1)
198 1.50 lukem err(1, "can't create socket");
199 1.51 lukem len = sizeof(rcvbuf_size);
200 1.51 lukem if (getsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size, &len)
201 1.51 lukem < 0)
202 1.51 lukem err(1, "unable to get default rcvbuf size");
203 1.50 lukem len = sizeof(sndbuf_size);
204 1.50 lukem if (getsockopt(s, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size, &len)
205 1.50 lukem < 0)
206 1.50 lukem err(1, "unable to get default sndbuf size");
207 1.51 lukem (void)close(s);
208 1.51 lukem /* sanity check returned buffer sizes */
209 1.51 lukem if (rcvbuf_size <= 0)
210 1.51 lukem rcvbuf_size = 8192;
211 1.51 lukem if (sndbuf_size <= 0)
212 1.51 lukem sndbuf_size = 8192;
213 1.44 lukem
214 1.16 lukem marg_sl = sl_init();
215 1.25 lukem if ((tmpdir = getenv("TMPDIR")) == NULL)
216 1.25 lukem tmpdir = _PATH_TMP;
217 1.3 cgd
218 1.31 lukem /* Set default operation mode based on FTPMODE environment variable */
219 1.31 lukem if ((cp = getenv("FTPMODE")) != NULL) {
220 1.65 lukem if (strcasecmp(cp, "passive") == 0) {
221 1.31 lukem passivemode = 1;
222 1.31 lukem activefallback = 0;
223 1.65 lukem } else if (strcasecmp(cp, "active") == 0) {
224 1.31 lukem passivemode = 0;
225 1.31 lukem activefallback = 0;
226 1.65 lukem } else if (strcasecmp(cp, "gate") == 0) {
227 1.31 lukem gatemode = 1;
228 1.65 lukem } else if (strcasecmp(cp, "auto") == 0) {
229 1.31 lukem passivemode = 1;
230 1.31 lukem activefallback = 1;
231 1.31 lukem } else
232 1.31 lukem warnx("unknown $FTPMODE '%s'; using defaults", cp);
233 1.31 lukem }
234 1.31 lukem
235 1.32 lukem if (strcmp(__progname, "pftp") == 0) {
236 1.15 lukem passivemode = 1;
237 1.31 lukem activefallback = 0;
238 1.32 lukem } else if (strcmp(__progname, "gate-ftp") == 0)
239 1.24 lukem gatemode = 1;
240 1.24 lukem
241 1.24 lukem gateserver = getenv("FTPSERVER");
242 1.24 lukem if (gateserver == NULL || *gateserver == '\0')
243 1.24 lukem gateserver = GATE_SERVER;
244 1.24 lukem if (gatemode) {
245 1.24 lukem if (*gateserver == '\0') {
246 1.24 lukem warnx(
247 1.42 lukem "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
248 1.24 lukem gatemode = 0;
249 1.24 lukem }
250 1.24 lukem }
251 1.15 lukem
252 1.22 lukem cp = getenv("TERM");
253 1.22 lukem if (cp == NULL || strcmp(cp, "dumb") == 0)
254 1.22 lukem dumbterm = 1;
255 1.22 lukem else
256 1.22 lukem dumbterm = 0;
257 1.17 lukem fromatty = isatty(fileno(stdin));
258 1.37 lukem ttyout = stdout;
259 1.37 lukem if (isatty(fileno(ttyout))) {
260 1.41 lukem verbose = 1; /* verbose if to a tty */
261 1.37 lukem if (! dumbterm) {
262 1.43 cgd #ifndef NO_EDITCOMPLETE
263 1.41 lukem if (fromatty) /* editing mode on if tty is usable */
264 1.41 lukem editing = 1;
265 1.43 cgd #endif
266 1.43 cgd #ifndef NO_PROGRESS
267 1.37 lukem if (foregroundproc())
268 1.37 lukem progress = 1; /* progress bar on if fg */
269 1.43 cgd #endif
270 1.37 lukem }
271 1.18 lukem }
272 1.17 lukem
273 1.44 lukem while ((ch = getopt(argc, argv, "Aadefgino:pP:r:RtT:vV")) != -1) {
274 1.6 mycroft switch (ch) {
275 1.31 lukem case 'A':
276 1.31 lukem activefallback = 0;
277 1.31 lukem passivemode = 0;
278 1.31 lukem break;
279 1.31 lukem
280 1.12 lukem case 'a':
281 1.12 lukem anonftp = 1;
282 1.12 lukem break;
283 1.12 lukem
284 1.3 cgd case 'd':
285 1.3 cgd options |= SO_DEBUG;
286 1.3 cgd debug++;
287 1.3 cgd break;
288 1.12 lukem
289 1.18 lukem case 'e':
290 1.43 cgd #ifndef NO_EDITCOMPLETE
291 1.18 lukem editing = 0;
292 1.18 lukem #endif
293 1.18 lukem break;
294 1.18 lukem
295 1.36 lukem case 'f':
296 1.36 lukem flushcache = 1;
297 1.36 lukem break;
298 1.36 lukem
299 1.3 cgd case 'g':
300 1.3 cgd doglob = 0;
301 1.3 cgd break;
302 1.1 cgd
303 1.3 cgd case 'i':
304 1.3 cgd interactive = 0;
305 1.3 cgd break;
306 1.1 cgd
307 1.3 cgd case 'n':
308 1.3 cgd autologin = 0;
309 1.3 cgd break;
310 1.1 cgd
311 1.31 lukem case 'o':
312 1.31 lukem outfile = optarg;
313 1.31 lukem if (strcmp(outfile, "-") == 0)
314 1.31 lukem ttyout = stderr;
315 1.31 lukem break;
316 1.31 lukem
317 1.12 lukem case 'p':
318 1.12 lukem passivemode = 1;
319 1.31 lukem activefallback = 0;
320 1.12 lukem break;
321 1.12 lukem
322 1.12 lukem case 'P':
323 1.45 itojun ftpport = optarg;
324 1.12 lukem break;
325 1.12 lukem
326 1.31 lukem case 'r':
327 1.31 lukem retry_connect = strtol(optarg, &ep, 10);
328 1.39 lukem if (retry_connect < 1 || *ep != '\0')
329 1.31 lukem errx(1, "bad retry value: %s", optarg);
330 1.31 lukem break;
331 1.31 lukem
332 1.39 lukem case 'R':
333 1.39 lukem restartautofetch = 1;
334 1.39 lukem break;
335 1.39 lukem
336 1.3 cgd case 't':
337 1.17 lukem trace = 1;
338 1.3 cgd break;
339 1.1 cgd
340 1.44 lukem case 'T':
341 1.44 lukem {
342 1.44 lukem int targc;
343 1.44 lukem char *targv[6], *oac;
344 1.44 lukem
345 1.44 lukem /* look for `dir,max[,incr]' */
346 1.44 lukem targc = 0;
347 1.44 lukem targv[targc++] = "-T";
348 1.44 lukem oac = xstrdup(optarg);
349 1.44 lukem
350 1.44 lukem while ((cp = strsep(&oac, ",")) != NULL) {
351 1.44 lukem if (*cp == '\0') {
352 1.44 lukem warnx("bad throttle value: %s", optarg);
353 1.44 lukem usage();
354 1.44 lukem /* NOTREACHED */
355 1.44 lukem }
356 1.44 lukem targv[targc++] = cp;
357 1.44 lukem if (targc >= 5)
358 1.44 lukem break;
359 1.44 lukem }
360 1.44 lukem if (parserate(targc, targv, 1) == -1)
361 1.44 lukem usage();
362 1.44 lukem free(oac);
363 1.44 lukem break;
364 1.44 lukem }
365 1.44 lukem
366 1.3 cgd case 'v':
367 1.37 lukem progress = verbose = 1;
368 1.17 lukem break;
369 1.17 lukem
370 1.17 lukem case 'V':
371 1.37 lukem progress = verbose = 0;
372 1.3 cgd break;
373 1.1 cgd
374 1.3 cgd default:
375 1.14 lukem usage();
376 1.3 cgd }
377 1.1 cgd }
378 1.37 lukem /* set line buffering on ttyout */
379 1.37 lukem setvbuf(ttyout, NULL, _IOLBF, 0);
380 1.3 cgd argc -= optind;
381 1.3 cgd argv += optind;
382 1.3 cgd
383 1.1 cgd cpend = 0; /* no pending replies */
384 1.1 cgd proxy = 0; /* proxy not active */
385 1.1 cgd crflag = 1; /* strip c.r. on ascii gets */
386 1.1 cgd sendport = -1; /* not using ports */
387 1.1 cgd /*
388 1.1 cgd * Set up the home directory in case we're globbing.
389 1.1 cgd */
390 1.1 cgd cp = getlogin();
391 1.65 lukem if (cp != NULL)
392 1.1 cgd pw = getpwnam(cp);
393 1.1 cgd if (pw == NULL)
394 1.1 cgd pw = getpwuid(getuid());
395 1.65 lukem if (pw != NULL) {
396 1.53 lukem (void)strlcpy(home, pw->pw_dir, sizeof(home));
397 1.65 lukem anonuser = pw->pw_name;
398 1.65 lukem } else {
399 1.57 lukem (void)strlcpy(home, "/", sizeof(home));
400 1.65 lukem anonuser = "anonymous";
401 1.65 lukem }
402 1.65 lukem
403 1.65 lukem /*
404 1.65 lukem * Every anonymous FTP server I've encountered will accept the
405 1.65 lukem * string "username@", and will append the hostname itself. We
406 1.65 lukem * do this by default since many servers are picky about not
407 1.65 lukem * having a FQDN in the anonymous password.
408 1.65 lukem * - thorpej (at) netbsd.org
409 1.65 lukem */
410 1.65 lukem len = strlen(anonuser) + 2;
411 1.65 lukem anonpass = xmalloc(len);
412 1.65 lukem (void)strlcpy(anonpass, anonuser, len);
413 1.65 lukem (void)strlcat(anonpass, "@", len);
414 1.65 lukem
415 1.67 lukem /*
416 1.67 lukem * set all the defaults for options defined in
417 1.67 lukem * struct option optiontab[] declared in cmdtab.c
418 1.67 lukem */
419 1.65 lukem setupoption("anonpass", getenv("FTPANONPASS"), anonpass);
420 1.66 lukem setupoption("ftp_proxy", getenv(FTP_PROXY), "");
421 1.66 lukem setupoption("http_proxy", getenv(HTTP_PROXY), "");
422 1.66 lukem setupoption("no_proxy", getenv(NO_PROXY), "");
423 1.66 lukem setupoption("pager", getenv("PAGER"), DEFAULTPAGER);
424 1.67 lukem setupoption("prompt", getenv("FTPPROMPT"), DEFAULTPROMPT);
425 1.67 lukem setupoption("rprompt", getenv("FTPRPROMPT"), DEFAULTRPROMPT);
426 1.66 lukem
427 1.66 lukem free(anonpass);
428 1.12 lukem
429 1.17 lukem setttywidth(0);
430 1.60 lukem #ifdef SIGINFO
431 1.60 lukem (void)xsignal(SIGINFO, psummary);
432 1.60 lukem #endif
433 1.60 lukem (void)xsignal(SIGQUIT, psummary);
434 1.44 lukem (void)xsignal(SIGUSR1, crankrate);
435 1.44 lukem (void)xsignal(SIGUSR2, crankrate);
436 1.60 lukem (void)xsignal(SIGWINCH, setttywidth);
437 1.23 lukem
438 1.31 lukem #ifdef __GNUC__ /* to shut up gcc warnings */
439 1.23 lukem (void)&argc;
440 1.23 lukem (void)&argv;
441 1.23 lukem #endif
442 1.17 lukem
443 1.1 cgd if (argc > 0) {
444 1.48 lukem if (strchr(argv[0], ':') != NULL && ! isipv6addr(argv[0])) {
445 1.39 lukem rval = auto_fetch(argc, argv);
446 1.16 lukem if (rval >= 0) /* -1 == connected and cd-ed */
447 1.16 lukem exit(rval);
448 1.16 lukem } else {
449 1.16 lukem char *xargv[5];
450 1.3 cgd
451 1.61 lukem if (sigsetjmp(toplevel, 1))
452 1.16 lukem exit(0);
453 1.61 lukem (void)xsignal(SIGINT, intr);
454 1.65 lukem (void)xsignal(SIGPIPE, lostpeer);
455 1.16 lukem xargv[0] = __progname;
456 1.16 lukem xargv[1] = argv[0];
457 1.16 lukem xargv[2] = argv[1];
458 1.16 lukem xargv[3] = argv[2];
459 1.16 lukem xargv[4] = NULL;
460 1.31 lukem do {
461 1.31 lukem setpeer(argc+1, xargv);
462 1.31 lukem if (!retry_connect)
463 1.31 lukem break;
464 1.31 lukem if (!connected) {
465 1.31 lukem macnum = 0;
466 1.31 lukem fprintf(ttyout,
467 1.31 lukem "Retrying in %d seconds...\n",
468 1.31 lukem retry_connect);
469 1.31 lukem sleep(retry_connect);
470 1.31 lukem }
471 1.31 lukem } while (!connected);
472 1.31 lukem retry_connect = 0; /* connected, stop hiding msgs */
473 1.16 lukem }
474 1.1 cgd }
475 1.43 cgd #ifndef NO_EDITCOMPLETE
476 1.21 lukem controlediting();
477 1.43 cgd #endif /* !NO_EDITCOMPLETE */
478 1.64 lukem
479 1.64 lukem (void)sigsetjmp(toplevel, 1);
480 1.64 lukem (void)xsignal(SIGINT, intr);
481 1.65 lukem (void)xsignal(SIGPIPE, lostpeer);
482 1.64 lukem for (;;)
483 1.64 lukem cmdscanner();
484 1.1 cgd }
485 1.1 cgd
486 1.3 cgd /*
487 1.16 lukem * Generate a prompt
488 1.16 lukem */
489 1.3 cgd char *
490 1.16 lukem prompt()
491 1.1 cgd {
492 1.66 lukem static char **prompt;
493 1.66 lukem static char buf[MAXPATHLEN];
494 1.66 lukem
495 1.66 lukem if (prompt == NULL) {
496 1.66 lukem struct option *o;
497 1.66 lukem
498 1.66 lukem o = getoption("prompt");
499 1.66 lukem if (o == NULL)
500 1.66 lukem errx(1, "no such option `prompt'");
501 1.66 lukem prompt = &(o->value);
502 1.66 lukem }
503 1.67 lukem formatbuf(buf, sizeof(buf), *prompt ? *prompt : DEFAULTPROMPT);
504 1.67 lukem return (buf);
505 1.67 lukem }
506 1.67 lukem
507 1.67 lukem /*
508 1.67 lukem * Generate an rprompt
509 1.67 lukem */
510 1.67 lukem char *
511 1.67 lukem rprompt()
512 1.67 lukem {
513 1.67 lukem static char **rprompt;
514 1.67 lukem static char buf[MAXPATHLEN];
515 1.67 lukem
516 1.67 lukem if (rprompt == NULL) {
517 1.67 lukem struct option *o;
518 1.66 lukem
519 1.67 lukem o = getoption("rprompt");
520 1.67 lukem if (o == NULL)
521 1.67 lukem errx(1, "no such option `rprompt'");
522 1.67 lukem rprompt = &(o->value);
523 1.66 lukem }
524 1.67 lukem formatbuf(buf, sizeof(buf), *rprompt ? *rprompt : DEFAULTRPROMPT);
525 1.66 lukem return (buf);
526 1.1 cgd }
527 1.3 cgd
528 1.1 cgd /*
529 1.1 cgd * Command parser.
530 1.1 cgd */
531 1.3 cgd void
532 1.64 lukem cmdscanner()
533 1.1 cgd {
534 1.67 lukem struct cmd *c;
535 1.67 lukem char *p;
536 1.67 lukem int num;
537 1.1 cgd
538 1.1 cgd for (;;) {
539 1.43 cgd #ifndef NO_EDITCOMPLETE
540 1.16 lukem if (!editing) {
541 1.43 cgd #endif /* !NO_EDITCOMPLETE */
542 1.16 lukem if (fromatty) {
543 1.31 lukem fputs(prompt(), ttyout);
544 1.67 lukem p = rprompt();
545 1.67 lukem if (*p)
546 1.67 lukem fprintf(ttyout, "%s ", p);
547 1.31 lukem (void)fflush(ttyout);
548 1.16 lukem }
549 1.63 lukem if (fgets(line, sizeof(line), stdin) == NULL) {
550 1.63 lukem if (fromatty)
551 1.63 lukem putc('\n', ttyout);
552 1.16 lukem quit(0, 0);
553 1.63 lukem }
554 1.16 lukem num = strlen(line);
555 1.16 lukem if (num == 0)
556 1.16 lukem break;
557 1.16 lukem if (line[--num] == '\n') {
558 1.16 lukem if (num == 0)
559 1.16 lukem break;
560 1.16 lukem line[num] = '\0';
561 1.16 lukem } else if (num == sizeof(line) - 2) {
562 1.31 lukem fputs("sorry, input line too long.\n", ttyout);
563 1.16 lukem while ((num = getchar()) != '\n' && num != EOF)
564 1.16 lukem /* void */;
565 1.16 lukem break;
566 1.16 lukem } /* else it was a line without a newline */
567 1.43 cgd #ifndef NO_EDITCOMPLETE
568 1.17 lukem } else {
569 1.16 lukem const char *buf;
570 1.26 christos HistEvent ev;
571 1.16 lukem cursor_pos = NULL;
572 1.16 lukem
573 1.63 lukem if ((buf = el_gets(el, &num)) == NULL || num == 0) {
574 1.63 lukem if (fromatty)
575 1.63 lukem putc('\n', ttyout);
576 1.17 lukem quit(0, 0);
577 1.63 lukem }
578 1.40 lukem if (buf[--num] == '\n') {
579 1.16 lukem if (num == 0)
580 1.16 lukem break;
581 1.16 lukem } else if (num >= sizeof(line)) {
582 1.31 lukem fputs("sorry, input line too long.\n", ttyout);
583 1.1 cgd break;
584 1.16 lukem }
585 1.16 lukem memcpy(line, buf, num);
586 1.16 lukem line[num] = '\0';
587 1.26 christos history(hist, &ev, H_ENTER, buf);
588 1.16 lukem }
589 1.43 cgd #endif /* !NO_EDITCOMPLETE */
590 1.16 lukem
591 1.1 cgd makeargv();
592 1.16 lukem if (margc == 0)
593 1.1 cgd continue;
594 1.1 cgd c = getcmd(margv[0]);
595 1.1 cgd if (c == (struct cmd *)-1) {
596 1.31 lukem fputs("?Ambiguous command.\n", ttyout);
597 1.1 cgd continue;
598 1.1 cgd }
599 1.30 lukem if (c == NULL) {
600 1.43 cgd #if !defined(NO_EDITCOMPLETE)
601 1.30 lukem /*
602 1.30 lukem * attempt to el_parse() unknown commands.
603 1.30 lukem * any command containing a ':' would be parsed
604 1.30 lukem * as "[prog:]cmd ...", and will result in a
605 1.30 lukem * false positive if prog != "ftp", so treat
606 1.30 lukem * such commands as invalid.
607 1.30 lukem */
608 1.38 lukem if (strchr(margv[0], ':') != NULL ||
609 1.30 lukem el_parse(el, margc, margv) != 0)
610 1.43 cgd #endif /* !NO_EDITCOMPLETE */
611 1.31 lukem fputs("?Invalid command.\n", ttyout);
612 1.1 cgd continue;
613 1.1 cgd }
614 1.1 cgd if (c->c_conn && !connected) {
615 1.31 lukem fputs("Not connected.\n", ttyout);
616 1.1 cgd continue;
617 1.1 cgd }
618 1.13 lukem confirmrest = 0;
619 1.1 cgd (*c->c_handler)(margc, margv);
620 1.1 cgd if (bell && c->c_bell)
621 1.31 lukem (void)putc('\007', ttyout);
622 1.1 cgd if (c->c_handler != help)
623 1.1 cgd break;
624 1.1 cgd }
625 1.61 lukem (void)xsignal(SIGINT, intr);
626 1.65 lukem (void)xsignal(SIGPIPE, lostpeer);
627 1.1 cgd }
628 1.1 cgd
629 1.1 cgd struct cmd *
630 1.1 cgd getcmd(name)
631 1.13 lukem const char *name;
632 1.1 cgd {
633 1.13 lukem const char *p, *q;
634 1.3 cgd struct cmd *c, *found;
635 1.3 cgd int nmatches, longest;
636 1.11 pk
637 1.11 pk if (name == NULL)
638 1.11 pk return (0);
639 1.1 cgd
640 1.1 cgd longest = 0;
641 1.1 cgd nmatches = 0;
642 1.1 cgd found = 0;
643 1.12 lukem for (c = cmdtab; (p = c->c_name) != NULL; c++) {
644 1.1 cgd for (q = name; *q == *p++; q++)
645 1.1 cgd if (*q == 0) /* exact match? */
646 1.1 cgd return (c);
647 1.1 cgd if (!*q) { /* the name was a prefix */
648 1.1 cgd if (q - name > longest) {
649 1.1 cgd longest = q - name;
650 1.1 cgd nmatches = 1;
651 1.1 cgd found = c;
652 1.1 cgd } else if (q - name == longest)
653 1.1 cgd nmatches++;
654 1.1 cgd }
655 1.1 cgd }
656 1.1 cgd if (nmatches > 1)
657 1.1 cgd return ((struct cmd *)-1);
658 1.1 cgd return (found);
659 1.1 cgd }
660 1.1 cgd
661 1.1 cgd /*
662 1.1 cgd * Slice a string up into argc/argv.
663 1.1 cgd */
664 1.1 cgd
665 1.1 cgd int slrflag;
666 1.1 cgd
667 1.3 cgd void
668 1.1 cgd makeargv()
669 1.1 cgd {
670 1.16 lukem char *argp;
671 1.1 cgd
672 1.1 cgd stringbase = line; /* scan from first of buffer */
673 1.1 cgd argbase = argbuf; /* store from first of buffer */
674 1.1 cgd slrflag = 0;
675 1.16 lukem marg_sl->sl_cur = 0; /* reset to start of marg_sl */
676 1.10 pk for (margc = 0; ; margc++) {
677 1.16 lukem argp = slurpstring();
678 1.16 lukem sl_add(marg_sl, argp);
679 1.16 lukem if (argp == NULL)
680 1.10 pk break;
681 1.10 pk }
682 1.43 cgd #ifndef NO_EDITCOMPLETE
683 1.16 lukem if (cursor_pos == line) {
684 1.16 lukem cursor_argc = 0;
685 1.16 lukem cursor_argo = 0;
686 1.16 lukem } else if (cursor_pos != NULL) {
687 1.16 lukem cursor_argc = margc;
688 1.16 lukem cursor_argo = strlen(margv[margc-1]);
689 1.16 lukem }
690 1.43 cgd #endif /* !NO_EDITCOMPLETE */
691 1.16 lukem }
692 1.10 pk
693 1.43 cgd #ifdef NO_EDITCOMPLETE
694 1.65 lukem #define INC_CHKCURSOR(x) (x)++
695 1.43 cgd #else /* !NO_EDITCOMPLETE */
696 1.65 lukem #define INC_CHKCURSOR(x) { (x)++ ; \
697 1.16 lukem if (x == cursor_pos) { \
698 1.16 lukem cursor_argc = margc; \
699 1.16 lukem cursor_argo = ap-argbase; \
700 1.16 lukem cursor_pos = NULL; \
701 1.16 lukem } }
702 1.38 lukem
703 1.43 cgd #endif /* !NO_EDITCOMPLETE */
704 1.1 cgd
705 1.1 cgd /*
706 1.1 cgd * Parse string into argbuf;
707 1.1 cgd * implemented with FSM to
708 1.1 cgd * handle quoting and strings
709 1.1 cgd */
710 1.1 cgd char *
711 1.1 cgd slurpstring()
712 1.1 cgd {
713 1.1 cgd int got_one = 0;
714 1.3 cgd char *sb = stringbase;
715 1.3 cgd char *ap = argbase;
716 1.1 cgd char *tmp = argbase; /* will return this if token found */
717 1.1 cgd
718 1.1 cgd if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
719 1.1 cgd switch (slrflag) { /* and $ as token for macro invoke */
720 1.1 cgd case 0:
721 1.1 cgd slrflag++;
722 1.16 lukem INC_CHKCURSOR(stringbase);
723 1.1 cgd return ((*sb == '!') ? "!" : "$");
724 1.1 cgd /* NOTREACHED */
725 1.1 cgd case 1:
726 1.1 cgd slrflag++;
727 1.1 cgd altarg = stringbase;
728 1.1 cgd break;
729 1.1 cgd default:
730 1.1 cgd break;
731 1.1 cgd }
732 1.1 cgd }
733 1.1 cgd
734 1.1 cgd S0:
735 1.1 cgd switch (*sb) {
736 1.1 cgd
737 1.1 cgd case '\0':
738 1.1 cgd goto OUT;
739 1.1 cgd
740 1.1 cgd case ' ':
741 1.1 cgd case '\t':
742 1.16 lukem INC_CHKCURSOR(sb);
743 1.16 lukem goto S0;
744 1.1 cgd
745 1.1 cgd default:
746 1.1 cgd switch (slrflag) {
747 1.1 cgd case 0:
748 1.1 cgd slrflag++;
749 1.1 cgd break;
750 1.1 cgd case 1:
751 1.1 cgd slrflag++;
752 1.1 cgd altarg = sb;
753 1.1 cgd break;
754 1.1 cgd default:
755 1.1 cgd break;
756 1.1 cgd }
757 1.1 cgd goto S1;
758 1.1 cgd }
759 1.1 cgd
760 1.1 cgd S1:
761 1.1 cgd switch (*sb) {
762 1.1 cgd
763 1.1 cgd case ' ':
764 1.1 cgd case '\t':
765 1.1 cgd case '\0':
766 1.1 cgd goto OUT; /* end of token */
767 1.1 cgd
768 1.1 cgd case '\\':
769 1.16 lukem INC_CHKCURSOR(sb);
770 1.16 lukem goto S2; /* slurp next character */
771 1.1 cgd
772 1.1 cgd case '"':
773 1.16 lukem INC_CHKCURSOR(sb);
774 1.16 lukem goto S3; /* slurp quoted string */
775 1.1 cgd
776 1.1 cgd default:
777 1.16 lukem *ap = *sb; /* add character to token */
778 1.16 lukem ap++;
779 1.16 lukem INC_CHKCURSOR(sb);
780 1.1 cgd got_one = 1;
781 1.1 cgd goto S1;
782 1.1 cgd }
783 1.1 cgd
784 1.1 cgd S2:
785 1.1 cgd switch (*sb) {
786 1.1 cgd
787 1.1 cgd case '\0':
788 1.1 cgd goto OUT;
789 1.1 cgd
790 1.1 cgd default:
791 1.16 lukem *ap = *sb;
792 1.16 lukem ap++;
793 1.16 lukem INC_CHKCURSOR(sb);
794 1.1 cgd got_one = 1;
795 1.1 cgd goto S1;
796 1.1 cgd }
797 1.1 cgd
798 1.1 cgd S3:
799 1.1 cgd switch (*sb) {
800 1.1 cgd
801 1.1 cgd case '\0':
802 1.1 cgd goto OUT;
803 1.1 cgd
804 1.1 cgd case '"':
805 1.16 lukem INC_CHKCURSOR(sb);
806 1.16 lukem goto S1;
807 1.1 cgd
808 1.1 cgd default:
809 1.16 lukem *ap = *sb;
810 1.16 lukem ap++;
811 1.16 lukem INC_CHKCURSOR(sb);
812 1.1 cgd got_one = 1;
813 1.1 cgd goto S3;
814 1.1 cgd }
815 1.1 cgd
816 1.1 cgd OUT:
817 1.1 cgd if (got_one)
818 1.1 cgd *ap++ = '\0';
819 1.1 cgd argbase = ap; /* update storage pointer */
820 1.1 cgd stringbase = sb; /* update scan pointer */
821 1.1 cgd if (got_one) {
822 1.3 cgd return (tmp);
823 1.1 cgd }
824 1.1 cgd switch (slrflag) {
825 1.1 cgd case 0:
826 1.1 cgd slrflag++;
827 1.1 cgd break;
828 1.1 cgd case 1:
829 1.1 cgd slrflag++;
830 1.28 lukem altarg = NULL;
831 1.1 cgd break;
832 1.1 cgd default:
833 1.1 cgd break;
834 1.1 cgd }
835 1.28 lukem return (NULL);
836 1.1 cgd }
837 1.1 cgd
838 1.1 cgd /*
839 1.65 lukem * Help/usage command.
840 1.1 cgd * Call each command handler with argc == 0 and argv[0] == name.
841 1.1 cgd */
842 1.3 cgd void
843 1.1 cgd help(argc, argv)
844 1.1 cgd int argc;
845 1.1 cgd char *argv[];
846 1.1 cgd {
847 1.3 cgd struct cmd *c;
848 1.67 lukem char *nargv[1], *p, *cmd;
849 1.65 lukem int isusage;
850 1.1 cgd
851 1.67 lukem cmd = argv[0];
852 1.67 lukem isusage = (strcmp(cmd, "usage") == 0);
853 1.65 lukem if (argc == 0 || (isusage && argc == 1)) {
854 1.67 lukem fprintf(ttyout, "usage: %s [command [...]]\n", cmd);
855 1.65 lukem return;
856 1.65 lukem }
857 1.1 cgd if (argc == 1) {
858 1.16 lukem StringList *buf;
859 1.1 cgd
860 1.16 lukem buf = sl_init();
861 1.31 lukem fprintf(ttyout,
862 1.31 lukem "%sommands may be abbreviated. Commands are:\n\n",
863 1.16 lukem proxy ? "Proxy c" : "C");
864 1.65 lukem for (c = cmdtab; (p = c->c_name) != NULL; c++)
865 1.65 lukem if (!proxy || c->c_proxy)
866 1.65 lukem sl_add(buf, p);
867 1.16 lukem list_vertical(buf);
868 1.16 lukem sl_free(buf, 0);
869 1.1 cgd return;
870 1.1 cgd }
871 1.16 lukem
872 1.65 lukem #define HELPINDENT ((int) sizeof("disconnect"))
873 1.16 lukem
874 1.1 cgd while (--argc > 0) {
875 1.3 cgd char *arg;
876 1.16 lukem
877 1.1 cgd arg = *++argv;
878 1.1 cgd c = getcmd(arg);
879 1.1 cgd if (c == (struct cmd *)-1)
880 1.65 lukem fprintf(ttyout, "?Ambiguous %s command `%s'\n",
881 1.67 lukem cmd, arg);
882 1.28 lukem else if (c == NULL)
883 1.65 lukem fprintf(ttyout, "?Invalid %s command `%s'\n",
884 1.67 lukem cmd, arg);
885 1.65 lukem else {
886 1.65 lukem if (isusage) {
887 1.65 lukem nargv[0] = arg;
888 1.65 lukem (*c->c_handler)(0, nargv);
889 1.65 lukem } else
890 1.65 lukem fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
891 1.65 lukem c->c_name, c->c_help);
892 1.65 lukem }
893 1.1 cgd }
894 1.65 lukem }
895 1.65 lukem
896 1.65 lukem struct option *
897 1.65 lukem getoption(name)
898 1.65 lukem const char *name;
899 1.65 lukem {
900 1.65 lukem const char *p;
901 1.65 lukem struct option *c;
902 1.65 lukem
903 1.65 lukem if (name == NULL)
904 1.65 lukem return (NULL);
905 1.65 lukem for (c = optiontab; (p = c->name) != NULL; c++) {
906 1.65 lukem if (strcasecmp(p, name) == 0)
907 1.65 lukem return (c);
908 1.65 lukem }
909 1.65 lukem return (NULL);
910 1.65 lukem }
911 1.65 lukem
912 1.65 lukem char *
913 1.65 lukem getoptionvalue(name)
914 1.65 lukem const char *name;
915 1.65 lukem {
916 1.65 lukem struct option *c;
917 1.65 lukem
918 1.65 lukem if (name == NULL)
919 1.65 lukem errx(1, "getoptionvalue() invoked with NULL name");
920 1.66 lukem c = getoption(name);
921 1.66 lukem if (c != NULL)
922 1.66 lukem return (c->value);
923 1.65 lukem errx(1, "getoptionvalue() invoked with unknown option `%s'", name);
924 1.65 lukem }
925 1.65 lukem
926 1.65 lukem static void
927 1.65 lukem setupoption(name, value, defaultvalue)
928 1.65 lukem char *name, *value, *defaultvalue;
929 1.65 lukem {
930 1.65 lukem char *nargv[3];
931 1.65 lukem int overbose;
932 1.65 lukem
933 1.65 lukem nargv[0] = "setupoption()";
934 1.65 lukem nargv[1] = name;
935 1.65 lukem nargv[2] = (value ? value : defaultvalue);
936 1.65 lukem overbose = verbose;
937 1.65 lukem verbose = 0;
938 1.65 lukem setoption(3, nargv);
939 1.65 lukem verbose = overbose;
940 1.12 lukem }
941 1.12 lukem
942 1.14 lukem void
943 1.14 lukem usage()
944 1.14 lukem {
945 1.14 lukem (void)fprintf(stderr,
946 1.44 lukem "usage: %s [-AadefginpRtvV] [-r retry] [-o outfile] [-P port] [-T dir,max[,inc]\n"
947 1.44 lukem " [host [port]] [file:///file] [ftp://[user[:pass]@]host[:port]/path[/]]\n"
948 1.44 lukem " [http://[user[:pass]@]host[:port]/path] [host:path[/]]\n", __progname);
949 1.14 lukem exit(1);
950 1.1 cgd }
951