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