1 /* $NetBSD: value.c,v 1.13 2006/04/03 04:53:58 christos Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)value.c 8.1 (Berkeley) 6/6/93";
37 __RCSID("$NetBSD: value.c,v 1.13 2006/04/03 04:53:58 christos Exp $");
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
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
)
67 if (p
->v_type
&IREMOTE
)
68 setnumber(p
->v_value
, *address(p
->v_value
));
71 * Read the .tiprc file in the HOME directory
74 (void)snprintf(file
, sizeof(file
), "%s/.tiprc", (char *)value(HOME
));
75 if ((f
= fopen(file
, "r")) != NULL
) {
78 while (fgets(file
, sizeof(file
)-1, f
) != NULL
) {
80 (void)printf("set %s", file
);
81 if ((tp
= strrchr(file
, '\n')) != NULL
)
88 * To allow definition of exception prior to fork
90 vtable
[EXCEPTIONS
].v_access
&= ~(WRITE
<<PUBLIC
);
94 vassign(value_t
*p
, char *v
)
97 if (!vaccess(p
->v_access
, (unsigned int)WRITE
)) {
98 (void)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
= strdup(v
)) == NULL
) {
109 (void)printf("out of core\r\n");
112 p
->v_type
&= ~(ENVIRON
|INIT
);
116 if (number(p
->v_value
) == number(v
))
118 setnumber(p
->v_value
, number(v
));
122 if (boolean(p
->v_value
) == (*v
!= '!'))
124 setboolean(p
->v_value
, (*v
!= '!'));
128 if (character(p
->v_value
) == *v
)
130 setcharacter(p
->v_value
, *v
);
132 p
->v_access
|= CHANGED
;
140 if (equal(s
, "all")) {
141 for (p
= vtable
; p
->v_name
; p
++)
142 if (vaccess(p
->v_access
, READ
))
148 if ((cp
= vinterp(s
, ' ')) != NULL
)
155 (void)printf("\r\n");
166 if ((cp
= strchr(s
, '=')) != NULL
) {
168 if ((p
= vlookup(s
)) != NULL
) {
170 if (p
->v_type
&NUMBER
)
171 vassign(p
, (char *)(long)atoi(cp
));
173 if (strcmp(s
, "record") == 0)
179 } else if ((cp
= strchr(s
, '?')) != NULL
) {
181 if ((p
= vlookup(s
)) && vaccess(p
->v_access
, READ
)) {
195 (void)printf("%s: unknown variable\r\n", s
);
203 if (col
> 0 && col
< MIDDLE
)
204 while (col
++ < MIDDLE
)
206 col
+= strlen(p
->v_name
);
207 switch (p
->v_type
&TMASK
) {
210 if (boolean(p
->v_value
) == FALSE
) {
214 (void)printf("%s", p
->v_name
);
218 (void)printf("%s=", p
->v_name
);
221 cp
= interp(p
->v_value
);
223 (void)printf("%s", cp
);
229 (void)printf("%s=%-5d", p
->v_name
, (int)number(p
->v_value
));
233 (void)printf("%s=", p
->v_name
);
236 cp
= ctrl(character(p
->v_value
));
238 (void)printf("%s", cp
);
244 (void)printf("\r\n");
251 vaccess(int mode
, int rw
)
254 if (mode
& (rw
<<PUBLIC
))
256 if (mode
& (rw
<<PRIVATE
))
258 return ((mode
& (rw
<<ROOT
)) && getuid() == 0);
262 vlookup(const char *s
)
266 for (p
= vtable
; p
->v_name
; p
++)
267 if (equal(p
->v_name
, s
) || (p
->v_abrev
&& equal(p
->v_abrev
, s
)))
273 vinterp(char *s
, char stp
)
278 while ((c
= *s
++) && c
!= stp
)
291 if (c
>= '0' && c
<= '7')
292 num
= (num
<<3)+(c
-'0');
294 const char *q
= "n\nr\rt\tb\bf\f";
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');
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
)
335 if (p
->v_type
&NUMBER
)
336 vassign(p
, (char *)(long)atoi(v
));
338 if (strcmp(s
, "record") == 0)