122373Sdist /* 222373Sdist * Copyright (c) 1983 Regents of the University of California. 3*33415Sbostic * All rights reserved. 4*33415Sbostic * 5*33415Sbostic * Redistribution and use in source and binary forms are permitted 6*33415Sbostic * provided that this notice is preserved and that due credit is given 7*33415Sbostic * to the University of California at Berkeley. The name of the University 8*33415Sbostic * may not be used to endorse or promote products derived from this 9*33415Sbostic * software without specific prior written permission. This software 10*33415Sbostic * is provided ``as is'' without express or implied warranty. 1122373Sdist */ 1222373Sdist 1315110Sralph #ifndef lint 14*33415Sbostic static char sccsid[] = "@(#)lookup.c 5.3 (Berkeley) 02/01/88"; 15*33415Sbostic #endif /* not lint */ 1615110Sralph 1715110Sralph #include "defs.h" 1815110Sralph 1916028Sralph /* symbol types */ 2016028Sralph #define VAR 1 2116028Sralph #define CONST 2 2216028Sralph 2316028Sralph struct syment { 2416028Sralph int s_type; 2516028Sralph char *s_name; 2616028Sralph struct namelist *s_value; 2716028Sralph struct syment *s_next; 2816028Sralph }; 2916028Sralph 3016028Sralph static struct syment *hashtab[HASHSIZE]; 3116028Sralph 3215110Sralph /* 3315110Sralph * Define a variable from a command line argument. 3415110Sralph */ 3515110Sralph define(name) 3615110Sralph char *name; 3715110Sralph { 3815110Sralph register char *cp, *s; 3916028Sralph register struct namelist *nl; 4016028Sralph struct namelist *value; 4115110Sralph 4215110Sralph if (debug) 4315110Sralph printf("define(%s)\n", name); 4415110Sralph 4515110Sralph cp = index(name, '='); 4615200Sralph if (cp == NULL) 4715110Sralph value = NULL; 4815200Sralph else if (cp[1] == '\0') { 4916028Sralph *cp = '\0'; 5015200Sralph value = NULL; 5115200Sralph } else if (cp[1] != '(') { 5215200Sralph *cp++ = '\0'; 5316059Sralph value = makenl(cp); 5415110Sralph } else { 5516028Sralph nl = NULL; 5615110Sralph *cp++ = '\0'; 5715110Sralph do 5815110Sralph cp++; 5915110Sralph while (*cp == ' ' || *cp == '\t'); 6015110Sralph for (s = cp; ; s++) { 6115110Sralph switch (*s) { 6215110Sralph case ')': 6315110Sralph *s = '\0'; 6415110Sralph case '\0': 6515110Sralph break; 6615110Sralph case ' ': 6715110Sralph case '\t': 6815110Sralph *s++ = '\0'; 6915110Sralph while (*s == ' ' || *s == '\t') 7015110Sralph s++; 7115110Sralph if (*s == ')') 7215110Sralph *s = '\0'; 7315110Sralph break; 7415110Sralph default: 7515110Sralph continue; 7615110Sralph } 7716028Sralph if (nl == NULL) 7816028Sralph value = nl = makenl(cp); 7915110Sralph else { 8016028Sralph nl->n_next = makenl(cp); 8116028Sralph nl = nl->n_next; 8215110Sralph } 8315110Sralph if (*s == '\0') 8415110Sralph break; 8515110Sralph cp = s; 8615110Sralph } 8715110Sralph } 8816028Sralph (void) lookup(name, REPLACE, value); 8915110Sralph } 9015110Sralph 9115110Sralph /* 9215110Sralph * Lookup name in the table and return a pointer to it. 9316028Sralph * LOOKUP - just do lookup, return NULL if not found. 9416028Sralph * INSERT - insert name with value, error if already defined. 9516028Sralph * REPLACE - insert or replace name with value. 9615110Sralph */ 9715110Sralph 9816028Sralph struct namelist * 9916028Sralph lookup(name, action, value) 10015110Sralph char *name; 10116028Sralph int action; 10216028Sralph struct namelist *value; 10315110Sralph { 10415110Sralph register unsigned n; 10515110Sralph register char *cp; 10616028Sralph register struct syment *s; 10717917Sralph char buf[256]; 10815110Sralph 10915110Sralph if (debug) 11016028Sralph printf("lookup(%s, %d, %x)\n", name, action, value); 11115110Sralph 11215110Sralph n = 0; 11315110Sralph for (cp = name; *cp; ) 11415110Sralph n += *cp++; 11515110Sralph n %= HASHSIZE; 11615110Sralph 11716028Sralph for (s = hashtab[n]; s != NULL; s = s->s_next) { 11816028Sralph if (strcmp(name, s->s_name)) 11915110Sralph continue; 12016028Sralph if (action != LOOKUP) { 12117917Sralph if (action != INSERT || s->s_type != CONST) { 12232503Sbostic (void)sprintf(buf, "%s redefined", name); 12317917Sralph yyerror(buf); 12417917Sralph } 12515110Sralph } 12616028Sralph return(s->s_value); 12715110Sralph } 12815110Sralph 12917917Sralph if (action == LOOKUP) { 13032503Sbostic (void)sprintf(buf, "%s undefined", name); 13132503Sbostic yyerror(buf); 13217917Sralph return(NULL); 13317917Sralph } 13415110Sralph 13516028Sralph s = ALLOC(syment); 13616028Sralph if (s == NULL) 13716028Sralph fatal("ran out of memory\n"); 13816028Sralph s->s_next = hashtab[n]; 13916028Sralph hashtab[n] = s; 14016028Sralph s->s_type = action == INSERT ? VAR : CONST; 14116028Sralph s->s_name = name; 14216028Sralph s->s_value = value; 14315110Sralph return(value); 14415110Sralph } 145