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