1/*
2 * Copyright (c) 1980, 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. 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
35#if 0
36static char sccsid[] = "@(#)vars.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: src/usr.bin/mail/vars.c,v 1.4 2002/06/30 05:25:06 obrien Exp $";
40#endif /* not lint */
41
42#include <sys/cdefs.h>
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 */
56void
57assign(name, value)
58	const char *name, *value;
59{
60	struct var *vp;
61	int h;
62
63	h = hash(name);
64	vp = lookup(name);
65	if (vp == NULL) {
66		vp = calloc(sizeof(*vp), 1);
67		vp->v_name = vcopy(name);
68		vp->v_link = variables[h];
69		variables[h] = vp;
70	}
71	else
72		v_free(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 */
81void
82v_free(cp)
83	char *cp;
84{
85	if (*cp != '\0')
86		(void)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
94char *
95vcopy(str)
96	const 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		err(1, "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
115char *
116value(name)
117	const char *name;
118{
119	struct var *vp;
120
121	if ((vp = lookup(name)) == NULL)
122		return (getenv(name));
123	return (vp->v_value);
124}
125
126/*
127 * Locate a variable and return its variable
128 * node.
129 */
130
131struct var *
132lookup(name)
133	const char *name;
134{
135	struct var *vp;
136
137	for (vp = variables[hash(name)]; vp != NULL; vp = vp->v_link)
138		if (*vp->v_name == *name && equal(vp->v_name, name))
139			return (vp);
140	return (NULL);
141}
142
143/*
144 * Locate a group name and return it.
145 */
146
147struct grouphead *
148findgroup(name)
149	char name[];
150{
151	struct grouphead *gh;
152
153	for (gh = groups[hash(name)]; gh != NULL; gh = gh->g_link)
154		if (*gh->g_name == *name && equal(gh->g_name, name))
155			return (gh);
156	return (NULL);
157}
158
159/*
160 * Print a group out on stdout
161 */
162void
163printgroup(name)
164	char name[];
165{
166	struct grouphead *gh;
167	struct group *gp;
168
169	if ((gh = findgroup(name)) == NULL) {
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 != NULL; gp = gp->ge_link)
175		printf(" %s", gp->ge_name);
176	printf("\n");
177}
178
179/*
180 * Hash the passed string and return an index into
181 * the variable or group hash table.
182 */
183int
184hash(name)
185	const char *name;
186{
187	int h = 0;
188
189	while (*name != '\0') {
190		h <<= 2;
191		h += *name++;
192	}
193	if (h < 0 && (h = -h) < 0)
194		h = 0;
195	return (h % HSHSIZE);
196}
197