1*5138Ssam /* value.c 4.3 81/11/29 */ 23700Sroot #include "tip.h" 33700Sroot 43700Sroot #define MIDDLE 35 53700Sroot 63700Sroot static value_t *vlookup(); 73700Sroot static int col = 0; 83700Sroot 93700Sroot /* 103700Sroot * Variable manipulation 113700Sroot */ 123700Sroot vinit() 133700Sroot { 143700Sroot register value_t *p; 153700Sroot register char *cp; 163700Sroot FILE *f; 173700Sroot char file[256]; 183700Sroot 193700Sroot for (p = vtable; p->v_name != NULL; p++) { 203700Sroot if (p->v_type&ENVIRON) 213700Sroot if (cp = getenv(p->v_name)) 223700Sroot p->v_value = cp; 233700Sroot if (p->v_type&IREMOTE) 243700Sroot number(p->v_value) = *address(p->v_value); 253700Sroot } 263700Sroot /* 273700Sroot * Read the .tiprc file in the HOME directory 283700Sroot * for sets 293700Sroot */ 303700Sroot strcpy(file, value(HOME)); 313700Sroot strcat(file, "/.tiprc"); 323700Sroot if ((f = fopen(file, "r")) != NULL) { 333700Sroot register char *tp; 343700Sroot 353700Sroot while (fgets(file, sizeof(file)-1, f) != NULL) { 363700Sroot if (vflag) 373700Sroot printf("set %s", file); 383700Sroot if (tp = rindex(file, '\n')) 393700Sroot *tp = '\0'; 403700Sroot vlex(file); 413700Sroot } 423700Sroot fclose(f); 433700Sroot } 443700Sroot /* 453700Sroot * To allow definition of exception prior to fork 463700Sroot */ 473700Sroot vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC); 483700Sroot } 493700Sroot 503700Sroot /*VARARGS1*/ 513700Sroot vassign(p, v) 524963Ssam register value_t *p; 534963Ssam char *v; 543700Sroot { 553700Sroot 563700Sroot if (!vaccess(p->v_access, WRITE)) { 573700Sroot printf("access denied\r\n"); 583700Sroot return; 593700Sroot } 60*5138Ssam switch (p->v_type&TMASK) { 613700Sroot 62*5138Ssam case STRING: 63*5138Ssam if (equal(p->v_value, v)) 64*5138Ssam return; 65*5138Ssam if (!(p->v_type&(ENVIRON|INIT))) 66*5138Ssam free(p->v_value); 67*5138Ssam if ((p->v_value = malloc(size(v)+1)) == NOSTR) { 68*5138Ssam printf("out of core\r\n"); 69*5138Ssam return; 70*5138Ssam } 71*5138Ssam p->v_type &= ~(ENVIRON|INIT); 72*5138Ssam strcpy(p->v_value, v); 73*5138Ssam break; 743700Sroot 75*5138Ssam case NUMBER: 76*5138Ssam if (number(p->v_value) == number(v)) 77*5138Ssam return; 78*5138Ssam number(p->v_value) = number(v); 79*5138Ssam break; 803700Sroot 81*5138Ssam case BOOL: 82*5138Ssam if (boolean(p->v_value) == (*v != '!')) 83*5138Ssam return; 84*5138Ssam boolean(p->v_value) = (*v != '!'); 85*5138Ssam break; 863700Sroot 87*5138Ssam case CHAR: 88*5138Ssam if (character(p->v_value) == *v) 89*5138Ssam return; 90*5138Ssam character(p->v_value) = *v; 913700Sroot } 923700Sroot p->v_access |= CHANGED; 933700Sroot } 943700Sroot 953700Sroot vlex(s) 964963Ssam register char *s; 973700Sroot { 983700Sroot register value_t *p; 993700Sroot 1003700Sroot if (equal(s, "all")) { 1013700Sroot for (p = vtable; p->v_name; p++) 1023700Sroot if (vaccess(p->v_access, READ)) 1033700Sroot vprint(p); 1043700Sroot } else { 1053700Sroot register char *cp; 1063700Sroot 1073700Sroot do { 1083700Sroot if (cp = vinterp(s, ' ')) 1093700Sroot cp++; 1103700Sroot vtoken(s); 1113700Sroot s = cp; 1123700Sroot } while (s); 1133700Sroot } 1143700Sroot if (col > 0) { 1153700Sroot printf("\r\n"); 1163700Sroot col = 0; 1173700Sroot } 1183700Sroot } 1193700Sroot 1203700Sroot static int 1213700Sroot vtoken(s) 1224963Ssam register char *s; 1233700Sroot { 1243700Sroot register value_t *p; 1253700Sroot register char *cp; 1263700Sroot 1273700Sroot if (cp = index(s, '=')) { 1283700Sroot *cp = '\0'; 1293700Sroot if (p = vlookup(s)) { 1303700Sroot cp++; 1313700Sroot if (p->v_type&NUMBER) 1323700Sroot vassign(p, atoi(cp)); 1333700Sroot else 1343700Sroot vassign(p, cp); 1353700Sroot return; 1363700Sroot } 1373700Sroot } else if (cp = index(s, '?')) { 1383700Sroot *cp = '\0'; 1393700Sroot if ((p = vlookup(s)) && vaccess(p->v_access, READ)) { 1403700Sroot vprint(p); 1413700Sroot return; 1423700Sroot } 1433700Sroot } else { 1443700Sroot if (*s != '!') 1453700Sroot p = vlookup(s); 1463700Sroot else 1473700Sroot p = vlookup(s+1); 1483700Sroot if (p != NOVAL) { 1493700Sroot vassign(p, s); 1503700Sroot return; 1513700Sroot } 1523700Sroot } 1533700Sroot printf("%s: unknown variable\r\n", s); 1543700Sroot } 1553700Sroot 1563700Sroot static int 1573700Sroot vprint(p) 1584963Ssam register value_t *p; 1593700Sroot { 1603700Sroot register char *cp; 1613700Sroot extern char *interp(), *ctrl(); 1623700Sroot 1633700Sroot if (col > 0 && col < MIDDLE) 1643700Sroot while (col++ < MIDDLE) 1653700Sroot putchar(' '); 1663700Sroot col += size(p->v_name); 167*5138Ssam switch (p->v_type&TMASK) { 168*5138Ssam 169*5138Ssam case BOOL: 170*5138Ssam if (boolean(p->v_value) == FALSE) { 1713700Sroot col++; 172*5138Ssam putchar('!'); 173*5138Ssam } 174*5138Ssam printf("%s", p->v_name); 175*5138Ssam break; 176*5138Ssam 177*5138Ssam case STRING: 178*5138Ssam printf("%s=", p->v_name); 179*5138Ssam col++; 180*5138Ssam if (p->v_value) { 181*5138Ssam cp = interp(p->v_value); 182*5138Ssam col += size(cp); 183*5138Ssam printf("%s", cp); 184*5138Ssam } 185*5138Ssam break; 186*5138Ssam 187*5138Ssam case NUMBER: 188*5138Ssam col += 6; 189*5138Ssam printf("%s=%-5d", p->v_name, number(p->v_value)); 190*5138Ssam break; 191*5138Ssam 192*5138Ssam case CHAR: 193*5138Ssam printf("%s=", p->v_name); 194*5138Ssam col++; 195*5138Ssam if (p->v_value) { 196*5138Ssam cp = ctrl(character(p->v_value)); 197*5138Ssam col += size(cp); 198*5138Ssam printf("%s", cp); 199*5138Ssam } 200*5138Ssam break; 2013700Sroot } 2023700Sroot if (col >= MIDDLE) { 2033700Sroot col = 0; 2043700Sroot printf("\r\n"); 2053700Sroot return; 2063700Sroot } 2073700Sroot } 2083700Sroot 2093700Sroot 2103700Sroot static int 2113700Sroot vaccess(mode, rw) 2124963Ssam register unsigned mode, rw; 2133700Sroot { 2143700Sroot if (mode & (rw<<PUBLIC)) 215*5138Ssam return (1); 2163700Sroot if (mode & (rw<<PRIVATE)) 217*5138Ssam return (1); 218*5138Ssam return ((mode & (rw<<ROOT)) && getuid() == 0); 2193700Sroot } 2203700Sroot 2213700Sroot static value_t * 2223700Sroot vlookup(s) 2234963Ssam register char *s; 2243700Sroot { 2253700Sroot register value_t *p; 2263700Sroot 2273700Sroot for (p = vtable; p->v_name; p++) 2283700Sroot if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s))) 229*5138Ssam return (p); 230*5138Ssam return (NULL); 2313700Sroot } 2323700Sroot 2333700Sroot char * 2343700Sroot vinterp(s, stop) 2354963Ssam register char *s; 236*5138Ssam char stop; 2373700Sroot { 2383700Sroot register char *p = s, c; 2393700Sroot int num; 2403700Sroot 241*5138Ssam while ((c = *s++) && c != stop) 242*5138Ssam switch (c) { 243*5138Ssam 2443700Sroot case '^': 2453700Sroot if (*s) 2463700Sroot *p++ = *s++ - 0100; 2473700Sroot else 2483700Sroot *p++ = c; 2493700Sroot break; 2503700Sroot 2513700Sroot case '\\': 2523700Sroot num = 0; 2533700Sroot c = *s++; 2543700Sroot if (c >= '0' && c <= '7') 2553700Sroot num = (num<<3)+(c-'0'); 2563700Sroot else { 2573700Sroot register char *q = "n\nr\rt\tb\bf\f"; 2583700Sroot 2593700Sroot for (; *q; q++) 2603700Sroot if (c == *q++) { 2613700Sroot *p++ = *q; 2623700Sroot goto cont; 2633700Sroot } 2643700Sroot *p++ = c; 2653700Sroot cont: 2663700Sroot break; 2673700Sroot } 2683700Sroot if ((c = *s++) >= '0' && c <= '7') { 2693700Sroot num = (num<<3)+(c-'0'); 2703700Sroot if ((c = *s++) >= '0' && c <= '7') 2713700Sroot num = (num<<3)+(c-'0'); 2723700Sroot else 2733700Sroot s--; 2743700Sroot } else 2753700Sroot s--; 2763700Sroot *p++ = num; 2773700Sroot break; 2783700Sroot 2793700Sroot default: 2803700Sroot *p++ = c; 281*5138Ssam } 2823700Sroot *p = '\0'; 283*5138Ssam return (c == stop ? s-1 : NULL); 2843700Sroot } 285