1*21740Sdist /* 2*21740Sdist * Copyright (c) 1983 Regents of the University of California. 3*21740Sdist * All rights reserved. The Berkeley software License Agreement 4*21740Sdist * specifies the terms and conditions for redistribution. 5*21740Sdist */ 6*21740Sdist 710297Ssam #ifndef lint 8*21740Sdist char copyright[] = 9*21740Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10*21740Sdist All rights reserved.\n"; 11*21740Sdist #endif not lint 1210297Ssam 13*21740Sdist #ifndef lint 14*21740Sdist static char sccsid[] = "@(#)main.c 5.1 (Berkeley) 05/31/85"; 15*21740Sdist #endif not lint 16*21740Sdist 1710297Ssam /* 1810297Ssam * FTP User Program -- Command Interface. 1910297Ssam */ 2011352Ssam #include <sys/param.h> 2110297Ssam #include <sys/socket.h> 2210297Ssam #include <sys/ioctl.h> 2310297Ssam 2412398Ssam #include <arpa/ftp.h> 2512398Ssam 2610297Ssam #include <signal.h> 2710297Ssam #include <stdio.h> 2810297Ssam #include <errno.h> 2910297Ssam #include <ctype.h> 3010297Ssam #include <netdb.h> 3111352Ssam #include <pwd.h> 3210297Ssam 3310297Ssam #include "ftp_var.h" 3410297Ssam 3510297Ssam int intr(); 3610297Ssam int lostpeer(); 3711352Ssam extern char *home; 3810297Ssam 3910297Ssam main(argc, argv) 4010297Ssam char *argv[]; 4110297Ssam { 4210297Ssam register char *cp; 4310297Ssam int top; 4411352Ssam struct passwd *pw; 4511352Ssam char homedir[MAXPATHLEN]; 4610297Ssam 4710297Ssam sp = getservbyname("ftp", "tcp"); 4810297Ssam if (sp == 0) { 4910297Ssam fprintf(stderr, "ftp: ftp/tcp: unknown service\n"); 5010297Ssam exit(1); 5110297Ssam } 5211351Ssam doglob = 1; 5311654Ssam interactive = 1; 5411351Ssam autologin = 1; 5510297Ssam argc--, argv++; 5610297Ssam while (argc > 0 && **argv == '-') { 5710297Ssam for (cp = *argv + 1; *cp; cp++) 5810297Ssam switch (*cp) { 5910297Ssam 6010297Ssam case 'd': 6110297Ssam options |= SO_DEBUG; 6210297Ssam debug++; 6310297Ssam break; 6410297Ssam 6510297Ssam case 'v': 6610297Ssam verbose++; 6710297Ssam break; 6810297Ssam 6910297Ssam case 't': 7010297Ssam trace++; 7110297Ssam break; 7210297Ssam 7310297Ssam case 'i': 7411654Ssam interactive = 0; 7510297Ssam break; 7610297Ssam 7710297Ssam case 'n': 7810297Ssam autologin = 0; 7910297Ssam break; 8010297Ssam 8111351Ssam case 'g': 8211351Ssam doglob = 0; 8311351Ssam break; 8411351Ssam 8510297Ssam default: 8610297Ssam fprintf(stderr, 8710297Ssam "ftp: %c: unknown option\n", *cp); 8810297Ssam exit(1); 8910297Ssam } 9010297Ssam argc--, argv++; 9110297Ssam } 9210297Ssam fromatty = isatty(fileno(stdin)); 9310297Ssam /* 9410297Ssam * Set up defaults for FTP. 9510297Ssam */ 9610297Ssam strcpy(typename, "ascii"), type = TYPE_A; 9710297Ssam strcpy(formname, "non-print"), form = FORM_N; 9810297Ssam strcpy(modename, "stream"), mode = MODE_S; 9910297Ssam strcpy(structname, "file"), stru = STRU_F; 10011227Ssam strcpy(bytename, "8"), bytesize = 8; 10110297Ssam if (fromatty) 10210297Ssam verbose++; 10311352Ssam /* 10411352Ssam * Set up the home directory in case we're globbing. 10511352Ssam */ 10611352Ssam pw = getpwnam(getlogin()); 10711352Ssam if (pw == NULL) 10811352Ssam pw = getpwuid(getuid()); 10911352Ssam if (pw != NULL) { 11011352Ssam home = homedir; 11111352Ssam strcpy(home, pw->pw_dir); 11211352Ssam } 11310297Ssam if (argc > 0) { 11410297Ssam if (setjmp(toplevel)) 11510297Ssam exit(0); 11612993Ssam signal(SIGINT, intr); 11712993Ssam signal(SIGPIPE, lostpeer); 11810297Ssam setpeer(argc + 1, argv - 1); 11910297Ssam } 12010297Ssam top = setjmp(toplevel) == 0; 12110297Ssam if (top) { 12212993Ssam signal(SIGINT, intr); 12312993Ssam signal(SIGPIPE, lostpeer); 12410297Ssam } 12510297Ssam for (;;) { 12610297Ssam cmdscanner(top); 12710297Ssam top = 1; 12810297Ssam } 12910297Ssam } 13010297Ssam 13110297Ssam intr() 13210297Ssam { 13310297Ssam 13410297Ssam longjmp(toplevel, 1); 13510297Ssam } 13610297Ssam 13710297Ssam lostpeer() 13810297Ssam { 13910297Ssam extern FILE *cout; 14010297Ssam extern int data; 14110297Ssam 14210297Ssam if (connected) { 14310297Ssam if (cout != NULL) { 14411239Ssam shutdown(fileno(cout), 1+1); 14510297Ssam fclose(cout); 14610297Ssam cout = NULL; 14710297Ssam } 14810297Ssam if (data >= 0) { 14911239Ssam shutdown(data, 1+1); 15010297Ssam (void) close(data); 15110297Ssam data = -1; 15210297Ssam } 15310297Ssam connected = 0; 15410297Ssam } 15510297Ssam longjmp(toplevel, 1); 15610297Ssam } 15710297Ssam 15810297Ssam char * 15910297Ssam tail(filename) 16010297Ssam char *filename; 16110297Ssam { 16210297Ssam register char *s; 16310297Ssam 16410297Ssam while (*filename) { 16510297Ssam s = rindex(filename, '/'); 16610297Ssam if (s == NULL) 16710297Ssam break; 16810297Ssam if (s[1]) 16910297Ssam return (s + 1); 17010297Ssam *s = '\0'; 17110297Ssam } 17210297Ssam return (filename); 17310297Ssam } 17410297Ssam 17510297Ssam /* 17610297Ssam * Command parser. 17710297Ssam */ 17810297Ssam cmdscanner(top) 17910297Ssam int top; 18010297Ssam { 18110297Ssam register struct cmd *c; 18210297Ssam struct cmd *getcmd(); 18310297Ssam extern struct cmd cmdtab[]; 18410297Ssam extern int help(); 18510297Ssam 18610297Ssam if (!top) 18710297Ssam putchar('\n'); 18810297Ssam for (;;) { 18910297Ssam if (fromatty) { 19010297Ssam printf("ftp> "); 19110297Ssam fflush(stdout); 19210297Ssam } 19313968Ssam if (gets(line) == 0) { 19413968Ssam if (feof(stdin)) { 19516735Sralph if (!fromatty) 19616735Sralph quit(); 19713968Ssam clearerr(stdin); 19813968Ssam putchar('\n'); 19913968Ssam } 20010297Ssam break; 20113968Ssam } 20210297Ssam if (line[0] == 0) 20310297Ssam break; 20410297Ssam makeargv(); 20510297Ssam c = getcmd(margv[0]); 20610297Ssam if (c == (struct cmd *)-1) { 20710297Ssam printf("?Ambiguous command\n"); 20810297Ssam continue; 20910297Ssam } 21010297Ssam if (c == 0) { 21110297Ssam printf("?Invalid command\n"); 21210297Ssam continue; 21310297Ssam } 21411654Ssam if (c->c_conn && !connected) { 21511654Ssam printf ("Not connected.\n"); 21611654Ssam continue; 21711654Ssam } 21810297Ssam (*c->c_handler)(margc, margv); 21910297Ssam if (bell && c->c_bell) 22010297Ssam putchar(CTRL(g)); 22110297Ssam if (c->c_handler != help) 22210297Ssam break; 22310297Ssam } 22410297Ssam longjmp(toplevel, 0); 22510297Ssam } 22610297Ssam 22710297Ssam struct cmd * 22810297Ssam getcmd(name) 22910297Ssam register char *name; 23010297Ssam { 23110297Ssam register char *p, *q; 23210297Ssam register struct cmd *c, *found; 23310297Ssam register int nmatches, longest; 23410297Ssam 23510297Ssam longest = 0; 23610297Ssam nmatches = 0; 23710297Ssam found = 0; 23810297Ssam for (c = cmdtab; p = c->c_name; c++) { 23910297Ssam for (q = name; *q == *p++; q++) 24010297Ssam if (*q == 0) /* exact match? */ 24110297Ssam return (c); 24210297Ssam if (!*q) { /* the name was a prefix */ 24310297Ssam if (q - name > longest) { 24410297Ssam longest = q - name; 24510297Ssam nmatches = 1; 24610297Ssam found = c; 24710297Ssam } else if (q - name == longest) 24810297Ssam nmatches++; 24910297Ssam } 25010297Ssam } 25110297Ssam if (nmatches > 1) 25210297Ssam return ((struct cmd *)-1); 25310297Ssam return (found); 25410297Ssam } 25510297Ssam 25610297Ssam /* 25710297Ssam * Slice a string up into argc/argv. 25810297Ssam */ 25910297Ssam makeargv() 26010297Ssam { 26110297Ssam char **argp; 26210297Ssam char *slurpstring(); 26310297Ssam 26410297Ssam margc = 0; 26510297Ssam argp = margv; 26610297Ssam stringbase = line; /* scan from first of buffer */ 26710297Ssam argbase = argbuf; /* store from first of buffer */ 26810297Ssam while (*argp++ = slurpstring()) 26910297Ssam margc++; 27010297Ssam } 27110297Ssam 27210297Ssam /* 27310297Ssam * Parse string into argbuf; 27410297Ssam * implemented with FSM to 27510297Ssam * handle quoting and strings 27610297Ssam */ 27710297Ssam char * 27810297Ssam slurpstring() 27910297Ssam { 28010297Ssam int got_one = 0; 28110297Ssam register char *sb = stringbase; 28210297Ssam register char *ap = argbase; 28310297Ssam char *tmp = argbase; /* will return this if token found */ 28410297Ssam 28511654Ssam if (*sb == '!') { /* recognize ! as a token for shell */ 28611654Ssam stringbase++; 28711654Ssam return ("!"); 28811654Ssam } 28910297Ssam S0: 29010297Ssam switch (*sb) { 29110297Ssam 29210297Ssam case '\0': 29310297Ssam goto OUT; 29410297Ssam 29510297Ssam case ' ': 29610297Ssam case '\t': 29710297Ssam sb++; goto S0; 29810297Ssam 29910297Ssam default: 30010297Ssam goto S1; 30110297Ssam } 30210297Ssam 30310297Ssam S1: 30410297Ssam switch (*sb) { 30510297Ssam 30610297Ssam case ' ': 30710297Ssam case '\t': 30810297Ssam case '\0': 30910297Ssam goto OUT; /* end of token */ 31010297Ssam 31110297Ssam case '\\': 31210297Ssam sb++; goto S2; /* slurp next character */ 31310297Ssam 31410297Ssam case '"': 31510297Ssam sb++; goto S3; /* slurp quoted string */ 31610297Ssam 31710297Ssam default: 31810297Ssam *ap++ = *sb++; /* add character to token */ 31910297Ssam got_one = 1; 32010297Ssam goto S1; 32110297Ssam } 32210297Ssam 32310297Ssam S2: 32410297Ssam switch (*sb) { 32510297Ssam 32610297Ssam case '\0': 32710297Ssam goto OUT; 32810297Ssam 32910297Ssam default: 33010297Ssam *ap++ = *sb++; 33110297Ssam got_one = 1; 33210297Ssam goto S1; 33310297Ssam } 33410297Ssam 33510297Ssam S3: 33610297Ssam switch (*sb) { 33710297Ssam 33810297Ssam case '\0': 33910297Ssam goto OUT; 34010297Ssam 34110297Ssam case '"': 34210297Ssam sb++; goto S1; 34310297Ssam 34410297Ssam default: 34510297Ssam *ap++ = *sb++; 34610297Ssam got_one = 1; 34710297Ssam goto S3; 34810297Ssam } 34910297Ssam 35010297Ssam OUT: 35110297Ssam if (got_one) 35210297Ssam *ap++ = '\0'; 35310297Ssam argbase = ap; /* update storage pointer */ 35410297Ssam stringbase = sb; /* update scan pointer */ 35510297Ssam if (got_one) 35610297Ssam return(tmp); 35710297Ssam return((char *)0); 35810297Ssam } 35910297Ssam 36010297Ssam #define HELPINDENT (sizeof ("directory")) 36110297Ssam 36210297Ssam /* 36310297Ssam * Help command. 36410297Ssam * Call each command handler with argc == 0 and argv[0] == name. 36510297Ssam */ 36610297Ssam help(argc, argv) 36710297Ssam int argc; 36810297Ssam char *argv[]; 36910297Ssam { 37010297Ssam register struct cmd *c; 37110297Ssam 37210297Ssam if (argc == 1) { 37310297Ssam register int i, j, w; 37410297Ssam int columns, width = 0, lines; 37510297Ssam extern int NCMDS; 37610297Ssam 37710297Ssam printf("Commands may be abbreviated. Commands are:\n\n"); 37810297Ssam for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 37910297Ssam int len = strlen(c->c_name); 38010297Ssam 38110297Ssam if (len > width) 38210297Ssam width = len; 38310297Ssam } 38410297Ssam width = (width + 8) &~ 7; 38510297Ssam columns = 80 / width; 38610297Ssam if (columns == 0) 38710297Ssam columns = 1; 38810297Ssam lines = (NCMDS + columns - 1) / columns; 38910297Ssam for (i = 0; i < lines; i++) { 39010297Ssam for (j = 0; j < columns; j++) { 39110297Ssam c = cmdtab + j * lines + i; 39210297Ssam printf("%s", c->c_name); 39310297Ssam if (c + lines >= &cmdtab[NCMDS]) { 39410297Ssam printf("\n"); 39510297Ssam break; 39610297Ssam } 39710297Ssam w = strlen(c->c_name); 39810297Ssam while (w < width) { 39910297Ssam w = (w + 8) &~ 7; 40010297Ssam putchar('\t'); 40110297Ssam } 40210297Ssam } 40310297Ssam } 40410297Ssam return; 40510297Ssam } 40610297Ssam while (--argc > 0) { 40710297Ssam register char *arg; 40810297Ssam arg = *++argv; 40910297Ssam c = getcmd(arg); 41010297Ssam if (c == (struct cmd *)-1) 41110297Ssam printf("?Ambiguous help command %s\n", arg); 41210297Ssam else if (c == (struct cmd *)0) 41310297Ssam printf("?Invalid help command %s\n", arg); 41410297Ssam else 41510297Ssam printf("%-*s\t%s\n", HELPINDENT, 41610297Ssam c->c_name, c->c_help); 41710297Ssam } 41810297Ssam } 41910297Ssam 42010297Ssam /* 42110297Ssam * Call routine with argc, argv set from args (terminated by 0). 42210297Ssam */ 42310297Ssam /* VARARGS2 */ 42410297Ssam call(routine, args) 42510297Ssam int (*routine)(); 42610297Ssam int args; 42710297Ssam { 42810297Ssam register int *argp; 42910297Ssam register int argc; 43010297Ssam 43110297Ssam for (argc = 0, argp = &args; *argp++ != 0; argc++) 43210297Ssam ; 43310297Ssam (*routine)(argc, &args); 43410297Ssam } 435