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