121740Sdist /* 226047Sminshall * Copyright (c) 1985 Regents of the University of California. 321740Sdist * All rights reserved. The Berkeley software License Agreement 421740Sdist * specifies the terms and conditions for redistribution. 521740Sdist */ 621740Sdist 710297Ssam #ifndef lint 821740Sdist char copyright[] = 926047Sminshall "@(#) Copyright (c) 1985 Regents of the University of California.\n\ 1021740Sdist All rights reserved.\n"; 1121740Sdist #endif not lint 1210297Ssam 1321740Sdist #ifndef lint 14*26082Slepreau static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 02/05/86"; 1521740Sdist #endif not lint 1621740Sdist 1710297Ssam /* 1810297Ssam * FTP User Program -- Command Interface. 1910297Ssam */ 2026047Sminshall #include "ftp_var.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 3410297Ssam int intr(); 3510297Ssam int lostpeer(); 3611352Ssam extern char *home; 3725907Smckusick char *getlogin(); 3810297Ssam 3910297Ssam main(argc, argv) 4010297Ssam char *argv[]; 4110297Ssam { 4210297Ssam register char *cp; 4310297Ssam int top; 4425907Smckusick struct passwd *pw = NULL; 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++; 10326047Sminshall cpend = 0; /* no pending replies */ 10426047Sminshall proxy = 0; /* proxy not active */ 10526047Sminshall crflag = 1; /* strip c.r. on ascii gets */ 10611352Ssam /* 10711352Ssam * Set up the home directory in case we're globbing. 10811352Ssam */ 10925907Smckusick cp = getlogin(); 11026047Sminshall if (cp != NULL) { 11125907Smckusick pw = getpwnam(cp); 11226047Sminshall } 11311352Ssam if (pw == NULL) 11411352Ssam pw = getpwuid(getuid()); 11511352Ssam if (pw != NULL) { 11611352Ssam home = homedir; 11711352Ssam strcpy(home, pw->pw_dir); 11811352Ssam } 11910297Ssam if (argc > 0) { 12010297Ssam if (setjmp(toplevel)) 12110297Ssam exit(0); 12212993Ssam signal(SIGINT, intr); 12312993Ssam signal(SIGPIPE, lostpeer); 12410297Ssam setpeer(argc + 1, argv - 1); 12510297Ssam } 12610297Ssam top = setjmp(toplevel) == 0; 12710297Ssam if (top) { 12812993Ssam signal(SIGINT, intr); 12912993Ssam signal(SIGPIPE, lostpeer); 13010297Ssam } 13110297Ssam for (;;) { 13210297Ssam cmdscanner(top); 13310297Ssam top = 1; 13410297Ssam } 13510297Ssam } 13610297Ssam 13710297Ssam intr() 13810297Ssam { 13910297Ssam 14010297Ssam longjmp(toplevel, 1); 14110297Ssam } 14210297Ssam 14310297Ssam lostpeer() 14410297Ssam { 14510297Ssam extern FILE *cout; 14610297Ssam extern int data; 14710297Ssam 14810297Ssam if (connected) { 14910297Ssam if (cout != NULL) { 15011239Ssam shutdown(fileno(cout), 1+1); 15110297Ssam fclose(cout); 15210297Ssam cout = NULL; 15310297Ssam } 15410297Ssam if (data >= 0) { 15511239Ssam shutdown(data, 1+1); 15610297Ssam (void) close(data); 15710297Ssam data = -1; 15810297Ssam } 15910297Ssam connected = 0; 16010297Ssam } 16126047Sminshall pswitch(1); 16226047Sminshall if (connected) { 16326047Sminshall if (cout != NULL) { 16426047Sminshall shutdown(fileno(cout), 1+1); 16526047Sminshall fclose(cout); 16626047Sminshall cout = NULL; 16726047Sminshall } 16826047Sminshall connected = 0; 16926047Sminshall } 17026047Sminshall proxflag = 0; 17126047Sminshall pswitch(0); 17210297Ssam } 17310297Ssam 17410297Ssam char * 17510297Ssam tail(filename) 17610297Ssam char *filename; 17710297Ssam { 17810297Ssam register char *s; 17910297Ssam 18010297Ssam while (*filename) { 18110297Ssam s = rindex(filename, '/'); 18210297Ssam if (s == NULL) 18310297Ssam break; 18410297Ssam if (s[1]) 18510297Ssam return (s + 1); 18610297Ssam *s = '\0'; 18710297Ssam } 18810297Ssam return (filename); 18910297Ssam } 19010297Ssam 19110297Ssam /* 19210297Ssam * Command parser. 19310297Ssam */ 19410297Ssam cmdscanner(top) 19510297Ssam int top; 19610297Ssam { 19710297Ssam register struct cmd *c; 19810297Ssam struct cmd *getcmd(); 19910297Ssam extern struct cmd cmdtab[]; 20010297Ssam extern int help(); 20110297Ssam 20210297Ssam if (!top) 20310297Ssam putchar('\n'); 20410297Ssam for (;;) { 20510297Ssam if (fromatty) { 20610297Ssam printf("ftp> "); 20710297Ssam fflush(stdout); 20810297Ssam } 20913968Ssam if (gets(line) == 0) { 210*26082Slepreau if (feof(stdin)) 211*26082Slepreau quit(); 21210297Ssam break; 21313968Ssam } 21410297Ssam if (line[0] == 0) 21510297Ssam break; 21610297Ssam makeargv(); 21726047Sminshall if (margc == 0) { 21825907Smckusick continue; 21926047Sminshall } 22010297Ssam c = getcmd(margv[0]); 22110297Ssam if (c == (struct cmd *)-1) { 22210297Ssam printf("?Ambiguous command\n"); 22310297Ssam continue; 22410297Ssam } 22510297Ssam if (c == 0) { 22610297Ssam printf("?Invalid command\n"); 22710297Ssam continue; 22810297Ssam } 22911654Ssam if (c->c_conn && !connected) { 23011654Ssam printf ("Not connected.\n"); 23111654Ssam continue; 23211654Ssam } 23310297Ssam (*c->c_handler)(margc, margv); 23410297Ssam if (bell && c->c_bell) 23510297Ssam putchar(CTRL(g)); 23610297Ssam if (c->c_handler != help) 23710297Ssam break; 23810297Ssam } 23926047Sminshall signal(SIGINT, intr); 24026047Sminshall signal(SIGPIPE, lostpeer); 24110297Ssam } 24210297Ssam 24310297Ssam struct cmd * 24410297Ssam getcmd(name) 24510297Ssam register char *name; 24610297Ssam { 24710297Ssam register char *p, *q; 24810297Ssam register struct cmd *c, *found; 24910297Ssam register int nmatches, longest; 25010297Ssam 25110297Ssam longest = 0; 25210297Ssam nmatches = 0; 25310297Ssam found = 0; 25410297Ssam for (c = cmdtab; p = c->c_name; c++) { 25510297Ssam for (q = name; *q == *p++; q++) 25610297Ssam if (*q == 0) /* exact match? */ 25710297Ssam return (c); 25810297Ssam if (!*q) { /* the name was a prefix */ 25910297Ssam if (q - name > longest) { 26010297Ssam longest = q - name; 26110297Ssam nmatches = 1; 26210297Ssam found = c; 26310297Ssam } else if (q - name == longest) 26410297Ssam nmatches++; 26510297Ssam } 26610297Ssam } 26710297Ssam if (nmatches > 1) 26810297Ssam return ((struct cmd *)-1); 26910297Ssam return (found); 27010297Ssam } 27110297Ssam 27210297Ssam /* 27310297Ssam * Slice a string up into argc/argv. 27410297Ssam */ 27526047Sminshall 27626047Sminshall int slrflag; 27726047Sminshall 27810297Ssam makeargv() 27910297Ssam { 28010297Ssam char **argp; 28110297Ssam char *slurpstring(); 28210297Ssam 28310297Ssam margc = 0; 28410297Ssam argp = margv; 28510297Ssam stringbase = line; /* scan from first of buffer */ 28610297Ssam argbase = argbuf; /* store from first of buffer */ 28726047Sminshall slrflag = 0; 28826047Sminshall while (*argp++ = slurpstring()) 28910297Ssam margc++; 29010297Ssam } 29110297Ssam 29210297Ssam /* 29310297Ssam * Parse string into argbuf; 29410297Ssam * implemented with FSM to 29510297Ssam * handle quoting and strings 29610297Ssam */ 29710297Ssam char * 29810297Ssam slurpstring() 29910297Ssam { 30010297Ssam int got_one = 0; 30110297Ssam register char *sb = stringbase; 30210297Ssam register char *ap = argbase; 30310297Ssam char *tmp = argbase; /* will return this if token found */ 30410297Ssam 30526047Sminshall if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */ 30626047Sminshall switch (slrflag) { /* and $ as token for macro invoke */ 30726047Sminshall case 0: 30826047Sminshall slrflag++; 30926047Sminshall stringbase++; 31026047Sminshall return ((*sb == '!') ? "!" : "$"); 31126047Sminshall break; 31226047Sminshall case 1: 31326047Sminshall slrflag++; 31426047Sminshall altarg = stringbase; 31526047Sminshall break; 31626047Sminshall default: 31726047Sminshall break; 31826047Sminshall } 31926047Sminshall } 32026047Sminshall 32110297Ssam S0: 32210297Ssam switch (*sb) { 32310297Ssam 32410297Ssam case '\0': 32510297Ssam goto OUT; 32610297Ssam 32710297Ssam case ' ': 32810297Ssam case '\t': 32910297Ssam sb++; goto S0; 33010297Ssam 33110297Ssam default: 33226047Sminshall switch (slrflag) { 33326047Sminshall case 0: 33426047Sminshall slrflag++; 33526047Sminshall break; 33626047Sminshall case 1: 33726047Sminshall slrflag++; 33826047Sminshall altarg = sb; 33926047Sminshall break; 34026047Sminshall default: 34126047Sminshall break; 34226047Sminshall } 34310297Ssam goto S1; 34410297Ssam } 34510297Ssam 34610297Ssam S1: 34710297Ssam switch (*sb) { 34810297Ssam 34910297Ssam case ' ': 35010297Ssam case '\t': 35110297Ssam case '\0': 35210297Ssam goto OUT; /* end of token */ 35310297Ssam 35410297Ssam case '\\': 35510297Ssam sb++; goto S2; /* slurp next character */ 35610297Ssam 35710297Ssam case '"': 35810297Ssam sb++; goto S3; /* slurp quoted string */ 35910297Ssam 36010297Ssam default: 36110297Ssam *ap++ = *sb++; /* add character to token */ 36210297Ssam got_one = 1; 36310297Ssam goto S1; 36410297Ssam } 36510297Ssam 36610297Ssam S2: 36710297Ssam switch (*sb) { 36810297Ssam 36910297Ssam case '\0': 37010297Ssam goto OUT; 37110297Ssam 37210297Ssam default: 37310297Ssam *ap++ = *sb++; 37410297Ssam got_one = 1; 37510297Ssam goto S1; 37610297Ssam } 37710297Ssam 37810297Ssam S3: 37910297Ssam switch (*sb) { 38010297Ssam 38110297Ssam case '\0': 38210297Ssam goto OUT; 38310297Ssam 38410297Ssam case '"': 38510297Ssam sb++; goto S1; 38610297Ssam 38710297Ssam default: 38810297Ssam *ap++ = *sb++; 38910297Ssam got_one = 1; 39010297Ssam goto S3; 39110297Ssam } 39210297Ssam 39310297Ssam OUT: 39410297Ssam if (got_one) 39510297Ssam *ap++ = '\0'; 39610297Ssam argbase = ap; /* update storage pointer */ 39710297Ssam stringbase = sb; /* update scan pointer */ 39826047Sminshall if (got_one) { 39910297Ssam return(tmp); 40026047Sminshall } 40126047Sminshall switch (slrflag) { 40226047Sminshall case 0: 40326047Sminshall slrflag++; 40426047Sminshall break; 40526047Sminshall case 1: 40626047Sminshall slrflag++; 40726047Sminshall altarg = (char *) 0; 40826047Sminshall break; 40926047Sminshall default: 41026047Sminshall break; 41126047Sminshall } 41210297Ssam return((char *)0); 41310297Ssam } 41410297Ssam 41510297Ssam #define HELPINDENT (sizeof ("directory")) 41610297Ssam 41710297Ssam /* 41810297Ssam * Help command. 41910297Ssam * Call each command handler with argc == 0 and argv[0] == name. 42010297Ssam */ 42110297Ssam help(argc, argv) 42210297Ssam int argc; 42310297Ssam char *argv[]; 42410297Ssam { 42510297Ssam register struct cmd *c; 42610297Ssam 42710297Ssam if (argc == 1) { 42826047Sminshall register int i, j, w, k; 42910297Ssam int columns, width = 0, lines; 43010297Ssam extern int NCMDS; 43110297Ssam 43210297Ssam printf("Commands may be abbreviated. Commands are:\n\n"); 43310297Ssam for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { 43410297Ssam int len = strlen(c->c_name); 43510297Ssam 43610297Ssam if (len > width) 43710297Ssam width = len; 43810297Ssam } 43910297Ssam width = (width + 8) &~ 7; 44010297Ssam columns = 80 / width; 44110297Ssam if (columns == 0) 44210297Ssam columns = 1; 44310297Ssam lines = (NCMDS + columns - 1) / columns; 44410297Ssam for (i = 0; i < lines; i++) { 44510297Ssam for (j = 0; j < columns; j++) { 44610297Ssam c = cmdtab + j * lines + i; 44726047Sminshall if (c->c_name && (!proxy || c->c_proxy)) { 44825907Smckusick printf("%s", c->c_name); 44926047Sminshall } 45026047Sminshall else if (c->c_name) { 45126047Sminshall for (k=0; k < strlen(c->c_name); k++) { 45226047Sminshall putchar(' '); 45326047Sminshall } 45426047Sminshall } 45510297Ssam if (c + lines >= &cmdtab[NCMDS]) { 45610297Ssam printf("\n"); 45710297Ssam break; 45810297Ssam } 45910297Ssam w = strlen(c->c_name); 46010297Ssam while (w < width) { 46110297Ssam w = (w + 8) &~ 7; 46210297Ssam putchar('\t'); 46310297Ssam } 46410297Ssam } 46510297Ssam } 46610297Ssam return; 46710297Ssam } 46810297Ssam while (--argc > 0) { 46910297Ssam register char *arg; 47010297Ssam arg = *++argv; 47110297Ssam c = getcmd(arg); 47210297Ssam if (c == (struct cmd *)-1) 47310297Ssam printf("?Ambiguous help command %s\n", arg); 47410297Ssam else if (c == (struct cmd *)0) 47510297Ssam printf("?Invalid help command %s\n", arg); 47610297Ssam else 47710297Ssam printf("%-*s\t%s\n", HELPINDENT, 47810297Ssam c->c_name, c->c_help); 47910297Ssam } 48010297Ssam } 48110297Ssam 48210297Ssam /* 48310297Ssam * Call routine with argc, argv set from args (terminated by 0). 48410297Ssam */ 48510297Ssam /* VARARGS2 */ 48610297Ssam call(routine, args) 48710297Ssam int (*routine)(); 48810297Ssam int args; 48910297Ssam { 49010297Ssam register int *argp; 49110297Ssam register int argc; 49210297Ssam 49310297Ssam for (argc = 0, argp = &args; *argp++ != 0; argc++) 49410297Ssam ; 49510297Ssam (*routine)(argc, &args); 49610297Ssam } 497