Use skinned_name()
[s-mailx.git] / vars.c
blob3f471d76efbed747df45c55d27bc0efcf52d6f9b
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #include "rcv.h"
41 #include "extern.h"
44 * Mail -- a mail program
46 * Variable handling stuff.
49 static char *canonify(const char *vn);
50 static void vfree(char *cp);
51 static struct var *lookup(const char *name);
52 static void remove_grouplist(struct grouphead *gh);
55 * If a variable name begins with a lowercase-character and contains at
56 * least one '@', it is converted to all-lowercase. This is necessary
57 * for lookups of names based on email addresses.
59 * Following the standard, only the part following the last '@' should
60 * be lower-cased, but practice has established otherwise here.
62 static char *
63 canonify(const char *vn)
65 const char *vp;
67 if (upperchar(*vn&0377))
68 return (char *)vn;
69 for (vp = vn; *vp && *vp != '@'; vp++);
70 if (*vp == '@')
71 return i_strdup(vn);
72 return (char *)vn;
76 * Assign a value to a variable.
78 void
79 assign(const char *name, const char *value)
81 struct var *vp;
82 int h;
84 name = canonify(name);
85 h = hash(name);
86 vp = lookup(name);
87 if (vp == NULL) {
88 vp = (struct var *)scalloc(1, sizeof *vp);
89 vp->v_name = vcopy(name);
90 vp->v_link = variables[h];
91 variables[h] = vp;
93 else
94 vfree(vp->v_value);
95 vp->v_value = vcopy(value);
99 * Free up a variable string. We do not bother to allocate
100 * strings whose value is "" since they are expected to be frequent.
101 * Thus, we cannot free same!
103 static void
104 vfree(char *cp)
106 if (*cp)
107 free(cp);
111 * Copy a variable value into permanent (ie, not collected after each
112 * command) space. Do not bother to alloc space for ""
115 char *
116 vcopy(const char *str)
118 char *new;
119 unsigned len;
121 if (*str == '\0')
122 return "";
123 len = strlen(str) + 1;
124 new = smalloc(len);
125 memcpy(new, str, (int) len);
126 return new;
130 * Get the value of a variable and return it.
131 * Look in the environment if its not available locally.
134 char *
135 value(const char *name)
137 struct var *vp;
138 char *vs;
140 name = canonify(name);
141 if ((vp = lookup(name)) == NULL) {
142 if ((vs = getenv(name)) != NULL && *vs)
143 vs = savestr(vs);
144 return vs;
146 return vp->v_value;
150 * Locate a variable and return its variable
151 * node.
154 static struct var *
155 lookup(const char *name)
157 struct var *vp;
159 for (vp = variables[hash(name)]; vp != NULL; vp = vp->v_link)
160 if (*vp->v_name == *name && strcmp(vp->v_name, name) == 0)
161 return(vp);
162 return(NULL);
166 * Locate a group name and return it.
169 struct grouphead *
170 findgroup(char *name)
172 struct grouphead *gh;
174 for (gh = groups[hash(name)]; gh != NULL; gh = gh->g_link)
175 if (*gh->g_name == *name && strcmp(gh->g_name, name) == 0)
176 return(gh);
177 return(NULL);
181 * Print a group out on stdout
183 void
184 printgroup(char *name)
186 struct grouphead *gh;
187 struct group *gp;
189 if ((gh = findgroup(name)) == NULL) {
190 printf(catgets(catd, CATSET, 202, "\"%s\": not a group\n"),
191 name);
192 return;
194 printf("%s\t", gh->g_name);
195 for (gp = gh->g_list; gp != NULL; gp = gp->ge_link)
196 printf(" %s", gp->ge_name);
197 putchar('\n');
201 * Hash the passed string and return an index into
202 * the variable or group hash table.
204 int
205 hash(const char *name)
207 int h = 0;
209 while (*name) {
210 h <<= 2;
211 h += *name++;
213 if (h < 0 && (h = -h) < 0)
214 h = 0;
215 return (h % HSHSIZE);
218 int
219 unset_internal(const char *name)
221 struct var *vp, *vp2;
222 int h;
224 name = canonify(name);
225 if ((vp2 = lookup(name)) == NULL) {
226 if (!sourcing && !unset_allow_undefined) {
227 printf(catgets(catd, CATSET, 203,
228 "\"%s\": undefined variable\n"), name);
229 return 1;
231 return 0;
233 h = hash(name);
234 if (vp2 == variables[h]) {
235 variables[h] = variables[h]->v_link;
236 vfree(vp2->v_name);
237 vfree(vp2->v_value);
238 free(vp2);
239 return 0;
241 for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link);
242 vp->v_link = vp2->v_link;
243 vfree(vp2->v_name);
244 vfree(vp2->v_value);
245 free(vp2);
246 return 0;
249 static void
250 remove_grouplist(struct grouphead *gh)
252 struct group *gp, *gq;
254 if ((gp = gh->g_list) != NULL) {
255 for (; gp; gp = gq) {
256 gq = gp->ge_link;
257 vfree(gp->ge_name);
258 free(gp);
263 void
264 remove_group(const char *name)
266 struct grouphead *gh, *gp = NULL;
267 int h = hash(name);
269 for (gh = groups[h]; gh != NULL; gh = gh->g_link) {
270 if (*gh->g_name == *name && strcmp(gh->g_name, name) == 0) {
271 remove_grouplist(gh);
272 vfree(gh->g_name);
273 if (gp != NULL)
274 gp->g_link = gh->g_link;
275 else
276 groups[h] = NULL;
277 free(gh);
278 break;
280 gp = gh;