1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include "e.h"
#include "y.tab.h"
#include <ctype.h>
#include <errno.h>
#define SSIZE 1000
char token[SSIZE];
int sp;
void space(void);
void dodef(tbl *);
void define(int);
void ifdef(void);
void include(void);
void delim(void);
int
yylex(void)
{
register int c;
tbl *tp;
begin:
while ((c = input()) == ' ' || c == '\n' || c == '\t')
;
yylval = c;
switch (c) {
case EOF:
ERROR "unexpected end of input inside equation" WARNING;
return(EOF);
case '~':
return(SPACE);
case '^':
return(THIN);
/* case '\t':
return(TAB);
*/
case '{':
return('{');
case '}':
return('}');
case '"':
for (sp = 0; (c=input())!='"' && c != '\n'; ) {
if (c == '\\')
if ((c = input()) != '"')
token[sp++] = '\\';
token[sp++] = c;
if (sp >= SSIZE)
ERROR "quoted string %.20s... too long", token FATAL;
}
token[sp] = '\0';
yylval = (intptr_t)&token[0];
if (c == '\n')
ERROR "missing \" in %.20s", token WARNING;
return(QTEXT);
}
if (!display && c == righteq)
return(EOF);
unput(c);
getstr(token, SSIZE);
dprintf(".\tlex token = |%s|\n", token);
if ((tp = lookup(deftbl, token)) != NULL) { /* defined term */
c = input();
unput(c);
if (c == '(') /* macro with args */
dodef(tp);
else { /* no args */
unput(' ');
pbstr(tp->cval);
dprintf(".\tfound %s|=%s|\n", token, tp->cval);
}
goto begin;
}
if ((tp = lookup(keytbl, token)) == NULL) /* not a keyword */
return CONTIG;
switch (tp->ival) { /* some kind of keyword */
case DEFINE: case TDEFINE: case NDEFINE:
define(tp->ival);
break;
case IFDEF:
ifdef();
break;
case DELIM:
delim();
break;
case GSIZE:
globsize();
break;
case GFONT:
globfont();
break;
case INCLUDE:
include();
break;
case SPACE:
space();
break;
case DOTEQ:
/* .EQ inside equation -- should warn if at bottom level */
break;
case DOTEN:
if (curfile == infile)
return EOF;
/* else ignore nested .EN */
break;
default:
return tp->ival;
}
goto begin;
}
void getstr(char *s, int n)
{
register int c;
register char *p;
p = s;
while ((c = input()) == ' ' || c == '\n')
;
if (c == EOF) {
*s = 0;
return;
}
while (c != ' ' && c != '\t' && c != '\n' && c != '{' && c != '}'
&& c != '"' && c != '~' && c != '^') {
if (!display && c == righteq)
break;
if (c == '(' && p > s) { /* might be defined(...) */
*p = '\0';
if (lookup(deftbl, s) != NULL)
break;
}
if (c == '\\')
if ((c = input()) != '"')
*p++ = '\\';
*p++ = c;
if (--n <= 0)
ERROR "token %.20s... too long", s FATAL;
c = input();
}
unput(c);
*p = '\0';
yylval = (uintptr_t)s;
}
int
cstr(char *s, int quote, int maxs)
{
int del, c, i;
s[0] = 0;
while ((del=input()) == ' ' || del == '\t')
;
if (quote)
for (i=0; (c=input()) != del && c != EOF;) {
s[i++] = c;
if (i >= maxs)
return(1); /* disaster */
}
else {
if (del == '\n')
return(1);
s[0] = del;
for (i=1; (c=input())!=' ' && c!= '\t' && c!='\n' && c!=EOF;) {
s[i++] = c;
if (i >= maxs)
return(1); /* disaster */
}
}
s[i] = '\0';
if (c == EOF)
ERROR "Unexpected end of input at %.20s", s FATAL;
return(0);
}
void define(int type)
{
char *p1, *p2;
extern int ftune(char *, char *);
getstr(token, SSIZE); /* get name */
if (type != DEFINE) {
cstr(token, 1, SSIZE); /* skip the definition too */
return;
}
p1 = strsave(token);
if (cstr(token, 1, SSIZE))
ERROR "Unterminated definition at %.20s", token FATAL;
if (lookup(ftunetbl, p1) != NULL) { /* double tuning param */
dprintf(".\ttune %s %s\n", p1, token);
ftune(p1, token);
} else {
p2 = strsave(token);
install(deftbl, p1, p2, 0);
dprintf(".\tname %s defined as %s\n", p1, p2);
}
}
void ifdef(void) /* do body if name is defined */
{
char name[100], *p;
getstr(name, sizeof(name)); /* get name */
cstr(token, 1, SSIZE); /* and body */
if (lookup(deftbl, name) != NULL) { /* found it */
p = strsave(token);
pushsrc(Free, p);
pushsrc(String, p);
}
}
char *spaceval = NULL;
void space(void) /* collect line of form "space amt" to replace \x in output */
{
getstr(token, SSIZE);
spaceval = strsave(token);
dprintf(".\tsetting spaceval to %s\n", token);
}
char *strsave(char *s)
{
register char *q;
q = malloc(strlen(s)+1);
if (q == NULL)
ERROR "out of space in strsave on %s", s FATAL;
strcpy(q, s);
return(q);
}
void include(void)
{
char name[100];
FILE *fin;
int c;
while ((c = input()) == ' ')
;
unput(c);
cstr(name, c == '"', sizeof(name)); /* gets it quoted or not */
if ((fin = fopen(name, "r")) == NULL)
ERROR "can't open file %s", name FATAL;
errno = 0;
curfile++;
curfile->fin = fin;
curfile->fname = strsave(name);
curfile->lineno = 0;
printf(".lf 1 %s\n", curfile->fname);
pushsrc(File, curfile->fname);
}
void delim(void)
{
yyval = eqnreg = 0;
if (cstr(token, 0, SSIZE))
ERROR "Bizarre delimiters" FATAL;
lefteq = token[0];
righteq = token[1];
if (!isprint(lefteq) || !isprint(righteq))
ERROR "Bizarre delimiters" FATAL;
if (lefteq == 'o' && righteq == 'f')
lefteq = righteq = '\0';
}
|