794a2af1e06a8356e1cec1f4d3af00be15b16b08
[dragonfly.git] / usr.bin / rdist / lookup.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)lookup.c 8.1 (Berkeley) 6/9/93
30  * $FreeBSD: src/usr.bin/rdist/lookup.c,v 1.6 1999/08/28 01:05:07 peter Exp $
31  * $DragonFly: src/usr.bin/rdist/lookup.c,v 1.4 2008/10/16 01:52:33 swildner Exp $
32  */
33
34 #include "defs.h"
35
36 /* symbol types */
37 #define VAR     1
38 #define CONST   2
39
40 struct syment {
41         int     s_type;
42         char    *s_name;
43         struct  namelist *s_value;
44         struct  syment *s_next;
45 };
46
47 static struct syment *hashtab[HASHSIZE];
48
49 /*
50  * Define a variable from a command line argument.
51  */
52 void
53 define(char *name)
54 {
55         char *cp, *s;
56         struct namelist *nl;
57         struct namelist *value;
58
59         value = NULL;
60
61         if (debug)
62                 printf("define(%s)\n", name);
63
64         cp = index(name, '=');
65         if (cp == NULL)
66                 value = NULL;
67         else if (cp[1] == '\0') {
68                 *cp = '\0';
69                 value = NULL;
70         } else if (cp[1] != '(') {
71                 *cp++ = '\0';
72                 value = makenl(cp);
73         } else {
74                 nl = NULL;
75                 *cp++ = '\0';
76                 do
77                         cp++;
78                 while (*cp == ' ' || *cp == '\t');
79                 for (s = cp; ; s++) {
80                         switch (*s) {
81                         case ')':
82                                 *s = '\0';
83                         case '\0':
84                                 break;
85                         case ' ':
86                         case '\t':
87                                 *s++ = '\0';
88                                 while (*s == ' ' || *s == '\t')
89                                         s++;
90                                 if (*s == ')')
91                                         *s = '\0';
92                                 break;
93                         default:
94                                 continue;
95                         }
96                         if (nl == NULL)
97                                 value = nl = makenl(cp);
98                         else {
99                                 nl->n_next = makenl(cp);
100                                 nl = nl->n_next;
101                         }
102                         if (*s == '\0')
103                                 break;
104                         cp = s;
105                 }
106         }
107         lookup(name, REPLACE, value);
108 }
109
110 /*
111  * Lookup name in the table and return a pointer to it.
112  * LOOKUP - just do lookup, return NULL if not found.
113  * INSERT - insert name with value, error if already defined.
114  * REPLACE - insert or replace name with value.
115  */
116 struct namelist *
117 lookup(char *name, int action, struct namelist *value)
118 {
119         unsigned n;
120         char *cp;
121         struct syment *s;
122         char buf[BUFSIZ];
123
124         if (debug)
125                 printf("lookup(%s, %d, %p)\n", name, action, value);
126
127         n = 0;
128         for (cp = name; *cp; )
129                 n += *cp++;
130         n %= HASHSIZE;
131
132         for (s = hashtab[n]; s != NULL; s = s->s_next) {
133                 if (strcmp(name, s->s_name))
134                         continue;
135                 if (action != LOOKUP) {
136                         if (action != INSERT || s->s_type != CONST) {
137                                 snprintf(buf, sizeof(buf),"%s redefined", name);
138                                 yyerror(buf);
139                         }
140                 }
141                 return(s->s_value);
142         }
143
144         if (action == LOOKUP) {
145                 snprintf(buf, sizeof(buf), "%s undefined", name);
146                 yyerror(buf);
147                 return(NULL);
148         }
149
150         s = ALLOC(syment);
151         if (s == NULL)
152                 fatal("ran out of memory\n");
153         s->s_next = hashtab[n];
154         hashtab[n] = s;
155         s->s_type = action == INSERT ? VAR : CONST;
156         s->s_name = name;
157         s->s_value = value;
158         return(value);
159 }