2 * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved. The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
12 #pragma ident "%Z%%M% %I% %E% SMI"
18 static value_t
*vlookup(char *);
21 extern char *interp(char *);
23 static void vtoken(char *);
24 static void vprint(value_t
*);
25 static int vaccess(unsigned, unsigned);
28 * Variable manipulation
38 for (p
= vtable
; p
->v_name
!= NULL
; p
++) {
39 if (p
->v_type
&ENVIRON
)
40 if (cp
= getenv(p
->v_name
))
42 if (p
->v_type
&IREMOTE
)
43 number(p
->v_value
) = *address(p
->v_value
);
46 * Read the .tiprc file in the HOME directory
49 if ((cp
= value(HOME
)) == NULL
)
51 (void) strlcpy(file
, cp
, sizeof (file
));
52 (void) strlcat(file
, "/.tiprc", sizeof (file
));
53 if ((f
= fopen(file
, "r")) != NULL
) {
56 while (fgets(file
, sizeof (file
)-1, f
) != NULL
) {
60 (void) printf("set %s", file
);
61 if (tp
= strrchr(file
, '\n'))
68 * To allow definition of exception prior to fork
70 vtable
[EXCEPTIONS
].v_access
&= ~(WRITE
<<PUBLIC
);
75 vassign(value_t
*p
, char *v
)
78 if (!vaccess(p
->v_access
, WRITE
)) {
79 (void) printf("access denied\r\n");
82 switch (p
->v_type
&TMASK
) {
85 if (p
->v_value
!= (char *)NULL
) {
86 if (equal(p
->v_value
, v
))
88 if (!(p
->v_type
&(ENVIRON
|INIT
)))
91 if ((p
->v_value
= malloc(strlen(v
)+1)) == NOSTR
) {
92 (void) printf("out of core\r\n");
95 p
->v_type
&= ~(ENVIRON
|INIT
);
96 (void) strcpy(p
->v_value
, v
);
100 if (number(p
->v_value
) == number(v
))
102 number(p
->v_value
) = number(v
);
106 if (boolean(p
->v_value
) == (*v
!= '!'))
108 boolean(p
->v_value
) = (*v
!= '!');
112 if (character(p
->v_value
) == *v
)
114 character(p
->v_value
) = *v
;
116 p
->v_access
|= CHANGED
;
124 if (equal(s
, "all")) {
125 for (p
= vtable
; p
->v_name
; p
++)
126 if (vaccess(p
->v_access
, READ
))
132 if (cp
= vinterp(s
, ' '))
139 (void) printf("\r\n");
150 if (cp
= strchr(s
, '=')) {
152 if (p
= vlookup(s
)) {
154 if (p
->v_type
&NUMBER
)
155 vassign(p
, (char *)atoi(cp
));
157 if (strcmp(s
, "record") == 0)
158 if ((cp2
= expand(cp
)) != NOSTR
)
164 } else if (cp
= strchr(s
, '?')) {
166 if ((p
= vlookup(s
)) != NULL
&& vaccess(p
->v_access
, READ
)) {
179 (void) printf("%s: no value specified\r\n", s
);
183 (void) printf("%s: unknown variable\r\n", s
);
191 if (col
> 0 && col
< MIDDLE
)
192 while (col
++ < MIDDLE
)
194 col
+= strlen(p
->v_name
);
195 switch (p
->v_type
&TMASK
) {
198 if (boolean(p
->v_value
) == FALSE
) {
202 (void) printf("%s", p
->v_name
);
206 (void) printf("%s=", p
->v_name
);
209 cp
= interp(p
->v_value
);
211 (void) printf("%s", cp
);
217 (void) printf("%s=%-5d", p
->v_name
, number(p
->v_value
));
221 (void) printf("%s=", p
->v_name
);
224 cp
= ctrl(character(p
->v_value
));
226 (void) printf("%s", cp
);
232 (void) printf("\r\n");
239 vaccess(unsigned mode
, unsigned rw
)
241 if (mode
& (rw
<<PUBLIC
))
243 if (mode
& (rw
<<PRIVATE
))
245 return ((mode
& (rw
<<ROOT
)) && uid
== 0);
253 for (p
= vtable
; p
->v_name
; p
++)
254 if (equal(p
->v_name
, s
) || (p
->v_abrev
&& equal(p
->v_abrev
, s
)))
260 vinterp(char *s
, char stop
)
265 while ((c
= *s
++) != 0 && c
!= stop
)
278 if (c
>= '0' && c
<= '7')
279 num
= (num
<<3)+(c
-'0');
281 char *q
= "n\nr\rt\tb\bf\f";
292 if ((c
= *s
++) >= '0' && c
<= '7') {
293 num
= (num
<<3)+(c
-'0');
294 if ((c
= *s
++) >= '0' && c
<= '7')
295 num
= (num
<<3)+(c
-'0');
307 return (c
== stop
? s
-1 : NULL
);
311 * assign variable s with value v (for NUMBER or STRING or CHAR types)
314 vstring(char *s
, char *v
)
322 if (p
->v_type
&NUMBER
)
323 vassign(p
, (char *)atoi(v
));
325 if (strcmp(s
, "record") == 0)
326 if ((v2
= expand(v
)) != NOSTR
)