xref: /illumos-gate/usr/src/cmd/eqn/io.c (revision 6a634c9d)
1779fc935Sceastha /*
2*23a1cceaSRoger A. Faulkner  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3779fc935Sceastha  */
4779fc935Sceastha 
57c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
67c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
107c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
117c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
14779fc935Sceastha #include "e.h"
15779fc935Sceastha #include <stdlib.h>
167c478bd9Sstevel@tonic-gate #include <locale.h>
17779fc935Sceastha 
187c478bd9Sstevel@tonic-gate #define	MAXLINE	8192	/* maximum input line */
197c478bd9Sstevel@tonic-gate 
20779fc935Sceastha char in[MAXLINE+1];	/* input buffer */
217c478bd9Sstevel@tonic-gate int noeqn;
227c478bd9Sstevel@tonic-gate 
23779fc935Sceastha void error(int, char *, char *);
247c478bd9Sstevel@tonic-gate 
25779fc935Sceastha static void do_inline(void);
26779fc935Sceastha int eqn(int, char *[]);
27*23a1cceaSRoger A. Faulkner static int getaline(char *);
28779fc935Sceastha static void init(void);
29779fc935Sceastha void nrwid(int, int, int);
30779fc935Sceastha int oalloc(void);
31779fc935Sceastha void ofree(int);
32779fc935Sceastha static void setfile(int, char *[]);
33779fc935Sceastha void setps(int);
34779fc935Sceastha 
35779fc935Sceastha int
main(int argc,char * argv[])36779fc935Sceastha main(int argc, char *argv[])
37779fc935Sceastha {
387c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
397c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
407c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
43779fc935Sceastha 	return (eqn(argc, argv));
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate 
46779fc935Sceastha int
eqn(int argc,char * argv[])47779fc935Sceastha eqn(int argc, char *argv[])
48779fc935Sceastha {
497c478bd9Sstevel@tonic-gate 	int i, type;
507c478bd9Sstevel@tonic-gate 
51779fc935Sceastha 	setfile(argc, argv);
527c478bd9Sstevel@tonic-gate 	init_tbl();	/* install keywords in tables */
53*23a1cceaSRoger A. Faulkner 	while ((type = getaline(in)) != EOF) {
547c478bd9Sstevel@tonic-gate 		eqline = linect;
55779fc935Sceastha 		if (in[0] == '.' && in[1] == 'E' && in[2] == 'Q') {
56779fc935Sceastha 			for (i = 11; i < 100; used[i++] = 0)
57779fc935Sceastha 				;
58779fc935Sceastha 			printf("%s", in);
597c478bd9Sstevel@tonic-gate 			printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
607c478bd9Sstevel@tonic-gate 			markline = 0;
617c478bd9Sstevel@tonic-gate 			init();
627c478bd9Sstevel@tonic-gate 			yyparse();
63779fc935Sceastha 			if (eqnreg > 0) {
647c478bd9Sstevel@tonic-gate 				printf(".nr %d \\w'\\*(%d'\n", eqnreg, eqnreg);
65779fc935Sceastha 				/*
66779fc935Sceastha 				 * printf(".if \\n(%d>\\n(.l .tm too-long eqn,
67779fc935Sceastha 				 * file %s, between lines %d-%d\n",
68779fc935Sceastha 				 * eqnreg, svargv[ifile], eqline, linect);
69779fc935Sceastha 				 */
70779fc935Sceastha 
71779fc935Sceastha 				/* for -ms macros */
72779fc935Sceastha 				printf(".nr MK %d\n", markline);
73779fc935Sceastha 
747c478bd9Sstevel@tonic-gate 				printf(".if %d>\\n(.v .ne %du\n", eqnht, eqnht);
757c478bd9Sstevel@tonic-gate 				printf(".rn %d 10\n", eqnreg);
76779fc935Sceastha 				if (!noeqn) printf("\\*(10\n");
777c478bd9Sstevel@tonic-gate 			}
787c478bd9Sstevel@tonic-gate 			printf(".ps \\n(99\n.ft \\n(98\n");
797c478bd9Sstevel@tonic-gate 			printf(".EN");
807c478bd9Sstevel@tonic-gate 			if (lastchar == EOF) {
817c478bd9Sstevel@tonic-gate 				putchar('\n');
827c478bd9Sstevel@tonic-gate 				break;
837c478bd9Sstevel@tonic-gate 			}
847c478bd9Sstevel@tonic-gate 			if (putchar(lastchar) != '\n')
85779fc935Sceastha 				while (putchar(gtc()) != '\n')
86779fc935Sceastha 					;
87779fc935Sceastha 		} else if (type == lefteq)
887c478bd9Sstevel@tonic-gate 			do_inline();
897c478bd9Sstevel@tonic-gate 		else
90779fc935Sceastha 			printf("%s", in);
917c478bd9Sstevel@tonic-gate 	}
92779fc935Sceastha 	return (0);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
95779fc935Sceastha static int
getaline(char * s)96*23a1cceaSRoger A. Faulkner getaline(char *s)
97779fc935Sceastha {
98779fc935Sceastha 	int c;
99779fc935Sceastha 	while ((*s++ = c = gtc()) != '\n' && c != EOF && c != lefteq)
1007c478bd9Sstevel@tonic-gate 		if (s >= in+MAXLINE) {
101779fc935Sceastha 			error(!FATAL, gettext(
102779fc935Sceastha 			    "input line too long: %.20s\n"), in);
1037c478bd9Sstevel@tonic-gate 			in[MAXLINE] = '\0';
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		}
106779fc935Sceastha 	if (c == lefteq)
1077c478bd9Sstevel@tonic-gate 		s--;
1087c478bd9Sstevel@tonic-gate 	*s++ = '\0';
109779fc935Sceastha 	return (c);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
112779fc935Sceastha static void
do_inline(void)113779fc935Sceastha do_inline(void)
114779fc935Sceastha {
1157c478bd9Sstevel@tonic-gate 	int ds;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
1187c478bd9Sstevel@tonic-gate 	ds = oalloc();
1197c478bd9Sstevel@tonic-gate 	printf(".rm %d \n", ds);
120779fc935Sceastha 	do {
1217c478bd9Sstevel@tonic-gate 		if (*in)
1227c478bd9Sstevel@tonic-gate 			printf(".as %d \"%s\n", ds, in);
1237c478bd9Sstevel@tonic-gate 		init();
1247c478bd9Sstevel@tonic-gate 		yyparse();
1257c478bd9Sstevel@tonic-gate 		if (eqnreg > 0) {
1267c478bd9Sstevel@tonic-gate 			printf(".as %d \\*(%d\n", ds, eqnreg);
1277c478bd9Sstevel@tonic-gate 			ofree(eqnreg);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		printf(".ps \\n(99\n.ft \\n(98\n");
130*23a1cceaSRoger A. Faulkner 	} while (getaline(in) == lefteq);
1317c478bd9Sstevel@tonic-gate 	if (*in)
1327c478bd9Sstevel@tonic-gate 		printf(".as %d \"%s", ds, in);
1337c478bd9Sstevel@tonic-gate 	printf(".ps \\n(99\n.ft \\n(98\n");
1347c478bd9Sstevel@tonic-gate 	printf("\\*(%d\n", ds);
1357c478bd9Sstevel@tonic-gate 	ofree(ds);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
138779fc935Sceastha void
putout(int p1)139779fc935Sceastha putout(int p1)
140779fc935Sceastha {
1417c478bd9Sstevel@tonic-gate 	extern int gsize, gfont;
1427c478bd9Sstevel@tonic-gate 	int before, after;
143779fc935Sceastha 	if (dbg)
144779fc935Sceastha 		printf(".\tanswer <- S%d, h=%d,b=%d\n", p1, eht[p1], ebase[p1]);
1457c478bd9Sstevel@tonic-gate 	eqnht = eht[p1];
1467c478bd9Sstevel@tonic-gate 	printf(".ds %d \\x'0'", p1);
1477c478bd9Sstevel@tonic-gate 	/* suppposed to leave room for a subscript or superscript */
1487c478bd9Sstevel@tonic-gate #ifndef NEQN
1497c478bd9Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(EM(1.2, ps));
150779fc935Sceastha #else	/* NEQN */
1517c478bd9Sstevel@tonic-gate 	before = eht[p1] - ebase[p1] - VERT(3);	/* 3 = 1.5 lines */
152779fc935Sceastha #endif	/* NEQN	*/
1537c478bd9Sstevel@tonic-gate 	if (spaceval != NULL)
1547c478bd9Sstevel@tonic-gate 		printf("\\x'0-%s'", spaceval);
1557c478bd9Sstevel@tonic-gate 	else if (before > 0)
1567c478bd9Sstevel@tonic-gate 		printf("\\x'0-%du'", before);
1577c478bd9Sstevel@tonic-gate 	printf("\\f%c\\s%d\\*(%d%s\\s\\n(99\\f\\n(98",
158779fc935Sceastha 	    gfont, gsize, p1, rfont[p1] == ITAL ? "\\|" : "");
1597c478bd9Sstevel@tonic-gate #ifndef NEQN
1607c478bd9Sstevel@tonic-gate 	after = ebase[p1] - VERT(EM(0.2, ps));
161779fc935Sceastha #else	/* NEQN */
1627c478bd9Sstevel@tonic-gate 	after = ebase[p1] - VERT(1);
163779fc935Sceastha #endif	/* NEQN */
1647c478bd9Sstevel@tonic-gate 	if (spaceval == NULL && after > 0)
1657c478bd9Sstevel@tonic-gate 		printf("\\x'%du'", after);
1667c478bd9Sstevel@tonic-gate 	putchar('\n');
1677c478bd9Sstevel@tonic-gate 	eqnreg = p1;
1687c478bd9Sstevel@tonic-gate 	if (spaceval != NULL) {
1697c478bd9Sstevel@tonic-gate 		free(spaceval);
1707c478bd9Sstevel@tonic-gate 		spaceval = NULL;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
175779fc935Sceastha int
max(int i,int j)176779fc935Sceastha max(int i, int j)
177779fc935Sceastha {
178779fc935Sceastha 	return (i > j ? i : j);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
181779fc935Sceastha int
oalloc(void)182779fc935Sceastha oalloc(void)
183779fc935Sceastha {
1847c478bd9Sstevel@tonic-gate 	int i;
185779fc935Sceastha 	char ebuf[3];
186779fc935Sceastha 
187779fc935Sceastha 	for (i = 11; i < 100; i++)
188779fc935Sceastha 		if (used[i]++ == 0)
189779fc935Sceastha 			return (i);
190779fc935Sceastha 	(void) snprintf(ebuf, sizeof (ebuf), "%d", i);
191779fc935Sceastha 	error(FATAL, gettext("no eqn strings left"), ebuf);
192779fc935Sceastha 	return (0);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
195779fc935Sceastha void
ofree(int n)196779fc935Sceastha ofree(int n)
197779fc935Sceastha {
1987c478bd9Sstevel@tonic-gate 	used[n] = 0;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
201779fc935Sceastha void
setps(int p)202779fc935Sceastha setps(int p)
203779fc935Sceastha {
2047c478bd9Sstevel@tonic-gate 	printf(".ps %d\n", EFFPS(p));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
207779fc935Sceastha void
nrwid(int n1,int p,int n2)208779fc935Sceastha nrwid(int n1, int p, int n2)
209779fc935Sceastha {
2107c478bd9Sstevel@tonic-gate 	printf(".nr %d \\w'\\s%d\\*(%d'\n", n1, EFFPS(p), n2);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
213779fc935Sceastha static void
setfile(int argc,char * argv[])214779fc935Sceastha setfile(int argc, char *argv[])
215779fc935Sceastha {
2167c478bd9Sstevel@tonic-gate 	static char *nullstr = "-";
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	svargc = --argc;
2197c478bd9Sstevel@tonic-gate 	svargv = argv;
2207c478bd9Sstevel@tonic-gate 	while (svargc > 0 && svargv[1][0] == '-') {
2217c478bd9Sstevel@tonic-gate 		switch (svargv[1][1]) {
2227c478bd9Sstevel@tonic-gate 
223779fc935Sceastha 		case 'd': lefteq = svargv[1][2]; righteq = svargv[1][3]; break;
2247c478bd9Sstevel@tonic-gate 		case 's': gsize = atoi(&svargv[1][2]); break;
2257c478bd9Sstevel@tonic-gate 		case 'p': deltaps = atoi(&svargv[1][2]); break;
2267c478bd9Sstevel@tonic-gate 		case 'f': gfont = svargv[1][2]; break;
2277c478bd9Sstevel@tonic-gate 		case 'e': noeqn++; break;
228779fc935Sceastha 		case 0:	goto endargs;
2297c478bd9Sstevel@tonic-gate 		default: dbg = 1;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 		svargc--;
2327c478bd9Sstevel@tonic-gate 		svargv++;
2337c478bd9Sstevel@tonic-gate 	}
234779fc935Sceastha endargs:
2357c478bd9Sstevel@tonic-gate 	ifile = 1;
2367c478bd9Sstevel@tonic-gate 	linect = 1;
2377c478bd9Sstevel@tonic-gate 	if (svargc <= 0) {
2387c478bd9Sstevel@tonic-gate 		curfile = stdin;
2397c478bd9Sstevel@tonic-gate 		svargv[1] = nullstr;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	else
2427c478bd9Sstevel@tonic-gate 		openinfile();	/* opens up the first input file */
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
245779fc935Sceastha void
yyerror(void)246779fc935Sceastha yyerror(void)
247779fc935Sceastha {
248779fc935Sceastha }
2497c478bd9Sstevel@tonic-gate 
250779fc935Sceastha static void
init(void)251779fc935Sceastha init(void)
252779fc935Sceastha {
2537c478bd9Sstevel@tonic-gate 	ct = 0;
2547c478bd9Sstevel@tonic-gate 	ps = gsize;
2557c478bd9Sstevel@tonic-gate 	ft = gfont;
2567c478bd9Sstevel@tonic-gate 	setps(ps);
2577c478bd9Sstevel@tonic-gate 	printf(".ft %c\n", ft);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
260779fc935Sceastha void
error(int fatal,char * s1,char * s2)261779fc935Sceastha error(int fatal, char *s1, char *s2)
262779fc935Sceastha {
263779fc935Sceastha 	if (fatal > 0)
2647c478bd9Sstevel@tonic-gate 		printf(gettext("eqn fatal error: "));
2657c478bd9Sstevel@tonic-gate 	printf(s1, s2);
2667c478bd9Sstevel@tonic-gate 	printf(gettext("\nfile %s, between lines %d and %d\n"),
267779fc935Sceastha 	    svargv[ifile], eqline, linect);
2687c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("eqn: "));
269779fc935Sceastha 	if (fatal > 0)
2707c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("fatal error: "));
2717c478bd9Sstevel@tonic-gate 	fprintf(stderr, s1, s2);
2727c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\nfile %s, between lines %d and %d\n"),
273779fc935Sceastha 	    svargv[ifile], eqline, linect);
2747c478bd9Sstevel@tonic-gate 	if (fatal > 0)
275779fc935Sceastha 		exit(1);
2767c478bd9Sstevel@tonic-gate }
277