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