114631Sslatteng # include "e.h"
214642Sslatteng # include "dev.h"
314631Sslatteng #define	MAXLINE	3600	/* maximum input line */
414631Sslatteng 	/* huge for Chris's graphics language */
514631Sslatteng 
615744Sslatteng #ifndef DEVDIR
714642Sslatteng #define DEVDIR	"/usr/lib/font"		/* place to find "dev" directory */
815744Sslatteng #endif
914642Sslatteng char	*devdir	= DEVDIR;
1014642Sslatteng 
1114631Sslatteng char	in[MAXLINE];	/* input buffer */
1214631Sslatteng int	eqnexit();
1314631Sslatteng int noeqn;
1414631Sslatteng 
main(argc,argv)1514631Sslatteng main(argc,argv) int argc; char *argv[];{
1614631Sslatteng 
1714631Sslatteng 	eqnexit(eqn(argc, argv));
1814631Sslatteng }
1914631Sslatteng 
eqnexit(n)2014631Sslatteng eqnexit(n) {
2114631Sslatteng #ifdef gcos
2214631Sslatteng 	if (n)
2314631Sslatteng 		fprintf(stderr, "run terminated due to eqn error\n");
2414631Sslatteng 	exit(0);
2514631Sslatteng #endif
2614631Sslatteng 	exit(n);
2714631Sslatteng }
2814631Sslatteng 
eqn(argc,argv)2914631Sslatteng eqn(argc,argv) int argc; char *argv[];{
3014631Sslatteng 	int i, type;
3114631Sslatteng 
3214631Sslatteng 	setfile(argc,argv);
3314631Sslatteng 	init_tbl();	/* install keywords in tables */
3414631Sslatteng 	while ((type=getline(in)) != EOF) {
3514631Sslatteng 		eqline = linect;
3614631Sslatteng 		if (in[0]=='.' && in[1]=='E' && in[2]=='Q') {
3714631Sslatteng 			for (i=11; i<100; used[i++]=0);
3814631Sslatteng 			printf("%s",in);
3915035Sslatteng 			printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n.af 98 01\n");
4014631Sslatteng 			markline = 0;
4114631Sslatteng 			init();
4214631Sslatteng 			yyparse();
4314631Sslatteng 			if (eqnreg>0) {
4414631Sslatteng 				printf(".nr %d \\w'\\*(%d'\n", eqnreg, eqnreg);
4514631Sslatteng 				/* printf(".if \\n(%d>\\n(.l .tm too-long eqn, file %s, between lines %d-%d\n",	*/
4614631Sslatteng 				/*	eqnreg, svargv[ifile], eqline, linect);	*/
4714631Sslatteng 				printf(".nr MK %d\n", markline);	/* for -ms macros */
4814631Sslatteng 				printf(".if %d>\\n(.v .ne %du\n", eqnht, eqnht);
4914631Sslatteng 				printf(".rn %d 10\n", eqnreg);
5014631Sslatteng 				if(!noeqn)printf("\\*(10\n");
5114631Sslatteng 			}
5214631Sslatteng 			printf(".ps \\n(99\n.ft \\n(98\n");
5314631Sslatteng 			printf(".EN");
5414631Sslatteng 			if (lastchar == EOF) {
5514631Sslatteng 				putchar('\n');
5614631Sslatteng 				break;
5714631Sslatteng 			}
5814631Sslatteng 			if (putchar(lastchar) != '\n')
5914631Sslatteng 				while (putchar(gtc()) != '\n');
6014631Sslatteng 		}
6114631Sslatteng 		else if (type == lefteq)
6214631Sslatteng 			inline();
6314631Sslatteng 		else
6414631Sslatteng 			printf("%s",in);
6514631Sslatteng 	}
6614631Sslatteng 	return(0);
6714631Sslatteng }
6814631Sslatteng 
getline(s)6914631Sslatteng getline(s) register char *s; {
7014631Sslatteng 	register c;
7114631Sslatteng 	while((*s++=c=gtc())!='\n' && c!=EOF && c!=lefteq)
7214631Sslatteng 		if (s >= in+MAXLINE) {
7314631Sslatteng 			error( !FATAL, "input line too long: %.20s\n", in);
7414631Sslatteng 			in[MAXLINE] = '\0';
7514631Sslatteng 			break;
7614631Sslatteng 		}
7714631Sslatteng 	if (c==lefteq)
7814631Sslatteng 		s--;
7914631Sslatteng 	*s++ = '\0';
8014631Sslatteng 	return(c);
8114631Sslatteng }
8214631Sslatteng 
8314631Sslatteng inline() {
8414631Sslatteng 	int ds;
8514631Sslatteng 
8615035Sslatteng 	printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n.af 98 01\n");
8714631Sslatteng 	ds = oalloc();
8814631Sslatteng 	printf(".rm %d \n", ds);
8914631Sslatteng 	do{
9014631Sslatteng 		if (*in)
9114631Sslatteng 			printf(".as %d \"%s\n", ds, in);
9214631Sslatteng 		init();
9314631Sslatteng 		yyparse();
9414631Sslatteng 		if (eqnreg > 0) {
9514631Sslatteng 			printf(".as %d \\*(%d\n", ds, eqnreg);
9614631Sslatteng 			ofree(eqnreg);
9714631Sslatteng 		}
9814631Sslatteng 		printf(".ps \\n(99\n.ft \\n(98\n");
9914631Sslatteng 	} while (getline(in) == lefteq);
10014631Sslatteng 	if (*in)
10114631Sslatteng 		printf(".as %d \"%s", ds, in);
10214631Sslatteng 	printf(".ps \\n(99\n.ft \\n(98\n");
10314631Sslatteng 	printf("\\*(%d\n", ds);
10414631Sslatteng 	ofree(ds);
10514631Sslatteng }
10614631Sslatteng 
putout(p1)10714631Sslatteng putout(p1) int p1; {
10814631Sslatteng 	extern int gsize, gfont;
10914631Sslatteng 	int before, after;
11014631Sslatteng 	if(dbg)printf(".\tanswer <- S%d, h=%d,b=%d\n",p1, eht[p1], ebase[p1]);
11114631Sslatteng 	eqnht = eht[p1];
11214631Sslatteng 	printf(".ds %d \\x'0'", p1);
11314631Sslatteng 	/* suppposed to leave room for a subscript or superscript */
11414631Sslatteng 	before = eht[p1] - ebase[p1] - VERT( EM(1.2, ps) );
11514631Sslatteng 	if (spaceval != NULL)
11614631Sslatteng 		printf("\\x'0-%s'", spaceval);
11714631Sslatteng 	else if (before > 0)
11814631Sslatteng 		printf("\\x'0-%du'", before);
11915035Sslatteng 	printf("\\f%c\\s%d\\*(%d%s\\s\\n(99\\f(\\n(98",
12014631Sslatteng 		gfont, gsize, p1, rfont[p1] == ITAL ? "\\|" : "");
12114631Sslatteng 	after = ebase[p1] - VERT( EM(0.2, ps) );
12214631Sslatteng 	if (spaceval == NULL && after > 0)
12314631Sslatteng 		printf("\\x'%du'", after);
12414631Sslatteng 	putchar('\n');
12514631Sslatteng 	eqnreg = p1;
12614631Sslatteng 	if (spaceval != NULL) {
12714631Sslatteng 		free(spaceval);
12814631Sslatteng 		spaceval = NULL;
12914631Sslatteng 	}
13014631Sslatteng }
13114631Sslatteng 
max(i,j)13214631Sslatteng max(i,j) int i,j; {
13314631Sslatteng 	return (i>j ? i : j);
13414631Sslatteng }
13514631Sslatteng 
oalloc()13614631Sslatteng oalloc() {
13714631Sslatteng 	int i;
13814631Sslatteng 	for (i=11; i<100; i++)
13914631Sslatteng 		if (used[i]++ == 0) return(i);
14014631Sslatteng 	error( FATAL, "no eqn strings left", i);
14114631Sslatteng 	return(0);
14214631Sslatteng }
14314631Sslatteng 
ofree(n)14414631Sslatteng ofree(n) int n; {
14514631Sslatteng 	used[n] = 0;
14614631Sslatteng }
14714631Sslatteng 
setps(p)14814631Sslatteng setps(p) int p; {
14914631Sslatteng 	printf(".ps %d\n", EFFPS(p));
15014631Sslatteng }
15114631Sslatteng 
nrwid(n1,p,n2)15214631Sslatteng nrwid(n1, p, n2) int n1, p, n2; {
15314631Sslatteng 	printf(".nr %d \\w'\\s%d\\*(%d'\n", n1, EFFPS(p), n2);
15414631Sslatteng }
15514631Sslatteng 
156*23734Sslatteng char *getenv();
157*23734Sslatteng 
setfile(argc,argv)15814631Sslatteng setfile(argc, argv) int argc; char *argv[]; {
15914631Sslatteng 	static char *nullstr = "-";
160*23734Sslatteng 	char *cp;
16114631Sslatteng 
162*23734Sslatteng 	if ((cp = getenv("PRINTER"))) device = cp;
163*23734Sslatteng 	if ((cp = getenv("TYPESETTER"))) device = cp;
16414631Sslatteng 	svargc = --argc;
16514631Sslatteng 	svargv = argv;
16614631Sslatteng 	while (svargc > 0 && svargv[1][0] == '-') {
16714631Sslatteng 		switch (svargv[1][1]) {
16814631Sslatteng 
16914631Sslatteng 		case 'd': lefteq=svargv[1][2]; righteq=svargv[1][3]; break;
17014631Sslatteng 		case 's': gsize = atoi(&svargv[1][2]); break;
17114631Sslatteng 		case 'p': deltaps = atoi(&svargv[1][2]); break;
17214631Sslatteng 		case 'r': res = atoi(&svargv[1][2]); break;
17314631Sslatteng 		case 'm': minsize = atoi(&svargv[1][2]); break;
17414631Sslatteng 		case 'f': gfont = svargv[1][2]; break;
17514631Sslatteng 		case 'e': noeqn++; break;
17615181Sslatteng 		case 'P':
17714642Sslatteng 		case 'T': device = &(svargv[1][2]); break;
17814642Sslatteng 		case 'F': devdir = &(svargv[1][2]); break;
17914631Sslatteng 		default: dbg = 1;
18014631Sslatteng 		}
18114631Sslatteng 		svargc--;
18214631Sslatteng 		svargv++;
18314631Sslatteng 	}
18414642Sslatteng 
18514642Sslatteng 	fileinit();
18614631Sslatteng 	ifile = 1;
18714631Sslatteng 	linect = 1;
18814631Sslatteng 	if (svargc <= 0) {
18914631Sslatteng 		curfile = stdin;
19014631Sslatteng 		svargv[1] = nullstr;
19114631Sslatteng 	}
19214631Sslatteng 	else if ((curfile = fopen(svargv[1], "r")) == NULL)
19314631Sslatteng 		error( FATAL,"can't open file %s", svargv[1]);
19414631Sslatteng }
19514631Sslatteng 
fileinit()19614642Sslatteng fileinit()
19714642Sslatteng {
19814642Sslatteng 	int fin;
19914642Sslatteng 	short readmin;
20014642Sslatteng 	struct dev device_info;
20114642Sslatteng 	char temp[100];
20214642Sslatteng 
20314642Sslatteng 	sprintf(temp, "%s/dev%s/DESC.out", devdir, device);
20414642Sslatteng 	if ((fin = open(temp, 0)) < 0) {
20514642Sslatteng 	    fprintf(stderr, "can't open tables for %s\n", temp);
20614642Sslatteng 	    exit(1);
20714642Sslatteng 	}
20814642Sslatteng 	read(fin, &device_info, sizeof(struct dev));
20914642Sslatteng 	read(fin, &readmin, sizeof readmin);
21014642Sslatteng 
21114642Sslatteng 		/* if res and minsize were not set by option, do it now */
21214642Sslatteng 	if (res <= 0) res = device_info.res;
21314642Sslatteng 	if (minsize <= 0) minsize = readmin;
21416751Sslatteng 	minvert = device_info.vert;
21514642Sslatteng 	close(fin);
21614642Sslatteng }
21714642Sslatteng 
yyerror()21814631Sslatteng yyerror() {;}
21914631Sslatteng 
init()22014631Sslatteng init() {
22114631Sslatteng 	ct = 0;
22214631Sslatteng 	ps = gsize;
22314631Sslatteng 	ft = gfont;
22414631Sslatteng 	setps(ps);
22514631Sslatteng 	printf(".ft %c\n", ft);
22614631Sslatteng }
22714631Sslatteng 
error(fatal,s1,s2)22814631Sslatteng error(fatal, s1, s2) int fatal; char *s1, *s2; {
22914631Sslatteng 	if (fatal>0)
23014631Sslatteng 		printf("eqn fatal error: ");
23114631Sslatteng 	printf(s1,s2);
23214631Sslatteng 	printf("\nfile %s, between lines %d and %d\n",
23314631Sslatteng 		 svargv[ifile], eqline, linect);
23414631Sslatteng 	fprintf(stderr, "eqn: ");
23514631Sslatteng 	if (fatal>0)
23614631Sslatteng 		fprintf(stderr, "fatal error: ");
23714631Sslatteng 	fprintf(stderr, s1, s2);
23814631Sslatteng 	fprintf(stderr, "\nfile %s, between lines %d and %d\n",
23914631Sslatteng 		 svargv[ifile], eqline, linect);
24014631Sslatteng 	if (fatal > 0)
24114631Sslatteng 		eqnexit(1);
24214631Sslatteng }
243