xref: /csrg-svn/usr.bin/ftp/main.c (revision 11654)
110297Ssam #ifndef lint
2*11654Ssam static char sccsid[] = "@(#)main.c	4.6 (Berkeley) 03/23/83";
310297Ssam #endif
410297Ssam 
510297Ssam /*
610297Ssam  * FTP User Program -- Command Interface.
710297Ssam  */
811352Ssam #include <sys/param.h>
910297Ssam #include <sys/socket.h>
1010297Ssam #include <sys/ioctl.h>
1110297Ssam 
1210297Ssam #include <signal.h>
1310297Ssam #include <stdio.h>
1410297Ssam #include <errno.h>
1510297Ssam #include <ctype.h>
1610297Ssam #include <netdb.h>
1711352Ssam #include <pwd.h>
1810297Ssam 
1910297Ssam #include "ftp.h"
2010297Ssam #include "ftp_var.h"
2110297Ssam 
2210297Ssam int	intr();
2310297Ssam int	lostpeer();
2411352Ssam extern	char *home;
2510297Ssam 
2610297Ssam main(argc, argv)
2710297Ssam 	char *argv[];
2810297Ssam {
2910297Ssam 	register char *cp;
3010297Ssam 	int top;
3111352Ssam 	struct passwd *pw;
3211352Ssam 	char homedir[MAXPATHLEN];
3310297Ssam 
3410297Ssam 	sp = getservbyname("ftp", "tcp");
3510297Ssam 	if (sp == 0) {
3610297Ssam 		fprintf(stderr, "ftp: ftp/tcp: unknown service\n");
3710297Ssam 		exit(1);
3810297Ssam 	}
3911351Ssam 	doglob = 1;
40*11654Ssam 	interactive = 1;
4111351Ssam 	autologin = 1;
4210297Ssam 	argc--, argv++;
4310297Ssam 	while (argc > 0 && **argv == '-') {
4410297Ssam 		for (cp = *argv + 1; *cp; cp++)
4510297Ssam 			switch (*cp) {
4610297Ssam 
4710297Ssam 			case 'd':
4810297Ssam 				options |= SO_DEBUG;
4910297Ssam 				debug++;
5010297Ssam 				break;
5110297Ssam 
5210297Ssam 			case 'v':
5310297Ssam 				verbose++;
5410297Ssam 				break;
5510297Ssam 
5610297Ssam 			case 't':
5710297Ssam 				trace++;
5810297Ssam 				break;
5910297Ssam 
6010297Ssam 			case 'i':
61*11654Ssam 				interactive = 0;
6210297Ssam 				break;
6310297Ssam 
6410297Ssam 			case 'n':
6510297Ssam 				autologin = 0;
6610297Ssam 				break;
6710297Ssam 
6811351Ssam 			case 'g':
6911351Ssam 				doglob = 0;
7011351Ssam 				break;
7111351Ssam 
7210297Ssam 			default:
7310297Ssam 				fprintf(stderr,
7410297Ssam 				  "ftp: %c: unknown option\n", *cp);
7510297Ssam 				exit(1);
7610297Ssam 			}
7710297Ssam 		argc--, argv++;
7810297Ssam 	}
7910297Ssam 	fromatty = isatty(fileno(stdin));
8010297Ssam 	/*
8110297Ssam 	 * Set up defaults for FTP.
8210297Ssam 	 */
8310297Ssam 	strcpy(typename, "ascii"), type = TYPE_A;
8410297Ssam 	strcpy(formname, "non-print"), form = FORM_N;
8510297Ssam 	strcpy(modename, "stream"), mode = MODE_S;
8610297Ssam 	strcpy(structname, "file"), stru = STRU_F;
8711227Ssam 	strcpy(bytename, "8"), bytesize = 8;
8810297Ssam 	if (fromatty)
8910297Ssam 		verbose++;
9011352Ssam 	/*
9111352Ssam 	 * Set up the home directory in case we're globbing.
9211352Ssam 	 */
9311352Ssam 	pw = getpwnam(getlogin());
9411352Ssam 	if (pw == NULL)
9511352Ssam 		pw = getpwuid(getuid());
9611352Ssam 	if (pw != NULL) {
9711352Ssam 		home = homedir;
9811352Ssam 		strcpy(home, pw->pw_dir);
9911352Ssam 	}
10010297Ssam 	if (argc > 0) {
10110297Ssam 		if (setjmp(toplevel))
10210297Ssam 			exit(0);
10310297Ssam 		sigset(SIGINT, intr);
10410297Ssam 		sigset(SIGPIPE, lostpeer);
10510297Ssam 		setpeer(argc + 1, argv - 1);
10610297Ssam 	}
10710297Ssam 	top = setjmp(toplevel) == 0;
10810297Ssam 	if (top) {
10910297Ssam 		sigset(SIGINT, intr);
11010297Ssam 		sigset(SIGPIPE, lostpeer);
11110297Ssam 	}
11210297Ssam 	for (;;) {
11310297Ssam 		cmdscanner(top);
11410297Ssam 		top = 1;
11510297Ssam 	}
11610297Ssam }
11710297Ssam 
11810297Ssam intr()
11910297Ssam {
12010297Ssam 
12110297Ssam 	longjmp(toplevel, 1);
12210297Ssam }
12310297Ssam 
12410297Ssam lostpeer()
12510297Ssam {
12610297Ssam 	extern FILE *cout;
12710297Ssam 	extern int data;
12810297Ssam 
12910297Ssam 	if (connected) {
13010297Ssam 		if (cout != NULL) {
13111239Ssam 			shutdown(fileno(cout), 1+1);
13210297Ssam 			fclose(cout);
13310297Ssam 			cout = NULL;
13410297Ssam 		}
13510297Ssam 		if (data >= 0) {
13611239Ssam 			shutdown(data, 1+1);
13710297Ssam 			(void) close(data);
13810297Ssam 			data = -1;
13910297Ssam 		}
14010297Ssam 		connected = 0;
14110297Ssam 	}
14210297Ssam 	longjmp(toplevel, 1);
14310297Ssam }
14410297Ssam 
14510297Ssam char *
14610297Ssam tail(filename)
14710297Ssam 	char *filename;
14810297Ssam {
14910297Ssam 	register char *s;
15010297Ssam 
15110297Ssam 	while (*filename) {
15210297Ssam 		s = rindex(filename, '/');
15310297Ssam 		if (s == NULL)
15410297Ssam 			break;
15510297Ssam 		if (s[1])
15610297Ssam 			return (s + 1);
15710297Ssam 		*s = '\0';
15810297Ssam 	}
15910297Ssam 	return (filename);
16010297Ssam }
16110297Ssam 
16210297Ssam /*
16310297Ssam  * Command parser.
16410297Ssam  */
16510297Ssam cmdscanner(top)
16610297Ssam 	int top;
16710297Ssam {
16810297Ssam 	register struct cmd *c;
16910297Ssam 	struct cmd *getcmd();
17010297Ssam 	extern struct cmd cmdtab[];
17110297Ssam 	extern int help();
17210297Ssam 
17310297Ssam 	if (!top)
17410297Ssam 		putchar('\n');
17510297Ssam 	for (;;) {
17610297Ssam 		if (fromatty) {
17710297Ssam 			printf("ftp> ");
17810297Ssam 			fflush(stdout);
17910297Ssam 		}
18010297Ssam 		if (gets(line) == 0)
18110297Ssam 			break;
18210297Ssam 		if (line[0] == 0)
18310297Ssam 			break;
18410297Ssam 		makeargv();
18510297Ssam 		c = getcmd(margv[0]);
18610297Ssam 		if (c == (struct cmd *)-1) {
18710297Ssam 			printf("?Ambiguous command\n");
18810297Ssam 			continue;
18910297Ssam 		}
19010297Ssam 		if (c == 0) {
19110297Ssam 			printf("?Invalid command\n");
19210297Ssam 			continue;
19310297Ssam 		}
194*11654Ssam 		if (c->c_conn && !connected) {
195*11654Ssam 			printf ("Not connected.\n");
196*11654Ssam 			continue;
197*11654Ssam 		}
19810297Ssam 		(*c->c_handler)(margc, margv);
19910297Ssam 		if (bell && c->c_bell)
20010297Ssam 			putchar(CTRL(g));
20110297Ssam 		if (c->c_handler != help)
20210297Ssam 			break;
20310297Ssam 	}
20410297Ssam 	longjmp(toplevel, 0);
20510297Ssam }
20610297Ssam 
20710297Ssam struct cmd *
20810297Ssam getcmd(name)
20910297Ssam 	register char *name;
21010297Ssam {
21110297Ssam 	register char *p, *q;
21210297Ssam 	register struct cmd *c, *found;
21310297Ssam 	register int nmatches, longest;
21410297Ssam 
21510297Ssam 	longest = 0;
21610297Ssam 	nmatches = 0;
21710297Ssam 	found = 0;
21810297Ssam 	for (c = cmdtab; p = c->c_name; c++) {
21910297Ssam 		for (q = name; *q == *p++; q++)
22010297Ssam 			if (*q == 0)		/* exact match? */
22110297Ssam 				return (c);
22210297Ssam 		if (!*q) {			/* the name was a prefix */
22310297Ssam 			if (q - name > longest) {
22410297Ssam 				longest = q - name;
22510297Ssam 				nmatches = 1;
22610297Ssam 				found = c;
22710297Ssam 			} else if (q - name == longest)
22810297Ssam 				nmatches++;
22910297Ssam 		}
23010297Ssam 	}
23110297Ssam 	if (nmatches > 1)
23210297Ssam 		return ((struct cmd *)-1);
23310297Ssam 	return (found);
23410297Ssam }
23510297Ssam 
23610297Ssam /*
23710297Ssam  * Slice a string up into argc/argv.
23810297Ssam  */
23910297Ssam makeargv()
24010297Ssam {
24110297Ssam 	char **argp;
24210297Ssam 	char *slurpstring();
24310297Ssam 
24410297Ssam 	margc = 0;
24510297Ssam 	argp = margv;
24610297Ssam 	stringbase = line;		/* scan from first of buffer */
24710297Ssam 	argbase = argbuf;		/* store from first of buffer */
24810297Ssam 	while (*argp++ = slurpstring())
24910297Ssam 		margc++;
25010297Ssam }
25110297Ssam 
25210297Ssam /*
25310297Ssam  * Parse string into argbuf;
25410297Ssam  * implemented with FSM to
25510297Ssam  * handle quoting and strings
25610297Ssam  */
25710297Ssam char *
25810297Ssam slurpstring()
25910297Ssam {
26010297Ssam 	int got_one = 0;
26110297Ssam 	register char *sb = stringbase;
26210297Ssam 	register char *ap = argbase;
26310297Ssam 	char *tmp = argbase;		/* will return this if token found */
26410297Ssam 
265*11654Ssam 	if (*sb == '!') {		/* recognize ! as a token for shell */
266*11654Ssam 		stringbase++;
267*11654Ssam 		return ("!");
268*11654Ssam 	}
26910297Ssam S0:
27010297Ssam 	switch (*sb) {
27110297Ssam 
27210297Ssam 	case '\0':
27310297Ssam 		goto OUT;
27410297Ssam 
27510297Ssam 	case ' ':
27610297Ssam 	case '\t':
27710297Ssam 		sb++; goto S0;
27810297Ssam 
27910297Ssam 	default:
28010297Ssam 		goto S1;
28110297Ssam 	}
28210297Ssam 
28310297Ssam S1:
28410297Ssam 	switch (*sb) {
28510297Ssam 
28610297Ssam 	case ' ':
28710297Ssam 	case '\t':
28810297Ssam 	case '\0':
28910297Ssam 		goto OUT;	/* end of token */
29010297Ssam 
29110297Ssam 	case '\\':
29210297Ssam 		sb++; goto S2;	/* slurp next character */
29310297Ssam 
29410297Ssam 	case '"':
29510297Ssam 		sb++; goto S3;	/* slurp quoted string */
29610297Ssam 
29710297Ssam 	default:
29810297Ssam 		*ap++ = *sb++;	/* add character to token */
29910297Ssam 		got_one = 1;
30010297Ssam 		goto S1;
30110297Ssam 	}
30210297Ssam 
30310297Ssam S2:
30410297Ssam 	switch (*sb) {
30510297Ssam 
30610297Ssam 	case '\0':
30710297Ssam 		goto OUT;
30810297Ssam 
30910297Ssam 	default:
31010297Ssam 		*ap++ = *sb++;
31110297Ssam 		got_one = 1;
31210297Ssam 		goto S1;
31310297Ssam 	}
31410297Ssam 
31510297Ssam S3:
31610297Ssam 	switch (*sb) {
31710297Ssam 
31810297Ssam 	case '\0':
31910297Ssam 		goto OUT;
32010297Ssam 
32110297Ssam 	case '"':
32210297Ssam 		sb++; goto S1;
32310297Ssam 
32410297Ssam 	default:
32510297Ssam 		*ap++ = *sb++;
32610297Ssam 		got_one = 1;
32710297Ssam 		goto S3;
32810297Ssam 	}
32910297Ssam 
33010297Ssam OUT:
33110297Ssam 	if (got_one)
33210297Ssam 		*ap++ = '\0';
33310297Ssam 	argbase = ap;			/* update storage pointer */
33410297Ssam 	stringbase = sb;		/* update scan pointer */
33510297Ssam 	if (got_one)
33610297Ssam 		return(tmp);
33710297Ssam 	return((char *)0);
33810297Ssam }
33910297Ssam 
34010297Ssam #define HELPINDENT (sizeof ("directory"))
34110297Ssam 
34210297Ssam /*
34310297Ssam  * Help command.
34410297Ssam  * Call each command handler with argc == 0 and argv[0] == name.
34510297Ssam  */
34610297Ssam help(argc, argv)
34710297Ssam 	int argc;
34810297Ssam 	char *argv[];
34910297Ssam {
35010297Ssam 	register struct cmd *c;
35110297Ssam 
35210297Ssam 	if (argc == 1) {
35310297Ssam 		register int i, j, w;
35410297Ssam 		int columns, width = 0, lines;
35510297Ssam 		extern int NCMDS;
35610297Ssam 
35710297Ssam 		printf("Commands may be abbreviated.  Commands are:\n\n");
35810297Ssam 		for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
35910297Ssam 			int len = strlen(c->c_name);
36010297Ssam 
36110297Ssam 			if (len > width)
36210297Ssam 				width = len;
36310297Ssam 		}
36410297Ssam 		width = (width + 8) &~ 7;
36510297Ssam 		columns = 80 / width;
36610297Ssam 		if (columns == 0)
36710297Ssam 			columns = 1;
36810297Ssam 		lines = (NCMDS + columns - 1) / columns;
36910297Ssam 		for (i = 0; i < lines; i++) {
37010297Ssam 			for (j = 0; j < columns; j++) {
37110297Ssam 				c = cmdtab + j * lines + i;
37210297Ssam 				printf("%s", c->c_name);
37310297Ssam 				if (c + lines >= &cmdtab[NCMDS]) {
37410297Ssam 					printf("\n");
37510297Ssam 					break;
37610297Ssam 				}
37710297Ssam 				w = strlen(c->c_name);
37810297Ssam 				while (w < width) {
37910297Ssam 					w = (w + 8) &~ 7;
38010297Ssam 					putchar('\t');
38110297Ssam 				}
38210297Ssam 			}
38310297Ssam 		}
38410297Ssam 		return;
38510297Ssam 	}
38610297Ssam 	while (--argc > 0) {
38710297Ssam 		register char *arg;
38810297Ssam 		arg = *++argv;
38910297Ssam 		c = getcmd(arg);
39010297Ssam 		if (c == (struct cmd *)-1)
39110297Ssam 			printf("?Ambiguous help command %s\n", arg);
39210297Ssam 		else if (c == (struct cmd *)0)
39310297Ssam 			printf("?Invalid help command %s\n", arg);
39410297Ssam 		else
39510297Ssam 			printf("%-*s\t%s\n", HELPINDENT,
39610297Ssam 				c->c_name, c->c_help);
39710297Ssam 	}
39810297Ssam }
39910297Ssam 
40010297Ssam /*
40110297Ssam  * Call routine with argc, argv set from args (terminated by 0).
40210297Ssam  */
40310297Ssam /* VARARGS2 */
40410297Ssam call(routine, args)
40510297Ssam 	int (*routine)();
40610297Ssam 	int args;
40710297Ssam {
40810297Ssam 	register int *argp;
40910297Ssam 	register int argc;
41010297Ssam 
41110297Ssam 	for (argc = 0, argp = &args; *argp++ != 0; argc++)
41210297Ssam 		;
41310297Ssam 	(*routine)(argc, &args);
41410297Ssam }
415