Rev | Line | |
---|
[9] | 1 | /*
|
---|
| 2 | * look.c
|
---|
| 3 | * Facility: m4 macro processor
|
---|
| 4 | * by: oz
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #include "mdef.h"
|
---|
| 8 | #include "extr.h"
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
| 11 | * hash - compute hash value using the proverbial
|
---|
| 12 | * hashing function. Taken from K&R.
|
---|
| 13 | */
|
---|
| 14 | int hash (name)
|
---|
| 15 | register char *name;
|
---|
| 16 | {
|
---|
| 17 | register int h = 0;
|
---|
| 18 | while (*name)
|
---|
| 19 | h += *name++;
|
---|
| 20 | return (h % HASHSIZE);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * lookup - find name in the hash table
|
---|
| 25 | *
|
---|
| 26 | */
|
---|
| 27 | ndptr lookup(name)
|
---|
| 28 | char *name;
|
---|
| 29 | {
|
---|
| 30 | register ndptr p;
|
---|
| 31 |
|
---|
| 32 | for (p = hashtab[hash(name)]; p != nil; p = p->nxtptr)
|
---|
| 33 | if (strcmp(name, p->name) == 0)
|
---|
| 34 | break;
|
---|
| 35 | return (p);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /*
|
---|
| 39 | * addent - hash and create an entry in the hash
|
---|
| 40 | * table. The new entry is added in front
|
---|
| 41 | * of a hash bucket.
|
---|
| 42 | */
|
---|
| 43 | ndptr addent(name)
|
---|
| 44 | char *name;
|
---|
| 45 | {
|
---|
| 46 | register int h;
|
---|
| 47 | ndptr p;
|
---|
| 48 |
|
---|
| 49 | h = hash(name);
|
---|
| 50 | if ((p = (ndptr) malloc(sizeof(struct ndblock))) != NULL) {
|
---|
| 51 | p->nxtptr = hashtab[h];
|
---|
| 52 | hashtab[h] = p;
|
---|
| 53 | p->name = strsave(name);
|
---|
| 54 | }
|
---|
| 55 | else
|
---|
| 56 | error("m4: no more memory.");
|
---|
| 57 | return p;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /*
|
---|
| 61 | * remhash - remove an entry from the hashtable
|
---|
| 62 | *
|
---|
| 63 | */
|
---|
| 64 | void remhash(name, all)
|
---|
| 65 | char *name;
|
---|
| 66 | int all;
|
---|
| 67 | {
|
---|
| 68 | register int h;
|
---|
| 69 | register ndptr xp, tp, mp;
|
---|
| 70 |
|
---|
| 71 | h = hash(name);
|
---|
| 72 | mp = hashtab[h];
|
---|
| 73 | tp = nil;
|
---|
| 74 | while (mp != nil) {
|
---|
| 75 | if (strcmp(mp->name, name) == 0) {
|
---|
| 76 | mp = mp->nxtptr;
|
---|
| 77 | if (tp == nil) {
|
---|
| 78 | freent(hashtab[h]);
|
---|
| 79 | hashtab[h] = mp;
|
---|
| 80 | }
|
---|
| 81 | else {
|
---|
| 82 | xp = tp->nxtptr;
|
---|
| 83 | tp->nxtptr = mp;
|
---|
| 84 | freent(xp);
|
---|
| 85 | }
|
---|
| 86 | if (!all)
|
---|
| 87 | break;
|
---|
| 88 | }
|
---|
| 89 | else {
|
---|
| 90 | tp = mp;
|
---|
| 91 | mp = mp->nxtptr;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /*
|
---|
| 97 | * freent - free a hashtable information block
|
---|
| 98 | *
|
---|
| 99 | */
|
---|
| 100 | void freent(p)
|
---|
| 101 | ndptr p;
|
---|
| 102 | {
|
---|
| 103 | if (!(p->type & STATIC)) {
|
---|
| 104 | free(p->name);
|
---|
| 105 | if (p->defn != null)
|
---|
| 106 | free(p->defn);
|
---|
| 107 | }
|
---|
| 108 | free(p);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.