xref: /openbsd-src/usr.bin/mail/vars.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: vars.c,v 1.5 1997/11/14 00:24:01 millert Exp $	*/
2 /*	$NetBSD: vars.c,v 1.4 1996/06/08 19:48:45 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)vars.c	8.1 (Berkeley) 6/6/93";
40 #else
41 static char rcsid[] = "$OpenBSD: vars.c,v 1.5 1997/11/14 00:24:01 millert Exp $";
42 #endif
43 #endif /* not lint */
44 
45 #include "rcv.h"
46 #include "extern.h"
47 
48 /*
49  * Mail -- a mail program
50  *
51  * Variable handling stuff.
52  */
53 
54 /*
55  * Assign a value to a variable.
56  */
57 void
58 assign(name, value)
59 	char name[], value[];
60 {
61 	struct var *vp;
62 	int h;
63 
64 	h = hash(name);
65 	vp = lookup(name);
66 	if (vp == NOVAR) {
67 		vp = (struct var *)calloc(sizeof(*vp), 1);
68 		vp->v_name = vcopy(name);
69 		vp->v_link = variables[h];
70 		variables[h] = vp;
71 	}
72 	else
73 		vfree(vp->v_value);
74 	vp->v_value = vcopy(value);
75 }
76 
77 /*
78  * Free up a variable string.  We do not bother to allocate
79  * strings whose value is "" since they are expected to be frequent.
80  * Thus, we cannot free same!
81  */
82 void
83 vfree(cp)
84 	char *cp;
85 {
86 	if (*cp)
87 		(void)free(cp);
88 }
89 
90 /*
91  * Copy a variable value into permanent (ie, not collected after each
92  * command) space.  Do not bother to alloc space for ""
93  */
94 
95 char *
96 vcopy(str)
97 	char str[];
98 {
99 	char *new;
100 	unsigned len;
101 
102 	if (*str == '\0')
103 		return("");
104 	len = strlen(str) + 1;
105 	if ((new = (char *)malloc(len)) == NULL)
106 		errx(1, "Out of memory");
107 	(void)memcpy(new, str, len);
108 	return(new);
109 }
110 
111 /*
112  * Get the value of a variable and return it.
113  * Look in the environment if its not available locally.
114  */
115 
116 char *
117 value(name)
118 	char name[];
119 {
120 	struct var *vp;
121 
122 	if ((vp = lookup(name)) == NOVAR)
123 		return(getenv(name));
124 	return(vp->v_value);
125 }
126 
127 /*
128  * Locate a variable and return its variable
129  * node.
130  */
131 
132 struct var *
133 lookup(name)
134 	char name[];
135 {
136 	struct var *vp;
137 
138 	for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link)
139 		if (*vp->v_name == *name && equal(vp->v_name, name))
140 			return(vp);
141 	return(NOVAR);
142 }
143 
144 /*
145  * Locate a group name and return it.
146  */
147 
148 struct grouphead *
149 findgroup(name)
150 	char name[];
151 {
152 	struct grouphead *gh;
153 
154 	for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link)
155 		if (*gh->g_name == *name && equal(gh->g_name, name))
156 			return(gh);
157 	return(NOGRP);
158 }
159 
160 /*
161  * Print a group out on stdout
162  */
163 void
164 printgroup(name)
165 	char name[];
166 {
167 	struct grouphead *gh;
168 	struct group *gp;
169 
170 	if ((gh = findgroup(name)) == NOGRP) {
171 		printf("\"%s\": not a group\n", name);
172 		return;
173 	}
174 	printf("%s\t", gh->g_name);
175 	for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link)
176 		printf(" %s", gp->ge_name);
177 	putchar('\n');
178 }
179 
180 /*
181  * Hash the passed string and return an index into
182  * the variable or group hash table.
183  */
184 int
185 hash(name)
186 	char *name;
187 {
188 	int h = 0;
189 
190 	while (*name) {
191 		h <<= 2;
192 		h += *name++;
193 	}
194 	if (h < 0 && (h = -h) < 0)
195 		h = 0;
196 	return(h % HSHSIZE);
197 }
198