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