xref: /illumos-gate/usr/src/cmd/tip/value.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
2*8d489c7aSmuffin  * Copyright 2000 Sun Microsystems, Inc.  All rights reserved.
3*8d489c7aSmuffin  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
11*8d489c7aSmuffin 
127c478bd9Sstevel@tonic-gate #include "tip.h"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #define	MIDDLE	35
157c478bd9Sstevel@tonic-gate 
16*8d489c7aSmuffin static value_t *vlookup(char *);
177c478bd9Sstevel@tonic-gate static int col = 0;
187c478bd9Sstevel@tonic-gate 
19*8d489c7aSmuffin extern char	*interp(char *);
20*8d489c7aSmuffin 
21*8d489c7aSmuffin static void	vtoken(char *);
22*8d489c7aSmuffin static void	vprint(value_t *);
23*8d489c7aSmuffin static int	vaccess(unsigned, unsigned);
24*8d489c7aSmuffin 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Variable manipulation
277c478bd9Sstevel@tonic-gate  */
28*8d489c7aSmuffin void
vinit(void)29*8d489c7aSmuffin vinit(void)
307c478bd9Sstevel@tonic-gate {
31*8d489c7aSmuffin 	value_t *p;
32*8d489c7aSmuffin 	char *cp;
337c478bd9Sstevel@tonic-gate 	FILE *f;
347c478bd9Sstevel@tonic-gate 	char file[1024];
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 	for (p = vtable; p->v_name != NULL; p++) {
377c478bd9Sstevel@tonic-gate 		if (p->v_type&ENVIRON)
387c478bd9Sstevel@tonic-gate 			if (cp = getenv(p->v_name))
397c478bd9Sstevel@tonic-gate 				p->v_value = cp;
407c478bd9Sstevel@tonic-gate 		if (p->v_type&IREMOTE)
417c478bd9Sstevel@tonic-gate 			number(p->v_value) = *address(p->v_value);
427c478bd9Sstevel@tonic-gate 	}
437c478bd9Sstevel@tonic-gate 	/*
447c478bd9Sstevel@tonic-gate 	 * Read the .tiprc file in the HOME directory
457c478bd9Sstevel@tonic-gate 	 *  for sets
467c478bd9Sstevel@tonic-gate 	 */
477c478bd9Sstevel@tonic-gate 	if ((cp = value(HOME)) == NULL)
487c478bd9Sstevel@tonic-gate 		cp = "";
49*8d489c7aSmuffin 	(void) strlcpy(file, cp, sizeof (file));
50*8d489c7aSmuffin 	(void) strlcat(file, "/.tiprc", sizeof (file));
517c478bd9Sstevel@tonic-gate 	if ((f = fopen(file, "r")) != NULL) {
52*8d489c7aSmuffin 		char *tp;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 		while (fgets(file, sizeof (file)-1, f) != NULL) {
557c478bd9Sstevel@tonic-gate 			if (file[0] == '#')
567c478bd9Sstevel@tonic-gate 				continue;
577c478bd9Sstevel@tonic-gate 			if (vflag)
58*8d489c7aSmuffin 				(void) printf("set %s", file);
597c478bd9Sstevel@tonic-gate 			if (tp = strrchr(file, '\n'))
607c478bd9Sstevel@tonic-gate 				*tp = '\0';
617c478bd9Sstevel@tonic-gate 			vlex(file);
627c478bd9Sstevel@tonic-gate 		}
63*8d489c7aSmuffin 		(void) fclose(f);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 	/*
667c478bd9Sstevel@tonic-gate 	 * To allow definition of exception prior to fork
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 	vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*VARARGS1*/
72*8d489c7aSmuffin void
vassign(value_t * p,char * v)73*8d489c7aSmuffin vassign(value_t *p, char *v)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (!vaccess(p->v_access, WRITE)) {
77*8d489c7aSmuffin 		(void) printf("access denied\r\n");
787c478bd9Sstevel@tonic-gate 		return;
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	switch (p->v_type&TMASK) {
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	case STRING:
837c478bd9Sstevel@tonic-gate 		if (p->v_value != (char *)NULL) {
847c478bd9Sstevel@tonic-gate 			if (equal(p->v_value, v))
857c478bd9Sstevel@tonic-gate 				return;
867c478bd9Sstevel@tonic-gate 			if (!(p->v_type&(ENVIRON|INIT)))
877c478bd9Sstevel@tonic-gate 				free(p->v_value);
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 		if ((p->v_value = malloc(strlen(v)+1)) == NOSTR) {
90*8d489c7aSmuffin 			(void) printf("out of core\r\n");
917c478bd9Sstevel@tonic-gate 			return;
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		p->v_type &= ~(ENVIRON|INIT);
94*8d489c7aSmuffin 		(void) strcpy(p->v_value, v);
957c478bd9Sstevel@tonic-gate 		break;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	case NUMBER:
987c478bd9Sstevel@tonic-gate 		if (number(p->v_value) == number(v))
997c478bd9Sstevel@tonic-gate 			return;
1007c478bd9Sstevel@tonic-gate 		number(p->v_value) = number(v);
1017c478bd9Sstevel@tonic-gate 		break;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	case BOOL:
1047c478bd9Sstevel@tonic-gate 		if (boolean(p->v_value) == (*v != '!'))
1057c478bd9Sstevel@tonic-gate 			return;
1067c478bd9Sstevel@tonic-gate 		boolean(p->v_value) = (*v != '!');
1077c478bd9Sstevel@tonic-gate 		break;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	case CHAR:
1107c478bd9Sstevel@tonic-gate 		if (character(p->v_value) == *v)
1117c478bd9Sstevel@tonic-gate 			return;
1127c478bd9Sstevel@tonic-gate 		character(p->v_value) = *v;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	p->v_access |= CHANGED;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
117*8d489c7aSmuffin void
vlex(char * s)118*8d489c7aSmuffin vlex(char *s)
1197c478bd9Sstevel@tonic-gate {
120*8d489c7aSmuffin 	value_t *p;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (equal(s, "all")) {
1237c478bd9Sstevel@tonic-gate 		for (p = vtable; p->v_name; p++)
1247c478bd9Sstevel@tonic-gate 			if (vaccess(p->v_access, READ))
1257c478bd9Sstevel@tonic-gate 				vprint(p);
1267c478bd9Sstevel@tonic-gate 	} else {
127*8d489c7aSmuffin 		char *cp;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		do {
1307c478bd9Sstevel@tonic-gate 			if (cp = vinterp(s, ' '))
1317c478bd9Sstevel@tonic-gate 				cp++;
1327c478bd9Sstevel@tonic-gate 			vtoken(s);
1337c478bd9Sstevel@tonic-gate 			s = cp;
1347c478bd9Sstevel@tonic-gate 		} while (s);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	if (col > 0) {
137*8d489c7aSmuffin 		(void) printf("\r\n");
1387c478bd9Sstevel@tonic-gate 		col = 0;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
142*8d489c7aSmuffin static void
vtoken(char * s)143*8d489c7aSmuffin vtoken(char *s)
1447c478bd9Sstevel@tonic-gate {
145*8d489c7aSmuffin 	value_t *p;
146*8d489c7aSmuffin 	char *cp, *cp2;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (cp = strchr(s, '=')) {
1497c478bd9Sstevel@tonic-gate 		*cp = '\0';
1507c478bd9Sstevel@tonic-gate 		if (p = vlookup(s)) {
1517c478bd9Sstevel@tonic-gate 			cp++;
1527c478bd9Sstevel@tonic-gate 			if (p->v_type&NUMBER)
153*8d489c7aSmuffin 				vassign(p, (char *)atoi(cp));
1547c478bd9Sstevel@tonic-gate 			else {
1557c478bd9Sstevel@tonic-gate 				if (strcmp(s, "record") == 0)
1567c478bd9Sstevel@tonic-gate 					if ((cp2 = expand(cp)) != NOSTR)
1577c478bd9Sstevel@tonic-gate 						cp = cp2;
1587c478bd9Sstevel@tonic-gate 				vassign(p, cp);
1597c478bd9Sstevel@tonic-gate 			}
1607c478bd9Sstevel@tonic-gate 			return;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 	} else if (cp = strchr(s, '?')) {
1637c478bd9Sstevel@tonic-gate 		*cp = '\0';
164*8d489c7aSmuffin 		if ((p = vlookup(s)) != NULL && vaccess(p->v_access, READ)) {
1657c478bd9Sstevel@tonic-gate 			vprint(p);
1667c478bd9Sstevel@tonic-gate 			return;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	} else {
1697c478bd9Sstevel@tonic-gate 		if (*s != '!')
1707c478bd9Sstevel@tonic-gate 			p = vlookup(s);
1717c478bd9Sstevel@tonic-gate 		else
1727c478bd9Sstevel@tonic-gate 			p = vlookup(s+1);
1737c478bd9Sstevel@tonic-gate 		if (p != NOVAL) {
1747c478bd9Sstevel@tonic-gate 			if (p->v_type&BOOL)
1757c478bd9Sstevel@tonic-gate 				vassign(p, s);
1767c478bd9Sstevel@tonic-gate 			else
177*8d489c7aSmuffin 				(void) printf("%s: no value specified\r\n", s);
1787c478bd9Sstevel@tonic-gate 			return;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	}
181*8d489c7aSmuffin 	(void) printf("%s: unknown variable\r\n", s);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
184*8d489c7aSmuffin static void
vprint(value_t * p)185*8d489c7aSmuffin vprint(value_t *p)
1867c478bd9Sstevel@tonic-gate {
187*8d489c7aSmuffin 	char *cp;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (col > 0 && col < MIDDLE)
1907c478bd9Sstevel@tonic-gate 		while (col++ < MIDDLE)
191*8d489c7aSmuffin 			(void) putchar(' ');
1927c478bd9Sstevel@tonic-gate 	col += strlen(p->v_name);
1937c478bd9Sstevel@tonic-gate 	switch (p->v_type&TMASK) {
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	case BOOL:
1967c478bd9Sstevel@tonic-gate 		if (boolean(p->v_value) == FALSE) {
1977c478bd9Sstevel@tonic-gate 			col++;
198*8d489c7aSmuffin 			(void) putchar('!');
1997c478bd9Sstevel@tonic-gate 		}
200*8d489c7aSmuffin 		(void) printf("%s", p->v_name);
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	case STRING:
204*8d489c7aSmuffin 		(void) printf("%s=", p->v_name);
2057c478bd9Sstevel@tonic-gate 		col++;
2067c478bd9Sstevel@tonic-gate 		if (p->v_value) {
207*8d489c7aSmuffin 			cp = interp(p->v_value);
2087c478bd9Sstevel@tonic-gate 			col += strlen(cp);
209*8d489c7aSmuffin 			(void) printf("%s", cp);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 		break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	case NUMBER:
2147c478bd9Sstevel@tonic-gate 		col += 6;
215*8d489c7aSmuffin 		(void) printf("%s=%-5d", p->v_name, number(p->v_value));
2167c478bd9Sstevel@tonic-gate 		break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	case CHAR:
219*8d489c7aSmuffin 		(void) printf("%s=", p->v_name);
2207c478bd9Sstevel@tonic-gate 		col++;
2217c478bd9Sstevel@tonic-gate 		if (p->v_value) {
2227c478bd9Sstevel@tonic-gate 			cp = ctrl(character(p->v_value));
2237c478bd9Sstevel@tonic-gate 			col += strlen(cp);
224*8d489c7aSmuffin 			(void) printf("%s", cp);
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		break;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	if (col >= MIDDLE) {
2297c478bd9Sstevel@tonic-gate 		col = 0;
230*8d489c7aSmuffin 		(void) printf("\r\n");
2317c478bd9Sstevel@tonic-gate 		return;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static int
vaccess(unsigned mode,unsigned rw)237*8d489c7aSmuffin vaccess(unsigned mode, unsigned rw)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	if (mode & (rw<<PUBLIC))
2407c478bd9Sstevel@tonic-gate 		return (1);
2417c478bd9Sstevel@tonic-gate 	if (mode & (rw<<PRIVATE))
2427c478bd9Sstevel@tonic-gate 		return (1);
2437c478bd9Sstevel@tonic-gate 	return ((mode & (rw<<ROOT)) && uid == 0);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static value_t *
vlookup(char * s)247*8d489c7aSmuffin vlookup(char *s)
2487c478bd9Sstevel@tonic-gate {
249*8d489c7aSmuffin 	value_t *p;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	for (p = vtable; p->v_name; p++)
2527c478bd9Sstevel@tonic-gate 		if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
2537c478bd9Sstevel@tonic-gate 			return (p);
2547c478bd9Sstevel@tonic-gate 	return (NULL);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate char *
vinterp(char * s,char stop)258*8d489c7aSmuffin vinterp(char *s, char stop)
2597c478bd9Sstevel@tonic-gate {
260*8d489c7aSmuffin 	char *p = s, c;
2617c478bd9Sstevel@tonic-gate 	int num;
2627c478bd9Sstevel@tonic-gate 
263*8d489c7aSmuffin 	while ((c = *s++) != 0 && c != stop)
2647c478bd9Sstevel@tonic-gate 		switch (c) {
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		case '^':
2677c478bd9Sstevel@tonic-gate 			if (*s)
2687c478bd9Sstevel@tonic-gate 				*p++ = *s++ - 0100;
2697c478bd9Sstevel@tonic-gate 			else
2707c478bd9Sstevel@tonic-gate 				*p++ = c;
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		case '\\':
2747c478bd9Sstevel@tonic-gate 			num = 0;
2757c478bd9Sstevel@tonic-gate 			c = *s++;
2767c478bd9Sstevel@tonic-gate 			if (c >= '0' && c <= '7')
2777c478bd9Sstevel@tonic-gate 				num = (num<<3)+(c-'0');
2787c478bd9Sstevel@tonic-gate 			else {
279*8d489c7aSmuffin 				char *q = "n\nr\rt\tb\bf\f";
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 				for (; *q; q++)
2827c478bd9Sstevel@tonic-gate 					if (c == *q++) {
2837c478bd9Sstevel@tonic-gate 						*p++ = *q;
2847c478bd9Sstevel@tonic-gate 						goto cont;
2857c478bd9Sstevel@tonic-gate 					}
2867c478bd9Sstevel@tonic-gate 				*p++ = c;
2877c478bd9Sstevel@tonic-gate 			cont:
2887c478bd9Sstevel@tonic-gate 				break;
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 			if ((c = *s++) >= '0' && c <= '7') {
2917c478bd9Sstevel@tonic-gate 				num = (num<<3)+(c-'0');
2927c478bd9Sstevel@tonic-gate 				if ((c = *s++) >= '0' && c <= '7')
2937c478bd9Sstevel@tonic-gate 					num = (num<<3)+(c-'0');
2947c478bd9Sstevel@tonic-gate 				else
2957c478bd9Sstevel@tonic-gate 					s--;
2967c478bd9Sstevel@tonic-gate 			} else
2977c478bd9Sstevel@tonic-gate 				s--;
2987c478bd9Sstevel@tonic-gate 			*p++ = num;
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		default:
3027c478bd9Sstevel@tonic-gate 			*p++ = c;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	*p = '\0';
3057c478bd9Sstevel@tonic-gate 	return (c == stop ? s-1 : NULL);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * assign variable s with value v (for NUMBER or STRING or CHAR types)
3107c478bd9Sstevel@tonic-gate  */
311*8d489c7aSmuffin int
vstring(char * s,char * v)312*8d489c7aSmuffin vstring(char *s, char *v)
3137c478bd9Sstevel@tonic-gate {
314*8d489c7aSmuffin 	value_t *p;
3157c478bd9Sstevel@tonic-gate 	char *v2;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	p = vlookup(s);
3187c478bd9Sstevel@tonic-gate 	if (p == 0)
3197c478bd9Sstevel@tonic-gate 		return (1);
3207c478bd9Sstevel@tonic-gate 	if (p->v_type&NUMBER)
321*8d489c7aSmuffin 		vassign(p, (char *)atoi(v));
3227c478bd9Sstevel@tonic-gate 	else {
3237c478bd9Sstevel@tonic-gate 		if (strcmp(s, "record") == 0)
3247c478bd9Sstevel@tonic-gate 			if ((v2 = expand(v)) != NOSTR)
3257c478bd9Sstevel@tonic-gate 				v = v2;
3267c478bd9Sstevel@tonic-gate 		vassign(p, v);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	return (0);
3297c478bd9Sstevel@tonic-gate }
330