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