xref: /titanic_50/usr/src/cmd/eqn/io.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983-1988, 2001 by Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All rights reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate # include "e.h"
19*7c478bd9Sstevel@tonic-gate #include <locale.h>
20*7c478bd9Sstevel@tonic-gate #define	MAXLINE	8192	/* maximum input line */
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate char	in[MAXLINE+1];	/* input buffer */
23*7c478bd9Sstevel@tonic-gate int	eqnexit();
24*7c478bd9Sstevel@tonic-gate int noeqn;
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate main(argc,argv) int argc; char *argv[];{
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
29*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
30*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
31*7c478bd9Sstevel@tonic-gate #endif
32*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
33*7c478bd9Sstevel@tonic-gate 	eqnexit(eqn(argc, argv));
34*7c478bd9Sstevel@tonic-gate }
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate eqnexit(n) {
37*7c478bd9Sstevel@tonic-gate #ifdef gcos
38*7c478bd9Sstevel@tonic-gate 	if (n)
39*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("run terminated due to eqn error\n"));
40*7c478bd9Sstevel@tonic-gate 	exit(0);
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate 	exit(n);
43*7c478bd9Sstevel@tonic-gate }
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate eqn(argc,argv) int argc; char *argv[];{
46*7c478bd9Sstevel@tonic-gate 	int i, type;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	setfile(argc,argv);
49*7c478bd9Sstevel@tonic-gate 	init_tbl();	/* install keywords in tables */
50*7c478bd9Sstevel@tonic-gate 	while ((type=getline(in)) != EOF) {
51*7c478bd9Sstevel@tonic-gate 		eqline = linect;
52*7c478bd9Sstevel@tonic-gate 		if (in[0]=='.' && in[1]=='E' && in[2]=='Q') {
53*7c478bd9Sstevel@tonic-gate 			for (i=11; i<100; used[i++]=0);
54*7c478bd9Sstevel@tonic-gate 			printf("%s",in);
55*7c478bd9Sstevel@tonic-gate 			printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
56*7c478bd9Sstevel@tonic-gate 			markline = 0;
57*7c478bd9Sstevel@tonic-gate 			init();
58*7c478bd9Sstevel@tonic-gate 			yyparse();
59*7c478bd9Sstevel@tonic-gate 			if (eqnreg>0) {
60*7c478bd9Sstevel@tonic-gate 				printf(".nr %d \\w'\\*(%d'\n", eqnreg, eqnreg);
61*7c478bd9Sstevel@tonic-gate 				/* printf(".if \\n(%d>\\n(.l .tm too-long eqn, file %s, between lines %d-%d\n",	*/
62*7c478bd9Sstevel@tonic-gate 				/*	eqnreg, svargv[ifile], eqline, linect);	*/
63*7c478bd9Sstevel@tonic-gate 				printf(".nr MK %d\n", markline);	/* for -ms macros */
64*7c478bd9Sstevel@tonic-gate 				printf(".if %d>\\n(.v .ne %du\n", eqnht, eqnht);
65*7c478bd9Sstevel@tonic-gate 				printf(".rn %d 10\n", eqnreg);
66*7c478bd9Sstevel@tonic-gate 				if(!noeqn)printf("\\*(10\n");
67*7c478bd9Sstevel@tonic-gate 			}
68*7c478bd9Sstevel@tonic-gate 			printf(".ps \\n(99\n.ft \\n(98\n");
69*7c478bd9Sstevel@tonic-gate 			printf(".EN");
70*7c478bd9Sstevel@tonic-gate 			if (lastchar == EOF) {
71*7c478bd9Sstevel@tonic-gate 				putchar('\n');
72*7c478bd9Sstevel@tonic-gate 				break;
73*7c478bd9Sstevel@tonic-gate 			}
74*7c478bd9Sstevel@tonic-gate 			if (putchar(lastchar) != '\n')
75*7c478bd9Sstevel@tonic-gate 				while (putchar(gtc()) != '\n');
76*7c478bd9Sstevel@tonic-gate 		}
77*7c478bd9Sstevel@tonic-gate 		else if (type == lefteq)
78*7c478bd9Sstevel@tonic-gate 			do_inline();
79*7c478bd9Sstevel@tonic-gate 		else
80*7c478bd9Sstevel@tonic-gate 			printf("%s",in);
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 	return(0);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate getline(s) register char *s; {
86*7c478bd9Sstevel@tonic-gate 	register c;
87*7c478bd9Sstevel@tonic-gate 	while((*s++=c=gtc())!='\n' && c!=EOF && c!=lefteq)
88*7c478bd9Sstevel@tonic-gate 		if (s >= in+MAXLINE) {
89*7c478bd9Sstevel@tonic-gate 			error( !FATAL, gettext("input line too long: %.20s\n"), in);
90*7c478bd9Sstevel@tonic-gate 			in[MAXLINE] = '\0';
91*7c478bd9Sstevel@tonic-gate 			break;
92*7c478bd9Sstevel@tonic-gate 		}
93*7c478bd9Sstevel@tonic-gate 	if (c==lefteq)
94*7c478bd9Sstevel@tonic-gate 		s--;
95*7c478bd9Sstevel@tonic-gate 	*s++ = '\0';
96*7c478bd9Sstevel@tonic-gate 	return(c);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate do_inline() {
100*7c478bd9Sstevel@tonic-gate 	int ds;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
103*7c478bd9Sstevel@tonic-gate 	ds = oalloc();
104*7c478bd9Sstevel@tonic-gate 	printf(".rm %d \n", ds);
105*7c478bd9Sstevel@tonic-gate 	do{
106*7c478bd9Sstevel@tonic-gate 		if (*in)
107*7c478bd9Sstevel@tonic-gate 			printf(".as %d \"%s\n", ds, in);
108*7c478bd9Sstevel@tonic-gate 		init();
109*7c478bd9Sstevel@tonic-gate 		yyparse();
110*7c478bd9Sstevel@tonic-gate 		if (eqnreg > 0) {
111*7c478bd9Sstevel@tonic-gate 			printf(".as %d \\*(%d\n", ds, eqnreg);
112*7c478bd9Sstevel@tonic-gate 			ofree(eqnreg);
113*7c478bd9Sstevel@tonic-gate 		}
114*7c478bd9Sstevel@tonic-gate 		printf(".ps \\n(99\n.ft \\n(98\n");
115*7c478bd9Sstevel@tonic-gate 	} while (getline(in) == lefteq);
116*7c478bd9Sstevel@tonic-gate 	if (*in)
117*7c478bd9Sstevel@tonic-gate 		printf(".as %d \"%s", ds, in);
118*7c478bd9Sstevel@tonic-gate 	printf(".ps \\n(99\n.ft \\n(98\n");
119*7c478bd9Sstevel@tonic-gate 	printf("\\*(%d\n", ds);
120*7c478bd9Sstevel@tonic-gate 	ofree(ds);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate putout(p1) int p1; {
124*7c478bd9Sstevel@tonic-gate 	extern int gsize, gfont;
125*7c478bd9Sstevel@tonic-gate 	int before, after;
126*7c478bd9Sstevel@tonic-gate 	if(dbg)printf(".\tanswer <- S%d, h=%d,b=%d\n",p1, eht[p1], ebase[p1]);
127*7c478bd9Sstevel@tonic-gate 	eqnht = eht[p1];
128*7c478bd9Sstevel@tonic-gate 	printf(".ds %d \\x'0'", p1);
129*7c478bd9Sstevel@tonic-gate 	/* suppposed to leave room for a subscript or superscript */
130*7c478bd9Sstevel@tonic-gate #ifndef NEQN
131*7c478bd9Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(EM(1.2, ps));
132*7c478bd9Sstevel@tonic-gate #else NEQN
133*7c478bd9Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(3);	/* 3 = 1.5 lines */
134*7c478bd9Sstevel@tonic-gate #endif NEQN
135*7c478bd9Sstevel@tonic-gate 	if (spaceval != NULL)
136*7c478bd9Sstevel@tonic-gate 		printf("\\x'0-%s'", spaceval);
137*7c478bd9Sstevel@tonic-gate 	else if (before > 0)
138*7c478bd9Sstevel@tonic-gate 		printf("\\x'0-%du'", before);
139*7c478bd9Sstevel@tonic-gate 	printf("\\f%c\\s%d\\*(%d%s\\s\\n(99\\f\\n(98",
140*7c478bd9Sstevel@tonic-gate 		gfont, gsize, p1, rfont[p1] == ITAL ? "\\|" : "");
141*7c478bd9Sstevel@tonic-gate #ifndef NEQN
142*7c478bd9Sstevel@tonic-gate 	after = ebase[p1] - VERT(EM(0.2, ps));
143*7c478bd9Sstevel@tonic-gate #else NEQN
144*7c478bd9Sstevel@tonic-gate 	after = ebase[p1] - VERT(1);
145*7c478bd9Sstevel@tonic-gate #endif NEQN
146*7c478bd9Sstevel@tonic-gate 	if (spaceval == NULL && after > 0)
147*7c478bd9Sstevel@tonic-gate 		printf("\\x'%du'", after);
148*7c478bd9Sstevel@tonic-gate 	putchar('\n');
149*7c478bd9Sstevel@tonic-gate 	eqnreg = p1;
150*7c478bd9Sstevel@tonic-gate 	if (spaceval != NULL) {
151*7c478bd9Sstevel@tonic-gate 		free(spaceval);
152*7c478bd9Sstevel@tonic-gate 		spaceval = NULL;
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate max(i,j) int i,j; {
158*7c478bd9Sstevel@tonic-gate 	return (i>j ? i : j);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate oalloc() {
162*7c478bd9Sstevel@tonic-gate 	int i;
163*7c478bd9Sstevel@tonic-gate 	for (i=11; i<100; i++)
164*7c478bd9Sstevel@tonic-gate 		if (used[i]++ == 0) return(i);
165*7c478bd9Sstevel@tonic-gate 	error( FATAL, gettext("no eqn strings left"), i);
166*7c478bd9Sstevel@tonic-gate 	return(0);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate ofree(n) int n; {
170*7c478bd9Sstevel@tonic-gate 	used[n] = 0;
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate setps(p) int p; {
174*7c478bd9Sstevel@tonic-gate 	printf(".ps %d\n", EFFPS(p));
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate nrwid(n1, p, n2) int n1, p, n2; {
178*7c478bd9Sstevel@tonic-gate 	printf(".nr %d \\w'\\s%d\\*(%d'\n", n1, EFFPS(p), n2);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate setfile(argc, argv) int argc; char *argv[]; {
182*7c478bd9Sstevel@tonic-gate 	static char *nullstr = "-";
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	svargc = --argc;
185*7c478bd9Sstevel@tonic-gate 	svargv = argv;
186*7c478bd9Sstevel@tonic-gate 	while (svargc > 0 && svargv[1][0] == '-') {
187*7c478bd9Sstevel@tonic-gate 		switch (svargv[1][1]) {
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 		case 'd': lefteq=svargv[1][2]; righteq=svargv[1][3]; break;
190*7c478bd9Sstevel@tonic-gate 		case 's': gsize = atoi(&svargv[1][2]); break;
191*7c478bd9Sstevel@tonic-gate 		case 'p': deltaps = atoi(&svargv[1][2]); break;
192*7c478bd9Sstevel@tonic-gate 		case 'f': gfont = svargv[1][2]; break;
193*7c478bd9Sstevel@tonic-gate 		case 'e': noeqn++; break;
194*7c478bd9Sstevel@tonic-gate 		case 0:	goto endargs;
195*7c478bd9Sstevel@tonic-gate 		default: dbg = 1;
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		svargc--;
198*7c478bd9Sstevel@tonic-gate 		svargv++;
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate   endargs:
201*7c478bd9Sstevel@tonic-gate 	ifile = 1;
202*7c478bd9Sstevel@tonic-gate 	linect = 1;
203*7c478bd9Sstevel@tonic-gate 	if (svargc <= 0) {
204*7c478bd9Sstevel@tonic-gate 		curfile = stdin;
205*7c478bd9Sstevel@tonic-gate 		svargv[1] = nullstr;
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 	else
208*7c478bd9Sstevel@tonic-gate 		openinfile();	/* opens up the first input file */
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate yyerror() {;}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate init() {
214*7c478bd9Sstevel@tonic-gate 	ct = 0;
215*7c478bd9Sstevel@tonic-gate 	ps = gsize;
216*7c478bd9Sstevel@tonic-gate 	ft = gfont;
217*7c478bd9Sstevel@tonic-gate 	setps(ps);
218*7c478bd9Sstevel@tonic-gate 	printf(".ft %c\n", ft);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate error(fatal, s1, s2) int fatal; char *s1, *s2; {
222*7c478bd9Sstevel@tonic-gate 	if (fatal>0)
223*7c478bd9Sstevel@tonic-gate 		printf(gettext("eqn fatal error: "));
224*7c478bd9Sstevel@tonic-gate 	printf(s1, s2);
225*7c478bd9Sstevel@tonic-gate 	printf(gettext("\nfile %s, between lines %d and %d\n"),
226*7c478bd9Sstevel@tonic-gate 		 svargv[ifile], eqline, linect);
227*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("eqn: "));
228*7c478bd9Sstevel@tonic-gate 	if (fatal>0)
229*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("fatal error: "));
230*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, s1, s2);
231*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\nfile %s, between lines %d and %d\n"),
232*7c478bd9Sstevel@tonic-gate 		 svargv[ifile], eqline, linect);
233*7c478bd9Sstevel@tonic-gate 	if (fatal > 0)
234*7c478bd9Sstevel@tonic-gate 		eqnexit(1);
235*7c478bd9Sstevel@tonic-gate }
236