xref: /csrg-svn/old/lex/lmain.c (revision 14490)
1*14490Ssam #ifndef lint
2*14490Ssam static char sccsid[] = "@(#)lmain.c	4.1 (Berkeley) 08/11/83";
3*14490Ssam #endif
4*14490Ssam 
5*14490Ssam # include "ldefs.c"
6*14490Ssam # include "once.c"
7*14490Ssam 
8*14490Ssam 	/* lex [-[drcyvntf]] [file] ... [file] */
9*14490Ssam 
10*14490Ssam 	/* Copyright 1976, Bell Telephone Laboratories, Inc.,
11*14490Ssam 	    written by Eric Schmidt, August 27, 1976   */
12*14490Ssam 
13*14490Ssam main(argc,argv)
14*14490Ssam   int argc;
15*14490Ssam   char **argv; {
16*14490Ssam 	register int i;
17*14490Ssam # ifdef DEBUG
18*14490Ssam #include <signal.h>
19*14490Ssam 	signal(SIGBUS,buserr);
20*14490Ssam 	signal(SIGSEGV,segviol);
21*14490Ssam # endif
22*14490Ssam 	while (argc > 1 && argv[1][0] == '-' ){
23*14490Ssam 		i = 0;
24*14490Ssam 		while(argv[1][++i]){
25*14490Ssam 			switch (argv[1][i]){
26*14490Ssam # ifdef DEBUG
27*14490Ssam 				case 'd': debug++; break;
28*14490Ssam 				case 'y': yydebug = TRUE; break;
29*14490Ssam # endif
30*14490Ssam 				case 'r': case 'R':
31*14490Ssam 					ratfor=TRUE; break;
32*14490Ssam 				case 'c': case 'C':
33*14490Ssam 					ratfor=FALSE; break;
34*14490Ssam 				case 't': case 'T':
35*14490Ssam 					fout = stdout;
36*14490Ssam 					errorf = stderr;
37*14490Ssam 					break;
38*14490Ssam 				case 'v': case 'V':
39*14490Ssam 					report = 1;
40*14490Ssam 					break;
41*14490Ssam 				case 'f': case 'F':
42*14490Ssam 					optim = FALSE;
43*14490Ssam 					break;
44*14490Ssam 				case 'n': case 'N':
45*14490Ssam 					report = 0;
46*14490Ssam 					break;
47*14490Ssam 				default:
48*14490Ssam 					warning("Unknown option %c",argv[1][i]);
49*14490Ssam 				}
50*14490Ssam 			}
51*14490Ssam 		argc--;
52*14490Ssam 		argv++;
53*14490Ssam 		}
54*14490Ssam 	sargc = argc;
55*14490Ssam 	sargv = argv;
56*14490Ssam 	if (argc > 1){
57*14490Ssam 		fin = fopen(argv[++fptr], "r");		/* open argv[1] */
58*14490Ssam 		sargc--;
59*14490Ssam 		sargv++;
60*14490Ssam 		}
61*14490Ssam 	else fin = stdin;
62*14490Ssam 	if(fin == NULL)
63*14490Ssam 		error ("Can't read input file %s",argc>1?argv[1]:"standard input");
64*14490Ssam 	gch();
65*14490Ssam 		/* may be gotten: def, subs, sname, schar, ccl, dchar */
66*14490Ssam 	get1core();
67*14490Ssam 		/* may be gotten: name, left, right, nullstr, parent */
68*14490Ssam 	scopy("INITIAL",sp);
69*14490Ssam 	sname[0] = sp;
70*14490Ssam 	sp += slength("INITIAL") + 1;
71*14490Ssam 	sname[1] = 0;
72*14490Ssam 	if(yyparse(0)) exit(1);	/* error return code */
73*14490Ssam 		/* may be disposed of: def, subs, dchar */
74*14490Ssam 	free1core();
75*14490Ssam 		/* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
76*14490Ssam 	get2core();
77*14490Ssam 	ptail();
78*14490Ssam 	mkmatch();
79*14490Ssam # ifdef DEBUG
80*14490Ssam 	if(debug) pccl();
81*14490Ssam # endif
82*14490Ssam 	sect  = ENDSECTION;
83*14490Ssam 	if(tptr>0)cfoll(tptr-1);
84*14490Ssam # ifdef DEBUG
85*14490Ssam 	if(debug)pfoll();
86*14490Ssam # endif
87*14490Ssam 	cgoto();
88*14490Ssam # ifdef DEBUG
89*14490Ssam 	if(debug){
90*14490Ssam 		printf("Print %d states:\n",stnum+1);
91*14490Ssam 		for(i=0;i<=stnum;i++)stprt(i);
92*14490Ssam 		}
93*14490Ssam # endif
94*14490Ssam 		/* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, schar, sname */
95*14490Ssam 		/* may be gotten: verify, advance, stoff */
96*14490Ssam 	free2core();
97*14490Ssam 	get3core();
98*14490Ssam 	layout();
99*14490Ssam 		/* may be disposed of: verify, advance, stoff, nexts, nchar,
100*14490Ssam 			gotof, atable, ccpackflg, sfall */
101*14490Ssam # ifdef DEBUG
102*14490Ssam 	free3core();
103*14490Ssam # endif
104*14490Ssam 	if (ZCH>NCH) cname="/usr/lib/lex/ebcform";
105*14490Ssam 	fother = fopen(ratfor?ratname:cname,"r");
106*14490Ssam 	if(fother == NULL)
107*14490Ssam 		error("Lex driver missing, file %s",ratfor?ratname:cname);
108*14490Ssam 	while ( (i=getc(fother)) != EOF)
109*14490Ssam 		putc(i,fout);
110*14490Ssam 
111*14490Ssam 	fclose(fother);
112*14490Ssam 	fclose(fout);
113*14490Ssam 	if(
114*14490Ssam # ifdef DEBUG
115*14490Ssam 		debug   ||
116*14490Ssam # endif
117*14490Ssam 			report == 1)statistics();
118*14490Ssam 	fclose(stdout);
119*14490Ssam 	fclose(stderr);
120*14490Ssam 	exit(0);	/* success return code */
121*14490Ssam 	}
122*14490Ssam get1core(){
123*14490Ssam 	register int i, val;
124*14490Ssam 	register char *p;
125*14490Ssam ccptr =	ccl = myalloc(CCLSIZE,sizeof(*ccl));
126*14490Ssam pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
127*14490Ssam 	def = myalloc(DEFSIZE,sizeof(*def));
128*14490Ssam 	subs = myalloc(DEFSIZE,sizeof(*subs));
129*14490Ssam dp =	dchar = myalloc(DEFCHAR,sizeof(*dchar));
130*14490Ssam 	sname = myalloc(STARTSIZE,sizeof(*sname));
131*14490Ssam sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
132*14490Ssam 	if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
133*14490Ssam 		error("Too little core to begin");
134*14490Ssam 	}
135*14490Ssam free1core(){
136*14490Ssam 	cfree(def,DEFSIZE,sizeof(*def));
137*14490Ssam 	cfree(subs,DEFSIZE,sizeof(*subs));
138*14490Ssam 	cfree(dchar,DEFCHAR,sizeof(*dchar));
139*14490Ssam 	}
140*14490Ssam get2core(){
141*14490Ssam 	register int i, val;
142*14490Ssam 	register char *p;
143*14490Ssam 	gotof = myalloc(nstates,sizeof(*gotof));
144*14490Ssam 	nexts = myalloc(ntrans,sizeof(*nexts));
145*14490Ssam 	nchar = myalloc(ntrans,sizeof(*nchar));
146*14490Ssam 	state = myalloc(nstates,sizeof(*state));
147*14490Ssam 	atable = myalloc(nstates,sizeof(*atable));
148*14490Ssam 	sfall = myalloc(nstates,sizeof(*sfall));
149*14490Ssam 	cpackflg = myalloc(nstates,sizeof(*cpackflg));
150*14490Ssam 	tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
151*14490Ssam 	foll = myalloc(tptr+1,sizeof(*foll));
152*14490Ssam nxtpos = positions = myalloc(maxpos,sizeof(*positions));
153*14490Ssam 	if(tmpstat == 0 || foll == 0 || positions == 0 ||
154*14490Ssam 		gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 )
155*14490Ssam 		error("Too little core for state generation");
156*14490Ssam 	for(i=0;i<=tptr;i++)foll[i] = 0;
157*14490Ssam 	}
158*14490Ssam free2core(){
159*14490Ssam 	cfree(positions,maxpos,sizeof(*positions));
160*14490Ssam 	cfree(tmpstat,tptr+1,sizeof(*tmpstat));
161*14490Ssam 	cfree(foll,tptr+1,sizeof(*foll));
162*14490Ssam 	cfree(name,treesize,sizeof(*name));
163*14490Ssam 	cfree(left,treesize,sizeof(*left));
164*14490Ssam 	cfree(right,treesize,sizeof(*right));
165*14490Ssam 	cfree(parent,treesize,sizeof(*parent));
166*14490Ssam 	cfree(nullstr,treesize,sizeof(*nullstr));
167*14490Ssam 	cfree(state,nstates,sizeof(*state));
168*14490Ssam 	cfree(sname,STARTSIZE,sizeof(*sname));
169*14490Ssam 	cfree(schar,STARTCHAR,sizeof(*schar));
170*14490Ssam 	cfree(ccl,CCLSIZE,sizeof(*ccl));
171*14490Ssam 	}
172*14490Ssam get3core(){
173*14490Ssam 	register int i, val;
174*14490Ssam 	register char *p;
175*14490Ssam 	verify = myalloc(outsize,sizeof(*verify));
176*14490Ssam 	advance = myalloc(outsize,sizeof(*advance));
177*14490Ssam 	stoff = myalloc(stnum+2,sizeof(*stoff));
178*14490Ssam 	if(verify == 0 || advance == 0 || stoff == 0)
179*14490Ssam 		error("Too little core for final packing");
180*14490Ssam 	}
181*14490Ssam # ifdef DEBUG
182*14490Ssam free3core(){
183*14490Ssam 	cfree(advance,outsize,sizeof(*advance));
184*14490Ssam 	cfree(verify,outsize,sizeof(*verify));
185*14490Ssam 	cfree(stoff,stnum+1,sizeof(*stoff));
186*14490Ssam 	cfree(gotof,nstates,sizeof(*gotof));
187*14490Ssam 	cfree(nexts,ntrans,sizeof(*nexts));
188*14490Ssam 	cfree(nchar,ntrans,sizeof(*nchar));
189*14490Ssam 	cfree(atable,nstates,sizeof(*atable));
190*14490Ssam 	cfree(sfall,nstates,sizeof(*sfall));
191*14490Ssam 	cfree(cpackflg,nstates,sizeof(*cpackflg));
192*14490Ssam 	}
193*14490Ssam # endif
194*14490Ssam char *myalloc(a,b)
195*14490Ssam   int a,b; {
196*14490Ssam 	register int i;
197*14490Ssam 	i = calloc(a, b);
198*14490Ssam 	if(i==0)
199*14490Ssam 		warning("OOPS - calloc returns a 0");
200*14490Ssam 	else if(i == -1){
201*14490Ssam # ifdef DEBUG
202*14490Ssam 		warning("calloc returns a -1");
203*14490Ssam # endif
204*14490Ssam 		return(0);
205*14490Ssam 		}
206*14490Ssam 	return(i);
207*14490Ssam 	}
208*14490Ssam # ifdef DEBUG
209*14490Ssam buserr(){
210*14490Ssam 	fflush(errorf);
211*14490Ssam 	fflush(fout);
212*14490Ssam 	fflush(stdout);
213*14490Ssam 	fprintf(errorf,"Bus error\n");
214*14490Ssam 	if(report == 1)statistics();
215*14490Ssam 	fflush(errorf);
216*14490Ssam 	}
217*14490Ssam segviol(){
218*14490Ssam 	fflush(errorf);
219*14490Ssam 	fflush(fout);
220*14490Ssam 	fflush(stdout);
221*14490Ssam 	fprintf(errorf,"Segmentation violation\n");
222*14490Ssam 	if(report == 1)statistics();
223*14490Ssam 	fflush(errorf);
224*14490Ssam 	}
225*14490Ssam # endif
226*14490Ssam 
227*14490Ssam yyerror(s)
228*14490Ssam char *s;
229*14490Ssam {
230*14490Ssam 	fprintf(stderr, "%s\n", s);
231*14490Ssam }
232