lookup.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef lint
  2. static char *sccsid = "@(#)lookup.c 4.5 (Berkeley) 84/02/16";
  3. #endif
  4. #include "defs.h"
  5. /* symbol types */
  6. #define VAR 1
  7. #define CONST 2
  8. struct syment {
  9. int s_type;
  10. char *s_name;
  11. struct namelist *s_value;
  12. struct syment *s_next;
  13. };
  14. static struct syment *hashtab[HASHSIZE];
  15. /*
  16. * Define a variable from a command line argument.
  17. */
  18. define(name)
  19. char *name;
  20. {
  21. register char *cp, *s;
  22. register struct namelist *nl;
  23. struct namelist *value;
  24. if (debug)
  25. printf("define(%s)\n", name);
  26. cp = index(name, '=');
  27. if (cp == NULL)
  28. value = NULL;
  29. else if (cp[1] == '\0') {
  30. *cp = '\0';
  31. value = NULL;
  32. } else if (cp[1] != '(') {
  33. *cp++ = '\0';
  34. value = makenl(cp);
  35. } else {
  36. nl = NULL;
  37. *cp++ = '\0';
  38. do
  39. cp++;
  40. while (*cp == ' ' || *cp == '\t');
  41. for (s = cp; ; s++) {
  42. switch (*s) {
  43. case ')':
  44. *s = '\0';
  45. case '\0':
  46. break;
  47. case ' ':
  48. case '\t':
  49. *s++ = '\0';
  50. while (*s == ' ' || *s == '\t')
  51. s++;
  52. if (*s == ')')
  53. *s = '\0';
  54. break;
  55. default:
  56. continue;
  57. }
  58. if (nl == NULL)
  59. value = nl = makenl(cp);
  60. else {
  61. nl->n_next = makenl(cp);
  62. nl = nl->n_next;
  63. }
  64. if (*s == '\0')
  65. break;
  66. cp = s;
  67. }
  68. }
  69. (void) lookup(name, REPLACE, value);
  70. }
  71. /*
  72. * Lookup name in the table and return a pointer to it.
  73. * LOOKUP - just do lookup, return NULL if not found.
  74. * INSERT - insert name with value, error if already defined.
  75. * REPLACE - insert or replace name with value.
  76. */
  77. struct namelist *
  78. lookup(name, action, value)
  79. char *name;
  80. int action;
  81. struct namelist *value;
  82. {
  83. register unsigned n;
  84. register char *cp;
  85. register struct syment *s;
  86. if (debug)
  87. printf("lookup(%s, %d, %x)\n", name, action, value);
  88. n = 0;
  89. for (cp = name; *cp; )
  90. n += *cp++;
  91. n %= HASHSIZE;
  92. for (s = hashtab[n]; s != NULL; s = s->s_next) {
  93. if (strcmp(name, s->s_name))
  94. continue;
  95. if (action != LOOKUP) {
  96. if (action != INSERT || s->s_type != CONST)
  97. fatal("%s redefined\n", name);
  98. }
  99. return(s->s_value);
  100. }
  101. if (action == LOOKUP)
  102. fatal("%s not defined", name);
  103. s = ALLOC(syment);
  104. if (s == NULL)
  105. fatal("ran out of memory\n");
  106. s->s_next = hashtab[n];
  107. hashtab[n] = s;
  108. s->s_type = action == INSERT ? VAR : CONST;
  109. s->s_name = name;
  110. s->s_value = value;
  111. return(value);
  112. }