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
36 static char sccsid[] = "@(#)value.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.bin/tip/tip/value.c,v 1.5.2.1 2000/07/01 12:24:23 ps Exp $";
46 static value_t *vlookup();
47 int vstring __P((char *, char *));
48 void vlex __P((char *));
49 void vassign __P((value_t *, char *));
54 * Variable manipulation
64 for (p = vtable; p->v_name != NULL; p++) {
65 if (p->v_type&ENVIRON)
66 if ((cp = getenv(p->v_name)))
68 if (p->v_type&IREMOTE)
70 p->v_value = *(char **) address(p->v_value);
72 number(p->v_value) = *address(p->v_value);
75 * Read the .tiprc file in the HOME directory
78 strcpy(file, value(HOME));
79 strcat(file, "/.tiprc");
80 if ((f = fopen(file, "r")) != NULL) {
83 while (fgets(file, sizeof(file)-1, f) != NULL) {
85 printf("set %s", file);
86 if ((tp = rindex(file, '\n')))
93 * To allow definition of exception prior to fork
95 vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
107 if (!vaccess(p->v_access, WRITE)) {
108 printf("access denied\r\n");
111 switch (p->v_type&TMASK) {
114 if (p->v_value && equal(p->v_value, v))
116 if (!(p->v_type&(ENVIRON|INIT)))
118 if ((p->v_value = malloc(size(v)+1)) == NOSTR) {
119 printf("out of core\r\n");
122 p->v_type &= ~(ENVIRON|INIT);
123 strcpy(p->v_value, v);
127 if (number(p->v_value) == number(v))
129 number(p->v_value) = number(v);
133 if (boolean(p->v_value) == (*v != '!'))
135 boolean(p->v_value) = (*v != '!');
139 if (character(p->v_value) == *v)
141 character(p->v_value) = *v;
143 p->v_access |= CHANGED;
146 static void vprint();
153 static void vtoken();
155 if (equal(s, "all")) {
156 for (p = vtable; p->v_name; p++)
157 if (vaccess(p->v_access, READ))
163 if ((cp = vinterp(s, ' ')))
183 if ((cp = index(s, '='))) {
185 if ((p = vlookup(s))) {
187 if (p->v_type&NUMBER)
188 vassign(p, (char *) atoi(cp));
190 if (strcmp(s, "record") == 0)
196 } else if ((cp = index(s, '?'))) {
198 if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
212 printf("%s: unknown variable\r\n", s);
220 extern char *interp(), *ctrl();
222 if (col > 0 && col < MIDDLE)
223 while (col++ < MIDDLE)
225 col += size(p->v_name);
226 switch (p->v_type&TMASK) {
229 if (boolean(p->v_value) == FALSE) {
233 printf("%s", p->v_name);
237 printf("%s=", p->v_name);
240 cp = interp(p->v_value, NULL);
248 printf("%s=%-5d", p->v_name, number(p->v_value));
252 printf("%s=", p->v_name);
255 cp = ctrl(character(p->v_value));
271 register unsigned mode, rw;
273 if (mode & (rw<<PUBLIC))
275 if (mode & (rw<<PRIVATE))
277 return ((mode & (rw<<ROOT)) && getuid() == 0);
286 for (p = vtable; p->v_name; p++)
287 if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
297 register char *p = s, c;
300 while ((c = *s++) && c != stop)
313 if (c >= '0' && c <= '7')
314 num = (num<<3)+(c-'0');
316 register char *q = "n\nr\rt\tb\bf\f";
327 if ((c = *s++) >= '0' && c <= '7') {
328 num = (num<<3)+(c-'0');
329 if ((c = *s++) >= '0' && c <= '7')
330 num = (num<<3)+(c-'0');
342 return (c == stop ? s-1 : NULL);
346 * assign variable s with value v (for NUMBER or STRING or CHAR types)
360 if (p->v_type&NUMBER)
361 vassign(p, (char *) atoi(v));
363 if (strcmp(s, "record") == 0)