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