vars.c revision 1.4 1 /* $NetBSD: vars.c,v 1.4 1996/06/08 19:48:45 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 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.
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
37 #if 0
38 static char sccsid[] = "@(#)vars.c 8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$NetBSD: vars.c,v 1.4 1996/06/08 19:48:45 christos Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include "rcv.h"
45 #include "extern.h"
46
47 /*
48 * Mail -- a mail program
49 *
50 * Variable handling stuff.
51 */
52
53 /*
54 * Assign a value to a variable.
55 */
56 void
57 assign(name, value)
58 char name[], value[];
59 {
60 register struct var *vp;
61 register int h;
62
63 h = hash(name);
64 vp = lookup(name);
65 if (vp == NOVAR) {
66 vp = (struct var *) calloc(sizeof *vp, 1);
67 vp->v_name = vcopy(name);
68 vp->v_link = variables[h];
69 variables[h] = vp;
70 }
71 else
72 vfree(vp->v_value);
73 vp->v_value = vcopy(value);
74 }
75
76 /*
77 * Free up a variable string. We do not bother to allocate
78 * strings whose value is "" since they are expected to be frequent.
79 * Thus, we cannot free same!
80 */
81 void
82 vfree(cp)
83 char *cp;
84 {
85 if (*cp)
86 free(cp);
87 }
88
89 /*
90 * Copy a variable value into permanent (ie, not collected after each
91 * command) space. Do not bother to alloc space for ""
92 */
93
94 char *
95 vcopy(str)
96 char str[];
97 {
98 char *new;
99 unsigned len;
100
101 if (*str == '\0')
102 return "";
103 len = strlen(str) + 1;
104 if ((new = malloc(len)) == NULL)
105 panic("Out of memory");
106 bcopy(str, new, (int) len);
107 return new;
108 }
109
110 /*
111 * Get the value of a variable and return it.
112 * Look in the environment if its not available locally.
113 */
114
115 char *
116 value(name)
117 char name[];
118 {
119 register struct var *vp;
120
121 if ((vp = lookup(name)) == NOVAR)
122 return(getenv(name));
123 return(vp->v_value);
124 }
125
126 /*
127 * Locate a variable and return its variable
128 * node.
129 */
130
131 struct var *
132 lookup(name)
133 register char name[];
134 {
135 register struct var *vp;
136
137 for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link)
138 if (*vp->v_name == *name && equal(vp->v_name, name))
139 return(vp);
140 return(NOVAR);
141 }
142
143 /*
144 * Locate a group name and return it.
145 */
146
147 struct grouphead *
148 findgroup(name)
149 register char name[];
150 {
151 register struct grouphead *gh;
152
153 for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link)
154 if (*gh->g_name == *name && equal(gh->g_name, name))
155 return(gh);
156 return(NOGRP);
157 }
158
159 /*
160 * Print a group out on stdout
161 */
162 void
163 printgroup(name)
164 char name[];
165 {
166 register struct grouphead *gh;
167 register struct group *gp;
168
169 if ((gh = findgroup(name)) == NOGRP) {
170 printf("\"%s\": not a group\n", name);
171 return;
172 }
173 printf("%s\t", gh->g_name);
174 for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
175 printf(" %s", gp->ge_name);
176 putchar('\n');
177 }
178
179 /*
180 * Hash the passed string and return an index into
181 * the variable or group hash table.
182 */
183 int
184 hash(name)
185 register char *name;
186 {
187 register h = 0;
188
189 while (*name) {
190 h <<= 2;
191 h += *name++;
192 }
193 if (h < 0 && (h = -h) < 0)
194 h = 0;
195 return (h % HSHSIZE);
196 }
197