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 | |
a93b814a | 6 | * Ozan Yigit at York University. |
e63ce3e7 | 7 | * |
6ecf3d85 | 8 | * %sccs.include.redist.c% |
e63ce3e7 KB |
9 | */ |
10 | ||
11 | #ifndef lint | |
a93b814a | 12 | static char sccsid[] = "@(#)look.c 5.5 (Berkeley) %G%"; |
e63ce3e7 KB |
13 | #endif /* not lint */ |
14 | ||
15 | /* | |
16 | * look.c | |
17 | * Facility: m4 macro processor | |
18 | * by: oz | |
19 | */ | |
20 | ||
ba487a99 | 21 | #include <sys/types.h> |
50841d34 KB |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
e63ce3e7 | 25 | #include "mdef.h" |
ba487a99 KB |
26 | #include "stdd.h" |
27 | #include "extern.h" | |
e63ce3e7 | 28 | |
ba487a99 KB |
29 | int |
30 | hash(name) | |
e63ce3e7 KB |
31 | register char *name; |
32 | { | |
ba487a99 | 33 | register unsigned long h = 0; |
e63ce3e7 | 34 | while (*name) |
ba487a99 | 35 | h = (h << 5) + h + *name++; |
e63ce3e7 KB |
36 | return (h % HASHSIZE); |
37 | } | |
38 | ||
39 | /* | |
ba487a99 | 40 | * find name in the hash table |
e63ce3e7 | 41 | */ |
ba487a99 KB |
42 | ndptr |
43 | lookup(name) | |
e63ce3e7 KB |
44 | char *name; |
45 | { | |
46 | register ndptr p; | |
47 | ||
48 | for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr) | |
ba487a99 | 49 | if (STREQ(name, p->name)) |
e63ce3e7 KB |
50 | break; |
51 | return (p); | |
52 | } | |
53 | ||
54 | /* | |
ba487a99 KB |
55 | * hash and create an entry in the hash table. |
56 | * The new entry is added in front of a hash bucket. | |
e63ce3e7 | 57 | */ |
ba487a99 KB |
58 | ndptr |
59 | addent(name) | |
e63ce3e7 KB |
60 | char *name; |
61 | { | |
62 | register int h; | |
63 | ndptr p; | |
64 | ||
65 | h = hash(name); | |
ba487a99 KB |
66 | p = (ndptr) xalloc(sizeof(struct ndblock)); |
67 | p->nxtptr = hashtab[h]; | |
68 | hashtab[h] = p; | |
69 | p->name = xstrdup(name); | |
e63ce3e7 KB |
70 | return p; |
71 | } | |
72 | ||
ba487a99 KB |
73 | static void |
74 | freent(p) | |
75 | ndptr p; | |
76 | { | |
77 | if (!(p->type & STATIC)) { | |
78 | free((char *) p->name); | |
79 | if (p->defn != null) | |
80 | free((char *) p->defn); | |
81 | } | |
82 | free((char *) p); | |
83 | } | |
84 | ||
e63ce3e7 | 85 | /* |
ba487a99 | 86 | * remove an entry from the hashtable |
e63ce3e7 | 87 | */ |
ba487a99 | 88 | void |
e63ce3e7 KB |
89 | remhash(name, all) |
90 | char *name; | |
91 | int all; | |
92 | { | |
93 | register int h; | |
94 | register ndptr xp, tp, mp; | |
95 | ||
96 | h = hash(name); | |
97 | mp = hashtab[h]; | |
98 | tp = nil; | |
99 | while (mp != nil) { | |
ba487a99 | 100 | if (STREQ(mp->name, name)) { |
e63ce3e7 KB |
101 | mp = mp->nxtptr; |
102 | if (tp == nil) { | |
103 | freent(hashtab[h]); | |
104 | hashtab[h] = mp; | |
105 | } | |
106 | else { | |
107 | xp = tp->nxtptr; | |
108 | tp->nxtptr = mp; | |
109 | freent(xp); | |
110 | } | |
111 | if (!all) | |
112 | break; | |
113 | } | |
114 | else { | |
115 | tp = mp; | |
116 | mp = mp->nxtptr; | |
117 | } | |
118 | } | |
119 | } |