115110Sralph #ifndef lint 2*16059Sralph static char *sccsid = "@(#)lookup.c 4.5 (Berkeley) 84/02/16"; 315110Sralph #endif 415110Sralph 515110Sralph #include "defs.h" 615110Sralph 716028Sralph /* symbol types */ 816028Sralph #define VAR 1 916028Sralph #define CONST 2 1016028Sralph 1116028Sralph struct syment { 1216028Sralph int s_type; 1316028Sralph char *s_name; 1416028Sralph struct namelist *s_value; 1516028Sralph struct syment *s_next; 1616028Sralph }; 1716028Sralph 1816028Sralph static struct syment *hashtab[HASHSIZE]; 1916028Sralph 2015110Sralph /* 2115110Sralph * Define a variable from a command line argument. 2215110Sralph */ 2315110Sralph define(name) 2415110Sralph char *name; 2515110Sralph { 2615110Sralph register char *cp, *s; 2716028Sralph register struct namelist *nl; 2816028Sralph struct namelist *value; 2915110Sralph 3015110Sralph if (debug) 3115110Sralph printf("define(%s)\n", name); 3215110Sralph 3315110Sralph cp = index(name, '='); 3415200Sralph if (cp == NULL) 3515110Sralph value = NULL; 3615200Sralph else if (cp[1] == '\0') { 3716028Sralph *cp = '\0'; 3815200Sralph value = NULL; 3915200Sralph } else if (cp[1] != '(') { 4015200Sralph *cp++ = '\0'; 41*16059Sralph value = makenl(cp); 4215110Sralph } else { 4316028Sralph nl = NULL; 4415110Sralph *cp++ = '\0'; 4515110Sralph do 4615110Sralph cp++; 4715110Sralph while (*cp == ' ' || *cp == '\t'); 4815110Sralph for (s = cp; ; s++) { 4915110Sralph switch (*s) { 5015110Sralph case ')': 5115110Sralph *s = '\0'; 5215110Sralph case '\0': 5315110Sralph break; 5415110Sralph case ' ': 5515110Sralph case '\t': 5615110Sralph *s++ = '\0'; 5715110Sralph while (*s == ' ' || *s == '\t') 5815110Sralph s++; 5915110Sralph if (*s == ')') 6015110Sralph *s = '\0'; 6115110Sralph break; 6215110Sralph default: 6315110Sralph continue; 6415110Sralph } 6516028Sralph if (nl == NULL) 6616028Sralph value = nl = makenl(cp); 6715110Sralph else { 6816028Sralph nl->n_next = makenl(cp); 6916028Sralph nl = nl->n_next; 7015110Sralph } 7115110Sralph if (*s == '\0') 7215110Sralph break; 7315110Sralph cp = s; 7415110Sralph } 7515110Sralph } 7616028Sralph (void) lookup(name, REPLACE, value); 7715110Sralph } 7815110Sralph 7915110Sralph /* 8015110Sralph * Lookup name in the table and return a pointer to it. 8116028Sralph * LOOKUP - just do lookup, return NULL if not found. 8216028Sralph * INSERT - insert name with value, error if already defined. 8316028Sralph * REPLACE - insert or replace name with value. 8415110Sralph */ 8515110Sralph 8616028Sralph struct namelist * 8716028Sralph lookup(name, action, value) 8815110Sralph char *name; 8916028Sralph int action; 9016028Sralph struct namelist *value; 9115110Sralph { 9215110Sralph register unsigned n; 9315110Sralph register char *cp; 9416028Sralph register struct syment *s; 9515110Sralph 9615110Sralph if (debug) 9716028Sralph printf("lookup(%s, %d, %x)\n", name, action, value); 9815110Sralph 9915110Sralph n = 0; 10015110Sralph for (cp = name; *cp; ) 10115110Sralph n += *cp++; 10215110Sralph n %= HASHSIZE; 10315110Sralph 10416028Sralph for (s = hashtab[n]; s != NULL; s = s->s_next) { 10516028Sralph if (strcmp(name, s->s_name)) 10615110Sralph continue; 10716028Sralph if (action != LOOKUP) { 108*16059Sralph if (action != INSERT || s->s_type != CONST) 10916028Sralph fatal("%s redefined\n", name); 11015110Sralph } 11116028Sralph return(s->s_value); 11215110Sralph } 11315110Sralph 11416028Sralph if (action == LOOKUP) 11515110Sralph fatal("%s not defined", name); 11615110Sralph 11716028Sralph s = ALLOC(syment); 11816028Sralph if (s == NULL) 11916028Sralph fatal("ran out of memory\n"); 12016028Sralph s->s_next = hashtab[n]; 12116028Sralph hashtab[n] = s; 12216028Sralph s->s_type = action == INSERT ? VAR : CONST; 12316028Sralph s->s_name = name; 12416028Sralph s->s_value = value; 12515110Sralph return(value); 12615110Sralph } 127