1 /*-
2  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
3  *     The Regents of the University of California.
4  * Copyright (C) Caldera International Inc.  2001-2002.
5  * Copyright (c) 2003, 2004, 2005
6  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms,
10  * with or without modification, are permitted provided
11  * that the following conditions are met:
12  *
13  * Redistributions of source code and documentation must retain
14  * the above copyright notice, this list of conditions and the
15  * following disclaimer.  Redistributions in binary form must
16  * reproduce the above copyright notice, this list of conditions
17  * and the following disclaimer in the documentation and/or other
18  * materials provided with the distribution.
19  *
20  * All advertising materials mentioning features or use of this
21  * software must display the following acknowledgement:
22  *   This product includes software developed or owned by
23  *   Caldera International, Inc.
24  *
25  * Neither the name of Caldera International, Inc. nor the names
26  * of other contributors may be used to endorse or promote products
27  * derived from this software without specific prior written permission.
28  *
29  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
30  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
31  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
34  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
39  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 #include <sys/cdefs.h>
44 __SCCSID("@(#)lookup.c	4.3 (Berkeley) 4/17/91");
45 __RCSID("$MirOS: src/usr.bin/oldroff/neqn/lookup.c,v 1.3 2008/11/08 23:04:37 tg Exp $");
46 
47 #include <stdlib.h>
48 
49 #include "e.h"
50 #include "ey.h"
51 
52 #define	TBLSIZE	100
53 
54 tbl	*keytbl[TBLSIZE];	/* key words */
55 tbl	*restbl[TBLSIZE];	/* reserved words */
56 tbl	*deftbl[TBLSIZE];	/* user-defined names */
57 
58 struct {
59 	char	*key;
60 	int	keyval;
61 } keyword[]	={
62 	"sub", 	SUB,
63 	"sup", 	SUP,
64 	".EN", 	EOF,
65 	"from", 	FROM,
66 	"to", 	TO,
67 	"sum", 	SUM,
68 	"hat", 	HAT,
69 	"vec", VEC,
70 	"dyad", DYAD,
71 	"dot", 	DOT,
72 	"dotdot", 	DOTDOT,
73 	"bar", 	BAR,
74 	"tilde", 	TILDE,
75 	"under", 	UNDER,
76 	"prod", 	PROD,
77 	"int", 	INT,
78 	"integral", 	INT,
79 	"union", 	UNION,
80 	"inter", 	INTER,
81 	"pile", 	PILE,
82 	"lpile", 	LPILE,
83 	"cpile", 	CPILE,
84 	"rpile", 	RPILE,
85 	"over", 	OVER,
86 	"sqrt", 	SQRT,
87 	"above", 	ABOVE,
88 	"size", 	SIZE,
89 	"font", 	FONT,
90 	"fat", FAT,
91 	"roman", 	ROMAN,
92 	"italic", 	ITALIC,
93 	"bold", 	BOLD,
94 	"left", 	LEFT,
95 	"right", 	RIGHT,
96 	"delim", 	DELIM,
97 	"define", 	DEFINE,
98 
99 #ifdef	NEQN	/* make ndefine synonym for define, tdefine a no-op */
100 
101 	"tdefine",	TDEFINE,
102 	"ndefine",	DEFINE,
103 
104 #else
105 
106 	"tdefine", 	DEFINE,
107 	"ndefine", 	NDEFINE,
108 
109 #endif
110 
111 	"gsize", 	GSIZE,
112 	".gsize", 	GSIZE,
113 	"gfont", 	GFONT,
114 	"include", 	INCLUDE,
115 	"up", 	UP,
116 	"down", 	DOWN,
117 	"fwd", 	FWD,
118 	"back", 	BACK,
119 	"mark", 	MARK,
120 	"lineup", 	LINEUP,
121 	"matrix", 	MATRIX,
122 	"col", 	COL,
123 	"lcol", 	LCOL,
124 	"ccol", 	CCOL,
125 	"rcol", 	RCOL,
126 	0, 	0
127 };
128 
129 struct {
130 	char	*res;
131 	char	*resval;
132 } resword[]	={
133 	">=",	"\\(>=",
134 	"<=",	"\\(<=",
135 	"==",	"\\(==",
136 	"!=",	"\\(!=",
137 	"+-",	"\\(+-",
138 	"->",	"\\(->",
139 	"<-",	"\\(<-",
140 	"inf",	"\\(if",
141 	"infinity",	"\\(if",
142 	"partial",	"\\(pd",
143 	"half",	"\\f1\\(12\\fP",
144 	"prime",	"\\f1\\(fm\\fP",
145 	"dollar",	"\\f1$\\fP",
146 	"nothing",	"",
147 	"times",	"\\(mu",
148 	"del",	"\\(gr",
149 	"grad",	"\\(gr",
150 #ifdef	NEQN
151 	"<<",	"<<",
152 	">>",	">>",
153 	"approx",	"~\b\\d~\\u",
154 	"cdot",	"\\v'-.5'.\\v'.5'",
155 	"...",	"...",
156 	",...,",	",...,",
157 #else
158 	"<<",	"<\\h'-.3m'<",
159 	">>",	">\\h'-.3m'>",
160 	"approx",	"\\v'-.2m'\\z\\(ap\\v'.25m'\\(ap\\v'-.05m'",
161 	"cdot",	"\\v'-.3m'.\\v'.3m'",
162 	"...",	"\\v'-.3m'\\ .\\ .\\ .\\ \\v'.3m'",
163 	",...,",	",\\ .\\ .\\ .\\ ,\\|",
164 #endif
165 
166 	"alpha",	"\\(*a",
167 	"beta",	"\\(*b",
168 	"gamma",	"\\(*g",
169 	"GAMMA",	"\\(*G",
170 	"delta",	"\\(*d",
171 	"DELTA",	"\\(*D",
172 	"epsilon",	"\\(*e",
173 	"EPSILON",	"\\f1E\\fP",
174 	"omega",	"\\(*w",
175 	"OMEGA",	"\\(*W",
176 	"lambda",	"\\(*l",
177 	"LAMBDA",	"\\(*L",
178 	"mu",	"\\(*m",
179 	"nu",	"\\(*n",
180 	"theta",	"\\(*h",
181 	"THETA",	"\\(*H",
182 	"phi",	"\\(*f",
183 	"PHI",	"\\(*F",
184 	"pi",	"\\(*p",
185 	"PI",	"\\(*P",
186 	"sigma",	"\\(*s",
187 	"SIGMA",	"\\(*S",
188 	"xi",	"\\(*c",
189 	"XI",	"\\(*C",
190 	"zeta",	"\\(*z",
191 	"iota",	"\\(*i",
192 	"eta",	"\\(*y",
193 	"kappa",	"\\(*k",
194 	"rho",	"\\(*r",
195 	"tau",	"\\(*t",
196 	"omicron",	"\\(*o",
197 	"upsilon",	"\\(*u",
198 	"UPSILON",	"\\(*U",
199 	"psi",	"\\(*q",
200 	"PSI",	"\\(*Q",
201 	"chi",	"\\(*x",
202 	"and",	"\\f1and\\fP",
203 	"for",	"\\f1for\\fP",
204 	"if",	"\\f1if\\fP",
205 	"Re",	"\\f1Re\\fP",
206 	"Im",	"\\f1Im\\fP",
207 	"sin",	"\\f1sin\\fP",
208 	"cos",	"\\f1cos\\fP",
209 	"tan",	"\\f1tan\\fP",
210 	"sec",  "\\f1sec\\fP",
211 	"csc",  "\\f1csc\\fP",
212 	"arc",	"\\f1arc\\fP",
213 	"asin", "\\f1asin\\fP",
214 	"acos", "\\f1acos\\fP",
215 	"atan", "\\f1atan\\fP",
216 	"asec", "\\f1asec\\fP",
217 	"acsc", "\\f1acsc\\fP",
218 	"sinh",	"\\f1sinh\\fP",
219 	"coth",	"\\f1coth\\fP",
220 	"tanh",	"\\f1tanh\\fP",
221 	"cosh",	"\\f1cosh\\fP",
222 	"lim",	"\\f1lim\\fP",
223 	"log",	"\\f1log\\fP",
224 	"max",	"\\f1max\\fP",
225 	"min",	"\\f1min\\fP",
226 	"ln",	"\\f1ln\\fP",
227 	"exp",	"\\f1exp\\fP",
228 	"det",	"\\f1det\\fP",
229 	0,	0
230 };
231 
lookup(tblp,name,defn)232 tbl *lookup(tblp, name, defn)	/* find name in tbl. if defn non-null, install */
233 tbl **tblp;
234 char *name, *defn;
235 {
236 	register tbl *p;
237 	register int h;
238 	register char *s = name;
239 
240 	for (h = 0; *s != '\0'; )
241 		h += *s++;
242 	h %= TBLSIZE;
243 
244 	for (p = tblp[h]; p != NULL; p = p->next)
245 		if (strcmp(name, p->name) == 0) {	/* found it */
246 			if (defn != NULL)
247 				p->defn = defn;
248 			return(p);
249 		}
250 	/* didn't find it */
251 	if (defn == NULL)
252 		return(NULL);
253 	p = (tbl *) malloc(sizeof (tbl));
254 	if (p == NULL)
255 		error(FATAL, "out of space in lookup");
256 	p->name = name;
257 	p->defn = defn;
258 	p->next = tblp[h];
259 	tblp[h] = p;
260 	return(p);
261 }
262 
init_tbl()263 init_tbl()	/* initialize all tables */
264 {
265 	int i;
266 
267 	for (i = 0; keyword[i].key != NULL; i++)
268 		lookup(keytbl, keyword[i].key, keyword[i].keyval);
269 	for (i = 0; resword[i].res != NULL; i++)
270 		lookup(restbl, resword[i].res, resword[i].resval);
271 }
272