xref: /freebsd/bin/sh/var.c (revision aa9caaf65748ace0f3cd08c2deaf3ef3048d6e4d)
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  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
3589730b29SDavid Greenman  *
36aa9caaf6SPeter Wemm  *	$Id: var.c,v 1.5 1996/08/12 22:14:50 ache Exp $
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
40aa9caaf6SPeter Wemm static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
43aa9caaf6SPeter Wemm #include <unistd.h>
44aa9caaf6SPeter Wemm #include <stdlib.h>
45aa9caaf6SPeter Wemm 
464b88c807SRodney W. Grimes /*
474b88c807SRodney W. Grimes  * Shell variables.
484b88c807SRodney W. Grimes  */
494b88c807SRodney W. Grimes 
50ba726b8aSAndrey A. Chernov #include <locale.h>
51ba726b8aSAndrey A. Chernov 
524b88c807SRodney W. Grimes #include "shell.h"
534b88c807SRodney W. Grimes #include "output.h"
544b88c807SRodney W. Grimes #include "expand.h"
554b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
564b88c807SRodney W. Grimes #include "eval.h"	/* defines cmdenviron */
574b88c807SRodney W. Grimes #include "exec.h"
584b88c807SRodney W. Grimes #include "syntax.h"
594b88c807SRodney W. Grimes #include "options.h"
604b88c807SRodney W. Grimes #include "mail.h"
61c07cbf9cSAndrey A. Chernov #include "parser.h"
624b88c807SRodney W. Grimes #include "var.h"
634b88c807SRodney W. Grimes #include "memalloc.h"
644b88c807SRodney W. Grimes #include "error.h"
654b88c807SRodney W. Grimes #include "mystring.h"
66aa9caaf6SPeter Wemm #ifndef NO_HISTORY
67aa9caaf6SPeter Wemm #include "myhistedit.h"
68aa9caaf6SPeter Wemm #endif
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes #define VTABSIZE 39
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes struct varinit {
754b88c807SRodney W. Grimes 	struct var *var;
764b88c807SRodney W. Grimes 	int flags;
774b88c807SRodney W. Grimes 	char *text;
784b88c807SRodney W. Grimes };
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes #if ATTY
824b88c807SRodney W. Grimes struct var vatty;
834b88c807SRodney W. Grimes #endif
84aa9caaf6SPeter Wemm #ifndef NO_HISTORY
854b88c807SRodney W. Grimes struct var vhistsize;
86aa9caaf6SPeter Wemm #endif
874b88c807SRodney W. Grimes struct var vifs;
884b88c807SRodney W. Grimes struct var vmail;
894b88c807SRodney W. Grimes struct var vmpath;
904b88c807SRodney W. Grimes struct var vpath;
914b88c807SRodney W. Grimes struct var vps1;
924b88c807SRodney W. Grimes struct var vps2;
934b88c807SRodney W. Grimes struct var vvers;
944b88c807SRodney W. Grimes #if ATTY
954b88c807SRodney W. Grimes struct var vterm;
964b88c807SRodney W. Grimes #endif
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes const struct varinit varinit[] = {
994b88c807SRodney W. Grimes #if ATTY
1004b88c807SRodney W. Grimes 	{&vatty,	VSTRFIXED|VTEXTFIXED|VUNSET,	"ATTY="},
1014b88c807SRodney W. Grimes #endif
102aa9caaf6SPeter Wemm #ifndef NO_HISTORY
1034b88c807SRodney W. Grimes 	{&vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE="},
104aa9caaf6SPeter Wemm #endif
1054b88c807SRodney W. Grimes 	{&vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n"},
1064b88c807SRodney W. Grimes 	{&vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL="},
1074b88c807SRodney W. Grimes 	{&vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH="},
108aa9caaf6SPeter Wemm 	{&vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=/bin:/usr/bin"},
1094b88c807SRodney W. Grimes 	/*
1104b88c807SRodney W. Grimes 	 * vps1 depends on uid
1114b88c807SRodney W. Grimes 	 */
1124b88c807SRodney W. Grimes 	{&vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> "},
1134b88c807SRodney W. Grimes #if ATTY
1144b88c807SRodney W. Grimes 	{&vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM="},
1154b88c807SRodney W. Grimes #endif
1164b88c807SRodney W. Grimes 	{NULL,	0,				NULL}
1174b88c807SRodney W. Grimes };
1184b88c807SRodney W. Grimes 
1194b88c807SRodney W. Grimes struct var *vartab[VTABSIZE];
1204b88c807SRodney W. Grimes 
1214b88c807SRodney W. Grimes STATIC int unsetvar __P((char *));
1224b88c807SRodney W. Grimes STATIC struct var **hashvar __P((char *));
1234b88c807SRodney W. Grimes STATIC int varequal __P((char *, char *));
124ba726b8aSAndrey A. Chernov STATIC int localevar __P((char *));
1254b88c807SRodney W. Grimes 
1264b88c807SRodney W. Grimes /*
1274b88c807SRodney W. Grimes  * Initialize the varable symbol tables and import the environment
1284b88c807SRodney W. Grimes  */
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes #ifdef mkinit
1314b88c807SRodney W. Grimes INCLUDE "var.h"
1324b88c807SRodney W. Grimes INIT {
1334b88c807SRodney W. Grimes 	char **envp;
1344b88c807SRodney W. Grimes 	extern char **environ;
1354b88c807SRodney W. Grimes 
1364b88c807SRodney W. Grimes 	initvar();
1374b88c807SRodney W. Grimes 	for (envp = environ ; *envp ; envp++) {
1384b88c807SRodney W. Grimes 		if (strchr(*envp, '=')) {
1394b88c807SRodney W. Grimes 			setvareq(*envp, VEXPORT|VTEXTFIXED);
1404b88c807SRodney W. Grimes 		}
1414b88c807SRodney W. Grimes 	}
1424b88c807SRodney W. Grimes }
1434b88c807SRodney W. Grimes #endif
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 
1464b88c807SRodney W. Grimes /*
1474b88c807SRodney W. Grimes  * This routine initializes the builtin variables.  It is called when the
1484b88c807SRodney W. Grimes  * shell is initialized and again when a shell procedure is spawned.
1494b88c807SRodney W. Grimes  */
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes void
1524b88c807SRodney W. Grimes initvar() {
1534b88c807SRodney W. Grimes 	const struct varinit *ip;
1544b88c807SRodney W. Grimes 	struct var *vp;
1554b88c807SRodney W. Grimes 	struct var **vpp;
1564b88c807SRodney W. Grimes 
1574b88c807SRodney W. Grimes 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1584b88c807SRodney W. Grimes 		if ((vp->flags & VEXPORT) == 0) {
1594b88c807SRodney W. Grimes 			vpp = hashvar(ip->text);
1604b88c807SRodney W. Grimes 			vp->next = *vpp;
1614b88c807SRodney W. Grimes 			*vpp = vp;
1624b88c807SRodney W. Grimes 			vp->text = ip->text;
1634b88c807SRodney W. Grimes 			vp->flags = ip->flags;
1644b88c807SRodney W. Grimes 		}
1654b88c807SRodney W. Grimes 	}
1664b88c807SRodney W. Grimes 	/*
1674b88c807SRodney W. Grimes 	 * PS1 depends on uid
1684b88c807SRodney W. Grimes 	 */
1694b88c807SRodney W. Grimes 	if ((vps1.flags & VEXPORT) == 0) {
1704b88c807SRodney W. Grimes 		vpp = hashvar("PS1=");
1714b88c807SRodney W. Grimes 		vps1.next = *vpp;
1724b88c807SRodney W. Grimes 		*vpp = &vps1;
1734b88c807SRodney W. Grimes 		vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
1744b88c807SRodney W. Grimes 		vps1.flags = VSTRFIXED|VTEXTFIXED;
1754b88c807SRodney W. Grimes 	}
1764b88c807SRodney W. Grimes }
1774b88c807SRodney W. Grimes 
1784b88c807SRodney W. Grimes /*
1794b88c807SRodney W. Grimes  * Set the value of a variable.  The flags argument is ored with the
1804b88c807SRodney W. Grimes  * flags of the variable.  If val is NULL, the variable is unset.
1814b88c807SRodney W. Grimes  */
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes void
1844b88c807SRodney W. Grimes setvar(name, val, flags)
1854b88c807SRodney W. Grimes 	char *name, *val;
186aa9caaf6SPeter Wemm 	int flags;
1874b88c807SRodney W. Grimes {
1884b88c807SRodney W. Grimes 	char *p, *q;
1894b88c807SRodney W. Grimes 	int len;
1904b88c807SRodney W. Grimes 	int namelen;
1914b88c807SRodney W. Grimes 	char *nameeq;
1924b88c807SRodney W. Grimes 	int isbad;
1934b88c807SRodney W. Grimes 
1944b88c807SRodney W. Grimes 	isbad = 0;
1954b88c807SRodney W. Grimes 	p = name;
196ba726b8aSAndrey A. Chernov 	if (! is_name(*p))
1974b88c807SRodney W. Grimes 		isbad = 1;
198ba726b8aSAndrey A. Chernov 	p++;
1994b88c807SRodney W. Grimes 	for (;;) {
2004b88c807SRodney W. Grimes 		if (! is_in_name(*p)) {
2014b88c807SRodney W. Grimes 			if (*p == '\0' || *p == '=')
2024b88c807SRodney W. Grimes 				break;
2034b88c807SRodney W. Grimes 			isbad = 1;
2044b88c807SRodney W. Grimes 		}
2054b88c807SRodney W. Grimes 		p++;
2064b88c807SRodney W. Grimes 	}
2074b88c807SRodney W. Grimes 	namelen = p - name;
2084b88c807SRodney W. Grimes 	if (isbad)
2094b88c807SRodney W. Grimes 		error("%.*s: bad variable name", namelen, name);
2104b88c807SRodney W. Grimes 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2114b88c807SRodney W. Grimes 	if (val == NULL) {
2124b88c807SRodney W. Grimes 		flags |= VUNSET;
2134b88c807SRodney W. Grimes 	} else {
2144b88c807SRodney W. Grimes 		len += strlen(val);
2154b88c807SRodney W. Grimes 	}
2164b88c807SRodney W. Grimes 	p = nameeq = ckmalloc(len);
2174b88c807SRodney W. Grimes 	q = name;
2184b88c807SRodney W. Grimes 	while (--namelen >= 0)
2194b88c807SRodney W. Grimes 		*p++ = *q++;
2204b88c807SRodney W. Grimes 	*p++ = '=';
2214b88c807SRodney W. Grimes 	*p = '\0';
2224b88c807SRodney W. Grimes 	if (val)
2234b88c807SRodney W. Grimes 		scopy(val, p);
2244b88c807SRodney W. Grimes 	setvareq(nameeq, flags);
2254b88c807SRodney W. Grimes }
2264b88c807SRodney W. Grimes 
227ba726b8aSAndrey A. Chernov STATIC int
228ba726b8aSAndrey A. Chernov localevar(s)
229ba726b8aSAndrey A. Chernov 	char *s;
230ba726b8aSAndrey A. Chernov 	{
231ba726b8aSAndrey A. Chernov 	static char *lnames[7] = {
232ba726b8aSAndrey A. Chernov 		"ALL", "COLLATE", "CTYPE", "MONETARY",
233ba726b8aSAndrey A. Chernov 		"NUMERIC", "TIME", NULL
234ba726b8aSAndrey A. Chernov 	};
235ba726b8aSAndrey A. Chernov 	char **ss;
2364b88c807SRodney W. Grimes 
237ba726b8aSAndrey A. Chernov 	if (*s != 'L')
238ba726b8aSAndrey A. Chernov 		return 0;
239ba726b8aSAndrey A. Chernov 	if (varequal(s + 1, "ANG"))
240ba726b8aSAndrey A. Chernov 		return 1;
241ba726b8aSAndrey A. Chernov 	if (strncmp(s + 1, "C_", 2) != 0)
242ba726b8aSAndrey A. Chernov 		return 0;
243ba726b8aSAndrey A. Chernov 	for (ss = lnames; *ss ; ss++)
244ba726b8aSAndrey A. Chernov 		if (varequal(s + 3, *ss))
245ba726b8aSAndrey A. Chernov 			return 1;
246ba726b8aSAndrey A. Chernov 	return 0;
247ba726b8aSAndrey A. Chernov }
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes /*
2504b88c807SRodney W. Grimes  * Same as setvar except that the variable and value are passed in
2514b88c807SRodney W. Grimes  * the first argument as name=value.  Since the first argument will
2524b88c807SRodney W. Grimes  * be actually stored in the table, it should not be a string that
2534b88c807SRodney W. Grimes  * will go away.
2544b88c807SRodney W. Grimes  */
2554b88c807SRodney W. Grimes 
2564b88c807SRodney W. Grimes void
2574b88c807SRodney W. Grimes setvareq(s, flags)
2584b88c807SRodney W. Grimes 	char *s;
259aa9caaf6SPeter Wemm 	int flags;
2604b88c807SRodney W. Grimes {
2614b88c807SRodney W. Grimes 	struct var *vp, **vpp;
2624b88c807SRodney W. Grimes 
2634b88c807SRodney W. Grimes 	vpp = hashvar(s);
2644b88c807SRodney W. Grimes 	for (vp = *vpp ; vp ; vp = vp->next) {
2654b88c807SRodney W. Grimes 		if (varequal(s, vp->text)) {
2664b88c807SRodney W. Grimes 			if (vp->flags & VREADONLY) {
2674b88c807SRodney W. Grimes 				int len = strchr(s, '=') - s;
2684b88c807SRodney W. Grimes 				error("%.*s: is read only", len, s);
2694b88c807SRodney W. Grimes 			}
2704b88c807SRodney W. Grimes 			INTOFF;
2714b88c807SRodney W. Grimes 			if (vp == &vpath)
2724b88c807SRodney W. Grimes 				changepath(s + 5);	/* 5 = strlen("PATH=") */
2734b88c807SRodney W. Grimes 			if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
2744b88c807SRodney W. Grimes 				ckfree(vp->text);
2754b88c807SRodney W. Grimes 			vp->flags &=~ (VTEXTFIXED|VSTACK|VUNSET);
2764b88c807SRodney W. Grimes 			vp->flags |= flags;
2774b88c807SRodney W. Grimes 			vp->text = s;
2784b88c807SRodney W. Grimes 			if (vp == &vmpath || (vp == &vmail && ! mpathset()))
2794b88c807SRodney W. Grimes 				chkmail(1);
280aa9caaf6SPeter Wemm #ifndef NO_HISTORY
2814b88c807SRodney W. Grimes 			if (vp == &vhistsize)
2824b88c807SRodney W. Grimes 				sethistsize();
283aa9caaf6SPeter Wemm #endif
284ba726b8aSAndrey A. Chernov 			if ((vp->flags & VEXPORT) && localevar(s)) {
285ba726b8aSAndrey A. Chernov 				putenv(s);
286ba726b8aSAndrey A. Chernov 				(void) setlocale(LC_ALL, "");
287ba726b8aSAndrey A. Chernov 			}
2884b88c807SRodney W. Grimes 			INTON;
2894b88c807SRodney W. Grimes 			return;
2904b88c807SRodney W. Grimes 		}
2914b88c807SRodney W. Grimes 	}
2924b88c807SRodney W. Grimes 	/* not found */
2934b88c807SRodney W. Grimes 	vp = ckmalloc(sizeof (*vp));
2944b88c807SRodney W. Grimes 	vp->flags = flags;
2954b88c807SRodney W. Grimes 	vp->text = s;
2964b88c807SRodney W. Grimes 	vp->next = *vpp;
297ba726b8aSAndrey A. Chernov 	INTOFF;
2984b88c807SRodney W. Grimes 	*vpp = vp;
299ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(s)) {
300ba726b8aSAndrey A. Chernov 		putenv(s);
301ba726b8aSAndrey A. Chernov 		(void) setlocale(LC_ALL, "");
302ba726b8aSAndrey A. Chernov 	}
303ba726b8aSAndrey A. Chernov 	INTON;
3044b88c807SRodney W. Grimes }
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes 
3084b88c807SRodney W. Grimes /*
3094b88c807SRodney W. Grimes  * Process a linked list of variable assignments.
3104b88c807SRodney W. Grimes  */
3114b88c807SRodney W. Grimes 
3124b88c807SRodney W. Grimes void
3134b88c807SRodney W. Grimes listsetvar(list)
3144b88c807SRodney W. Grimes 	struct strlist *list;
3154b88c807SRodney W. Grimes 	{
3164b88c807SRodney W. Grimes 	struct strlist *lp;
3174b88c807SRodney W. Grimes 
3184b88c807SRodney W. Grimes 	INTOFF;
3194b88c807SRodney W. Grimes 	for (lp = list ; lp ; lp = lp->next) {
3204b88c807SRodney W. Grimes 		setvareq(savestr(lp->text), 0);
3214b88c807SRodney W. Grimes 	}
3224b88c807SRodney W. Grimes 	INTON;
3234b88c807SRodney W. Grimes }
3244b88c807SRodney W. Grimes 
3254b88c807SRodney W. Grimes 
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes /*
3284b88c807SRodney W. Grimes  * Find the value of a variable.  Returns NULL if not set.
3294b88c807SRodney W. Grimes  */
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes char *
3324b88c807SRodney W. Grimes lookupvar(name)
3334b88c807SRodney W. Grimes 	char *name;
3344b88c807SRodney W. Grimes 	{
3354b88c807SRodney W. Grimes 	struct var *v;
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes 	for (v = *hashvar(name) ; v ; v = v->next) {
3384b88c807SRodney W. Grimes 		if (varequal(v->text, name)) {
3394b88c807SRodney W. Grimes 			if (v->flags & VUNSET)
3404b88c807SRodney W. Grimes 				return NULL;
3414b88c807SRodney W. Grimes 			return strchr(v->text, '=') + 1;
3424b88c807SRodney W. Grimes 		}
3434b88c807SRodney W. Grimes 	}
3444b88c807SRodney W. Grimes 	return NULL;
3454b88c807SRodney W. Grimes }
3464b88c807SRodney W. Grimes 
3474b88c807SRodney W. Grimes 
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes /*
3504b88c807SRodney W. Grimes  * Search the environment of a builtin command.  If the second argument
3514b88c807SRodney W. Grimes  * is nonzero, return the value of a variable even if it hasn't been
3524b88c807SRodney W. Grimes  * exported.
3534b88c807SRodney W. Grimes  */
3544b88c807SRodney W. Grimes 
3554b88c807SRodney W. Grimes char *
3564b88c807SRodney W. Grimes bltinlookup(name, doall)
3574b88c807SRodney W. Grimes 	char *name;
358aa9caaf6SPeter Wemm 	int doall;
3594b88c807SRodney W. Grimes {
3604b88c807SRodney W. Grimes 	struct strlist *sp;
3614b88c807SRodney W. Grimes 	struct var *v;
3624b88c807SRodney W. Grimes 
3634b88c807SRodney W. Grimes 	for (sp = cmdenviron ; sp ; sp = sp->next) {
3644b88c807SRodney W. Grimes 		if (varequal(sp->text, name))
3654b88c807SRodney W. Grimes 			return strchr(sp->text, '=') + 1;
3664b88c807SRodney W. Grimes 	}
3674b88c807SRodney W. Grimes 	for (v = *hashvar(name) ; v ; v = v->next) {
3684b88c807SRodney W. Grimes 		if (varequal(v->text, name)) {
369aa9caaf6SPeter Wemm 			if ((v->flags & VUNSET)
370aa9caaf6SPeter Wemm 			 || (!doall && (v->flags & VEXPORT) == 0))
3714b88c807SRodney W. Grimes 				return NULL;
3724b88c807SRodney W. Grimes 			return strchr(v->text, '=') + 1;
3734b88c807SRodney W. Grimes 		}
3744b88c807SRodney W. Grimes 	}
3754b88c807SRodney W. Grimes 	return NULL;
3764b88c807SRodney W. Grimes }
3774b88c807SRodney W. Grimes 
3784b88c807SRodney W. Grimes 
3794b88c807SRodney W. Grimes 
3804b88c807SRodney W. Grimes /*
3814b88c807SRodney W. Grimes  * Generate a list of exported variables.  This routine is used to construct
3824b88c807SRodney W. Grimes  * the third argument to execve when executing a program.
3834b88c807SRodney W. Grimes  */
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes char **
3864b88c807SRodney W. Grimes environment() {
3874b88c807SRodney W. Grimes 	int nenv;
3884b88c807SRodney W. Grimes 	struct var **vpp;
3894b88c807SRodney W. Grimes 	struct var *vp;
3904b88c807SRodney W. Grimes 	char **env, **ep;
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes 	nenv = 0;
3934b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
3944b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
3954b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
3964b88c807SRodney W. Grimes 				nenv++;
3974b88c807SRodney W. Grimes 	}
3984b88c807SRodney W. Grimes 	ep = env = stalloc((nenv + 1) * sizeof *env);
3994b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4004b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
4014b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
4024b88c807SRodney W. Grimes 				*ep++ = vp->text;
4034b88c807SRodney W. Grimes 	}
4044b88c807SRodney W. Grimes 	*ep = NULL;
4054b88c807SRodney W. Grimes 	return env;
4064b88c807SRodney W. Grimes }
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 
4094b88c807SRodney W. Grimes /*
4104b88c807SRodney W. Grimes  * Called when a shell procedure is invoked to clear out nonexported
4114b88c807SRodney W. Grimes  * variables.  It is also necessary to reallocate variables of with
4124b88c807SRodney W. Grimes  * VSTACK set since these are currently allocated on the stack.
4134b88c807SRodney W. Grimes  */
4144b88c807SRodney W. Grimes 
4154b88c807SRodney W. Grimes #ifdef mkinit
4164b88c807SRodney W. Grimes MKINIT void shprocvar();
4174b88c807SRodney W. Grimes 
4184b88c807SRodney W. Grimes SHELLPROC {
4194b88c807SRodney W. Grimes 	shprocvar();
4204b88c807SRodney W. Grimes }
4214b88c807SRodney W. Grimes #endif
4224b88c807SRodney W. Grimes 
4234b88c807SRodney W. Grimes void
4244b88c807SRodney W. Grimes shprocvar() {
4254b88c807SRodney W. Grimes 	struct var **vpp;
4264b88c807SRodney W. Grimes 	struct var *vp, **prev;
4274b88c807SRodney W. Grimes 
4284b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4294b88c807SRodney W. Grimes 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
4304b88c807SRodney W. Grimes 			if ((vp->flags & VEXPORT) == 0) {
4314b88c807SRodney W. Grimes 				*prev = vp->next;
4324b88c807SRodney W. Grimes 				if ((vp->flags & VTEXTFIXED) == 0)
4334b88c807SRodney W. Grimes 					ckfree(vp->text);
4344b88c807SRodney W. Grimes 				if ((vp->flags & VSTRFIXED) == 0)
4354b88c807SRodney W. Grimes 					ckfree(vp);
4364b88c807SRodney W. Grimes 			} else {
4374b88c807SRodney W. Grimes 				if (vp->flags & VSTACK) {
4384b88c807SRodney W. Grimes 					vp->text = savestr(vp->text);
4394b88c807SRodney W. Grimes 					vp->flags &=~ VSTACK;
4404b88c807SRodney W. Grimes 				}
4414b88c807SRodney W. Grimes 				prev = &vp->next;
4424b88c807SRodney W. Grimes 			}
4434b88c807SRodney W. Grimes 		}
4444b88c807SRodney W. Grimes 	}
4454b88c807SRodney W. Grimes 	initvar();
4464b88c807SRodney W. Grimes }
4474b88c807SRodney W. Grimes 
4484b88c807SRodney W. Grimes 
4494b88c807SRodney W. Grimes 
4504b88c807SRodney W. Grimes /*
4514b88c807SRodney W. Grimes  * Command to list all variables which are set.  Currently this command
4524b88c807SRodney W. Grimes  * is invoked from the set command when the set command is called without
4534b88c807SRodney W. Grimes  * any variables.
4544b88c807SRodney W. Grimes  */
4554b88c807SRodney W. Grimes 
4564b88c807SRodney W. Grimes int
457aa9caaf6SPeter Wemm showvarscmd(argc, argv)
458aa9caaf6SPeter Wemm 	int argc;
459aa9caaf6SPeter Wemm 	char **argv;
460aa9caaf6SPeter Wemm {
4614b88c807SRodney W. Grimes 	struct var **vpp;
4624b88c807SRodney W. Grimes 	struct var *vp;
4634b88c807SRodney W. Grimes 
4644b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4654b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next) {
4664b88c807SRodney W. Grimes 			if ((vp->flags & VUNSET) == 0)
4674b88c807SRodney W. Grimes 				out1fmt("%s\n", vp->text);
4684b88c807SRodney W. Grimes 		}
4694b88c807SRodney W. Grimes 	}
4704b88c807SRodney W. Grimes 	return 0;
4714b88c807SRodney W. Grimes }
4724b88c807SRodney W. Grimes 
4734b88c807SRodney W. Grimes 
4744b88c807SRodney W. Grimes 
4754b88c807SRodney W. Grimes /*
4764b88c807SRodney W. Grimes  * The export and readonly commands.
4774b88c807SRodney W. Grimes  */
4784b88c807SRodney W. Grimes 
4794b88c807SRodney W. Grimes int
480aa9caaf6SPeter Wemm exportcmd(argc, argv)
481aa9caaf6SPeter Wemm 	int argc;
482aa9caaf6SPeter Wemm 	char **argv;
483aa9caaf6SPeter Wemm {
4844b88c807SRodney W. Grimes 	struct var **vpp;
4854b88c807SRodney W. Grimes 	struct var *vp;
4864b88c807SRodney W. Grimes 	char *name;
4874b88c807SRodney W. Grimes 	char *p;
4884b88c807SRodney W. Grimes 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
4894b88c807SRodney W. Grimes 
4904b88c807SRodney W. Grimes 	listsetvar(cmdenviron);
4914b88c807SRodney W. Grimes 	if (argc > 1) {
4924b88c807SRodney W. Grimes 		while ((name = *argptr++) != NULL) {
4934b88c807SRodney W. Grimes 			if ((p = strchr(name, '=')) != NULL) {
4944b88c807SRodney W. Grimes 				p++;
4954b88c807SRodney W. Grimes 			} else {
4964b88c807SRodney W. Grimes 				vpp = hashvar(name);
4974b88c807SRodney W. Grimes 				for (vp = *vpp ; vp ; vp = vp->next) {
4984b88c807SRodney W. Grimes 					if (varequal(vp->text, name)) {
4994b88c807SRodney W. Grimes 						vp->flags |= flag;
500ba726b8aSAndrey A. Chernov 						if ((vp->flags & VEXPORT) && localevar(vp->text)) {
501ba726b8aSAndrey A. Chernov 							putenv(vp->text);
502ba726b8aSAndrey A. Chernov 							(void) setlocale(LC_ALL, "");
503ba726b8aSAndrey A. Chernov 						}
5044b88c807SRodney W. Grimes 						goto found;
5054b88c807SRodney W. Grimes 					}
5064b88c807SRodney W. Grimes 				}
5074b88c807SRodney W. Grimes 			}
5084b88c807SRodney W. Grimes 			setvar(name, p, flag);
5094b88c807SRodney W. Grimes found:;
5104b88c807SRodney W. Grimes 		}
5114b88c807SRodney W. Grimes 	} else {
5124b88c807SRodney W. Grimes 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5134b88c807SRodney W. Grimes 			for (vp = *vpp ; vp ; vp = vp->next) {
5144b88c807SRodney W. Grimes 				if (vp->flags & flag) {
5154b88c807SRodney W. Grimes 					for (p = vp->text ; *p != '=' ; p++)
5164b88c807SRodney W. Grimes 						out1c(*p);
5174b88c807SRodney W. Grimes 					out1c('\n');
5184b88c807SRodney W. Grimes 				}
5194b88c807SRodney W. Grimes 			}
5204b88c807SRodney W. Grimes 		}
5214b88c807SRodney W. Grimes 	}
5224b88c807SRodney W. Grimes 	return 0;
5234b88c807SRodney W. Grimes }
5244b88c807SRodney W. Grimes 
5254b88c807SRodney W. Grimes 
5264b88c807SRodney W. Grimes /*
5274b88c807SRodney W. Grimes  * The "local" command.
5284b88c807SRodney W. Grimes  */
5294b88c807SRodney W. Grimes 
530aa9caaf6SPeter Wemm int
531aa9caaf6SPeter Wemm localcmd(argc, argv)
532aa9caaf6SPeter Wemm 	int argc;
533aa9caaf6SPeter Wemm 	char **argv;
534aa9caaf6SPeter Wemm {
5354b88c807SRodney W. Grimes 	char *name;
5364b88c807SRodney W. Grimes 
5374b88c807SRodney W. Grimes 	if (! in_function())
5384b88c807SRodney W. Grimes 		error("Not in a function");
5394b88c807SRodney W. Grimes 	while ((name = *argptr++) != NULL) {
5404b88c807SRodney W. Grimes 		mklocal(name);
5414b88c807SRodney W. Grimes 	}
5424b88c807SRodney W. Grimes 	return 0;
5434b88c807SRodney W. Grimes }
5444b88c807SRodney W. Grimes 
5454b88c807SRodney W. Grimes 
5464b88c807SRodney W. Grimes /*
5474b88c807SRodney W. Grimes  * Make a variable a local variable.  When a variable is made local, it's
5484b88c807SRodney W. Grimes  * value and flags are saved in a localvar structure.  The saved values
5494b88c807SRodney W. Grimes  * will be restored when the shell function returns.  We handle the name
5504b88c807SRodney W. Grimes  * "-" as a special case.
5514b88c807SRodney W. Grimes  */
5524b88c807SRodney W. Grimes 
5534b88c807SRodney W. Grimes void
5544b88c807SRodney W. Grimes mklocal(name)
5554b88c807SRodney W. Grimes 	char *name;
5564b88c807SRodney W. Grimes 	{
5574b88c807SRodney W. Grimes 	struct localvar *lvp;
5584b88c807SRodney W. Grimes 	struct var **vpp;
5594b88c807SRodney W. Grimes 	struct var *vp;
5604b88c807SRodney W. Grimes 
5614b88c807SRodney W. Grimes 	INTOFF;
5624b88c807SRodney W. Grimes 	lvp = ckmalloc(sizeof (struct localvar));
5634b88c807SRodney W. Grimes 	if (name[0] == '-' && name[1] == '\0') {
5644b88c807SRodney W. Grimes 		lvp->text = ckmalloc(sizeof optlist);
565aa9caaf6SPeter Wemm 		memcpy(lvp->text, optlist, sizeof optlist);
5664b88c807SRodney W. Grimes 		vp = NULL;
5674b88c807SRodney W. Grimes 	} else {
5684b88c807SRodney W. Grimes 		vpp = hashvar(name);
5694b88c807SRodney W. Grimes 		for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
5704b88c807SRodney W. Grimes 		if (vp == NULL) {
5714b88c807SRodney W. Grimes 			if (strchr(name, '='))
5724b88c807SRodney W. Grimes 				setvareq(savestr(name), VSTRFIXED);
5734b88c807SRodney W. Grimes 			else
5744b88c807SRodney W. Grimes 				setvar(name, NULL, VSTRFIXED);
5754b88c807SRodney W. Grimes 			vp = *vpp;	/* the new variable */
5764b88c807SRodney W. Grimes 			lvp->text = NULL;
5774b88c807SRodney W. Grimes 			lvp->flags = VUNSET;
5784b88c807SRodney W. Grimes 		} else {
5794b88c807SRodney W. Grimes 			lvp->text = vp->text;
5804b88c807SRodney W. Grimes 			lvp->flags = vp->flags;
5814b88c807SRodney W. Grimes 			vp->flags |= VSTRFIXED|VTEXTFIXED;
5824b88c807SRodney W. Grimes 			if (strchr(name, '='))
5834b88c807SRodney W. Grimes 				setvareq(savestr(name), 0);
5844b88c807SRodney W. Grimes 		}
5854b88c807SRodney W. Grimes 	}
5864b88c807SRodney W. Grimes 	lvp->vp = vp;
5874b88c807SRodney W. Grimes 	lvp->next = localvars;
5884b88c807SRodney W. Grimes 	localvars = lvp;
5894b88c807SRodney W. Grimes 	INTON;
5904b88c807SRodney W. Grimes }
5914b88c807SRodney W. Grimes 
5924b88c807SRodney W. Grimes 
5934b88c807SRodney W. Grimes /*
5944b88c807SRodney W. Grimes  * Called after a function returns.
5954b88c807SRodney W. Grimes  */
5964b88c807SRodney W. Grimes 
5974b88c807SRodney W. Grimes void
5984b88c807SRodney W. Grimes poplocalvars() {
5994b88c807SRodney W. Grimes 	struct localvar *lvp;
6004b88c807SRodney W. Grimes 	struct var *vp;
6014b88c807SRodney W. Grimes 
6024b88c807SRodney W. Grimes 	while ((lvp = localvars) != NULL) {
6034b88c807SRodney W. Grimes 		localvars = lvp->next;
6044b88c807SRodney W. Grimes 		vp = lvp->vp;
6054b88c807SRodney W. Grimes 		if (vp == NULL) {	/* $- saved */
606aa9caaf6SPeter Wemm 			memcpy(optlist, lvp->text, sizeof optlist);
6074b88c807SRodney W. Grimes 			ckfree(lvp->text);
6084b88c807SRodney W. Grimes 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
6094b88c807SRodney W. Grimes 			(void)unsetvar(vp->text);
6104b88c807SRodney W. Grimes 		} else {
6114b88c807SRodney W. Grimes 			if ((vp->flags & VTEXTFIXED) == 0)
6124b88c807SRodney W. Grimes 				ckfree(vp->text);
6134b88c807SRodney W. Grimes 			vp->flags = lvp->flags;
6144b88c807SRodney W. Grimes 			vp->text = lvp->text;
6154b88c807SRodney W. Grimes 		}
6164b88c807SRodney W. Grimes 		ckfree(lvp);
6174b88c807SRodney W. Grimes 	}
6184b88c807SRodney W. Grimes }
6194b88c807SRodney W. Grimes 
6204b88c807SRodney W. Grimes 
621aa9caaf6SPeter Wemm int
622aa9caaf6SPeter Wemm setvarcmd(argc, argv)
623aa9caaf6SPeter Wemm 	int argc;
624aa9caaf6SPeter Wemm 	char **argv;
625aa9caaf6SPeter Wemm {
6264b88c807SRodney W. Grimes 	if (argc <= 2)
6274b88c807SRodney W. Grimes 		return unsetcmd(argc, argv);
6284b88c807SRodney W. Grimes 	else if (argc == 3)
6294b88c807SRodney W. Grimes 		setvar(argv[1], argv[2], 0);
6304b88c807SRodney W. Grimes 	else
6314b88c807SRodney W. Grimes 		error("List assignment not implemented");
6324b88c807SRodney W. Grimes 	return 0;
6334b88c807SRodney W. Grimes }
6344b88c807SRodney W. Grimes 
6354b88c807SRodney W. Grimes 
6364b88c807SRodney W. Grimes /*
6374b88c807SRodney W. Grimes  * The unset builtin command.  We unset the function before we unset the
6384b88c807SRodney W. Grimes  * variable to allow a function to be unset when there is a readonly variable
6394b88c807SRodney W. Grimes  * with the same name.
6404b88c807SRodney W. Grimes  */
6414b88c807SRodney W. Grimes 
642aa9caaf6SPeter Wemm int
643aa9caaf6SPeter Wemm unsetcmd(argc, argv)
644aa9caaf6SPeter Wemm 	int argc;
645aa9caaf6SPeter Wemm 	char **argv;
646aa9caaf6SPeter Wemm {
6474b88c807SRodney W. Grimes 	char **ap;
6484b88c807SRodney W. Grimes 	int i;
6494b88c807SRodney W. Grimes 	int flg_func = 0;
6504b88c807SRodney W. Grimes 	int flg_var = 0;
6514b88c807SRodney W. Grimes 	int ret = 0;
6524b88c807SRodney W. Grimes 
6534b88c807SRodney W. Grimes 	while ((i = nextopt("vf")) != '\0') {
6544b88c807SRodney W. Grimes 		if (i == 'f')
6554b88c807SRodney W. Grimes 			flg_func = 1;
6564b88c807SRodney W. Grimes 		else
6574b88c807SRodney W. Grimes 			flg_var = 1;
6584b88c807SRodney W. Grimes 	}
6594b88c807SRodney W. Grimes 	if (flg_func == 0 && flg_var == 0)
6604b88c807SRodney W. Grimes 		flg_var = 1;
6614b88c807SRodney W. Grimes 
6624b88c807SRodney W. Grimes 	for (ap = argptr; *ap ; ap++) {
6634b88c807SRodney W. Grimes 		if (flg_func)
6644b88c807SRodney W. Grimes 			ret |= unsetfunc(*ap);
6654b88c807SRodney W. Grimes 		if (flg_var)
6664b88c807SRodney W. Grimes 			ret |= unsetvar(*ap);
6674b88c807SRodney W. Grimes 	}
6684b88c807SRodney W. Grimes 	return ret;
6694b88c807SRodney W. Grimes }
6704b88c807SRodney W. Grimes 
6714b88c807SRodney W. Grimes 
6724b88c807SRodney W. Grimes /*
6734b88c807SRodney W. Grimes  * Unset the specified variable.
6744b88c807SRodney W. Grimes  */
6754b88c807SRodney W. Grimes 
6764b88c807SRodney W. Grimes STATIC int
6774b88c807SRodney W. Grimes unsetvar(s)
6784b88c807SRodney W. Grimes 	char *s;
6794b88c807SRodney W. Grimes 	{
6804b88c807SRodney W. Grimes 	struct var **vpp;
6814b88c807SRodney W. Grimes 	struct var *vp;
6824b88c807SRodney W. Grimes 
6834b88c807SRodney W. Grimes 	vpp = hashvar(s);
6844b88c807SRodney W. Grimes 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
6854b88c807SRodney W. Grimes 		if (varequal(vp->text, s)) {
6864b88c807SRodney W. Grimes 			if (vp->flags & VREADONLY)
6874b88c807SRodney W. Grimes 				return (1);
6884b88c807SRodney W. Grimes 			INTOFF;
6894b88c807SRodney W. Grimes 			if (*(strchr(vp->text, '=') + 1) != '\0')
6904b88c807SRodney W. Grimes 				setvar(s, nullstr, 0);
691ba726b8aSAndrey A. Chernov 			if ((vp->flags & VEXPORT) && localevar(vp->text)) {
692ba726b8aSAndrey A. Chernov 				unsetenv(s);
693ba726b8aSAndrey A. Chernov 				setlocale(LC_ALL, "");
694ba726b8aSAndrey A. Chernov 			}
6954b88c807SRodney W. Grimes 			vp->flags &=~ VEXPORT;
6964b88c807SRodney W. Grimes 			vp->flags |= VUNSET;
6974b88c807SRodney W. Grimes 			if ((vp->flags & VSTRFIXED) == 0) {
6984b88c807SRodney W. Grimes 				if ((vp->flags & VTEXTFIXED) == 0)
6994b88c807SRodney W. Grimes 					ckfree(vp->text);
7004b88c807SRodney W. Grimes 				*vpp = vp->next;
7014b88c807SRodney W. Grimes 				ckfree(vp);
7024b88c807SRodney W. Grimes 			}
7034b88c807SRodney W. Grimes 			INTON;
7044b88c807SRodney W. Grimes 			return (0);
7054b88c807SRodney W. Grimes 		}
7064b88c807SRodney W. Grimes 	}
7074b88c807SRodney W. Grimes 
7084b88c807SRodney W. Grimes 	return (1);
7094b88c807SRodney W. Grimes }
7104b88c807SRodney W. Grimes 
7114b88c807SRodney W. Grimes 
7124b88c807SRodney W. Grimes 
7134b88c807SRodney W. Grimes /*
7144b88c807SRodney W. Grimes  * Find the appropriate entry in the hash table from the name.
7154b88c807SRodney W. Grimes  */
7164b88c807SRodney W. Grimes 
7174b88c807SRodney W. Grimes STATIC struct var **
7184b88c807SRodney W. Grimes hashvar(p)
7194b88c807SRodney W. Grimes 	register char *p;
7204b88c807SRodney W. Grimes 	{
7214b88c807SRodney W. Grimes 	unsigned int hashval;
7224b88c807SRodney W. Grimes 
723ba726b8aSAndrey A. Chernov 	hashval = ((unsigned char)*p) << 4;
7244b88c807SRodney W. Grimes 	while (*p && *p != '=')
725ba726b8aSAndrey A. Chernov 		hashval += (unsigned char)*p++;
7264b88c807SRodney W. Grimes 	return &vartab[hashval % VTABSIZE];
7274b88c807SRodney W. Grimes }
7284b88c807SRodney W. Grimes 
7294b88c807SRodney W. Grimes 
7304b88c807SRodney W. Grimes 
7314b88c807SRodney W. Grimes /*
7324b88c807SRodney W. Grimes  * Returns true if the two strings specify the same varable.  The first
7334b88c807SRodney W. Grimes  * variable name is terminated by '='; the second may be terminated by
7344b88c807SRodney W. Grimes  * either '=' or '\0'.
7354b88c807SRodney W. Grimes  */
7364b88c807SRodney W. Grimes 
7374b88c807SRodney W. Grimes STATIC int
7384b88c807SRodney W. Grimes varequal(p, q)
7394b88c807SRodney W. Grimes 	register char *p, *q;
7404b88c807SRodney W. Grimes 	{
7414b88c807SRodney W. Grimes 	while (*p == *q++) {
7424b88c807SRodney W. Grimes 		if (*p++ == '=')
7434b88c807SRodney W. Grimes 			return 1;
7444b88c807SRodney W. Grimes 	}
7454b88c807SRodney W. Grimes 	if (*p == '=' && *(q - 1) == '\0')
7464b88c807SRodney W. Grimes 		return 1;
7474b88c807SRodney W. Grimes 	return 0;
7484b88c807SRodney W. Grimes }
749