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