lookup.c revision 1.5 1 /* $NetBSD: lookup.c,v 1.5 1996/07/12 00:46:25 thorpej 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)lookup.c 8.1 (Berkeley) 6/9/93";
39 #else
40 static char *rcsid = "$NetBSD: lookup.c,v 1.5 1996/07/12 00:46:25 thorpej Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include "defs.h"
45
46 /* symbol types */
47 #define VAR 1
48 #define CONST 2
49
50 struct syment {
51 int s_type;
52 char *s_name;
53 struct namelist *s_value;
54 struct syment *s_next;
55 };
56
57 static struct syment *hashtab[HASHSIZE];
58
59 /*
60 * Define a variable from a command line argument.
61 */
62 void
63 define(name)
64 char *name;
65 {
66 register char *cp, *s;
67 register struct namelist *nl;
68 struct namelist *value;
69
70 if (debug)
71 printf("define(%s)\n", name);
72
73 cp = index(name, '=');
74 if (cp == NULL)
75 value = NULL;
76 else if (cp[1] == '\0') {
77 *cp = '\0';
78 value = NULL;
79 } else if (cp[1] != '(') {
80 *cp++ = '\0';
81 value = makenl(cp);
82 } else {
83 nl = NULL;
84 *cp++ = '\0';
85 do
86 cp++;
87 while (*cp == ' ' || *cp == '\t');
88 for (s = cp; ; s++) {
89 switch (*s) {
90 case ')':
91 *s = '\0';
92 case '\0':
93 break;
94 case ' ':
95 case '\t':
96 *s++ = '\0';
97 while (*s == ' ' || *s == '\t')
98 s++;
99 if (*s == ')')
100 *s = '\0';
101 break;
102 default:
103 continue;
104 }
105 if (nl == NULL)
106 value = nl = makenl(cp);
107 else {
108 nl->n_next = makenl(cp);
109 nl = nl->n_next;
110 }
111 if (*s == '\0')
112 break;
113 cp = s;
114 }
115 }
116 (void) lookup(name, REPLACE, value);
117 }
118
119 /*
120 * Lookup name in the table and return a pointer to it.
121 * LOOKUP - just do lookup, return NULL if not found.
122 * INSERT - insert name with value, error if already defined.
123 * REPLACE - insert or replace name with value.
124 */
125
126 struct namelist *
127 lookup(name, action, value)
128 char *name;
129 int action;
130 struct namelist *value;
131 {
132 register unsigned n;
133 register char *cp;
134 register struct syment *s;
135 char buf[256];
136
137 if (debug)
138 printf("lookup(%s, %d, %x)\n", 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(buf, sizeof(buf),
151 "%s redefined", name);
152 yyerror(buf);
153 }
154 }
155 return(s->s_value);
156 }
157
158 if (action == LOOKUP) {
159 (void)snprintf(buf, sizeof(buf), "%s undefined", name);
160 yyerror(buf);
161 return(NULL);
162 }
163
164 s = ALLOC(syment);
165 if (s == NULL)
166 fatal("ran out of memory\n");
167 s->s_next = hashtab[n];
168 hashtab[n] = s;
169 s->s_type = action == INSERT ? VAR : CONST;
170 s->s_name = name;
171 s->s_value = value;
172 return(value);
173 }
174