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 #pragma ident "%Z%%M% %I% %E% SMI"
137c478bd9Sstevel@tonic-gate
147c478bd9Sstevel@tonic-gate #include "tip.h"
157c478bd9Sstevel@tonic-gate
167c478bd9Sstevel@tonic-gate #define MIDDLE 35
177c478bd9Sstevel@tonic-gate
18*8d489c7aSmuffin static value_t *vlookup(char *);
197c478bd9Sstevel@tonic-gate static int col = 0;
207c478bd9Sstevel@tonic-gate
21*8d489c7aSmuffin extern char *interp(char *);
22*8d489c7aSmuffin
23*8d489c7aSmuffin static void vtoken(char *);
24*8d489c7aSmuffin static void vprint(value_t *);
25*8d489c7aSmuffin static int vaccess(unsigned, unsigned);
26*8d489c7aSmuffin
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * Variable manipulation
297c478bd9Sstevel@tonic-gate */
30*8d489c7aSmuffin void
vinit(void)31*8d489c7aSmuffin vinit(void)
327c478bd9Sstevel@tonic-gate {
33*8d489c7aSmuffin value_t *p;
34*8d489c7aSmuffin char *cp;
357c478bd9Sstevel@tonic-gate FILE *f;
367c478bd9Sstevel@tonic-gate char file[1024];
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate for (p = vtable; p->v_name != NULL; p++) {
397c478bd9Sstevel@tonic-gate if (p->v_type&ENVIRON)
407c478bd9Sstevel@tonic-gate if (cp = getenv(p->v_name))
417c478bd9Sstevel@tonic-gate p->v_value = cp;
427c478bd9Sstevel@tonic-gate if (p->v_type&IREMOTE)
437c478bd9Sstevel@tonic-gate number(p->v_value) = *address(p->v_value);
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * Read the .tiprc file in the HOME directory
477c478bd9Sstevel@tonic-gate * for sets
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate if ((cp = value(HOME)) == NULL)
507c478bd9Sstevel@tonic-gate cp = "";
51*8d489c7aSmuffin (void) strlcpy(file, cp, sizeof (file));
52*8d489c7aSmuffin (void) strlcat(file, "/.tiprc", sizeof (file));
537c478bd9Sstevel@tonic-gate if ((f = fopen(file, "r")) != NULL) {
54*8d489c7aSmuffin char *tp;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate while (fgets(file, sizeof (file)-1, f) != NULL) {
577c478bd9Sstevel@tonic-gate if (file[0] == '#')
587c478bd9Sstevel@tonic-gate continue;
597c478bd9Sstevel@tonic-gate if (vflag)
60*8d489c7aSmuffin (void) printf("set %s", file);
617c478bd9Sstevel@tonic-gate if (tp = strrchr(file, '\n'))
627c478bd9Sstevel@tonic-gate *tp = '\0';
637c478bd9Sstevel@tonic-gate vlex(file);
647c478bd9Sstevel@tonic-gate }
65*8d489c7aSmuffin (void) fclose(f);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * To allow definition of exception prior to fork
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*VARARGS1*/
74*8d489c7aSmuffin void
vassign(value_t * p,char * v)75*8d489c7aSmuffin vassign(value_t *p, char *v)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate if (!vaccess(p->v_access, WRITE)) {
79*8d489c7aSmuffin (void) printf("access denied\r\n");
807c478bd9Sstevel@tonic-gate return;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate switch (p->v_type&TMASK) {
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate case STRING:
857c478bd9Sstevel@tonic-gate if (p->v_value != (char *)NULL) {
867c478bd9Sstevel@tonic-gate if (equal(p->v_value, v))
877c478bd9Sstevel@tonic-gate return;
887c478bd9Sstevel@tonic-gate if (!(p->v_type&(ENVIRON|INIT)))
897c478bd9Sstevel@tonic-gate free(p->v_value);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate if ((p->v_value = malloc(strlen(v)+1)) == NOSTR) {
92*8d489c7aSmuffin (void) printf("out of core\r\n");
937c478bd9Sstevel@tonic-gate return;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate p->v_type &= ~(ENVIRON|INIT);
96*8d489c7aSmuffin (void) strcpy(p->v_value, v);
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate case NUMBER:
1007c478bd9Sstevel@tonic-gate if (number(p->v_value) == number(v))
1017c478bd9Sstevel@tonic-gate return;
1027c478bd9Sstevel@tonic-gate number(p->v_value) = number(v);
1037c478bd9Sstevel@tonic-gate break;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate case BOOL:
1067c478bd9Sstevel@tonic-gate if (boolean(p->v_value) == (*v != '!'))
1077c478bd9Sstevel@tonic-gate return;
1087c478bd9Sstevel@tonic-gate boolean(p->v_value) = (*v != '!');
1097c478bd9Sstevel@tonic-gate break;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate case CHAR:
1127c478bd9Sstevel@tonic-gate if (character(p->v_value) == *v)
1137c478bd9Sstevel@tonic-gate return;
1147c478bd9Sstevel@tonic-gate character(p->v_value) = *v;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate p->v_access |= CHANGED;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
119*8d489c7aSmuffin void
vlex(char * s)120*8d489c7aSmuffin vlex(char *s)
1217c478bd9Sstevel@tonic-gate {
122*8d489c7aSmuffin value_t *p;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate if (equal(s, "all")) {
1257c478bd9Sstevel@tonic-gate for (p = vtable; p->v_name; p++)
1267c478bd9Sstevel@tonic-gate if (vaccess(p->v_access, READ))
1277c478bd9Sstevel@tonic-gate vprint(p);
1287c478bd9Sstevel@tonic-gate } else {
129*8d489c7aSmuffin char *cp;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate do {
1327c478bd9Sstevel@tonic-gate if (cp = vinterp(s, ' '))
1337c478bd9Sstevel@tonic-gate cp++;
1347c478bd9Sstevel@tonic-gate vtoken(s);
1357c478bd9Sstevel@tonic-gate s = cp;
1367c478bd9Sstevel@tonic-gate } while (s);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate if (col > 0) {
139*8d489c7aSmuffin (void) printf("\r\n");
1407c478bd9Sstevel@tonic-gate col = 0;
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
144*8d489c7aSmuffin static void
vtoken(char * s)145*8d489c7aSmuffin vtoken(char *s)
1467c478bd9Sstevel@tonic-gate {
147*8d489c7aSmuffin value_t *p;
148*8d489c7aSmuffin char *cp, *cp2;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (cp = strchr(s, '=')) {
1517c478bd9Sstevel@tonic-gate *cp = '\0';
1527c478bd9Sstevel@tonic-gate if (p = vlookup(s)) {
1537c478bd9Sstevel@tonic-gate cp++;
1547c478bd9Sstevel@tonic-gate if (p->v_type&NUMBER)
155*8d489c7aSmuffin vassign(p, (char *)atoi(cp));
1567c478bd9Sstevel@tonic-gate else {
1577c478bd9Sstevel@tonic-gate if (strcmp(s, "record") == 0)
1587c478bd9Sstevel@tonic-gate if ((cp2 = expand(cp)) != NOSTR)
1597c478bd9Sstevel@tonic-gate cp = cp2;
1607c478bd9Sstevel@tonic-gate vassign(p, cp);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate return;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate } else if (cp = strchr(s, '?')) {
1657c478bd9Sstevel@tonic-gate *cp = '\0';
166*8d489c7aSmuffin if ((p = vlookup(s)) != NULL && vaccess(p->v_access, READ)) {
1677c478bd9Sstevel@tonic-gate vprint(p);
1687c478bd9Sstevel@tonic-gate return;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate } else {
1717c478bd9Sstevel@tonic-gate if (*s != '!')
1727c478bd9Sstevel@tonic-gate p = vlookup(s);
1737c478bd9Sstevel@tonic-gate else
1747c478bd9Sstevel@tonic-gate p = vlookup(s+1);
1757c478bd9Sstevel@tonic-gate if (p != NOVAL) {
1767c478bd9Sstevel@tonic-gate if (p->v_type&BOOL)
1777c478bd9Sstevel@tonic-gate vassign(p, s);
1787c478bd9Sstevel@tonic-gate else
179*8d489c7aSmuffin (void) printf("%s: no value specified\r\n", s);
1807c478bd9Sstevel@tonic-gate return;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
183*8d489c7aSmuffin (void) printf("%s: unknown variable\r\n", s);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
186*8d489c7aSmuffin static void
vprint(value_t * p)187*8d489c7aSmuffin vprint(value_t *p)
1887c478bd9Sstevel@tonic-gate {
189*8d489c7aSmuffin char *cp;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate if (col > 0 && col < MIDDLE)
1927c478bd9Sstevel@tonic-gate while (col++ < MIDDLE)
193*8d489c7aSmuffin (void) putchar(' ');
1947c478bd9Sstevel@tonic-gate col += strlen(p->v_name);
1957c478bd9Sstevel@tonic-gate switch (p->v_type&TMASK) {
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate case BOOL:
1987c478bd9Sstevel@tonic-gate if (boolean(p->v_value) == FALSE) {
1997c478bd9Sstevel@tonic-gate col++;
200*8d489c7aSmuffin (void) putchar('!');
2017c478bd9Sstevel@tonic-gate }
202*8d489c7aSmuffin (void) printf("%s", p->v_name);
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate case STRING:
206*8d489c7aSmuffin (void) printf("%s=", p->v_name);
2077c478bd9Sstevel@tonic-gate col++;
2087c478bd9Sstevel@tonic-gate if (p->v_value) {
209*8d489c7aSmuffin cp = interp(p->v_value);
2107c478bd9Sstevel@tonic-gate col += strlen(cp);
211*8d489c7aSmuffin (void) printf("%s", cp);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate break;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate case NUMBER:
2167c478bd9Sstevel@tonic-gate col += 6;
217*8d489c7aSmuffin (void) printf("%s=%-5d", p->v_name, number(p->v_value));
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate case CHAR:
221*8d489c7aSmuffin (void) printf("%s=", p->v_name);
2227c478bd9Sstevel@tonic-gate col++;
2237c478bd9Sstevel@tonic-gate if (p->v_value) {
2247c478bd9Sstevel@tonic-gate cp = ctrl(character(p->v_value));
2257c478bd9Sstevel@tonic-gate col += strlen(cp);
226*8d489c7aSmuffin (void) printf("%s", cp);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate if (col >= MIDDLE) {
2317c478bd9Sstevel@tonic-gate col = 0;
232*8d489c7aSmuffin (void) printf("\r\n");
2337c478bd9Sstevel@tonic-gate return;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate static int
vaccess(unsigned mode,unsigned rw)239*8d489c7aSmuffin vaccess(unsigned mode, unsigned rw)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate if (mode & (rw<<PUBLIC))
2427c478bd9Sstevel@tonic-gate return (1);
2437c478bd9Sstevel@tonic-gate if (mode & (rw<<PRIVATE))
2447c478bd9Sstevel@tonic-gate return (1);
2457c478bd9Sstevel@tonic-gate return ((mode & (rw<<ROOT)) && uid == 0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate static value_t *
vlookup(char * s)249*8d489c7aSmuffin vlookup(char *s)
2507c478bd9Sstevel@tonic-gate {
251*8d489c7aSmuffin value_t *p;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate for (p = vtable; p->v_name; p++)
2547c478bd9Sstevel@tonic-gate if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
2557c478bd9Sstevel@tonic-gate return (p);
2567c478bd9Sstevel@tonic-gate return (NULL);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate char *
vinterp(char * s,char stop)260*8d489c7aSmuffin vinterp(char *s, char stop)
2617c478bd9Sstevel@tonic-gate {
262*8d489c7aSmuffin char *p = s, c;
2637c478bd9Sstevel@tonic-gate int num;
2647c478bd9Sstevel@tonic-gate
265*8d489c7aSmuffin while ((c = *s++) != 0 && c != stop)
2667c478bd9Sstevel@tonic-gate switch (c) {
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate case '^':
2697c478bd9Sstevel@tonic-gate if (*s)
2707c478bd9Sstevel@tonic-gate *p++ = *s++ - 0100;
2717c478bd9Sstevel@tonic-gate else
2727c478bd9Sstevel@tonic-gate *p++ = c;
2737c478bd9Sstevel@tonic-gate break;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate case '\\':
2767c478bd9Sstevel@tonic-gate num = 0;
2777c478bd9Sstevel@tonic-gate c = *s++;
2787c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '7')
2797c478bd9Sstevel@tonic-gate num = (num<<3)+(c-'0');
2807c478bd9Sstevel@tonic-gate else {
281*8d489c7aSmuffin char *q = "n\nr\rt\tb\bf\f";
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate for (; *q; q++)
2847c478bd9Sstevel@tonic-gate if (c == *q++) {
2857c478bd9Sstevel@tonic-gate *p++ = *q;
2867c478bd9Sstevel@tonic-gate goto cont;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate *p++ = c;
2897c478bd9Sstevel@tonic-gate cont:
2907c478bd9Sstevel@tonic-gate break;
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate if ((c = *s++) >= '0' && c <= '7') {
2937c478bd9Sstevel@tonic-gate num = (num<<3)+(c-'0');
2947c478bd9Sstevel@tonic-gate if ((c = *s++) >= '0' && c <= '7')
2957c478bd9Sstevel@tonic-gate num = (num<<3)+(c-'0');
2967c478bd9Sstevel@tonic-gate else
2977c478bd9Sstevel@tonic-gate s--;
2987c478bd9Sstevel@tonic-gate } else
2997c478bd9Sstevel@tonic-gate s--;
3007c478bd9Sstevel@tonic-gate *p++ = num;
3017c478bd9Sstevel@tonic-gate break;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate default:
3047c478bd9Sstevel@tonic-gate *p++ = c;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate *p = '\0';
3077c478bd9Sstevel@tonic-gate return (c == stop ? s-1 : NULL);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate * assign variable s with value v (for NUMBER or STRING or CHAR types)
3127c478bd9Sstevel@tonic-gate */
313*8d489c7aSmuffin int
vstring(char * s,char * v)314*8d489c7aSmuffin vstring(char *s, char *v)
3157c478bd9Sstevel@tonic-gate {
316*8d489c7aSmuffin value_t *p;
3177c478bd9Sstevel@tonic-gate char *v2;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate p = vlookup(s);
3207c478bd9Sstevel@tonic-gate if (p == 0)
3217c478bd9Sstevel@tonic-gate return (1);
3227c478bd9Sstevel@tonic-gate if (p->v_type&NUMBER)
323*8d489c7aSmuffin vassign(p, (char *)atoi(v));
3247c478bd9Sstevel@tonic-gate else {
3257c478bd9Sstevel@tonic-gate if (strcmp(s, "record") == 0)
3267c478bd9Sstevel@tonic-gate if ((v2 = expand(v)) != NOSTR)
3277c478bd9Sstevel@tonic-gate v = v2;
3287c478bd9Sstevel@tonic-gate vassign(p, v);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate return (0);
3317c478bd9Sstevel@tonic-gate }
332