Commit | Line | Data |
---|---|---|
761330fe | 1 | /* |
a12ff486 KB |
2 | * Copyright (c) 1980, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
0c5f72fb | 4 | * |
f15db449 | 5 | * %sccs.include.redist.c% |
761330fe DF |
6 | */ |
7 | ||
acfc7e9b | 8 | #ifndef lint |
a12ff486 | 9 | static char sccsid[] = "@(#)vars.c 8.1 (Berkeley) %G%"; |
acfc7e9b | 10 | #endif /* not lint */ |
2e4ee99d KS |
11 | |
12 | #include "rcv.h" | |
a0d23834 | 13 | #include "extern.h" |
2e4ee99d KS |
14 | |
15 | /* | |
16 | * Mail -- a mail program | |
17 | * | |
18 | * Variable handling stuff. | |
19 | */ | |
20 | ||
2e4ee99d KS |
21 | /* |
22 | * Assign a value to a variable. | |
23 | */ | |
a0d23834 | 24 | void |
2e4ee99d KS |
25 | assign(name, value) |
26 | char name[], value[]; | |
27 | { | |
28 | register struct var *vp; | |
29 | register int h; | |
30 | ||
31 | h = hash(name); | |
32 | vp = lookup(name); | |
33 | if (vp == NOVAR) { | |
34 | vp = (struct var *) calloc(sizeof *vp, 1); | |
35 | vp->v_name = vcopy(name); | |
36 | vp->v_link = variables[h]; | |
37 | variables[h] = vp; | |
38 | } | |
39 | else | |
40 | vfree(vp->v_value); | |
41 | vp->v_value = vcopy(value); | |
42 | } | |
43 | ||
44 | /* | |
45 | * Free up a variable string. We do not bother to allocate | |
46 | * strings whose value is "" since they are expected to be frequent. | |
47 | * Thus, we cannot free same! | |
48 | */ | |
a0d23834 | 49 | void |
2e4ee99d | 50 | vfree(cp) |
828615a1 | 51 | char *cp; |
2e4ee99d | 52 | { |
828615a1 EW |
53 | if (*cp) |
54 | free(cp); | |
2e4ee99d KS |
55 | } |
56 | ||
57 | /* | |
58 | * Copy a variable value into permanent (ie, not collected after each | |
59 | * command) space. Do not bother to alloc space for "" | |
60 | */ | |
61 | ||
62 | char * | |
63 | vcopy(str) | |
64 | char str[]; | |
65 | { | |
828615a1 EW |
66 | char *new; |
67 | unsigned len; | |
68 | ||
69 | if (*str == '\0') | |
70 | return ""; | |
71 | len = strlen(str) + 1; | |
72 | if ((new = malloc(len)) == NULL) | |
73 | panic("Out of memory"); | |
74 | bcopy(str, new, (int) len); | |
75 | return new; | |
2e4ee99d KS |
76 | } |
77 | ||
78 | /* | |
79 | * Get the value of a variable and return it. | |
80 | * Look in the environment if its not available locally. | |
81 | */ | |
82 | ||
83 | char * | |
84 | value(name) | |
85 | char name[]; | |
86 | { | |
87 | register struct var *vp; | |
88 | ||
89 | if ((vp = lookup(name)) == NOVAR) | |
90 | return(getenv(name)); | |
91 | return(vp->v_value); | |
92 | } | |
93 | ||
94 | /* | |
95 | * Locate a variable and return its variable | |
96 | * node. | |
97 | */ | |
98 | ||
99 | struct var * | |
100 | lookup(name) | |
828615a1 | 101 | register char name[]; |
2e4ee99d KS |
102 | { |
103 | register struct var *vp; | |
2e4ee99d | 104 | |
828615a1 EW |
105 | for (vp = variables[hash(name)]; vp != NOVAR; vp = vp->v_link) |
106 | if (*vp->v_name == *name && equal(vp->v_name, name)) | |
2e4ee99d KS |
107 | return(vp); |
108 | return(NOVAR); | |
109 | } | |
110 | ||
111 | /* | |
112 | * Locate a group name and return it. | |
113 | */ | |
114 | ||
115 | struct grouphead * | |
116 | findgroup(name) | |
828615a1 | 117 | register char name[]; |
2e4ee99d KS |
118 | { |
119 | register struct grouphead *gh; | |
2e4ee99d | 120 | |
828615a1 EW |
121 | for (gh = groups[hash(name)]; gh != NOGRP; gh = gh->g_link) |
122 | if (*gh->g_name == *name && equal(gh->g_name, name)) | |
2e4ee99d KS |
123 | return(gh); |
124 | return(NOGRP); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Print a group out on stdout | |
129 | */ | |
a0d23834 | 130 | void |
2e4ee99d KS |
131 | printgroup(name) |
132 | char name[]; | |
133 | { | |
134 | register struct grouphead *gh; | |
135 | register struct group *gp; | |
136 | ||
137 | if ((gh = findgroup(name)) == NOGRP) { | |
138 | printf("\"%s\": not a group\n", name); | |
139 | return; | |
140 | } | |
141 | printf("%s\t", gh->g_name); | |
142 | for (gp = gh->g_list; gp != NOGE; gp = gp->ge_link) | |
143 | printf(" %s", gp->ge_name); | |
828615a1 | 144 | putchar('\n'); |
2e4ee99d KS |
145 | } |
146 | ||
147 | /* | |
148 | * Hash the passed string and return an index into | |
149 | * the variable or group hash table. | |
150 | */ | |
a0d23834 | 151 | int |
2e4ee99d | 152 | hash(name) |
828615a1 | 153 | register char *name; |
2e4ee99d | 154 | { |
828615a1 EW |
155 | register h = 0; |
156 | ||
157 | while (*name) { | |
158 | h <<= 2; | |
159 | h += *name++; | |
160 | } | |
161 | if (h < 0 && (h = -h) < 0) | |
46691008 | 162 | h = 0; |
828615a1 | 163 | return (h % HSHSIZE); |
2e4ee99d | 164 | } |