1 /* $OpenBSD: lookup.c,v 1.9 2001/11/19 19:02:15 mpech 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. 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 #ifndef lint 37 #if 0 38 static char RCSid[] = 39 "$From: lookup.c,v 6.8 1996/07/19 16:49:55 michaelc Exp $"; 40 #else 41 static char RCSid[] = 42 "$OpenBSD: lookup.c,v 1.9 2001/11/19 19:02:15 mpech Exp $"; 43 #endif 44 45 static char sccsid[] = "@(#)lookup.c 5.1 (Berkeley) 6/6/85"; 46 47 static char copyright[] = 48 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 49 All rights reserved.\n"; 50 #endif /* not lint */ 51 52 #include "defs.h" 53 54 /* symbol types */ 55 #define VAR 1 56 #define CONST 2 57 58 struct syment { 59 int s_type; 60 char *s_name; 61 struct namelist *s_value; 62 struct syment *s_next; 63 }; 64 65 static struct syment *hashtab[HASHSIZE]; 66 67 /* 68 * Define a variable from a command line argument. 69 */ 70 void 71 define(name) 72 char *name; 73 { 74 char *cp, *s; 75 struct namelist *nl; 76 struct namelist *value; 77 78 debugmsg(DM_CALL, "define(%s)", name); 79 80 cp = strchr(name, '='); 81 if (cp == NULL) 82 value = NULL; 83 else if (cp[1] == '\0') { 84 *cp = '\0'; 85 value = NULL; 86 } else if (cp[1] != '(') { 87 *cp++ = '\0'; 88 value = makenl(cp); 89 } else { 90 value = NULL; 91 nl = NULL; 92 *cp++ = '\0'; 93 do 94 cp++; 95 while (*cp == ' ' || *cp == '\t'); 96 for (s = cp; ; s++) { 97 switch (*s) { 98 case ')': 99 *s = '\0'; 100 case '\0': 101 break; 102 case ' ': 103 case '\t': 104 *s++ = '\0'; 105 while (*s == ' ' || *s == '\t') 106 s++; 107 if (*s == ')') 108 *s = '\0'; 109 break; 110 default: 111 continue; 112 } 113 if (nl == NULL) 114 value = nl = makenl(cp); 115 else { 116 nl->n_next = makenl(cp); 117 nl = nl->n_next; 118 } 119 if (*s == '\0') 120 break; 121 cp = s; 122 } 123 } 124 (void) lookup(name, REPLACE, value); 125 } 126 127 /* 128 * Lookup name in the table and return a pointer to it. 129 * LOOKUP - just do lookup, return NULL if not found. 130 * INSERT - insert name with value, error if already defined. 131 * REPLACE - insert or replace name with value. 132 */ 133 134 struct namelist * 135 lookup(name, action, value) /* %% in name. Ignore quotas in name */ 136 char *name; 137 int action; 138 struct namelist *value; 139 { 140 unsigned n; 141 char *cp; 142 struct syment *s; 143 char ebuf[BUFSIZ]; 144 145 debugmsg(DM_CALL, "lookup(%s, %d, %x)", name, action, value); 146 147 n = 0; 148 for (cp = name; *cp; ) 149 n += *cp++; 150 n %= HASHSIZE; 151 152 for (s = hashtab[n]; s != NULL; s = s->s_next) { 153 if (strcmp(name, s->s_name)) 154 continue; 155 if (action != LOOKUP) { 156 if (action != INSERT || s->s_type != CONST) { 157 (void) snprintf(ebuf, sizeof(ebuf), 158 "%s redefined", name); 159 yyerror(ebuf); 160 } 161 } 162 return(s->s_value); 163 } 164 165 if (action == LOOKUP) { 166 (void) snprintf(ebuf, sizeof(ebuf), "%s undefined", name); 167 yyerror(ebuf); 168 return(NULL); 169 } 170 171 s = ALLOC(syment); 172 s->s_next = hashtab[n]; 173 hashtab[n] = s; 174 s->s_type = action == INSERT ? VAR : CONST; 175 s->s_name = name; 176 s->s_value = value; 177 178 return(value); 179 } 180