1 /* $OpenBSD: lookup.c,v 1.12 2003/06/03 02:56:14 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1983 Regents of the University of California. 5 * 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "defs.h" 33 34 #ifndef lint 35 #if 0 36 static char RCSid[] __attribute__((__unused__)) = 37 "$From: lookup.c,v 1.4 1999/08/04 15:57:33 christos Exp $"; 38 #else 39 static char RCSid[] __attribute__((__unused__)) = 40 "$OpenBSD: lookup.c,v 1.12 2003/06/03 02:56:14 millert Exp $"; 41 #endif 42 43 static char sccsid[] __attribute__((__unused__)) = 44 "@(#)lookup.c 5.1 (Berkeley) 6/6/85"; 45 46 static char copyright[] __attribute__((__unused__)) = 47 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 48 All rights reserved.\n"; 49 #endif /* not lint */ 50 51 /* symbol types */ 52 #define VAR 1 53 #define CONST 2 54 55 struct syment { 56 int s_type; 57 char *s_name; 58 struct namelist *s_value; 59 struct syment *s_next; 60 }; 61 62 static struct syment *hashtab[HASHSIZE]; 63 64 /* 65 * Define a variable from a command line argument. 66 */ 67 void 68 define(char *name) 69 { 70 char *cp, *s; 71 struct namelist *nl; 72 struct namelist *value; 73 74 debugmsg(DM_CALL, "define(%s)", 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 value = NULL; 87 nl = NULL; 88 *cp++ = '\0'; 89 do 90 cp++; 91 while (*cp == ' ' || *cp == '\t'); 92 for (s = cp; ; s++) { 93 switch (*s) { 94 case ')': 95 *s = '\0'; 96 case '\0': 97 break; 98 case ' ': 99 case '\t': 100 *s++ = '\0'; 101 while (*s == ' ' || *s == '\t') 102 s++; 103 if (*s == ')') 104 *s = '\0'; 105 break; 106 default: 107 continue; 108 } 109 if (nl == NULL) 110 value = nl = makenl(cp); 111 else { 112 nl->n_next = makenl(cp); 113 nl = nl->n_next; 114 } 115 if (*s == '\0') 116 break; 117 cp = s; 118 } 119 } 120 (void) lookup(name, REPLACE, value); 121 } 122 123 /* 124 * Lookup name in the table and return a pointer to it. 125 * LOOKUP - just do lookup, return NULL if not found. 126 * INSERT - insert name with value, error if already defined. 127 * REPLACE - insert or replace name with value. 128 */ 129 130 struct namelist * 131 lookup(char *name, int action, struct namelist *value) 132 { 133 unsigned int n; 134 char *cp; 135 struct syment *s; 136 char ebuf[BUFSIZ]; 137 138 debugmsg(DM_CALL, "lookup(%s, %d, %x)", name, action, value); 139 140 n = 0; 141 for (cp = name; *cp; ) 142 n += *cp++; 143 n %= HASHSIZE; 144 145 for (s = hashtab[n]; s != NULL; s = s->s_next) { 146 if (strcmp(name, s->s_name)) 147 continue; 148 if (action != LOOKUP) { 149 if (action != INSERT || s->s_type != CONST) { 150 (void) snprintf(ebuf, sizeof(ebuf), 151 "%.*s redefined", 152 (int)(sizeof(ebuf) - 153 sizeof(" redefined")), name); 154 yyerror(ebuf); 155 } 156 } 157 return(s->s_value); 158 } 159 160 if (action == LOOKUP) { 161 (void) snprintf(ebuf, sizeof(ebuf), "%.*s undefined", 162 (int)(sizeof(ebuf) - sizeof(" undefined")), 163 name); 164 yyerror(ebuf); 165 return(NULL); 166 } 167 168 s = ALLOC(syment); 169 s->s_next = hashtab[n]; 170 hashtab[n] = s; 171 s->s_type = action == INSERT ? VAR : CONST; 172 s->s_name = name; 173 s->s_value = value; 174 175 return(value); 176 } 177