1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2 /* All Rights Reserved */
3
4
5 /*
6 * Copyright (c) 1980 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
9 */
10
11 /*
12 * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13 * All Rights Reserved.
14 */
15
16 /* from OpenSolaris "lookup.c 1.5 05/06/02 SMI" SVr4.0 1.1 */
17
18 /*
19 * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
20 *
21 * Sccsid @(#)lookup.c 1.5 (gritter) 9/18/05
22 */
23
24 #include "e.h"
25 #include "y.tab.h"
26 #include <string.h>
27 #include <stdlib.h>
28 #include <inttypes.h>
29
30 #define TBLSIZE 100
31
32 tbl *keytbl[TBLSIZE]; /* key words */
33 tbl *restbl[TBLSIZE]; /* reserved words */
34 tbl *deftbl[TBLSIZE]; /* user-defined names */
35
36 struct {
37 char *key;
38 int keyval;
39 } keyword[] ={
40 { "sub", SUB },
41 { "sup", SUP },
42 { ".EN", EOF },
43 { "from", FROM },
44 { "to", TO },
45 { "sum", SUM },
46 { "hat", HAT },
47 { "vec", VEC },
48 { "dyad", DYAD },
49 { "dot", DOT },
50 { "dotdot", DOTDOT },
51 { "bar", BAR },
52 { "tilde", TILDE },
53 { "under", UNDER },
54 { "prod", PROD },
55 { "int", INT },
56 { "integral", INT },
57 { "union", UNION },
58 { "inter", INTER },
59 { "pile", PILE },
60 { "lpile", LPILE },
61 { "cpile", CPILE },
62 { "rpile", RPILE },
63 { "over", OVER },
64 { "sqrt", SQRT },
65 { "above", ABOVE },
66 { "size", SIZE },
67 { "font", FONT },
68 { "fat", FAT },
69 { "roman", ROMAN },
70 { "italic", ITALIC },
71 { "bold", BOLD },
72 { "left", LEFT },
73 { "right", RIGHT },
74 { "delim", DELIM },
75 { "define", DEFINE },
76
77 #ifdef NEQN /* make ndefine synonym for define, tdefine a no-op */
78
79 { "tdefine", TDEFINE },
80 { "ndefine", DEFINE },
81
82 #else /* tdefine = define, ndefine = no-op */
83
84 { "tdefine", DEFINE },
85 { "ndefine", NDEFINE },
86
87 #endif
88
89 { "gsize", GSIZE },
90 { ".gsize", GSIZE },
91 { "gfont", GFONT },
92 { "include", INCLUDE },
93 { "up", UP },
94 { "down", DOWN },
95 { "fwd", FWD },
96 { "back", BACK },
97 { "mark", MARK },
98 { "lineup", LINEUP },
99 { "matrix", MATRIX },
100 { "col", COL },
101 { "lcol", LCOL },
102 { "ccol", CCOL },
103 { "rcol", RCOL },
104 { NULL, 0 }
105 };
106
107 struct {
108 char *res;
109 char *resval;
110 } resword[] ={
111 { ">=", "\\(>=" },
112 { "<=", "\\(<=" },
113 { "==", "\\(==" },
114 { "!=", "\\(!=" },
115 { "+-", "\\(+-" },
116 { "->", "\\(->" },
117 { "<-", "\\(<-" },
118 { "inf", "\\(if" },
119 { "infinity", "\\(if" },
120 { "partial", "\\(pd" },
121 { "half", "\\f1\\(12\\fP" },
122 { "prime", "\\f1\\(fm\\fP" },
123 { "dollar", "\\f1$\\fP" },
124 { "nothing", "" },
125 { "times", "\\(mu" },
126 { "del", "\\(gr" },
127 { "grad", "\\(gr" },
128 #ifdef NEQN
129 { "<<", "<<" },
130 { ">>", ">>" },
131 { "approx", "~\b\\d~\\u" },
132 { "cdot", "\\v'-.5'.\\v'.5'" },
133 { "...", "..." },
134 { ",...,", ",...," },
135 #else
136 { "<<", "<\\h'-.3m'<" },
137 { ">>", ">\\h'-.3m'>" },
138 { "approx", "\\v'-.2m'\\z\\(ap\\v'.25m'\\(ap\\v'-.05m'" },
139 { "cdot", "\\v'-.3m'.\\v'.3m'" },
140 { "...", "\\v'-.3m'\\ .\\ .\\ .\\ \\v'.3m'" },
141 { ",...,", ",\\ .\\ .\\ .\\ ,\\|" },
142 #endif
143
144 { "alpha", "\\(*a" },
145 { "ALPHA", "\\(*A" },
146 { "beta", "\\(*b" },
147 { "BETA", "\\(*B" },
148 { "gamma", "\\(*g" },
149 { "GAMMA", "\\(*G" },
150 { "delta", "\\(*d" },
151 { "DELTA", "\\(*D" },
152 { "epsilon", "\\(*e" },
153 { "EPSILON", "\\(*E" },
154 { "omega", "\\(*w" },
155 { "OMEGA", "\\(*W" },
156 { "lambda", "\\(*l" },
157 { "LAMBDA", "\\(*L" },
158 { "mu", "\\(*m" },
159 { "MU", "\\(*M" },
160 { "nu", "\\(*n" },
161 { "NU", "\\(*N" },
162 { "theta", "\\(*h" },
163 { "THETA", "\\(*H" },
164 { "phi", "\\(*f" },
165 { "PHI", "\\(*F" },
166 { "pi", "\\(*p" },
167 { "PI", "\\(*P" },
168 { "sigma", "\\(*s" },
169 { "SIGMA", "\\(*S" },
170 { "xi", "\\(*c" },
171 { "XI", "\\(*C" },
172 { "zeta", "\\(*z" },
173 { "ZETA", "\\(*Z" },
174 { "iota", "\\(*i" },
175 { "IOTA", "\\(*I" },
176 { "eta", "\\(*y" },
177 { "ETA", "\\(*Y" },
178 { "kappa", "\\(*k" },
179 { "KAPPA", "\\(*K" },
180 { "rho", "\\(*r" },
181 { "RHO", "\\(*R" },
182 { "tau", "\\(*t" },
183 { "TAU", "\\(*T" },
184 { "omicron", "\\(*o" },
185 { "OMICRON", "\\(*O" },
186 { "upsilon", "\\(*u" },
187 { "UPSILON", "\\(*U" },
188 { "psi", "\\(*q" },
189 { "PSI", "\\(*Q" },
190 { "chi", "\\(*x" },
191 { "CHI", "\\(*X" },
192 { "and", "\\f1and\\fP" },
193 { "for", "\\f1for\\fP" },
194 { "if", "\\f1if\\fP" },
195 { "Re", "\\f1Re\\fP" },
196 { "Im", "\\f1Im\\fP" },
197 { "sin", "\\f1sin\\fP" },
198 { "cos", "\\f1cos\\fP" },
199 { "tan", "\\f1tan\\fP" },
200 { "sec", "\\f1sec\\fP" },
201 { "csc", "\\f1csc\\fP" },
202 { "arc", "\\f1arc\\fP" },
203 { "asin", "\\f1asin\\fP" },
204 { "acos", "\\f1acos\\fP" },
205 { "atan", "\\f1atan\\fP" },
206 { "asec", "\\f1asec\\fP" },
207 { "acsc", "\\f1acsc\\fP" },
208 { "sinh", "\\f1sinh\\fP" },
209 { "coth", "\\f1coth\\fP" },
210 { "tanh", "\\f1tanh\\fP" },
211 { "cosh", "\\f1cosh\\fP" },
212 { "lim", "\\f1lim\\fP" },
213 { "log", "\\f1log\\fP" },
214 { "max", "\\f1max\\fP" },
215 { "min", "\\f1min\\fP" },
216 { "ln", "\\f1ln\\fP" },
217 { "exp", "\\f1exp\\fP" },
218 { "det", "\\f1det\\fP" },
219 { NULL, NULL }
220 };
221
222 tbl *
lookup(tbl ** tblp,char * name,char * defn)223 lookup(tbl **tblp, char *name, char *defn) /* find name in tbl. if defn non-null, install */
224 {
225 register tbl *p;
226 register int h;
227 register unsigned char *s = (unsigned char *)name;
228
229 for (h = 0; *s != '\0'; )
230 h += *s++;
231 h %= TBLSIZE;
232
233 for (p = tblp[h]; p != NULL; p = p->next)
234 if (strcmp(name, p->name) == 0) { /* found it */
235 if (defn != NULL)
236 p->defn = defn;
237 return(p);
238 }
239 /* didn't find it */
240 if (defn == NULL)
241 return(NULL);
242 p = (tbl *) malloc(sizeof (tbl));
243 if (p == NULL)
244 error(FATAL, "out of space in lookup");
245 p->name = name;
246 p->defn = defn;
247 p->next = tblp[h];
248 tblp[h] = p;
249 return(p);
250 }
251
252 void
init_tbl(void)253 init_tbl(void) /* initialize all tables */
254 {
255 int i;
256
257 for (i = 0; keyword[i].key != NULL; i++)
258 lookup(keytbl, keyword[i].key, (char *)(intptr_t)keyword[i].keyval);
259 for (i = 0; resword[i].res != NULL; i++)
260 lookup(restbl, resword[i].res, resword[i].resval);
261 }
262