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