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