1 /* $NetBSD: lookup.c,v 1.7 2002/06/14 01:18:55 wiz 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.7 2002/06/14 01:18:55 wiz 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(char *name) 65 { 66 char *cp, *s; 67 struct namelist *nl; 68 struct namelist *value; 69 70 value = NULL; 71 72 if (debug) 73 printf("define(%s)\n", name); 74 75 cp = strchr(name, '='); 76 if (cp == NULL) 77 value = NULL; 78 else if (cp[1] == '\0') { 79 *cp = '\0'; 80 value = NULL; 81 } else if (cp[1] != '(') { 82 *cp++ = '\0'; 83 value = makenl(cp); 84 } else { 85 nl = NULL; 86 *cp++ = '\0'; 87 do 88 cp++; 89 while (*cp == ' ' || *cp == '\t'); 90 for (s = cp; ; s++) { 91 switch (*s) { 92 case ')': 93 *s = '\0'; 94 case '\0': 95 break; 96 case ' ': 97 case '\t': 98 *s++ = '\0'; 99 while (*s == ' ' || *s == '\t') 100 s++; 101 if (*s == ')') 102 *s = '\0'; 103 break; 104 default: 105 continue; 106 } 107 if (nl == NULL) 108 value = nl = makenl(cp); 109 else { 110 nl->n_next = makenl(cp); 111 nl = nl->n_next; 112 } 113 if (*s == '\0') 114 break; 115 cp = s; 116 } 117 } 118 (void) lookup(name, REPLACE, value); 119 } 120 121 /* 122 * Lookup name in the table and return a pointer to it. 123 * LOOKUP - just do lookup, return NULL if not found. 124 * INSERT - insert name with value, error if already defined. 125 * REPLACE - insert or replace name with value. 126 */ 127 128 struct namelist * 129 lookup(char *name, int action, struct namelist *value) 130 { 131 unsigned n; 132 char *cp; 133 struct syment *s; 134 char buf[256]; 135 136 if (debug) 137 printf("lookup(%s, %d, %lx)\n", name, action, (long)value); 138 139 n = 0; 140 for (cp = name; *cp; ) 141 n += *cp++; 142 n %= HASHSIZE; 143 144 for (s = hashtab[n]; s != NULL; s = s->s_next) { 145 if (strcmp(name, s->s_name)) 146 continue; 147 if (action != LOOKUP) { 148 if (action != INSERT || s->s_type != CONST) { 149 (void)snprintf(buf, sizeof(buf), 150 "%s redefined", name); 151 yyerror(buf); 152 } 153 } 154 return(s->s_value); 155 } 156 157 if (action == LOOKUP) { 158 (void)snprintf(buf, sizeof(buf), "%s undefined", name); 159 yyerror(buf); 160 return(NULL); 161 } 162 163 s = ALLOC(syment); 164 if (s == NULL) 165 fatal("ran out of memory\n"); 166 s->s_next = hashtab[n]; 167 hashtab[n] = s; 168 s->s_type = action == INSERT ? VAR : CONST; 169 s->s_name = name; 170 s->s_value = value; 171 return(value); 172 } 173