Annotation of src/usr.bin/ftp/main.c, Revision 1.57
1.57 ! deraadt 1: /* $OpenBSD: main.c,v 1.56 2003/11/20 12:43:55 jmc Exp $ */
1.36 millert 2: /* $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $ */
1.5 deraadt 3:
1.1 deraadt 4: /*
1.44 itojun 5: * Copyright (C) 1997 and 1998 WIDE Project.
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. Neither the name of the project nor the names of its contributors
17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
1.1 deraadt 34: * Copyright (c) 1985, 1989, 1993, 1994
35: * The Regents of the University of California. All rights reserved.
36: *
37: * Redistribution and use in source and binary forms, with or without
38: * modification, are permitted provided that the following conditions
39: * are met:
40: * 1. Redistributions of source code must retain the above copyright
41: * notice, this list of conditions and the following disclaimer.
42: * 2. Redistributions in binary form must reproduce the above copyright
43: * notice, this list of conditions and the following disclaimer in the
44: * documentation and/or other materials provided with the distribution.
1.53 millert 45: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 46: * may be used to endorse or promote products derived from this software
47: * without specific prior written permission.
48: *
49: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59: * SUCH DAMAGE.
60: */
61:
62: #ifndef lint
63: static char copyright[] =
64: "@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
65: The Regents of the University of California. All rights reserved.\n";
66: #endif /* not lint */
67:
1.57 ! deraadt 68: #if !defined(lint) && !defined(SMALL)
! 69: static char rcsid[] = "$OpenBSD: main.c,v 1.56 2003/11/20 12:43:55 jmc Exp $";
! 70: #endif /* not lint and not SMALL */
1.1 deraadt 71:
72: /*
73: * FTP User Program -- Command Interface.
74: */
75: #include <sys/types.h>
76: #include <sys/socket.h>
77:
1.18 millert 78: #include <ctype.h>
1.1 deraadt 79: #include <err.h>
80: #include <netdb.h>
81: #include <pwd.h>
82: #include <stdio.h>
1.39 deraadt 83: #include <errno.h>
1.18 millert 84: #include <stdlib.h>
1.1 deraadt 85: #include <string.h>
86: #include <unistd.h>
87:
88: #include "ftp_var.h"
89:
1.49 deraadt 90: int family = PF_UNSPEC;
91:
1.1 deraadt 92: int
1.54 deraadt 93: main(volatile int argc, char *argv[])
1.1 deraadt 94: {
1.34 millert 95: int ch, top, rval;
1.1 deraadt 96: struct passwd *pw = NULL;
1.44 itojun 97: char *cp, homedir[MAXPATHLEN];
1.38 millert 98: char *outfile = NULL;
1.28 millert 99: int dumb_terminal = 0;
1.1 deraadt 100:
1.44 itojun 101: ftpport = "ftp";
102: httpport = "http";
103: gateport = getenv("FTPSERVERPORT");
1.51 millert 104: if (gateport == NULL || *gateport == '\0')
1.44 itojun 105: gateport = "ftpgate";
1.1 deraadt 106: doglob = 1;
107: interactive = 1;
108: autologin = 1;
1.37 millert 109: passivemode = 1;
110: activefallback = 1;
1.17 millert 111: preserve = 1;
112: verbose = 0;
113: progress = 0;
1.36 millert 114: gatemode = 0;
1.25 millert 115: #ifndef SMALL
1.22 millert 116: editing = 0;
1.28 millert 117: el = NULL;
118: hist = NULL;
1.22 millert 119: #endif
1.8 kstailey 120: mark = HASHBYTES;
1.17 millert 121: marg_sl = sl_init();
1.47 itojun 122: #ifdef INET6
123: epsv4 = 1;
124: #else
125: epsv4 = 0;
126: #endif
127: epsv4bad = 0;
1.1 deraadt 128:
1.37 millert 129: /* Set default operation mode based on FTPMODE environment variable */
1.51 millert 130: if ((cp = getenv("FTPMODE")) != NULL && *cp != '\0') {
1.37 millert 131: if (strcmp(cp, "passive") == 0) {
132: passivemode = 1;
133: activefallback = 0;
134: } else if (strcmp(cp, "active") == 0) {
135: passivemode = 0;
136: activefallback = 0;
137: } else if (strcmp(cp, "gate") == 0) {
138: gatemode = 1;
139: } else if (strcmp(cp, "auto") == 0) {
140: passivemode = 1;
141: activefallback = 1;
142: } else
143: warnx("unknown FTPMODE: %s. Using defaults", cp);
144: }
145:
146: if (strcmp(__progname, "gate-ftp") == 0)
1.36 millert 147: gatemode = 1;
148: gateserver = getenv("FTPSERVER");
149: if (gateserver == NULL || *gateserver == '\0')
150: gateserver = GATE_SERVER;
151: if (gatemode) {
152: if (*gateserver == '\0') {
153: warnx(
1.37 millert 154: "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
1.36 millert 155: gatemode = 0;
156: }
157: }
1.17 millert 158:
1.31 millert 159: cp = getenv("TERM");
1.51 millert 160: dumb_terminal = (cp == NULL || *cp == '\0' || !strcmp(cp, "dumb") ||
1.31 millert 161: !strcmp(cp, "emacs") || !strcmp(cp, "su"));
1.17 millert 162: fromatty = isatty(fileno(stdin));
1.22 millert 163: if (fromatty) {
1.17 millert 164: verbose = 1; /* verbose if from a tty */
1.25 millert 165: #ifndef SMALL
1.28 millert 166: if (!dumb_terminal)
1.34 millert 167: editing = 1; /* editing mode on if tty is usable */
1.22 millert 168: #endif
169: }
1.30 deraadt 170:
171: ttyout = stdout;
1.33 millert 172: if (isatty(fileno(ttyout)) && !dumb_terminal && foregroundproc())
1.34 millert 173: progress = 1; /* progress bar on if tty is usable */
1.32 deraadt 174:
1.49 deraadt 175: while ((ch = getopt(argc, argv, "46Aadegimno:pP:r:tvV")) != -1) {
1.1 deraadt 176: switch (ch) {
1.49 deraadt 177: case '4':
178: family = PF_INET;
179: break;
180: case '6':
181: family = PF_INET6;
182: break;
1.37 millert 183: case 'A':
184: activefallback = 0;
185: passivemode = 0;
186: break;
187:
1.17 millert 188: case 'a':
189: anonftp = 1;
190: break;
191:
1.1 deraadt 192: case 'd':
193: options |= SO_DEBUG;
194: debug++;
195: break;
1.17 millert 196:
1.28 millert 197: case 'e':
198: #ifndef SMALL
1.22 millert 199: editing = 0;
200: #endif
1.21 kstailey 201: break;
202:
1.1 deraadt 203: case 'g':
204: doglob = 0;
205: break;
206:
207: case 'i':
208: interactive = 0;
1.41 millert 209: break;
210:
211: case 'm':
1.42 millert 212: progress = -1;
1.1 deraadt 213: break;
214:
215: case 'n':
216: autologin = 0;
217: break;
218:
1.38 millert 219: case 'o':
220: outfile = optarg;
221: if (strcmp(outfile, "-") == 0)
222: ttyout = stderr;
223: break;
224:
1.3 deraadt 225: case 'p':
1.17 millert 226: passivemode = 1;
1.37 millert 227: activefallback = 0;
1.3 deraadt 228: break;
229:
1.17 millert 230: case 'P':
1.44 itojun 231: ftpport = optarg;
1.7 mickey 232: break;
233:
1.18 millert 234: case 'r':
235: if (isdigit(*optarg))
236: retry_connect = atoi(optarg);
237: else
238: errx(1, "-r requires numeric argument");
239: break;
240:
1.1 deraadt 241: case 't':
1.17 millert 242: trace = 1;
1.1 deraadt 243: break;
244:
245: case 'v':
1.17 millert 246: verbose = 1;
247: break;
248:
249: case 'V':
250: verbose = 0;
1.1 deraadt 251: break;
252:
253: default:
1.17 millert 254: usage();
1.1 deraadt 255: }
256: }
257: argc -= optind;
258: argv += optind;
259:
260: cpend = 0; /* no pending replies */
261: proxy = 0; /* proxy not active */
262: crflag = 1; /* strip c.r. on ascii gets */
263: sendport = -1; /* not using ports */
264: /*
265: * Set up the home directory in case we're globbing.
266: */
267: cp = getlogin();
268: if (cp != NULL) {
269: pw = getpwnam(cp);
270: }
271: if (pw == NULL)
272: pw = getpwuid(getuid());
273: if (pw != NULL) {
1.52 deraadt 274: (void)strlcpy(homedir, pw->pw_dir, sizeof homedir);
1.1 deraadt 275: home = homedir;
1.3 deraadt 276: }
1.9 michaels 277:
1.17 millert 278: setttywidth(0);
1.18 millert 279: (void)signal(SIGWINCH, setttywidth);
1.34 millert 280:
1.17 millert 281: if (argc > 0) {
1.44 itojun 282: if (isurl(argv[0])) {
1.17 millert 283: anonftp = 1; /* Handle "automatic" transfers. */
1.38 millert 284: rval = auto_fetch(argc, argv, outfile);
1.17 millert 285: if (rval >= 0) /* -1 == connected and cd-ed */
286: exit(rval);
287: } else {
1.3 deraadt 288: char *xargv[5];
289:
290: if (setjmp(toplevel))
291: exit(0);
1.20 millert 292: (void)signal(SIGINT, (sig_t)intr);
293: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.3 deraadt 294: xargv[0] = __progname;
1.17 millert 295: xargv[1] = argv[0];
296: xargv[2] = argv[1];
297: xargv[3] = argv[2];
298: xargv[4] = NULL;
1.18 millert 299: do {
300: setpeer(argc+1, xargv);
301: if (!retry_connect)
302: break;
303: if (!connected) {
304: macnum = 0;
1.30 deraadt 305: fputs("Retrying...\n", ttyout);
1.18 millert 306: sleep(retry_connect);
307: }
308: } while (!connected);
1.35 mickey 309: retry_connect = 0; /* connected, stop hiding msgs */
1.3 deraadt 310: }
1.1 deraadt 311: }
1.28 millert 312: #ifndef SMALL
313: controlediting();
314: #endif /* !SMALL */
1.1 deraadt 315: top = setjmp(toplevel) == 0;
316: if (top) {
1.20 millert 317: (void)signal(SIGINT, (sig_t)intr);
318: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 deraadt 319: }
320: for (;;) {
321: cmdscanner(top);
322: top = 1;
323: }
324: }
325:
326: void
327: intr()
328: {
329:
1.17 millert 330: alarmtimer(0);
1.1 deraadt 331: longjmp(toplevel, 1);
332: }
333:
334: void
335: lostpeer()
336: {
1.39 deraadt 337: int save_errno = errno;
1.1 deraadt 338:
1.17 millert 339: alarmtimer(0);
1.1 deraadt 340: if (connected) {
341: if (cout != NULL) {
1.18 millert 342: (void)shutdown(fileno(cout), 1+1);
343: (void)fclose(cout);
1.1 deraadt 344: cout = NULL;
345: }
346: if (data >= 0) {
1.18 millert 347: (void)shutdown(data, 1+1);
348: (void)close(data);
1.1 deraadt 349: data = -1;
350: }
351: connected = 0;
352: }
353: pswitch(1);
354: if (connected) {
355: if (cout != NULL) {
1.18 millert 356: (void)shutdown(fileno(cout), 1+1);
357: (void)fclose(cout);
1.1 deraadt 358: cout = NULL;
359: }
360: connected = 0;
361: }
362: proxflag = 0;
363: pswitch(0);
1.39 deraadt 364: errno = save_errno;
1.1 deraadt 365: }
366:
367: /*
1.17 millert 368: * Generate a prompt
369: */
1.1 deraadt 370: char *
1.17 millert 371: prompt()
1.1 deraadt 372: {
1.17 millert 373: return ("ftp> ");
1.1 deraadt 374: }
375:
376: /*
377: * Command parser.
378: */
379: void
380: cmdscanner(top)
381: int top;
382: {
383: struct cmd *c;
1.17 millert 384: int num;
1.55 otto 385: #ifndef SMALL
386: HistEvent hev;
387: #endif
1.1 deraadt 388:
1.17 millert 389: if (!top
1.25 millert 390: #ifndef SMALL
1.17 millert 391: && !editing
1.25 millert 392: #endif /* !SMALL */
1.17 millert 393: )
1.30 deraadt 394: (void)putc('\n', ttyout);
1.1 deraadt 395: for (;;) {
1.25 millert 396: #ifndef SMALL
1.17 millert 397: if (!editing) {
1.25 millert 398: #endif /* !SMALL */
1.17 millert 399: if (fromatty) {
1.30 deraadt 400: fputs(prompt(), ttyout);
401: (void)fflush(ttyout);
1.17 millert 402: }
403: if (fgets(line, sizeof(line), stdin) == NULL)
404: quit(0, 0);
405: num = strlen(line);
406: if (num == 0)
407: break;
408: if (line[--num] == '\n') {
409: if (num == 0)
410: break;
411: line[num] = '\0';
412: } else if (num == sizeof(line) - 2) {
1.30 deraadt 413: fputs("sorry, input line too long.\n", ttyout);
1.17 millert 414: while ((num = getchar()) != '\n' && num != EOF)
415: /* void */;
416: break;
417: } /* else it was a line without a newline */
1.25 millert 418: #ifndef SMALL
1.17 millert 419: } else {
420: const char *buf;
421: cursor_pos = NULL;
422:
423: if ((buf = el_gets(el, &num)) == NULL || num == 0)
424: quit(0, 0);
1.46 fgsch 425: if (buf[--num] == '\n') {
1.17 millert 426: if (num == 0)
427: break;
1.46 fgsch 428: }
429: if (num >= sizeof(line)) {
1.30 deraadt 430: fputs("sorry, input line too long.\n", ttyout);
1.1 deraadt 431: break;
1.17 millert 432: }
1.34 millert 433: memcpy(line, buf, (size_t)num);
1.17 millert 434: line[num] = '\0';
1.55 otto 435: history(hist, &hev, H_ENTER, buf);
1.17 millert 436: }
1.25 millert 437: #endif /* !SMALL */
1.17 millert 438:
1.1 deraadt 439: makeargv();
1.17 millert 440: if (margc == 0)
1.1 deraadt 441: continue;
442: c = getcmd(margv[0]);
443: if (c == (struct cmd *)-1) {
1.30 deraadt 444: fputs("?Ambiguous command.\n", ttyout);
1.1 deraadt 445: continue;
446: }
447: if (c == 0) {
1.25 millert 448: #ifndef SMALL
1.24 millert 449: /*
450: * Give editline(3) a shot at unknown commands.
451: * XXX - bogus commands with a colon in
452: * them will not elicit an error.
453: */
1.45 markus 454: if (editing &&
1.55 otto 455: el_parse(el, margc, (const char **)margv) != 0)
1.25 millert 456: #endif /* !SMALL */
1.30 deraadt 457: fputs("?Invalid command.\n", ttyout);
1.1 deraadt 458: continue;
459: }
460: if (c->c_conn && !connected) {
1.30 deraadt 461: fputs("Not connected.\n", ttyout);
1.1 deraadt 462: continue;
463: }
1.17 millert 464: confirmrest = 0;
1.1 deraadt 465: (*c->c_handler)(margc, margv);
466: if (bell && c->c_bell)
1.30 deraadt 467: (void)putc('\007', ttyout);
1.1 deraadt 468: if (c->c_handler != help)
469: break;
470: }
1.20 millert 471: (void)signal(SIGINT, (sig_t)intr);
472: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 deraadt 473: }
474:
475: struct cmd *
476: getcmd(name)
1.17 millert 477: const char *name;
1.1 deraadt 478: {
1.17 millert 479: const char *p, *q;
1.1 deraadt 480: struct cmd *c, *found;
481: int nmatches, longest;
1.2 deraadt 482:
483: if (name == NULL)
484: return (0);
1.1 deraadt 485:
486: longest = 0;
487: nmatches = 0;
488: found = 0;
1.17 millert 489: for (c = cmdtab; (p = c->c_name) != NULL; c++) {
1.1 deraadt 490: for (q = name; *q == *p++; q++)
491: if (*q == 0) /* exact match? */
492: return (c);
493: if (!*q) { /* the name was a prefix */
494: if (q - name > longest) {
495: longest = q - name;
496: nmatches = 1;
497: found = c;
498: } else if (q - name == longest)
499: nmatches++;
500: }
501: }
502: if (nmatches > 1)
503: return ((struct cmd *)-1);
504: return (found);
505: }
506:
507: /*
508: * Slice a string up into argc/argv.
509: */
510:
511: int slrflag;
512:
513: void
514: makeargv()
515: {
1.17 millert 516: char *argp;
1.1 deraadt 517:
518: stringbase = line; /* scan from first of buffer */
519: argbase = argbuf; /* store from first of buffer */
520: slrflag = 0;
1.17 millert 521: marg_sl->sl_cur = 0; /* reset to start of marg_sl */
1.1 deraadt 522: for (margc = 0; ; margc++) {
1.17 millert 523: argp = slurpstring();
524: sl_add(marg_sl, argp);
525: if (argp == NULL)
1.1 deraadt 526: break;
527: }
1.25 millert 528: #ifndef SMALL
1.17 millert 529: if (cursor_pos == line) {
530: cursor_argc = 0;
531: cursor_argo = 0;
532: } else if (cursor_pos != NULL) {
533: cursor_argc = margc;
534: cursor_argo = strlen(margv[margc-1]);
535: }
1.25 millert 536: #endif /* !SMALL */
1.17 millert 537: }
1.1 deraadt 538:
1.25 millert 539: #ifdef SMALL
1.17 millert 540: #define INC_CHKCURSOR(x) (x)++
1.25 millert 541: #else /* !SMALL */
1.17 millert 542: #define INC_CHKCURSOR(x) { (x)++ ; \
543: if (x == cursor_pos) { \
544: cursor_argc = margc; \
545: cursor_argo = ap-argbase; \
546: cursor_pos = NULL; \
547: } }
548:
1.25 millert 549: #endif /* !SMALL */
1.1 deraadt 550:
551: /*
552: * Parse string into argbuf;
553: * implemented with FSM to
554: * handle quoting and strings
555: */
556: char *
557: slurpstring()
558: {
559: int got_one = 0;
560: char *sb = stringbase;
561: char *ap = argbase;
562: char *tmp = argbase; /* will return this if token found */
563:
564: if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
565: switch (slrflag) { /* and $ as token for macro invoke */
566: case 0:
567: slrflag++;
1.17 millert 568: INC_CHKCURSOR(stringbase);
1.1 deraadt 569: return ((*sb == '!') ? "!" : "$");
570: /* NOTREACHED */
571: case 1:
572: slrflag++;
573: altarg = stringbase;
574: break;
575: default:
576: break;
577: }
578: }
579:
580: S0:
581: switch (*sb) {
582:
583: case '\0':
584: goto OUT;
585:
586: case ' ':
587: case '\t':
1.17 millert 588: INC_CHKCURSOR(sb);
589: goto S0;
1.1 deraadt 590:
591: default:
592: switch (slrflag) {
593: case 0:
594: slrflag++;
595: break;
596: case 1:
597: slrflag++;
598: altarg = sb;
599: break;
600: default:
601: break;
602: }
603: goto S1;
604: }
605:
606: S1:
607: switch (*sb) {
608:
609: case ' ':
610: case '\t':
611: case '\0':
612: goto OUT; /* end of token */
613:
614: case '\\':
1.17 millert 615: INC_CHKCURSOR(sb);
616: goto S2; /* slurp next character */
1.1 deraadt 617:
618: case '"':
1.17 millert 619: INC_CHKCURSOR(sb);
620: goto S3; /* slurp quoted string */
1.1 deraadt 621:
622: default:
1.17 millert 623: *ap = *sb; /* add character to token */
624: ap++;
625: INC_CHKCURSOR(sb);
1.1 deraadt 626: got_one = 1;
627: goto S1;
628: }
629:
630: S2:
631: switch (*sb) {
632:
633: case '\0':
634: goto OUT;
635:
636: default:
1.17 millert 637: *ap = *sb;
638: ap++;
639: INC_CHKCURSOR(sb);
1.1 deraadt 640: got_one = 1;
641: goto S1;
642: }
643:
644: S3:
645: switch (*sb) {
646:
647: case '\0':
648: goto OUT;
649:
650: case '"':
1.17 millert 651: INC_CHKCURSOR(sb);
652: goto S1;
1.1 deraadt 653:
654: default:
1.17 millert 655: *ap = *sb;
656: ap++;
657: INC_CHKCURSOR(sb);
1.1 deraadt 658: got_one = 1;
659: goto S3;
660: }
661:
662: OUT:
663: if (got_one)
664: *ap++ = '\0';
665: argbase = ap; /* update storage pointer */
666: stringbase = sb; /* update scan pointer */
667: if (got_one) {
668: return (tmp);
669: }
670: switch (slrflag) {
671: case 0:
672: slrflag++;
673: break;
674: case 1:
675: slrflag++;
676: altarg = (char *) 0;
677: break;
678: default:
679: break;
680: }
681: return ((char *)0);
682: }
683:
684: /*
685: * Help command.
686: * Call each command handler with argc == 0 and argv[0] == name.
687: */
688: void
689: help(argc, argv)
690: int argc;
691: char *argv[];
692: {
693: struct cmd *c;
694:
695: if (argc == 1) {
1.17 millert 696: StringList *buf;
1.1 deraadt 697:
1.17 millert 698: buf = sl_init();
1.30 deraadt 699: fprintf(ttyout, "%sommands may be abbreviated. Commands are:\n\n",
1.17 millert 700: proxy ? "Proxy c" : "C");
701: for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
702: if (c->c_name && (!proxy || c->c_proxy))
703: sl_add(buf, c->c_name);
704: list_vertical(buf);
705: sl_free(buf, 0);
1.1 deraadt 706: return;
707: }
1.17 millert 708:
1.18 millert 709: #define HELPINDENT ((int) sizeof("disconnect"))
1.17 millert 710:
1.1 deraadt 711: while (--argc > 0) {
712: char *arg;
1.17 millert 713:
1.1 deraadt 714: arg = *++argv;
715: c = getcmd(arg);
716: if (c == (struct cmd *)-1)
1.30 deraadt 717: fprintf(ttyout, "?Ambiguous help command %s\n", arg);
1.1 deraadt 718: else if (c == (struct cmd *)0)
1.30 deraadt 719: fprintf(ttyout, "?Invalid help command %s\n", arg);
1.1 deraadt 720: else
1.30 deraadt 721: fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
1.1 deraadt 722: c->c_name, c->c_help);
723: }
1.17 millert 724: }
725:
726: void
727: usage()
728: {
729: (void)fprintf(stderr,
1.56 jmc 730: "usage: %s [-46AadegimnptVv] [-P port] [-r seconds] [host [port]]\n"
731: " %s [-o output] ftp://[user:password@]host[:port]/file[/]\n"
732: " %s [-o output] http://host[:port]/file\n"
733: " %s [-o output] host:[/path/]file[/]\n",
1.17 millert 734: __progname, __progname, __progname, __progname);
735: exit(1);
1.1 deraadt 736: }