114473Ssam #ifndef lint 2*17490Ssam static char sccsid[] = "@(#)main.c 4.4 (Berkeley) 12/08/84"; 314473Ssam #endif 46672Smckusick 56672Smckusick #include "stdio.h" 66672Smckusick #include "ctype.h" 76672Smckusick #include "awk.def" 86672Smckusick #include "awk.h" 96672Smckusick #define TOLOWER(c) (isupper(c) ? tolower(c) : c) /* ugh!!! */ 106672Smckusick 116672Smckusick int dbg = 0; 12*17490Ssam int ldbg = 0; 136672Smckusick int svflg = 0; 146672Smckusick int rstflg = 0; 156672Smckusick int svargc; 166672Smckusick char **svargv, **xargv; 176672Smckusick extern FILE *yyin; /* lex input file */ 186672Smckusick char *lexprog; /* points to program argument if it exists */ 196672Smckusick extern errorflag; /* non-zero if any syntax errors; set by yyerror */ 206672Smckusick 216672Smckusick int filefd, symnum, ansfd; 226672Smckusick char *filelist; 236672Smckusick extern int maxsym, errno; 246672Smckusick main(argc, argv) int argc; char *argv[]; { 256672Smckusick if (argc == 1) 266672Smckusick error(FATAL, "Usage: awk [-f source | 'cmds'] [files]"); 276672Smckusick syminit(); 286672Smckusick while (argc > 1) { 296672Smckusick argc--; 306672Smckusick argv++; 316672Smckusick /* this nonsense is because gcos argument handling */ 326672Smckusick /* folds -F into -f. accordingly, one checks the next 336672Smckusick /* character after f to see if it's -f file or -Fx. 346672Smckusick */ 356672Smckusick if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f' && argv[0][2] == '\0') { 366672Smckusick yyin = fopen(argv[1], "r"); 376672Smckusick if (yyin == NULL) 386672Smckusick error(FATAL, "can't open %s", argv[1]); 396672Smckusick argc--; 406672Smckusick argv++; 416672Smckusick break; 426672Smckusick } else if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f') { /* set field sep */ 436672Smckusick if (argv[0][2] == 't') /* special case for tab */ 446672Smckusick **FS = '\t'; 456672Smckusick else 466672Smckusick **FS = argv[0][2]; 476672Smckusick continue; 486672Smckusick } else if (argv[0][0] != '-') { 496672Smckusick dprintf("cmds=|%s|\n", argv[0], NULL, NULL); 506672Smckusick yyin = NULL; 516672Smckusick lexprog = argv[0]; 526672Smckusick argv[0] = argv[-1]; /* need this space */ 536672Smckusick break; 546672Smckusick } else if (strcmp("-d", argv[0])==0) { 556672Smckusick dbg = 1; 566672Smckusick } 57*17490Ssam else if (strcmp("-l", argv[0])==0) { 58*17490Ssam ldbg = 1; 59*17490Ssam } 606672Smckusick else if(strcmp("-S", argv[0]) == 0) { 616672Smckusick svflg = 1; 626672Smckusick } 636672Smckusick else if(strncmp("-R", argv[0], 2) == 0) { 646672Smckusick if(thaw(argv[0] + 2) == 0) 656672Smckusick rstflg = 1; 666672Smckusick else { 676672Smckusick fprintf(stderr, "not restored\n"); 686672Smckusick exit(1); 696672Smckusick } 706672Smckusick } 716672Smckusick } 726672Smckusick if (argc <= 1) { 736672Smckusick argv[0][0] = '-'; 746672Smckusick argv[0][1] = '\0'; 756672Smckusick argc++; 766672Smckusick argv--; 776672Smckusick } 786672Smckusick svargc = --argc; 796672Smckusick svargv = ++argv; 806672Smckusick dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0], NULL); 816672Smckusick *FILENAME = *svargv; /* initial file name */ 826672Smckusick if(rstflg == 0) 836672Smckusick yyparse(); 846672Smckusick dprintf("errorflag=%d\n", errorflag, NULL, NULL); 856672Smckusick if (errorflag) 866672Smckusick exit(errorflag); 876672Smckusick if(svflg) { 886672Smckusick svflg = 0; 896672Smckusick if(freeze("awk.out") != 0) 906672Smckusick fprintf(stderr, "not saved\n"); 916672Smckusick exit(0); 926672Smckusick } 936672Smckusick run(); 946672Smckusick exit(errorflag); 956672Smckusick } 966672Smckusick 976672Smckusick yywrap() 986672Smckusick { 996672Smckusick return(1); 1006672Smckusick } 101