date and time created 86/11/11 09:42:21 by minshall
[unix-history] / usr / src / usr.bin / rdist / lookup.c
CommitLineData
7172eb74
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
837386b3 7#ifndef lint
7172eb74
DF
8static char sccsid[] = "@(#)lookup.c 5.1 (Berkeley) %G%";
9#endif not lint
837386b3
RC
10
11#include "defs.h"
12
0fccdfef
RC
13 /* symbol types */
14#define VAR 1
15#define CONST 2
16
17struct syment {
18 int s_type;
19 char *s_name;
20 struct namelist *s_value;
21 struct syment *s_next;
22};
23
24static struct syment *hashtab[HASHSIZE];
25
837386b3
RC
26/*
27 * Define a variable from a command line argument.
28 */
29define(name)
30 char *name;
31{
32 register char *cp, *s;
0fccdfef
RC
33 register struct namelist *nl;
34 struct namelist *value;
837386b3
RC
35
36 if (debug)
37 printf("define(%s)\n", name);
38
39 cp = index(name, '=');
3024eb6f 40 if (cp == NULL)
837386b3 41 value = NULL;
3024eb6f 42 else if (cp[1] == '\0') {
0fccdfef 43 *cp = '\0';
3024eb6f
RC
44 value = NULL;
45 } else if (cp[1] != '(') {
837386b3 46 *cp++ = '\0';
58157a31 47 value = makenl(cp);
837386b3 48 } else {
0fccdfef 49 nl = NULL;
837386b3
RC
50 *cp++ = '\0';
51 do
52 cp++;
53 while (*cp == ' ' || *cp == '\t');
54 for (s = cp; ; s++) {
55 switch (*s) {
56 case ')':
57 *s = '\0';
58 case '\0':
59 break;
60 case ' ':
61 case '\t':
62 *s++ = '\0';
63 while (*s == ' ' || *s == '\t')
64 s++;
65 if (*s == ')')
66 *s = '\0';
67 break;
68 default:
69 continue;
70 }
0fccdfef
RC
71 if (nl == NULL)
72 value = nl = makenl(cp);
837386b3 73 else {
0fccdfef
RC
74 nl->n_next = makenl(cp);
75 nl = nl->n_next;
837386b3
RC
76 }
77 if (*s == '\0')
78 break;
79 cp = s;
80 }
81 }
0fccdfef 82 (void) lookup(name, REPLACE, value);
837386b3
RC
83}
84
837386b3
RC
85/*
86 * Lookup name in the table and return a pointer to it.
0fccdfef
RC
87 * LOOKUP - just do lookup, return NULL if not found.
88 * INSERT - insert name with value, error if already defined.
89 * REPLACE - insert or replace name with value.
837386b3
RC
90 */
91
0fccdfef
RC
92struct namelist *
93lookup(name, action, value)
837386b3 94 char *name;
0fccdfef
RC
95 int action;
96 struct namelist *value;
837386b3
RC
97{
98 register unsigned n;
99 register char *cp;
0fccdfef 100 register struct syment *s;
55669d5b 101 char buf[256];
837386b3
RC
102
103 if (debug)
0fccdfef 104 printf("lookup(%s, %d, %x)\n", name, action, value);
837386b3
RC
105
106 n = 0;
107 for (cp = name; *cp; )
108 n += *cp++;
109 n %= HASHSIZE;
110
0fccdfef
RC
111 for (s = hashtab[n]; s != NULL; s = s->s_next) {
112 if (strcmp(name, s->s_name))
837386b3 113 continue;
0fccdfef 114 if (action != LOOKUP) {
55669d5b
RC
115 if (action != INSERT || s->s_type != CONST) {
116 sprintf(buf, "%s redefined", name);
117 yyerror(buf);
118 }
837386b3 119 }
0fccdfef 120 return(s->s_value);
837386b3
RC
121 }
122
55669d5b
RC
123 if (action == LOOKUP) {
124 yyerror(sprintf(buf, "%s undefined", name));
125 return(NULL);
126 }
837386b3 127
0fccdfef
RC
128 s = ALLOC(syment);
129 if (s == NULL)
837386b3 130 fatal("ran out of memory\n");
0fccdfef
RC
131 s->s_next = hashtab[n];
132 hashtab[n] = s;
133 s->s_type = action == INSERT ? VAR : CONST;
134 s->s_name = name;
135 s->s_value = value;
136 return(value);
837386b3 137}