2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)value.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/tip/tip/value.c,v 1.5.2.1 2000/07/01 12:24:23 ps Exp $
37 static value_t *vlookup(char *);
38 int vstring(char *, char *);
40 void vassign(value_t *, char *);
45 * Variable manipulation
55 for (p = vtable; p->v_name != NULL; p++) {
56 if (p->v_type&ENVIRON)
57 if ((cp = getenv(p->v_name)))
59 if (p->v_type&IREMOTE) {
61 p->v_value = *(char **) address(p->v_value);
63 number(p->v_value) = *address(p->v_value);
67 * Read the .tiprc file in the HOME directory
70 strcpy(file, value(HOME));
71 strcat(file, "/.tiprc");
72 if ((f = fopen(file, "r")) != NULL) {
75 while (fgets(file, sizeof(file)-1, f) != NULL) {
77 printf("set %s", file);
78 if ((tp = rindex(file, '\n')))
85 * To allow definition of exception prior to fork
87 vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
90 static int vaccess(unsigned, unsigned);
94 vassign(value_t *p, char *v)
97 if (!vaccess(p->v_access, WRITE)) {
98 printf("access denied\r\n");
101 switch (p->v_type&TMASK) {
104 if (p->v_value && equal(p->v_value, v))
106 if (!(p->v_type&(ENVIRON|INIT)))
108 if ((p->v_value = malloc(size(v)+1)) == NULL) {
109 printf("out of core\r\n");
112 p->v_type &= ~(ENVIRON|INIT);
113 strcpy(p->v_value, v);
117 if (number(p->v_value) == number(v))
119 number(p->v_value) = number(v);
123 if (boolean(p->v_value) == (*v != '!'))
125 boolean(p->v_value) = (*v != '!');
129 if (character(p->v_value) == *v)
131 character(p->v_value) = *v;
133 p->v_access |= CHANGED;
136 static void vprint(value_t *);
137 static void vtoken(char *);
144 if (equal(s, "all")) {
145 for (p = vtable; p->v_name; p++)
146 if (vaccess(p->v_access, READ))
152 if ((cp = vinterp(s, ' ')))
170 if ((cp = index(s, '='))) {
172 if ((p = vlookup(s))) {
174 if (p->v_type&NUMBER)
175 vassign(p, (char *)(intptr_t)atoi(cp));
177 if (strcmp(s, "record") == 0)
183 } else if ((cp = index(s, '?'))) {
185 if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
199 printf("%s: unknown variable\r\n", s);
207 if (col > 0 && col < MIDDLE)
208 while (col++ < MIDDLE)
210 col += size(p->v_name);
211 switch (p->v_type&TMASK) {
214 if (boolean(p->v_value) == FALSE) {
218 printf("%s", p->v_name);
222 printf("%s=", p->v_name);
225 cp = interp(p->v_value);
233 printf("%s=%-5d", p->v_name, number(p->v_value));
237 printf("%s=", p->v_name);
240 cp = ctrl(character(p->v_value));
255 vaccess(unsigned mode, unsigned rw)
257 if (mode & (rw<<PUBLIC))
259 if (mode & (rw<<PRIVATE))
261 return ((mode & (rw<<ROOT)) && getuid() == 0);
269 for (p = vtable; p->v_name; p++)
270 if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
276 vinterp(char *s, char stop)
281 while ((c = *s++) && c != stop)
294 if (c >= '0' && c <= '7')
295 num = (num<<3)+(c-'0');
297 char *q = "n\nr\rt\tb\bf\f";
308 if ((c = *s++) >= '0' && c <= '7') {
309 num = (num<<3)+(c-'0');
310 if ((c = *s++) >= '0' && c <= '7')
311 num = (num<<3)+(c-'0');
323 return (c == stop ? s-1 : NULL);
327 * assign variable s with value v (for NUMBER or STRING or CHAR types)
331 vstring(char *s, char *v)
338 if (p->v_type&NUMBER)
339 vassign(p, (char *)(intptr_t)atoi(v));
341 if (strcmp(s, "record") == 0)