xref: /csrg-svn/old/lex/lmain.c (revision 43617)
114490Ssam #ifndef lint
2*43617Sbostic static char sccsid[] = "@(#)lmain.c	4.5 (Berkeley) 06/24/90";
314490Ssam #endif
414490Ssam 
537803Sbostic # 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 
main(argc,argv)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
10437803Sbostic 	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 	}
get1core()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 	}
free1core()13514490Ssam free1core(){
136*43617Sbostic 	free(def);
137*43617Sbostic 	free(subs);
138*43617Sbostic 	free(dchar);
13914490Ssam 	}
get2core()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 	}
free2core()15814490Ssam free2core(){
159*43617Sbostic 	free(positions);
160*43617Sbostic 	free(tmpstat);
161*43617Sbostic 	free(foll);
162*43617Sbostic 	free(name);
163*43617Sbostic 	free(left);
164*43617Sbostic 	free(right);
165*43617Sbostic 	free(parent);
166*43617Sbostic 	free(nullstr);
167*43617Sbostic 	free(state);
168*43617Sbostic 	free(sname);
169*43617Sbostic 	free(schar);
170*43617Sbostic 	free(ccl);
17114490Ssam 	}
get3core()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
free3core()18214490Ssam free3core(){
183*43617Sbostic 	free(advance);
184*43617Sbostic 	free(verify);
185*43617Sbostic 	free(stoff);
186*43617Sbostic 	free(gotof);
187*43617Sbostic 	free(nexts);
188*43617Sbostic 	free(nchar);
189*43617Sbostic 	free(atable);
190*43617Sbostic 	free(sfall);
191*43617Sbostic 	free(cpackflg);
19214490Ssam 	}
19314490Ssam # endif
myalloc(a,b)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
buserr()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 	}
segviol()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 
yyerror(s)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