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