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