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