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