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