1 /* $NetBSD: lookup.c,v 1.6 1997/10/19 13:59:04 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)lookup.c 8.1 (Berkeley) 6/9/93"; 40 #else 41 __RCSID("$NetBSD: lookup.c,v 1.6 1997/10/19 13:59:04 lukem Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include "defs.h" 46 47 /* symbol types */ 48 #define VAR 1 49 #define CONST 2 50 51 struct syment { 52 int s_type; 53 char *s_name; 54 struct namelist *s_value; 55 struct syment *s_next; 56 }; 57 58 static struct syment *hashtab[HASHSIZE]; 59 60 /* 61 * Define a variable from a command line argument. 62 */ 63 void 64 define(name) 65 char *name; 66 { 67 char *cp, *s; 68 struct namelist *nl; 69 struct namelist *value; 70 71 value = NULL; 72 73 if (debug) 74 printf("define(%s)\n", name); 75 76 cp = strchr(name, '='); 77 if (cp == NULL) 78 value = NULL; 79 else if (cp[1] == '\0') { 80 *cp = '\0'; 81 value = NULL; 82 } else if (cp[1] != '(') { 83 *cp++ = '\0'; 84 value = makenl(cp); 85 } else { 86 nl = NULL; 87 *cp++ = '\0'; 88 do 89 cp++; 90 while (*cp == ' ' || *cp == '\t'); 91 for (s = cp; ; s++) { 92 switch (*s) { 93 case ')': 94 *s = '\0'; 95 case '\0': 96 break; 97 case ' ': 98 case '\t': 99 *s++ = '\0'; 100 while (*s == ' ' || *s == '\t') 101 s++; 102 if (*s == ')') 103 *s = '\0'; 104 break; 105 default: 106 continue; 107 } 108 if (nl == NULL) 109 value = nl = makenl(cp); 110 else { 111 nl->n_next = makenl(cp); 112 nl = nl->n_next; 113 } 114 if (*s == '\0') 115 break; 116 cp = s; 117 } 118 } 119 (void) lookup(name, REPLACE, value); 120 } 121 122 /* 123 * Lookup name in the table and return a pointer to it. 124 * LOOKUP - just do lookup, return NULL if not found. 125 * INSERT - insert name with value, error if already defined. 126 * REPLACE - insert or replace name with value. 127 */ 128 129 struct namelist * 130 lookup(name, action, value) 131 char *name; 132 int action; 133 struct namelist *value; 134 { 135 unsigned n; 136 char *cp; 137 struct syment *s; 138 char buf[256]; 139 140 if (debug) 141 printf("lookup(%s, %d, %lx)\n", name, action, (long)value); 142 143 n = 0; 144 for (cp = name; *cp; ) 145 n += *cp++; 146 n %= HASHSIZE; 147 148 for (s = hashtab[n]; s != NULL; s = s->s_next) { 149 if (strcmp(name, s->s_name)) 150 continue; 151 if (action != LOOKUP) { 152 if (action != INSERT || s->s_type != CONST) { 153 (void)snprintf(buf, sizeof(buf), 154 "%s redefined", name); 155 yyerror(buf); 156 } 157 } 158 return(s->s_value); 159 } 160 161 if (action == LOOKUP) { 162 (void)snprintf(buf, sizeof(buf), "%s undefined", name); 163 yyerror(buf); 164 return(NULL); 165 } 166 167 s = ALLOC(syment); 168 if (s == NULL) 169 fatal("ran out of memory\n"); 170 s->s_next = hashtab[n]; 171 hashtab[n] = s; 172 s->s_type = action == INSERT ? VAR : CONST; 173 s->s_name = name; 174 s->s_value = value; 175 return(value); 176 } 177