main.c revision 1.20 1 /* $NetBSD: main.c,v 1.20 1997/03/16 14:24:21 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1985, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
45 #else
46 static char rcsid[] = "$NetBSD: main.c,v 1.20 1997/03/16 14:24:21 lukem Exp $";
47 #endif
48 #endif /* not lint */
49
50 /*
51 * FTP User Program -- Command Interface.
52 */
53 #include <sys/types.h>
54 #include <sys/socket.h>
55
56 #include <err.h>
57 #include <netdb.h>
58 #include <pwd.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "ftp_var.h"
64
65 int
66 main(argc, argv)
67 int argc;
68 char *argv[];
69 {
70 struct servent *sp;
71 int ch, top, port, rval;
72 struct passwd *pw = NULL;
73 char *cp, homedir[MAXPATHLEN];
74
75 sp = getservbyname("ftp", "tcp");
76 if (sp == 0)
77 ftpport = htons(FTP_PORT); /* good fallback */
78 else
79 ftpport = sp->s_port;
80 sp = getservbyname("http", "tcp");
81 if (sp == 0)
82 httpport = htons(HTTP_PORT); /* good fallback */
83 else
84 httpport = sp->s_port;
85 doglob = 1;
86 interactive = 1;
87 autologin = 1;
88 passivemode = 0;
89 preserve = 1;
90 verbose = 0;
91 progress = 0;
92 #ifndef SMALL
93 editing = 0;
94 #endif
95 mark = HASHBYTES;
96 marg_sl = sl_init();
97
98 cp = strrchr(argv[0], '/');
99 cp = (cp == NULL) ? argv[0] : cp + 1;
100 if (strcmp(cp, "pftp") == 0)
101 passivemode = 1;
102
103 fromatty = isatty(fileno(stdin));
104 if (fromatty) {
105 verbose = 1; /* verbose if from a tty */
106 #ifndef SMALL
107 editing = 1; /* editing mode on if from a tty */
108 #endif
109 }
110 if (isatty(fileno(stdout)))
111 progress = 1; /* progress bar on if going to a tty */
112
113 while ((ch = getopt(argc, argv, "adeginpP:tvV")) != -1) {
114 switch (ch) {
115 case 'a':
116 anonftp = 1;
117 break;
118
119 case 'd':
120 options |= SO_DEBUG;
121 debug++;
122 break;
123
124 case 'e':
125 #ifndef SMALL
126 editing = 0;
127 #endif
128 break;
129
130 case 'g':
131 doglob = 0;
132 break;
133
134 case 'i':
135 interactive = 0;
136 break;
137
138 case 'n':
139 autologin = 0;
140 break;
141
142 case 'p':
143 passivemode = 1;
144 break;
145
146 case 'P':
147 port = atoi(optarg);
148 if (port <= 0)
149 warnx("bad port number: %s (ignored)", optarg);
150 else
151 ftpport = htons(port);
152 break;
153
154 case 't':
155 trace = 1;
156 break;
157
158 case 'v':
159 verbose = 1;
160 break;
161
162 case 'V':
163 verbose = 0;
164 break;
165
166 default:
167 usage();
168 }
169 }
170 argc -= optind;
171 argv += optind;
172
173 cpend = 0; /* no pending replies */
174 proxy = 0; /* proxy not active */
175 crflag = 1; /* strip c.r. on ascii gets */
176 sendport = -1; /* not using ports */
177 /*
178 * Set up the home directory in case we're globbing.
179 */
180 cp = getlogin();
181 if (cp != NULL) {
182 pw = getpwnam(cp);
183 }
184 if (pw == NULL)
185 pw = getpwuid(getuid());
186 if (pw != NULL) {
187 home = homedir;
188 (void)strcpy(home, pw->pw_dir);
189 }
190
191 #ifndef SMALL
192 if (editing) {
193 el = el_init(__progname, stdin, stdout); /* init editline */
194
195 hist = history_init(); /* init the builtin history */
196 history(hist, H_EVENT, 100); /* remember 100 events */
197 el_set(el, EL_HIST, history, hist); /* use history */
198
199 el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
200 el_set(el, EL_PROMPT, prompt); /* set the prompt function */
201
202 /* add local file completion, bind to TAB */
203 el_set(el, EL_ADDFN, "ftp-complete",
204 "Context sensitive argument completion",
205 complete);
206 el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
207
208 el_source(el, NULL); /* read ~/.editrc */
209 }
210 #endif /* !SMALL */
211
212 setttywidth(0);
213 (void)signal(SIGWINCH, setttywidth);
214 #ifndef SMALL
215 if (editing)
216 el_set(el, EL_SIGNAL, 1);
217 #endif /* !SMALL */
218
219 if (argc > 0) {
220 if (strchr(argv[0], ':') != NULL) {
221 anonftp = 1; /* Handle "automatic" transfers. */
222 rval = auto_fetch(argc, argv);
223 if (rval >= 0) /* -1 == connected and cd-ed */
224 exit(rval);
225 } else {
226 char *xargv[5];
227
228 if (setjmp(toplevel))
229 exit(0);
230 (void)signal(SIGINT, (sig_t)intr);
231 (void)signal(SIGPIPE, (sig_t)lostpeer);
232 xargv[0] = __progname;
233 xargv[1] = argv[0];
234 xargv[2] = argv[1];
235 xargv[3] = argv[2];
236 xargv[4] = NULL;
237 setpeer(argc+1, xargv);
238 }
239 }
240 top = setjmp(toplevel) == 0;
241 if (top) {
242 (void)signal(SIGINT, (sig_t)intr);
243 (void)signal(SIGPIPE, (sig_t)lostpeer);
244 }
245 for (;;) {
246 cmdscanner(top);
247 top = 1;
248 }
249 }
250
251 void
252 intr()
253 {
254
255 alarmtimer(0);
256 longjmp(toplevel, 1);
257 }
258
259 void
260 lostpeer()
261 {
262
263 alarmtimer(0);
264 if (connected) {
265 if (cout != NULL) {
266 (void)shutdown(fileno(cout), 1+1);
267 (void)fclose(cout);
268 cout = NULL;
269 }
270 if (data >= 0) {
271 (void)shutdown(data, 1+1);
272 (void)close(data);
273 data = -1;
274 }
275 connected = 0;
276 }
277 pswitch(1);
278 if (connected) {
279 if (cout != NULL) {
280 (void)shutdown(fileno(cout), 1+1);
281 (void)fclose(cout);
282 cout = NULL;
283 }
284 connected = 0;
285 }
286 proxflag = 0;
287 pswitch(0);
288 }
289
290 /*
291 * Generate a prompt
292 */
293 char *
294 prompt()
295 {
296 return ("ftp> ");
297 }
298
299 /*
300 * Command parser.
301 */
302 void
303 cmdscanner(top)
304 int top;
305 {
306 struct cmd *c;
307 int num;
308
309 if (!top
310 #ifndef SMALL
311 && !editing
312 #endif /* !SMALL */
313 )
314 (void)putchar('\n');
315 for (;;) {
316 #ifndef SMALL
317 if (!editing) {
318 #endif /* !SMALL */
319 if (fromatty) {
320 fputs(prompt(), stdout);
321 (void)fflush(stdout);
322 }
323 if (fgets(line, sizeof(line), stdin) == NULL)
324 quit(0, 0);
325 num = strlen(line);
326 if (num == 0)
327 break;
328 if (line[--num] == '\n') {
329 if (num == 0)
330 break;
331 line[num] = '\0';
332 } else if (num == sizeof(line) - 2) {
333 puts("sorry, input line too long.");
334 while ((num = getchar()) != '\n' && num != EOF)
335 /* void */;
336 break;
337 } /* else it was a line without a newline */
338 #ifndef SMALL
339 } else {
340 const char *buf;
341 cursor_pos = NULL;
342
343 if ((buf = el_gets(el, &num)) == NULL || num == 0)
344 quit(0, 0);
345 if (line[--num] == '\n') {
346 if (num == 0)
347 break;
348 } else if (num >= sizeof(line)) {
349 puts("sorry, input line too long.");
350 break;
351 }
352 memcpy(line, buf, num);
353 line[num] = '\0';
354 history(hist, H_ENTER, buf);
355 }
356 #endif /* !SMALL */
357
358 makeargv();
359 if (margc == 0)
360 continue;
361 #if 0 && !defined(SMALL) /* XXX: don't want el_parse */
362 /*
363 * el_parse returns -1 to signal that it's not been handled
364 * internally.
365 */
366 if (el_parse(el, margc, margv) != -1)
367 continue;
368 #endif /* !SMALL */
369 c = getcmd(margv[0]);
370 if (c == (struct cmd *)-1) {
371 puts("?Ambiguous command.");
372 continue;
373 }
374 if (c == 0) {
375 puts("?Invalid command.");
376 continue;
377 }
378 if (c->c_conn && !connected) {
379 puts("Not connected.");
380 continue;
381 }
382 confirmrest = 0;
383 (*c->c_handler)(margc, margv);
384 if (bell && c->c_bell)
385 (void)putchar('\007');
386 if (c->c_handler != help)
387 break;
388 }
389 (void)signal(SIGINT, (sig_t)intr);
390 (void)signal(SIGPIPE, (sig_t)lostpeer);
391 }
392
393 struct cmd *
394 getcmd(name)
395 const char *name;
396 {
397 const char *p, *q;
398 struct cmd *c, *found;
399 int nmatches, longest;
400
401 if (name == NULL)
402 return (0);
403
404 longest = 0;
405 nmatches = 0;
406 found = 0;
407 for (c = cmdtab; (p = c->c_name) != NULL; c++) {
408 for (q = name; *q == *p++; q++)
409 if (*q == 0) /* exact match? */
410 return (c);
411 if (!*q) { /* the name was a prefix */
412 if (q - name > longest) {
413 longest = q - name;
414 nmatches = 1;
415 found = c;
416 } else if (q - name == longest)
417 nmatches++;
418 }
419 }
420 if (nmatches > 1)
421 return ((struct cmd *)-1);
422 return (found);
423 }
424
425 /*
426 * Slice a string up into argc/argv.
427 */
428
429 int slrflag;
430
431 void
432 makeargv()
433 {
434 char *argp;
435
436 stringbase = line; /* scan from first of buffer */
437 argbase = argbuf; /* store from first of buffer */
438 slrflag = 0;
439 marg_sl->sl_cur = 0; /* reset to start of marg_sl */
440 for (margc = 0; ; margc++) {
441 argp = slurpstring();
442 sl_add(marg_sl, argp);
443 if (argp == NULL)
444 break;
445 }
446 #ifndef SMALL
447 if (cursor_pos == line) {
448 cursor_argc = 0;
449 cursor_argo = 0;
450 } else if (cursor_pos != NULL) {
451 cursor_argc = margc;
452 cursor_argo = strlen(margv[margc-1]);
453 }
454 #endif /* !SMALL */
455 }
456
457 #ifdef SMALL
458 #define INC_CHKCURSOR(x) (x)++
459 #else /* !SMALL */
460 #define INC_CHKCURSOR(x) { (x)++ ; \
461 if (x == cursor_pos) { \
462 cursor_argc = margc; \
463 cursor_argo = ap-argbase; \
464 cursor_pos = NULL; \
465 } }
466
467 #endif /* !SMALL */
468
469 /*
470 * Parse string into argbuf;
471 * implemented with FSM to
472 * handle quoting and strings
473 */
474 char *
475 slurpstring()
476 {
477 int got_one = 0;
478 char *sb = stringbase;
479 char *ap = argbase;
480 char *tmp = argbase; /* will return this if token found */
481
482 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
483 switch (slrflag) { /* and $ as token for macro invoke */
484 case 0:
485 slrflag++;
486 INC_CHKCURSOR(stringbase);
487 return ((*sb == '!') ? "!" : "$");
488 /* NOTREACHED */
489 case 1:
490 slrflag++;
491 altarg = stringbase;
492 break;
493 default:
494 break;
495 }
496 }
497
498 S0:
499 switch (*sb) {
500
501 case '\0':
502 goto OUT;
503
504 case ' ':
505 case '\t':
506 INC_CHKCURSOR(sb);
507 goto S0;
508
509 default:
510 switch (slrflag) {
511 case 0:
512 slrflag++;
513 break;
514 case 1:
515 slrflag++;
516 altarg = sb;
517 break;
518 default:
519 break;
520 }
521 goto S1;
522 }
523
524 S1:
525 switch (*sb) {
526
527 case ' ':
528 case '\t':
529 case '\0':
530 goto OUT; /* end of token */
531
532 case '\\':
533 INC_CHKCURSOR(sb);
534 goto S2; /* slurp next character */
535
536 case '"':
537 INC_CHKCURSOR(sb);
538 goto S3; /* slurp quoted string */
539
540 default:
541 *ap = *sb; /* add character to token */
542 ap++;
543 INC_CHKCURSOR(sb);
544 got_one = 1;
545 goto S1;
546 }
547
548 S2:
549 switch (*sb) {
550
551 case '\0':
552 goto OUT;
553
554 default:
555 *ap = *sb;
556 ap++;
557 INC_CHKCURSOR(sb);
558 got_one = 1;
559 goto S1;
560 }
561
562 S3:
563 switch (*sb) {
564
565 case '\0':
566 goto OUT;
567
568 case '"':
569 INC_CHKCURSOR(sb);
570 goto S1;
571
572 default:
573 *ap = *sb;
574 ap++;
575 INC_CHKCURSOR(sb);
576 got_one = 1;
577 goto S3;
578 }
579
580 OUT:
581 if (got_one)
582 *ap++ = '\0';
583 argbase = ap; /* update storage pointer */
584 stringbase = sb; /* update scan pointer */
585 if (got_one) {
586 return (tmp);
587 }
588 switch (slrflag) {
589 case 0:
590 slrflag++;
591 break;
592 case 1:
593 slrflag++;
594 altarg = (char *) 0;
595 break;
596 default:
597 break;
598 }
599 return ((char *)0);
600 }
601
602 /*
603 * Help command.
604 * Call each command handler with argc == 0 and argv[0] == name.
605 */
606 void
607 help(argc, argv)
608 int argc;
609 char *argv[];
610 {
611 struct cmd *c;
612
613 if (argc == 1) {
614 StringList *buf;
615
616 buf = sl_init();
617 printf("%sommands may be abbreviated. Commands are:\n\n",
618 proxy ? "Proxy c" : "C");
619 for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
620 if (c->c_name && (!proxy || c->c_proxy))
621 sl_add(buf, c->c_name);
622 list_vertical(buf);
623 sl_free(buf, 0);
624 return;
625 }
626
627 #define HELPINDENT ((int) sizeof("disconnect"))
628
629 while (--argc > 0) {
630 char *arg;
631
632 arg = *++argv;
633 c = getcmd(arg);
634 if (c == (struct cmd *)-1)
635 printf("?Ambiguous help command %s\n", arg);
636 else if (c == (struct cmd *)0)
637 printf("?Invalid help command %s\n", arg);
638 else
639 printf("%-*s\t%s\n", HELPINDENT,
640 c->c_name, c->c_help);
641 }
642 }
643
644 void
645 usage()
646 {
647 (void)fprintf(stderr,
648 "usage: %s [-adeginptvV] [host [port]]\n"
649 " %s host:path[/]\n"
650 " %s ftp://host[:port]/path[/]\n"
651 " %s http://host[:port]/file\n",
652 __progname, __progname, __progname, __progname);
653 exit(1);
654 }
655