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