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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)value.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/tip/tip/value.c,v 1.5.2.1 2000/07/01 12:24:23 ps Exp $
41 static value_t *vlookup(char *);
42 int vstring(char *, char *);
44 void vassign(value_t *, char *);
49 * Variable manipulation
59 for (p = vtable; p->v_name != NULL; p++) {
60 if (p->v_type&ENVIRON)
61 if ((cp = getenv(p->v_name)))
63 if (p->v_type&IREMOTE) {
65 p->v_value = *(char **) address(p->v_value);
67 number(p->v_value) = *address(p->v_value);
71 * Read the .tiprc file in the HOME directory
74 strcpy(file, value(HOME));
75 strcat(file, "/.tiprc");
76 if ((f = fopen(file, "r")) != NULL) {
79 while (fgets(file, sizeof(file)-1, f) != NULL) {
81 printf("set %s", file);
82 if ((tp = rindex(file, '\n')))
89 * To allow definition of exception prior to fork
91 vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
94 static int vaccess(unsigned, unsigned);
98 vassign(value_t *p, char *v)
101 if (!vaccess(p->v_access, WRITE)) {
102 printf("access denied\r\n");
105 switch (p->v_type&TMASK) {
108 if (p->v_value && equal(p->v_value, v))
110 if (!(p->v_type&(ENVIRON|INIT)))
112 if ((p->v_value = malloc(size(v)+1)) == NULL) {
113 printf("out of core\r\n");
116 p->v_type &= ~(ENVIRON|INIT);
117 strcpy(p->v_value, v);
121 if (number(p->v_value) == number(v))
123 number(p->v_value) = number(v);
127 if (boolean(p->v_value) == (*v != '!'))
129 boolean(p->v_value) = (*v != '!');
133 if (character(p->v_value) == *v)
135 character(p->v_value) = *v;
137 p->v_access |= CHANGED;
140 static void vprint(value_t *);
141 static void vtoken(char *);
148 if (equal(s, "all")) {
149 for (p = vtable; p->v_name; p++)
150 if (vaccess(p->v_access, READ))
156 if ((cp = vinterp(s, ' ')))
174 if ((cp = index(s, '='))) {
176 if ((p = vlookup(s))) {
178 if (p->v_type&NUMBER)
179 vassign(p, (char *)(intptr_t)atoi(cp));
181 if (strcmp(s, "record") == 0)
187 } else if ((cp = index(s, '?'))) {
189 if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
203 printf("%s: unknown variable\r\n", s);
211 if (col > 0 && col < MIDDLE)
212 while (col++ < MIDDLE)
214 col += size(p->v_name);
215 switch (p->v_type&TMASK) {
218 if (boolean(p->v_value) == FALSE) {
222 printf("%s", p->v_name);
226 printf("%s=", p->v_name);
229 cp = interp(p->v_value);
237 printf("%s=%-5d", p->v_name, number(p->v_value));
241 printf("%s=", p->v_name);
244 cp = ctrl(character(p->v_value));
259 vaccess(unsigned mode, unsigned rw)
261 if (mode & (rw<<PUBLIC))
263 if (mode & (rw<<PRIVATE))
265 return ((mode & (rw<<ROOT)) && getuid() == 0);
273 for (p = vtable; p->v_name; p++)
274 if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
280 vinterp(char *s, char stop)
285 while ((c = *s++) && c != stop)
298 if (c >= '0' && c <= '7')
299 num = (num<<3)+(c-'0');
301 char *q = "n\nr\rt\tb\bf\f";
312 if ((c = *s++) >= '0' && c <= '7') {
313 num = (num<<3)+(c-'0');
314 if ((c = *s++) >= '0' && c <= '7')
315 num = (num<<3)+(c-'0');
327 return (c == stop ? s-1 : NULL);
331 * assign variable s with value v (for NUMBER or STRING or CHAR types)
335 vstring(char *s, char *v)
342 if (p->v_type&NUMBER)
343 vassign(p, (char *)(intptr_t)atoi(v));
345 if (strcmp(s, "record") == 0)