* reordered a little bit
[mascara-docs.git] / i86 / elks / elkscmd / m4 / look.c
blob25356842e8fb0fc73067a9b6a697aca598bdafdd
1 /*
2 * look.c
3 * Facility: m4 macro processor
4 * by: oz
5 */
7 #include "mdef.h"
8 #include "extr.h"
10 extern char *strsave();
13 * hash - compute hash value using the proverbial
14 * hashing function. Taken from K&R.
16 hash (name)
17 register char *name;
19 register int h = 0;
20 while (*name)
21 h += *name++;
22 return (h % HASHSIZE);
26 * lookup - find name in the hash table
29 ndptr lookup(name)
30 char *name;
32 register ndptr p;
34 for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
35 if (strcmp(name, p->name) == 0)
36 break;
37 return (p);
41 * addent - hash and create an entry in the hash
42 * table. The new entry is added in front
43 * of a hash bucket.
45 ndptr addent(name)
46 char *name;
48 register int h;
49 ndptr p;
51 h = hash(name);
52 if ((p = (ndptr) malloc(sizeof(struct ndblock))) != NULL) {
53 p->nxtptr = hashtab[h];
54 hashtab[h] = p;
55 p->name = strsave(name);
57 else
58 error("m4: no more memory.");
59 return p;
63 * remhash - remove an entry from the hashtable
66 remhash(name, all)
67 char *name;
68 int all;
70 register int h;
71 register ndptr xp, tp, mp;
73 h = hash(name);
74 mp = hashtab[h];
75 tp = nil;
76 while (mp != nil) {
77 if (strcmp(mp->name, name) == 0) {
78 mp = mp->nxtptr;
79 if (tp == nil) {
80 freent(hashtab[h]);
81 hashtab[h] = mp;
83 else {
84 xp = tp->nxtptr;
85 tp->nxtptr = mp;
86 freent(xp);
88 if (!all)
89 break;
91 else {
92 tp = mp;
93 mp = mp->nxtptr;
99 * freent - free a hashtable information block
102 freent(p)
103 ndptr p;
105 if (!(p->type & STATIC)) {
106 free(p->name);
107 if (p->defn != null)
108 free(p->defn);
110 free(p);