110297Ssam #ifndef lint 2*11239Ssam static char sccsid[] = "@(#)main.c 4.3 (Berkeley) 02/23/83"; 310297Ssam #endif 410297Ssam 510297Ssam /* 610297Ssam * FTP User Program -- Command Interface. 710297Ssam */ 810297Ssam #include <sys/types.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> 1710297Ssam 1810297Ssam #include "ftp.h" 1910297Ssam #include "ftp_var.h" 2010297Ssam 2110297Ssam int intr(); 2210297Ssam int lostpeer(); 2310297Ssam 2410297Ssam main(argc, argv) 2510297Ssam char *argv[]; 2610297Ssam { 2710297Ssam register char *cp; 2810297Ssam int top; 2910297Ssam 3010297Ssam sp = getservbyname("ftp", "tcp"); 3110297Ssam if (sp == 0) { 3210297Ssam fprintf(stderr, "ftp: ftp/tcp: unknown service\n"); 3310297Ssam exit(1); 3410297Ssam } 3510297Ssam argc--, argv++; 3610297Ssam while (argc > 0 && **argv == '-') { 3710297Ssam for (cp = *argv + 1; *cp; cp++) 3810297Ssam switch (*cp) { 3910297Ssam 4010297Ssam case 'd': 4110297Ssam options |= SO_DEBUG; 4210297Ssam debug++; 4310297Ssam break; 4410297Ssam 4510297Ssam case 'v': 4610297Ssam verbose++; 4710297Ssam break; 4810297Ssam 4910297Ssam case 't': 5010297Ssam trace++; 5110297Ssam break; 5210297Ssam 5310297Ssam case 'i': 5410297Ssam interactive++; 5510297Ssam break; 5610297Ssam 5710297Ssam case 'n': 5810297Ssam autologin = 0; 5910297Ssam break; 6010297Ssam 6110297Ssam default: 6210297Ssam fprintf(stderr, 6310297Ssam "ftp: %c: unknown option\n", *cp); 6410297Ssam exit(1); 6510297Ssam } 6610297Ssam argc--, argv++; 6710297Ssam } 6810297Ssam fromatty = isatty(fileno(stdin)); 6910297Ssam /* 7010297Ssam * Set up defaults for FTP. 7110297Ssam */ 7210297Ssam strcpy(typename, "ascii"), type = TYPE_A; 7310297Ssam strcpy(formname, "non-print"), form = FORM_N; 7410297Ssam strcpy(modename, "stream"), mode = MODE_S; 7510297Ssam strcpy(structname, "file"), stru = STRU_F; 7611227Ssam strcpy(bytename, "8"), bytesize = 8; 7710297Ssam if (fromatty) 7810297Ssam verbose++; 7910297Ssam if (argc > 0) { 8010297Ssam if (setjmp(toplevel)) 8110297Ssam exit(0); 8210297Ssam sigset(SIGINT, intr); 8310297Ssam sigset(SIGPIPE, lostpeer); 8410297Ssam setpeer(argc + 1, argv - 1); 8510297Ssam } 8610297Ssam top = setjmp(toplevel) == 0; 8710297Ssam if (top) { 8810297Ssam sigset(SIGINT, intr); 8910297Ssam sigset(SIGPIPE, lostpeer); 9010297Ssam } 9110297Ssam for (;;) { 9210297Ssam cmdscanner(top); 9310297Ssam top = 1; 9410297Ssam } 9510297Ssam } 9610297Ssam 9710297Ssam intr() 9810297Ssam { 9910297Ssam 10010297Ssam longjmp(toplevel, 1); 10110297Ssam } 10210297Ssam 10310297Ssam lostpeer() 10410297Ssam { 10510297Ssam extern FILE *cout; 10610297Ssam extern int data; 10710297Ssam 10810297Ssam if (connected) { 10910297Ssam if (cout != NULL) { 110*11239Ssam shutdown(fileno(cout), 1+1); 11110297Ssam fclose(cout); 11210297Ssam cout = NULL; 11310297Ssam } 11410297Ssam if (data >= 0) { 115*11239Ssam shutdown(data, 1+1); 11610297Ssam (void) close(data); 11710297Ssam data = -1; 11810297Ssam } 11910297Ssam connected = 0; 12010297Ssam } 12110297Ssam longjmp(toplevel, 1); 12210297Ssam } 12310297Ssam 12410297Ssam char * 12510297Ssam tail(filename) 12610297Ssam char *filename; 12710297Ssam { 12810297Ssam register char *s; 12910297Ssam 13010297Ssam while (*filename) { 13110297Ssam s = rindex(filename, '/'); 13210297Ssam if (s == NULL) 13310297Ssam break; 13410297Ssam if (s[1]) 13510297Ssam return (s + 1); 13610297Ssam *s = '\0'; 13710297Ssam } 13810297Ssam return (filename); 13910297Ssam } 14010297Ssam 14110297Ssam /* 14210297Ssam * Command parser. 14310297Ssam */ 14410297Ssam cmdscanner(top) 14510297Ssam int top; 14610297Ssam { 14710297Ssam register struct cmd *c; 14810297Ssam struct cmd *getcmd(); 14910297Ssam extern struct cmd cmdtab[]; 15010297Ssam extern int help(); 15110297Ssam 15210297Ssam if (!top) 15310297Ssam putchar('\n'); 15410297Ssam for (;;) { 15510297Ssam if (fromatty) { 15610297Ssam printf("ftp> "); 15710297Ssam fflush(stdout); 15810297Ssam } 15910297Ssam if (gets(line) == 0) 16010297Ssam break; 16110297Ssam if (line[0] == 0) 16210297Ssam break; 16310297Ssam makeargv(); 16410297Ssam c = getcmd(margv[0]); 16510297Ssam if (c == (struct cmd *)-1) { 16610297Ssam printf("?Ambiguous command\n"); 16710297Ssam continue; 16810297Ssam } 16910297Ssam if (c == 0) { 17010297Ssam printf("?Invalid command\n"); 17110297Ssam continue; 17210297Ssam } 17310297Ssam (*c->c_handler)(margc, margv); 17410297Ssam if (bell && c->c_bell) 17510297Ssam putchar(CTRL(g)); 17610297Ssam if (c->c_handler != help) 17710297Ssam break; 17810297Ssam } 17910297Ssam longjmp(toplevel, 0); 18010297Ssam } 18110297Ssam 18210297Ssam struct cmd * 18310297Ssam getcmd(name) 18410297Ssam register char *name; 18510297Ssam { 18610297Ssam register char *p, *q; 18710297Ssam register struct cmd *c, *found; 18810297Ssam register int nmatches, longest; 18910297Ssam 19010297Ssam longest = 0; 19110297Ssam nmatches = 0; 19210297Ssam found = 0; 19310297Ssam for (c = cmdtab; p = c->c_name; c++) { 19410297Ssam for (q = name; *q == *p++; q++) 19510297Ssam if (*q == 0) /* exact match? */ 19610297Ssam return (c); 19710297Ssam if (!*q) { /* the name was a prefix */ 19810297Ssam if (q - name > longest) { 19910297Ssam longest = q - name; 20010297Ssam nmatches = 1; 20110297Ssam found = c; 20210297Ssam } else if (q - name == longest) 20310297Ssam nmatches++; 20410297Ssam } 20510297Ssam } 20610297Ssam if (nmatches > 1) 20710297Ssam return ((struct cmd *)-1); 20810297Ssam return (found); 20910297Ssam } 21010297Ssam 21110297Ssam /* 21210297Ssam * Slice a string up into argc/argv. 21310297Ssam */ 21410297Ssam makeargv() 21510297Ssam { 21610297Ssam char **argp; 21710297Ssam char *slurpstring(); 21810297Ssam 21910297Ssam margc = 0; 22010297Ssam argp = margv; 22110297Ssam stringbase = line; /* scan from first of buffer */ 22210297Ssam argbase = argbuf; /* store from first of buffer */ 22310297Ssam while (*argp++ = slurpstring()) 22410297Ssam margc++; 22510297Ssam } 22610297Ssam 22710297Ssam /* 22810297Ssam * Parse string into argbuf; 22910297Ssam * implemented with FSM to 23010297Ssam * handle quoting and strings 23110297Ssam */ 23210297Ssam char * 23310297Ssam slurpstring() 23410297Ssam { 23510297Ssam int got_one = 0; 23610297Ssam register char *sb = stringbase; 23710297Ssam register char *ap = argbase; 23810297Ssam char *tmp = argbase; /* will return this if token found */ 23910297Ssam 24010297Ssam S0: 24110297Ssam switch (*sb) { 24210297Ssam 24310297Ssam case '\0': 24410297Ssam goto OUT; 24510297Ssam 24610297Ssam case ' ': 24710297Ssam case '\t': 24810297Ssam sb++; goto S0; 24910297Ssam 25010297Ssam default: 25110297Ssam goto S1; 25210297Ssam } 25310297Ssam 25410297Ssam S1: 25510297Ssam switch (*sb) { 25610297Ssam 25710297Ssam case ' ': 25810297Ssam case '\t': 25910297Ssam case '\0': 26010297Ssam goto OUT; /* end of token */ 26110297Ssam 26210297Ssam case '\\': 26310297Ssam sb++; goto S2; /* slurp next character */ 26410297Ssam 26510297Ssam case '"': 26610297Ssam sb++; goto S3; /* slurp quoted string */ 26710297Ssam 26810297Ssam default: 26910297Ssam *ap++ = *sb++; /* add character to token */ 27010297Ssam got_one = 1; 27110297Ssam goto S1; 27210297Ssam } 27310297Ssam 27410297Ssam S2: 27510297Ssam switch (*sb) { 27610297Ssam 27710297Ssam case '\0': 27810297Ssam goto OUT; 27910297Ssam 28010297Ssam default: 28110297Ssam *ap++ = *sb++; 28210297Ssam got_one = 1; 28310297Ssam goto S1; 28410297Ssam } 28510297Ssam 28610297Ssam S3: 28710297Ssam switch (*sb) { 28810297Ssam 28910297Ssam case '\0': 29010297Ssam goto OUT; 29110297Ssam 29210297Ssam case '"': 29310297Ssam sb++; goto S1; 29410297Ssam 29510297Ssam default: 29610297Ssam *ap++ = *sb++; 29710297Ssam got_one = 1; 29810297Ssam goto S3; 29910297Ssam } 30010297Ssam 30110297Ssam OUT: 30210297Ssam if (got_one) 30310297Ssam *ap++ = '\0'; 30410297Ssam argbase = ap; /* update storage pointer */ 30510297Ssam stringbase = sb; /* update scan pointer */ 30610297Ssam if (got_one) 30710297Ssam return(tmp); 30810297Ssam return((char *)0); 30910297Ssam } 31010297Ssam 31110297Ssam #define HELPINDENT (sizeof ("directory")) 31210297Ssam 31310297Ssam /* 31410297Ssam * Help command. 31510297Ssam * Call each command handler with argc == 0 and argv[0] == name. 31610297Ssam */ 31710297Ssam help(argc, argv) 31810297Ssam int argc; 31910297Ssam char *argv[]; 32010297Ssam { 32110297Ssam register struct cmd *c; 32210297Ssam 32310297Ssam if (argc == 1) { 32410297Ssam register int i, j, w; 32510297Ssam int columns, width = 0, lines; 32610297Ssam extern int NCMDS; 32710297Ssam 32810297Ssam printf("Commands may be abbreviated. Commands are:\n\n"); 32910297Ssam for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 33010297Ssam int len = strlen(c->c_name); 33110297Ssam 33210297Ssam if (len > width) 33310297Ssam width = len; 33410297Ssam } 33510297Ssam width = (width + 8) &~ 7; 33610297Ssam columns = 80 / width; 33710297Ssam if (columns == 0) 33810297Ssam columns = 1; 33910297Ssam lines = (NCMDS + columns - 1) / columns; 34010297Ssam for (i = 0; i < lines; i++) { 34110297Ssam for (j = 0; j < columns; j++) { 34210297Ssam c = cmdtab + j * lines + i; 34310297Ssam printf("%s", c->c_name); 34410297Ssam if (c + lines >= &cmdtab[NCMDS]) { 34510297Ssam printf("\n"); 34610297Ssam break; 34710297Ssam } 34810297Ssam w = strlen(c->c_name); 34910297Ssam while (w < width) { 35010297Ssam w = (w + 8) &~ 7; 35110297Ssam putchar('\t'); 35210297Ssam } 35310297Ssam } 35410297Ssam } 35510297Ssam return; 35610297Ssam } 35710297Ssam while (--argc > 0) { 35810297Ssam register char *arg; 35910297Ssam arg = *++argv; 36010297Ssam c = getcmd(arg); 36110297Ssam if (c == (struct cmd *)-1) 36210297Ssam printf("?Ambiguous help command %s\n", arg); 36310297Ssam else if (c == (struct cmd *)0) 36410297Ssam printf("?Invalid help command %s\n", arg); 36510297Ssam else 36610297Ssam printf("%-*s\t%s\n", HELPINDENT, 36710297Ssam c->c_name, c->c_help); 36810297Ssam } 36910297Ssam } 37010297Ssam 37110297Ssam /* 37210297Ssam * Call routine with argc, argv set from args (terminated by 0). 37310297Ssam */ 37410297Ssam /* VARARGS2 */ 37510297Ssam call(routine, args) 37610297Ssam int (*routine)(); 37710297Ssam int args; 37810297Ssam { 37910297Ssam register int *argp; 38010297Ssam register int argc; 38110297Ssam 38210297Ssam for (argc = 0, argp = &args; *argp++ != 0; argc++) 38310297Ssam ; 38410297Ssam (*routine)(argc, &args); 38510297Ssam } 386