set.c revision 1.16 1 1.16 wiz /* $NetBSD: set.c,v 1.16 2001/09/14 14:04:01 wiz Exp $ */
2 1.7 cgd
3 1.1 cgd /*-
4 1.5 mycroft * Copyright (c) 1980, 1991, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.10 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.7 cgd #if 0
39 1.7 cgd static char sccsid[] = "@(#)set.c 8.1 (Berkeley) 5/31/93";
40 1.7 cgd #else
41 1.16 wiz __RCSID("$NetBSD: set.c,v 1.16 2001/09/14 14:04:01 wiz Exp $");
42 1.7 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.16 wiz
47 1.1 cgd #include <stdlib.h>
48 1.16 wiz
49 1.5 mycroft #ifndef SHORT_STRINGS
50 1.5 mycroft #include <string.h>
51 1.5 mycroft #endif /* SHORT_STRINGS */
52 1.16 wiz
53 1.1 cgd #if __STDC__
54 1.1 cgd # include <stdarg.h>
55 1.1 cgd #else
56 1.1 cgd # include <varargs.h>
57 1.1 cgd #endif
58 1.1 cgd
59 1.1 cgd #include "csh.h"
60 1.1 cgd #include "extern.h"
61 1.1 cgd
62 1.16 wiz static Char *getinx(Char *, int *);
63 1.16 wiz static void asx(Char *, int, Char *);
64 1.16 wiz static struct varent *getvx(Char *, int);
65 1.16 wiz static Char *xset(Char *, Char ***);
66 1.16 wiz static Char *operate(int, Char *, Char *);
67 1.16 wiz static void putn1(int);
68 1.16 wiz static struct varent *madrof(Char *, struct varent *);
69 1.16 wiz static void unsetv1(struct varent *);
70 1.16 wiz static void exportpath(Char **);
71 1.16 wiz static void balance(struct varent *, int, int);
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * C Shell
75 1.1 cgd */
76 1.1 cgd
77 1.1 cgd void
78 1.5 mycroft /*ARGSUSED*/
79 1.16 wiz doset(Char **v, struct command *t)
80 1.16 wiz {
81 1.16 wiz Char op, *p, **vecp, *vp;
82 1.16 wiz int subscr;
83 1.16 wiz bool hadsub;
84 1.1 cgd
85 1.1 cgd v++;
86 1.1 cgd p = *v++;
87 1.1 cgd if (p == 0) {
88 1.1 cgd prvars();
89 1.1 cgd return;
90 1.1 cgd }
91 1.1 cgd do {
92 1.1 cgd hadsub = 0;
93 1.1 cgd vp = p;
94 1.1 cgd if (letter(*p))
95 1.1 cgd for (; alnum(*p); p++)
96 1.1 cgd continue;
97 1.13 mycroft if (vp == p || !letter(*vp))
98 1.1 cgd stderror(ERR_NAME | ERR_VARBEGIN);
99 1.13 mycroft if ((p - vp) > MAXVARLEN)
100 1.1 cgd stderror(ERR_NAME | ERR_VARTOOLONG);
101 1.1 cgd if (*p == '[') {
102 1.1 cgd hadsub++;
103 1.1 cgd p = getinx(p, &subscr);
104 1.1 cgd }
105 1.5 mycroft if ((op = *p) != '\0') {
106 1.1 cgd *p++ = 0;
107 1.1 cgd if (*p == 0 && *v && **v == '(')
108 1.1 cgd p = *v++;
109 1.1 cgd }
110 1.1 cgd else if (*v && eq(*v, STRequal)) {
111 1.1 cgd op = '=', v++;
112 1.1 cgd if (*v)
113 1.1 cgd p = *v++;
114 1.1 cgd }
115 1.13 mycroft if (op && op != '=')
116 1.1 cgd stderror(ERR_NAME | ERR_SYNTAX);
117 1.1 cgd if (eq(p, STRLparen)) {
118 1.9 tls Char **e = v;
119 1.1 cgd
120 1.13 mycroft if (hadsub)
121 1.1 cgd stderror(ERR_NAME | ERR_SYNTAX);
122 1.1 cgd for (;;) {
123 1.13 mycroft if (!*e)
124 1.1 cgd stderror(ERR_NAME | ERR_MISSING, ')');
125 1.1 cgd if (**e == ')')
126 1.1 cgd break;
127 1.1 cgd e++;
128 1.1 cgd }
129 1.1 cgd p = *e;
130 1.1 cgd *e = 0;
131 1.1 cgd vecp = saveblk(v);
132 1.1 cgd set1(vp, vecp, &shvhed);
133 1.1 cgd *e = p;
134 1.1 cgd v = e + 1;
135 1.1 cgd }
136 1.1 cgd else if (hadsub)
137 1.1 cgd asx(vp, subscr, Strsave(p));
138 1.1 cgd else
139 1.1 cgd set(vp, Strsave(p));
140 1.1 cgd if (eq(vp, STRpath)) {
141 1.1 cgd exportpath(adrof(STRpath)->vec);
142 1.5 mycroft dohash(NULL, NULL);
143 1.1 cgd }
144 1.1 cgd else if (eq(vp, STRhistchars)) {
145 1.9 tls Char *pn = value(STRhistchars);
146 1.1 cgd
147 1.1 cgd HIST = *pn++;
148 1.1 cgd HISTSUB = *pn;
149 1.1 cgd }
150 1.1 cgd else if (eq(vp, STRuser)) {
151 1.1 cgd Setenv(STRUSER, value(vp));
152 1.1 cgd Setenv(STRLOGNAME, value(vp));
153 1.1 cgd }
154 1.1 cgd else if (eq(vp, STRwordchars)) {
155 1.1 cgd word_chars = value(vp);
156 1.1 cgd }
157 1.1 cgd else if (eq(vp, STRterm))
158 1.1 cgd Setenv(STRTERM, value(vp));
159 1.1 cgd else if (eq(vp, STRhome)) {
160 1.9 tls Char *cp;
161 1.1 cgd
162 1.1 cgd cp = Strsave(value(vp)); /* get the old value back */
163 1.1 cgd
164 1.1 cgd /*
165 1.1 cgd * convert to cononical pathname (possibly resolving symlinks)
166 1.1 cgd */
167 1.1 cgd cp = dcanon(cp, cp);
168 1.1 cgd
169 1.1 cgd set(vp, Strsave(cp)); /* have to save the new val */
170 1.1 cgd
171 1.1 cgd /* and now mirror home with HOME */
172 1.1 cgd Setenv(STRHOME, cp);
173 1.1 cgd /* fix directory stack for new tilde home */
174 1.1 cgd dtilde();
175 1.16 wiz xfree((ptr_t)cp);
176 1.1 cgd }
177 1.1 cgd #ifdef FILEC
178 1.1 cgd else if (eq(vp, STRfilec))
179 1.1 cgd filec = 1;
180 1.1 cgd #endif
181 1.5 mycroft } while ((p = *v++) != NULL);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd static Char *
185 1.16 wiz getinx(Char *cp, int *ip)
186 1.1 cgd {
187 1.1 cgd *ip = 0;
188 1.1 cgd *cp++ = 0;
189 1.1 cgd while (*cp && Isdigit(*cp))
190 1.1 cgd *ip = *ip * 10 + *cp++ - '0';
191 1.13 mycroft if (*cp++ != ']')
192 1.1 cgd stderror(ERR_NAME | ERR_SUBSCRIPT);
193 1.1 cgd return (cp);
194 1.1 cgd }
195 1.1 cgd
196 1.1 cgd static void
197 1.16 wiz asx(Char *vp, int subscr, Char *p)
198 1.1 cgd {
199 1.16 wiz struct varent *v;
200 1.1 cgd
201 1.16 wiz v = getvx(vp, subscr);
202 1.1 cgd xfree((ptr_t) v->vec[subscr - 1]);
203 1.1 cgd v->vec[subscr - 1] = globone(p, G_APPEND);
204 1.1 cgd }
205 1.1 cgd
206 1.1 cgd static struct varent *
207 1.16 wiz getvx(Char *vp, int subscr)
208 1.1 cgd {
209 1.16 wiz struct varent *v;
210 1.1 cgd
211 1.16 wiz v = adrof(vp);
212 1.1 cgd if (v == 0)
213 1.1 cgd udvar(vp);
214 1.13 mycroft if (subscr < 1 || subscr > blklen(v->vec))
215 1.1 cgd stderror(ERR_NAME | ERR_RANGE);
216 1.1 cgd return (v);
217 1.1 cgd }
218 1.1 cgd
219 1.1 cgd void
220 1.5 mycroft /*ARGSUSED*/
221 1.16 wiz dolet(Char **v, struct command *t)
222 1.16 wiz {
223 1.16 wiz Char c, op, *p, *vp;
224 1.16 wiz int subscr;
225 1.16 wiz bool hadsub;
226 1.1 cgd
227 1.1 cgd v++;
228 1.1 cgd p = *v++;
229 1.1 cgd if (p == 0) {
230 1.1 cgd prvars();
231 1.1 cgd return;
232 1.1 cgd }
233 1.1 cgd do {
234 1.1 cgd hadsub = 0;
235 1.1 cgd vp = p;
236 1.1 cgd if (letter(*p))
237 1.1 cgd for (; alnum(*p); p++)
238 1.1 cgd continue;
239 1.13 mycroft if (vp == p || !letter(*vp))
240 1.1 cgd stderror(ERR_NAME | ERR_VARBEGIN);
241 1.13 mycroft if ((p - vp) > MAXVARLEN)
242 1.1 cgd stderror(ERR_NAME | ERR_VARTOOLONG);
243 1.1 cgd if (*p == '[') {
244 1.1 cgd hadsub++;
245 1.1 cgd p = getinx(p, &subscr);
246 1.1 cgd }
247 1.1 cgd if (*p == 0 && *v)
248 1.1 cgd p = *v++;
249 1.5 mycroft if ((op = *p) != '\0')
250 1.1 cgd *p++ = 0;
251 1.13 mycroft else
252 1.1 cgd stderror(ERR_NAME | ERR_ASSIGN);
253 1.1 cgd
254 1.13 mycroft if (*p == '\0' && *v == NULL)
255 1.1 cgd stderror(ERR_NAME | ERR_ASSIGN);
256 1.1 cgd
257 1.1 cgd vp = Strsave(vp);
258 1.1 cgd if (op == '=') {
259 1.1 cgd c = '=';
260 1.1 cgd p = xset(p, &v);
261 1.1 cgd }
262 1.1 cgd else {
263 1.1 cgd c = *p++;
264 1.1 cgd if (any("+-", c)) {
265 1.13 mycroft if (c != op || *p)
266 1.1 cgd stderror(ERR_NAME | ERR_UNKNOWNOP);
267 1.1 cgd p = Strsave(STR1);
268 1.1 cgd }
269 1.1 cgd else {
270 1.1 cgd if (any("<>", op)) {
271 1.13 mycroft if (c != op)
272 1.1 cgd stderror(ERR_NAME | ERR_UNKNOWNOP);
273 1.1 cgd c = *p++;
274 1.1 cgd stderror(ERR_NAME | ERR_SYNTAX);
275 1.1 cgd }
276 1.1 cgd if (c != '=')
277 1.1 cgd stderror(ERR_NAME | ERR_UNKNOWNOP);
278 1.1 cgd p = xset(p, &v);
279 1.1 cgd }
280 1.1 cgd }
281 1.14 christos if (op == '=') {
282 1.1 cgd if (hadsub)
283 1.1 cgd asx(vp, subscr, p);
284 1.1 cgd else
285 1.1 cgd set(vp, p);
286 1.14 christos } else if (hadsub) {
287 1.1 cgd struct varent *gv = getvx(vp, subscr);
288 1.1 cgd
289 1.1 cgd asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
290 1.1 cgd }
291 1.1 cgd else
292 1.1 cgd set(vp, operate(op, value(vp), p));
293 1.1 cgd if (eq(vp, STRpath)) {
294 1.1 cgd exportpath(adrof(STRpath)->vec);
295 1.5 mycroft dohash(NULL, NULL);
296 1.1 cgd }
297 1.1 cgd xfree((ptr_t) vp);
298 1.1 cgd if (c != '=')
299 1.1 cgd xfree((ptr_t) p);
300 1.5 mycroft } while ((p = *v++) != NULL);
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd static Char *
304 1.16 wiz xset(Char *cp, Char ***vp)
305 1.1 cgd {
306 1.9 tls Char *dp;
307 1.1 cgd
308 1.1 cgd if (*cp) {
309 1.1 cgd dp = Strsave(cp);
310 1.1 cgd --(*vp);
311 1.1 cgd xfree((ptr_t) ** vp);
312 1.1 cgd **vp = dp;
313 1.1 cgd }
314 1.5 mycroft return (putn(expr(vp)));
315 1.1 cgd }
316 1.1 cgd
317 1.1 cgd static Char *
318 1.16 wiz operate(int op, Char *vp, Char *p)
319 1.16 wiz {
320 1.16 wiz Char opr[2], **v, *vec[5], **vecp;
321 1.9 tls int i;
322 1.1 cgd
323 1.16 wiz v = vec;
324 1.16 wiz vecp = v;
325 1.1 cgd if (op != '=') {
326 1.1 cgd if (*vp)
327 1.1 cgd *v++ = vp;
328 1.1 cgd opr[0] = op;
329 1.1 cgd opr[1] = 0;
330 1.1 cgd *v++ = opr;
331 1.1 cgd if (op == '<' || op == '>')
332 1.1 cgd *v++ = opr;
333 1.1 cgd }
334 1.1 cgd *v++ = p;
335 1.1 cgd *v++ = 0;
336 1.5 mycroft i = expr(&vecp);
337 1.13 mycroft if (*vecp)
338 1.1 cgd stderror(ERR_NAME | ERR_EXPRESSION);
339 1.1 cgd return (putn(i));
340 1.1 cgd }
341 1.1 cgd
342 1.1 cgd static Char *putp;
343 1.1 cgd
344 1.16 wiz Char *
345 1.16 wiz putn(int n)
346 1.1 cgd {
347 1.1 cgd static Char number[15];
348 1.16 wiz int num;
349 1.1 cgd
350 1.1 cgd putp = number;
351 1.1 cgd if (n < 0) {
352 1.1 cgd n = -n;
353 1.1 cgd *putp++ = '-';
354 1.1 cgd }
355 1.1 cgd num = 2; /* confuse lint */
356 1.5 mycroft if (sizeof(int) == num && ((unsigned int) n) == 0x8000) {
357 1.1 cgd *putp++ = '3';
358 1.1 cgd n = 2768;
359 1.1 cgd #ifdef pdp11
360 1.1 cgd }
361 1.1 cgd #else
362 1.1 cgd }
363 1.1 cgd else {
364 1.1 cgd num = 4; /* confuse lint */
365 1.5 mycroft if (sizeof(int) == num && ((unsigned int) n) == 0x80000000) {
366 1.1 cgd *putp++ = '2';
367 1.1 cgd n = 147483648;
368 1.1 cgd }
369 1.1 cgd }
370 1.1 cgd #endif
371 1.1 cgd putn1(n);
372 1.1 cgd *putp = 0;
373 1.1 cgd return (Strsave(number));
374 1.1 cgd }
375 1.1 cgd
376 1.1 cgd static void
377 1.16 wiz putn1(int n)
378 1.1 cgd {
379 1.1 cgd if (n > 9)
380 1.1 cgd putn1(n / 10);
381 1.1 cgd *putp++ = n % 10 + '0';
382 1.1 cgd }
383 1.1 cgd
384 1.1 cgd int
385 1.16 wiz getn(Char *cp)
386 1.1 cgd {
387 1.16 wiz int n, sign;
388 1.1 cgd
389 1.1 cgd sign = 0;
390 1.1 cgd if (cp[0] == '+' && cp[1])
391 1.1 cgd cp++;
392 1.1 cgd if (*cp == '-') {
393 1.1 cgd sign++;
394 1.1 cgd cp++;
395 1.13 mycroft if (!Isdigit(*cp))
396 1.1 cgd stderror(ERR_NAME | ERR_BADNUM);
397 1.1 cgd }
398 1.1 cgd n = 0;
399 1.1 cgd while (Isdigit(*cp))
400 1.1 cgd n = n * 10 + *cp++ - '0';
401 1.13 mycroft if (*cp)
402 1.1 cgd stderror(ERR_NAME | ERR_BADNUM);
403 1.1 cgd return (sign ? -n : n);
404 1.1 cgd }
405 1.1 cgd
406 1.16 wiz Char *
407 1.16 wiz value1(Char *var, struct varent *head)
408 1.1 cgd {
409 1.9 tls struct varent *vp;
410 1.1 cgd
411 1.1 cgd vp = adrof1(var, head);
412 1.1 cgd return (vp == 0 || vp->vec[0] == 0 ? STRNULL : vp->vec[0]);
413 1.1 cgd }
414 1.1 cgd
415 1.1 cgd static struct varent *
416 1.16 wiz madrof(Char *pat, struct varent *vp)
417 1.1 cgd {
418 1.9 tls struct varent *vp1;
419 1.1 cgd
420 1.1 cgd for (; vp; vp = vp->v_right) {
421 1.1 cgd if (vp->v_left && (vp1 = madrof(pat, vp->v_left)))
422 1.1 cgd return vp1;
423 1.1 cgd if (Gmatch(vp->v_name, pat))
424 1.1 cgd return vp;
425 1.1 cgd }
426 1.1 cgd return vp;
427 1.1 cgd }
428 1.1 cgd
429 1.1 cgd struct varent *
430 1.16 wiz adrof1(Char *name, struct varent *v)
431 1.1 cgd {
432 1.9 tls int cmp;
433 1.1 cgd
434 1.1 cgd v = v->v_left;
435 1.1 cgd while (v && ((cmp = *name - *v->v_name) ||
436 1.1 cgd (cmp = Strcmp(name, v->v_name))))
437 1.1 cgd if (cmp < 0)
438 1.1 cgd v = v->v_left;
439 1.1 cgd else
440 1.1 cgd v = v->v_right;
441 1.1 cgd return v;
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * The caller is responsible for putting value in a safe place
446 1.1 cgd */
447 1.1 cgd void
448 1.16 wiz set(Char *var, Char *val)
449 1.1 cgd {
450 1.16 wiz Char **vec;
451 1.1 cgd
452 1.16 wiz vec = (Char **)xmalloc((size_t)(2 * sizeof(Char **)));
453 1.1 cgd vec[0] = val;
454 1.1 cgd vec[1] = 0;
455 1.1 cgd set1(var, vec, &shvhed);
456 1.1 cgd }
457 1.1 cgd
458 1.1 cgd void
459 1.16 wiz set1(Char *var, Char **vec, struct varent *head)
460 1.1 cgd {
461 1.16 wiz Char **oldv;
462 1.1 cgd
463 1.16 wiz oldv = vec;
464 1.1 cgd gflag = 0;
465 1.1 cgd tglob(oldv);
466 1.1 cgd if (gflag) {
467 1.1 cgd vec = globall(oldv);
468 1.1 cgd if (vec == 0) {
469 1.1 cgd blkfree(oldv);
470 1.1 cgd stderror(ERR_NAME | ERR_NOMATCH);
471 1.1 cgd }
472 1.1 cgd blkfree(oldv);
473 1.1 cgd gargv = 0;
474 1.1 cgd }
475 1.1 cgd setq(var, vec, head);
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd void
479 1.16 wiz setq(Char *name, Char **vec, struct varent *p)
480 1.1 cgd {
481 1.9 tls struct varent *c;
482 1.9 tls int f;
483 1.1 cgd
484 1.1 cgd f = 0; /* tree hangs off the header's left link */
485 1.5 mycroft while ((c = p->v_link[f]) != NULL) {
486 1.1 cgd if ((f = *name - *c->v_name) == 0 &&
487 1.1 cgd (f = Strcmp(name, c->v_name)) == 0) {
488 1.1 cgd blkfree(c->vec);
489 1.1 cgd goto found;
490 1.1 cgd }
491 1.1 cgd p = c;
492 1.1 cgd f = f > 0;
493 1.1 cgd }
494 1.16 wiz p->v_link[f] = c = (struct varent *)xmalloc((size_t)sizeof(struct varent));
495 1.1 cgd c->v_name = Strsave(name);
496 1.1 cgd c->v_bal = 0;
497 1.1 cgd c->v_left = c->v_right = 0;
498 1.1 cgd c->v_parent = p;
499 1.1 cgd balance(p, f, 0);
500 1.1 cgd found:
501 1.1 cgd trim(c->vec = vec);
502 1.1 cgd }
503 1.1 cgd
504 1.1 cgd void
505 1.5 mycroft /*ARGSUSED*/
506 1.16 wiz unset(Char **v, struct command *t)
507 1.1 cgd {
508 1.1 cgd unset1(v, &shvhed);
509 1.1 cgd #ifdef FILEC
510 1.1 cgd if (adrof(STRfilec) == 0)
511 1.1 cgd filec = 0;
512 1.1 cgd #endif
513 1.1 cgd if (adrof(STRhistchars) == 0) {
514 1.1 cgd HIST = '!';
515 1.1 cgd HISTSUB = '^';
516 1.1 cgd }
517 1.1 cgd if (adrof(STRwordchars) == 0)
518 1.1 cgd word_chars = STR_WORD_CHARS;
519 1.1 cgd }
520 1.1 cgd
521 1.1 cgd void
522 1.16 wiz unset1(Char *v[], struct varent *head)
523 1.1 cgd {
524 1.9 tls struct varent *vp;
525 1.9 tls int cnt;
526 1.1 cgd
527 1.1 cgd while (*++v) {
528 1.1 cgd cnt = 0;
529 1.5 mycroft while ((vp = madrof(*v, head->v_left)) != NULL)
530 1.1 cgd unsetv1(vp), cnt++;
531 1.1 cgd if (cnt == 0)
532 1.5 mycroft setname(vis_str(*v));
533 1.1 cgd }
534 1.1 cgd }
535 1.1 cgd
536 1.1 cgd void
537 1.16 wiz unsetv(Char *var)
538 1.1 cgd {
539 1.9 tls struct varent *vp;
540 1.1 cgd
541 1.1 cgd if ((vp = adrof1(var, &shvhed)) == 0)
542 1.1 cgd udvar(var);
543 1.1 cgd unsetv1(vp);
544 1.1 cgd }
545 1.1 cgd
546 1.1 cgd static void
547 1.16 wiz unsetv1(struct varent *p)
548 1.1 cgd {
549 1.9 tls struct varent *c, *pp;
550 1.9 tls int f;
551 1.1 cgd
552 1.1 cgd /*
553 1.1 cgd * Free associated memory first to avoid complications.
554 1.1 cgd */
555 1.1 cgd blkfree(p->vec);
556 1.1 cgd xfree((ptr_t) p->v_name);
557 1.1 cgd /*
558 1.1 cgd * If p is missing one child, then we can move the other into where p is.
559 1.1 cgd * Otherwise, we find the predecessor of p, which is guaranteed to have no
560 1.1 cgd * right child, copy it into p, and move it's left child into it.
561 1.1 cgd */
562 1.1 cgd if (p->v_right == 0)
563 1.1 cgd c = p->v_left;
564 1.1 cgd else if (p->v_left == 0)
565 1.1 cgd c = p->v_right;
566 1.1 cgd else {
567 1.5 mycroft for (c = p->v_left; c->v_right; c = c->v_right)
568 1.5 mycroft continue;
569 1.1 cgd p->v_name = c->v_name;
570 1.1 cgd p->vec = c->vec;
571 1.1 cgd p = c;
572 1.1 cgd c = p->v_left;
573 1.1 cgd }
574 1.1 cgd /*
575 1.1 cgd * Move c into where p is.
576 1.1 cgd */
577 1.1 cgd pp = p->v_parent;
578 1.1 cgd f = pp->v_right == p;
579 1.5 mycroft if ((pp->v_link[f] = c) != NULL)
580 1.1 cgd c->v_parent = pp;
581 1.1 cgd /*
582 1.1 cgd * Free the deleted node, and rebalance.
583 1.1 cgd */
584 1.1 cgd xfree((ptr_t) p);
585 1.1 cgd balance(pp, f, 1);
586 1.1 cgd }
587 1.1 cgd
588 1.1 cgd void
589 1.16 wiz setNS(Char *cp)
590 1.1 cgd {
591 1.1 cgd set(cp, Strsave(STRNULL));
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd void
595 1.5 mycroft /*ARGSUSED*/
596 1.16 wiz shift(Char **v, struct command *t)
597 1.1 cgd {
598 1.9 tls struct varent *argv;
599 1.9 tls Char *name;
600 1.1 cgd
601 1.1 cgd v++;
602 1.1 cgd name = *v;
603 1.1 cgd if (name == 0)
604 1.1 cgd name = STRargv;
605 1.1 cgd else
606 1.1 cgd (void) strip(name);
607 1.1 cgd argv = adrof(name);
608 1.1 cgd if (argv == 0)
609 1.1 cgd udvar(name);
610 1.13 mycroft if (argv->vec[0] == 0)
611 1.1 cgd stderror(ERR_NAME | ERR_NOMORE);
612 1.1 cgd lshift(argv->vec, 1);
613 1.1 cgd }
614 1.1 cgd
615 1.1 cgd static void
616 1.16 wiz exportpath(Char **val)
617 1.1 cgd {
618 1.16 wiz Char exppath[BUFSIZE];
619 1.1 cgd
620 1.1 cgd exppath[0] = 0;
621 1.1 cgd if (val)
622 1.1 cgd while (*val) {
623 1.15 christos if (Strlen(*val) + Strlen(exppath) + 2 > BUFSIZE) {
624 1.16 wiz (void)fprintf(csherr,
625 1.5 mycroft "Warning: ridiculously long PATH truncated\n");
626 1.1 cgd break;
627 1.1 cgd }
628 1.16 wiz (void)Strcat(exppath, *val++);
629 1.1 cgd if (*val == 0 || eq(*val, STRRparen))
630 1.1 cgd break;
631 1.16 wiz (void)Strcat(exppath, STRcolon);
632 1.1 cgd }
633 1.1 cgd Setenv(STRPATH, exppath);
634 1.1 cgd }
635 1.1 cgd
636 1.1 cgd #ifndef lint
637 1.1 cgd /*
638 1.1 cgd * Lint thinks these have null effect
639 1.1 cgd */
640 1.1 cgd /* macros to do single rotations on node p */
641 1.1 cgd #define rright(p) (\
642 1.1 cgd t = (p)->v_left,\
643 1.1 cgd (t)->v_parent = (p)->v_parent,\
644 1.1 cgd ((p)->v_left = t->v_right) ? (t->v_right->v_parent = (p)) : 0,\
645 1.1 cgd (t->v_right = (p))->v_parent = t,\
646 1.1 cgd (p) = t)
647 1.1 cgd #define rleft(p) (\
648 1.1 cgd t = (p)->v_right,\
649 1.1 cgd (t)->v_parent = (p)->v_parent,\
650 1.1 cgd ((p)->v_right = t->v_left) ? (t->v_left->v_parent = (p)) : 0,\
651 1.1 cgd (t->v_left = (p))->v_parent = t,\
652 1.1 cgd (p) = t)
653 1.1 cgd #else
654 1.1 cgd struct varent *
655 1.16 wiz rleft(struct varent *p)
656 1.1 cgd {
657 1.1 cgd return (p);
658 1.1 cgd }
659 1.1 cgd struct varent *
660 1.16 wiz rright(struct varent *p)
661 1.1 cgd {
662 1.1 cgd return (p);
663 1.1 cgd }
664 1.1 cgd #endif /* ! lint */
665 1.1 cgd
666 1.1 cgd
667 1.1 cgd /*
668 1.1 cgd * Rebalance a tree, starting at p and up.
669 1.1 cgd * F == 0 means we've come from p's left child.
670 1.1 cgd * D == 1 means we've just done a delete, otherwise an insert.
671 1.1 cgd */
672 1.1 cgd static void
673 1.16 wiz balance(struct varent *p, int f, int d)
674 1.1 cgd {
675 1.9 tls struct varent *pp;
676 1.1 cgd
677 1.1 cgd #ifndef lint
678 1.9 tls struct varent *t; /* used by the rotate macros */
679 1.1 cgd
680 1.1 cgd #endif
681 1.9 tls int ff;
682 1.1 cgd
683 1.1 cgd /*
684 1.1 cgd * Ok, from here on, p is the node we're operating on; pp is it's parent; f
685 1.1 cgd * is the branch of p from which we have come; ff is the branch of pp which
686 1.1 cgd * is p.
687 1.1 cgd */
688 1.5 mycroft for (; (pp = p->v_parent) != NULL; p = pp, f = ff) {
689 1.1 cgd ff = pp->v_right == p;
690 1.1 cgd if (f ^ d) { /* right heavy */
691 1.1 cgd switch (p->v_bal) {
692 1.1 cgd case -1: /* was left heavy */
693 1.1 cgd p->v_bal = 0;
694 1.1 cgd break;
695 1.1 cgd case 0: /* was balanced */
696 1.1 cgd p->v_bal = 1;
697 1.1 cgd break;
698 1.1 cgd case 1: /* was already right heavy */
699 1.1 cgd switch (p->v_right->v_bal) {
700 1.1 cgd case 1: /* sigle rotate */
701 1.1 cgd pp->v_link[ff] = rleft(p);
702 1.1 cgd p->v_left->v_bal = 0;
703 1.1 cgd p->v_bal = 0;
704 1.1 cgd break;
705 1.1 cgd case 0: /* single rotate */
706 1.1 cgd pp->v_link[ff] = rleft(p);
707 1.1 cgd p->v_left->v_bal = 1;
708 1.1 cgd p->v_bal = -1;
709 1.1 cgd break;
710 1.1 cgd case -1: /* double rotate */
711 1.1 cgd (void) rright(p->v_right);
712 1.1 cgd pp->v_link[ff] = rleft(p);
713 1.1 cgd p->v_left->v_bal =
714 1.1 cgd p->v_bal < 1 ? 0 : -1;
715 1.1 cgd p->v_right->v_bal =
716 1.1 cgd p->v_bal > -1 ? 0 : 1;
717 1.1 cgd p->v_bal = 0;
718 1.1 cgd break;
719 1.1 cgd }
720 1.1 cgd break;
721 1.1 cgd }
722 1.1 cgd }
723 1.1 cgd else { /* left heavy */
724 1.1 cgd switch (p->v_bal) {
725 1.1 cgd case 1: /* was right heavy */
726 1.1 cgd p->v_bal = 0;
727 1.1 cgd break;
728 1.1 cgd case 0: /* was balanced */
729 1.1 cgd p->v_bal = -1;
730 1.1 cgd break;
731 1.1 cgd case -1: /* was already left heavy */
732 1.1 cgd switch (p->v_left->v_bal) {
733 1.1 cgd case -1: /* single rotate */
734 1.1 cgd pp->v_link[ff] = rright(p);
735 1.1 cgd p->v_right->v_bal = 0;
736 1.1 cgd p->v_bal = 0;
737 1.1 cgd break;
738 1.1 cgd case 0: /* signle rotate */
739 1.1 cgd pp->v_link[ff] = rright(p);
740 1.1 cgd p->v_right->v_bal = -1;
741 1.1 cgd p->v_bal = 1;
742 1.1 cgd break;
743 1.1 cgd case 1: /* double rotate */
744 1.1 cgd (void) rleft(p->v_left);
745 1.1 cgd pp->v_link[ff] = rright(p);
746 1.1 cgd p->v_left->v_bal =
747 1.1 cgd p->v_bal < 1 ? 0 : -1;
748 1.1 cgd p->v_right->v_bal =
749 1.1 cgd p->v_bal > -1 ? 0 : 1;
750 1.1 cgd p->v_bal = 0;
751 1.1 cgd break;
752 1.1 cgd }
753 1.1 cgd break;
754 1.1 cgd }
755 1.1 cgd }
756 1.1 cgd /*
757 1.1 cgd * If from insert, then we terminate when p is balanced. If from
758 1.1 cgd * delete, then we terminate when p is unbalanced.
759 1.1 cgd */
760 1.1 cgd if ((p->v_bal == 0) ^ d)
761 1.1 cgd break;
762 1.1 cgd }
763 1.1 cgd }
764 1.1 cgd
765 1.1 cgd void
766 1.16 wiz plist(struct varent *p)
767 1.1 cgd {
768 1.9 tls struct varent *c;
769 1.16 wiz sigset_t sigset;
770 1.9 tls int len;
771 1.1 cgd
772 1.8 mycroft if (setintr) {
773 1.8 mycroft sigemptyset(&sigset);
774 1.16 wiz (void)sigaddset(&sigset, SIGINT);
775 1.16 wiz (void)sigprocmask(SIG_UNBLOCK, &sigset, NULL);
776 1.8 mycroft }
777 1.1 cgd
778 1.1 cgd for (;;) {
779 1.1 cgd while (p->v_left)
780 1.1 cgd p = p->v_left;
781 1.1 cgd x:
782 1.1 cgd if (p->v_parent == 0) /* is it the header? */
783 1.1 cgd return;
784 1.1 cgd len = blklen(p->vec);
785 1.16 wiz (void)fprintf(cshout, "%s\t", short2str(p->v_name));
786 1.1 cgd if (len != 1)
787 1.16 wiz (void)fputc('(', cshout);
788 1.5 mycroft blkpr(cshout, p->vec);
789 1.1 cgd if (len != 1)
790 1.16 wiz (void)fputc(')', cshout);
791 1.16 wiz (void)fputc('\n', cshout);
792 1.1 cgd if (p->v_right) {
793 1.1 cgd p = p->v_right;
794 1.1 cgd continue;
795 1.1 cgd }
796 1.1 cgd do {
797 1.1 cgd c = p;
798 1.1 cgd p = p->v_parent;
799 1.1 cgd } while (p->v_right == c);
800 1.1 cgd goto x;
801 1.1 cgd }
802 1.1 cgd }
803