Home | History | Annotate | Line # | Download | only in ftp
main.c revision 1.8
      1 /*
      2  * Copyright (c) 1985, 1989, 1993, 1994
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char copyright[] =
     36 "@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 10/9/94";
     43 #else
     44 static char rcsid[] = "$NetBSD: main.c,v 1.8 1995/09/08 01:06:36 tls Exp $";
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  * FTP User Program -- Command Interface.
     50  */
     51 /*#include <sys/ioctl.h>*/
     52 #include <sys/types.h>
     53 #include <sys/socket.h>
     54 
     55 #include <arpa/ftp.h>
     56 
     57 #include <ctype.h>
     58 #include <err.h>
     59 #include <netdb.h>
     60 #include <pwd.h>
     61 #include <signal.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <unistd.h>
     65 
     66 #include "ftp_var.h"
     67 
     68 int
     69 main(argc, argv)
     70 	int argc;
     71 	char *argv[];
     72 {
     73 	int ch, top;
     74 	struct passwd *pw = NULL;
     75 	char *cp, homedir[MAXPATHLEN];
     76 
     77 	sp = getservbyname("ftp", "tcp");
     78 	if (sp == 0)
     79 		errx(1, "ftp/tcp: unknown service");
     80 	doglob = 1;
     81 	interactive = 1;
     82 	autologin = 1;
     83 
     84 	while ((ch = getopt(argc, argv, "dgintv")) != EOF) {
     85 		switch (ch) {
     86 		case 'd':
     87 			options |= SO_DEBUG;
     88 			debug++;
     89 			break;
     90 
     91 		case 'g':
     92 			doglob = 0;
     93 			break;
     94 
     95 		case 'i':
     96 			interactive = 0;
     97 			break;
     98 
     99 		case 'n':
    100 			autologin = 0;
    101 			break;
    102 
    103 		case 't':
    104 			trace++;
    105 			break;
    106 
    107 		case 'v':
    108 			verbose++;
    109 			break;
    110 
    111 		default:
    112 			(void)fprintf(stderr,
    113 				"usage: ftp [-dgintv] [host [port]]\n");
    114 			exit(1);
    115 		}
    116 	}
    117 	argc -= optind;
    118 	argv += optind;
    119 
    120 	fromatty = isatty(fileno(stdin));
    121 	if (fromatty)
    122 		verbose++;
    123 	cpend = 0;	/* no pending replies */
    124 	proxy = 0;	/* proxy not active */
    125 	passivemode = 0; /* passive mode not active */
    126 	crflag = 1;	/* strip c.r. on ascii gets */
    127 	sendport = -1;	/* not using ports */
    128 	/*
    129 	 * Set up the home directory in case we're globbing.
    130 	 */
    131 	cp = getlogin();
    132 	if (cp != NULL) {
    133 		pw = getpwnam(cp);
    134 	}
    135 	if (pw == NULL)
    136 		pw = getpwuid(getuid());
    137 	if (pw != NULL) {
    138 		home = homedir;
    139 		(void) strcpy(home, pw->pw_dir);
    140 	}
    141 	if (argc > 0) {
    142 		char *xargv[5];
    143 		extern char *__progname;
    144 
    145 		if (setjmp(toplevel))
    146 			exit(0);
    147 		(void) signal(SIGINT, intr);
    148 		(void) signal(SIGPIPE, lostpeer);
    149 		xargv[0] = __progname;
    150 		xargv[1] = argv[0];
    151 		xargv[2] = argv[1];
    152 		xargv[3] = argv[2];
    153 		xargv[4] = NULL;
    154 		setpeer(argc+1, xargv);
    155 	}
    156 	top = setjmp(toplevel) == 0;
    157 	if (top) {
    158 		(void) signal(SIGINT, intr);
    159 		(void) signal(SIGPIPE, lostpeer);
    160 	}
    161 	for (;;) {
    162 		cmdscanner(top);
    163 		top = 1;
    164 	}
    165 }
    166 
    167 void
    168 intr()
    169 {
    170 
    171 	longjmp(toplevel, 1);
    172 }
    173 
    174 void
    175 lostpeer()
    176 {
    177 
    178 	if (connected) {
    179 		if (cout != NULL) {
    180 			(void) shutdown(fileno(cout), 1+1);
    181 			(void) fclose(cout);
    182 			cout = NULL;
    183 		}
    184 		if (data >= 0) {
    185 			(void) shutdown(data, 1+1);
    186 			(void) close(data);
    187 			data = -1;
    188 		}
    189 		connected = 0;
    190 	}
    191 	pswitch(1);
    192 	if (connected) {
    193 		if (cout != NULL) {
    194 			(void) shutdown(fileno(cout), 1+1);
    195 			(void) fclose(cout);
    196 			cout = NULL;
    197 		}
    198 		connected = 0;
    199 	}
    200 	proxflag = 0;
    201 	pswitch(0);
    202 }
    203 
    204 /*
    205 char *
    206 tail(filename)
    207 	char *filename;
    208 {
    209 	char *s;
    210 
    211 	while (*filename) {
    212 		s = strrchr(filename, '/');
    213 		if (s == NULL)
    214 			break;
    215 		if (s[1])
    216 			return (s + 1);
    217 		*s = '\0';
    218 	}
    219 	return (filename);
    220 }
    221 */
    222 
    223 /*
    224  * Command parser.
    225  */
    226 void
    227 cmdscanner(top)
    228 	int top;
    229 {
    230 	struct cmd *c;
    231 	int l;
    232 
    233 	if (!top)
    234 		(void) putchar('\n');
    235 	for (;;) {
    236 		if (fromatty) {
    237 			printf("ftp> ");
    238 			(void) fflush(stdout);
    239 		}
    240 		if (fgets(line, sizeof line, stdin) == NULL)
    241 			quit(0, 0);
    242 		l = strlen(line);
    243 		if (l == 0)
    244 			break;
    245 		if (line[--l] == '\n') {
    246 			if (l == 0)
    247 				break;
    248 			line[l] = '\0';
    249 		} else if (l == sizeof(line) - 2) {
    250 			printf("sorry, input line too long\n");
    251 			while ((l = getchar()) != '\n' && l != EOF)
    252 				/* void */;
    253 			break;
    254 		} /* else it was a line without a newline */
    255 		makeargv();
    256 		if (margc == 0) {
    257 			continue;
    258 		}
    259 		c = getcmd(margv[0]);
    260 		if (c == (struct cmd *)-1) {
    261 			printf("?Ambiguous command\n");
    262 			continue;
    263 		}
    264 		if (c == 0) {
    265 			printf("?Invalid command\n");
    266 			continue;
    267 		}
    268 		if (c->c_conn && !connected) {
    269 			printf("Not connected.\n");
    270 			continue;
    271 		}
    272 		(*c->c_handler)(margc, margv);
    273 		if (bell && c->c_bell)
    274 			(void) putchar('\007');
    275 		if (c->c_handler != help)
    276 			break;
    277 	}
    278 	(void) signal(SIGINT, intr);
    279 	(void) signal(SIGPIPE, lostpeer);
    280 }
    281 
    282 struct cmd *
    283 getcmd(name)
    284 	char *name;
    285 {
    286 	char *p, *q;
    287 	struct cmd *c, *found;
    288 	int nmatches, longest;
    289 
    290 	longest = 0;
    291 	nmatches = 0;
    292 	found = 0;
    293 	for (c = cmdtab; p = c->c_name; c++) {
    294 		for (q = name; *q == *p++; q++)
    295 			if (*q == 0)		/* exact match? */
    296 				return (c);
    297 		if (!*q) {			/* the name was a prefix */
    298 			if (q - name > longest) {
    299 				longest = q - name;
    300 				nmatches = 1;
    301 				found = c;
    302 			} else if (q - name == longest)
    303 				nmatches++;
    304 		}
    305 	}
    306 	if (nmatches > 1)
    307 		return ((struct cmd *)-1);
    308 	return (found);
    309 }
    310 
    311 /*
    312  * Slice a string up into argc/argv.
    313  */
    314 
    315 int slrflag;
    316 
    317 void
    318 makeargv()
    319 {
    320 	char **argp;
    321 
    322 	margc = 0;
    323 	argp = margv;
    324 	stringbase = line;		/* scan from first of buffer */
    325 	argbase = argbuf;		/* store from first of buffer */
    326 	slrflag = 0;
    327 	while (*argp++ = slurpstring())
    328 		margc++;
    329 }
    330 
    331 /*
    332  * Parse string into argbuf;
    333  * implemented with FSM to
    334  * handle quoting and strings
    335  */
    336 char *
    337 slurpstring()
    338 {
    339 	int got_one = 0;
    340 	char *sb = stringbase;
    341 	char *ap = argbase;
    342 	char *tmp = argbase;		/* will return this if token found */
    343 
    344 	if (*sb == '!' || *sb == '$') {	/* recognize ! as a token for shell */
    345 		switch (slrflag) {	/* and $ as token for macro invoke */
    346 			case 0:
    347 				slrflag++;
    348 				stringbase++;
    349 				return ((*sb == '!') ? "!" : "$");
    350 				/* NOTREACHED */
    351 			case 1:
    352 				slrflag++;
    353 				altarg = stringbase;
    354 				break;
    355 			default:
    356 				break;
    357 		}
    358 	}
    359 
    360 S0:
    361 	switch (*sb) {
    362 
    363 	case '\0':
    364 		goto OUT;
    365 
    366 	case ' ':
    367 	case '\t':
    368 		sb++; goto S0;
    369 
    370 	default:
    371 		switch (slrflag) {
    372 			case 0:
    373 				slrflag++;
    374 				break;
    375 			case 1:
    376 				slrflag++;
    377 				altarg = sb;
    378 				break;
    379 			default:
    380 				break;
    381 		}
    382 		goto S1;
    383 	}
    384 
    385 S1:
    386 	switch (*sb) {
    387 
    388 	case ' ':
    389 	case '\t':
    390 	case '\0':
    391 		goto OUT;	/* end of token */
    392 
    393 	case '\\':
    394 		sb++; goto S2;	/* slurp next character */
    395 
    396 	case '"':
    397 		sb++; goto S3;	/* slurp quoted string */
    398 
    399 	default:
    400 		*ap++ = *sb++;	/* add character to token */
    401 		got_one = 1;
    402 		goto S1;
    403 	}
    404 
    405 S2:
    406 	switch (*sb) {
    407 
    408 	case '\0':
    409 		goto OUT;
    410 
    411 	default:
    412 		*ap++ = *sb++;
    413 		got_one = 1;
    414 		goto S1;
    415 	}
    416 
    417 S3:
    418 	switch (*sb) {
    419 
    420 	case '\0':
    421 		goto OUT;
    422 
    423 	case '"':
    424 		sb++; goto S1;
    425 
    426 	default:
    427 		*ap++ = *sb++;
    428 		got_one = 1;
    429 		goto S3;
    430 	}
    431 
    432 OUT:
    433 	if (got_one)
    434 		*ap++ = '\0';
    435 	argbase = ap;			/* update storage pointer */
    436 	stringbase = sb;		/* update scan pointer */
    437 	if (got_one) {
    438 		return (tmp);
    439 	}
    440 	switch (slrflag) {
    441 		case 0:
    442 			slrflag++;
    443 			break;
    444 		case 1:
    445 			slrflag++;
    446 			altarg = (char *) 0;
    447 			break;
    448 		default:
    449 			break;
    450 	}
    451 	return ((char *)0);
    452 }
    453 
    454 #define HELPINDENT ((int) sizeof ("directory"))
    455 
    456 /*
    457  * Help command.
    458  * Call each command handler with argc == 0 and argv[0] == name.
    459  */
    460 void
    461 help(argc, argv)
    462 	int argc;
    463 	char *argv[];
    464 {
    465 	struct cmd *c;
    466 
    467 	if (argc == 1) {
    468 		int i, j, w, k;
    469 		int columns, width = 0, lines;
    470 
    471 		printf("Commands may be abbreviated.  Commands are:\n\n");
    472 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
    473 			int len = strlen(c->c_name);
    474 
    475 			if (len > width)
    476 				width = len;
    477 		}
    478 		width = (width + 8) &~ 7;
    479 		columns = 80 / width;
    480 		if (columns == 0)
    481 			columns = 1;
    482 		lines = (NCMDS + columns - 1) / columns;
    483 		for (i = 0; i < lines; i++) {
    484 			for (j = 0; j < columns; j++) {
    485 				c = cmdtab + j * lines + i;
    486 				if (c->c_name && (!proxy || c->c_proxy)) {
    487 					printf("%s", c->c_name);
    488 				}
    489 				else if (c->c_name) {
    490 					for (k=0; k < strlen(c->c_name); k++) {
    491 						(void) putchar(' ');
    492 					}
    493 				}
    494 				if (c + lines >= &cmdtab[NCMDS]) {
    495 					printf("\n");
    496 					break;
    497 				}
    498 				w = strlen(c->c_name);
    499 				while (w < width) {
    500 					w = (w + 8) &~ 7;
    501 					(void) putchar('\t');
    502 				}
    503 			}
    504 		}
    505 		return;
    506 	}
    507 	while (--argc > 0) {
    508 		char *arg;
    509 		arg = *++argv;
    510 		c = getcmd(arg);
    511 		if (c == (struct cmd *)-1)
    512 			printf("?Ambiguous help command %s\n", arg);
    513 		else if (c == (struct cmd *)0)
    514 			printf("?Invalid help command %s\n", arg);
    515 		else
    516 			printf("%-*s\t%s\n", HELPINDENT,
    517 				c->c_name, c->c_help);
    518 	}
    519 }
    520