xref: /onnv-gate/usr/src/cmd/eqn/io.c (revision 13093:48f2dbca79a2)
1364Sceastha /*
2*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3364Sceastha  */
4364Sceastha 
50Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
60Sstevel@tonic-gate /*	  All Rights Reserved  	*/
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
100Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
110Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
120Sstevel@tonic-gate  */
13364Sceastha 
14364Sceastha #include "e.h"
15364Sceastha #include <stdlib.h>
16364Sceastha #include <locale.h>
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #define	MAXLINE	8192	/* maximum input line */
190Sstevel@tonic-gate 
20364Sceastha char in[MAXLINE+1];	/* input buffer */
210Sstevel@tonic-gate int noeqn;
220Sstevel@tonic-gate 
23364Sceastha void error(int, char *, char *);
240Sstevel@tonic-gate 
25364Sceastha static void do_inline(void);
26364Sceastha int eqn(int, char *[]);
27*13093SRoger.Faulkner@Oracle.COM static int getaline(char *);
28364Sceastha static void init(void);
29364Sceastha void nrwid(int, int, int);
30364Sceastha int oalloc(void);
31364Sceastha void ofree(int);
32364Sceastha static void setfile(int, char *[]);
33364Sceastha void setps(int);
34364Sceastha 
35364Sceastha int
main(int argc,char * argv[])36364Sceastha main(int argc, char *argv[])
37364Sceastha {
380Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
390Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
400Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
43364Sceastha 	return (eqn(argc, argv));
440Sstevel@tonic-gate }
450Sstevel@tonic-gate 
46364Sceastha int
eqn(int argc,char * argv[])47364Sceastha eqn(int argc, char *argv[])
48364Sceastha {
490Sstevel@tonic-gate 	int i, type;
500Sstevel@tonic-gate 
51364Sceastha 	setfile(argc, argv);
520Sstevel@tonic-gate 	init_tbl();	/* install keywords in tables */
53*13093SRoger.Faulkner@Oracle.COM 	while ((type = getaline(in)) != EOF) {
540Sstevel@tonic-gate 		eqline = linect;
55364Sceastha 		if (in[0] == '.' && in[1] == 'E' && in[2] == 'Q') {
56364Sceastha 			for (i = 11; i < 100; used[i++] = 0)
57364Sceastha 				;
58364Sceastha 			printf("%s", in);
590Sstevel@tonic-gate 			printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
600Sstevel@tonic-gate 			markline = 0;
610Sstevel@tonic-gate 			init();
620Sstevel@tonic-gate 			yyparse();
63364Sceastha 			if (eqnreg > 0) {
640Sstevel@tonic-gate 				printf(".nr %d \\w'\\*(%d'\n", eqnreg, eqnreg);
65364Sceastha 				/*
66364Sceastha 				 * printf(".if \\n(%d>\\n(.l .tm too-long eqn,
67364Sceastha 				 * file %s, between lines %d-%d\n",
68364Sceastha 				 * eqnreg, svargv[ifile], eqline, linect);
69364Sceastha 				 */
70364Sceastha 
71364Sceastha 				/* for -ms macros */
72364Sceastha 				printf(".nr MK %d\n", markline);
73364Sceastha 
740Sstevel@tonic-gate 				printf(".if %d>\\n(.v .ne %du\n", eqnht, eqnht);
750Sstevel@tonic-gate 				printf(".rn %d 10\n", eqnreg);
76364Sceastha 				if (!noeqn) printf("\\*(10\n");
770Sstevel@tonic-gate 			}
780Sstevel@tonic-gate 			printf(".ps \\n(99\n.ft \\n(98\n");
790Sstevel@tonic-gate 			printf(".EN");
800Sstevel@tonic-gate 			if (lastchar == EOF) {
810Sstevel@tonic-gate 				putchar('\n');
820Sstevel@tonic-gate 				break;
830Sstevel@tonic-gate 			}
840Sstevel@tonic-gate 			if (putchar(lastchar) != '\n')
85364Sceastha 				while (putchar(gtc()) != '\n')
86364Sceastha 					;
87364Sceastha 		} else if (type == lefteq)
880Sstevel@tonic-gate 			do_inline();
890Sstevel@tonic-gate 		else
90364Sceastha 			printf("%s", in);
910Sstevel@tonic-gate 	}
92364Sceastha 	return (0);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
95364Sceastha static int
getaline(char * s)96*13093SRoger.Faulkner@Oracle.COM getaline(char *s)
97364Sceastha {
98364Sceastha 	int c;
99364Sceastha 	while ((*s++ = c = gtc()) != '\n' && c != EOF && c != lefteq)
1000Sstevel@tonic-gate 		if (s >= in+MAXLINE) {
101364Sceastha 			error(!FATAL, gettext(
102364Sceastha 			    "input line too long: %.20s\n"), in);
1030Sstevel@tonic-gate 			in[MAXLINE] = '\0';
1040Sstevel@tonic-gate 			break;
1050Sstevel@tonic-gate 		}
106364Sceastha 	if (c == lefteq)
1070Sstevel@tonic-gate 		s--;
1080Sstevel@tonic-gate 	*s++ = '\0';
109364Sceastha 	return (c);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
112364Sceastha static void
do_inline(void)113364Sceastha do_inline(void)
114364Sceastha {
1150Sstevel@tonic-gate 	int ds;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
1180Sstevel@tonic-gate 	ds = oalloc();
1190Sstevel@tonic-gate 	printf(".rm %d \n", ds);
120364Sceastha 	do {
1210Sstevel@tonic-gate 		if (*in)
1220Sstevel@tonic-gate 			printf(".as %d \"%s\n", ds, in);
1230Sstevel@tonic-gate 		init();
1240Sstevel@tonic-gate 		yyparse();
1250Sstevel@tonic-gate 		if (eqnreg > 0) {
1260Sstevel@tonic-gate 			printf(".as %d \\*(%d\n", ds, eqnreg);
1270Sstevel@tonic-gate 			ofree(eqnreg);
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 		printf(".ps \\n(99\n.ft \\n(98\n");
130*13093SRoger.Faulkner@Oracle.COM 	} while (getaline(in) == lefteq);
1310Sstevel@tonic-gate 	if (*in)
1320Sstevel@tonic-gate 		printf(".as %d \"%s", ds, in);
1330Sstevel@tonic-gate 	printf(".ps \\n(99\n.ft \\n(98\n");
1340Sstevel@tonic-gate 	printf("\\*(%d\n", ds);
1350Sstevel@tonic-gate 	ofree(ds);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
138364Sceastha void
putout(int p1)139364Sceastha putout(int p1)
140364Sceastha {
1410Sstevel@tonic-gate 	extern int gsize, gfont;
1420Sstevel@tonic-gate 	int before, after;
143364Sceastha 	if (dbg)
144364Sceastha 		printf(".\tanswer <- S%d, h=%d,b=%d\n", p1, eht[p1], ebase[p1]);
1450Sstevel@tonic-gate 	eqnht = eht[p1];
1460Sstevel@tonic-gate 	printf(".ds %d \\x'0'", p1);
1470Sstevel@tonic-gate 	/* suppposed to leave room for a subscript or superscript */
1480Sstevel@tonic-gate #ifndef NEQN
1490Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(EM(1.2, ps));
150364Sceastha #else	/* NEQN */
1510Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(3);	/* 3 = 1.5 lines */
152364Sceastha #endif	/* NEQN	*/
1530Sstevel@tonic-gate 	if (spaceval != NULL)
1540Sstevel@tonic-gate 		printf("\\x'0-%s'", spaceval);
1550Sstevel@tonic-gate 	else if (before > 0)
1560Sstevel@tonic-gate 		printf("\\x'0-%du'", before);
1570Sstevel@tonic-gate 	printf("\\f%c\\s%d\\*(%d%s\\s\\n(99\\f\\n(98",
158364Sceastha 	    gfont, gsize, p1, rfont[p1] == ITAL ? "\\|" : "");
1590Sstevel@tonic-gate #ifndef NEQN
1600Sstevel@tonic-gate 	after = ebase[p1] - VERT(EM(0.2, ps));
161364Sceastha #else	/* NEQN */
1620Sstevel@tonic-gate 	after = ebase[p1] - VERT(1);
163364Sceastha #endif	/* NEQN */
1640Sstevel@tonic-gate 	if (spaceval == NULL && after > 0)
1650Sstevel@tonic-gate 		printf("\\x'%du'", after);
1660Sstevel@tonic-gate 	putchar('\n');
1670Sstevel@tonic-gate 	eqnreg = p1;
1680Sstevel@tonic-gate 	if (spaceval != NULL) {
1690Sstevel@tonic-gate 		free(spaceval);
1700Sstevel@tonic-gate 		spaceval = NULL;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
175364Sceastha int
max(int i,int j)176364Sceastha max(int i, int j)
177364Sceastha {
178364Sceastha 	return (i > j ? i : j);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
181364Sceastha int
oalloc(void)182364Sceastha oalloc(void)
183364Sceastha {
1840Sstevel@tonic-gate 	int i;
185364Sceastha 	char ebuf[3];
186364Sceastha 
187364Sceastha 	for (i = 11; i < 100; i++)
188364Sceastha 		if (used[i]++ == 0)
189364Sceastha 			return (i);
190364Sceastha 	(void) snprintf(ebuf, sizeof (ebuf), "%d", i);
191364Sceastha 	error(FATAL, gettext("no eqn strings left"), ebuf);
192364Sceastha 	return (0);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
195364Sceastha void
ofree(int n)196364Sceastha ofree(int n)
197364Sceastha {
1980Sstevel@tonic-gate 	used[n] = 0;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
201364Sceastha void
setps(int p)202364Sceastha setps(int p)
203364Sceastha {
2040Sstevel@tonic-gate 	printf(".ps %d\n", EFFPS(p));
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
207364Sceastha void
nrwid(int n1,int p,int n2)208364Sceastha nrwid(int n1, int p, int n2)
209364Sceastha {
2100Sstevel@tonic-gate 	printf(".nr %d \\w'\\s%d\\*(%d'\n", n1, EFFPS(p), n2);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
213364Sceastha static void
setfile(int argc,char * argv[])214364Sceastha setfile(int argc, char *argv[])
215364Sceastha {
2160Sstevel@tonic-gate 	static char *nullstr = "-";
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	svargc = --argc;
2190Sstevel@tonic-gate 	svargv = argv;
2200Sstevel@tonic-gate 	while (svargc > 0 && svargv[1][0] == '-') {
2210Sstevel@tonic-gate 		switch (svargv[1][1]) {
2220Sstevel@tonic-gate 
223364Sceastha 		case 'd': lefteq = svargv[1][2]; righteq = svargv[1][3]; break;
2240Sstevel@tonic-gate 		case 's': gsize = atoi(&svargv[1][2]); break;
2250Sstevel@tonic-gate 		case 'p': deltaps = atoi(&svargv[1][2]); break;
2260Sstevel@tonic-gate 		case 'f': gfont = svargv[1][2]; break;
2270Sstevel@tonic-gate 		case 'e': noeqn++; break;
228364Sceastha 		case 0:	goto endargs;
2290Sstevel@tonic-gate 		default: dbg = 1;
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 		svargc--;
2320Sstevel@tonic-gate 		svargv++;
2330Sstevel@tonic-gate 	}
234364Sceastha endargs:
2350Sstevel@tonic-gate 	ifile = 1;
2360Sstevel@tonic-gate 	linect = 1;
2370Sstevel@tonic-gate 	if (svargc <= 0) {
2380Sstevel@tonic-gate 		curfile = stdin;
2390Sstevel@tonic-gate 		svargv[1] = nullstr;
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 	else
2420Sstevel@tonic-gate 		openinfile();	/* opens up the first input file */
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
245364Sceastha void
yyerror(void)246364Sceastha yyerror(void)
247364Sceastha {
248364Sceastha }
2490Sstevel@tonic-gate 
250364Sceastha static void
init(void)251364Sceastha init(void)
252364Sceastha {
2530Sstevel@tonic-gate 	ct = 0;
2540Sstevel@tonic-gate 	ps = gsize;
2550Sstevel@tonic-gate 	ft = gfont;
2560Sstevel@tonic-gate 	setps(ps);
2570Sstevel@tonic-gate 	printf(".ft %c\n", ft);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
260364Sceastha void
error(int fatal,char * s1,char * s2)261364Sceastha error(int fatal, char *s1, char *s2)
262364Sceastha {
263364Sceastha 	if (fatal > 0)
2640Sstevel@tonic-gate 		printf(gettext("eqn fatal error: "));
2650Sstevel@tonic-gate 	printf(s1, s2);
2660Sstevel@tonic-gate 	printf(gettext("\nfile %s, between lines %d and %d\n"),
267364Sceastha 	    svargv[ifile], eqline, linect);
2680Sstevel@tonic-gate 	fprintf(stderr, gettext("eqn: "));
269364Sceastha 	if (fatal > 0)
2700Sstevel@tonic-gate 		fprintf(stderr, gettext("fatal error: "));
2710Sstevel@tonic-gate 	fprintf(stderr, s1, s2);
2720Sstevel@tonic-gate 	fprintf(stderr, gettext("\nfile %s, between lines %d and %d\n"),
273364Sceastha 	    svargv[ifile], eqline, linect);
2740Sstevel@tonic-gate 	if (fatal > 0)
275364Sceastha 		exit(1);
2760Sstevel@tonic-gate }
277