Commit | Line | Data |
---|---|---|
e63ce3e7 KB |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Ozan Yigit. | |
7 | * | |
6ecf3d85 | 8 | * %sccs.include.redist.c% |
e63ce3e7 KB |
9 | */ |
10 | ||
11 | #ifndef lint | |
50841d34 | 12 | static char sccsid[] = "@(#)look.c 5.3 (Berkeley) %G%"; |
e63ce3e7 KB |
13 | #endif /* not lint */ |
14 | ||
15 | /* | |
16 | * look.c | |
17 | * Facility: m4 macro processor | |
18 | * by: oz | |
19 | */ | |
20 | ||
50841d34 KB |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
e63ce3e7 KB |
24 | #include "mdef.h" |
25 | #include "extr.h" | |
26 | ||
e63ce3e7 KB |
27 | /* |
28 | * hash - compute hash value using the proverbial | |
29 | * hashing function. Taken from K&R. | |
30 | */ | |
31 | hash (name) | |
32 | register char *name; | |
33 | { | |
34 | register int h = 0; | |
35 | while (*name) | |
36 | h += *name++; | |
37 | return (h % HASHSIZE); | |
38 | } | |
39 | ||
40 | /* | |
41 | * lookup - find name in the hash table | |
42 | * | |
43 | */ | |
44 | ndptr lookup(name) | |
45 | char *name; | |
46 | { | |
47 | register ndptr p; | |
48 | ||
49 | for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr) | |
50 | if (strcmp(name, p->name) == 0) | |
51 | break; | |
52 | return (p); | |
53 | } | |
54 | ||
55 | /* | |
56 | * addent - hash and create an entry in the hash | |
57 | * table. The new entry is added in front | |
58 | * of a hash bucket. | |
59 | */ | |
60 | ndptr addent(name) | |
61 | char *name; | |
62 | { | |
63 | register int h; | |
64 | ndptr p; | |
65 | ||
66 | h = hash(name); | |
67 | if ((p = (ndptr) malloc(sizeof(struct ndblock))) != NULL) { | |
68 | p->nxtptr = hashtab[h]; | |
69 | hashtab[h] = p; | |
50841d34 | 70 | p->name = strdup(name); |
e63ce3e7 KB |
71 | } |
72 | else | |
73 | error("m4: no more memory."); | |
74 | return p; | |
75 | } | |
76 | ||
77 | /* | |
78 | * remhash - remove an entry from the hashtable | |
79 | * | |
80 | */ | |
81 | remhash(name, all) | |
82 | char *name; | |
83 | int all; | |
84 | { | |
85 | register int h; | |
86 | register ndptr xp, tp, mp; | |
87 | ||
88 | h = hash(name); | |
89 | mp = hashtab[h]; | |
90 | tp = nil; | |
91 | while (mp != nil) { | |
92 | if (strcmp(mp->name, name) == 0) { | |
93 | mp = mp->nxtptr; | |
94 | if (tp == nil) { | |
95 | freent(hashtab[h]); | |
96 | hashtab[h] = mp; | |
97 | } | |
98 | else { | |
99 | xp = tp->nxtptr; | |
100 | tp->nxtptr = mp; | |
101 | freent(xp); | |
102 | } | |
103 | if (!all) | |
104 | break; | |
105 | } | |
106 | else { | |
107 | tp = mp; | |
108 | mp = mp->nxtptr; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | /* | |
114 | * freent - free a hashtable information block | |
115 | * | |
116 | */ | |
117 | freent(p) | |
118 | ndptr p; | |
119 | { | |
120 | if (!(p->type & STATIC)) { | |
121 | free(p->name); | |
122 | if (p->defn != null) | |
123 | free(p->defn); | |
124 | } | |
125 | free(p); | |
126 | } | |
127 |