/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: main.c 1.6 89/12/01 $@(#)lex:main.c	1.5"
# include "ldefs.c"
# include "once.c"

	/* lex [-[drcyvntf]] [file] ... [file] */

	/* Copyright 1976, Bell Telephone Laboratories, Inc.,
	    written by Eric Schmidt, August 27, 1976   */

#ifdef CCS
# include "../../tools/compat.h"
extern char *getenv();
static char cname_path[BUFSIZ];
#else
# include <euc.h>
# include <getwidth.h>
#endif

#include <ctype.h>
eucwidth_t   wp;
int   difset1;
int   difset2;
int   difset3;

main(argc,argv)
  int argc;
  char **argv; {
	register int i;
# ifdef DEBUG
	signal(10,buserr);
	signal(11,segviol);
# endif
 
  getwidth(&wp);
  difset1 = wp._eucw1 - wp._scrw1;
  difset2 = wp._eucw2 - wp._scrw2;
  difset3 = wp._eucw3 - wp._scrw3;

	while (argc > 1 && argv[1][0] == '-' ){
		i = 0;
		while(argv[1][++i]){
			switch (argv[1][i]){
# ifdef DEBUG
				case 'd': debug++; break;
				case 'y': yydebug = TRUE; break;
# endif
				case 'r': case 'R':
					warning("Ratfor not currently supported with lex");
					ratfor=TRUE; break;
				case 'c': case 'C':
					ratfor=FALSE; break;
				case 't': case 'T':
					fout = stdout;
					errorf = stderr;
					break;
				case 'v': case 'V':
					report = 1;
					break;
				case 'n': case 'N':
					report = 0;
					break;
				default:
					warning("Unknown option %c",argv[1][i]);
				}
			}
		argc--;
		argv++;
		}
	sargc = argc;
	sargv = argv;
	if (argc > 1){
		fin = fopen(argv[++fptr], "r");		/* open argv[1] */
		sargc--;
		sargv++;
		}
	else fin = stdin;
	if(fin == NULL)
		error ("Can't read input file %s",argc>1?argv[1]:"standard input");
	gch();
		/* may be gotten: def, subs, sname, schar, ccl, dchar */
	get1core();
		/* may be gotten: name, left, right, nullstr, parent */
	scopy("INITIAL",sp);
	sname[0] = sp;
	sp += slength("INITIAL") + 1;
	sname[1] = 0;
	if(yyparse(0)) exit(1);	/* error return code */
		/* may be disposed of: def, subs, dchar */
	free1core();
		/* may be gotten: tmpstat, foll, positions, gotof, nexts, nchar, state, atable, sfall, cpackflg */
	get2core();
	ptail();
	mkmatch();
# ifdef DEBUG
	if(debug) pccl();
# endif
	sect  = ENDSECTION;
	if(tptr>0)cfoll(tptr-1);
# ifdef DEBUG
	if(debug)pfoll();
# endif
	cgoto();
# ifdef DEBUG
	if(debug){
		printf("Print %d states:\n",stnum+1);
		for(i=0;i<=stnum;i++)stprt(i);
		}
# endif
		/* may be disposed of: positions, tmpstat, foll, state, name, left, right, parent, ccl, schar, sname */
		/* may be gotten: verify, advance, stoff */
	free2core();
	get3core();
	layout();
		/* may be disposed of: verify, advance, stoff, nexts, nchar,
			gotof, atable, ccpackflg, sfall */
# ifdef DEBUG
	free3core();
# endif
#ifdef	CCS
#define CFORM	"/usr/lib/lex/ncform"
	if ((cname = getenv("CONTEXT")) == NULL)
		cname = CFORM;
	else {
		strcpy(cname_path, cname);
		strcat(cname_path, "/tmpos/usr/lib/lex/ncform");
		cname = cname_path;
	}
	fother = fopen(cname, "r");
	if (fother == 0) {
		error("Can't find %s using %s instead.\n",cname,CFORM);
		cname = CFORM;
	}
	fclose(fother);
#endif	/* CCS */
	if (ZCH>NCH) cname="/usr/lib/lex/ebcform";
	fother = fopen(ratfor?ratname:cname,"r");
	if(fother == NULL)
		error("Lex driver missing, file %s",ratfor?ratname:cname);
	while ( (i=getc(fother)) != EOF)
		putc(i,fout);

	fclose(fother);
	fclose(fout);
	if(
# ifdef DEBUG
		debug   ||
# endif
			report == 1)statistics();
	fclose(stdout);
	fclose(stderr);
	exit(0);	/* success return code */
	}
get1core(){
	register int i, val;
	register unsigned char *p;
ccptr =	ccl = myalloc(CCLSIZE,sizeof(*ccl));
pcptr = pchar = myalloc(pchlen, sizeof(*pchar));
	def = (unsigned char **)myalloc(DEFSIZE,sizeof(*def));
	subs = (unsigned char **)myalloc(DEFSIZE,sizeof(*subs));
	dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
	sname = (unsigned char **)myalloc(STARTSIZE,sizeof(*sname));
sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
	if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
		error("Too little core to begin");
	}
free1core(){
	cfree((unsigned char *)def,DEFSIZE,sizeof(*def));
	cfree((unsigned char *)subs,DEFSIZE,sizeof(*subs));
	cfree((unsigned char *)dchar,DEFCHAR,sizeof(*dchar));
	}
get2core(){
	register int i, val;
	register unsigned char *p;
	gotof = (int *)myalloc(nstates,sizeof(*gotof));
	nexts = (int *)myalloc(ntrans,sizeof(*nexts));
	nchar = myalloc(ntrans,sizeof(*nchar));
	state = (int **)myalloc(nstates,sizeof(*state));
	atable = (int *)myalloc(nstates,sizeof(*atable));
	sfall = (int *)myalloc(nstates,sizeof(*sfall));
	cpackflg = myalloc(nstates,sizeof(*cpackflg));
	tmpstat = myalloc(tptr+1,sizeof(*tmpstat));
	foll = (int **)myalloc(tptr+1,sizeof(*foll));
	nxtpos = positions = (int *)myalloc(maxpos,sizeof(*positions));
	if(tmpstat == 0 || foll == 0 || positions == 0 ||
		gotof == 0 || nexts == 0 || nchar == 0 || state == 0 || atable == 0 || sfall == 0 || cpackflg == 0 )
		error("Too little core for state generation");
	for(i=0;i<=tptr;i++)foll[i] = 0;
	}
free2core(){
	cfree((unsigned char *)positions,maxpos,sizeof(*positions));
	cfree((unsigned char *)tmpstat,tptr+1,sizeof(*tmpstat));
	cfree((unsigned char *)foll,tptr+1,sizeof(*foll));
	cfree((unsigned char *)name,treesize,sizeof(*name));
	cfree((unsigned char *)left,treesize,sizeof(*left));
	cfree((unsigned char *)right,treesize,sizeof(*right));
	cfree((unsigned char *)parent,treesize,sizeof(*parent));
	cfree((unsigned char *)nullstr,treesize,sizeof(*nullstr));
	cfree((unsigned char *)state,nstates,sizeof(*state));
	cfree((unsigned char *)sname,STARTSIZE,sizeof(*sname));
	cfree((unsigned char *)schar,STARTCHAR,sizeof(*schar));
	cfree((unsigned char *)ccl,CCLSIZE,sizeof(*ccl));
	}
get3core(){
	register int i, val;
	register unsigned char *p;
	verify = (int *)myalloc(outsize,sizeof(*verify));
	advance = (int *)myalloc(outsize,sizeof(*advance));
	stoff = (int *)myalloc(stnum+2,sizeof(*stoff));
	if(verify == 0 || advance == 0 || stoff == 0)
		error("Too little core for final packing");
	}
# ifdef DEBUG
free3core(){
	cfree((unsigned char *)advance,outsize,sizeof(*advance));
	cfree((unsigned char *)verify,outsize,sizeof(*verify));
	cfree((unsigned char *)stoff,stnum+1,sizeof(*stoff));
	cfree((unsigned char *)gotof,nstates,sizeof(*gotof));
	cfree((unsigned char *)nexts,ntrans,sizeof(*nexts));
	cfree((unsigned char *)nchar,ntrans,sizeof(*nchar));
	cfree((unsigned char *)atable,nstates,sizeof(*atable));
	cfree((unsigned char *)sfall,nstates,sizeof(*sfall));
	cfree((unsigned char *)cpackflg,nstates,sizeof(*cpackflg));
	}
# endif
unsigned char *myalloc(a,b)
  int a,b; {
	register int i;
	i = (int)calloc(a, b);
	if(i==0)
		warning("OOPS - calloc returns a 0");
	else if(i == -1){
# ifdef DEBUG
		warning("calloc returns a -1");
# endif
		return(0);
		}
	return((unsigned char *)i);
	}
sisprint(cisp)
int cisp;  {
        return( ISPRINT(cisp, wp) );
        }
sisalpha(cisa)
int cisa;  {
        int icha;
        icha = ISPRINT(cisa,wp);
        if( isspace(cisa) >= 1 )   icha = 0;
        if( iscntrl(cisa) >= 1 )   icha = 0;
        if( index(cisa, " .,;:><+*)('&%!-=#$^~[]{}_/?|\"") >= 0 )  icha = 0;
        return ( icha );
        }
# ifdef DEBUG
buserr(){
	fflush(errorf);
	fflush(fout);
	fflush(stdout);
	fprintf(errorf,"Bus error\n");
	if(report == 1)statistics();
	fflush(errorf);
	}
segviol(){
	fflush(errorf);
	fflush(fout);
	fflush(stdout);
	fprintf(errorf,"Segmentation violation\n");
	if(report == 1)statistics();
	fflush(errorf);
	}
# endif

yyerror(s)
unsigned char *s;
{
         fprintf(stderr, "line %d: %s\n",yyline, s);
}
