file reorg, pathnames.h, paths.h
[unix-history] / usr / src / old / lex / lmain.c
CommitLineData
a5423a97 1#ifndef lint
dc0e9d50 2static char sccsid[] = "@(#)lmain.c 4.4 (Berkeley) %G%";
a5423a97
SL
3#endif
4
dc0e9d50 5# include "pathnames.h"
a5423a97
SL
6# include "ldefs.c"
7# include "once.c"
8
9 /* lex [-[drcyvntf]] [file] ... [file] */
10
11 /* Copyright 1976, Bell Telephone Laboratories, Inc.,
12 written by Eric Schmidt, August 27, 1976 */
13
14main(argc,argv)
15 int argc;
16 char **argv; {
17 register int i;
18# ifdef DEBUG
19#include <signal.h>
20 signal(SIGBUS,buserr);
21 signal(SIGSEGV,segviol);
22# endif
23 while (argc > 1 && argv[1][0] == '-' ){
24 i = 0;
25 while(argv[1][++i]){
26 switch (argv[1][i]){
27# ifdef DEBUG
28 case 'd': debug++; break;
29 case 'y': yydebug = TRUE; break;
30# endif
31 case 'r': case 'R':
32 ratfor=TRUE; break;
33 case 'c': case 'C':
34 ratfor=FALSE; break;
35 case 't': case 'T':
36 fout = stdout;
37 errorf = stderr;
38 break;
39 case 'v': case 'V':
40 report = 1;
41 break;
42 case 'f': case 'F':
43 optim = FALSE;
44 break;
45 case 'n': case 'N':
46 report = 0;
47 break;
48 default:
49 warning("Unknown option %c",argv[1][i]);
50 }
51 }
52 argc--;
53 argv++;
54 }
55 sargc = argc;
56 sargv = argv;
57 if (argc > 1){
58 fin = fopen(argv[++fptr], "r"); /* open argv[1] */
59 sargc--;
a5423a97
SL
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
dc0e9d50 104 if (ZCH>NCH) cname=_PATH_EBCFORM;
a5423a97
SL
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 }
122get1core(){
123 register int i, val;
124 register char *p;
125ccptr = ccl = myalloc(CCLSIZE,sizeof(*ccl));
126pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
3d47cc8b
RC
127 def = (char **) myalloc(DEFSIZE,sizeof(*def));
128 subs = (char **) myalloc(DEFSIZE,sizeof(*subs));
a5423a97 129dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
3d47cc8b 130 sname = (char **) myalloc(STARTSIZE,sizeof(*sname));
a5423a97
SL
131sp = 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 }
135free1core(){
136 cfree(def,DEFSIZE,sizeof(*def));
137 cfree(subs,DEFSIZE,sizeof(*subs));
138 cfree(dchar,DEFCHAR,sizeof(*dchar));
139 }
140get2core(){
141 register int i, val;
142 register char *p;
3d47cc8b
RC
143 gotof = (int *) myalloc(nstates,sizeof(*gotof));
144 nexts = (int *) myalloc(ntrans,sizeof(*nexts));
a5423a97 145 nchar = myalloc(ntrans,sizeof(*nchar));
3d47cc8b
RC
146 state = (int **) myalloc(nstates,sizeof(*state));
147 atable = (int *) myalloc(nstates,sizeof(*atable));
148 sfall = (int *) myalloc(nstates,sizeof(*sfall));
a5423a97
SL
149 cpackflg = myalloc(nstates,sizeof(*cpackflg));
150 tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
3d47cc8b
RC
151 foll = (int **) myalloc(tptr+1,sizeof(*foll));
152nxtpos = positions = (int *) myalloc(maxpos,sizeof(*positions));
a5423a97
SL
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 }
158free2core(){
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 }
172get3core(){
173 register int i, val;
174 register char *p;
3d47cc8b
RC
175 verify = (int *) myalloc(outsize,sizeof(*verify));
176 advance = (int *) myalloc(outsize,sizeof(*advance));
177 stoff = (int *) myalloc(stnum+2,sizeof(*stoff));
a5423a97
SL
178 if(verify == 0 || advance == 0 || stoff == 0)
179 error("Too little core for final packing");
180 }
181# ifdef DEBUG
182free3core(){
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
194char *myalloc(a,b)
195 int a,b; {
3d47cc8b 196 register char *i;
a5423a97
SL
197 i = calloc(a, b);
198 if(i==0)
199 warning("OOPS - calloc returns a 0");
3d47cc8b 200 else if(i == (char *)-1){
a5423a97
SL
201# ifdef DEBUG
202 warning("calloc returns a -1");
203# endif
204 return(0);
205 }
206 return(i);
207 }
208# ifdef DEBUG
209buserr(){
210 fflush(errorf);
211 fflush(fout);
212 fflush(stdout);
213 fprintf(errorf,"Bus error\n");
214 if(report == 1)statistics();
215 fflush(errorf);
216 }
217segviol(){
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
227yyerror(s)
228char *s;
229{
5092bf15
S
230 fprintf(stderr, "\"%s\", line %d: %s\n",
231 fptr > 0 ? sargv[fptr] : "<stdin>", yyline, s);
a5423a97 232}