tip(1): Fix some amd64 warnings.
[dragonfly.git] / usr.bin / tip / tip / value.c
blobb8073633f28808d595238951ac994e69f01a3b96
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)value.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/tip/tip/value.c,v 1.5.2.1 2000/07/01 12:24:23 ps Exp $
35 * $DragonFly: src/usr.bin/tip/tip/value.c,v 1.5 2005/05/07 23:20:43 corecode Exp $
38 #include "tip.h"
40 #define MIDDLE 35
42 static value_t *vlookup();
43 int vstring(char *, char *);
44 void vlex(char *);
45 void vassign(value_t *, char *);
47 static int col = 0;
50 * Variable manipulation
52 void
53 vinit()
55 register value_t *p;
56 register char *cp;
57 FILE *f;
58 char file[256];
60 for (p = vtable; p->v_name != NULL; p++) {
61 if (p->v_type&ENVIRON)
62 if ((cp = getenv(p->v_name)))
63 p->v_value = cp;
64 if (p->v_type&IREMOTE)
65 if (p->v_type&STRING)
66 p->v_value = *(char **) address(p->v_value);
67 else
68 number(p->v_value) = *address(p->v_value);
71 * Read the .tiprc file in the HOME directory
72 * for sets
74 strcpy(file, value(HOME));
75 strcat(file, "/.tiprc");
76 if ((f = fopen(file, "r")) != NULL) {
77 register char *tp;
79 while (fgets(file, sizeof(file)-1, f) != NULL) {
80 if (vflag)
81 printf("set %s", file);
82 if ((tp = rindex(file, '\n')))
83 *tp = '\0';
84 vlex(file);
86 fclose(f);
89 * To allow definition of exception prior to fork
91 vtable[EXCEPTIONS].v_access &= ~(WRITE<<PUBLIC);
94 static int vaccess();
96 /*VARARGS1*/
97 void
98 vassign(p, v)
99 register value_t *p;
100 char *v;
103 if (!vaccess(p->v_access, WRITE)) {
104 printf("access denied\r\n");
105 return;
107 switch (p->v_type&TMASK) {
109 case STRING:
110 if (p->v_value && equal(p->v_value, v))
111 return;
112 if (!(p->v_type&(ENVIRON|INIT)))
113 free(p->v_value);
114 if ((p->v_value = malloc(size(v)+1)) == NULL) {
115 printf("out of core\r\n");
116 return;
118 p->v_type &= ~(ENVIRON|INIT);
119 strcpy(p->v_value, v);
120 break;
122 case NUMBER:
123 if (number(p->v_value) == number(v))
124 return;
125 number(p->v_value) = number(v);
126 break;
128 case BOOL:
129 if (boolean(p->v_value) == (*v != '!'))
130 return;
131 boolean(p->v_value) = (*v != '!');
132 break;
134 case CHAR:
135 if (character(p->v_value) == *v)
136 return;
137 character(p->v_value) = *v;
139 p->v_access |= CHANGED;
142 static void vprint();
143 static void vtoken(char *);
145 void
146 vlex(s)
147 register char *s;
149 register value_t *p;
151 if (equal(s, "all")) {
152 for (p = vtable; p->v_name; p++)
153 if (vaccess(p->v_access, READ))
154 vprint(p);
155 } else {
156 register char *cp;
158 do {
159 if ((cp = vinterp(s, ' ')))
160 cp++;
161 vtoken(s);
162 s = cp;
163 } while (s);
165 if (col > 0) {
166 printf("\r\n");
167 col = 0;
171 static void
172 vtoken(s)
173 register char *s;
175 register value_t *p;
176 register char *cp;
177 char *expand();
179 if ((cp = index(s, '='))) {
180 *cp = '\0';
181 if ((p = vlookup(s))) {
182 cp++;
183 if (p->v_type&NUMBER)
184 vassign(p, (char *)(intptr_t)atoi(cp));
185 else {
186 if (strcmp(s, "record") == 0)
187 cp = expand(cp);
188 vassign(p, cp);
190 return;
192 } else if ((cp = index(s, '?'))) {
193 *cp = '\0';
194 if ((p = vlookup(s)) && vaccess(p->v_access, READ)) {
195 vprint(p);
196 return;
198 } else {
199 if (*s != '!')
200 p = vlookup(s);
201 else
202 p = vlookup(s+1);
203 if (p != NULL) {
204 vassign(p, s);
205 return;
208 printf("%s: unknown variable\r\n", s);
211 static void
212 vprint(p)
213 register value_t *p;
215 register char *cp;
216 extern char *interp(), *ctrl();
218 if (col > 0 && col < MIDDLE)
219 while (col++ < MIDDLE)
220 putchar(' ');
221 col += size(p->v_name);
222 switch (p->v_type&TMASK) {
224 case BOOL:
225 if (boolean(p->v_value) == FALSE) {
226 col++;
227 putchar('!');
229 printf("%s", p->v_name);
230 break;
232 case STRING:
233 printf("%s=", p->v_name);
234 col++;
235 if (p->v_value) {
236 cp = interp(p->v_value, NULL);
237 col += size(cp);
238 printf("%s", cp);
240 break;
242 case NUMBER:
243 col += 6;
244 printf("%s=%-5d", p->v_name, number(p->v_value));
245 break;
247 case CHAR:
248 printf("%s=", p->v_name);
249 col++;
250 if (p->v_value) {
251 cp = ctrl(character(p->v_value));
252 col += size(cp);
253 printf("%s", cp);
255 break;
257 if (col >= MIDDLE) {
258 col = 0;
259 printf("\r\n");
260 return;
265 static int
266 vaccess(mode, rw)
267 register unsigned mode, rw;
269 if (mode & (rw<<PUBLIC))
270 return (1);
271 if (mode & (rw<<PRIVATE))
272 return (1);
273 return ((mode & (rw<<ROOT)) && getuid() == 0);
276 static value_t *
277 vlookup(s)
278 register char *s;
280 register value_t *p;
282 for (p = vtable; p->v_name; p++)
283 if (equal(p->v_name, s) || (p->v_abrev && equal(p->v_abrev, s)))
284 return (p);
285 return (NULL);
288 char *
289 vinterp(s, stop)
290 register char *s;
291 char stop;
293 register char *p = s, c;
294 int num;
296 while ((c = *s++) && c != stop)
297 switch (c) {
299 case '^':
300 if (*s)
301 *p++ = *s++ - 0100;
302 else
303 *p++ = c;
304 break;
306 case '\\':
307 num = 0;
308 c = *s++;
309 if (c >= '0' && c <= '7')
310 num = (num<<3)+(c-'0');
311 else {
312 register char *q = "n\nr\rt\tb\bf\f";
314 for (; *q; q++)
315 if (c == *q++) {
316 *p++ = *q;
317 goto cont;
319 *p++ = c;
320 cont:
321 break;
323 if ((c = *s++) >= '0' && c <= '7') {
324 num = (num<<3)+(c-'0');
325 if ((c = *s++) >= '0' && c <= '7')
326 num = (num<<3)+(c-'0');
327 else
328 s--;
329 } else
330 s--;
331 *p++ = num;
332 break;
334 default:
335 *p++ = c;
337 *p = '\0';
338 return (c == stop ? s-1 : NULL);
342 * assign variable s with value v (for NUMBER or STRING or CHAR types)
346 vstring(s,v)
347 register char *s;
348 register char *v;
350 register value_t *p;
351 char *expand();
353 p = vlookup(s);
354 if (p == 0)
355 return (1);
356 if (p->v_type&NUMBER)
357 vassign(p, (char *)(intptr_t)atoi(v));
358 else {
359 if (strcmp(s, "record") == 0)
360 v = expand(v);
361 vassign(p, v);
363 return (0);