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