Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.19
      1 /*	$NetBSD: main.c,v 1.19 1997/03/14 01:39:39 christos 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.19 1997/03/14 01:39:39 christos 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 (fromatty) {
    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 
    215 	if (argc > 0) {
    216 		if (strchr(argv[0], ':') != NULL) {
    217 			anonftp = 1;	/* Handle "automatic" transfers. */
    218 			rval = auto_fetch(argc, argv);
    219 			if (rval >= 0)		/* -1 == connected and cd-ed */
    220 				exit(rval);
    221 		} else {
    222 			char *xargv[5];
    223 
    224 			if (setjmp(toplevel))
    225 				exit(0);
    226 			(void)signal(SIGINT, (sig_t)intr);
    227 			(void)signal(SIGPIPE, (sig_t)lostpeer);
    228 			xargv[0] = __progname;
    229 			xargv[1] = argv[0];
    230 			xargv[2] = argv[1];
    231 			xargv[3] = argv[2];
    232 			xargv[4] = NULL;
    233 			setpeer(argc+1, xargv);
    234 		}
    235 	}
    236 	top = setjmp(toplevel) == 0;
    237 	if (top) {
    238 		(void)signal(SIGINT, (sig_t)intr);
    239 		(void)signal(SIGPIPE, (sig_t)lostpeer);
    240 	}
    241 	for (;;) {
    242 		cmdscanner(top);
    243 		top = 1;
    244 	}
    245 }
    246 
    247 void
    248 intr()
    249 {
    250 
    251 	alarmtimer(0);
    252 	longjmp(toplevel, 1);
    253 }
    254 
    255 void
    256 lostpeer()
    257 {
    258 
    259 	alarmtimer(0);
    260 	if (connected) {
    261 		if (cout != NULL) {
    262 			(void)shutdown(fileno(cout), 1+1);
    263 			(void)fclose(cout);
    264 			cout = NULL;
    265 		}
    266 		if (data >= 0) {
    267 			(void)shutdown(data, 1+1);
    268 			(void)close(data);
    269 			data = -1;
    270 		}
    271 		connected = 0;
    272 	}
    273 	pswitch(1);
    274 	if (connected) {
    275 		if (cout != NULL) {
    276 			(void)shutdown(fileno(cout), 1+1);
    277 			(void)fclose(cout);
    278 			cout = NULL;
    279 		}
    280 		connected = 0;
    281 	}
    282 	proxflag = 0;
    283 	pswitch(0);
    284 }
    285 
    286 /*
    287  * Generate a prompt
    288  */
    289 char *
    290 prompt()
    291 {
    292 	return ("ftp> ");
    293 }
    294 
    295 /*
    296  * Command parser.
    297  */
    298 void
    299 cmdscanner(top)
    300 	int top;
    301 {
    302 	struct cmd *c;
    303 	int num;
    304 
    305 	if (!top
    306 #ifndef SMALL
    307 	    && !editing
    308 #endif /* !SMALL */
    309 	    )
    310 		(void)putchar('\n');
    311 	for (;;) {
    312 #ifndef SMALL
    313 		if (!editing) {
    314 #endif /* !SMALL */
    315 			if (fromatty) {
    316 				fputs(prompt(), stdout);
    317 				(void)fflush(stdout);
    318 			}
    319 			if (fgets(line, sizeof(line), stdin) == NULL)
    320 				quit(0, 0);
    321 			num = strlen(line);
    322 			if (num == 0)
    323 				break;
    324 			if (line[--num] == '\n') {
    325 				if (num == 0)
    326 					break;
    327 				line[num] = '\0';
    328 			} else if (num == sizeof(line) - 2) {
    329 				puts("sorry, input line too long.");
    330 				while ((num = getchar()) != '\n' && num != EOF)
    331 					/* void */;
    332 				break;
    333 			} /* else it was a line without a newline */
    334 #ifndef SMALL
    335 		} else {
    336 			const char *buf;
    337 			cursor_pos = NULL;
    338 
    339 			if ((buf = el_gets(el, &num)) == NULL || num == 0)
    340 				quit(0, 0);
    341 			if (line[--num] == '\n') {
    342 				if (num == 0)
    343 					break;
    344 			} else if (num >= sizeof(line)) {
    345 				puts("sorry, input line too long.");
    346 				break;
    347 			}
    348 			memcpy(line, buf, num);
    349 			line[num] = '\0';
    350 			history(hist, H_ENTER, buf);
    351 		}
    352 #endif /* !SMALL */
    353 
    354 		makeargv();
    355 		if (margc == 0)
    356 			continue;
    357 #if 0 && !defined(SMALL)	/* XXX: don't want el_parse */
    358 		/*
    359 		 * el_parse returns -1 to signal that it's not been handled
    360 		 * internally.
    361 		 */
    362 		if (el_parse(el, margc, margv) != -1)
    363 			continue;
    364 #endif /* !SMALL */
    365 		c = getcmd(margv[0]);
    366 		if (c == (struct cmd *)-1) {
    367 			puts("?Ambiguous command.");
    368 			continue;
    369 		}
    370 		if (c == 0) {
    371 			puts("?Invalid command.");
    372 			continue;
    373 		}
    374 		if (c->c_conn && !connected) {
    375 			puts("Not connected.");
    376 			continue;
    377 		}
    378 		confirmrest = 0;
    379 		(*c->c_handler)(margc, margv);
    380 		if (bell && c->c_bell)
    381 			(void)putchar('\007');
    382 		if (c->c_handler != help)
    383 			break;
    384 	}
    385 	(void)signal(SIGINT, (sig_t)intr);
    386 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    387 }
    388 
    389 struct cmd *
    390 getcmd(name)
    391 	const char *name;
    392 {
    393 	const char *p, *q;
    394 	struct cmd *c, *found;
    395 	int nmatches, longest;
    396 
    397 	if (name == NULL)
    398 		return (0);
    399 
    400 	longest = 0;
    401 	nmatches = 0;
    402 	found = 0;
    403 	for (c = cmdtab; (p = c->c_name) != NULL; c++) {
    404 		for (q = name; *q == *p++; q++)
    405 			if (*q == 0)		/* exact match? */
    406 				return (c);
    407 		if (!*q) {			/* the name was a prefix */
    408 			if (q - name > longest) {
    409 				longest = q - name;
    410 				nmatches = 1;
    411 				found = c;
    412 			} else if (q - name == longest)
    413 				nmatches++;
    414 		}
    415 	}
    416 	if (nmatches > 1)
    417 		return ((struct cmd *)-1);
    418 	return (found);
    419 }
    420 
    421 /*
    422  * Slice a string up into argc/argv.
    423  */
    424 
    425 int slrflag;
    426 
    427 void
    428 makeargv()
    429 {
    430 	char *argp;
    431 
    432 	stringbase = line;		/* scan from first of buffer */
    433 	argbase = argbuf;		/* store from first of buffer */
    434 	slrflag = 0;
    435 	marg_sl->sl_cur = 0;		/* reset to start of marg_sl */
    436 	for (margc = 0; ; margc++) {
    437 		argp = slurpstring();
    438 		sl_add(marg_sl, argp);
    439 		if (argp == NULL)
    440 			break;
    441 	}
    442 #ifndef SMALL
    443 	if (cursor_pos == line) {
    444 		cursor_argc = 0;
    445 		cursor_argo = 0;
    446 	} else if (cursor_pos != NULL) {
    447 		cursor_argc = margc;
    448 		cursor_argo = strlen(margv[margc-1]);
    449 	}
    450 #endif /* !SMALL */
    451 }
    452 
    453 #ifdef SMALL
    454 #define INC_CHKCURSOR(x)	(x)++
    455 #else  /* !SMALL */
    456 #define INC_CHKCURSOR(x)	{ (x)++ ; \
    457 				if (x == cursor_pos) { \
    458 					cursor_argc = margc; \
    459 					cursor_argo = ap-argbase; \
    460 					cursor_pos = NULL; \
    461 				} }
    462 
    463 #endif /* !SMALL */
    464 
    465 /*
    466  * Parse string into argbuf;
    467  * implemented with FSM to
    468  * handle quoting and strings
    469  */
    470 char *
    471 slurpstring()
    472 {
    473 	int got_one = 0;
    474 	char *sb = stringbase;
    475 	char *ap = argbase;
    476 	char *tmp = argbase;		/* will return this if token found */
    477 
    478 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    479 		switch (slrflag) {	/* and $ as token for macro invoke */
    480 			case 0:
    481 				slrflag++;
    482 				INC_CHKCURSOR(stringbase);
    483 				return ((*sb == '!') ? "!" : "$");
    484 				/* NOTREACHED */
    485 			case 1:
    486 				slrflag++;
    487 				altarg = stringbase;
    488 				break;
    489 			default:
    490 				break;
    491 		}
    492 	}
    493 
    494 S0:
    495 	switch (*sb) {
    496 
    497 	case '\0':
    498 		goto OUT;
    499 
    500 	case ' ':
    501 	case '\t':
    502 		INC_CHKCURSOR(sb);
    503 		goto S0;
    504 
    505 	default:
    506 		switch (slrflag) {
    507 			case 0:
    508 				slrflag++;
    509 				break;
    510 			case 1:
    511 				slrflag++;
    512 				altarg = sb;
    513 				break;
    514 			default:
    515 				break;
    516 		}
    517 		goto S1;
    518 	}
    519 
    520 S1:
    521 	switch (*sb) {
    522 
    523 	case ' ':
    524 	case '\t':
    525 	case '\0':
    526 		goto OUT;	/* end of token */
    527 
    528 	case '\\':
    529 		INC_CHKCURSOR(sb);
    530 		goto S2;	/* slurp next character */
    531 
    532 	case '"':
    533 		INC_CHKCURSOR(sb);
    534 		goto S3;	/* slurp quoted string */
    535 
    536 	default:
    537 		*ap = *sb;	/* add character to token */
    538 		ap++;
    539 		INC_CHKCURSOR(sb);
    540 		got_one = 1;
    541 		goto S1;
    542 	}
    543 
    544 S2:
    545 	switch (*sb) {
    546 
    547 	case '\0':
    548 		goto OUT;
    549 
    550 	default:
    551 		*ap = *sb;
    552 		ap++;
    553 		INC_CHKCURSOR(sb);
    554 		got_one = 1;
    555 		goto S1;
    556 	}
    557 
    558 S3:
    559 	switch (*sb) {
    560 
    561 	case '\0':
    562 		goto OUT;
    563 
    564 	case '"':
    565 		INC_CHKCURSOR(sb);
    566 		goto S1;
    567 
    568 	default:
    569 		*ap = *sb;
    570 		ap++;
    571 		INC_CHKCURSOR(sb);
    572 		got_one = 1;
    573 		goto S3;
    574 	}
    575 
    576 OUT:
    577 	if (got_one)
    578 		*ap++ = '\0';
    579 	argbase = ap;			/* update storage pointer */
    580 	stringbase = sb;		/* update scan pointer */
    581 	if (got_one) {
    582 		return (tmp);
    583 	}
    584 	switch (slrflag) {
    585 		case 0:
    586 			slrflag++;
    587 			break;
    588 		case 1:
    589 			slrflag++;
    590 			altarg = (char *) 0;
    591 			break;
    592 		default:
    593 			break;
    594 	}
    595 	return ((char *)0);
    596 }
    597 
    598 /*
    599  * Help command.
    600  * Call each command handler with argc == 0 and argv[0] == name.
    601  */
    602 void
    603 help(argc, argv)
    604 	int argc;
    605 	char *argv[];
    606 {
    607 	struct cmd *c;
    608 
    609 	if (argc == 1) {
    610 		StringList *buf;
    611 
    612 		buf = sl_init();
    613 		printf("%sommands may be abbreviated.  Commands are:\n\n",
    614 		    proxy ? "Proxy c" : "C");
    615 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
    616 			if (c->c_name && (!proxy || c->c_proxy))
    617 				sl_add(buf, c->c_name);
    618 		list_vertical(buf);
    619 		sl_free(buf, 0);
    620 		return;
    621 	}
    622 
    623 #define HELPINDENT ((int) sizeof("disconnect"))
    624 
    625 	while (--argc > 0) {
    626 		char *arg;
    627 
    628 		arg = *++argv;
    629 		c = getcmd(arg);
    630 		if (c == (struct cmd *)-1)
    631 			printf("?Ambiguous help command %s\n", arg);
    632 		else if (c == (struct cmd *)0)
    633 			printf("?Invalid help command %s\n", arg);
    634 		else
    635 			printf("%-*s\t%s\n", HELPINDENT,
    636 				c->c_name, c->c_help);
    637 	}
    638 }
    639 
    640 void
    641 usage()
    642 {
    643 	(void)fprintf(stderr,
    644 	    "usage: %s [-adeginptvV] [host [port]]\n"
    645 	    "       %s host:path[/]\n"
    646 	    "       %s ftp://host[:port]/path[/]\n"
    647 	    "       %s http://host[:port]/file\n",
    648 	    __progname, __progname, __progname, __progname);
    649 	exit(1);
    650 }
    651