Home | History | Annotate | Line # | Download | only in sh
var.c revision 1.52
      1  1.52       kre /*	$NetBSD: var.c,v 1.52 2017/05/10 06:18:43 kre Exp $	*/
      2  1.12       cgd 
      3   1.1       cgd /*-
      4   1.5       jtc  * Copyright (c) 1991, 1993
      5   1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.33       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.12       cgd #if 0
     38  1.13  christos static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
     39  1.12       cgd #else
     40  1.52       kre __RCSID("$NetBSD: var.c,v 1.52 2017/05/10 06:18:43 kre Exp $");
     41  1.12       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44  1.13  christos #include <unistd.h>
     45  1.13  christos #include <stdlib.h>
     46  1.42  christos #include <string.h>
     47  1.21      fair #include <paths.h>
     48  1.41  christos #include <limits.h>
     49  1.13  christos 
     50   1.1       cgd /*
     51   1.1       cgd  * Shell variables.
     52   1.1       cgd  */
     53   1.1       cgd 
     54   1.1       cgd #include "shell.h"
     55   1.1       cgd #include "output.h"
     56   1.1       cgd #include "expand.h"
     57   1.1       cgd #include "nodes.h"	/* for other headers */
     58   1.1       cgd #include "eval.h"	/* defines cmdenviron */
     59   1.1       cgd #include "exec.h"
     60   1.1       cgd #include "syntax.h"
     61   1.1       cgd #include "options.h"
     62  1.40  christos #include "builtins.h"
     63   1.1       cgd #include "mail.h"
     64   1.1       cgd #include "var.h"
     65   1.1       cgd #include "memalloc.h"
     66   1.1       cgd #include "error.h"
     67   1.1       cgd #include "mystring.h"
     68  1.15  christos #include "parser.h"
     69  1.32       dsl #include "show.h"
     70  1.17  christos #ifndef SMALL
     71  1.13  christos #include "myhistedit.h"
     72  1.13  christos #endif
     73   1.1       cgd 
     74  1.35       dsl #ifdef SMALL
     75   1.1       cgd #define VTABSIZE 39
     76  1.35       dsl #else
     77  1.35       dsl #define VTABSIZE 517
     78  1.35       dsl #endif
     79   1.1       cgd 
     80   1.1       cgd 
     81   1.1       cgd struct varinit {
     82   1.1       cgd 	struct var *var;
     83   1.1       cgd 	int flags;
     84  1.23  christos 	const char *text;
     85  1.30  christos 	void (*func)(const char *);
     86   1.1       cgd };
     87   1.1       cgd 
     88  1.39  dholland struct localvar *localvars;
     89   1.1       cgd 
     90  1.17  christos #ifndef SMALL
     91   1.5       jtc struct var vhistsize;
     92  1.18  christos struct var vterm;
     93   1.7       cgd #endif
     94   1.1       cgd struct var vifs;
     95   1.1       cgd struct var vmail;
     96   1.1       cgd struct var vmpath;
     97   1.1       cgd struct var vpath;
     98   1.1       cgd struct var vps1;
     99   1.1       cgd struct var vps2;
    100  1.32       dsl struct var vps4;
    101  1.49  christos struct var vvers;
    102  1.14  christos struct var voptind;
    103   1.1       cgd 
    104  1.48  christos char ifs_default[] = " \t\n";
    105  1.48  christos 
    106   1.1       cgd const struct varinit varinit[] = {
    107  1.17  christos #ifndef SMALL
    108  1.14  christos 	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
    109  1.14  christos 	  sethistsize },
    110   1.7       cgd #endif
    111  1.14  christos 	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
    112  1.14  christos 	  NULL },
    113  1.14  christos 	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
    114  1.14  christos 	  NULL },
    115  1.14  christos 	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
    116  1.14  christos 	  NULL },
    117  1.49  christos 	{ &vvers,	VSTRFIXED|VTEXTFIXED|VNOEXPORT, "NETBSD_SHELL=",
    118  1.49  christos 	  NULL },
    119  1.26       cgd 	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
    120  1.14  christos 	  changepath },
    121  1.15  christos 	/*
    122   1.1       cgd 	 * vps1 depends on uid
    123   1.1       cgd 	 */
    124  1.14  christos 	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
    125  1.14  christos 	  NULL },
    126  1.32       dsl 	{ &vps4,	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",
    127  1.32       dsl 	  NULL },
    128  1.18  christos #ifndef SMALL
    129  1.14  christos 	{ &vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM=",
    130  1.18  christos 	  setterm },
    131   1.1       cgd #endif
    132  1.32       dsl 	{ &voptind,	VSTRFIXED|VTEXTFIXED|VNOFUNC,	"OPTIND=1",
    133  1.14  christos 	  getoptsreset },
    134  1.14  christos 	{ NULL,	0,				NULL,
    135  1.14  christos 	  NULL }
    136   1.1       cgd };
    137   1.1       cgd 
    138   1.1       cgd struct var *vartab[VTABSIZE];
    139   1.1       cgd 
    140  1.35       dsl STATIC int strequal(const char *, const char *);
    141  1.35       dsl STATIC struct var *find_var(const char *, struct var ***, int *);
    142   1.1       cgd 
    143   1.1       cgd /*
    144   1.1       cgd  * Initialize the varable symbol tables and import the environment
    145   1.1       cgd  */
    146   1.1       cgd 
    147   1.1       cgd #ifdef mkinit
    148  1.47  christos INCLUDE <stdio.h>
    149  1.47  christos INCLUDE <unistd.h>
    150   1.1       cgd INCLUDE "var.h"
    151  1.49  christos INCLUDE "version.h"
    152  1.27  christos MKINIT char **environ;
    153   1.1       cgd INIT {
    154   1.1       cgd 	char **envp;
    155  1.47  christos 	char buf[64];
    156   1.1       cgd 
    157   1.1       cgd 	initvar();
    158   1.1       cgd 	for (envp = environ ; *envp ; envp++) {
    159   1.1       cgd 		if (strchr(*envp, '=')) {
    160   1.1       cgd 			setvareq(*envp, VEXPORT|VTEXTFIXED);
    161   1.1       cgd 		}
    162   1.1       cgd 	}
    163  1.47  christos 
    164  1.47  christos 	/*
    165  1.47  christos 	 * PPID is readonly
    166  1.47  christos 	 *	set after processing environ to override anything there
    167  1.48  christos 	 * Always default IFS, ignore any value from environment.
    168  1.47  christos 	 */
    169  1.47  christos 	snprintf(buf, sizeof(buf), "%d", (int)getppid());
    170  1.47  christos 	setvar("PPID", buf, VREADONLY);
    171  1.48  christos 	setvar("IFS", ifs_default, VTEXTFIXED);
    172  1.49  christos 	setvar("NETBSD_SHELL", NETBSD_SHELL, VTEXTFIXED|VREADONLY|VNOEXPORT);
    173   1.1       cgd }
    174   1.1       cgd #endif
    175   1.1       cgd 
    176   1.1       cgd 
    177   1.1       cgd /*
    178   1.1       cgd  * This routine initializes the builtin variables.  It is called when the
    179   1.1       cgd  * shell is initialized and again when a shell procedure is spawned.
    180   1.1       cgd  */
    181   1.1       cgd 
    182   1.1       cgd void
    183  1.30  christos initvar(void)
    184  1.30  christos {
    185   1.1       cgd 	const struct varinit *ip;
    186   1.1       cgd 	struct var *vp;
    187   1.1       cgd 	struct var **vpp;
    188   1.1       cgd 
    189   1.1       cgd 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
    190  1.35       dsl 		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
    191  1.35       dsl 			continue;
    192  1.35       dsl 		vp->next = *vpp;
    193  1.35       dsl 		*vpp = vp;
    194  1.35       dsl 		vp->text = strdup(ip->text);
    195  1.35       dsl 		vp->flags = ip->flags;
    196  1.35       dsl 		vp->func = ip->func;
    197   1.1       cgd 	}
    198   1.1       cgd 	/*
    199   1.1       cgd 	 * PS1 depends on uid
    200   1.1       cgd 	 */
    201  1.35       dsl 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
    202   1.1       cgd 		vps1.next = *vpp;
    203   1.1       cgd 		*vpp = &vps1;
    204   1.1       cgd 		vps1.flags = VSTRFIXED|VTEXTFIXED;
    205  1.44  christos 		vps1.text = NULL;
    206  1.44  christos 		choose_ps1();
    207   1.1       cgd 	}
    208   1.1       cgd }
    209   1.1       cgd 
    210  1.44  christos void
    211  1.44  christos choose_ps1(void)
    212  1.44  christos {
    213  1.44  christos 	free(vps1.text);
    214  1.44  christos 	vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
    215  1.44  christos }
    216  1.44  christos 
    217   1.1       cgd /*
    218  1.14  christos  * Safe version of setvar, returns 1 on success 0 on failure.
    219  1.14  christos  */
    220  1.14  christos 
    221  1.14  christos int
    222  1.30  christos setvarsafe(const char *name, const char *val, int flags)
    223  1.14  christos {
    224  1.14  christos 	struct jmploc jmploc;
    225  1.14  christos 	struct jmploc *volatile savehandler = handler;
    226  1.38  christos 	int volatile err = 0;
    227  1.14  christos 
    228  1.14  christos 	if (setjmp(jmploc.loc))
    229  1.14  christos 		err = 1;
    230  1.14  christos 	else {
    231  1.14  christos 		handler = &jmploc;
    232  1.14  christos 		setvar(name, val, flags);
    233  1.14  christos 	}
    234  1.14  christos 	handler = savehandler;
    235  1.14  christos 	return err;
    236  1.14  christos }
    237  1.14  christos 
    238  1.14  christos /*
    239   1.1       cgd  * Set the value of a variable.  The flags argument is ored with the
    240   1.1       cgd  * flags of the variable.  If val is NULL, the variable is unset.
    241   1.1       cgd  */
    242   1.1       cgd 
    243   1.1       cgd void
    244  1.30  christos setvar(const char *name, const char *val, int flags)
    245  1.10       cgd {
    246  1.23  christos 	const char *p;
    247  1.23  christos 	const char *q;
    248  1.23  christos 	char *d;
    249   1.1       cgd 	int len;
    250   1.1       cgd 	int namelen;
    251   1.1       cgd 	char *nameeq;
    252   1.1       cgd 	int isbad;
    253   1.1       cgd 
    254   1.1       cgd 	isbad = 0;
    255   1.1       cgd 	p = name;
    256  1.15  christos 	if (! is_name(*p))
    257   1.1       cgd 		isbad = 1;
    258  1.15  christos 	p++;
    259   1.1       cgd 	for (;;) {
    260   1.1       cgd 		if (! is_in_name(*p)) {
    261   1.1       cgd 			if (*p == '\0' || *p == '=')
    262   1.1       cgd 				break;
    263   1.1       cgd 			isbad = 1;
    264   1.1       cgd 		}
    265   1.1       cgd 		p++;
    266   1.1       cgd 	}
    267   1.1       cgd 	namelen = p - name;
    268   1.1       cgd 	if (isbad)
    269   1.5       jtc 		error("%.*s: bad variable name", namelen, name);
    270   1.1       cgd 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
    271   1.1       cgd 	if (val == NULL) {
    272   1.1       cgd 		flags |= VUNSET;
    273   1.1       cgd 	} else {
    274   1.1       cgd 		len += strlen(val);
    275   1.1       cgd 	}
    276  1.23  christos 	d = nameeq = ckmalloc(len);
    277   1.1       cgd 	q = name;
    278   1.1       cgd 	while (--namelen >= 0)
    279  1.23  christos 		*d++ = *q++;
    280  1.23  christos 	*d++ = '=';
    281  1.23  christos 	*d = '\0';
    282   1.1       cgd 	if (val)
    283  1.23  christos 		scopy(val, d);
    284   1.1       cgd 	setvareq(nameeq, flags);
    285   1.1       cgd }
    286   1.1       cgd 
    287   1.1       cgd 
    288   1.1       cgd 
    289   1.1       cgd /*
    290   1.1       cgd  * Same as setvar except that the variable and value are passed in
    291   1.1       cgd  * the first argument as name=value.  Since the first argument will
    292   1.1       cgd  * be actually stored in the table, it should not be a string that
    293   1.1       cgd  * will go away.
    294   1.1       cgd  */
    295   1.1       cgd 
    296   1.1       cgd void
    297  1.30  christos setvareq(char *s, int flags)
    298  1.10       cgd {
    299   1.1       cgd 	struct var *vp, **vpp;
    300  1.35       dsl 	int nlen;
    301   1.1       cgd 
    302  1.49  christos 	if (aflag && !(flags & VNOEXPORT))
    303  1.28     bjh21 		flags |= VEXPORT;
    304  1.35       dsl 	vp = find_var(s, &vpp, &nlen);
    305  1.35       dsl 	if (vp != NULL) {
    306  1.35       dsl 		if (vp->flags & VREADONLY)
    307  1.35       dsl 			error("%.*s: is read only", vp->name_len, s);
    308  1.35       dsl 		if (flags & VNOSET)
    309  1.35       dsl 			return;
    310  1.35       dsl 		INTOFF;
    311  1.14  christos 
    312  1.35       dsl 		if (vp->func && (flags & VNOFUNC) == 0)
    313  1.35       dsl 			(*vp->func)(s + vp->name_len + 1);
    314  1.14  christos 
    315  1.35       dsl 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
    316  1.35       dsl 			ckfree(vp->text);
    317  1.14  christos 
    318  1.35       dsl 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
    319  1.49  christos 		if (flags & VNOEXPORT)
    320  1.49  christos 			vp->flags &= ~VEXPORT;
    321  1.35       dsl 		vp->flags |= flags & ~VNOFUNC;
    322  1.35       dsl 		vp->text = s;
    323  1.35       dsl 
    324  1.35       dsl 		/*
    325  1.35       dsl 		 * We could roll this to a function, to handle it as
    326  1.35       dsl 		 * a regular variable function callback, but why bother?
    327  1.35       dsl 		 */
    328  1.35       dsl 		if (vp == &vmpath || (vp == &vmail && ! mpathset()))
    329  1.35       dsl 			chkmail(1);
    330  1.35       dsl 		INTON;
    331  1.35       dsl 		return;
    332   1.1       cgd 	}
    333   1.1       cgd 	/* not found */
    334  1.30  christos 	if (flags & VNOSET)
    335  1.30  christos 		return;
    336   1.1       cgd 	vp = ckmalloc(sizeof (*vp));
    337  1.32       dsl 	vp->flags = flags & ~VNOFUNC;
    338   1.1       cgd 	vp->text = s;
    339  1.35       dsl 	vp->name_len = nlen;
    340   1.1       cgd 	vp->next = *vpp;
    341  1.14  christos 	vp->func = NULL;
    342   1.1       cgd 	*vpp = vp;
    343   1.1       cgd }
    344   1.1       cgd 
    345   1.1       cgd 
    346   1.1       cgd 
    347   1.1       cgd /*
    348   1.1       cgd  * Process a linked list of variable assignments.
    349   1.1       cgd  */
    350   1.1       cgd 
    351   1.1       cgd void
    352  1.30  christos listsetvar(struct strlist *list, int flags)
    353  1.30  christos {
    354   1.1       cgd 	struct strlist *lp;
    355   1.1       cgd 
    356   1.1       cgd 	INTOFF;
    357   1.1       cgd 	for (lp = list ; lp ; lp = lp->next) {
    358  1.30  christos 		setvareq(savestr(lp->text), flags);
    359   1.1       cgd 	}
    360   1.1       cgd 	INTON;
    361   1.1       cgd }
    362   1.1       cgd 
    363  1.32       dsl void
    364  1.32       dsl listmklocal(struct strlist *list, int flags)
    365  1.32       dsl {
    366  1.32       dsl 	struct strlist *lp;
    367  1.32       dsl 
    368  1.32       dsl 	for (lp = list ; lp ; lp = lp->next)
    369  1.32       dsl 		mklocal(lp->text, flags);
    370  1.32       dsl }
    371   1.1       cgd 
    372   1.1       cgd 
    373   1.1       cgd /*
    374   1.1       cgd  * Find the value of a variable.  Returns NULL if not set.
    375   1.1       cgd  */
    376   1.1       cgd 
    377   1.1       cgd char *
    378  1.30  christos lookupvar(const char *name)
    379  1.30  christos {
    380   1.1       cgd 	struct var *v;
    381   1.1       cgd 
    382  1.35       dsl 	v = find_var(name, NULL, NULL);
    383  1.35       dsl 	if (v == NULL || v->flags & VUNSET)
    384  1.35       dsl 		return NULL;
    385  1.35       dsl 	return v->text + v->name_len + 1;
    386   1.1       cgd }
    387   1.1       cgd 
    388   1.1       cgd 
    389   1.1       cgd 
    390   1.1       cgd /*
    391   1.1       cgd  * Search the environment of a builtin command.  If the second argument
    392   1.1       cgd  * is nonzero, return the value of a variable even if it hasn't been
    393   1.1       cgd  * exported.
    394   1.1       cgd  */
    395   1.1       cgd 
    396   1.1       cgd char *
    397  1.30  christos bltinlookup(const char *name, int doall)
    398  1.10       cgd {
    399   1.1       cgd 	struct strlist *sp;
    400   1.1       cgd 	struct var *v;
    401   1.1       cgd 
    402   1.1       cgd 	for (sp = cmdenviron ; sp ; sp = sp->next) {
    403  1.35       dsl 		if (strequal(sp->text, name))
    404   1.1       cgd 			return strchr(sp->text, '=') + 1;
    405   1.1       cgd 	}
    406  1.35       dsl 
    407  1.35       dsl 	v = find_var(name, NULL, NULL);
    408  1.35       dsl 
    409  1.35       dsl 	if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
    410  1.35       dsl 		return NULL;
    411  1.35       dsl 	return v->text + v->name_len + 1;
    412   1.1       cgd }
    413   1.1       cgd 
    414   1.1       cgd 
    415   1.1       cgd 
    416   1.1       cgd /*
    417   1.1       cgd  * Generate a list of exported variables.  This routine is used to construct
    418   1.1       cgd  * the third argument to execve when executing a program.
    419   1.1       cgd  */
    420   1.1       cgd 
    421   1.1       cgd char **
    422  1.30  christos environment(void)
    423  1.30  christos {
    424   1.1       cgd 	int nenv;
    425   1.1       cgd 	struct var **vpp;
    426   1.1       cgd 	struct var *vp;
    427  1.23  christos 	char **env;
    428  1.23  christos 	char **ep;
    429   1.1       cgd 
    430   1.1       cgd 	nenv = 0;
    431   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    432   1.1       cgd 		for (vp = *vpp ; vp ; vp = vp->next)
    433  1.51       kre 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
    434   1.1       cgd 				nenv++;
    435   1.1       cgd 	}
    436   1.1       cgd 	ep = env = stalloc((nenv + 1) * sizeof *env);
    437   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    438   1.1       cgd 		for (vp = *vpp ; vp ; vp = vp->next)
    439  1.51       kre 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
    440   1.1       cgd 				*ep++ = vp->text;
    441   1.1       cgd 	}
    442   1.1       cgd 	*ep = NULL;
    443   1.1       cgd 	return env;
    444   1.1       cgd }
    445   1.1       cgd 
    446   1.1       cgd 
    447   1.1       cgd /*
    448   1.1       cgd  * Called when a shell procedure is invoked to clear out nonexported
    449   1.1       cgd  * variables.  It is also necessary to reallocate variables of with
    450   1.1       cgd  * VSTACK set since these are currently allocated on the stack.
    451   1.1       cgd  */
    452   1.1       cgd 
    453   1.1       cgd #ifdef mkinit
    454  1.30  christos void shprocvar(void);
    455   1.1       cgd 
    456   1.1       cgd SHELLPROC {
    457   1.1       cgd 	shprocvar();
    458   1.1       cgd }
    459   1.1       cgd #endif
    460   1.1       cgd 
    461   1.1       cgd void
    462  1.30  christos shprocvar(void)
    463  1.30  christos {
    464   1.1       cgd 	struct var **vpp;
    465   1.1       cgd 	struct var *vp, **prev;
    466   1.1       cgd 
    467   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    468   1.1       cgd 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
    469   1.1       cgd 			if ((vp->flags & VEXPORT) == 0) {
    470   1.1       cgd 				*prev = vp->next;
    471   1.1       cgd 				if ((vp->flags & VTEXTFIXED) == 0)
    472   1.1       cgd 					ckfree(vp->text);
    473   1.1       cgd 				if ((vp->flags & VSTRFIXED) == 0)
    474   1.1       cgd 					ckfree(vp);
    475   1.1       cgd 			} else {
    476   1.1       cgd 				if (vp->flags & VSTACK) {
    477   1.1       cgd 					vp->text = savestr(vp->text);
    478   1.1       cgd 					vp->flags &=~ VSTACK;
    479   1.1       cgd 				}
    480   1.1       cgd 				prev = &vp->next;
    481   1.1       cgd 			}
    482   1.1       cgd 		}
    483   1.1       cgd 	}
    484   1.1       cgd 	initvar();
    485   1.1       cgd }
    486   1.1       cgd 
    487   1.1       cgd 
    488   1.1       cgd 
    489   1.1       cgd /*
    490   1.1       cgd  * Command to list all variables which are set.  Currently this command
    491   1.1       cgd  * is invoked from the set command when the set command is called without
    492   1.1       cgd  * any variables.
    493   1.1       cgd  */
    494   1.1       cgd 
    495  1.30  christos void
    496  1.30  christos print_quoted(const char *p)
    497  1.30  christos {
    498  1.30  christos 	const char *q;
    499  1.30  christos 
    500  1.50       kre 	if (p[0] == '\0') {
    501  1.50       kre 		out1fmt("''");
    502  1.50       kre 		return;
    503  1.50       kre 	}
    504  1.30  christos 	if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
    505  1.30  christos 		out1fmt("%s", p);
    506  1.30  christos 		return;
    507  1.30  christos 	}
    508  1.30  christos 	while (*p) {
    509  1.30  christos 		if (*p == '\'') {
    510  1.30  christos 			out1fmt("\\'");
    511  1.30  christos 			p++;
    512  1.30  christos 			continue;
    513  1.30  christos 		}
    514  1.42  christos 		q = strchr(p, '\'');
    515  1.30  christos 		if (!q) {
    516  1.30  christos 			out1fmt("'%s'", p );
    517  1.30  christos 			return;
    518  1.30  christos 		}
    519  1.31       agc 		out1fmt("'%.*s'", (int)(q - p), p );
    520  1.30  christos 		p = q;
    521  1.30  christos 	}
    522  1.30  christos }
    523  1.30  christos 
    524  1.30  christos static int
    525  1.30  christos sort_var(const void *v_v1, const void *v_v2)
    526  1.30  christos {
    527  1.30  christos 	const struct var * const *v1 = v_v1;
    528  1.30  christos 	const struct var * const *v2 = v_v2;
    529  1.52       kre 	char *t1 = (*v1)->text, *t2 = (*v2)->text;
    530  1.30  christos 
    531  1.52       kre 	if (*t1 == *t2) {
    532  1.52       kre 		char *p, *s;
    533  1.52       kre 
    534  1.52       kre 		STARTSTACKSTR(p);
    535  1.52       kre 
    536  1.52       kre 		/*
    537  1.52       kre 		 * note: if lengths are equal, strings must be different
    538  1.52       kre 		 * so we don't care which string we pick for the \0 in
    539  1.52       kre 		 * that case.
    540  1.52       kre 		 */
    541  1.52       kre 		if ((strchr(t1, '=') - t1) <= (strchr(t2, '=') - t2)) {
    542  1.52       kre 			s = t1;
    543  1.52       kre 			t1 = p;
    544  1.52       kre 		} else {
    545  1.52       kre 			s = t2;
    546  1.52       kre 			t2 = p;
    547  1.52       kre 		}
    548  1.52       kre 
    549  1.52       kre 		while (*s && *s != '=') {
    550  1.52       kre 			STPUTC(*s, p);
    551  1.52       kre 			s++;
    552  1.52       kre 		}
    553  1.52       kre 		STPUTC('\0', p);
    554  1.52       kre 	}
    555  1.52       kre 
    556  1.52       kre 	return strcoll(t1, t2);
    557  1.30  christos }
    558  1.30  christos 
    559  1.30  christos /*
    560  1.30  christos  * POSIX requires that 'set' (but not export or readonly) output the
    561  1.30  christos  * variables in lexicographic order - by the locale's collating order (sigh).
    562  1.30  christos  * Maybe we could keep them in an ordered balanced binary tree
    563  1.30  christos  * instead of hashed lists.
    564  1.30  christos  * For now just roll 'em through qsort for printing...
    565  1.30  christos  */
    566  1.30  christos 
    567   1.1       cgd int
    568  1.49  christos showvars(const char *name, int flag, int show_value, const char *xtra)
    569  1.10       cgd {
    570   1.1       cgd 	struct var **vpp;
    571   1.1       cgd 	struct var *vp;
    572  1.30  christos 	const char *p;
    573  1.30  christos 
    574  1.30  christos 	static struct var **list;	/* static in case we are interrupted */
    575  1.30  christos 	static int list_len;
    576  1.30  christos 	int count = 0;
    577  1.30  christos 
    578  1.30  christos 	if (!list) {
    579  1.30  christos 		list_len = 32;
    580  1.30  christos 		list = ckmalloc(list_len * sizeof *list);
    581  1.30  christos 	}
    582   1.1       cgd 
    583   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    584   1.1       cgd 		for (vp = *vpp ; vp ; vp = vp->next) {
    585  1.30  christos 			if (flag && !(vp->flags & flag))
    586  1.30  christos 				continue;
    587  1.30  christos 			if (vp->flags & VUNSET && !(show_value & 2))
    588  1.30  christos 				continue;
    589  1.30  christos 			if (count >= list_len) {
    590  1.30  christos 				list = ckrealloc(list,
    591  1.30  christos 					(list_len << 1) * sizeof *list);
    592  1.30  christos 				list_len <<= 1;
    593  1.30  christos 			}
    594  1.30  christos 			list[count++] = vp;
    595   1.1       cgd 		}
    596   1.1       cgd 	}
    597  1.30  christos 
    598  1.30  christos 	qsort(list, count, sizeof *list, sort_var);
    599  1.30  christos 
    600  1.30  christos 	for (vpp = list; count--; vpp++) {
    601  1.30  christos 		vp = *vpp;
    602  1.30  christos 		if (name)
    603  1.30  christos 			out1fmt("%s ", name);
    604  1.49  christos 		if (xtra)
    605  1.49  christos 			out1fmt("%s ", xtra);
    606  1.30  christos 		for (p = vp->text ; *p != '=' ; p++)
    607  1.30  christos 			out1c(*p);
    608  1.30  christos 		if (!(vp->flags & VUNSET) && show_value) {
    609  1.30  christos 			out1fmt("=");
    610  1.30  christos 			print_quoted(++p);
    611  1.30  christos 		}
    612  1.30  christos 		out1c('\n');
    613  1.30  christos 	}
    614   1.1       cgd 	return 0;
    615   1.1       cgd }
    616   1.1       cgd 
    617   1.1       cgd 
    618   1.1       cgd 
    619   1.1       cgd /*
    620   1.1       cgd  * The export and readonly commands.
    621   1.1       cgd  */
    622   1.1       cgd 
    623   1.1       cgd int
    624  1.30  christos exportcmd(int argc, char **argv)
    625  1.10       cgd {
    626   1.1       cgd 	struct var *vp;
    627   1.1       cgd 	char *name;
    628  1.23  christos 	const char *p;
    629   1.1       cgd 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
    630  1.49  christos 	int pflg = 0;
    631  1.49  christos 	int nflg = 0;
    632  1.49  christos 	int xflg = 0;
    633  1.49  christos 	int res;
    634  1.49  christos 	int c;
    635  1.51       kre 	int f;
    636  1.49  christos 
    637  1.49  christos 	while ((c = nextopt("npx")) != '\0') {
    638  1.49  christos 		switch (c) {
    639  1.49  christos 		case 'p':
    640  1.49  christos 			if (nflg)
    641  1.49  christos 				return 1;
    642  1.49  christos 			pflg = 3;
    643  1.49  christos 			break;
    644  1.49  christos 		case 'n':
    645  1.49  christos 			if (pflg || xflg || flag == VREADONLY)
    646  1.49  christos 				return 1;
    647  1.49  christos 			nflg = 1;
    648  1.49  christos 			break;
    649  1.49  christos 		case 'x':
    650  1.49  christos 			if (nflg || flag == VREADONLY)
    651  1.49  christos 				return 1;
    652  1.49  christos 			flag = VNOEXPORT;
    653  1.49  christos 			xflg = 1;
    654  1.49  christos 			break;
    655  1.49  christos 		default:
    656  1.49  christos 			return 1;
    657  1.49  christos 		}
    658  1.49  christos 	}
    659   1.1       cgd 
    660  1.49  christos 	if (nflg && *argptr == NULL)
    661  1.49  christos 		return 1;
    662  1.49  christos 
    663  1.49  christos 	if (pflg || *argptr == NULL) {
    664  1.49  christos 		showvars( pflg ? argv[0] : 0, flag, pflg,
    665  1.49  christos 		    pflg && xflg ? "-x" : NULL );
    666  1.30  christos 		return 0;
    667  1.30  christos 	}
    668  1.30  christos 
    669  1.49  christos 	res = 0;
    670  1.30  christos 	while ((name = *argptr++) != NULL) {
    671  1.51       kre 		f = flag;
    672  1.30  christos 		if ((p = strchr(name, '=')) != NULL) {
    673  1.30  christos 			p++;
    674  1.30  christos 		} else {
    675  1.35       dsl 			vp = find_var(name, NULL, NULL);
    676  1.35       dsl 			if (vp != NULL) {
    677  1.49  christos 				if (nflg)
    678  1.49  christos 					vp->flags &= ~flag;
    679  1.49  christos 				else if (flag&VEXPORT && vp->flags&VNOEXPORT)
    680  1.49  christos 					res = 1;
    681  1.49  christos 				else {
    682  1.49  christos 					vp->flags |= flag;
    683  1.49  christos 					if (flag == VNOEXPORT)
    684  1.49  christos 						vp->flags &= ~VEXPORT;
    685  1.49  christos 				}
    686  1.36     enami 				continue;
    687  1.51       kre 			} else
    688  1.51       kre 				f |= VUNSET;
    689   1.1       cgd 		}
    690  1.49  christos 		if (!nflg)
    691  1.51       kre 			setvar(name, p, f);
    692   1.1       cgd 	}
    693  1.49  christos 	return res;
    694   1.1       cgd }
    695   1.1       cgd 
    696   1.1       cgd 
    697   1.1       cgd /*
    698   1.1       cgd  * The "local" command.
    699   1.1       cgd  */
    700   1.1       cgd 
    701  1.10       cgd int
    702  1.30  christos localcmd(int argc, char **argv)
    703  1.10       cgd {
    704   1.1       cgd 	char *name;
    705   1.1       cgd 
    706   1.1       cgd 	if (! in_function())
    707   1.1       cgd 		error("Not in a function");
    708   1.1       cgd 	while ((name = *argptr++) != NULL) {
    709  1.29  christos 		mklocal(name, 0);
    710   1.1       cgd 	}
    711   1.1       cgd 	return 0;
    712   1.1       cgd }
    713   1.1       cgd 
    714   1.1       cgd 
    715   1.1       cgd /*
    716  1.37       snj  * Make a variable a local variable.  When a variable is made local, its
    717   1.1       cgd  * value and flags are saved in a localvar structure.  The saved values
    718   1.1       cgd  * will be restored when the shell function returns.  We handle the name
    719   1.1       cgd  * "-" as a special case.
    720   1.1       cgd  */
    721   1.1       cgd 
    722   1.1       cgd void
    723  1.32       dsl mklocal(const char *name, int flags)
    724  1.30  christos {
    725   1.1       cgd 	struct localvar *lvp;
    726   1.1       cgd 	struct var **vpp;
    727   1.1       cgd 	struct var *vp;
    728   1.1       cgd 
    729   1.1       cgd 	INTOFF;
    730   1.1       cgd 	lvp = ckmalloc(sizeof (struct localvar));
    731   1.1       cgd 	if (name[0] == '-' && name[1] == '\0') {
    732  1.23  christos 		char *p;
    733  1.30  christos 		p = ckmalloc(sizeof_optlist);
    734  1.30  christos 		lvp->text = memcpy(p, optlist, sizeof_optlist);
    735   1.1       cgd 		vp = NULL;
    736   1.1       cgd 	} else {
    737  1.35       dsl 		vp = find_var(name, &vpp, NULL);
    738   1.1       cgd 		if (vp == NULL) {
    739   1.1       cgd 			if (strchr(name, '='))
    740  1.29  christos 				setvareq(savestr(name), VSTRFIXED|flags);
    741   1.1       cgd 			else
    742  1.29  christos 				setvar(name, NULL, VSTRFIXED|flags);
    743   1.1       cgd 			vp = *vpp;	/* the new variable */
    744   1.1       cgd 			lvp->text = NULL;
    745   1.1       cgd 			lvp->flags = VUNSET;
    746   1.1       cgd 		} else {
    747   1.1       cgd 			lvp->text = vp->text;
    748   1.1       cgd 			lvp->flags = vp->flags;
    749   1.1       cgd 			vp->flags |= VSTRFIXED|VTEXTFIXED;
    750  1.35       dsl 			if (name[vp->name_len] == '=')
    751  1.29  christos 				setvareq(savestr(name), flags);
    752   1.1       cgd 		}
    753   1.1       cgd 	}
    754   1.1       cgd 	lvp->vp = vp;
    755   1.1       cgd 	lvp->next = localvars;
    756   1.1       cgd 	localvars = lvp;
    757   1.1       cgd 	INTON;
    758   1.1       cgd }
    759   1.1       cgd 
    760   1.1       cgd 
    761   1.1       cgd /*
    762   1.1       cgd  * Called after a function returns.
    763   1.1       cgd  */
    764   1.1       cgd 
    765   1.1       cgd void
    766  1.30  christos poplocalvars(void)
    767  1.30  christos {
    768   1.1       cgd 	struct localvar *lvp;
    769   1.1       cgd 	struct var *vp;
    770   1.1       cgd 
    771   1.1       cgd 	while ((lvp = localvars) != NULL) {
    772   1.1       cgd 		localvars = lvp->next;
    773   1.1       cgd 		vp = lvp->vp;
    774  1.32       dsl 		TRACE(("poplocalvar %s", vp ? vp->text : "-"));
    775   1.1       cgd 		if (vp == NULL) {	/* $- saved */
    776  1.30  christos 			memcpy(optlist, lvp->text, sizeof_optlist);
    777   1.1       cgd 			ckfree(lvp->text);
    778   1.1       cgd 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
    779  1.30  christos 			(void)unsetvar(vp->text, 0);
    780   1.1       cgd 		} else {
    781  1.32       dsl 			if (vp->func && (vp->flags & VNOFUNC) == 0)
    782  1.35       dsl 				(*vp->func)(lvp->text + vp->name_len + 1);
    783   1.1       cgd 			if ((vp->flags & VTEXTFIXED) == 0)
    784   1.1       cgd 				ckfree(vp->text);
    785   1.1       cgd 			vp->flags = lvp->flags;
    786   1.1       cgd 			vp->text = lvp->text;
    787   1.1       cgd 		}
    788   1.1       cgd 		ckfree(lvp);
    789   1.1       cgd 	}
    790   1.1       cgd }
    791   1.1       cgd 
    792   1.1       cgd 
    793  1.10       cgd int
    794  1.30  christos setvarcmd(int argc, char **argv)
    795  1.10       cgd {
    796   1.1       cgd 	if (argc <= 2)
    797   1.1       cgd 		return unsetcmd(argc, argv);
    798   1.1       cgd 	else if (argc == 3)
    799   1.1       cgd 		setvar(argv[1], argv[2], 0);
    800   1.1       cgd 	else
    801   1.1       cgd 		error("List assignment not implemented");
    802   1.1       cgd 	return 0;
    803   1.1       cgd }
    804   1.1       cgd 
    805   1.1       cgd 
    806   1.1       cgd /*
    807   1.1       cgd  * The unset builtin command.  We unset the function before we unset the
    808   1.1       cgd  * variable to allow a function to be unset when there is a readonly variable
    809   1.1       cgd  * with the same name.
    810   1.1       cgd  */
    811   1.1       cgd 
    812  1.10       cgd int
    813  1.30  christos unsetcmd(int argc, char **argv)
    814  1.10       cgd {
    815   1.1       cgd 	char **ap;
    816   1.5       jtc 	int i;
    817   1.5       jtc 	int flg_func = 0;
    818   1.5       jtc 	int flg_var = 0;
    819   1.5       jtc 	int ret = 0;
    820   1.5       jtc 
    821  1.30  christos 	while ((i = nextopt("evf")) != '\0') {
    822   1.5       jtc 		if (i == 'f')
    823   1.5       jtc 			flg_func = 1;
    824   1.5       jtc 		else
    825  1.30  christos 			flg_var = i;
    826   1.5       jtc 	}
    827   1.5       jtc 	if (flg_func == 0 && flg_var == 0)
    828   1.5       jtc 		flg_var = 1;
    829  1.15  christos 
    830   1.5       jtc 	for (ap = argptr; *ap ; ap++) {
    831   1.5       jtc 		if (flg_func)
    832   1.5       jtc 			ret |= unsetfunc(*ap);
    833   1.5       jtc 		if (flg_var)
    834  1.30  christos 			ret |= unsetvar(*ap, flg_var == 'e');
    835   1.1       cgd 	}
    836   1.5       jtc 	return ret;
    837   1.1       cgd }
    838   1.1       cgd 
    839   1.1       cgd 
    840   1.1       cgd /*
    841   1.1       cgd  * Unset the specified variable.
    842   1.1       cgd  */
    843   1.1       cgd 
    844  1.14  christos int
    845  1.30  christos unsetvar(const char *s, int unexport)
    846  1.30  christos {
    847   1.1       cgd 	struct var **vpp;
    848   1.1       cgd 	struct var *vp;
    849   1.1       cgd 
    850  1.35       dsl 	vp = find_var(s, &vpp, NULL);
    851  1.35       dsl 	if (vp == NULL)
    852  1.43  christos 		return 0;
    853  1.35       dsl 
    854  1.49  christos 	if (vp->flags & VREADONLY && !unexport)
    855  1.43  christos 		return 1;
    856  1.35       dsl 
    857  1.35       dsl 	INTOFF;
    858  1.35       dsl 	if (unexport) {
    859  1.35       dsl 		vp->flags &= ~VEXPORT;
    860  1.35       dsl 	} else {
    861  1.35       dsl 		if (vp->text[vp->name_len + 1] != '\0')
    862  1.35       dsl 			setvar(s, nullstr, 0);
    863  1.35       dsl 		vp->flags &= ~VEXPORT;
    864  1.35       dsl 		vp->flags |= VUNSET;
    865  1.35       dsl 		if ((vp->flags & VSTRFIXED) == 0) {
    866  1.35       dsl 			if ((vp->flags & VTEXTFIXED) == 0)
    867  1.35       dsl 				ckfree(vp->text);
    868  1.35       dsl 			*vpp = vp->next;
    869  1.35       dsl 			ckfree(vp);
    870   1.1       cgd 		}
    871   1.1       cgd 	}
    872  1.35       dsl 	INTON;
    873  1.35       dsl 	return 0;
    874   1.1       cgd }
    875   1.1       cgd 
    876   1.1       cgd 
    877   1.1       cgd /*
    878   1.1       cgd  * Returns true if the two strings specify the same varable.  The first
    879   1.1       cgd  * variable name is terminated by '='; the second may be terminated by
    880   1.1       cgd  * either '=' or '\0'.
    881   1.1       cgd  */
    882   1.1       cgd 
    883   1.1       cgd STATIC int
    884  1.35       dsl strequal(const char *p, const char *q)
    885  1.30  christos {
    886   1.1       cgd 	while (*p == *q++) {
    887   1.1       cgd 		if (*p++ == '=')
    888   1.1       cgd 			return 1;
    889   1.1       cgd 	}
    890   1.1       cgd 	if (*p == '=' && *(q - 1) == '\0')
    891   1.1       cgd 		return 1;
    892   1.1       cgd 	return 0;
    893   1.1       cgd }
    894  1.35       dsl 
    895  1.35       dsl /*
    896  1.35       dsl  * Search for a variable.
    897  1.35       dsl  * 'name' may be terminated by '=' or a NUL.
    898  1.35       dsl  * vppp is set to the pointer to vp, or the list head if vp isn't found
    899  1.35       dsl  * lenp is set to the number of charactets in 'name'
    900  1.35       dsl  */
    901  1.35       dsl 
    902  1.35       dsl STATIC struct var *
    903  1.35       dsl find_var(const char *name, struct var ***vppp, int *lenp)
    904  1.35       dsl {
    905  1.35       dsl 	unsigned int hashval;
    906  1.35       dsl 	int len;
    907  1.35       dsl 	struct var *vp, **vpp;
    908  1.35       dsl 	const char *p = name;
    909  1.35       dsl 
    910  1.35       dsl 	hashval = 0;
    911  1.35       dsl 	while (*p && *p != '=')
    912  1.35       dsl 		hashval = 2 * hashval + (unsigned char)*p++;
    913  1.35       dsl 	len = p - name;
    914  1.35       dsl 
    915  1.35       dsl 	if (lenp)
    916  1.35       dsl 		*lenp = len;
    917  1.35       dsl 	vpp = &vartab[hashval % VTABSIZE];
    918  1.35       dsl 	if (vppp)
    919  1.35       dsl 		*vppp = vpp;
    920  1.35       dsl 
    921  1.35       dsl 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
    922  1.35       dsl 		if (vp->name_len != len)
    923  1.35       dsl 			continue;
    924  1.35       dsl 		if (memcmp(vp->text, name, len) != 0)
    925  1.35       dsl 			continue;
    926  1.35       dsl 		if (vppp)
    927  1.35       dsl 			*vppp = vpp;
    928  1.35       dsl 		return vp;
    929  1.35       dsl 	}
    930  1.35       dsl 	return NULL;
    931  1.35       dsl }
    932