xref: /original-bsd/old/eqn/common_source/io.c (revision 2301fdfb)
1 #ifndef lint
2 static char sccsid[] = "@(#)io.c	4.5 07/22/88";
3 #endif
4 
5 # include "e.h"
6 #define	MAXLINE	1200	/* maximum input line */
7 
8 char	in[MAXLINE];	/* input buffer */
9 int	eqnexit();
10 int noeqn;
11 
12 main(argc,argv) int argc; char *argv[];{
13 
14 	eqnexit(eqn(argc, argv));
15 }
16 
17 eqnexit(n) {
18 #ifdef gcos
19 	if (n)
20 		fprintf(stderr, "run terminated due to eqn error\n");
21 	exit(0);
22 #endif
23 	exit(n);
24 }
25 
26 eqn(argc,argv) int argc; char *argv[];{
27 	int i, type;
28 
29 	setfile(argc,argv);
30 	init_tbl();	/* install keywords in tables */
31 	while ((type=getline(in)) != EOF) {
32 		eqline = linect;
33 		if (in[0]=='.' && in[1]=='E' && in[2]=='Q') {
34 			for (i=11; i<100; used[i++]=0);
35 			printf("%s",in);
36 			printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
37 			markline = 0;
38 			init();
39 			yyparse();
40 			if (eqnreg>0) {
41 				printf(".nr %d \\w'\\*(%d'\n", eqnreg, eqnreg);
42 				/* printf(".if \\n(%d>\\n(.l .tm too-long eqn, file %s, between lines %d-%d\n",	*/
43 				/*	eqnreg, svargv[ifile], eqline, linect);	*/
44 				printf(".nr MK %d\n", markline);	/* for -ms macros */
45 				printf(".if %d>\\n(.v .ne %du\n", eqnht, eqnht);
46 				printf(".rn %d 10\n", eqnreg);
47 				if(!noeqn)printf("\\*(10\n");
48 			}
49 			printf(".ps \\n(99\n.ft \\n(98\n");
50 			printf(".EN");
51 			if (lastchar == EOF) {
52 				putchar('\n');
53 				break;
54 			}
55 			if (putchar(lastchar) != '\n')
56 				while (putchar(gtc()) != '\n');
57 		}
58 		else if (type == lefteq)
59 			in_line();
60 		else
61 			printf("%s",in);
62 	}
63 	return(0);
64 }
65 
66 getline(s) register char *s; {
67 	register c;
68 	while((*s++=c=gtc())!='\n' && c!=EOF && c!=lefteq)
69 		if (s >= in+MAXLINE) {
70 			error( !FATAL, "input line too long: %.20s\n", in);
71 			in[MAXLINE] = '\0';
72 			break;
73 		}
74 	if (c==lefteq)
75 		s--;
76 	*s++ = '\0';
77 	return(c);
78 }
79 
80 in_line() {
81 	int ds;
82 
83 	printf(".nr 99 \\n(.s\n.nr 98 \\n(.f\n");
84 	ds = oalloc();
85 	printf(".rm %d \n", ds);
86 	do{
87 		if (*in)
88 			printf(".as %d \"%s\n", ds, in);
89 		init();
90 		yyparse();
91 		if (eqnreg > 0) {
92 			printf(".as %d \\*(%d\n", ds, eqnreg);
93 			ofree(eqnreg);
94 		}
95 		printf(".ps \\n(99\n.ft \\n(98\n");
96 	} while (getline(in) == lefteq);
97 	if (*in)
98 		printf(".as %d \"%s", ds, in);
99 	printf(".ps \\n(99\n.ft \\n(98\n");
100 	printf("\\*(%d\n", ds);
101 	ofree(ds);
102 }
103 
104 putout(p1) int p1; {
105 	extern int gsize, gfont;
106 	int before, after;
107 	if(dbg)printf(".\tanswer <- S%d, h=%d,b=%d\n",p1, eht[p1], ebase[p1]);
108 	eqnht = eht[p1];
109 	printf(".ds %d \\x'0'", p1);
110 	/* suppposed to leave room for a subscript or superscript */
111 #ifndef NEQN
112 	before = eht[p1] - ebase[p1] - VERT((ps*6*12)/10);
113 #else NEQN
114 	before = eht[p1] - ebase[p1] - VERT(3);	/* 3 = 1.5 lines */
115 #endif NEQN
116 	if (before > 0)
117 		printf("\\x'0-%du'", before);
118 	printf("\\f%c\\s%d\\*(%d%s\\s\\n(99\\f\\n(98",
119 		gfont, gsize, p1, rfont[p1] == ITAL ? "\\|" : "");
120 #ifndef NEQN
121 	after = ebase[p1] - VERT((ps*6*2)/10);
122 #else NEQN
123 	after = ebase[p1] - VERT(1);
124 #endif NEQN
125 	if (after > 0)
126 		printf("\\x'%du'", after);
127 	putchar('\n');
128 	eqnreg = p1;
129 }
130 
131 max(i,j) int i,j; {
132 	return (i>j ? i : j);
133 }
134 
135 oalloc() {
136 	int i;
137 	for (i=11; i<100; i++)
138 		if (used[i]++ == 0) return(i);
139 	error( FATAL, "no eqn strings left", i);
140 	return(0);
141 }
142 
143 ofree(n) int n; {
144 	used[n] = 0;
145 }
146 
147 setps(p) int p; {
148 	printf(".ps %d\n", EFFPS(p));
149 }
150 
151 nrwid(n1, p, n2) int n1, p, n2; {
152 	printf(".nr %d \\w'\\s%d\\*(%d'\n", n1, EFFPS(p), n2);
153 }
154 
155 setfile(argc, argv) int argc; char *argv[]; {
156 	static char *nullstr = "-";
157 
158 	svargc = --argc;
159 	svargv = argv;
160 	while (svargc > 0 && svargv[1][0] == '-') {
161 		switch (svargv[1][1]) {
162 
163 		case 'd': lefteq=svargv[1][2]; righteq=svargv[1][3]; break;
164 		case 's': gsize = atoi(&svargv[1][2]); break;
165 		case 'p': deltaps = atoi(&svargv[1][2]); break;
166 		case 'f': gfont = svargv[1][2]; break;
167 		case 'e': noeqn++; break;
168 		case 0:	goto endargs;
169 		default: dbg = 1;
170 		}
171 		svargc--;
172 		svargv++;
173 	}
174   endargs:
175 	ifile = 1;
176 	linect = 1;
177 	if (svargc <= 0) {
178 		curfile = stdin;
179 		svargv[1] = nullstr;
180 	}
181 	else
182 		openinfile();	/* opens up the first input file */
183 }
184 
185 yyerror() {;}
186 
187 init() {
188 	ct = 0;
189 	ps = gsize;
190 	ft = gfont;
191 	setps(ps);
192 	printf(".ft %c\n", ft);
193 }
194 
195 error(fatal, s1, s2) int fatal; char *s1, *s2; {
196 	if (fatal>0)
197 		printf("eqn fatal error: ");
198 	printf(s1,s2);
199 	printf("\nfile %s, between lines %d and %d\n",
200 		 svargv[ifile], eqline, linect);
201 	fprintf(stderr, "eqn: ");
202 	if (fatal>0)
203 		fprintf(stderr, "fatal error: ");
204 	fprintf(stderr, s1, s2);
205 	fprintf(stderr, "\nfile %s, between lines %d and %d\n",
206 		 svargv[ifile], eqline, linect);
207 	if (fatal > 0)
208 		eqnexit(1);
209 }
210