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