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