xref: /titanic_51/usr/src/cmd/eqn/io.c (revision 779fc935796a940997d18b31d64a9fec9c6b40f6)
1*779fc935Sceastha /*
2*779fc935Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*779fc935Sceastha  * Use is subject to license terms.
4*779fc935Sceastha  */
5*779fc935Sceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
15*779fc935Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include "e.h"
18*779fc935Sceastha #include <stdlib.h>
197c478bd9Sstevel@tonic-gate #include <locale.h>
20*779fc935Sceastha 
217c478bd9Sstevel@tonic-gate #define	MAXLINE	8192	/* maximum input line */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate char in[MAXLINE+1];	/* input buffer */
247c478bd9Sstevel@tonic-gate int noeqn;
257c478bd9Sstevel@tonic-gate 
26*779fc935Sceastha void error(int, char *, char *);
277c478bd9Sstevel@tonic-gate 
28*779fc935Sceastha static void do_inline(void);
29*779fc935Sceastha int eqn(int, char *[]);
30*779fc935Sceastha static int getline(char *);
31*779fc935Sceastha static void init(void);
32*779fc935Sceastha void nrwid(int, int, int);
33*779fc935Sceastha int oalloc(void);
34*779fc935Sceastha void ofree(int);
35*779fc935Sceastha static void setfile(int, char *[]);
36*779fc935Sceastha void setps(int);
37*779fc935Sceastha 
38*779fc935Sceastha int
39*779fc935Sceastha main(int argc, char *argv[])
40*779fc935Sceastha {
417c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
427c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
437c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
46*779fc935Sceastha 	return (eqn(argc, argv));
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate 
49*779fc935Sceastha int
50*779fc935Sceastha eqn(int argc, char *argv[])
51*779fc935Sceastha {
527c478bd9Sstevel@tonic-gate 	int i, type;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	setfile(argc, argv);
557c478bd9Sstevel@tonic-gate 	init_tbl();	/* install keywords in tables */
567c478bd9Sstevel@tonic-gate 	while ((type = getline(in)) != EOF) {
577c478bd9Sstevel@tonic-gate 		eqline = linect;
587c478bd9Sstevel@tonic-gate 		if (in[0] == '.' && in[1] == 'E' && in[2] == 'Q') {
59*779fc935Sceastha 			for (i = 11; i < 100; used[i++] = 0)
60*779fc935Sceastha 				;
617c478bd9Sstevel@tonic-gate 			printf("%s", in);
627c478bd9Sstevel@tonic-gate 			printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
637c478bd9Sstevel@tonic-gate 			markline = 0;
647c478bd9Sstevel@tonic-gate 			init();
657c478bd9Sstevel@tonic-gate 			yyparse();
667c478bd9Sstevel@tonic-gate 			if (eqnreg > 0) {
677c478bd9Sstevel@tonic-gate 				printf(".nr %d \\w'\\*(%d'\n", eqnreg, eqnreg);
68*779fc935Sceastha 				/*
69*779fc935Sceastha 				 * printf(".if \\n(%d>\\n(.l .tm too-long eqn,
70*779fc935Sceastha 				 * file %s, between lines %d-%d\n",
71*779fc935Sceastha 				 * eqnreg, svargv[ifile], eqline, linect);
72*779fc935Sceastha 				 */
73*779fc935Sceastha 
74*779fc935Sceastha 				/* for -ms macros */
75*779fc935Sceastha 				printf(".nr MK %d\n", markline);
76*779fc935Sceastha 
777c478bd9Sstevel@tonic-gate 				printf(".if %d>\\n(.v .ne %du\n", eqnht, eqnht);
787c478bd9Sstevel@tonic-gate 				printf(".rn %d 10\n", eqnreg);
797c478bd9Sstevel@tonic-gate 				if (!noeqn) printf("\\*(10\n");
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 			printf(".ps \\n(99\n.ft \\n(98\n");
827c478bd9Sstevel@tonic-gate 			printf(".EN");
837c478bd9Sstevel@tonic-gate 			if (lastchar == EOF) {
847c478bd9Sstevel@tonic-gate 				putchar('\n');
857c478bd9Sstevel@tonic-gate 				break;
867c478bd9Sstevel@tonic-gate 			}
877c478bd9Sstevel@tonic-gate 			if (putchar(lastchar) != '\n')
88*779fc935Sceastha 				while (putchar(gtc()) != '\n')
89*779fc935Sceastha 					;
90*779fc935Sceastha 		} else if (type == lefteq)
917c478bd9Sstevel@tonic-gate 			do_inline();
927c478bd9Sstevel@tonic-gate 		else
937c478bd9Sstevel@tonic-gate 			printf("%s", in);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	return (0);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
98*779fc935Sceastha static int
99*779fc935Sceastha getline(char *s)
100*779fc935Sceastha {
101*779fc935Sceastha 	int c;
1027c478bd9Sstevel@tonic-gate 	while ((*s++ = c = gtc()) != '\n' && c != EOF && c != lefteq)
1037c478bd9Sstevel@tonic-gate 		if (s >= in+MAXLINE) {
104*779fc935Sceastha 			error(!FATAL, gettext(
105*779fc935Sceastha 			    "input line too long: %.20s\n"), in);
1067c478bd9Sstevel@tonic-gate 			in[MAXLINE] = '\0';
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	if (c == lefteq)
1107c478bd9Sstevel@tonic-gate 		s--;
1117c478bd9Sstevel@tonic-gate 	*s++ = '\0';
1127c478bd9Sstevel@tonic-gate 	return (c);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
115*779fc935Sceastha static void
116*779fc935Sceastha do_inline(void)
117*779fc935Sceastha {
1187c478bd9Sstevel@tonic-gate 	int ds;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
1217c478bd9Sstevel@tonic-gate 	ds = oalloc();
1227c478bd9Sstevel@tonic-gate 	printf(".rm %d \n", ds);
1237c478bd9Sstevel@tonic-gate 	do {
1247c478bd9Sstevel@tonic-gate 		if (*in)
1257c478bd9Sstevel@tonic-gate 			printf(".as %d \"%s\n", ds, in);
1267c478bd9Sstevel@tonic-gate 		init();
1277c478bd9Sstevel@tonic-gate 		yyparse();
1287c478bd9Sstevel@tonic-gate 		if (eqnreg > 0) {
1297c478bd9Sstevel@tonic-gate 			printf(".as %d \\*(%d\n", ds, eqnreg);
1307c478bd9Sstevel@tonic-gate 			ofree(eqnreg);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		printf(".ps \\n(99\n.ft \\n(98\n");
1337c478bd9Sstevel@tonic-gate 	} while (getline(in) == lefteq);
1347c478bd9Sstevel@tonic-gate 	if (*in)
1357c478bd9Sstevel@tonic-gate 		printf(".as %d \"%s", ds, in);
1367c478bd9Sstevel@tonic-gate 	printf(".ps \\n(99\n.ft \\n(98\n");
1377c478bd9Sstevel@tonic-gate 	printf("\\*(%d\n", ds);
1387c478bd9Sstevel@tonic-gate 	ofree(ds);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
141*779fc935Sceastha void
142*779fc935Sceastha putout(int p1)
143*779fc935Sceastha {
1447c478bd9Sstevel@tonic-gate 	extern int gsize, gfont;
1457c478bd9Sstevel@tonic-gate 	int before, after;
146*779fc935Sceastha 	if (dbg)
147*779fc935Sceastha 		printf(".\tanswer <- S%d, h=%d,b=%d\n", p1, eht[p1], ebase[p1]);
1487c478bd9Sstevel@tonic-gate 	eqnht = eht[p1];
1497c478bd9Sstevel@tonic-gate 	printf(".ds %d \\x'0'", p1);
1507c478bd9Sstevel@tonic-gate 	/* suppposed to leave room for a subscript or superscript */
1517c478bd9Sstevel@tonic-gate #ifndef NEQN
1527c478bd9Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(EM(1.2, ps));
153*779fc935Sceastha #else	/* NEQN */
1547c478bd9Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(3);	/* 3 = 1.5 lines */
155*779fc935Sceastha #endif	/* NEQN	*/
1567c478bd9Sstevel@tonic-gate 	if (spaceval != NULL)
1577c478bd9Sstevel@tonic-gate 		printf("\\x'0-%s'", spaceval);
1587c478bd9Sstevel@tonic-gate 	else if (before > 0)
1597c478bd9Sstevel@tonic-gate 		printf("\\x'0-%du'", before);
1607c478bd9Sstevel@tonic-gate 	printf("\\f%c\\s%d\\*(%d%s\\s\\n(99\\f\\n(98",
1617c478bd9Sstevel@tonic-gate 	    gfont, gsize, p1, rfont[p1] == ITAL ? "\\|" : "");
1627c478bd9Sstevel@tonic-gate #ifndef NEQN
1637c478bd9Sstevel@tonic-gate 	after = ebase[p1] - VERT(EM(0.2, ps));
164*779fc935Sceastha #else	/* NEQN */
1657c478bd9Sstevel@tonic-gate 	after = ebase[p1] - VERT(1);
166*779fc935Sceastha #endif	/* NEQN */
1677c478bd9Sstevel@tonic-gate 	if (spaceval == NULL && after > 0)
1687c478bd9Sstevel@tonic-gate 		printf("\\x'%du'", after);
1697c478bd9Sstevel@tonic-gate 	putchar('\n');
1707c478bd9Sstevel@tonic-gate 	eqnreg = p1;
1717c478bd9Sstevel@tonic-gate 	if (spaceval != NULL) {
1727c478bd9Sstevel@tonic-gate 		free(spaceval);
1737c478bd9Sstevel@tonic-gate 		spaceval = NULL;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
178*779fc935Sceastha int
179*779fc935Sceastha max(int i, int j)
180*779fc935Sceastha {
1817c478bd9Sstevel@tonic-gate 	return (i > j ? i : j);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
184*779fc935Sceastha int
185*779fc935Sceastha oalloc(void)
186*779fc935Sceastha {
1877c478bd9Sstevel@tonic-gate 	int i;
188*779fc935Sceastha 	char ebuf[3];
189*779fc935Sceastha 
1907c478bd9Sstevel@tonic-gate 	for (i = 11; i < 100; i++)
191*779fc935Sceastha 		if (used[i]++ == 0)
192*779fc935Sceastha 			return (i);
193*779fc935Sceastha 	(void) snprintf(ebuf, sizeof (ebuf), "%d", i);
194*779fc935Sceastha 	error(FATAL, gettext("no eqn strings left"), ebuf);
1957c478bd9Sstevel@tonic-gate 	return (0);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
198*779fc935Sceastha void
199*779fc935Sceastha ofree(int n)
200*779fc935Sceastha {
2017c478bd9Sstevel@tonic-gate 	used[n] = 0;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
204*779fc935Sceastha void
205*779fc935Sceastha setps(int p)
206*779fc935Sceastha {
2077c478bd9Sstevel@tonic-gate 	printf(".ps %d\n", EFFPS(p));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
210*779fc935Sceastha void
211*779fc935Sceastha nrwid(int n1, int p, int n2)
212*779fc935Sceastha {
2137c478bd9Sstevel@tonic-gate 	printf(".nr %d \\w'\\s%d\\*(%d'\n", n1, EFFPS(p), n2);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
216*779fc935Sceastha static void
217*779fc935Sceastha setfile(int argc, char *argv[])
218*779fc935Sceastha {
2197c478bd9Sstevel@tonic-gate 	static char *nullstr = "-";
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	svargc = --argc;
2227c478bd9Sstevel@tonic-gate 	svargv = argv;
2237c478bd9Sstevel@tonic-gate 	while (svargc > 0 && svargv[1][0] == '-') {
2247c478bd9Sstevel@tonic-gate 		switch (svargv[1][1]) {
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		case 'd': lefteq = svargv[1][2]; righteq = svargv[1][3]; break;
2277c478bd9Sstevel@tonic-gate 		case 's': gsize = atoi(&svargv[1][2]); break;
2287c478bd9Sstevel@tonic-gate 		case 'p': deltaps = atoi(&svargv[1][2]); break;
2297c478bd9Sstevel@tonic-gate 		case 'f': gfont = svargv[1][2]; break;
2307c478bd9Sstevel@tonic-gate 		case 'e': noeqn++; break;
2317c478bd9Sstevel@tonic-gate 		case 0:	goto endargs;
2327c478bd9Sstevel@tonic-gate 		default: dbg = 1;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		svargc--;
2357c478bd9Sstevel@tonic-gate 		svargv++;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate endargs:
2387c478bd9Sstevel@tonic-gate 	ifile = 1;
2397c478bd9Sstevel@tonic-gate 	linect = 1;
2407c478bd9Sstevel@tonic-gate 	if (svargc <= 0) {
2417c478bd9Sstevel@tonic-gate 		curfile = stdin;
2427c478bd9Sstevel@tonic-gate 		svargv[1] = nullstr;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	else
2457c478bd9Sstevel@tonic-gate 		openinfile();	/* opens up the first input file */
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
248*779fc935Sceastha void
249*779fc935Sceastha yyerror(void)
250*779fc935Sceastha {
251*779fc935Sceastha }
2527c478bd9Sstevel@tonic-gate 
253*779fc935Sceastha static void
254*779fc935Sceastha init(void)
255*779fc935Sceastha {
2567c478bd9Sstevel@tonic-gate 	ct = 0;
2577c478bd9Sstevel@tonic-gate 	ps = gsize;
2587c478bd9Sstevel@tonic-gate 	ft = gfont;
2597c478bd9Sstevel@tonic-gate 	setps(ps);
2607c478bd9Sstevel@tonic-gate 	printf(".ft %c\n", ft);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
263*779fc935Sceastha void
264*779fc935Sceastha error(int fatal, char *s1, char *s2)
265*779fc935Sceastha {
2667c478bd9Sstevel@tonic-gate 	if (fatal > 0)
2677c478bd9Sstevel@tonic-gate 		printf(gettext("eqn fatal error: "));
2687c478bd9Sstevel@tonic-gate 	printf(s1, s2);
2697c478bd9Sstevel@tonic-gate 	printf(gettext("\nfile %s, between lines %d and %d\n"),
2707c478bd9Sstevel@tonic-gate 	    svargv[ifile], eqline, linect);
2717c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("eqn: "));
2727c478bd9Sstevel@tonic-gate 	if (fatal > 0)
2737c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("fatal error: "));
2747c478bd9Sstevel@tonic-gate 	fprintf(stderr, s1, s2);
2757c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\nfile %s, between lines %d and %d\n"),
2767c478bd9Sstevel@tonic-gate 	    svargv[ifile], eqline, linect);
2777c478bd9Sstevel@tonic-gate 	if (fatal > 0)
278*779fc935Sceastha 		exit(1);
2797c478bd9Sstevel@tonic-gate }
280