xref: /onnv-gate/usr/src/cmd/tip/value.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /* from UCB 4.5 6/25/83 */
7*0Sstevel@tonic-gate /*
8*0Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
9*0Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
10*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
11*0Sstevel@tonic-gate  */
12*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #include "tip.h"
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #define	MIDDLE	35
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate static value_t *vlookup();
19*0Sstevel@tonic-gate static int col = 0;
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate /*
22*0Sstevel@tonic-gate  * Variable manipulation
23*0Sstevel@tonic-gate  */
24*0Sstevel@tonic-gate vinit()
25*0Sstevel@tonic-gate {
26*0Sstevel@tonic-gate 	register value_t *p;
27*0Sstevel@tonic-gate 	register char *cp;
28*0Sstevel@tonic-gate 	FILE *f;
29*0Sstevel@tonic-gate 	char file[1024];
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 	for (p = vtable; p->v_name != NULL; p++) {
32*0Sstevel@tonic-gate 		if (p->v_type&ENVIRON)
33*0Sstevel@tonic-gate 			if (cp = getenv(p->v_name))
34*0Sstevel@tonic-gate 				p->v_value = cp;
35*0Sstevel@tonic-gate 		if (p->v_type&IREMOTE)
36*0Sstevel@tonic-gate 			number(p->v_value) = *address(p->v_value);
37*0Sstevel@tonic-gate 	}
38*0Sstevel@tonic-gate 	/*
39*0Sstevel@tonic-gate 	 * Read the .tiprc file in the HOME directory
40*0Sstevel@tonic-gate 	 *  for sets
41*0Sstevel@tonic-gate 	 */
42*0Sstevel@tonic-gate 	if ((cp = value(HOME)) == NULL)
43*0Sstevel@tonic-gate 		cp = "";
44*0Sstevel@tonic-gate 	strlcpy(file, cp, sizeof (file));
45*0Sstevel@tonic-gate 	strlcat(file, "/.tiprc", sizeof (file));
46*0Sstevel@tonic-gate 	if ((f = fopen(file, "r")) != NULL) {
47*0Sstevel@tonic-gate 		register char *tp;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 		while (fgets(file, sizeof (file)-1, f) != NULL) {
50*0Sstevel@tonic-gate 			if (file[0] == '#')
51*0Sstevel@tonic-gate 				continue;
52*0Sstevel@tonic-gate 			if (vflag)
53*0Sstevel@tonic-gate 				printf("set %s", file);
54*0Sstevel@tonic-gate 			if (tp = strrchr(file, '\n'))
55*0Sstevel@tonic-gate 				*tp = '\0';
56*0Sstevel@tonic-gate 			vlex(file);
57*0Sstevel@tonic-gate 		}
58*0Sstevel@tonic-gate 		fclose(f);
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate 	/*
61*0Sstevel@tonic-gate 	 * To allow definition of exception prior to fork
62*0Sstevel@tonic-gate 	 */
63*0Sstevel@tonic-gate 	vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*VARARGS1*/
67*0Sstevel@tonic-gate vassign(p, v)
68*0Sstevel@tonic-gate 	register value_t *p;
69*0Sstevel@tonic-gate 	char *v;
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	if (!vaccess(p->v_access, WRITE)) {
73*0Sstevel@tonic-gate 		printf("access denied\r\n");
74*0Sstevel@tonic-gate 		return;
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 	switch (p->v_type&TMASK) {
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	case STRING:
79*0Sstevel@tonic-gate 		if (p->v_value != (char *)NULL) {
80*0Sstevel@tonic-gate 			if (equal(p->v_value, v))
81*0Sstevel@tonic-gate 				return;
82*0Sstevel@tonic-gate 			if (!(p->v_type&(ENVIRON|INIT)))
83*0Sstevel@tonic-gate 				free(p->v_value);
84*0Sstevel@tonic-gate 		}
85*0Sstevel@tonic-gate 		if ((p->v_value = malloc(strlen(v)+1)) == NOSTR) {
86*0Sstevel@tonic-gate 			printf("out of core\r\n");
87*0Sstevel@tonic-gate 			return;
88*0Sstevel@tonic-gate 		}
89*0Sstevel@tonic-gate 		p->v_type &= ~(ENVIRON|INIT);
90*0Sstevel@tonic-gate 		strcpy(p->v_value, v);
91*0Sstevel@tonic-gate 		break;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	case NUMBER:
94*0Sstevel@tonic-gate 		if (number(p->v_value) == number(v))
95*0Sstevel@tonic-gate 			return;
96*0Sstevel@tonic-gate 		number(p->v_value) = number(v);
97*0Sstevel@tonic-gate 		break;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	case BOOL:
100*0Sstevel@tonic-gate 		if (boolean(p->v_value) == (*v != '!'))
101*0Sstevel@tonic-gate 			return;
102*0Sstevel@tonic-gate 		boolean(p->v_value) = (*v != '!');
103*0Sstevel@tonic-gate 		break;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	case CHAR:
106*0Sstevel@tonic-gate 		if (character(p->v_value) == *v)
107*0Sstevel@tonic-gate 			return;
108*0Sstevel@tonic-gate 		character(p->v_value) = *v;
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 	p->v_access |= CHANGED;
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate vlex(s)
114*0Sstevel@tonic-gate 	register char *s;
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	register value_t *p;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (equal(s, "all")) {
119*0Sstevel@tonic-gate 		for (p = vtable; p->v_name; p++)
120*0Sstevel@tonic-gate 			if (vaccess(p->v_access, READ))
121*0Sstevel@tonic-gate 				vprint(p);
122*0Sstevel@tonic-gate 	} else {
123*0Sstevel@tonic-gate 		register char *cp;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 		do {
126*0Sstevel@tonic-gate 			if (cp = vinterp(s, ' '))
127*0Sstevel@tonic-gate 				cp++;
128*0Sstevel@tonic-gate 			vtoken(s);
129*0Sstevel@tonic-gate 			s = cp;
130*0Sstevel@tonic-gate 		} while (s);
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	if (col > 0) {
133*0Sstevel@tonic-gate 		printf("\r\n");
134*0Sstevel@tonic-gate 		col = 0;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static int
139*0Sstevel@tonic-gate vtoken(s)
140*0Sstevel@tonic-gate 	register char *s;
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	register value_t *p;
143*0Sstevel@tonic-gate 	register char *cp, *cp2;
144*0Sstevel@tonic-gate 	char *expand();
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (cp = strchr(s, '=')) {
147*0Sstevel@tonic-gate 		*cp = '\0';
148*0Sstevel@tonic-gate 		if (p = vlookup(s)) {
149*0Sstevel@tonic-gate 			cp++;
150*0Sstevel@tonic-gate 			if (p->v_type&NUMBER)
151*0Sstevel@tonic-gate 				vassign(p, atoi(cp));
152*0Sstevel@tonic-gate 			else {
153*0Sstevel@tonic-gate 				if (strcmp(s, "record") == 0)
154*0Sstevel@tonic-gate 					if ((cp2 = expand(cp)) != NOSTR)
155*0Sstevel@tonic-gate 						cp = cp2;
156*0Sstevel@tonic-gate 				vassign(p, cp);
157*0Sstevel@tonic-gate 			}
158*0Sstevel@tonic-gate 			return;
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 	} else if (cp = strchr(s, '?')) {
161*0Sstevel@tonic-gate 		*cp = '\0';
162*0Sstevel@tonic-gate 		if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
163*0Sstevel@tonic-gate 			vprint(p);
164*0Sstevel@tonic-gate 			return;
165*0Sstevel@tonic-gate 		}
166*0Sstevel@tonic-gate 	} else {
167*0Sstevel@tonic-gate 		if (*s != '!')
168*0Sstevel@tonic-gate 			p = vlookup(s);
169*0Sstevel@tonic-gate 		else
170*0Sstevel@tonic-gate 			p = vlookup(s+1);
171*0Sstevel@tonic-gate 		if (p != NOVAL) {
172*0Sstevel@tonic-gate 			if (p->v_type&BOOL)
173*0Sstevel@tonic-gate 				vassign(p, s);
174*0Sstevel@tonic-gate 			else
175*0Sstevel@tonic-gate 				printf("%s: no value specified\r\n", s);
176*0Sstevel@tonic-gate 			return;
177*0Sstevel@tonic-gate 		}
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 	printf("%s: unknown variable\r\n", s);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate static int
183*0Sstevel@tonic-gate vprint(p)
184*0Sstevel@tonic-gate 	register value_t *p;
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate 	register char *cp;
187*0Sstevel@tonic-gate 	extern char *interp(), *ctrl();
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (col > 0 && col < MIDDLE)
190*0Sstevel@tonic-gate 		while (col++ < MIDDLE)
191*0Sstevel@tonic-gate 			putchar(' ');
192*0Sstevel@tonic-gate 	col += strlen(p->v_name);
193*0Sstevel@tonic-gate 	switch (p->v_type&TMASK) {
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	case BOOL:
196*0Sstevel@tonic-gate 		if (boolean(p->v_value) == FALSE) {
197*0Sstevel@tonic-gate 			col++;
198*0Sstevel@tonic-gate 			putchar('!');
199*0Sstevel@tonic-gate 		}
200*0Sstevel@tonic-gate 		printf("%s", p->v_name);
201*0Sstevel@tonic-gate 		break;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	case STRING:
204*0Sstevel@tonic-gate 		printf("%s=", p->v_name);
205*0Sstevel@tonic-gate 		col++;
206*0Sstevel@tonic-gate 		if (p->v_value) {
207*0Sstevel@tonic-gate 			cp = interp(p->v_value, NULL);
208*0Sstevel@tonic-gate 			col += strlen(cp);
209*0Sstevel@tonic-gate 			printf("%s", cp);
210*0Sstevel@tonic-gate 		}
211*0Sstevel@tonic-gate 		break;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	case NUMBER:
214*0Sstevel@tonic-gate 		col += 6;
215*0Sstevel@tonic-gate 		printf("%s=%-5d", p->v_name, number(p->v_value));
216*0Sstevel@tonic-gate 		break;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	case CHAR:
219*0Sstevel@tonic-gate 		printf("%s=", p->v_name);
220*0Sstevel@tonic-gate 		col++;
221*0Sstevel@tonic-gate 		if (p->v_value) {
222*0Sstevel@tonic-gate 			cp = ctrl(character(p->v_value));
223*0Sstevel@tonic-gate 			col += strlen(cp);
224*0Sstevel@tonic-gate 			printf("%s", cp);
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 		break;
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	if (col >= MIDDLE) {
229*0Sstevel@tonic-gate 		col = 0;
230*0Sstevel@tonic-gate 		printf("\r\n");
231*0Sstevel@tonic-gate 		return;
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate static int
237*0Sstevel@tonic-gate vaccess(mode, rw)
238*0Sstevel@tonic-gate 	register unsigned mode, rw;
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate 	if (mode & (rw<<PUBLIC))
241*0Sstevel@tonic-gate 		return (1);
242*0Sstevel@tonic-gate 	if (mode & (rw<<PRIVATE))
243*0Sstevel@tonic-gate 		return (1);
244*0Sstevel@tonic-gate 	return ((mode & (rw<<ROOT)) && uid == 0);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate static value_t *
248*0Sstevel@tonic-gate vlookup(s)
249*0Sstevel@tonic-gate 	register char *s;
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	register value_t *p;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	for (p = vtable; p->v_name; p++)
254*0Sstevel@tonic-gate 		if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
255*0Sstevel@tonic-gate 			return (p);
256*0Sstevel@tonic-gate 	return (NULL);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate char *
260*0Sstevel@tonic-gate vinterp(s, stop)
261*0Sstevel@tonic-gate 	register char *s;
262*0Sstevel@tonic-gate 	char stop;
263*0Sstevel@tonic-gate {
264*0Sstevel@tonic-gate 	register char *p = s, c;
265*0Sstevel@tonic-gate 	int num;
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	while ((c = *s++) && c != stop)
268*0Sstevel@tonic-gate 		switch (c) {
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 		case '^':
271*0Sstevel@tonic-gate 			if (*s)
272*0Sstevel@tonic-gate 				*p++ = *s++ - 0100;
273*0Sstevel@tonic-gate 			else
274*0Sstevel@tonic-gate 				*p++ = c;
275*0Sstevel@tonic-gate 			break;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		case '\\':
278*0Sstevel@tonic-gate 			num = 0;
279*0Sstevel@tonic-gate 			c = *s++;
280*0Sstevel@tonic-gate 			if (c >= '0' && c <= '7')
281*0Sstevel@tonic-gate 				num = (num<<3)+(c-'0');
282*0Sstevel@tonic-gate 			else {
283*0Sstevel@tonic-gate 				register char *q = "n\nr\rt\tb\bf\f";
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 				for (; *q; q++)
286*0Sstevel@tonic-gate 					if (c == *q++) {
287*0Sstevel@tonic-gate 						*p++ = *q;
288*0Sstevel@tonic-gate 						goto cont;
289*0Sstevel@tonic-gate 					}
290*0Sstevel@tonic-gate 				*p++ = c;
291*0Sstevel@tonic-gate 			cont:
292*0Sstevel@tonic-gate 				break;
293*0Sstevel@tonic-gate 			}
294*0Sstevel@tonic-gate 			if ((c = *s++) >= '0' && c <= '7') {
295*0Sstevel@tonic-gate 				num = (num<<3)+(c-'0');
296*0Sstevel@tonic-gate 				if ((c = *s++) >= '0' && c <= '7')
297*0Sstevel@tonic-gate 					num = (num<<3)+(c-'0');
298*0Sstevel@tonic-gate 				else
299*0Sstevel@tonic-gate 					s--;
300*0Sstevel@tonic-gate 			} else
301*0Sstevel@tonic-gate 				s--;
302*0Sstevel@tonic-gate 			*p++ = num;
303*0Sstevel@tonic-gate 			break;
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 		default:
306*0Sstevel@tonic-gate 			*p++ = c;
307*0Sstevel@tonic-gate 		}
308*0Sstevel@tonic-gate 	*p = '\0';
309*0Sstevel@tonic-gate 	return (c == stop ? s-1 : NULL);
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate  * assign variable s with value v (for NUMBER or STRING or CHAR types)
314*0Sstevel@tonic-gate  */
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate vstring(s, v)
317*0Sstevel@tonic-gate 	register char *s;
318*0Sstevel@tonic-gate 	register char *v;
319*0Sstevel@tonic-gate {
320*0Sstevel@tonic-gate 	register value_t *p;
321*0Sstevel@tonic-gate 	char *v2;
322*0Sstevel@tonic-gate 	char *expand();
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	p = vlookup(s);
325*0Sstevel@tonic-gate 	if (p == 0)
326*0Sstevel@tonic-gate 		return (1);
327*0Sstevel@tonic-gate 	if (p->v_type&NUMBER)
328*0Sstevel@tonic-gate 		vassign(p, atoi(v));
329*0Sstevel@tonic-gate 	else {
330*0Sstevel@tonic-gate 		if (strcmp(s, "record") == 0)
331*0Sstevel@tonic-gate 			if ((v2 = expand(v)) != NOSTR)
332*0Sstevel@tonic-gate 				v = v2;
333*0Sstevel@tonic-gate 		vassign(p, v);
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 	return (0);
336*0Sstevel@tonic-gate }
337