114490Ssam #ifndef lint 2*37803Sbostic static char sccsid[] = "@(#)lmain.c 4.4 (Berkeley) 05/10/89"; 314490Ssam #endif 414490Ssam 5*37803Sbostic # include "pathnames.h" 614490Ssam # include "ldefs.c" 714490Ssam # include "once.c" 814490Ssam 914490Ssam /* lex [-[drcyvntf]] [file] ... [file] */ 1014490Ssam 1114490Ssam /* Copyright 1976, Bell Telephone Laboratories, Inc., 1214490Ssam written by Eric Schmidt, August 27, 1976 */ 1314490Ssam 1414490Ssam main(argc,argv) 1514490Ssam int argc; 1614490Ssam char **argv; { 1714490Ssam register int i; 1814490Ssam # ifdef DEBUG 1914490Ssam #include <signal.h> 2014490Ssam signal(SIGBUS,buserr); 2114490Ssam signal(SIGSEGV,segviol); 2214490Ssam # endif 2314490Ssam while (argc > 1 && argv[1][0] == '-' ){ 2414490Ssam i = 0; 2514490Ssam while(argv[1][++i]){ 2614490Ssam switch (argv[1][i]){ 2714490Ssam # ifdef DEBUG 2814490Ssam case 'd': debug++; break; 2914490Ssam case 'y': yydebug = TRUE; break; 3014490Ssam # endif 3114490Ssam case 'r': case 'R': 3214490Ssam ratfor=TRUE; break; 3314490Ssam case 'c': case 'C': 3414490Ssam ratfor=FALSE; break; 3514490Ssam case 't': case 'T': 3614490Ssam fout = stdout; 3714490Ssam errorf = stderr; 3814490Ssam break; 3914490Ssam case 'v': case 'V': 4014490Ssam report = 1; 4114490Ssam break; 4214490Ssam case 'f': case 'F': 4314490Ssam optim = FALSE; 4414490Ssam break; 4514490Ssam case 'n': case 'N': 4614490Ssam report = 0; 4714490Ssam break; 4814490Ssam default: 4914490Ssam warning("Unknown option %c",argv[1][i]); 5014490Ssam } 5114490Ssam } 5214490Ssam argc--; 5314490Ssam argv++; 5414490Ssam } 5514490Ssam sargc = argc; 5614490Ssam sargv = argv; 5714490Ssam if (argc > 1){ 5814490Ssam fin = fopen(argv[++fptr], "r"); /* open argv[1] */ 5914490Ssam sargc--; 6014490Ssam } 6114490Ssam else fin = stdin; 6214490Ssam if(fin == NULL) 6314490Ssam error ("Can't read input file %s",argc>1?argv[1]:"standard input"); 6414490Ssam gch(); 6514490Ssam /* may be gotten: def, subs, sname, schar, ccl, dchar */ 6614490Ssam get1core(); 6714490Ssam /* may be gotten: name, left, right, nullstr, parent */ 6814490Ssam scopy("INITIAL",sp); 6914490Ssam sname[0] = sp; 7014490Ssam sp += slength("INITIAL") + 1; 7114490Ssam sname[1] = 0; 7214490Ssam if(yyparse(0)) exit(1); /* error return code */ 7314490Ssam /* may be disposed of: def, subs, dchar */ 7414490Ssam free1core(); 7514490Ssam /* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */ 7614490Ssam get2core(); 7714490Ssam ptail(); 7814490Ssam mkmatch(); 7914490Ssam # ifdef DEBUG 8014490Ssam if(debug) pccl(); 8114490Ssam # endif 8214490Ssam sect = ENDSECTION; 8314490Ssam if(tptr>0)cfoll(tptr-1); 8414490Ssam # ifdef DEBUG 8514490Ssam if(debug)pfoll(); 8614490Ssam # endif 8714490Ssam cgoto(); 8814490Ssam # ifdef DEBUG 8914490Ssam if(debug){ 9014490Ssam printf("Print %d states:\n",stnum+1); 9114490Ssam for(i=0;i<=stnum;i++)stprt(i); 9214490Ssam } 9314490Ssam # endif 9414490Ssam /* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, schar, sname */ 9514490Ssam /* may be gotten: verify, advance, stoff */ 9614490Ssam free2core(); 9714490Ssam get3core(); 9814490Ssam layout(); 9914490Ssam /* may be disposed of: verify, advance, stoff, nexts, nchar, 10014490Ssam gotof, atable, ccpackflg, sfall */ 10114490Ssam # ifdef DEBUG 10214490Ssam free3core(); 10314490Ssam # endif 104*37803Sbostic if (ZCH>NCH) cname=_PATH_EBCFORM; 10514490Ssam fother = fopen(ratfor?ratname:cname,"r"); 10614490Ssam if(fother == NULL) 10714490Ssam error("Lex driver missing, file %s",ratfor?ratname:cname); 10814490Ssam while ( (i=getc(fother)) != EOF) 10914490Ssam putc(i,fout); 11014490Ssam 11114490Ssam fclose(fother); 11214490Ssam fclose(fout); 11314490Ssam if( 11414490Ssam # ifdef DEBUG 11514490Ssam debug || 11614490Ssam # endif 11714490Ssam report == 1)statistics(); 11814490Ssam fclose(stdout); 11914490Ssam fclose(stderr); 12014490Ssam exit(0); /* success return code */ 12114490Ssam } 12214490Ssam get1core(){ 12314490Ssam register int i, val; 12414490Ssam register char *p; 12514490Ssam ccptr = ccl = myalloc(CCLSIZE,sizeof(*ccl)); 12614490Ssam pcptr = pchar = myalloc(pchlen, sizeof(*pchar)); 12717672Sralph def = (char **) myalloc(DEFSIZE,sizeof(*def)); 12817672Sralph subs = (char **) myalloc(DEFSIZE,sizeof(*subs)); 12914490Ssam dp = dchar = myalloc(DEFCHAR,sizeof(*dchar)); 13017672Sralph sname = (char **) myalloc(STARTSIZE,sizeof(*sname)); 13114490Ssam sp = schar = myalloc(STARTCHAR,sizeof(*schar)); 13214490Ssam if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0) 13314490Ssam error("Too little core to begin"); 13414490Ssam } 13514490Ssam free1core(){ 13614490Ssam cfree(def,DEFSIZE,sizeof(*def)); 13714490Ssam cfree(subs,DEFSIZE,sizeof(*subs)); 13814490Ssam cfree(dchar,DEFCHAR,sizeof(*dchar)); 13914490Ssam } 14014490Ssam get2core(){ 14114490Ssam register int i, val; 14214490Ssam register char *p; 14317672Sralph gotof = (int *) myalloc(nstates,sizeof(*gotof)); 14417672Sralph nexts = (int *) myalloc(ntrans,sizeof(*nexts)); 14514490Ssam nchar = myalloc(ntrans,sizeof(*nchar)); 14617672Sralph state = (int **) myalloc(nstates,sizeof(*state)); 14717672Sralph atable = (int *) myalloc(nstates,sizeof(*atable)); 14817672Sralph sfall = (int *) myalloc(nstates,sizeof(*sfall)); 14914490Ssam cpackflg = myalloc(nstates,sizeof(*cpackflg)); 15014490Ssam tmpstat = myalloc(tptr+1,sizeof(*tmpstat)); 15117672Sralph foll = (int **) myalloc(tptr+1,sizeof(*foll)); 15217672Sralph nxtpos = positions = (int *) myalloc(maxpos,sizeof(*positions)); 15314490Ssam if(tmpstat == 0 || foll == 0 || positions == 0 || 15414490Ssam gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 ) 15514490Ssam error("Too little core for state generation"); 15614490Ssam for(i=0;i<=tptr;i++)foll[i] = 0; 15714490Ssam } 15814490Ssam free2core(){ 15914490Ssam cfree(positions,maxpos,sizeof(*positions)); 16014490Ssam cfree(tmpstat,tptr+1,sizeof(*tmpstat)); 16114490Ssam cfree(foll,tptr+1,sizeof(*foll)); 16214490Ssam cfree(name,treesize,sizeof(*name)); 16314490Ssam cfree(left,treesize,sizeof(*left)); 16414490Ssam cfree(right,treesize,sizeof(*right)); 16514490Ssam cfree(parent,treesize,sizeof(*parent)); 16614490Ssam cfree(nullstr,treesize,sizeof(*nullstr)); 16714490Ssam cfree(state,nstates,sizeof(*state)); 16814490Ssam cfree(sname,STARTSIZE,sizeof(*sname)); 16914490Ssam cfree(schar,STARTCHAR,sizeof(*schar)); 17014490Ssam cfree(ccl,CCLSIZE,sizeof(*ccl)); 17114490Ssam } 17214490Ssam get3core(){ 17314490Ssam register int i, val; 17414490Ssam register char *p; 17517672Sralph verify = (int *) myalloc(outsize,sizeof(*verify)); 17617672Sralph advance = (int *) myalloc(outsize,sizeof(*advance)); 17717672Sralph stoff = (int *) myalloc(stnum+2,sizeof(*stoff)); 17814490Ssam if(verify == 0 || advance == 0 || stoff == 0) 17914490Ssam error("Too little core for final packing"); 18014490Ssam } 18114490Ssam # ifdef DEBUG 18214490Ssam free3core(){ 18314490Ssam cfree(advance,outsize,sizeof(*advance)); 18414490Ssam cfree(verify,outsize,sizeof(*verify)); 18514490Ssam cfree(stoff,stnum+1,sizeof(*stoff)); 18614490Ssam cfree(gotof,nstates,sizeof(*gotof)); 18714490Ssam cfree(nexts,ntrans,sizeof(*nexts)); 18814490Ssam cfree(nchar,ntrans,sizeof(*nchar)); 18914490Ssam cfree(atable,nstates,sizeof(*atable)); 19014490Ssam cfree(sfall,nstates,sizeof(*sfall)); 19114490Ssam cfree(cpackflg,nstates,sizeof(*cpackflg)); 19214490Ssam } 19314490Ssam # endif 19414490Ssam char *myalloc(a,b) 19514490Ssam int a,b; { 19617672Sralph register char *i; 19714490Ssam i = calloc(a, b); 19814490Ssam if(i==0) 19914490Ssam warning("OOPS - calloc returns a 0"); 20017672Sralph else if(i == (char *)-1){ 20114490Ssam # ifdef DEBUG 20214490Ssam warning("calloc returns a -1"); 20314490Ssam # endif 20414490Ssam return(0); 20514490Ssam } 20614490Ssam return(i); 20714490Ssam } 20814490Ssam # ifdef DEBUG 20914490Ssam buserr(){ 21014490Ssam fflush(errorf); 21114490Ssam fflush(fout); 21214490Ssam fflush(stdout); 21314490Ssam fprintf(errorf,"Bus error\n"); 21414490Ssam if(report == 1)statistics(); 21514490Ssam fflush(errorf); 21614490Ssam } 21714490Ssam segviol(){ 21814490Ssam fflush(errorf); 21914490Ssam fflush(fout); 22014490Ssam fflush(stdout); 22114490Ssam fprintf(errorf,"Segmentation violation\n"); 22214490Ssam if(report == 1)statistics(); 22314490Ssam fflush(errorf); 22414490Ssam } 22514490Ssam # endif 22614490Ssam 22714490Ssam yyerror(s) 22814490Ssam char *s; 22914490Ssam { 23018034Sserge fprintf(stderr, "\"%s\", line %d: %s\n", 23118034Sserge fptr > 0 ? sargv[fptr] : "<stdin>", yyline, s); 23214490Ssam } 233