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