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