110297Ssam #ifndef lint 2*11227Ssam static char sccsid[] = "@(#)main.c 4.2 (Berkeley) 02/22/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; 76*11227Ssam 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 int how = 1+1; 10810297Ssam 10910297Ssam if (connected) { 11010297Ssam if (cout != NULL) { 11110297Ssam shutdown(fileno(cout), &how); 11210297Ssam fclose(cout); 11310297Ssam cout = NULL; 11410297Ssam } 11510297Ssam if (data >= 0) { 11610297Ssam shutdown(data, &how); 11710297Ssam (void) close(data); 11810297Ssam data = -1; 11910297Ssam } 12010297Ssam connected = 0; 12110297Ssam } 12210297Ssam longjmp(toplevel, 1); 12310297Ssam } 12410297Ssam 12510297Ssam char * 12610297Ssam tail(filename) 12710297Ssam char *filename; 12810297Ssam { 12910297Ssam register char *s; 13010297Ssam 13110297Ssam while (*filename) { 13210297Ssam s = rindex(filename, '/'); 13310297Ssam if (s == NULL) 13410297Ssam break; 13510297Ssam if (s[1]) 13610297Ssam return (s + 1); 13710297Ssam *s = '\0'; 13810297Ssam } 13910297Ssam return (filename); 14010297Ssam } 14110297Ssam 14210297Ssam /* 14310297Ssam * Command parser. 14410297Ssam */ 14510297Ssam cmdscanner(top) 14610297Ssam int top; 14710297Ssam { 14810297Ssam register struct cmd *c; 14910297Ssam struct cmd *getcmd(); 15010297Ssam extern struct cmd cmdtab[]; 15110297Ssam extern int help(); 15210297Ssam 15310297Ssam if (!top) 15410297Ssam putchar('\n'); 15510297Ssam for (;;) { 15610297Ssam if (fromatty) { 15710297Ssam printf("ftp> "); 15810297Ssam fflush(stdout); 15910297Ssam } 16010297Ssam if (gets(line) == 0) 16110297Ssam break; 16210297Ssam if (line[0] == 0) 16310297Ssam break; 16410297Ssam makeargv(); 16510297Ssam c = getcmd(margv[0]); 16610297Ssam if (c == (struct cmd *)-1) { 16710297Ssam printf("?Ambiguous command\n"); 16810297Ssam continue; 16910297Ssam } 17010297Ssam if (c == 0) { 17110297Ssam printf("?Invalid command\n"); 17210297Ssam continue; 17310297Ssam } 17410297Ssam (*c->c_handler)(margc, margv); 17510297Ssam if (bell && c->c_bell) 17610297Ssam putchar(CTRL(g)); 17710297Ssam if (c->c_handler != help) 17810297Ssam break; 17910297Ssam } 18010297Ssam longjmp(toplevel, 0); 18110297Ssam } 18210297Ssam 18310297Ssam struct cmd * 18410297Ssam getcmd(name) 18510297Ssam register char *name; 18610297Ssam { 18710297Ssam register char *p, *q; 18810297Ssam register struct cmd *c, *found; 18910297Ssam register int nmatches, longest; 19010297Ssam 19110297Ssam longest = 0; 19210297Ssam nmatches = 0; 19310297Ssam found = 0; 19410297Ssam for (c = cmdtab; p = c->c_name; c++) { 19510297Ssam for (q = name; *q == *p++; q++) 19610297Ssam if (*q == 0) /* exact match? */ 19710297Ssam return (c); 19810297Ssam if (!*q) { /* the name was a prefix */ 19910297Ssam if (q - name > longest) { 20010297Ssam longest = q - name; 20110297Ssam nmatches = 1; 20210297Ssam found = c; 20310297Ssam } else if (q - name == longest) 20410297Ssam nmatches++; 20510297Ssam } 20610297Ssam } 20710297Ssam if (nmatches > 1) 20810297Ssam return ((struct cmd *)-1); 20910297Ssam return (found); 21010297Ssam } 21110297Ssam 21210297Ssam /* 21310297Ssam * Slice a string up into argc/argv. 21410297Ssam */ 21510297Ssam makeargv() 21610297Ssam { 21710297Ssam char **argp; 21810297Ssam char *slurpstring(); 21910297Ssam 22010297Ssam margc = 0; 22110297Ssam argp = margv; 22210297Ssam stringbase = line; /* scan from first of buffer */ 22310297Ssam argbase = argbuf; /* store from first of buffer */ 22410297Ssam while (*argp++ = slurpstring()) 22510297Ssam margc++; 22610297Ssam } 22710297Ssam 22810297Ssam /* 22910297Ssam * Parse string into argbuf; 23010297Ssam * implemented with FSM to 23110297Ssam * handle quoting and strings 23210297Ssam */ 23310297Ssam char * 23410297Ssam slurpstring() 23510297Ssam { 23610297Ssam int got_one = 0; 23710297Ssam register char *sb = stringbase; 23810297Ssam register char *ap = argbase; 23910297Ssam char *tmp = argbase; /* will return this if token found */ 24010297Ssam 24110297Ssam S0: 24210297Ssam switch (*sb) { 24310297Ssam 24410297Ssam case '\0': 24510297Ssam goto OUT; 24610297Ssam 24710297Ssam case ' ': 24810297Ssam case '\t': 24910297Ssam sb++; goto S0; 25010297Ssam 25110297Ssam default: 25210297Ssam goto S1; 25310297Ssam } 25410297Ssam 25510297Ssam S1: 25610297Ssam switch (*sb) { 25710297Ssam 25810297Ssam case ' ': 25910297Ssam case '\t': 26010297Ssam case '\0': 26110297Ssam goto OUT; /* end of token */ 26210297Ssam 26310297Ssam case '\\': 26410297Ssam sb++; goto S2; /* slurp next character */ 26510297Ssam 26610297Ssam case '"': 26710297Ssam sb++; goto S3; /* slurp quoted string */ 26810297Ssam 26910297Ssam default: 27010297Ssam *ap++ = *sb++; /* add character to token */ 27110297Ssam got_one = 1; 27210297Ssam goto S1; 27310297Ssam } 27410297Ssam 27510297Ssam S2: 27610297Ssam switch (*sb) { 27710297Ssam 27810297Ssam case '\0': 27910297Ssam goto OUT; 28010297Ssam 28110297Ssam default: 28210297Ssam *ap++ = *sb++; 28310297Ssam got_one = 1; 28410297Ssam goto S1; 28510297Ssam } 28610297Ssam 28710297Ssam S3: 28810297Ssam switch (*sb) { 28910297Ssam 29010297Ssam case '\0': 29110297Ssam goto OUT; 29210297Ssam 29310297Ssam case '"': 29410297Ssam sb++; goto S1; 29510297Ssam 29610297Ssam default: 29710297Ssam *ap++ = *sb++; 29810297Ssam got_one = 1; 29910297Ssam goto S3; 30010297Ssam } 30110297Ssam 30210297Ssam OUT: 30310297Ssam if (got_one) 30410297Ssam *ap++ = '\0'; 30510297Ssam argbase = ap; /* update storage pointer */ 30610297Ssam stringbase = sb; /* update scan pointer */ 30710297Ssam if (got_one) 30810297Ssam return(tmp); 30910297Ssam return((char *)0); 31010297Ssam } 31110297Ssam 31210297Ssam #define HELPINDENT (sizeof ("directory")) 31310297Ssam 31410297Ssam /* 31510297Ssam * Help command. 31610297Ssam * Call each command handler with argc == 0 and argv[0] == name. 31710297Ssam */ 31810297Ssam help(argc, argv) 31910297Ssam int argc; 32010297Ssam char *argv[]; 32110297Ssam { 32210297Ssam register struct cmd *c; 32310297Ssam 32410297Ssam if (argc == 1) { 32510297Ssam register int i, j, w; 32610297Ssam int columns, width = 0, lines; 32710297Ssam extern int NCMDS; 32810297Ssam 32910297Ssam printf("Commands may be abbreviated. Commands are:\n\n"); 33010297Ssam for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 33110297Ssam int len = strlen(c->c_name); 33210297Ssam 33310297Ssam if (len > width) 33410297Ssam width = len; 33510297Ssam } 33610297Ssam width = (width + 8) &~ 7; 33710297Ssam columns = 80 / width; 33810297Ssam if (columns == 0) 33910297Ssam columns = 1; 34010297Ssam lines = (NCMDS + columns - 1) / columns; 34110297Ssam for (i = 0; i < lines; i++) { 34210297Ssam for (j = 0; j < columns; j++) { 34310297Ssam c = cmdtab + j * lines + i; 34410297Ssam printf("%s", c->c_name); 34510297Ssam if (c + lines >= &cmdtab[NCMDS]) { 34610297Ssam printf("\n"); 34710297Ssam break; 34810297Ssam } 34910297Ssam w = strlen(c->c_name); 35010297Ssam while (w < width) { 35110297Ssam w = (w + 8) &~ 7; 35210297Ssam putchar('\t'); 35310297Ssam } 35410297Ssam } 35510297Ssam } 35610297Ssam return; 35710297Ssam } 35810297Ssam while (--argc > 0) { 35910297Ssam register char *arg; 36010297Ssam arg = *++argv; 36110297Ssam c = getcmd(arg); 36210297Ssam if (c == (struct cmd *)-1) 36310297Ssam printf("?Ambiguous help command %s\n", arg); 36410297Ssam else if (c == (struct cmd *)0) 36510297Ssam printf("?Invalid help command %s\n", arg); 36610297Ssam else 36710297Ssam printf("%-*s\t%s\n", HELPINDENT, 36810297Ssam c->c_name, c->c_help); 36910297Ssam } 37010297Ssam } 37110297Ssam 37210297Ssam /* 37310297Ssam * Call routine with argc, argv set from args (terminated by 0). 37410297Ssam */ 37510297Ssam /* VARARGS2 */ 37610297Ssam call(routine, args) 37710297Ssam int (*routine)(); 37810297Ssam int args; 37910297Ssam { 38010297Ssam register int *argp; 38110297Ssam register int argc; 38210297Ssam 38310297Ssam for (argc = 0, argp = &args; *argp++ != 0; argc++) 38410297Ssam ; 38510297Ssam (*routine)(argc, &args); 38610297Ssam } 387