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