Annotation of src/usr.bin/rdist/lookup.c, Revision 1.2
1.1 deraadt 1: /*
1.2 ! dm 2: * Copyright (c) 1983 Regents of the University of California.
! 3: * All rights reserved.
1.1 deraadt 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
1.2 ! dm 35: static char RCSid[] =
! 36: "$Id: lookup.c,v 6.7 1994/04/26 17:03:09 mcooper Exp $";
! 37:
! 38: static char sccsid[] = "@(#)lookup.c 5.1 (Berkeley) 6/6/85";
! 39:
! 40: static char copyright[] =
! 41: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
! 42: All rights reserved.\n";
1.1 deraadt 43: #endif /* not lint */
44:
45: #include "defs.h"
46:
47: /* symbol types */
48: #define VAR 1
49: #define CONST 2
50:
51: struct syment {
52: int s_type;
53: char *s_name;
54: struct namelist *s_value;
55: struct syment *s_next;
56: };
57:
58: static struct syment *hashtab[HASHSIZE];
59:
60: /*
61: * Define a variable from a command line argument.
62: */
63: define(name)
64: char *name;
65: {
66: register char *cp, *s;
67: register struct namelist *nl;
68: struct namelist *value;
69:
1.2 ! dm 70: debugmsg(DM_CALL, "define(%s)", name);
1.1 deraadt 71:
1.2 ! dm 72: cp = strchr(name, '=');
1.1 deraadt 73: if (cp == NULL)
74: value = NULL;
75: else if (cp[1] == '\0') {
76: *cp = '\0';
77: value = NULL;
78: } else if (cp[1] != '(') {
79: *cp++ = '\0';
80: value = makenl(cp);
81: } else {
1.2 ! dm 82: value = NULL;
1.1 deraadt 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 *
1.2 ! dm 127: lookup(name, action, value) /* %% in name. Ignore quotas in name */
1.1 deraadt 128: char *name;
129: int action;
130: struct namelist *value;
131: {
132: register unsigned n;
133: register char *cp;
134: register struct syment *s;
1.2 ! dm 135: char ebuf[BUFSIZ];
1.1 deraadt 136:
1.2 ! dm 137: debugmsg(DM_CALL, "lookup(%s, %d, %x)", name, action, value);
1.1 deraadt 138:
139: n = 0;
140: for (cp = name; *cp; )
141: n += *cp++;
142: n %= HASHSIZE;
143:
144: for (s = hashtab[n]; s != NULL; s = s->s_next) {
145: if (strcmp(name, s->s_name))
146: continue;
147: if (action != LOOKUP) {
148: if (action != INSERT || s->s_type != CONST) {
1.2 ! dm 149: (void) sprintf(ebuf, "%s redefined", name);
! 150: yyerror(ebuf);
1.1 deraadt 151: }
152: }
153: return(s->s_value);
154: }
155:
156: if (action == LOOKUP) {
1.2 ! dm 157: (void) sprintf(ebuf, "%s undefined", name);
! 158: yyerror(ebuf);
1.1 deraadt 159: return(NULL);
160: }
161:
162: s = ALLOC(syment);
163: s->s_next = hashtab[n];
164: hashtab[n] = s;
165: s->s_type = action == INSERT ? VAR : CONST;
166: s->s_name = name;
167: s->s_value = value;
1.2 ! dm 168:
1.1 deraadt 169: return(value);
170: }