home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 138.lha / M4 / Sources / look.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  2KB  |  102 lines

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