xref: /csrg-svn/old/ratfor/rlook.c (revision 9730)
1*9730Sclemc /* @(#)rlook.c	1.1 (Berkeley) 12/15/82 */
2*9730Sclemc #define NULL 0
3*9730Sclemc #define EOS 0
4*9730Sclemc #define	HSHSIZ	101
5*9730Sclemc struct	nlist {
6*9730Sclemc 	char	*name;
7*9730Sclemc 	char	*def;
8*9730Sclemc 	int	ydef;
9*9730Sclemc 	struct	nlist *next;
10*9730Sclemc };
11*9730Sclemc 
12*9730Sclemc struct	nlist	*hshtab[HSHSIZ];
13*9730Sclemc struct nlist	*lookup();
14*9730Sclemc char	*install();
15*9730Sclemc char	*malloc();
16*9730Sclemc char	*copy();
17*9730Sclemc int	hshval;
18*9730Sclemc 
19*9730Sclemc struct nlist *lookup(str)
20*9730Sclemc char *str;
21*9730Sclemc {
22*9730Sclemc 	register char *s1, *s2;
23*9730Sclemc 	register struct nlist *np;
24*9730Sclemc 	static struct nlist nodef;
25*9730Sclemc 
26*9730Sclemc 	s1 = str;
27*9730Sclemc 	for (hshval = 0; *s1; )
28*9730Sclemc 		hshval += *s1++;
29*9730Sclemc 	hshval %= HSHSIZ;
30*9730Sclemc 	for (np = hshtab[hshval]; np!=NULL; np = np->next) {
31*9730Sclemc 		s1 = str;
32*9730Sclemc 		s2 = np->name;
33*9730Sclemc 		while (*s1++ == *s2)
34*9730Sclemc 			if (*s2++ == EOS)
35*9730Sclemc 				return(np);
36*9730Sclemc 	}
37*9730Sclemc 	return(&nodef);
38*9730Sclemc }
39*9730Sclemc 
40*9730Sclemc char *install(nam, val, tran)
41*9730Sclemc char *nam, *val;
42*9730Sclemc int tran;
43*9730Sclemc {
44*9730Sclemc 	register struct nlist *np;
45*9730Sclemc 
46*9730Sclemc 	if ((np = lookup(nam))->name == NULL) {
47*9730Sclemc 		np = (struct nlist *)malloc(sizeof(*np));
48*9730Sclemc 		np->name = copy(nam);
49*9730Sclemc 		np->def = copy(val);
50*9730Sclemc 		np->ydef = tran;
51*9730Sclemc 		np->next = hshtab[hshval];
52*9730Sclemc 		hshtab[hshval] = np;
53*9730Sclemc 		return(np->def);
54*9730Sclemc 	}
55*9730Sclemc 	free(np->def);
56*9730Sclemc 	np->def = copy(val);
57*9730Sclemc 	return(np->def);
58*9730Sclemc }
59*9730Sclemc 
60*9730Sclemc char *copy(s)
61*9730Sclemc register char *s;
62*9730Sclemc {
63*9730Sclemc 	register char *p, *s1;
64*9730Sclemc 
65*9730Sclemc 	p = s1 = (char *) malloc((unsigned)strlen(s)+1);
66*9730Sclemc 	while (*s1++ = *s++);
67*9730Sclemc 	return(p);
68*9730Sclemc }
69