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