xref: /onnv-gate/usr/src/cmd/eqn/lookup.c (revision 413:1d1413a1c800)
1364Sceastha /*
2364Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3364Sceastha  * Use is subject to license terms.
4364Sceastha  */
5364Sceastha 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved  	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
15364Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
16364Sceastha 
17364Sceastha #include "e.h"
180Sstevel@tonic-gate #include "e.def"
190Sstevel@tonic-gate #include <locale.h>
200Sstevel@tonic-gate 
210Sstevel@tonic-gate #define	TBLSIZE	100
220Sstevel@tonic-gate 
230Sstevel@tonic-gate tbl	*keytbl[TBLSIZE];	/* key words */
240Sstevel@tonic-gate tbl	*restbl[TBLSIZE];	/* reserved words */
250Sstevel@tonic-gate tbl	*deftbl[TBLSIZE];	/* user-defined names */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate struct {
280Sstevel@tonic-gate 	char	*key;
290Sstevel@tonic-gate 	int	keyval;
30364Sceastha } keyword[]	= {
31364Sceastha 	"sub", 	SUB,
32364Sceastha 	"sup", 	SUP,
33364Sceastha 	".EN", 	EOF,
34364Sceastha 	"from", 	FROM,
35364Sceastha 	"to", 	TO,
36364Sceastha 	"sum", 	SUM,
37364Sceastha 	"hat", 	HAT,
38364Sceastha 	"vec", VEC,
39364Sceastha 	"dyad", DYAD,
40364Sceastha 	"dot", 	DOT,
41364Sceastha 	"dotdot", 	DOTDOT,
42364Sceastha 	"bar", 	BAR,
43364Sceastha 	"tilde", 	TILDE,
44364Sceastha 	"under", 	UNDER,
45364Sceastha 	"prod", 	PROD,
46364Sceastha 	"int", 	INT,
47364Sceastha 	"integral", 	INT,
48364Sceastha 	"union", 	UNION,
49364Sceastha 	"inter", 	INTER,
50364Sceastha 	"pile", 	PILE,
51364Sceastha 	"lpile", 	LPILE,
52364Sceastha 	"cpile", 	CPILE,
53364Sceastha 	"rpile", 	RPILE,
54364Sceastha 	"over", 	OVER,
55364Sceastha 	"sqrt", 	SQRT,
56364Sceastha 	"above", 	ABOVE,
57364Sceastha 	"size", 	SIZE,
58364Sceastha 	"font", 	FONT,
59364Sceastha 	"fat", FAT,
60364Sceastha 	"roman", 	ROMAN,
61364Sceastha 	"italic", 	ITALIC,
62364Sceastha 	"bold", 	BOLD,
63364Sceastha 	"left", 	LEFT,
64364Sceastha 	"right", 	RIGHT,
65364Sceastha 	"delim", 	DELIM,
66364Sceastha 	"define", 	DEFINE,
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifdef	NEQN	/* make ndefine synonym for define, tdefine a no-op */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	"tdefine",	TDEFINE,
710Sstevel@tonic-gate 	"ndefine",	DEFINE,
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #else		/* tdefine = define, ndefine = no-op */
740Sstevel@tonic-gate 
75364Sceastha 	"tdefine", 	DEFINE,
76364Sceastha 	"ndefine", 	NDEFINE,
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 
80364Sceastha 	"gsize", 	GSIZE,
81364Sceastha 	".gsize", 	GSIZE,
82364Sceastha 	"gfont", 	GFONT,
83364Sceastha 	"include", 	INCLUDE,
84364Sceastha 	"up", 	UP,
85364Sceastha 	"down", 	DOWN,
86364Sceastha 	"fwd", 	FWD,
87364Sceastha 	"back", 	BACK,
88364Sceastha 	"mark", 	MARK,
89364Sceastha 	"lineup", 	LINEUP,
90364Sceastha 	"matrix", 	MATRIX,
91364Sceastha 	"col", 	COL,
92364Sceastha 	"lcol", 	LCOL,
93364Sceastha 	"ccol", 	CCOL,
94364Sceastha 	"rcol", 	RCOL,
950Sstevel@tonic-gate 	0, 	0
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate struct {
990Sstevel@tonic-gate 	char	*res;
1000Sstevel@tonic-gate 	char	*resval;
101364Sceastha } resword[]	= {
1020Sstevel@tonic-gate 	">=",	"\\(>=",
1030Sstevel@tonic-gate 	"<=",	"\\(<=",
1040Sstevel@tonic-gate 	"==",	"\\(==",
1050Sstevel@tonic-gate 	"!=",	"\\(!=",
1060Sstevel@tonic-gate 	"+-",	"\\(+-",
1070Sstevel@tonic-gate 	"->",	"\\(->",
1080Sstevel@tonic-gate 	"<-",	"\\(<-",
1090Sstevel@tonic-gate 	"inf",	"\\(if",
1100Sstevel@tonic-gate 	"infinity",	"\\(if",
1110Sstevel@tonic-gate 	"partial",	"\\(pd",
1120Sstevel@tonic-gate 	"half",	"\\f1\\(12\\fP",
1130Sstevel@tonic-gate 	"prime",	"\\f1\\(fm\\fP",
1140Sstevel@tonic-gate 	"dollar",	"\\f1$\\fP",
1150Sstevel@tonic-gate 	"nothing",	"",
1160Sstevel@tonic-gate 	"times",	"\\(mu",
1170Sstevel@tonic-gate 	"del",	"\\(gr",
1180Sstevel@tonic-gate 	"grad",	"\\(gr",
1190Sstevel@tonic-gate #ifdef	NEQN
1200Sstevel@tonic-gate 	"<<",	"<<",
1210Sstevel@tonic-gate 	">>",	">>",
1220Sstevel@tonic-gate 	"approx",	"~\b\\d~\\u",
1230Sstevel@tonic-gate 	"cdot",	"\\v'-.5'.\\v'.5'",
1240Sstevel@tonic-gate 	"...",	"...",
1250Sstevel@tonic-gate 	",...,",	",...,",
1260Sstevel@tonic-gate #else
1270Sstevel@tonic-gate 	"<<",	"<\\h'-.3m'<",
1280Sstevel@tonic-gate 	">>",	">\\h'-.3m'>",
1290Sstevel@tonic-gate 	"approx",	"\\v'-.2m'\\z\\(ap\\v'.25m'\\(ap\\v'-.05m'",
1300Sstevel@tonic-gate 	"cdot",	"\\v'-.3m'.\\v'.3m'",
1310Sstevel@tonic-gate 	"...",	"\\v'-.3m'\\ .\\ .\\ .\\ \\v'.3m'",
1320Sstevel@tonic-gate 	",...,",	",\\ .\\ .\\ .\\ ,\\|",
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	"alpha",	"\\(*a",
1360Sstevel@tonic-gate 	"beta",	"\\(*b",
1370Sstevel@tonic-gate 	"gamma",	"\\(*g",
1380Sstevel@tonic-gate 	"GAMMA",	"\\(*G",
1390Sstevel@tonic-gate 	"delta",	"\\(*d",
1400Sstevel@tonic-gate 	"DELTA",	"\\(*D",
1410Sstevel@tonic-gate 	"epsilon",	"\\(*e",
1420Sstevel@tonic-gate 	"EPSILON",	"\\f1E\\fP",
1430Sstevel@tonic-gate 	"omega",	"\\(*w",
1440Sstevel@tonic-gate 	"OMEGA",	"\\(*W",
1450Sstevel@tonic-gate 	"lambda",	"\\(*l",
1460Sstevel@tonic-gate 	"LAMBDA",	"\\(*L",
1470Sstevel@tonic-gate 	"mu",	"\\(*m",
1480Sstevel@tonic-gate 	"nu",	"\\(*n",
1490Sstevel@tonic-gate 	"theta",	"\\(*h",
1500Sstevel@tonic-gate 	"THETA",	"\\(*H",
1510Sstevel@tonic-gate 	"phi",	"\\(*f",
1520Sstevel@tonic-gate 	"PHI",	"\\(*F",
1530Sstevel@tonic-gate 	"pi",	"\\(*p",
1540Sstevel@tonic-gate 	"PI",	"\\(*P",
1550Sstevel@tonic-gate 	"sigma",	"\\(*s",
1560Sstevel@tonic-gate 	"SIGMA",	"\\(*S",
1570Sstevel@tonic-gate 	"xi",	"\\(*c",
1580Sstevel@tonic-gate 	"XI",	"\\(*C",
1590Sstevel@tonic-gate 	"zeta",	"\\(*z",
1600Sstevel@tonic-gate 	"iota",	"\\(*i",
1610Sstevel@tonic-gate 	"eta",	"\\(*y",
1620Sstevel@tonic-gate 	"kappa",	"\\(*k",
1630Sstevel@tonic-gate 	"rho",	"\\(*r",
1640Sstevel@tonic-gate 	"tau",	"\\(*t",
1650Sstevel@tonic-gate 	"omicron",	"\\(*o",
1660Sstevel@tonic-gate 	"upsilon",	"\\(*u",
1670Sstevel@tonic-gate 	"UPSILON",	"\\(*U",
1680Sstevel@tonic-gate 	"psi",	"\\(*q",
1690Sstevel@tonic-gate 	"PSI",	"\\(*Q",
1700Sstevel@tonic-gate 	"chi",	"\\(*x",
1710Sstevel@tonic-gate 	"and",	"\\f1and\\fP",
1720Sstevel@tonic-gate 	"for",	"\\f1for\\fP",
1730Sstevel@tonic-gate 	"if",	"\\f1if\\fP",
1740Sstevel@tonic-gate 	"Re",	"\\f1Re\\fP",
1750Sstevel@tonic-gate 	"Im",	"\\f1Im\\fP",
1760Sstevel@tonic-gate 	"sin",	"\\f1sin\\fP",
1770Sstevel@tonic-gate 	"cos",	"\\f1cos\\fP",
1780Sstevel@tonic-gate 	"tan",	"\\f1tan\\fP",
1790Sstevel@tonic-gate 	"sec",  "\\f1sec\\fP",
1800Sstevel@tonic-gate 	"csc",  "\\f1csc\\fP",
1810Sstevel@tonic-gate 	"arc",	"\\f1arc\\fP",
1820Sstevel@tonic-gate 	"asin", "\\f1asin\\fP",
1830Sstevel@tonic-gate 	"acos", "\\f1acos\\fP",
1840Sstevel@tonic-gate 	"atan", "\\f1atan\\fP",
1850Sstevel@tonic-gate 	"asec", "\\f1asec\\fP",
1860Sstevel@tonic-gate 	"acsc", "\\f1acsc\\fP",
1870Sstevel@tonic-gate 	"sinh",	"\\f1sinh\\fP",
1880Sstevel@tonic-gate 	"coth",	"\\f1coth\\fP",
1890Sstevel@tonic-gate 	"tanh",	"\\f1tanh\\fP",
1900Sstevel@tonic-gate 	"cosh",	"\\f1cosh\\fP",
1910Sstevel@tonic-gate 	"lim",	"\\f1lim\\fP",
1920Sstevel@tonic-gate 	"log",	"\\f1log\\fP",
1930Sstevel@tonic-gate 	"max",	"\\f1max\\fP",
1940Sstevel@tonic-gate 	"min",	"\\f1min\\fP",
1950Sstevel@tonic-gate 	"ln",	"\\f1ln\\fP",
1960Sstevel@tonic-gate 	"exp",	"\\f1exp\\fP",
1970Sstevel@tonic-gate 	"det",	"\\f1det\\fP",
1980Sstevel@tonic-gate 	0,	0
1990Sstevel@tonic-gate };
2000Sstevel@tonic-gate 
201364Sceastha /* find name in tbl. if defn non-null, install */
202364Sceastha tbl *
lookup(tbl * tblp[],char * name,char * defn)203*413Sceastha lookup(tbl *tblp[], char *name, char *defn)
2040Sstevel@tonic-gate {
205364Sceastha 	tbl *p;
206364Sceastha 	int h;
207364Sceastha 	unsigned char *s = (unsigned char *)name;
2080Sstevel@tonic-gate 	char *malloc();
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	for (h = 0; *s != '\0'; )
2110Sstevel@tonic-gate 		h += *s++;
2120Sstevel@tonic-gate 	h %= TBLSIZE;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	for (p = tblp[h]; p != NULL; p = p->next)
2150Sstevel@tonic-gate 		if (strcmp(name, p->name) == 0) {	/* found it */
2160Sstevel@tonic-gate 			if (defn != NULL)
2170Sstevel@tonic-gate 				p->defn = defn;
218364Sceastha 			return (p);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	/* didn't find it */
2210Sstevel@tonic-gate 	if (defn == NULL)
222364Sceastha 		return (NULL);
2230Sstevel@tonic-gate 	p = (tbl *) malloc(sizeof (tbl));
2240Sstevel@tonic-gate 	if (p == NULL)
225364Sceastha 		error(FATAL, gettext("out of space in lookup"), NULL);
2260Sstevel@tonic-gate 	p->name = name;
2270Sstevel@tonic-gate 	p->defn = defn;
2280Sstevel@tonic-gate 	p->next = tblp[h];
2290Sstevel@tonic-gate 	tblp[h] = p;
230364Sceastha 	return (p);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
233364Sceastha void
init_tbl(void)234364Sceastha init_tbl(void)	/* initialize all tables */
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	int i;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	for (i = 0; keyword[i].key != NULL; i++)
239364Sceastha 		lookup(keytbl, keyword[i].key, (char *)keyword[i].keyval);
2400Sstevel@tonic-gate 	for (i = 0; resword[i].res != NULL; i++)
2410Sstevel@tonic-gate 		lookup(restbl, resword[i].res, resword[i].resval);
2420Sstevel@tonic-gate }
243