1 /* $OpenBSD: value.c,v 1.14 2006/03/17 22:02:58 moritz Exp $ */
2 /* $NetBSD: value.c,v 1.6 1997/02/11 09:24:09 mrg Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 static char sccsid[] = "@(#)value.c 8.1 (Berkeley) 6/6/93";
39 static const char rcsid[] = "$OpenBSD: value.c,v 1.14 2006/03/17 22:02:58 moritz Exp $";
47 static value_t *vlookup(char *);
48 static void vassign(value_t *, char *);
49 static void vtoken(char *);
50 static void vprint(value_t *);
51 static int vaccess(unsigned int, unsigned int);
52 static char *vinterp(char *, int);
54 static size_t col = 0;
57 * Variable manipulation
62 char file[FILENAME_MAX], *cp;
66 for (p = vtable; p->v_name != NULL; p++) {
67 if (p->v_type&ENVIRON)
68 if ((cp = getenv(p->v_name)))
70 if (p->v_type&IREMOTE)
71 setnumber(p->v_value, *address(p->v_value));
74 * Read the .tiprc file in the HOME directory
80 "$HOME not set. Skipping check for ~/.tiprc\n");
81 } else if (strlen(cp) + sizeof("/.tiprc") > sizeof(file)) {
82 (void)fprintf(stderr, "Home directory path too long: %s\n",
85 snprintf(file, sizeof file, "%s/.tiprc", value(HOME));
86 if ((fp = fopen(file, "r")) != NULL) {
89 while (fgets(file, sizeof(file)-1, fp) != NULL) {
91 printf("set %s", file);
92 if ((tp = strrchr(file, '\n')))
100 * To allow definition of exception prior to fork
102 vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
107 vassign(value_t *p, char *v)
109 if (!vaccess(p->v_access, WRITE)) {
110 printf("access denied\r\n");
114 switch (p->v_type&TMASK) {
116 if (p->v_value && equal(p->v_value, v))
118 if (!(p->v_type&(ENVIRON|INIT)))
120 if ((p->v_value = strdup(v)) == NOSTR) {
121 printf("out of core\r\n");
124 p->v_type &= ~(ENVIRON|INIT);
127 if (number(p->v_value) == number(v))
129 setnumber(p->v_value, number(v));
132 if (boolean(p->v_value) == (*v != '!'))
134 setboolean(p->v_value, (*v != '!'));
137 if (character(p->v_value) == *v)
139 setcharacter(p->v_value, *v);
141 p->v_access |= CHANGED;
150 if (equal(s, "all")) {
151 for (p = vtable; p->v_name; p++)
152 if (vaccess(p->v_access, READ))
156 if ((cp = vinterp(s, ' ')))
174 if ((cp = strchr(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 = strchr(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=%-5ld", p->v_name, number(p->v_value));
241 printf("%s=", p->v_name);
244 cp = ctrl(character(p->v_value));
258 vaccess(unsigned int mode, unsigned int rw)
260 if (mode & (rw<<PUBLIC))
262 if (mode & (rw<<PRIVATE))
264 return ((mode & (rw<<ROOT)) && getuid() == 0);
272 for (p = vtable; p->v_name; p++)
273 if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
279 vinterp(char *s, int stop)
284 while ((c = *s++) && c != stop) {
297 if (c >= '0' && c <= '7')
298 num = (num<<3)+(c-'0');
300 char *q = "n\nr\rt\tb\bf\f";
311 if ((c = *s++) >= '0' && c <= '7') {
312 num = (num<<3)+(c-'0');
313 if ((c = *s++) >= '0' && c <= '7')
314 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)
334 vstring(char *s, char *v)
341 if (p->v_type&NUMBER)
342 vassign(p, (char *)(intptr_t)atoi(v));
344 if (strcmp(s, "record") == 0)