1*19802Sdist /* 2*19802Sdist * Copyright (c) 1983 Regents of the University of California. 3*19802Sdist * All rights reserved. The Berkeley software License Agreement 4*19802Sdist * specifies the terms and conditions for redistribution. 5*19802Sdist */ 6*19802Sdist 713280Ssam #ifndef lint 8*19802Sdist static char sccsid[] = "@(#)value.c 5.1 (Berkeley) 04/30/85"; 9*19802Sdist #endif not lint 1013280Ssam 113700Sroot #include "tip.h" 123700Sroot 133700Sroot #define MIDDLE 35 143700Sroot 153700Sroot static value_t *vlookup(); 163700Sroot static int col = 0; 173700Sroot 183700Sroot /* 193700Sroot * Variable manipulation 203700Sroot */ 213700Sroot vinit() 223700Sroot { 233700Sroot register value_t *p; 243700Sroot register char *cp; 253700Sroot FILE *f; 263700Sroot char file[256]; 273700Sroot 283700Sroot for (p = vtable; p->v_name != NULL; p++) { 293700Sroot if (p->v_type&ENVIRON) 303700Sroot if (cp = getenv(p->v_name)) 313700Sroot p->v_value = cp; 323700Sroot if (p->v_type&IREMOTE) 333700Sroot number(p->v_value) = *address(p->v_value); 343700Sroot } 353700Sroot /* 363700Sroot * Read the .tiprc file in the HOME directory 373700Sroot * for sets 383700Sroot */ 393700Sroot strcpy(file, value(HOME)); 403700Sroot strcat(file, "/.tiprc"); 413700Sroot if ((f = fopen(file, "r")) != NULL) { 423700Sroot register char *tp; 433700Sroot 443700Sroot while (fgets(file, sizeof(file)-1, f) != NULL) { 453700Sroot if (vflag) 463700Sroot printf("set %s", file); 473700Sroot if (tp = rindex(file, '\n')) 483700Sroot *tp = '\0'; 493700Sroot vlex(file); 503700Sroot } 513700Sroot fclose(f); 523700Sroot } 533700Sroot /* 543700Sroot * To allow definition of exception prior to fork 553700Sroot */ 563700Sroot vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC); 573700Sroot } 583700Sroot 593700Sroot /*VARARGS1*/ 603700Sroot vassign(p, v) 614963Ssam register value_t *p; 624963Ssam char *v; 633700Sroot { 643700Sroot 653700Sroot if (!vaccess(p->v_access, WRITE)) { 663700Sroot printf("access denied\r\n"); 673700Sroot return; 683700Sroot } 695138Ssam switch (p->v_type&TMASK) { 703700Sroot 715138Ssam case STRING: 725138Ssam if (equal(p->v_value, v)) 735138Ssam return; 745138Ssam if (!(p->v_type&(ENVIRON|INIT))) 755138Ssam free(p->v_value); 765138Ssam if ((p->v_value = malloc(size(v)+1)) == NOSTR) { 775138Ssam printf("out of core\r\n"); 785138Ssam return; 795138Ssam } 805138Ssam p->v_type &= ~(ENVIRON|INIT); 815138Ssam strcpy(p->v_value, v); 825138Ssam break; 833700Sroot 845138Ssam case NUMBER: 855138Ssam if (number(p->v_value) == number(v)) 865138Ssam return; 875138Ssam number(p->v_value) = number(v); 885138Ssam break; 893700Sroot 905138Ssam case BOOL: 915138Ssam if (boolean(p->v_value) == (*v != '!')) 925138Ssam return; 935138Ssam boolean(p->v_value) = (*v != '!'); 945138Ssam break; 953700Sroot 965138Ssam case CHAR: 975138Ssam if (character(p->v_value) == *v) 985138Ssam return; 995138Ssam character(p->v_value) = *v; 1003700Sroot } 1013700Sroot p->v_access |= CHANGED; 1023700Sroot } 1033700Sroot 1043700Sroot vlex(s) 1054963Ssam register char *s; 1063700Sroot { 1073700Sroot register value_t *p; 1083700Sroot 1093700Sroot if (equal(s, "all")) { 1103700Sroot for (p = vtable; p->v_name; p++) 1113700Sroot if (vaccess(p->v_access, READ)) 1123700Sroot vprint(p); 1133700Sroot } else { 1143700Sroot register char *cp; 1153700Sroot 1163700Sroot do { 1173700Sroot if (cp = vinterp(s, ' ')) 1183700Sroot cp++; 1193700Sroot vtoken(s); 1203700Sroot s = cp; 1213700Sroot } while (s); 1223700Sroot } 1233700Sroot if (col > 0) { 1243700Sroot printf("\r\n"); 1253700Sroot col = 0; 1263700Sroot } 1273700Sroot } 1283700Sroot 1293700Sroot static int 1303700Sroot vtoken(s) 1314963Ssam register char *s; 1323700Sroot { 1333700Sroot register value_t *p; 1343700Sroot register char *cp; 13513140Sralph char *expand(); 1363700Sroot 1373700Sroot if (cp = index(s, '=')) { 1383700Sroot *cp = '\0'; 1393700Sroot if (p = vlookup(s)) { 1403700Sroot cp++; 1413700Sroot if (p->v_type&NUMBER) 1423700Sroot vassign(p, atoi(cp)); 14313140Sralph else { 14413140Sralph if (strcmp(s, "record") == 0) 14513140Sralph cp = expand(cp); 1463700Sroot vassign(p, cp); 14713140Sralph } 1483700Sroot return; 1493700Sroot } 1503700Sroot } else if (cp = index(s, '?')) { 1513700Sroot *cp = '\0'; 1523700Sroot if ((p = vlookup(s)) && vaccess(p->v_access, READ)) { 1533700Sroot vprint(p); 1543700Sroot return; 1553700Sroot } 1563700Sroot } else { 1573700Sroot if (*s != '!') 1583700Sroot p = vlookup(s); 1593700Sroot else 1603700Sroot p = vlookup(s+1); 1613700Sroot if (p != NOVAL) { 1623700Sroot vassign(p, s); 1633700Sroot return; 1643700Sroot } 1653700Sroot } 1663700Sroot printf("%s: unknown variable\r\n", s); 1673700Sroot } 1683700Sroot 1693700Sroot static int 1703700Sroot vprint(p) 1714963Ssam register value_t *p; 1723700Sroot { 1733700Sroot register char *cp; 1743700Sroot extern char *interp(), *ctrl(); 1753700Sroot 1763700Sroot if (col > 0 && col < MIDDLE) 1773700Sroot while (col++ < MIDDLE) 1783700Sroot putchar(' '); 1793700Sroot col += size(p->v_name); 1805138Ssam switch (p->v_type&TMASK) { 1815138Ssam 1825138Ssam case BOOL: 1835138Ssam if (boolean(p->v_value) == FALSE) { 1843700Sroot col++; 1855138Ssam putchar('!'); 1865138Ssam } 1875138Ssam printf("%s", p->v_name); 1885138Ssam break; 1895138Ssam 1905138Ssam case STRING: 1915138Ssam printf("%s=", p->v_name); 1925138Ssam col++; 1935138Ssam if (p->v_value) { 19413140Sralph cp = interp(p->v_value, NULL); 1955138Ssam col += size(cp); 1965138Ssam printf("%s", cp); 1975138Ssam } 1985138Ssam break; 1995138Ssam 2005138Ssam case NUMBER: 2015138Ssam col += 6; 2025138Ssam printf("%s=%-5d", p->v_name, number(p->v_value)); 2035138Ssam break; 2045138Ssam 2055138Ssam case CHAR: 2065138Ssam printf("%s=", p->v_name); 2075138Ssam col++; 2085138Ssam if (p->v_value) { 2095138Ssam cp = ctrl(character(p->v_value)); 2105138Ssam col += size(cp); 2115138Ssam printf("%s", cp); 2125138Ssam } 2135138Ssam break; 2143700Sroot } 2153700Sroot if (col >= MIDDLE) { 2163700Sroot col = 0; 2173700Sroot printf("\r\n"); 2183700Sroot return; 2193700Sroot } 2203700Sroot } 2213700Sroot 2223700Sroot 2233700Sroot static int 2243700Sroot vaccess(mode, rw) 2254963Ssam register unsigned mode, rw; 2263700Sroot { 2273700Sroot if (mode & (rw<<PUBLIC)) 2285138Ssam return (1); 2293700Sroot if (mode & (rw<<PRIVATE)) 2305138Ssam return (1); 2315138Ssam return ((mode & (rw<<ROOT)) && getuid() == 0); 2323700Sroot } 2333700Sroot 2343700Sroot static value_t * 2353700Sroot vlookup(s) 2364963Ssam register char *s; 2373700Sroot { 2383700Sroot register value_t *p; 2393700Sroot 2403700Sroot for (p = vtable; p->v_name; p++) 2413700Sroot if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s))) 2425138Ssam return (p); 2435138Ssam return (NULL); 2443700Sroot } 2453700Sroot 2463700Sroot char * 2473700Sroot vinterp(s, stop) 2484963Ssam register char *s; 2495138Ssam char stop; 2503700Sroot { 2513700Sroot register char *p = s, c; 2523700Sroot int num; 2533700Sroot 2545138Ssam while ((c = *s++) && c != stop) 2555138Ssam switch (c) { 2565138Ssam 2573700Sroot case '^': 2583700Sroot if (*s) 2593700Sroot *p++ = *s++ - 0100; 2603700Sroot else 2613700Sroot *p++ = c; 2623700Sroot break; 2633700Sroot 2643700Sroot case '\\': 2653700Sroot num = 0; 2663700Sroot c = *s++; 2673700Sroot if (c >= '0' && c <= '7') 2683700Sroot num = (num<<3)+(c-'0'); 2693700Sroot else { 2703700Sroot register char *q = "n\nr\rt\tb\bf\f"; 2713700Sroot 2723700Sroot for (; *q; q++) 2733700Sroot if (c == *q++) { 2743700Sroot *p++ = *q; 2753700Sroot goto cont; 2763700Sroot } 2773700Sroot *p++ = c; 2783700Sroot cont: 2793700Sroot break; 2803700Sroot } 2813700Sroot if ((c = *s++) >= '0' && c <= '7') { 2823700Sroot num = (num<<3)+(c-'0'); 2833700Sroot if ((c = *s++) >= '0' && c <= '7') 2843700Sroot num = (num<<3)+(c-'0'); 2853700Sroot else 2863700Sroot s--; 2873700Sroot } else 2883700Sroot s--; 2893700Sroot *p++ = num; 2903700Sroot break; 2913700Sroot 2923700Sroot default: 2933700Sroot *p++ = c; 2945138Ssam } 2953700Sroot *p = '\0'; 2965138Ssam return (c == stop ? s-1 : NULL); 2973700Sroot } 29813140Sralph 29913140Sralph /* 30013140Sralph * assign variable s with value v (for NUMBER or STRING or CHAR types) 30113140Sralph */ 30213140Sralph 30313140Sralph vstring(s,v) 30413140Sralph register char *s; 30513140Sralph register char *v; 30613140Sralph { 30713140Sralph register value_t *p; 30813140Sralph char *expand(); 30913140Sralph 31013280Ssam p = vlookup(s); 31113280Ssam if (p == 0) 31213280Ssam return (1); 31313280Ssam if (p->v_type&NUMBER) 31413280Ssam vassign(p, atoi(v)); 31513280Ssam else { 31613280Ssam if (strcmp(s, "record") == 0) 31713280Ssam v = expand(v); 31813280Ssam vassign(p, v); 31913280Ssam } 32013280Ssam return (0); 32113140Sralph } 322