value.c revision 1.11 1 1.11 christos /* $NetBSD: value.c,v 1.11 2004/04/23 22:11:44 christos Exp $ */
2 1.3 jtc
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1983, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.10 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.7 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.3 jtc #if 0
35 1.3 jtc static char sccsid[] = "@(#)value.c 8.1 (Berkeley) 6/6/93";
36 1.3 jtc #endif
37 1.11 christos __RCSID("$NetBSD: value.c,v 1.11 2004/04/23 22:11:44 christos Exp $");
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #include "tip.h"
41 1.1 cgd
42 1.1 cgd #define MIDDLE 35
43 1.1 cgd
44 1.1 cgd static int col = 0;
45 1.1 cgd
46 1.7 lukem static int vaccess __P((unsigned, unsigned));
47 1.7 lukem static void vassign __P((value_t *, char *));
48 1.11 christos static value_t *vlookup __P((const char *));
49 1.7 lukem static void vprint __P((value_t *));
50 1.7 lukem static void vtoken __P((char *));
51 1.7 lukem
52 1.1 cgd /*
53 1.1 cgd * Variable manipulation
54 1.1 cgd */
55 1.7 lukem void
56 1.1 cgd vinit()
57 1.1 cgd {
58 1.7 lukem value_t *p;
59 1.7 lukem char *cp;
60 1.1 cgd FILE *f;
61 1.7 lukem char file[MAXPATHLEN];
62 1.1 cgd
63 1.1 cgd for (p = vtable; p->v_name != NULL; p++) {
64 1.1 cgd if (p->v_type&ENVIRON)
65 1.7 lukem if ((cp = getenv(p->v_name)) != NULL)
66 1.1 cgd p->v_value = cp;
67 1.1 cgd if (p->v_type&IREMOTE)
68 1.4 cgd setnumber(p->v_value, *address(p->v_value));
69 1.1 cgd }
70 1.1 cgd /*
71 1.1 cgd * Read the .tiprc file in the HOME directory
72 1.1 cgd * for sets
73 1.1 cgd */
74 1.7 lukem snprintf(file, sizeof(file), "%s/.tiprc", value(HOME));
75 1.1 cgd if ((f = fopen(file, "r")) != NULL) {
76 1.7 lukem char *tp;
77 1.1 cgd
78 1.1 cgd while (fgets(file, sizeof(file)-1, f) != NULL) {
79 1.1 cgd if (vflag)
80 1.1 cgd printf("set %s", file);
81 1.7 lukem if ((tp = strrchr(file, '\n')) != NULL)
82 1.1 cgd *tp = '\0';
83 1.1 cgd vlex(file);
84 1.1 cgd }
85 1.1 cgd fclose(f);
86 1.1 cgd }
87 1.1 cgd /*
88 1.1 cgd * To allow definition of exception prior to fork
89 1.1 cgd */
90 1.1 cgd vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
91 1.1 cgd }
92 1.1 cgd
93 1.7 lukem void
94 1.1 cgd vassign(p, v)
95 1.7 lukem value_t *p;
96 1.1 cgd char *v;
97 1.1 cgd {
98 1.1 cgd
99 1.1 cgd if (!vaccess(p->v_access, WRITE)) {
100 1.1 cgd printf("access denied\r\n");
101 1.1 cgd return;
102 1.1 cgd }
103 1.1 cgd switch (p->v_type&TMASK) {
104 1.1 cgd
105 1.1 cgd case STRING:
106 1.1 cgd if (p->v_value && equal(p->v_value, v))
107 1.1 cgd return;
108 1.1 cgd if (!(p->v_type&(ENVIRON|INIT)))
109 1.1 cgd free(p->v_value);
110 1.7 lukem if ((p->v_value = strdup(v)) == NULL) {
111 1.1 cgd printf("out of core\r\n");
112 1.1 cgd return;
113 1.1 cgd }
114 1.1 cgd p->v_type &= ~(ENVIRON|INIT);
115 1.1 cgd break;
116 1.1 cgd
117 1.1 cgd case NUMBER:
118 1.1 cgd if (number(p->v_value) == number(v))
119 1.1 cgd return;
120 1.4 cgd setnumber(p->v_value, number(v));
121 1.1 cgd break;
122 1.1 cgd
123 1.1 cgd case BOOL:
124 1.1 cgd if (boolean(p->v_value) == (*v != '!'))
125 1.1 cgd return;
126 1.4 cgd setboolean(p->v_value, (*v != '!'));
127 1.1 cgd break;
128 1.1 cgd
129 1.1 cgd case CHAR:
130 1.1 cgd if (character(p->v_value) == *v)
131 1.1 cgd return;
132 1.4 cgd setcharacter(p->v_value, *v);
133 1.1 cgd }
134 1.1 cgd p->v_access |= CHANGED;
135 1.1 cgd }
136 1.1 cgd
137 1.7 lukem void
138 1.1 cgd vlex(s)
139 1.7 lukem char *s;
140 1.1 cgd {
141 1.7 lukem value_t *p;
142 1.1 cgd
143 1.1 cgd if (equal(s, "all")) {
144 1.1 cgd for (p = vtable; p->v_name; p++)
145 1.1 cgd if (vaccess(p->v_access, READ))
146 1.1 cgd vprint(p);
147 1.1 cgd } else {
148 1.7 lukem char *cp;
149 1.1 cgd
150 1.1 cgd do {
151 1.7 lukem if ((cp = vinterp(s, ' ')) != NULL)
152 1.1 cgd cp++;
153 1.1 cgd vtoken(s);
154 1.1 cgd s = cp;
155 1.1 cgd } while (s);
156 1.1 cgd }
157 1.1 cgd if (col > 0) {
158 1.1 cgd printf("\r\n");
159 1.1 cgd col = 0;
160 1.1 cgd }
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd static void
164 1.1 cgd vtoken(s)
165 1.7 lukem char *s;
166 1.1 cgd {
167 1.7 lukem value_t *p;
168 1.7 lukem char *cp;
169 1.1 cgd
170 1.7 lukem if ((cp = strchr(s, '=')) != NULL) {
171 1.1 cgd *cp = '\0';
172 1.7 lukem if ((p = vlookup(s)) != NULL) {
173 1.1 cgd cp++;
174 1.1 cgd if (p->v_type&NUMBER)
175 1.8 mrg vassign(p, (char *)(long)atoi(cp));
176 1.1 cgd else {
177 1.1 cgd if (strcmp(s, "record") == 0)
178 1.1 cgd cp = expand(cp);
179 1.1 cgd vassign(p, cp);
180 1.1 cgd }
181 1.1 cgd return;
182 1.1 cgd }
183 1.7 lukem } else if ((cp = strchr(s, '?')) != NULL) {
184 1.1 cgd *cp = '\0';
185 1.1 cgd if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
186 1.1 cgd vprint(p);
187 1.1 cgd return;
188 1.1 cgd }
189 1.1 cgd } else {
190 1.1 cgd if (*s != '!')
191 1.1 cgd p = vlookup(s);
192 1.1 cgd else
193 1.1 cgd p = vlookup(s+1);
194 1.7 lukem if (p != NULL) {
195 1.1 cgd vassign(p, s);
196 1.1 cgd return;
197 1.1 cgd }
198 1.1 cgd }
199 1.1 cgd printf("%s: unknown variable\r\n", s);
200 1.1 cgd }
201 1.1 cgd
202 1.1 cgd static void
203 1.1 cgd vprint(p)
204 1.7 lukem value_t *p;
205 1.1 cgd {
206 1.7 lukem char *cp;
207 1.1 cgd
208 1.1 cgd if (col > 0 && col < MIDDLE)
209 1.1 cgd while (col++ < MIDDLE)
210 1.1 cgd putchar(' ');
211 1.7 lukem col += strlen(p->v_name);
212 1.1 cgd switch (p->v_type&TMASK) {
213 1.1 cgd
214 1.1 cgd case BOOL:
215 1.1 cgd if (boolean(p->v_value) == FALSE) {
216 1.1 cgd col++;
217 1.1 cgd putchar('!');
218 1.1 cgd }
219 1.1 cgd printf("%s", p->v_name);
220 1.1 cgd break;
221 1.1 cgd
222 1.1 cgd case STRING:
223 1.1 cgd printf("%s=", p->v_name);
224 1.1 cgd col++;
225 1.1 cgd if (p->v_value) {
226 1.11 christos /*###226 [cc] warning: passing arg 1 of `interp' discards qualifiers from pointer target type%%%*/
227 1.7 lukem cp = interp(p->v_value);
228 1.7 lukem col += strlen(cp);
229 1.1 cgd printf("%s", cp);
230 1.1 cgd }
231 1.1 cgd break;
232 1.1 cgd
233 1.1 cgd case NUMBER:
234 1.1 cgd col += 6;
235 1.7 lukem printf("%s=%-5d", p->v_name, (int)number(p->v_value));
236 1.1 cgd break;
237 1.1 cgd
238 1.1 cgd case CHAR:
239 1.1 cgd printf("%s=", p->v_name);
240 1.1 cgd col++;
241 1.1 cgd if (p->v_value) {
242 1.1 cgd cp = ctrl(character(p->v_value));
243 1.7 lukem col += strlen(cp);
244 1.1 cgd printf("%s", cp);
245 1.1 cgd }
246 1.1 cgd break;
247 1.1 cgd }
248 1.1 cgd if (col >= MIDDLE) {
249 1.1 cgd col = 0;
250 1.1 cgd printf("\r\n");
251 1.1 cgd return;
252 1.1 cgd }
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd
256 1.1 cgd static int
257 1.1 cgd vaccess(mode, rw)
258 1.7 lukem unsigned mode, rw;
259 1.1 cgd {
260 1.9 mrg
261 1.1 cgd if (mode & (rw<<PUBLIC))
262 1.1 cgd return (1);
263 1.1 cgd if (mode & (rw<<PRIVATE))
264 1.1 cgd return (1);
265 1.1 cgd return ((mode & (rw<<ROOT)) && getuid() == 0);
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd static value_t *
269 1.1 cgd vlookup(s)
270 1.11 christos const char *s;
271 1.1 cgd {
272 1.7 lukem value_t *p;
273 1.1 cgd
274 1.1 cgd for (p = vtable; p->v_name; p++)
275 1.1 cgd if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
276 1.1 cgd return (p);
277 1.1 cgd return (NULL);
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd char *
281 1.11 christos vinterp(s, stp)
282 1.7 lukem char *s;
283 1.11 christos char stp;
284 1.1 cgd {
285 1.7 lukem char *p = s, c;
286 1.1 cgd int num;
287 1.1 cgd
288 1.11 christos while ((c = *s++) && c != stp)
289 1.1 cgd switch (c) {
290 1.1 cgd
291 1.1 cgd case '^':
292 1.1 cgd if (*s)
293 1.1 cgd *p++ = *s++ - 0100;
294 1.1 cgd else
295 1.1 cgd *p++ = c;
296 1.1 cgd break;
297 1.1 cgd
298 1.1 cgd case '\\':
299 1.1 cgd num = 0;
300 1.1 cgd c = *s++;
301 1.1 cgd if (c >= '0' && c <= '7')
302 1.1 cgd num = (num<<3)+(c-'0');
303 1.1 cgd else {
304 1.11 christos const char *q = "n\nr\rt\tb\bf\f";
305 1.1 cgd
306 1.1 cgd for (; *q; q++)
307 1.1 cgd if (c == *q++) {
308 1.1 cgd *p++ = *q;
309 1.1 cgd goto cont;
310 1.1 cgd }
311 1.1 cgd *p++ = c;
312 1.1 cgd cont:
313 1.1 cgd break;
314 1.1 cgd }
315 1.1 cgd if ((c = *s++) >= '0' && c <= '7') {
316 1.1 cgd num = (num<<3)+(c-'0');
317 1.1 cgd if ((c = *s++) >= '0' && c <= '7')
318 1.1 cgd num = (num<<3)+(c-'0');
319 1.1 cgd else
320 1.1 cgd s--;
321 1.1 cgd } else
322 1.1 cgd s--;
323 1.1 cgd *p++ = num;
324 1.1 cgd break;
325 1.1 cgd
326 1.1 cgd default:
327 1.1 cgd *p++ = c;
328 1.1 cgd }
329 1.1 cgd *p = '\0';
330 1.11 christos return (c == stp ? s-1 : NULL);
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd /*
334 1.1 cgd * assign variable s with value v (for NUMBER or STRING or CHAR types)
335 1.1 cgd */
336 1.1 cgd
337 1.7 lukem int
338 1.1 cgd vstring(s,v)
339 1.11 christos const char *s;
340 1.7 lukem char *v;
341 1.1 cgd {
342 1.7 lukem value_t *p;
343 1.1 cgd
344 1.1 cgd p = vlookup(s);
345 1.1 cgd if (p == 0)
346 1.1 cgd return (1);
347 1.1 cgd if (p->v_type&NUMBER)
348 1.8 mrg vassign(p, (char *)(long)atoi(v));
349 1.1 cgd else {
350 1.1 cgd if (strcmp(s, "record") == 0)
351 1.1 cgd v = expand(v);
352 1.1 cgd vassign(p, v);
353 1.1 cgd }
354 1.1 cgd return (0);
355 1.1 cgd }
356