|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1983 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: static char sccsid[] = "@(#)lookup.c 5.5 (Berkeley) 6/1/90"; ! 22: #endif /* not lint */ ! 23: ! 24: #include "defs.h" ! 25: ! 26: /* symbol types */ ! 27: #define VAR 1 ! 28: #define CONST 2 ! 29: ! 30: struct syment { ! 31: int s_type; ! 32: char *s_name; ! 33: struct namelist *s_value; ! 34: struct syment *s_next; ! 35: }; ! 36: ! 37: static struct syment *hashtab[HASHSIZE]; ! 38: ! 39: /* ! 40: * Define a variable from a command line argument. ! 41: */ ! 42: define(name) ! 43: char *name; ! 44: { ! 45: register char *cp, *s; ! 46: register struct namelist *nl; ! 47: struct namelist *value; ! 48: ! 49: if (debug) ! 50: printf("define(%s)\n", name); ! 51: ! 52: cp = index(name, '='); ! 53: if (cp == NULL) ! 54: value = NULL; ! 55: else if (cp[1] == '\0') { ! 56: *cp = '\0'; ! 57: value = NULL; ! 58: } else if (cp[1] != '(') { ! 59: *cp++ = '\0'; ! 60: value = makenl(cp); ! 61: } else { ! 62: nl = NULL; ! 63: *cp++ = '\0'; ! 64: do ! 65: cp++; ! 66: while (*cp == ' ' || *cp == '\t'); ! 67: for (s = cp; ; s++) { ! 68: switch (*s) { ! 69: case ')': ! 70: *s = '\0'; ! 71: case '\0': ! 72: break; ! 73: case ' ': ! 74: case '\t': ! 75: *s++ = '\0'; ! 76: while (*s == ' ' || *s == '\t') ! 77: s++; ! 78: if (*s == ')') ! 79: *s = '\0'; ! 80: break; ! 81: default: ! 82: continue; ! 83: } ! 84: if (nl == NULL) ! 85: value = nl = makenl(cp); ! 86: else { ! 87: nl->n_next = makenl(cp); ! 88: nl = nl->n_next; ! 89: } ! 90: if (*s == '\0') ! 91: break; ! 92: cp = s; ! 93: } ! 94: } ! 95: (void) lookup(name, REPLACE, value); ! 96: } ! 97: ! 98: /* ! 99: * Lookup name in the table and return a pointer to it. ! 100: * LOOKUP - just do lookup, return NULL if not found. ! 101: * INSERT - insert name with value, error if already defined. ! 102: * REPLACE - insert or replace name with value. ! 103: */ ! 104: ! 105: struct namelist * ! 106: lookup(name, action, value) ! 107: char *name; ! 108: int action; ! 109: struct namelist *value; ! 110: { ! 111: register unsigned n; ! 112: register char *cp; ! 113: register struct syment *s; ! 114: char buf[256]; ! 115: ! 116: if (debug) ! 117: printf("lookup(%s, %d, %x)\n", name, action, value); ! 118: ! 119: n = 0; ! 120: for (cp = name; *cp; ) ! 121: n += *cp++; ! 122: n %= HASHSIZE; ! 123: ! 124: for (s = hashtab[n]; s != NULL; s = s->s_next) { ! 125: if (strcmp(name, s->s_name)) ! 126: continue; ! 127: if (action != LOOKUP) { ! 128: if (action != INSERT || s->s_type != CONST) { ! 129: (void)sprintf(buf, "%s redefined", name); ! 130: yyerror(buf); ! 131: } ! 132: } ! 133: return(s->s_value); ! 134: } ! 135: ! 136: if (action == LOOKUP) { ! 137: (void)sprintf(buf, "%s undefined", name); ! 138: yyerror(buf); ! 139: return(NULL); ! 140: } ! 141: ! 142: s = ALLOC(syment); ! 143: if (s == NULL) ! 144: fatal("ran out of memory\n"); ! 145: s->s_next = hashtab[n]; ! 146: hashtab[n] = s; ! 147: s->s_type = action == INSERT ? VAR : CONST; ! 148: s->s_name = name; ! 149: s->s_value = value; ! 150: return(value); ! 151: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.