1: #include "stdio.h" 2: #include "awk.def" 3: #include "awk.h" 4: 5: int dbg = 0; 6: int svargc; 7: char **svargv, **xargv; 8: extern FILE *yyin; /* lex input file */ 9: char *lexprog; /* points to program argument if it exists */ 10: extern errorflag; /* non-zero if any syntax errors; set by yyerror */ 11: 12: int filefd, iflag, symnum, ansfd; 13: char *filelist; 14: extern int maxsym, errno; 15: main(argc, argv) int argc; char *argv[]; { 16: if (argc == 1) 17: error(FATAL, "Usage: awk [-f source | 'cmds'] [files]"); 18: if (strcmp(argv[0], "a.out")) 19: logit(argc, argv); 20: syminit(); 21: while (argc > 1) { 22: argc--; 23: argv++; 24: if (argv[0][0] == '-' && argv[0][1] == 'f') { 25: yyin = fopen(argv[1], "r"); 26: if (yyin == NULL) 27: error(FATAL, "can't open %s", argv[1]); 28: argc--; 29: argv++; 30: break; 31: } else if (argv[0][0] == '-' && argv[0][1] == 'F') { /* set field sep */ 32: if (argv[0][2] == 't') /* special case for tab */ 33: **FS = '\t'; 34: else 35: **FS = argv[0][2]; 36: continue; 37: } else if (argv[0][0] != '-') { 38: dprintf("cmds=|%s|\n", argv[0], NULL, NULL); 39: yyin = NULL; 40: lexprog = argv[0]; 41: argv[0] = argv[-1]; /* need this space */ 42: break; 43: } else if (strcmp("-d", argv[0])==0) 44: dbg = 1; 45: else if (argv[0][0]=='-' && argv[0][1]=='i') { 46: iflag=1; 47: sscanf(argv[0], "-i%d, %d", &filefd, &ansfd); 48: } 49: } 50: if (argc <= 1 && !iflag) { 51: argv[0][0] = '-'; 52: argv[0][1] = '\0'; 53: argc++; 54: argv--; 55: } 56: if (!iflag) { 57: svargc = --argc; 58: svargv = ++argv; 59: dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0], NULL); 60: } 61: *FILENAME = *svargv; /* initial file name */ 62: iloop: 63: if (iflag) 64: msgfiles(); 65: yyparse(); 66: dprintf("errorflag=%d\n", errorflag, NULL, NULL); 67: if (errorflag) 68: exit(0); 69: run(); 70: if (iflag) 71: write(ansfd, &errorflag, sizeof(errorflag)); 72: else exit(errorflag); 73: /*sym cleanup should go here , followed by another syminit*/ 74: goto iloop; 75: } 76: 77: logit(n, s) char *s[]; 78: { int i, tvec[2]; 79: FILE *f, *g; 80: char buf[512]; 81: if ((f=fopen("/usr/pjw/awk/awkhist", "a"))==NULL) 82: return; 83: time(tvec); 84: fprintf(f, "%-8s %s", getlogin(), ctime(tvec)); 85: for (i=0; i<n; i++) 86: fprintf(f, "'%s'", s[i]); 87: putc('\n', f); 88: if (strcmp(s[1], "-f")) { 89: fclose(f); 90: return; 91: } 92: if ((g=fopen(s[2], "r"))==NULL) { 93: fclose(f); 94: return; 95: } 96: while ((i=fread(buf, 1, 512, g))>0) 97: fwrite(buf, 1, i, f); 98: fclose(f); 99: fclose(g); 100: } 101: 102: yywrap() 103: { 104: return(1); 105: } 106: 107: msgfiles() 108: { char buf[512], *p, *q, **s; 109: int n; 110: n=read(filefd, buf, 512); 111: if (n<=0) /*no one at other end?*/ { 112: perror("no files"); 113: exit(errno); 114: } 115: xfree(filelist); 116: q=filelist=malloc(n); 117: for (p=buf; *p==' ' || *p=='\t' || *p=='\n'; p++); 118: for (n=0; *p!=';'; ) 119: { 120: if (*p==' ' || *p=='\t' || *p=='\n') { 121: n++; 122: *q++=0; 123: while (*p==' ' || *p=='\t' || *p=='\n') 124: p++; 125: } 126: else *q++ = *p++; 127: } 128: if (q!=filelist && *(q-1)!=0) { 129: n++; 130: *q++ = 0; 131: } 132: svargc=n; 133: xfree(xargv); 134: xargv=s=svargv=malloc(n*sizeof(char *)); 135: for (p=filelist; n>0; n--) 136: { 137: *s++=p; 138: while (*p++ != 0); 139: } 140: }