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