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