xref: /csrg-svn/usr.bin/tip/value.c (revision 13280)
1*13280Ssam #ifndef lint
2*13280Ssam static char sccsid[] = "@(#)value.c	4.5 (Berkeley) 06/25/83";
3*13280Ssam #endif
4*13280Ssam 
53700Sroot #include "tip.h"
63700Sroot 
73700Sroot #define MIDDLE	35
83700Sroot 
93700Sroot static value_t *vlookup();
103700Sroot static int col = 0;
113700Sroot 
123700Sroot /*
133700Sroot  * Variable manipulation
143700Sroot  */
153700Sroot vinit()
163700Sroot {
173700Sroot 	register value_t *p;
183700Sroot 	register char *cp;
193700Sroot 	FILE *f;
203700Sroot 	char file[256];
213700Sroot 
223700Sroot 	for (p = vtable; p->v_name != NULL; p++) {
233700Sroot 		if (p->v_type&ENVIRON)
243700Sroot 			if (cp = getenv(p->v_name))
253700Sroot 				p->v_value = cp;
263700Sroot 		if (p->v_type&IREMOTE)
273700Sroot 			number(p->v_value) = *address(p->v_value);
283700Sroot 	}
293700Sroot 	/*
303700Sroot 	 * Read the .tiprc file in the HOME directory
313700Sroot 	 *  for sets
323700Sroot 	 */
333700Sroot 	strcpy(file, value(HOME));
343700Sroot 	strcat(file, "/.tiprc");
353700Sroot 	if ((f = fopen(file, "r")) != NULL) {
363700Sroot 		register char *tp;
373700Sroot 
383700Sroot 		while (fgets(file, sizeof(file)-1, f) != NULL) {
393700Sroot 			if (vflag)
403700Sroot 				printf("set %s", file);
413700Sroot 			if (tp = rindex(file, '\n'))
423700Sroot 				*tp = '\0';
433700Sroot 			vlex(file);
443700Sroot 		}
453700Sroot 		fclose(f);
463700Sroot 	}
473700Sroot 	/*
483700Sroot 	 * To allow definition of exception prior to fork
493700Sroot 	 */
503700Sroot 	vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
513700Sroot }
523700Sroot 
533700Sroot /*VARARGS1*/
543700Sroot vassign(p, v)
554963Ssam 	register value_t *p;
564963Ssam 	char *v;
573700Sroot {
583700Sroot 
593700Sroot 	if (!vaccess(p->v_access, WRITE)) {
603700Sroot 		printf("access denied\r\n");
613700Sroot 		return;
623700Sroot 	}
635138Ssam 	switch (p->v_type&TMASK) {
643700Sroot 
655138Ssam 	case STRING:
665138Ssam 		if (equal(p->v_value, v))
675138Ssam 			return;
685138Ssam 		if (!(p->v_type&(ENVIRON|INIT)))
695138Ssam 			free(p->v_value);
705138Ssam 		if ((p->v_value = malloc(size(v)+1)) == NOSTR) {
715138Ssam 			printf("out of core\r\n");
725138Ssam 			return;
735138Ssam 		}
745138Ssam 		p->v_type &= ~(ENVIRON|INIT);
755138Ssam 		strcpy(p->v_value, v);
765138Ssam 		break;
773700Sroot 
785138Ssam 	case NUMBER:
795138Ssam 		if (number(p->v_value) == number(v))
805138Ssam 			return;
815138Ssam 		number(p->v_value) = number(v);
825138Ssam 		break;
833700Sroot 
845138Ssam 	case BOOL:
855138Ssam 		if (boolean(p->v_value) == (*v != '!'))
865138Ssam 			return;
875138Ssam 		boolean(p->v_value) = (*v != '!');
885138Ssam 		break;
893700Sroot 
905138Ssam 	case CHAR:
915138Ssam 		if (character(p->v_value) == *v)
925138Ssam 			return;
935138Ssam 		character(p->v_value) = *v;
943700Sroot 	}
953700Sroot 	p->v_access |= CHANGED;
963700Sroot }
973700Sroot 
983700Sroot vlex(s)
994963Ssam 	register char *s;
1003700Sroot {
1013700Sroot 	register value_t *p;
1023700Sroot 
1033700Sroot 	if (equal(s, "all")) {
1043700Sroot 		for (p = vtable; p->v_name; p++)
1053700Sroot 			if (vaccess(p->v_access, READ))
1063700Sroot 				vprint(p);
1073700Sroot 	} else {
1083700Sroot 		register char *cp;
1093700Sroot 
1103700Sroot 		do {
1113700Sroot 			if (cp = vinterp(s, ' '))
1123700Sroot 				cp++;
1133700Sroot 			vtoken(s);
1143700Sroot 			s = cp;
1153700Sroot 		} while (s);
1163700Sroot 	}
1173700Sroot 	if (col > 0) {
1183700Sroot 		printf("\r\n");
1193700Sroot 		col = 0;
1203700Sroot 	}
1213700Sroot }
1223700Sroot 
1233700Sroot static int
1243700Sroot vtoken(s)
1254963Ssam 	register char *s;
1263700Sroot {
1273700Sroot 	register value_t *p;
1283700Sroot 	register char *cp;
12913140Sralph 	char *expand();
1303700Sroot 
1313700Sroot 	if (cp = index(s, '=')) {
1323700Sroot 		*cp = '\0';
1333700Sroot 		if (p = vlookup(s)) {
1343700Sroot 			cp++;
1353700Sroot 			if (p->v_type&NUMBER)
1363700Sroot 				vassign(p, atoi(cp));
13713140Sralph 			else {
13813140Sralph 				if (strcmp(s, "record") == 0)
13913140Sralph 					cp = expand(cp);
1403700Sroot 				vassign(p, cp);
14113140Sralph 			}
1423700Sroot 			return;
1433700Sroot 		}
1443700Sroot 	} else if (cp = index(s, '?')) {
1453700Sroot 		*cp = '\0';
1463700Sroot 		if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
1473700Sroot 			vprint(p);
1483700Sroot 			return;
1493700Sroot 		}
1503700Sroot 	} else {
1513700Sroot 		if (*s != '!')
1523700Sroot 			p = vlookup(s);
1533700Sroot 		else
1543700Sroot 			p = vlookup(s+1);
1553700Sroot 		if (p != NOVAL) {
1563700Sroot 			vassign(p, s);
1573700Sroot 			return;
1583700Sroot 		}
1593700Sroot 	}
1603700Sroot 	printf("%s: unknown variable\r\n", s);
1613700Sroot }
1623700Sroot 
1633700Sroot static int
1643700Sroot vprint(p)
1654963Ssam 	register value_t *p;
1663700Sroot {
1673700Sroot 	register char *cp;
1683700Sroot 	extern char *interp(), *ctrl();
1693700Sroot 
1703700Sroot 	if (col > 0 && col < MIDDLE)
1713700Sroot 		while (col++ < MIDDLE)
1723700Sroot 			putchar(' ');
1733700Sroot 	col += size(p->v_name);
1745138Ssam 	switch (p->v_type&TMASK) {
1755138Ssam 
1765138Ssam 	case BOOL:
1775138Ssam 		if (boolean(p->v_value) == FALSE) {
1783700Sroot 			col++;
1795138Ssam 			putchar('!');
1805138Ssam 		}
1815138Ssam 		printf("%s", p->v_name);
1825138Ssam 		break;
1835138Ssam 
1845138Ssam 	case STRING:
1855138Ssam 		printf("%s=", p->v_name);
1865138Ssam 		col++;
1875138Ssam 		if (p->v_value) {
18813140Sralph 			cp = interp(p->v_value, NULL);
1895138Ssam 			col += size(cp);
1905138Ssam 			printf("%s", cp);
1915138Ssam 		}
1925138Ssam 		break;
1935138Ssam 
1945138Ssam 	case NUMBER:
1955138Ssam 		col += 6;
1965138Ssam 		printf("%s=%-5d", p->v_name, number(p->v_value));
1975138Ssam 		break;
1985138Ssam 
1995138Ssam 	case CHAR:
2005138Ssam 		printf("%s=", p->v_name);
2015138Ssam 		col++;
2025138Ssam 		if (p->v_value) {
2035138Ssam 			cp = ctrl(character(p->v_value));
2045138Ssam 			col += size(cp);
2055138Ssam 			printf("%s", cp);
2065138Ssam 		}
2075138Ssam 		break;
2083700Sroot 	}
2093700Sroot 	if (col >= MIDDLE) {
2103700Sroot 		col = 0;
2113700Sroot 		printf("\r\n");
2123700Sroot 		return;
2133700Sroot 	}
2143700Sroot }
2153700Sroot 
2163700Sroot 
2173700Sroot static int
2183700Sroot vaccess(mode, rw)
2194963Ssam 	register unsigned mode, rw;
2203700Sroot {
2213700Sroot 	if (mode & (rw<<PUBLIC))
2225138Ssam 		return (1);
2233700Sroot 	if (mode & (rw<<PRIVATE))
2245138Ssam 		return (1);
2255138Ssam 	return ((mode & (rw<<ROOT)) && getuid() == 0);
2263700Sroot }
2273700Sroot 
2283700Sroot static value_t *
2293700Sroot vlookup(s)
2304963Ssam 	register char *s;
2313700Sroot {
2323700Sroot 	register value_t *p;
2333700Sroot 
2343700Sroot 	for (p = vtable; p->v_name; p++)
2353700Sroot 		if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
2365138Ssam 			return (p);
2375138Ssam 	return (NULL);
2383700Sroot }
2393700Sroot 
2403700Sroot char *
2413700Sroot vinterp(s, stop)
2424963Ssam 	register char *s;
2435138Ssam 	char stop;
2443700Sroot {
2453700Sroot 	register char *p = s, c;
2463700Sroot 	int num;
2473700Sroot 
2485138Ssam 	while ((c = *s++) && c != stop)
2495138Ssam 		switch (c) {
2505138Ssam 
2513700Sroot 		case '^':
2523700Sroot 			if (*s)
2533700Sroot 				*p++ = *s++ - 0100;
2543700Sroot 			else
2553700Sroot 				*p++ = c;
2563700Sroot 			break;
2573700Sroot 
2583700Sroot 		case '\\':
2593700Sroot 			num = 0;
2603700Sroot 			c = *s++;
2613700Sroot 			if (c >= '0' && c <= '7')
2623700Sroot 				num = (num<<3)+(c-'0');
2633700Sroot 			else {
2643700Sroot 				register char *q = "n\nr\rt\tb\bf\f";
2653700Sroot 
2663700Sroot 				for (; *q; q++)
2673700Sroot 					if (c == *q++) {
2683700Sroot 						*p++ = *q;
2693700Sroot 						goto cont;
2703700Sroot 					}
2713700Sroot 				*p++ = c;
2723700Sroot 			cont:
2733700Sroot 				break;
2743700Sroot 			}
2753700Sroot 			if ((c = *s++) >= '0' && c <= '7') {
2763700Sroot 				num = (num<<3)+(c-'0');
2773700Sroot 				if ((c = *s++) >= '0' && c <= '7')
2783700Sroot 					num = (num<<3)+(c-'0');
2793700Sroot 				else
2803700Sroot 					s--;
2813700Sroot 			} else
2823700Sroot 				s--;
2833700Sroot 			*p++ = num;
2843700Sroot 			break;
2853700Sroot 
2863700Sroot 		default:
2873700Sroot 			*p++ = c;
2885138Ssam 		}
2893700Sroot 	*p = '\0';
2905138Ssam 	return (c == stop ? s-1 : NULL);
2913700Sroot }
29213140Sralph 
29313140Sralph /*
29413140Sralph  * assign variable s with value v (for NUMBER or STRING or CHAR types)
29513140Sralph  */
29613140Sralph 
29713140Sralph vstring(s,v)
29813140Sralph 	register char *s;
29913140Sralph 	register char *v;
30013140Sralph {
30113140Sralph 	register value_t *p;
30213140Sralph 	char *expand();
30313140Sralph 
304*13280Ssam 	p = vlookup(s);
305*13280Ssam 	if (p == 0)
306*13280Ssam 		return (1);
307*13280Ssam 	if (p->v_type&NUMBER)
308*13280Ssam 		vassign(p, atoi(v));
309*13280Ssam 	else {
310*13280Ssam 		if (strcmp(s, "record") == 0)
311*13280Ssam 			v = expand(v);
312*13280Ssam 		vassign(p, v);
313*13280Ssam 	}
314*13280Ssam 	return (0);
31513140Sralph }
316