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 $
35 * $DragonFly: src/usr.bin/tip/tip/value.c,v 1.5 2005/05/07 23:20:43 corecode Exp $
42 static value_t
*vlookup();
43 int vstring(char *, char *);
45 void vassign(value_t
*, char *);
50 * Variable manipulation
60 for (p
= vtable
; p
->v_name
!= NULL
; p
++) {
61 if (p
->v_type
&ENVIRON
)
62 if ((cp
= getenv(p
->v_name
)))
64 if (p
->v_type
&IREMOTE
)
66 p
->v_value
= *(char **) address(p
->v_value
);
68 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
);
103 if (!vaccess(p
->v_access
, WRITE
)) {
104 printf("access denied\r\n");
107 switch (p
->v_type
&TMASK
) {
110 if (p
->v_value
&& equal(p
->v_value
, v
))
112 if (!(p
->v_type
&(ENVIRON
|INIT
)))
114 if ((p
->v_value
= malloc(size(v
)+1)) == NULL
) {
115 printf("out of core\r\n");
118 p
->v_type
&= ~(ENVIRON
|INIT
);
119 strcpy(p
->v_value
, v
);
123 if (number(p
->v_value
) == number(v
))
125 number(p
->v_value
) = number(v
);
129 if (boolean(p
->v_value
) == (*v
!= '!'))
131 boolean(p
->v_value
) = (*v
!= '!');
135 if (character(p
->v_value
) == *v
)
137 character(p
->v_value
) = *v
;
139 p
->v_access
|= CHANGED
;
142 static void vprint();
143 static void vtoken(char *);
151 if (equal(s
, "all")) {
152 for (p
= vtable
; p
->v_name
; p
++)
153 if (vaccess(p
->v_access
, READ
))
159 if ((cp
= vinterp(s
, ' ')))
179 if ((cp
= index(s
, '='))) {
181 if ((p
= vlookup(s
))) {
183 if (p
->v_type
&NUMBER
)
184 vassign(p
, (char *)(intptr_t)atoi(cp
));
186 if (strcmp(s
, "record") == 0)
192 } else if ((cp
= index(s
, '?'))) {
194 if ((p
= vlookup(s
)) && vaccess(p
->v_access
, READ
)) {
208 printf("%s: unknown variable\r\n", s
);
216 extern char *interp(), *ctrl();
218 if (col
> 0 && col
< MIDDLE
)
219 while (col
++ < MIDDLE
)
221 col
+= size(p
->v_name
);
222 switch (p
->v_type
&TMASK
) {
225 if (boolean(p
->v_value
) == FALSE
) {
229 printf("%s", p
->v_name
);
233 printf("%s=", p
->v_name
);
236 cp
= interp(p
->v_value
, NULL
);
244 printf("%s=%-5d", p
->v_name
, number(p
->v_value
));
248 printf("%s=", p
->v_name
);
251 cp
= ctrl(character(p
->v_value
));
267 register unsigned mode
, rw
;
269 if (mode
& (rw
<<PUBLIC
))
271 if (mode
& (rw
<<PRIVATE
))
273 return ((mode
& (rw
<<ROOT
)) && getuid() == 0);
282 for (p
= vtable
; p
->v_name
; p
++)
283 if (equal(p
->v_name
, s
) || (p
->v_abrev
&& equal(p
->v_abrev
, s
)))
293 register char *p
= s
, c
;
296 while ((c
= *s
++) && c
!= stop
)
309 if (c
>= '0' && c
<= '7')
310 num
= (num
<<3)+(c
-'0');
312 register char *q
= "n\nr\rt\tb\bf\f";
323 if ((c
= *s
++) >= '0' && c
<= '7') {
324 num
= (num
<<3)+(c
-'0');
325 if ((c
= *s
++) >= '0' && c
<= '7')
326 num
= (num
<<3)+(c
-'0');
338 return (c
== stop
? s
-1 : NULL
);
342 * assign variable s with value v (for NUMBER or STRING or CHAR types)
356 if (p
->v_type
&NUMBER
)
357 vassign(p
, (char *)(intptr_t)atoi(v
));
359 if (strcmp(s
, "record") == 0)