var.c revision 90111
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
4036150Scharnier#endif
4136150Scharnierstatic const char rcsid[] =
4250471Speter  "$FreeBSD: head/bin/sh/var.c 90111 2002-02-02 06:50:57Z imp $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
4517987Speter#include <unistd.h>
4617987Speter#include <stdlib.h>
4717987Speter
481556Srgrimes/*
491556Srgrimes * Shell variables.
501556Srgrimes */
511556Srgrimes
5217525Sache#include <locale.h>
5317525Sache
541556Srgrimes#include "shell.h"
551556Srgrimes#include "output.h"
561556Srgrimes#include "expand.h"
571556Srgrimes#include "nodes.h"	/* for other headers */
581556Srgrimes#include "eval.h"	/* defines cmdenviron */
591556Srgrimes#include "exec.h"
601556Srgrimes#include "syntax.h"
611556Srgrimes#include "options.h"
621556Srgrimes#include "mail.h"
631556Srgrimes#include "var.h"
641556Srgrimes#include "memalloc.h"
651556Srgrimes#include "error.h"
661556Srgrimes#include "mystring.h"
6720425Ssteve#include "parser.h"
6817987Speter#ifndef NO_HISTORY
6917987Speter#include "myhistedit.h"
7017987Speter#endif
711556Srgrimes
721556Srgrimes
731556Srgrimes#define VTABSIZE 39
741556Srgrimes
751556Srgrimes
761556Srgrimesstruct varinit {
771556Srgrimes	struct var *var;
781556Srgrimes	int flags;
791556Srgrimes	char *text;
8090111Simp	void (*func)(const char *);
811556Srgrimes};
821556Srgrimes
831556Srgrimes
841556Srgrimes#if ATTY
851556Srgrimesstruct var vatty;
861556Srgrimes#endif
8717987Speter#ifndef NO_HISTORY
881556Srgrimesstruct var vhistsize;
8917987Speter#endif
901556Srgrimesstruct var vifs;
911556Srgrimesstruct var vmail;
921556Srgrimesstruct var vmpath;
931556Srgrimesstruct var vpath;
941556Srgrimesstruct var vps1;
951556Srgrimesstruct var vps2;
961556Srgrimesstruct var vvers;
971556Srgrimes#if ATTY
981556Srgrimesstruct var vterm;
991556Srgrimes#endif
10020425Sstevestruct var voptind;
1011556Srgrimes
1021556Srgrimesconst struct varinit varinit[] = {
1031556Srgrimes#if ATTY
10420425Ssteve	{ &vatty,	VSTRFIXED|VTEXTFIXED|VUNSET,	"ATTY=",
10520425Ssteve	  NULL },
1061556Srgrimes#endif
10717987Speter#ifndef NO_HISTORY
10820425Ssteve	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
10920425Ssteve	  sethistsize },
11017987Speter#endif
11120425Ssteve	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
11220425Ssteve	  NULL },
11320425Ssteve	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
11420425Ssteve	  NULL },
11520425Ssteve	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
11620425Ssteve	  NULL },
11720425Ssteve	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=/bin:/usr/bin",
11820425Ssteve	  changepath },
1198855Srgrimes	/*
1201556Srgrimes	 * vps1 depends on uid
1211556Srgrimes	 */
12220425Ssteve	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
12320425Ssteve	  NULL },
1241556Srgrimes#if ATTY
12520425Ssteve	{ &vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM=",
12620425Ssteve	  NULL },
1271556Srgrimes#endif
12820425Ssteve	{ &voptind,	VSTRFIXED|VTEXTFIXED,		"OPTIND=1",
12920425Ssteve	  getoptsreset },
13020425Ssteve	{ NULL,	0,				NULL,
13120425Ssteve	  NULL }
1321556Srgrimes};
1331556Srgrimes
1341556Srgrimesstruct var *vartab[VTABSIZE];
1351556Srgrimes
13690111SimpSTATIC struct var **hashvar(char *);
13790111SimpSTATIC int varequal(char *, char *);
13890111SimpSTATIC int localevar(char *);
1391556Srgrimes
1401556Srgrimes/*
1411556Srgrimes * Initialize the varable symbol tables and import the environment
1421556Srgrimes */
1431556Srgrimes
1441556Srgrimes#ifdef mkinit
1451556SrgrimesINCLUDE "var.h"
1461556SrgrimesINIT {
1471556Srgrimes	char **envp;
1481556Srgrimes	extern char **environ;
1491556Srgrimes
1501556Srgrimes	initvar();
1511556Srgrimes	for (envp = environ ; *envp ; envp++) {
1521556Srgrimes		if (strchr(*envp, '=')) {
1531556Srgrimes			setvareq(*envp, VEXPORT|VTEXTFIXED);
1541556Srgrimes		}
1551556Srgrimes	}
1561556Srgrimes}
1571556Srgrimes#endif
1581556Srgrimes
1591556Srgrimes
1601556Srgrimes/*
1611556Srgrimes * This routine initializes the builtin variables.  It is called when the
1621556Srgrimes * shell is initialized and again when a shell procedure is spawned.
1631556Srgrimes */
1641556Srgrimes
1651556Srgrimesvoid
16690111Simpinitvar(void)
16790111Simp{
1681556Srgrimes	const struct varinit *ip;
1691556Srgrimes	struct var *vp;
1701556Srgrimes	struct var **vpp;
1711556Srgrimes
1721556Srgrimes	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1731556Srgrimes		if ((vp->flags & VEXPORT) == 0) {
1741556Srgrimes			vpp = hashvar(ip->text);
1751556Srgrimes			vp->next = *vpp;
1761556Srgrimes			*vpp = vp;
1771556Srgrimes			vp->text = ip->text;
1781556Srgrimes			vp->flags = ip->flags;
17920425Ssteve			vp->func = ip->func;
1801556Srgrimes		}
1811556Srgrimes	}
1821556Srgrimes	/*
1831556Srgrimes	 * PS1 depends on uid
1841556Srgrimes	 */
1851556Srgrimes	if ((vps1.flags & VEXPORT) == 0) {
1861556Srgrimes		vpp = hashvar("PS1=");
1871556Srgrimes		vps1.next = *vpp;
1881556Srgrimes		*vpp = &vps1;
1891556Srgrimes		vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
1901556Srgrimes		vps1.flags = VSTRFIXED|VTEXTFIXED;
1911556Srgrimes	}
1921556Srgrimes}
1931556Srgrimes
1941556Srgrimes/*
19520425Ssteve * Safe version of setvar, returns 1 on success 0 on failure.
19620425Ssteve */
19720425Ssteve
19820425Ssteveint
19990111Simpsetvarsafe(char *name, char *val, int flags)
20020425Ssteve{
20120425Ssteve	struct jmploc jmploc;
20220425Ssteve	struct jmploc *volatile savehandler = handler;
20320425Ssteve	int err = 0;
20420425Ssteve#if __GNUC__
20520425Ssteve	/* Avoid longjmp clobbering */
20620425Ssteve	(void) &err;
20720425Ssteve#endif
20820425Ssteve
20920425Ssteve	if (setjmp(jmploc.loc))
21020425Ssteve		err = 1;
21120425Ssteve	else {
21220425Ssteve		handler = &jmploc;
21320425Ssteve		setvar(name, val, flags);
21420425Ssteve	}
21520425Ssteve	handler = savehandler;
21620425Ssteve	return err;
21720425Ssteve}
21820425Ssteve
21920425Ssteve/*
22045621Scracauer * Set the value of a variable.  The flags argument is tored with the
2211556Srgrimes * flags of the variable.  If val is NULL, the variable is unset.
2221556Srgrimes */
2231556Srgrimes
2241556Srgrimesvoid
22590111Simpsetvar(char *name, char *val, int flags)
22617987Speter{
2271556Srgrimes	char *p, *q;
2281556Srgrimes	int len;
2291556Srgrimes	int namelen;
2301556Srgrimes	char *nameeq;
2311556Srgrimes	int isbad;
2321556Srgrimes
2331556Srgrimes	isbad = 0;
2341556Srgrimes	p = name;
23517525Sache	if (! is_name(*p))
2361556Srgrimes		isbad = 1;
23717525Sache	p++;
2381556Srgrimes	for (;;) {
2391556Srgrimes		if (! is_in_name(*p)) {
2401556Srgrimes			if (*p == '\0' || *p == '=')
2411556Srgrimes				break;
2421556Srgrimes			isbad = 1;
2431556Srgrimes		}
2441556Srgrimes		p++;
2451556Srgrimes	}
2461556Srgrimes	namelen = p - name;
2471556Srgrimes	if (isbad)
2481556Srgrimes		error("%.*s: bad variable name", namelen, name);
2491556Srgrimes	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2501556Srgrimes	if (val == NULL) {
2511556Srgrimes		flags |= VUNSET;
2521556Srgrimes	} else {
2531556Srgrimes		len += strlen(val);
2541556Srgrimes	}
2551556Srgrimes	p = nameeq = ckmalloc(len);
2561556Srgrimes	q = name;
2571556Srgrimes	while (--namelen >= 0)
2581556Srgrimes		*p++ = *q++;
2591556Srgrimes	*p++ = '=';
2601556Srgrimes	*p = '\0';
2611556Srgrimes	if (val)
2621556Srgrimes		scopy(val, p);
2631556Srgrimes	setvareq(nameeq, flags);
2641556Srgrimes}
2651556Srgrimes
26617525SacheSTATIC int
26790111Simplocalevar(char *s)
26890111Simp{
26917525Sache	static char *lnames[7] = {
27017525Sache		"ALL", "COLLATE", "CTYPE", "MONETARY",
27117525Sache		"NUMERIC", "TIME", NULL
27217525Sache	};
27317525Sache	char **ss;
2741556Srgrimes
27517525Sache	if (*s != 'L')
27617525Sache		return 0;
27717525Sache	if (varequal(s + 1, "ANG"))
27817525Sache		return 1;
27917525Sache	if (strncmp(s + 1, "C_", 2) != 0)
28017525Sache		return 0;
28117525Sache	for (ss = lnames; *ss ; ss++)
28217525Sache		if (varequal(s + 3, *ss))
28317525Sache			return 1;
28417525Sache	return 0;
28517525Sache}
2861556Srgrimes
2871556Srgrimes/*
2881556Srgrimes * Same as setvar except that the variable and value are passed in
2891556Srgrimes * the first argument as name=value.  Since the first argument will
2901556Srgrimes * be actually stored in the table, it should not be a string that
2911556Srgrimes * will go away.
2921556Srgrimes */
2931556Srgrimes
2941556Srgrimesvoid
29590111Simpsetvareq(char *s, int flags)
29617987Speter{
2971556Srgrimes	struct var *vp, **vpp;
2981556Srgrimes
29945263Scracauer	if (aflag)
30045263Scracauer		flags |= VEXPORT;
3011556Srgrimes	vpp = hashvar(s);
3021556Srgrimes	for (vp = *vpp ; vp ; vp = vp->next) {
3031556Srgrimes		if (varequal(s, vp->text)) {
3041556Srgrimes			if (vp->flags & VREADONLY) {
30520425Ssteve				size_t len = strchr(s, '=') - s;
3061556Srgrimes				error("%.*s: is read only", len, s);
3071556Srgrimes			}
3081556Srgrimes			INTOFF;
30920425Ssteve
31020425Ssteve			if (vp->func && (flags & VNOFUNC) == 0)
31120425Ssteve				(*vp->func)(strchr(s, '=') + 1);
31220425Ssteve
3131556Srgrimes			if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3141556Srgrimes				ckfree(vp->text);
31520425Ssteve
31620425Ssteve			vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3171556Srgrimes			vp->flags |= flags;
3181556Srgrimes			vp->text = s;
31920425Ssteve
32020425Ssteve			/*
32120425Ssteve			 * We could roll this to a function, to handle it as
32220425Ssteve			 * a regular variable function callback, but why bother?
32320425Ssteve			 */
3241556Srgrimes			if (vp == &vmpath || (vp == &vmail && ! mpathset()))
3251556Srgrimes				chkmail(1);
32617525Sache			if ((vp->flags & VEXPORT) && localevar(s)) {
32717525Sache				putenv(s);
32817525Sache				(void) setlocale(LC_ALL, "");
32917525Sache			}
3301556Srgrimes			INTON;
3311556Srgrimes			return;
3321556Srgrimes		}
3331556Srgrimes	}
3341556Srgrimes	/* not found */
3351556Srgrimes	vp = ckmalloc(sizeof (*vp));
3361556Srgrimes	vp->flags = flags;
3371556Srgrimes	vp->text = s;
3381556Srgrimes	vp->next = *vpp;
33920425Ssteve	vp->func = NULL;
34017525Sache	INTOFF;
3411556Srgrimes	*vpp = vp;
34217525Sache	if ((vp->flags & VEXPORT) && localevar(s)) {
34317525Sache		putenv(s);
34417525Sache		(void) setlocale(LC_ALL, "");
34517525Sache	}
34617525Sache	INTON;
3471556Srgrimes}
3481556Srgrimes
3491556Srgrimes
3501556Srgrimes
3511556Srgrimes/*
3521556Srgrimes * Process a linked list of variable assignments.
3531556Srgrimes */
3541556Srgrimes
3551556Srgrimesvoid
35690111Simplistsetvar(struct strlist *list)
35790111Simp{
3581556Srgrimes	struct strlist *lp;
3591556Srgrimes
3601556Srgrimes	INTOFF;
3611556Srgrimes	for (lp = list ; lp ; lp = lp->next) {
3621556Srgrimes		setvareq(savestr(lp->text), 0);
3631556Srgrimes	}
3641556Srgrimes	INTON;
3651556Srgrimes}
3661556Srgrimes
3671556Srgrimes
3681556Srgrimes
3691556Srgrimes/*
3701556Srgrimes * Find the value of a variable.  Returns NULL if not set.
3711556Srgrimes */
3721556Srgrimes
3731556Srgrimeschar *
37490111Simplookupvar(char *name)
37590111Simp{
3761556Srgrimes	struct var *v;
3771556Srgrimes
3781556Srgrimes	for (v = *hashvar(name) ; v ; v = v->next) {
3791556Srgrimes		if (varequal(v->text, name)) {
3801556Srgrimes			if (v->flags & VUNSET)
3811556Srgrimes				return NULL;
3821556Srgrimes			return strchr(v->text, '=') + 1;
3831556Srgrimes		}
3841556Srgrimes	}
3851556Srgrimes	return NULL;
3861556Srgrimes}
3871556Srgrimes
3881556Srgrimes
3891556Srgrimes
3901556Srgrimes/*
3911556Srgrimes * Search the environment of a builtin command.  If the second argument
3921556Srgrimes * is nonzero, return the value of a variable even if it hasn't been
3931556Srgrimes * exported.
3941556Srgrimes */
3951556Srgrimes
3961556Srgrimeschar *
39790111Simpbltinlookup(char *name, int doall)
39817987Speter{
3991556Srgrimes	struct strlist *sp;
4001556Srgrimes	struct var *v;
4011556Srgrimes
4021556Srgrimes	for (sp = cmdenviron ; sp ; sp = sp->next) {
4031556Srgrimes		if (varequal(sp->text, name))
4041556Srgrimes			return strchr(sp->text, '=') + 1;
4051556Srgrimes	}
4061556Srgrimes	for (v = *hashvar(name) ; v ; v = v->next) {
4071556Srgrimes		if (varequal(v->text, name)) {
40817987Speter			if ((v->flags & VUNSET)
40917987Speter			 || (!doall && (v->flags & VEXPORT) == 0))
4101556Srgrimes				return NULL;
4111556Srgrimes			return strchr(v->text, '=') + 1;
4121556Srgrimes		}
4131556Srgrimes	}
4141556Srgrimes	return NULL;
4151556Srgrimes}
4161556Srgrimes
4171556Srgrimes
4181556Srgrimes
4191556Srgrimes/*
4201556Srgrimes * Generate a list of exported variables.  This routine is used to construct
4211556Srgrimes * the third argument to execve when executing a program.
4221556Srgrimes */
4231556Srgrimes
4241556Srgrimeschar **
42590111Simpenvironment(void)
42690111Simp{
4271556Srgrimes	int nenv;
4281556Srgrimes	struct var **vpp;
4291556Srgrimes	struct var *vp;
4301556Srgrimes	char **env, **ep;
4311556Srgrimes
4321556Srgrimes	nenv = 0;
4331556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4341556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next)
4351556Srgrimes			if (vp->flags & VEXPORT)
4361556Srgrimes				nenv++;
4371556Srgrimes	}
4381556Srgrimes	ep = env = stalloc((nenv + 1) * sizeof *env);
4391556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4401556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next)
4411556Srgrimes			if (vp->flags & VEXPORT)
4421556Srgrimes				*ep++ = vp->text;
4431556Srgrimes	}
4441556Srgrimes	*ep = NULL;
4451556Srgrimes	return env;
4461556Srgrimes}
4471556Srgrimes
4481556Srgrimes
4491556Srgrimes/*
4501556Srgrimes * Called when a shell procedure is invoked to clear out nonexported
4511556Srgrimes * variables.  It is also necessary to reallocate variables of with
4521556Srgrimes * VSTACK set since these are currently allocated on the stack.
4531556Srgrimes */
4541556Srgrimes
4551556Srgrimes#ifdef mkinit
4561556SrgrimesMKINIT void shprocvar();
4571556Srgrimes
4581556SrgrimesSHELLPROC {
4591556Srgrimes	shprocvar();
4601556Srgrimes}
4611556Srgrimes#endif
4621556Srgrimes
4631556Srgrimesvoid
46490111Simpshprocvar(void)
46590111Simp{
4661556Srgrimes	struct var **vpp;
4671556Srgrimes	struct var *vp, **prev;
4681556Srgrimes
4691556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4701556Srgrimes		for (prev = vpp ; (vp = *prev) != NULL ; ) {
4711556Srgrimes			if ((vp->flags & VEXPORT) == 0) {
4721556Srgrimes				*prev = vp->next;
4731556Srgrimes				if ((vp->flags & VTEXTFIXED) == 0)
4741556Srgrimes					ckfree(vp->text);
4751556Srgrimes				if ((vp->flags & VSTRFIXED) == 0)
4761556Srgrimes					ckfree(vp);
4771556Srgrimes			} else {
4781556Srgrimes				if (vp->flags & VSTACK) {
4791556Srgrimes					vp->text = savestr(vp->text);
4801556Srgrimes					vp->flags &=~ VSTACK;
4811556Srgrimes				}
4821556Srgrimes				prev = &vp->next;
4831556Srgrimes			}
4841556Srgrimes		}
4851556Srgrimes	}
4861556Srgrimes	initvar();
4871556Srgrimes}
4881556Srgrimes
4891556Srgrimes
4901556Srgrimes
4911556Srgrimes/*
4921556Srgrimes * Command to list all variables which are set.  Currently this command
4931556Srgrimes * is invoked from the set command when the set command is called without
4941556Srgrimes * any variables.
4951556Srgrimes */
4961556Srgrimes
4971556Srgrimesint
49890111Simpshowvarscmd(int argc __unused, char **argv __unused)
49917987Speter{
5001556Srgrimes	struct var **vpp;
5011556Srgrimes	struct var *vp;
5021556Srgrimes
5031556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5041556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next) {
5051556Srgrimes			if ((vp->flags & VUNSET) == 0)
5061556Srgrimes				out1fmt("%s\n", vp->text);
5071556Srgrimes		}
5081556Srgrimes	}
5091556Srgrimes	return 0;
5101556Srgrimes}
5111556Srgrimes
5121556Srgrimes
5131556Srgrimes
5141556Srgrimes/*
5151556Srgrimes * The export and readonly commands.
5161556Srgrimes */
5171556Srgrimes
5181556Srgrimesint
51990111Simpexportcmd(int argc, char **argv)
52017987Speter{
5211556Srgrimes	struct var **vpp;
5221556Srgrimes	struct var *vp;
5231556Srgrimes	char *name;
5241556Srgrimes	char *p;
5251556Srgrimes	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
5261556Srgrimes
5271556Srgrimes	listsetvar(cmdenviron);
5281556Srgrimes	if (argc > 1) {
5291556Srgrimes		while ((name = *argptr++) != NULL) {
5301556Srgrimes			if ((p = strchr(name, '=')) != NULL) {
5311556Srgrimes				p++;
5321556Srgrimes			} else {
5331556Srgrimes				vpp = hashvar(name);
5341556Srgrimes				for (vp = *vpp ; vp ; vp = vp->next) {
5351556Srgrimes					if (varequal(vp->text, name)) {
53690111Simp
5371556Srgrimes						vp->flags |= flag;
53817525Sache						if ((vp->flags & VEXPORT) && localevar(vp->text)) {
53917525Sache							putenv(vp->text);
54017525Sache							(void) setlocale(LC_ALL, "");
54117525Sache						}
5421556Srgrimes						goto found;
5431556Srgrimes					}
5441556Srgrimes				}
5451556Srgrimes			}
5461556Srgrimes			setvar(name, p, flag);
5471556Srgrimesfound:;
5481556Srgrimes		}
5491556Srgrimes	} else {
5501556Srgrimes		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5511556Srgrimes			for (vp = *vpp ; vp ; vp = vp->next) {
5521556Srgrimes				if (vp->flags & flag) {
5531556Srgrimes					for (p = vp->text ; *p != '=' ; p++)
5541556Srgrimes						out1c(*p);
5551556Srgrimes					out1c('\n');
5561556Srgrimes				}
5571556Srgrimes			}
5581556Srgrimes		}
5591556Srgrimes	}
5601556Srgrimes	return 0;
5611556Srgrimes}
5621556Srgrimes
5631556Srgrimes
5641556Srgrimes/*
5651556Srgrimes * The "local" command.
5661556Srgrimes */
5671556Srgrimes
56817987Speterint
56990111Simplocalcmd(int argc __unused, char **argv __unused)
57017987Speter{
5711556Srgrimes	char *name;
5721556Srgrimes
5731556Srgrimes	if (! in_function())
5741556Srgrimes		error("Not in a function");
5751556Srgrimes	while ((name = *argptr++) != NULL) {
5761556Srgrimes		mklocal(name);
5771556Srgrimes	}
5781556Srgrimes	return 0;
5791556Srgrimes}
5801556Srgrimes
5811556Srgrimes
5821556Srgrimes/*
5831556Srgrimes * Make a variable a local variable.  When a variable is made local, it's
5841556Srgrimes * value and flags are saved in a localvar structure.  The saved values
5851556Srgrimes * will be restored when the shell function returns.  We handle the name
5861556Srgrimes * "-" as a special case.
5871556Srgrimes */
5881556Srgrimes
5891556Srgrimesvoid
59090111Simpmklocal(char *name)
59190111Simp{
5921556Srgrimes	struct localvar *lvp;
5931556Srgrimes	struct var **vpp;
5941556Srgrimes	struct var *vp;
5951556Srgrimes
5961556Srgrimes	INTOFF;
5971556Srgrimes	lvp = ckmalloc(sizeof (struct localvar));
5981556Srgrimes	if (name[0] == '-' && name[1] == '\0') {
5991556Srgrimes		lvp->text = ckmalloc(sizeof optlist);
60017987Speter		memcpy(lvp->text, optlist, sizeof optlist);
6011556Srgrimes		vp = NULL;
6021556Srgrimes	} else {
6031556Srgrimes		vpp = hashvar(name);
6041556Srgrimes		for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
6051556Srgrimes		if (vp == NULL) {
6061556Srgrimes			if (strchr(name, '='))
6071556Srgrimes				setvareq(savestr(name), VSTRFIXED);
6081556Srgrimes			else
6091556Srgrimes				setvar(name, NULL, VSTRFIXED);
6101556Srgrimes			vp = *vpp;	/* the new variable */
6111556Srgrimes			lvp->text = NULL;
6121556Srgrimes			lvp->flags = VUNSET;
6131556Srgrimes		} else {
6141556Srgrimes			lvp->text = vp->text;
6151556Srgrimes			lvp->flags = vp->flags;
6161556Srgrimes			vp->flags |= VSTRFIXED|VTEXTFIXED;
6171556Srgrimes			if (strchr(name, '='))
6181556Srgrimes				setvareq(savestr(name), 0);
6191556Srgrimes		}
6201556Srgrimes	}
6211556Srgrimes	lvp->vp = vp;
6221556Srgrimes	lvp->next = localvars;
6231556Srgrimes	localvars = lvp;
6241556Srgrimes	INTON;
6251556Srgrimes}
6261556Srgrimes
6271556Srgrimes
6281556Srgrimes/*
6291556Srgrimes * Called after a function returns.
6301556Srgrimes */
6311556Srgrimes
6321556Srgrimesvoid
63390111Simppoplocalvars(void)
63490111Simp{
6351556Srgrimes	struct localvar *lvp;
6361556Srgrimes	struct var *vp;
6371556Srgrimes
6381556Srgrimes	while ((lvp = localvars) != NULL) {
6391556Srgrimes		localvars = lvp->next;
6401556Srgrimes		vp = lvp->vp;
6411556Srgrimes		if (vp == NULL) {	/* $- saved */
64217987Speter			memcpy(optlist, lvp->text, sizeof optlist);
6431556Srgrimes			ckfree(lvp->text);
6441556Srgrimes		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
6451556Srgrimes			(void)unsetvar(vp->text);
6461556Srgrimes		} else {
6471556Srgrimes			if ((vp->flags & VTEXTFIXED) == 0)
6481556Srgrimes				ckfree(vp->text);
6491556Srgrimes			vp->flags = lvp->flags;
6501556Srgrimes			vp->text = lvp->text;
6511556Srgrimes		}
6521556Srgrimes		ckfree(lvp);
6531556Srgrimes	}
6541556Srgrimes}
6551556Srgrimes
6561556Srgrimes
65717987Speterint
65890111Simpsetvarcmd(int argc, char **argv)
65917987Speter{
6601556Srgrimes	if (argc <= 2)
6611556Srgrimes		return unsetcmd(argc, argv);
6621556Srgrimes	else if (argc == 3)
6631556Srgrimes		setvar(argv[1], argv[2], 0);
6641556Srgrimes	else
6651556Srgrimes		error("List assignment not implemented");
6661556Srgrimes	return 0;
6671556Srgrimes}
6681556Srgrimes
6691556Srgrimes
6701556Srgrimes/*
6711556Srgrimes * The unset builtin command.  We unset the function before we unset the
6721556Srgrimes * variable to allow a function to be unset when there is a readonly variable
6731556Srgrimes * with the same name.
6741556Srgrimes */
6751556Srgrimes
67617987Speterint
67790111Simpunsetcmd(int argc __unused, char **argv __unused)
67817987Speter{
6791556Srgrimes	char **ap;
6801556Srgrimes	int i;
6811556Srgrimes	int flg_func = 0;
6821556Srgrimes	int flg_var = 0;
6831556Srgrimes	int ret = 0;
6841556Srgrimes
6851556Srgrimes	while ((i = nextopt("vf")) != '\0') {
6861556Srgrimes		if (i == 'f')
6871556Srgrimes			flg_func = 1;
6881556Srgrimes		else
6891556Srgrimes			flg_var = 1;
6901556Srgrimes	}
6911556Srgrimes	if (flg_func == 0 && flg_var == 0)
6921556Srgrimes		flg_var = 1;
6938855Srgrimes
6941556Srgrimes	for (ap = argptr; *ap ; ap++) {
6951556Srgrimes		if (flg_func)
6961556Srgrimes			ret |= unsetfunc(*ap);
6971556Srgrimes		if (flg_var)
6981556Srgrimes			ret |= unsetvar(*ap);
6991556Srgrimes	}
7001556Srgrimes	return ret;
7011556Srgrimes}
7021556Srgrimes
7031556Srgrimes
7041556Srgrimes/*
7051556Srgrimes * Unset the specified variable.
7061556Srgrimes */
7071556Srgrimes
70820425Ssteveint
70990111Simpunsetvar(char *s)
71090111Simp{
7111556Srgrimes	struct var **vpp;
7121556Srgrimes	struct var *vp;
7131556Srgrimes
7141556Srgrimes	vpp = hashvar(s);
7151556Srgrimes	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
7161556Srgrimes		if (varequal(vp->text, s)) {
7171556Srgrimes			if (vp->flags & VREADONLY)
7181556Srgrimes				return (1);
7191556Srgrimes			INTOFF;
7201556Srgrimes			if (*(strchr(vp->text, '=') + 1) != '\0')
7211556Srgrimes				setvar(s, nullstr, 0);
72217525Sache			if ((vp->flags & VEXPORT) && localevar(vp->text)) {
72317525Sache				unsetenv(s);
72417525Sache				setlocale(LC_ALL, "");
72517525Sache			}
72620425Ssteve			vp->flags &= ~VEXPORT;
7271556Srgrimes			vp->flags |= VUNSET;
7281556Srgrimes			if ((vp->flags & VSTRFIXED) == 0) {
7291556Srgrimes				if ((vp->flags & VTEXTFIXED) == 0)
7301556Srgrimes					ckfree(vp->text);
7311556Srgrimes				*vpp = vp->next;
7321556Srgrimes				ckfree(vp);
7331556Srgrimes			}
7341556Srgrimes			INTON;
7351556Srgrimes			return (0);
7361556Srgrimes		}
7371556Srgrimes	}
7381556Srgrimes
7391556Srgrimes	return (1);
7401556Srgrimes}
7411556Srgrimes
7421556Srgrimes
7431556Srgrimes
7441556Srgrimes/*
7451556Srgrimes * Find the appropriate entry in the hash table from the name.
7461556Srgrimes */
7471556Srgrimes
7481556SrgrimesSTATIC struct var **
74990111Simphashvar(char *p)
75090111Simp{
7511556Srgrimes	unsigned int hashval;
7521556Srgrimes
75320425Ssteve	hashval = ((unsigned char) *p) << 4;
7541556Srgrimes	while (*p && *p != '=')
75520425Ssteve		hashval += (unsigned char) *p++;
7561556Srgrimes	return &vartab[hashval % VTABSIZE];
7571556Srgrimes}
7581556Srgrimes
7591556Srgrimes
7601556Srgrimes
7611556Srgrimes/*
7621556Srgrimes * Returns true if the two strings specify the same varable.  The first
7631556Srgrimes * variable name is terminated by '='; the second may be terminated by
7641556Srgrimes * either '=' or '\0'.
7651556Srgrimes */
7661556Srgrimes
7671556SrgrimesSTATIC int
76890111Simpvarequal(char *p, char *q)
76990111Simp{
7701556Srgrimes	while (*p == *q++) {
7711556Srgrimes		if (*p++ == '=')
7721556Srgrimes			return 1;
7731556Srgrimes	}
7741556Srgrimes	if (*p == '=' && *(q - 1) == '\0')
7751556Srgrimes		return 1;
7761556Srgrimes	return 0;
7771556Srgrimes}
778