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