Annotation of src/usr.bin/oldrdist/lookup.c, Revision 1.9
1.9 ! otto 1: /* $OpenBSD: lookup.c,v 1.8 2003/06/03 02:56:14 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.
1.8 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 dm 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: #ifndef lint
33: /* from: static char sccsid[] = "@(#)lookup.c 8.1 (Berkeley) 6/9/93"; */
1.9 ! otto 34: static char *rcsid = "$OpenBSD: lookup.c,v 1.8 2003/06/03 02:56:14 millert Exp $";
1.1 dm 35: #endif /* not lint */
36:
37: #include "defs.h"
38:
39: /* symbol types */
40: #define VAR 1
41: #define CONST 2
42:
43: struct syment {
44: int s_type;
45: char *s_name;
46: struct namelist *s_value;
47: struct syment *s_next;
48: };
49:
50: static struct syment *hashtab[HASHSIZE];
51:
52: /*
53: * Define a variable from a command line argument.
54: */
55: void
56: define(name)
57: char *name;
58: {
1.6 mpech 59: char *cp, *s;
60: struct namelist *nl;
1.1 dm 61: struct namelist *value;
62:
63: if (debug)
64: printf("define(%s)\n", name);
65:
1.4 millert 66: cp = strchr(name, '=');
1.1 dm 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: {
1.7 deraadt 125: unsigned int n;
1.6 mpech 126: char *cp;
127: struct syment *s;
1.5 millert 128: char buf[BUFSIZ];
1.1 dm 129:
130: if (debug)
1.9 ! otto 131: printf("lookup(%s, %d, %p)\n", name, action, value);
1.1 dm 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) {
1.5 millert 143: (void)snprintf(buf, sizeof(buf),
144: "%s redefined", name);
1.1 dm 145: yyerror(buf);
146: }
147: }
148: return(s->s_value);
149: }
150:
151: if (action == LOOKUP) {
1.5 millert 152: (void)snprintf(buf, sizeof(buf), "%s undefined", name);
1.1 dm 153: yyerror(buf);
154: return(NULL);
155: }
156:
157: s = ALLOC(syment);
158: if (s == NULL)
159: fatal("ran out of memory\n");
160: s->s_next = hashtab[n];
161: hashtab[n] = s;
162: s->s_type = action == INSERT ? VAR : CONST;
163: s->s_name = name;
164: s->s_value = value;
165: return(value);
166: }