xref: /freebsd/bin/sh/var.c (revision 2cac6e364a22fbb88eff4a1d36a09fef0064b421)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
41aa9caaf6SPeter Wemm #include <unistd.h>
42aa9caaf6SPeter Wemm #include <stdlib.h>
438d5c19ffSDavid E. O'Brien #include <paths.h>
44aa9caaf6SPeter Wemm 
454b88c807SRodney W. Grimes /*
464b88c807SRodney W. Grimes  * Shell variables.
474b88c807SRodney W. Grimes  */
484b88c807SRodney W. Grimes 
49ba726b8aSAndrey A. Chernov #include <locale.h>
50ba726b8aSAndrey A. Chernov 
514b88c807SRodney W. Grimes #include "shell.h"
524b88c807SRodney W. Grimes #include "output.h"
534b88c807SRodney W. Grimes #include "expand.h"
544b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
554b88c807SRodney W. Grimes #include "eval.h"	/* defines cmdenviron */
564b88c807SRodney W. Grimes #include "exec.h"
574b88c807SRodney W. Grimes #include "syntax.h"
584b88c807SRodney W. Grimes #include "options.h"
594b88c807SRodney W. Grimes #include "mail.h"
604b88c807SRodney W. Grimes #include "var.h"
614b88c807SRodney W. Grimes #include "memalloc.h"
624b88c807SRodney W. Grimes #include "error.h"
634b88c807SRodney W. Grimes #include "mystring.h"
64ab0a2172SSteve Price #include "parser.h"
65aa9caaf6SPeter Wemm #ifndef NO_HISTORY
66aa9caaf6SPeter Wemm #include "myhistedit.h"
67aa9caaf6SPeter Wemm #endif
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes #define VTABSIZE 39
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes struct varinit {
744b88c807SRodney W. Grimes 	struct var *var;
754b88c807SRodney W. Grimes 	int flags;
764b88c807SRodney W. Grimes 	char *text;
775134c3f7SWarner Losh 	void (*func)(const char *);
784b88c807SRodney W. Grimes };
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes 
81aa9caaf6SPeter Wemm #ifndef NO_HISTORY
824b88c807SRodney W. Grimes struct var vhistsize;
83aa9caaf6SPeter Wemm #endif
844b88c807SRodney W. Grimes struct var vifs;
854b88c807SRodney W. Grimes struct var vmail;
864b88c807SRodney W. Grimes struct var vmpath;
874b88c807SRodney W. Grimes struct var vpath;
8839dccc6fSTim J. Robbins struct var vppid;
894b88c807SRodney W. Grimes struct var vps1;
904b88c807SRodney W. Grimes struct var vps2;
91120c8e6cSStefan Farfeleder struct var vps4;
924b88c807SRodney W. Grimes struct var vvers;
932ba1b30bSDiomidis Spinellis STATIC struct var voptind;
944b88c807SRodney W. Grimes 
952ba1b30bSDiomidis Spinellis STATIC const struct varinit varinit[] = {
96aa9caaf6SPeter Wemm #ifndef NO_HISTORY
97ab0a2172SSteve Price 	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
98ab0a2172SSteve Price 	  sethistsize },
99aa9caaf6SPeter Wemm #endif
100ab0a2172SSteve Price 	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
101ab0a2172SSteve Price 	  NULL },
102ab0a2172SSteve Price 	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
103ab0a2172SSteve Price 	  NULL },
104ab0a2172SSteve Price 	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
105ab0a2172SSteve Price 	  NULL },
1068d5c19ffSDavid E. O'Brien 	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
107ab0a2172SSteve Price 	  changepath },
10839dccc6fSTim J. Robbins 	{ &vppid,	VSTRFIXED|VTEXTFIXED|VUNSET,	"PPID=",
10939dccc6fSTim J. Robbins 	  NULL },
1104b88c807SRodney W. Grimes 	/*
1114b88c807SRodney W. Grimes 	 * vps1 depends on uid
1124b88c807SRodney W. Grimes 	 */
113ab0a2172SSteve Price 	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
114ab0a2172SSteve Price 	  NULL },
115120c8e6cSStefan Farfeleder 	{ &vps4,	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",
116120c8e6cSStefan Farfeleder 	  NULL },
117ab0a2172SSteve Price 	{ &voptind,	VSTRFIXED|VTEXTFIXED,		"OPTIND=1",
118ab0a2172SSteve Price 	  getoptsreset },
119ab0a2172SSteve Price 	{ NULL,	0,				NULL,
120ab0a2172SSteve Price 	  NULL }
1214b88c807SRodney W. Grimes };
1224b88c807SRodney W. Grimes 
1232ba1b30bSDiomidis Spinellis STATIC struct var *vartab[VTABSIZE];
1244b88c807SRodney W. Grimes 
1252cac6e36SJilles Tjoelker STATIC struct var **hashvar(const char *);
1262cac6e36SJilles Tjoelker STATIC int varequal(const char *, const char *);
1272cac6e36SJilles Tjoelker STATIC int localevar(const char *);
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes /*
130b3decf89SJens Schweikhardt  * Initialize the variable symbol tables and import the environment.
1314b88c807SRodney W. Grimes  */
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes #ifdef mkinit
1344b88c807SRodney W. Grimes INCLUDE "var.h"
1354b88c807SRodney W. Grimes INIT {
1364b88c807SRodney W. Grimes 	char **envp;
1374b88c807SRodney W. Grimes 	extern char **environ;
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes 	initvar();
1404b88c807SRodney W. Grimes 	for (envp = environ ; *envp ; envp++) {
1414b88c807SRodney W. Grimes 		if (strchr(*envp, '=')) {
1424b88c807SRodney W. Grimes 			setvareq(*envp, VEXPORT|VTEXTFIXED);
1434b88c807SRodney W. Grimes 		}
1444b88c807SRodney W. Grimes 	}
1454b88c807SRodney W. Grimes }
1464b88c807SRodney W. Grimes #endif
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes /*
1504b88c807SRodney W. Grimes  * This routine initializes the builtin variables.  It is called when the
1514b88c807SRodney W. Grimes  * shell is initialized and again when a shell procedure is spawned.
1524b88c807SRodney W. Grimes  */
1534b88c807SRodney W. Grimes 
1544b88c807SRodney W. Grimes void
1555134c3f7SWarner Losh initvar(void)
1565134c3f7SWarner Losh {
15739dccc6fSTim J. Robbins 	char ppid[20];
1584b88c807SRodney W. Grimes 	const struct varinit *ip;
1594b88c807SRodney W. Grimes 	struct var *vp;
1604b88c807SRodney W. Grimes 	struct var **vpp;
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1634b88c807SRodney W. Grimes 		if ((vp->flags & VEXPORT) == 0) {
1644b88c807SRodney W. Grimes 			vpp = hashvar(ip->text);
1654b88c807SRodney W. Grimes 			vp->next = *vpp;
1664b88c807SRodney W. Grimes 			*vpp = vp;
1674b88c807SRodney W. Grimes 			vp->text = ip->text;
1684b88c807SRodney W. Grimes 			vp->flags = ip->flags;
169ab0a2172SSteve Price 			vp->func = ip->func;
1704b88c807SRodney W. Grimes 		}
1714b88c807SRodney W. Grimes 	}
1724b88c807SRodney W. Grimes 	/*
1734b88c807SRodney W. Grimes 	 * PS1 depends on uid
1744b88c807SRodney W. Grimes 	 */
1754b88c807SRodney W. Grimes 	if ((vps1.flags & VEXPORT) == 0) {
1764b88c807SRodney W. Grimes 		vpp = hashvar("PS1=");
1774b88c807SRodney W. Grimes 		vps1.next = *vpp;
1784b88c807SRodney W. Grimes 		*vpp = &vps1;
1794b88c807SRodney W. Grimes 		vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
1804b88c807SRodney W. Grimes 		vps1.flags = VSTRFIXED|VTEXTFIXED;
1814b88c807SRodney W. Grimes 	}
18239dccc6fSTim J. Robbins 	if ((vppid.flags & VEXPORT) == 0) {
18339dccc6fSTim J. Robbins 		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
18439dccc6fSTim J. Robbins 		setvarsafe("PPID", ppid, 0);
18539dccc6fSTim J. Robbins 	}
1864b88c807SRodney W. Grimes }
1874b88c807SRodney W. Grimes 
1884b88c807SRodney W. Grimes /*
189ab0a2172SSteve Price  * Safe version of setvar, returns 1 on success 0 on failure.
190ab0a2172SSteve Price  */
191ab0a2172SSteve Price 
192ab0a2172SSteve Price int
1932cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags)
194ab0a2172SSteve Price {
195ab0a2172SSteve Price 	struct jmploc jmploc;
196224fbf9fSJilles Tjoelker 	struct jmploc *const savehandler = handler;
197ab0a2172SSteve Price 	int err = 0;
1989922c6d2SJilles Tjoelker 	int inton;
199ab0a2172SSteve Price 
2009922c6d2SJilles Tjoelker 	inton = is_int_on();
201ab0a2172SSteve Price 	if (setjmp(jmploc.loc))
202ab0a2172SSteve Price 		err = 1;
203ab0a2172SSteve Price 	else {
204ab0a2172SSteve Price 		handler = &jmploc;
205ab0a2172SSteve Price 		setvar(name, val, flags);
206ab0a2172SSteve Price 	}
207ab0a2172SSteve Price 	handler = savehandler;
2089922c6d2SJilles Tjoelker 	SETINTON(inton);
209ab0a2172SSteve Price 	return err;
210ab0a2172SSteve Price }
211ab0a2172SSteve Price 
212ab0a2172SSteve Price /*
213b3decf89SJens Schweikhardt  * Set the value of a variable.  The flags argument is stored with the
2144b88c807SRodney W. Grimes  * flags of the variable.  If val is NULL, the variable is unset.
2154b88c807SRodney W. Grimes  */
2164b88c807SRodney W. Grimes 
2174b88c807SRodney W. Grimes void
2182cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags)
2194b88c807SRodney W. Grimes {
2202cac6e36SJilles Tjoelker 	const char *p;
2214b88c807SRodney W. Grimes 	int len;
2224b88c807SRodney W. Grimes 	int namelen;
2234b88c807SRodney W. Grimes 	char *nameeq;
2244b88c807SRodney W. Grimes 	int isbad;
2254b88c807SRodney W. Grimes 
2264b88c807SRodney W. Grimes 	isbad = 0;
2274b88c807SRodney W. Grimes 	p = name;
228ba726b8aSAndrey A. Chernov 	if (! is_name(*p))
2294b88c807SRodney W. Grimes 		isbad = 1;
230ba726b8aSAndrey A. Chernov 	p++;
2314b88c807SRodney W. Grimes 	for (;;) {
2324b88c807SRodney W. Grimes 		if (! is_in_name(*p)) {
2334b88c807SRodney W. Grimes 			if (*p == '\0' || *p == '=')
2344b88c807SRodney W. Grimes 				break;
2354b88c807SRodney W. Grimes 			isbad = 1;
2364b88c807SRodney W. Grimes 		}
2374b88c807SRodney W. Grimes 		p++;
2384b88c807SRodney W. Grimes 	}
2394b88c807SRodney W. Grimes 	namelen = p - name;
2404b88c807SRodney W. Grimes 	if (isbad)
2414b88c807SRodney W. Grimes 		error("%.*s: bad variable name", namelen, name);
2424b88c807SRodney W. Grimes 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2434b88c807SRodney W. Grimes 	if (val == NULL) {
2444b88c807SRodney W. Grimes 		flags |= VUNSET;
2454b88c807SRodney W. Grimes 	} else {
2464b88c807SRodney W. Grimes 		len += strlen(val);
2474b88c807SRodney W. Grimes 	}
2482cac6e36SJilles Tjoelker 	nameeq = ckmalloc(len);
2492cac6e36SJilles Tjoelker 	memcpy(nameeq, name, namelen);
2502cac6e36SJilles Tjoelker 	nameeq[namelen] = '=';
2514b88c807SRodney W. Grimes 	if (val)
2522cac6e36SJilles Tjoelker 		scopy(val, nameeq + namelen + 1);
2532cac6e36SJilles Tjoelker 	else
2542cac6e36SJilles Tjoelker 		nameeq[namelen + 1] = '\0';
2554b88c807SRodney W. Grimes 	setvareq(nameeq, flags);
2564b88c807SRodney W. Grimes }
2574b88c807SRodney W. Grimes 
258ba726b8aSAndrey A. Chernov STATIC int
2592cac6e36SJilles Tjoelker localevar(const char *s)
260ba726b8aSAndrey A. Chernov {
261ba726b8aSAndrey A. Chernov 	static char *lnames[7] = {
262ba726b8aSAndrey A. Chernov 		"ALL", "COLLATE", "CTYPE", "MONETARY",
263ba726b8aSAndrey A. Chernov 		"NUMERIC", "TIME", NULL
264ba726b8aSAndrey A. Chernov 	};
265ba726b8aSAndrey A. Chernov 	char **ss;
2664b88c807SRodney W. Grimes 
267ba726b8aSAndrey A. Chernov 	if (*s != 'L')
268ba726b8aSAndrey A. Chernov 		return 0;
269ba726b8aSAndrey A. Chernov 	if (varequal(s + 1, "ANG"))
270ba726b8aSAndrey A. Chernov 		return 1;
271ba726b8aSAndrey A. Chernov 	if (strncmp(s + 1, "C_", 2) != 0)
272ba726b8aSAndrey A. Chernov 		return 0;
273ba726b8aSAndrey A. Chernov 	for (ss = lnames; *ss ; ss++)
274ba726b8aSAndrey A. Chernov 		if (varequal(s + 3, *ss))
275ba726b8aSAndrey A. Chernov 			return 1;
276ba726b8aSAndrey A. Chernov 	return 0;
277ba726b8aSAndrey A. Chernov }
2784b88c807SRodney W. Grimes 
2795b78c6c2SSean Farley 
2805b78c6c2SSean Farley /*
2815b78c6c2SSean Farley  * Sets/unsets an environment variable from a pointer that may actually be a
2825b78c6c2SSean Farley  * pointer into environ where the string should not be manipulated.
2835b78c6c2SSean Farley  */
2845b78c6c2SSean Farley static void
2852cac6e36SJilles Tjoelker change_env(const char *s, int set)
2865b78c6c2SSean Farley {
2875b78c6c2SSean Farley 	char *eqp;
2885b78c6c2SSean Farley 	char *ss;
2895b78c6c2SSean Farley 
2905b78c6c2SSean Farley 	ss = savestr(s);
2915b78c6c2SSean Farley 	if ((eqp = strchr(ss, '=')) != NULL)
2925b78c6c2SSean Farley 		*eqp = '\0';
2935b78c6c2SSean Farley 	if (set && eqp != NULL)
2945b78c6c2SSean Farley 		(void) setenv(ss, eqp + 1, 1);
2955b78c6c2SSean Farley 	else
2965b78c6c2SSean Farley 		(void) unsetenv(ss);
2975b78c6c2SSean Farley 	ckfree(ss);
2985b78c6c2SSean Farley 
2995b78c6c2SSean Farley 	return;
3005b78c6c2SSean Farley }
3015b78c6c2SSean Farley 
3025b78c6c2SSean Farley 
3034b88c807SRodney W. Grimes /*
3044b88c807SRodney W. Grimes  * Same as setvar except that the variable and value are passed in
3054b88c807SRodney W. Grimes  * the first argument as name=value.  Since the first argument will
3064b88c807SRodney W. Grimes  * be actually stored in the table, it should not be a string that
3074b88c807SRodney W. Grimes  * will go away.
3084b88c807SRodney W. Grimes  */
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes void
3115134c3f7SWarner Losh setvareq(char *s, int flags)
3124b88c807SRodney W. Grimes {
3134b88c807SRodney W. Grimes 	struct var *vp, **vpp;
31490e41fc4SWarner Losh 	int len;
3154b88c807SRodney W. Grimes 
316b8ec435eSMartin Cracauer 	if (aflag)
317b8ec435eSMartin Cracauer 		flags |= VEXPORT;
3184b88c807SRodney W. Grimes 	vpp = hashvar(s);
3194b88c807SRodney W. Grimes 	for (vp = *vpp ; vp ; vp = vp->next) {
3204b88c807SRodney W. Grimes 		if (varequal(s, vp->text)) {
3214b88c807SRodney W. Grimes 			if (vp->flags & VREADONLY) {
32290e41fc4SWarner Losh 				len = strchr(s, '=') - s;
3234b88c807SRodney W. Grimes 				error("%.*s: is read only", len, s);
3244b88c807SRodney W. Grimes 			}
3254b88c807SRodney W. Grimes 			INTOFF;
326ab0a2172SSteve Price 
327ab0a2172SSteve Price 			if (vp->func && (flags & VNOFUNC) == 0)
328ab0a2172SSteve Price 				(*vp->func)(strchr(s, '=') + 1);
329ab0a2172SSteve Price 
3304b88c807SRodney W. Grimes 			if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3314b88c807SRodney W. Grimes 				ckfree(vp->text);
332ab0a2172SSteve Price 
3334b88c807SRodney W. Grimes 			vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3344b88c807SRodney W. Grimes 			vp->flags |= flags;
3354b88c807SRodney W. Grimes 			vp->text = s;
336ab0a2172SSteve Price 
337ab0a2172SSteve Price 			/*
338ab0a2172SSteve Price 			 * We could roll this to a function, to handle it as
339ab0a2172SSteve Price 			 * a regular variable function callback, but why bother?
340ab0a2172SSteve Price 			 */
3414b88c807SRodney W. Grimes 			if (vp == &vmpath || (vp == &vmail && ! mpathset()))
3424b88c807SRodney W. Grimes 				chkmail(1);
343ba726b8aSAndrey A. Chernov 			if ((vp->flags & VEXPORT) && localevar(s)) {
3445b78c6c2SSean Farley 				change_env(s, 1);
345ba726b8aSAndrey A. Chernov 				(void) setlocale(LC_ALL, "");
346ba726b8aSAndrey A. Chernov 			}
3474b88c807SRodney W. Grimes 			INTON;
3484b88c807SRodney W. Grimes 			return;
3494b88c807SRodney W. Grimes 		}
3504b88c807SRodney W. Grimes 	}
3514b88c807SRodney W. Grimes 	/* not found */
3524b88c807SRodney W. Grimes 	vp = ckmalloc(sizeof (*vp));
3534b88c807SRodney W. Grimes 	vp->flags = flags;
3544b88c807SRodney W. Grimes 	vp->text = s;
3554b88c807SRodney W. Grimes 	vp->next = *vpp;
356ab0a2172SSteve Price 	vp->func = NULL;
357ba726b8aSAndrey A. Chernov 	INTOFF;
3584b88c807SRodney W. Grimes 	*vpp = vp;
359ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(s)) {
3605b78c6c2SSean Farley 		change_env(s, 1);
361ba726b8aSAndrey A. Chernov 		(void) setlocale(LC_ALL, "");
362ba726b8aSAndrey A. Chernov 	}
363ba726b8aSAndrey A. Chernov 	INTON;
3644b88c807SRodney W. Grimes }
3654b88c807SRodney W. Grimes 
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes 
3684b88c807SRodney W. Grimes /*
3694b88c807SRodney W. Grimes  * Process a linked list of variable assignments.
3704b88c807SRodney W. Grimes  */
3714b88c807SRodney W. Grimes 
3724b88c807SRodney W. Grimes void
3735134c3f7SWarner Losh listsetvar(struct strlist *list)
3744b88c807SRodney W. Grimes {
3754b88c807SRodney W. Grimes 	struct strlist *lp;
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes 	INTOFF;
3784b88c807SRodney W. Grimes 	for (lp = list ; lp ; lp = lp->next) {
3794b88c807SRodney W. Grimes 		setvareq(savestr(lp->text), 0);
3804b88c807SRodney W. Grimes 	}
3814b88c807SRodney W. Grimes 	INTON;
3824b88c807SRodney W. Grimes }
3834b88c807SRodney W. Grimes 
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes 
3864b88c807SRodney W. Grimes /*
3874b88c807SRodney W. Grimes  * Find the value of a variable.  Returns NULL if not set.
3884b88c807SRodney W. Grimes  */
3894b88c807SRodney W. Grimes 
3904b88c807SRodney W. Grimes char *
3912cac6e36SJilles Tjoelker lookupvar(const char *name)
3924b88c807SRodney W. Grimes {
3934b88c807SRodney W. Grimes 	struct var *v;
3944b88c807SRodney W. Grimes 
3954b88c807SRodney W. Grimes 	for (v = *hashvar(name) ; v ; v = v->next) {
3964b88c807SRodney W. Grimes 		if (varequal(v->text, name)) {
3974b88c807SRodney W. Grimes 			if (v->flags & VUNSET)
3984b88c807SRodney W. Grimes 				return NULL;
3994b88c807SRodney W. Grimes 			return strchr(v->text, '=') + 1;
4004b88c807SRodney W. Grimes 		}
4014b88c807SRodney W. Grimes 	}
4024b88c807SRodney W. Grimes 	return NULL;
4034b88c807SRodney W. Grimes }
4044b88c807SRodney W. Grimes 
4054b88c807SRodney W. Grimes 
4064b88c807SRodney W. Grimes 
4074b88c807SRodney W. Grimes /*
4084b88c807SRodney W. Grimes  * Search the environment of a builtin command.  If the second argument
4094b88c807SRodney W. Grimes  * is nonzero, return the value of a variable even if it hasn't been
4104b88c807SRodney W. Grimes  * exported.
4114b88c807SRodney W. Grimes  */
4124b88c807SRodney W. Grimes 
4134b88c807SRodney W. Grimes char *
4142cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall)
4154b88c807SRodney W. Grimes {
4164b88c807SRodney W. Grimes 	struct strlist *sp;
4174b88c807SRodney W. Grimes 	struct var *v;
4184b88c807SRodney W. Grimes 
4194b88c807SRodney W. Grimes 	for (sp = cmdenviron ; sp ; sp = sp->next) {
4204b88c807SRodney W. Grimes 		if (varequal(sp->text, name))
4214b88c807SRodney W. Grimes 			return strchr(sp->text, '=') + 1;
4224b88c807SRodney W. Grimes 	}
4234b88c807SRodney W. Grimes 	for (v = *hashvar(name) ; v ; v = v->next) {
4244b88c807SRodney W. Grimes 		if (varequal(v->text, name)) {
425aa9caaf6SPeter Wemm 			if ((v->flags & VUNSET)
426aa9caaf6SPeter Wemm 			 || (!doall && (v->flags & VEXPORT) == 0))
4274b88c807SRodney W. Grimes 				return NULL;
4284b88c807SRodney W. Grimes 			return strchr(v->text, '=') + 1;
4294b88c807SRodney W. Grimes 		}
4304b88c807SRodney W. Grimes 	}
4314b88c807SRodney W. Grimes 	return NULL;
4324b88c807SRodney W. Grimes }
4334b88c807SRodney W. Grimes 
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes /*
4374b88c807SRodney W. Grimes  * Generate a list of exported variables.  This routine is used to construct
4384b88c807SRodney W. Grimes  * the third argument to execve when executing a program.
4394b88c807SRodney W. Grimes  */
4404b88c807SRodney W. Grimes 
4414b88c807SRodney W. Grimes char **
4425134c3f7SWarner Losh environment(void)
4435134c3f7SWarner Losh {
4444b88c807SRodney W. Grimes 	int nenv;
4454b88c807SRodney W. Grimes 	struct var **vpp;
4464b88c807SRodney W. Grimes 	struct var *vp;
4474b88c807SRodney W. Grimes 	char **env, **ep;
4484b88c807SRodney W. Grimes 
4494b88c807SRodney W. Grimes 	nenv = 0;
4504b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4514b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
4524b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
4534b88c807SRodney W. Grimes 				nenv++;
4544b88c807SRodney W. Grimes 	}
4554b88c807SRodney W. Grimes 	ep = env = stalloc((nenv + 1) * sizeof *env);
4564b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4574b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
4584b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
4594b88c807SRodney W. Grimes 				*ep++ = vp->text;
4604b88c807SRodney W. Grimes 	}
4614b88c807SRodney W. Grimes 	*ep = NULL;
4624b88c807SRodney W. Grimes 	return env;
4634b88c807SRodney W. Grimes }
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 
4664b88c807SRodney W. Grimes /*
4674b88c807SRodney W. Grimes  * Called when a shell procedure is invoked to clear out nonexported
4684b88c807SRodney W. Grimes  * variables.  It is also necessary to reallocate variables of with
4694b88c807SRodney W. Grimes  * VSTACK set since these are currently allocated on the stack.
4704b88c807SRodney W. Grimes  */
4714b88c807SRodney W. Grimes 
4724b88c807SRodney W. Grimes #ifdef mkinit
473c7893739SStefan Farfeleder MKINIT void shprocvar(void);
4744b88c807SRodney W. Grimes 
4754b88c807SRodney W. Grimes SHELLPROC {
4764b88c807SRodney W. Grimes 	shprocvar();
4774b88c807SRodney W. Grimes }
4784b88c807SRodney W. Grimes #endif
4794b88c807SRodney W. Grimes 
4804b88c807SRodney W. Grimes void
4815134c3f7SWarner Losh shprocvar(void)
4825134c3f7SWarner Losh {
4834b88c807SRodney W. Grimes 	struct var **vpp;
4844b88c807SRodney W. Grimes 	struct var *vp, **prev;
4854b88c807SRodney W. Grimes 
4864b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4874b88c807SRodney W. Grimes 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
4884b88c807SRodney W. Grimes 			if ((vp->flags & VEXPORT) == 0) {
4894b88c807SRodney W. Grimes 				*prev = vp->next;
4904b88c807SRodney W. Grimes 				if ((vp->flags & VTEXTFIXED) == 0)
4914b88c807SRodney W. Grimes 					ckfree(vp->text);
4924b88c807SRodney W. Grimes 				if ((vp->flags & VSTRFIXED) == 0)
4934b88c807SRodney W. Grimes 					ckfree(vp);
4944b88c807SRodney W. Grimes 			} else {
4954b88c807SRodney W. Grimes 				if (vp->flags & VSTACK) {
4964b88c807SRodney W. Grimes 					vp->text = savestr(vp->text);
4974b88c807SRodney W. Grimes 					vp->flags &=~ VSTACK;
4984b88c807SRodney W. Grimes 				}
4994b88c807SRodney W. Grimes 				prev = &vp->next;
5004b88c807SRodney W. Grimes 			}
5014b88c807SRodney W. Grimes 		}
5024b88c807SRodney W. Grimes 	}
5034b88c807SRodney W. Grimes 	initvar();
5044b88c807SRodney W. Grimes }
5054b88c807SRodney W. Grimes 
5064b88c807SRodney W. Grimes 
507692f35fdSStefan Farfeleder static int
508692f35fdSStefan Farfeleder var_compare(const void *a, const void *b)
509692f35fdSStefan Farfeleder {
510692f35fdSStefan Farfeleder 	const char *const *sa, *const *sb;
511692f35fdSStefan Farfeleder 
512692f35fdSStefan Farfeleder 	sa = a;
513692f35fdSStefan Farfeleder 	sb = b;
514692f35fdSStefan Farfeleder 	/*
515692f35fdSStefan Farfeleder 	 * This compares two var=value strings which creates a different
516692f35fdSStefan Farfeleder 	 * order from what you would probably expect.  POSIX is somewhat
517692f35fdSStefan Farfeleder 	 * ambiguous on what should be sorted exactly.
518692f35fdSStefan Farfeleder 	 */
519692f35fdSStefan Farfeleder 	return strcoll(*sa, *sb);
520692f35fdSStefan Farfeleder }
521692f35fdSStefan Farfeleder 
5224b88c807SRodney W. Grimes 
5234b88c807SRodney W. Grimes /*
5244b88c807SRodney W. Grimes  * Command to list all variables which are set.  Currently this command
5254b88c807SRodney W. Grimes  * is invoked from the set command when the set command is called without
5264b88c807SRodney W. Grimes  * any variables.
5274b88c807SRodney W. Grimes  */
5284b88c807SRodney W. Grimes 
5294b88c807SRodney W. Grimes int
5305134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused)
531aa9caaf6SPeter Wemm {
5324b88c807SRodney W. Grimes 	struct var **vpp;
5334b88c807SRodney W. Grimes 	struct var *vp;
53459258844STim J. Robbins 	const char *s;
535692f35fdSStefan Farfeleder 	const char **vars;
536692f35fdSStefan Farfeleder 	int i, n;
5374b88c807SRodney W. Grimes 
538692f35fdSStefan Farfeleder 	/*
539692f35fdSStefan Farfeleder 	 * POSIX requires us to sort the variables.
540692f35fdSStefan Farfeleder 	 */
541692f35fdSStefan Farfeleder 	n = 0;
5424b88c807SRodney W. Grimes 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
5434b88c807SRodney W. Grimes 		for (vp = *vpp; vp; vp = vp->next) {
544692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
545692f35fdSStefan Farfeleder 				n++;
546692f35fdSStefan Farfeleder 		}
547692f35fdSStefan Farfeleder 	}
548692f35fdSStefan Farfeleder 
549692f35fdSStefan Farfeleder 	INTON;
550692f35fdSStefan Farfeleder 	vars = ckmalloc(n * sizeof(*vars));
551692f35fdSStefan Farfeleder 	i = 0;
552692f35fdSStefan Farfeleder 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
553692f35fdSStefan Farfeleder 		for (vp = *vpp; vp; vp = vp->next) {
554692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
555692f35fdSStefan Farfeleder 				vars[i++] = vp->text;
556692f35fdSStefan Farfeleder 		}
557692f35fdSStefan Farfeleder 	}
558692f35fdSStefan Farfeleder 
559692f35fdSStefan Farfeleder 	qsort(vars, n, sizeof(*vars), var_compare);
560692f35fdSStefan Farfeleder 	for (i = 0; i < n; i++) {
561692f35fdSStefan Farfeleder 		for (s = vars[i]; *s != '='; s++)
56259258844STim J. Robbins 			out1c(*s);
56359258844STim J. Robbins 		out1c('=');
56459258844STim J. Robbins 		out1qstr(s + 1);
56559258844STim J. Robbins 		out1c('\n');
5664b88c807SRodney W. Grimes 	}
567692f35fdSStefan Farfeleder 	ckfree(vars);
568692f35fdSStefan Farfeleder 	INTOFF;
569692f35fdSStefan Farfeleder 
5704b88c807SRodney W. Grimes 	return 0;
5714b88c807SRodney W. Grimes }
5724b88c807SRodney W. Grimes 
5734b88c807SRodney W. Grimes 
5744b88c807SRodney W. Grimes 
5754b88c807SRodney W. Grimes /*
5764b88c807SRodney W. Grimes  * The export and readonly commands.
5774b88c807SRodney W. Grimes  */
5784b88c807SRodney W. Grimes 
5794b88c807SRodney W. Grimes int
5805134c3f7SWarner Losh exportcmd(int argc, char **argv)
581aa9caaf6SPeter Wemm {
5824b88c807SRodney W. Grimes 	struct var **vpp;
5834b88c807SRodney W. Grimes 	struct var *vp;
5844b88c807SRodney W. Grimes 	char *name;
5854b88c807SRodney W. Grimes 	char *p;
58645086f8cSTim J. Robbins 	char *cmdname;
58745086f8cSTim J. Robbins 	int ch, values;
5884b88c807SRodney W. Grimes 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
5894b88c807SRodney W. Grimes 
59045086f8cSTim J. Robbins 	cmdname = argv[0];
59145086f8cSTim J. Robbins 	optreset = optind = 1;
592050f7913STim J. Robbins 	opterr = 0;
59345086f8cSTim J. Robbins 	values = 0;
59445086f8cSTim J. Robbins 	while ((ch = getopt(argc, argv, "p")) != -1) {
59545086f8cSTim J. Robbins 		switch (ch) {
59645086f8cSTim J. Robbins 		case 'p':
59745086f8cSTim J. Robbins 			values = 1;
59845086f8cSTim J. Robbins 			break;
59945086f8cSTim J. Robbins 		case '?':
60045086f8cSTim J. Robbins 		default:
60145086f8cSTim J. Robbins 			error("unknown option: -%c", optopt);
60245086f8cSTim J. Robbins 		}
60345086f8cSTim J. Robbins 	}
60445086f8cSTim J. Robbins 	argc -= optind;
60545086f8cSTim J. Robbins 	argv += optind;
60645086f8cSTim J. Robbins 
6073fda45ecSStefan Farfeleder 	if (values && argc != 0)
6083fda45ecSStefan Farfeleder 		error("-p requires no arguments");
60945086f8cSTim J. Robbins 	if (argc != 0) {
6103fda45ecSStefan Farfeleder 		while ((name = *argv++) != NULL) {
6114b88c807SRodney W. Grimes 			if ((p = strchr(name, '=')) != NULL) {
6124b88c807SRodney W. Grimes 				p++;
6134b88c807SRodney W. Grimes 			} else {
6144b88c807SRodney W. Grimes 				vpp = hashvar(name);
6154b88c807SRodney W. Grimes 				for (vp = *vpp ; vp ; vp = vp->next) {
6164b88c807SRodney W. Grimes 					if (varequal(vp->text, name)) {
6175134c3f7SWarner Losh 
6184b88c807SRodney W. Grimes 						vp->flags |= flag;
619ba726b8aSAndrey A. Chernov 						if ((vp->flags & VEXPORT) && localevar(vp->text)) {
6205b78c6c2SSean Farley 							change_env(vp->text, 1);
621ba726b8aSAndrey A. Chernov 							(void) setlocale(LC_ALL, "");
622ba726b8aSAndrey A. Chernov 						}
6234b88c807SRodney W. Grimes 						goto found;
6244b88c807SRodney W. Grimes 					}
6254b88c807SRodney W. Grimes 				}
6264b88c807SRodney W. Grimes 			}
6274b88c807SRodney W. Grimes 			setvar(name, p, flag);
6284b88c807SRodney W. Grimes found:;
6294b88c807SRodney W. Grimes 		}
6304b88c807SRodney W. Grimes 	} else {
6314b88c807SRodney W. Grimes 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6324b88c807SRodney W. Grimes 			for (vp = *vpp ; vp ; vp = vp->next) {
6334b88c807SRodney W. Grimes 				if (vp->flags & flag) {
63445086f8cSTim J. Robbins 					if (values) {
63545086f8cSTim J. Robbins 						out1str(cmdname);
63645086f8cSTim J. Robbins 						out1c(' ');
63745086f8cSTim J. Robbins 					}
6384b88c807SRodney W. Grimes 					for (p = vp->text ; *p != '=' ; p++)
6394b88c807SRodney W. Grimes 						out1c(*p);
64045086f8cSTim J. Robbins 					if (values && !(vp->flags & VUNSET)) {
64145086f8cSTim J. Robbins 						out1c('=');
64245086f8cSTim J. Robbins 						out1qstr(p + 1);
64345086f8cSTim J. Robbins 					}
6444b88c807SRodney W. Grimes 					out1c('\n');
6454b88c807SRodney W. Grimes 				}
6464b88c807SRodney W. Grimes 			}
6474b88c807SRodney W. Grimes 		}
6484b88c807SRodney W. Grimes 	}
6494b88c807SRodney W. Grimes 	return 0;
6504b88c807SRodney W. Grimes }
6514b88c807SRodney W. Grimes 
6524b88c807SRodney W. Grimes 
6534b88c807SRodney W. Grimes /*
6544b88c807SRodney W. Grimes  * The "local" command.
6554b88c807SRodney W. Grimes  */
6564b88c807SRodney W. Grimes 
657aa9caaf6SPeter Wemm int
6585134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused)
659aa9caaf6SPeter Wemm {
6604b88c807SRodney W. Grimes 	char *name;
6614b88c807SRodney W. Grimes 
6624b88c807SRodney W. Grimes 	if (! in_function())
6634b88c807SRodney W. Grimes 		error("Not in a function");
6644b88c807SRodney W. Grimes 	while ((name = *argptr++) != NULL) {
6654b88c807SRodney W. Grimes 		mklocal(name);
6664b88c807SRodney W. Grimes 	}
6674b88c807SRodney W. Grimes 	return 0;
6684b88c807SRodney W. Grimes }
6694b88c807SRodney W. Grimes 
6704b88c807SRodney W. Grimes 
6714b88c807SRodney W. Grimes /*
6724b88c807SRodney W. Grimes  * Make a variable a local variable.  When a variable is made local, it's
6734b88c807SRodney W. Grimes  * value and flags are saved in a localvar structure.  The saved values
6744b88c807SRodney W. Grimes  * will be restored when the shell function returns.  We handle the name
6754b88c807SRodney W. Grimes  * "-" as a special case.
6764b88c807SRodney W. Grimes  */
6774b88c807SRodney W. Grimes 
6784b88c807SRodney W. Grimes void
6795134c3f7SWarner Losh mklocal(char *name)
6804b88c807SRodney W. Grimes {
6814b88c807SRodney W. Grimes 	struct localvar *lvp;
6824b88c807SRodney W. Grimes 	struct var **vpp;
6834b88c807SRodney W. Grimes 	struct var *vp;
6844b88c807SRodney W. Grimes 
6854b88c807SRodney W. Grimes 	INTOFF;
6864b88c807SRodney W. Grimes 	lvp = ckmalloc(sizeof (struct localvar));
6874b88c807SRodney W. Grimes 	if (name[0] == '-' && name[1] == '\0') {
6884b88c807SRodney W. Grimes 		lvp->text = ckmalloc(sizeof optlist);
689aa9caaf6SPeter Wemm 		memcpy(lvp->text, optlist, sizeof optlist);
6904b88c807SRodney W. Grimes 		vp = NULL;
6914b88c807SRodney W. Grimes 	} else {
6924b88c807SRodney W. Grimes 		vpp = hashvar(name);
6934b88c807SRodney W. Grimes 		for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
6944b88c807SRodney W. Grimes 		if (vp == NULL) {
6954b88c807SRodney W. Grimes 			if (strchr(name, '='))
6964b88c807SRodney W. Grimes 				setvareq(savestr(name), VSTRFIXED);
6974b88c807SRodney W. Grimes 			else
6984b88c807SRodney W. Grimes 				setvar(name, NULL, VSTRFIXED);
6994b88c807SRodney W. Grimes 			vp = *vpp;	/* the new variable */
7004b88c807SRodney W. Grimes 			lvp->text = NULL;
7014b88c807SRodney W. Grimes 			lvp->flags = VUNSET;
7024b88c807SRodney W. Grimes 		} else {
7034b88c807SRodney W. Grimes 			lvp->text = vp->text;
7044b88c807SRodney W. Grimes 			lvp->flags = vp->flags;
7054b88c807SRodney W. Grimes 			vp->flags |= VSTRFIXED|VTEXTFIXED;
7064b88c807SRodney W. Grimes 			if (strchr(name, '='))
7074b88c807SRodney W. Grimes 				setvareq(savestr(name), 0);
7084b88c807SRodney W. Grimes 		}
7094b88c807SRodney W. Grimes 	}
7104b88c807SRodney W. Grimes 	lvp->vp = vp;
7114b88c807SRodney W. Grimes 	lvp->next = localvars;
7124b88c807SRodney W. Grimes 	localvars = lvp;
7134b88c807SRodney W. Grimes 	INTON;
7144b88c807SRodney W. Grimes }
7154b88c807SRodney W. Grimes 
7164b88c807SRodney W. Grimes 
7174b88c807SRodney W. Grimes /*
7184b88c807SRodney W. Grimes  * Called after a function returns.
7194b88c807SRodney W. Grimes  */
7204b88c807SRodney W. Grimes 
7214b88c807SRodney W. Grimes void
7225134c3f7SWarner Losh poplocalvars(void)
7235134c3f7SWarner Losh {
7244b88c807SRodney W. Grimes 	struct localvar *lvp;
7254b88c807SRodney W. Grimes 	struct var *vp;
7264b88c807SRodney W. Grimes 
7274b88c807SRodney W. Grimes 	while ((lvp = localvars) != NULL) {
7284b88c807SRodney W. Grimes 		localvars = lvp->next;
7294b88c807SRodney W. Grimes 		vp = lvp->vp;
7304b88c807SRodney W. Grimes 		if (vp == NULL) {	/* $- saved */
731aa9caaf6SPeter Wemm 			memcpy(optlist, lvp->text, sizeof optlist);
7324b88c807SRodney W. Grimes 			ckfree(lvp->text);
7334b88c807SRodney W. Grimes 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
7344b88c807SRodney W. Grimes 			(void)unsetvar(vp->text);
7354b88c807SRodney W. Grimes 		} else {
7364b88c807SRodney W. Grimes 			if ((vp->flags & VTEXTFIXED) == 0)
7374b88c807SRodney W. Grimes 				ckfree(vp->text);
7384b88c807SRodney W. Grimes 			vp->flags = lvp->flags;
7394b88c807SRodney W. Grimes 			vp->text = lvp->text;
7404b88c807SRodney W. Grimes 		}
7414b88c807SRodney W. Grimes 		ckfree(lvp);
7424b88c807SRodney W. Grimes 	}
7434b88c807SRodney W. Grimes }
7444b88c807SRodney W. Grimes 
7454b88c807SRodney W. Grimes 
746aa9caaf6SPeter Wemm int
7475134c3f7SWarner Losh setvarcmd(int argc, char **argv)
748aa9caaf6SPeter Wemm {
7494b88c807SRodney W. Grimes 	if (argc <= 2)
7504b88c807SRodney W. Grimes 		return unsetcmd(argc, argv);
7514b88c807SRodney W. Grimes 	else if (argc == 3)
7524b88c807SRodney W. Grimes 		setvar(argv[1], argv[2], 0);
7534b88c807SRodney W. Grimes 	else
7544b88c807SRodney W. Grimes 		error("List assignment not implemented");
7554b88c807SRodney W. Grimes 	return 0;
7564b88c807SRodney W. Grimes }
7574b88c807SRodney W. Grimes 
7584b88c807SRodney W. Grimes 
7594b88c807SRodney W. Grimes /*
7604b88c807SRodney W. Grimes  * The unset builtin command.  We unset the function before we unset the
7614b88c807SRodney W. Grimes  * variable to allow a function to be unset when there is a readonly variable
7624b88c807SRodney W. Grimes  * with the same name.
7634b88c807SRodney W. Grimes  */
7644b88c807SRodney W. Grimes 
765aa9caaf6SPeter Wemm int
7665134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused)
767aa9caaf6SPeter Wemm {
7684b88c807SRodney W. Grimes 	char **ap;
7694b88c807SRodney W. Grimes 	int i;
7704b88c807SRodney W. Grimes 	int flg_func = 0;
7714b88c807SRodney W. Grimes 	int flg_var = 0;
7724b88c807SRodney W. Grimes 	int ret = 0;
7734b88c807SRodney W. Grimes 
7744b88c807SRodney W. Grimes 	while ((i = nextopt("vf")) != '\0') {
7754b88c807SRodney W. Grimes 		if (i == 'f')
7764b88c807SRodney W. Grimes 			flg_func = 1;
7774b88c807SRodney W. Grimes 		else
7784b88c807SRodney W. Grimes 			flg_var = 1;
7794b88c807SRodney W. Grimes 	}
7804b88c807SRodney W. Grimes 	if (flg_func == 0 && flg_var == 0)
7814b88c807SRodney W. Grimes 		flg_var = 1;
7824b88c807SRodney W. Grimes 
7834b88c807SRodney W. Grimes 	for (ap = argptr; *ap ; ap++) {
7844b88c807SRodney W. Grimes 		if (flg_func)
7854b88c807SRodney W. Grimes 			ret |= unsetfunc(*ap);
7864b88c807SRodney W. Grimes 		if (flg_var)
7874b88c807SRodney W. Grimes 			ret |= unsetvar(*ap);
7884b88c807SRodney W. Grimes 	}
7894b88c807SRodney W. Grimes 	return ret;
7904b88c807SRodney W. Grimes }
7914b88c807SRodney W. Grimes 
7924b88c807SRodney W. Grimes 
7934b88c807SRodney W. Grimes /*
7944b88c807SRodney W. Grimes  * Unset the specified variable.
7954b88c807SRodney W. Grimes  */
7964b88c807SRodney W. Grimes 
797ab0a2172SSteve Price int
7982cac6e36SJilles Tjoelker unsetvar(const char *s)
7994b88c807SRodney W. Grimes {
8004b88c807SRodney W. Grimes 	struct var **vpp;
8014b88c807SRodney W. Grimes 	struct var *vp;
8024b88c807SRodney W. Grimes 
8034b88c807SRodney W. Grimes 	vpp = hashvar(s);
8044b88c807SRodney W. Grimes 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
8054b88c807SRodney W. Grimes 		if (varequal(vp->text, s)) {
8064b88c807SRodney W. Grimes 			if (vp->flags & VREADONLY)
8074b88c807SRodney W. Grimes 				return (1);
8084b88c807SRodney W. Grimes 			INTOFF;
8094b88c807SRodney W. Grimes 			if (*(strchr(vp->text, '=') + 1) != '\0')
8104b88c807SRodney W. Grimes 				setvar(s, nullstr, 0);
811ba726b8aSAndrey A. Chernov 			if ((vp->flags & VEXPORT) && localevar(vp->text)) {
8125b78c6c2SSean Farley 				change_env(s, 0);
813ba726b8aSAndrey A. Chernov 				setlocale(LC_ALL, "");
814ba726b8aSAndrey A. Chernov 			}
8154b88c807SRodney W. Grimes 			vp->flags &= ~VEXPORT;
8164b88c807SRodney W. Grimes 			vp->flags |= VUNSET;
8174b88c807SRodney W. Grimes 			if ((vp->flags & VSTRFIXED) == 0) {
8184b88c807SRodney W. Grimes 				if ((vp->flags & VTEXTFIXED) == 0)
8194b88c807SRodney W. Grimes 					ckfree(vp->text);
8204b88c807SRodney W. Grimes 				*vpp = vp->next;
8214b88c807SRodney W. Grimes 				ckfree(vp);
8224b88c807SRodney W. Grimes 			}
8234b88c807SRodney W. Grimes 			INTON;
8244b88c807SRodney W. Grimes 			return (0);
8254b88c807SRodney W. Grimes 		}
8264b88c807SRodney W. Grimes 	}
8274b88c807SRodney W. Grimes 
828bd766773SDag-Erling Smørgrav 	return (0);
8294b88c807SRodney W. Grimes }
8304b88c807SRodney W. Grimes 
8314b88c807SRodney W. Grimes 
8324b88c807SRodney W. Grimes 
8334b88c807SRodney W. Grimes /*
8344b88c807SRodney W. Grimes  * Find the appropriate entry in the hash table from the name.
8354b88c807SRodney W. Grimes  */
8364b88c807SRodney W. Grimes 
8374b88c807SRodney W. Grimes STATIC struct var **
8382cac6e36SJilles Tjoelker hashvar(const char *p)
8394b88c807SRodney W. Grimes {
8404b88c807SRodney W. Grimes 	unsigned int hashval;
8414b88c807SRodney W. Grimes 
842ba726b8aSAndrey A. Chernov 	hashval = ((unsigned char) *p) << 4;
8434b88c807SRodney W. Grimes 	while (*p && *p != '=')
844ba726b8aSAndrey A. Chernov 		hashval += (unsigned char) *p++;
8454b88c807SRodney W. Grimes 	return &vartab[hashval % VTABSIZE];
8464b88c807SRodney W. Grimes }
8474b88c807SRodney W. Grimes 
8484b88c807SRodney W. Grimes 
8494b88c807SRodney W. Grimes 
8504b88c807SRodney W. Grimes /*
8514b88c807SRodney W. Grimes  * Returns true if the two strings specify the same varable.  The first
8524b88c807SRodney W. Grimes  * variable name is terminated by '='; the second may be terminated by
8534b88c807SRodney W. Grimes  * either '=' or '\0'.
8544b88c807SRodney W. Grimes  */
8554b88c807SRodney W. Grimes 
8564b88c807SRodney W. Grimes STATIC int
8572cac6e36SJilles Tjoelker varequal(const char *p, const char *q)
8584b88c807SRodney W. Grimes {
8594b88c807SRodney W. Grimes 	while (*p == *q++) {
8604b88c807SRodney W. Grimes 		if (*p++ == '=')
8614b88c807SRodney W. Grimes 			return 1;
8624b88c807SRodney W. Grimes 	}
8634b88c807SRodney W. Grimes 	if (*p == '=' && *(q - 1) == '\0')
8644b88c807SRodney W. Grimes 		return 1;
8654b88c807SRodney W. Grimes 	return 0;
8664b88c807SRodney W. Grimes }
867