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