Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / tip / value.c
blob758e2e3c374f5534c58a5b9aa49e3f03d8e9140e
1 /* $NetBSD: value.c,v 1.13 2006/04/03 04:53:58 christos Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)value.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: value.c,v 1.13 2006/04/03 04:53:58 christos Exp $");
38 #endif /* not lint */
40 #include "tip.h"
42 #define MIDDLE 35
44 static int col = 0;
46 static int vaccess(int, int);
47 static void vassign(value_t *, char *);
48 static value_t *vlookup(const char *);
49 static void vprint(value_t *);
50 static void vtoken(char *);
53 * Variable manipulation
55 void
56 vinit(void)
58 value_t *p;
59 char *cp;
60 FILE *f;
61 char file[MAXPATHLEN];
63 for (p = vtable; p->v_name != NULL; p++) {
64 if (p->v_type&ENVIRON)
65 if ((cp = getenv(p->v_name)) != NULL)
66 p->v_value = cp;
67 if (p->v_type&IREMOTE)
68 setnumber(p->v_value, *address(p->v_value));
71 * Read the .tiprc file in the HOME directory
72 * for sets
74 (void)snprintf(file, sizeof(file), "%s/.tiprc", (char *)value(HOME));
75 if ((f = fopen(file, "r")) != NULL) {
76 char *tp;
78 while (fgets(file, sizeof(file)-1, f) != NULL) {
79 if (vflag)
80 (void)printf("set %s", file);
81 if ((tp = strrchr(file, '\n')) != NULL)
82 *tp = '\0';
83 vlex(file);
85 (void)fclose(f);
88 * To allow definition of exception prior to fork
90 vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
93 void
94 vassign(value_t *p, char *v)
97 if (!vaccess(p->v_access, (unsigned int)WRITE)) {
98 (void)printf("access denied\r\n");
99 return;
101 switch (p->v_type&TMASK) {
103 case STRING:
104 if (p->v_value && equal(p->v_value, v))
105 return;
106 if (!(p->v_type&(ENVIRON|INIT)))
107 free(p->v_value);
108 if ((p->v_value = strdup(v)) == NULL) {
109 (void)printf("out of core\r\n");
110 return;
112 p->v_type &= ~(ENVIRON|INIT);
113 break;
115 case NUMBER:
116 if (number(p->v_value) == number(v))
117 return;
118 setnumber(p->v_value, number(v));
119 break;
121 case BOOL:
122 if (boolean(p->v_value) == (*v != '!'))
123 return;
124 setboolean(p->v_value, (*v != '!'));
125 break;
127 case CHAR:
128 if (character(p->v_value) == *v)
129 return;
130 setcharacter(p->v_value, *v);
132 p->v_access |= CHANGED;
135 void
136 vlex(char *s)
138 value_t *p;
140 if (equal(s, "all")) {
141 for (p = vtable; p->v_name; p++)
142 if (vaccess(p->v_access, READ))
143 vprint(p);
144 } else {
145 char *cp;
147 do {
148 if ((cp = vinterp(s, ' ')) != NULL)
149 cp++;
150 vtoken(s);
151 s = cp;
152 } while (s);
154 if (col > 0) {
155 (void)printf("\r\n");
156 col = 0;
160 static void
161 vtoken(char *s)
163 value_t *p;
164 char *cp;
166 if ((cp = strchr(s, '=')) != NULL) {
167 *cp = '\0';
168 if ((p = vlookup(s)) != NULL) {
169 cp++;
170 if (p->v_type&NUMBER)
171 vassign(p, (char *)(long)atoi(cp));
172 else {
173 if (strcmp(s, "record") == 0)
174 cp = expand(cp);
175 vassign(p, cp);
177 return;
179 } else if ((cp = strchr(s, '?')) != NULL) {
180 *cp = '\0';
181 if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
182 vprint(p);
183 return;
185 } else {
186 if (*s != '!')
187 p = vlookup(s);
188 else
189 p = vlookup(s+1);
190 if (p != NULL) {
191 vassign(p, s);
192 return;
195 (void)printf("%s: unknown variable\r\n", s);
198 static void
199 vprint(value_t *p)
201 char *cp;
203 if (col > 0 && col < MIDDLE)
204 while (col++ < MIDDLE)
205 (void)putchar(' ');
206 col += strlen(p->v_name);
207 switch (p->v_type&TMASK) {
209 case BOOL:
210 if (boolean(p->v_value) == FALSE) {
211 col++;
212 (void)putchar('!');
214 (void)printf("%s", p->v_name);
215 break;
217 case STRING:
218 (void)printf("%s=", p->v_name);
219 col++;
220 if (p->v_value) {
221 cp = interp(p->v_value);
222 col += strlen(cp);
223 (void)printf("%s", cp);
225 break;
227 case NUMBER:
228 col += 6;
229 (void)printf("%s=%-5d", p->v_name, (int)number(p->v_value));
230 break;
232 case CHAR:
233 (void)printf("%s=", p->v_name);
234 col++;
235 if (p->v_value) {
236 cp = ctrl(character(p->v_value));
237 col += strlen(cp);
238 (void)printf("%s", cp);
240 break;
242 if (col >= MIDDLE) {
243 col = 0;
244 (void)printf("\r\n");
245 return;
250 static int
251 vaccess(int mode, int rw)
254 if (mode & (rw<<PUBLIC))
255 return (1);
256 if (mode & (rw<<PRIVATE))
257 return (1);
258 return ((mode & (rw<<ROOT)) && getuid() == 0);
261 static value_t *
262 vlookup(const char *s)
264 value_t *p;
266 for (p = vtable; p->v_name; p++)
267 if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
268 return (p);
269 return (NULL);
272 char *
273 vinterp(char *s, char stp)
275 char *p = s, c;
276 int num;
278 while ((c = *s++) && c != stp)
279 switch (c) {
281 case '^':
282 if (*s)
283 *p++ = *s++ - 0100;
284 else
285 *p++ = c;
286 break;
288 case '\\':
289 num = 0;
290 c = *s++;
291 if (c >= '0' && c <= '7')
292 num = (num<<3)+(c-'0');
293 else {
294 const char *q = "n\nr\rt\tb\bf\f";
296 for (; *q; q++)
297 if (c == *q++) {
298 *p++ = *q;
299 goto cont;
301 *p++ = c;
302 cont:
303 break;
305 if ((c = *s++) >= '0' && c <= '7') {
306 num = (num<<3)+(c-'0');
307 if ((c = *s++) >= '0' && c <= '7')
308 num = (num<<3)+(c-'0');
309 else
310 s--;
311 } else
312 s--;
313 *p++ = num;
314 break;
316 default:
317 *p++ = c;
319 *p = '\0';
320 return (c == stp ? s-1 : NULL);
324 * assign variable s with value v (for NUMBER or STRING or CHAR types)
328 vstring(const char *s, char *v)
330 value_t *p;
332 p = vlookup(s);
333 if (p == 0)
334 return (1);
335 if (p->v_type&NUMBER)
336 vassign(p, (char *)(long)atoi(v));
337 else {
338 if (strcmp(s, "record") == 0)
339 v = expand(v);
340 vassign(p, v);
342 return (0);