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