xref: /freebsd/bin/sh/var.c (revision 8d5c19ffbccf759faae955929ac28c0d0252cf78)
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.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
414b88c807SRodney W. Grimes #endif /* not lint */
422749b141SDavid E. O'Brien #include <sys/cdefs.h>
432749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <unistd.h>
46aa9caaf6SPeter Wemm #include <stdlib.h>
478d5c19ffSDavid E. O'Brien #include <paths.h>
48aa9caaf6SPeter Wemm 
494b88c807SRodney W. Grimes /*
504b88c807SRodney W. Grimes  * Shell variables.
514b88c807SRodney W. Grimes  */
524b88c807SRodney W. Grimes 
53ba726b8aSAndrey A. Chernov #include <locale.h>
54ba726b8aSAndrey A. Chernov 
554b88c807SRodney W. Grimes #include "shell.h"
564b88c807SRodney W. Grimes #include "output.h"
574b88c807SRodney W. Grimes #include "expand.h"
584b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
594b88c807SRodney W. Grimes #include "eval.h"	/* defines cmdenviron */
604b88c807SRodney W. Grimes #include "exec.h"
614b88c807SRodney W. Grimes #include "syntax.h"
624b88c807SRodney W. Grimes #include "options.h"
634b88c807SRodney W. Grimes #include "mail.h"
644b88c807SRodney W. Grimes #include "var.h"
654b88c807SRodney W. Grimes #include "memalloc.h"
664b88c807SRodney W. Grimes #include "error.h"
674b88c807SRodney W. Grimes #include "mystring.h"
68ab0a2172SSteve Price #include "parser.h"
69aa9caaf6SPeter Wemm #ifndef NO_HISTORY
70aa9caaf6SPeter Wemm #include "myhistedit.h"
71aa9caaf6SPeter Wemm #endif
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes #define VTABSIZE 39
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes struct varinit {
784b88c807SRodney W. Grimes 	struct var *var;
794b88c807SRodney W. Grimes 	int flags;
804b88c807SRodney W. Grimes 	char *text;
815134c3f7SWarner Losh 	void (*func)(const char *);
824b88c807SRodney W. Grimes };
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes 
85aa9caaf6SPeter Wemm #ifndef NO_HISTORY
864b88c807SRodney W. Grimes struct var vhistsize;
87aa9caaf6SPeter Wemm #endif
884b88c807SRodney W. Grimes struct var vifs;
894b88c807SRodney W. Grimes struct var vmail;
904b88c807SRodney W. Grimes struct var vmpath;
914b88c807SRodney W. Grimes struct var vpath;
9239dccc6fSTim J. Robbins struct var vppid;
934b88c807SRodney W. Grimes struct var vps1;
944b88c807SRodney W. Grimes struct var vps2;
954b88c807SRodney W. Grimes struct var vvers;
96ab0a2172SSteve Price struct var voptind;
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes const struct varinit varinit[] = {
99aa9caaf6SPeter Wemm #ifndef NO_HISTORY
100ab0a2172SSteve Price 	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
101ab0a2172SSteve Price 	  sethistsize },
102aa9caaf6SPeter Wemm #endif
103ab0a2172SSteve Price 	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
104ab0a2172SSteve Price 	  NULL },
105ab0a2172SSteve Price 	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
106ab0a2172SSteve Price 	  NULL },
107ab0a2172SSteve Price 	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
108ab0a2172SSteve Price 	  NULL },
1098d5c19ffSDavid E. O'Brien 	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
110ab0a2172SSteve Price 	  changepath },
11139dccc6fSTim J. Robbins 	{ &vppid,	VSTRFIXED|VTEXTFIXED|VUNSET,	"PPID=",
11239dccc6fSTim J. Robbins 	  NULL },
1134b88c807SRodney W. Grimes 	/*
1144b88c807SRodney W. Grimes 	 * vps1 depends on uid
1154b88c807SRodney W. Grimes 	 */
116ab0a2172SSteve Price 	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
117ab0a2172SSteve Price 	  NULL },
118ab0a2172SSteve Price 	{ &voptind,	VSTRFIXED|VTEXTFIXED,		"OPTIND=1",
119ab0a2172SSteve Price 	  getoptsreset },
120ab0a2172SSteve Price 	{ NULL,	0,				NULL,
121ab0a2172SSteve Price 	  NULL }
1224b88c807SRodney W. Grimes };
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes struct var *vartab[VTABSIZE];
1254b88c807SRodney W. Grimes 
1265134c3f7SWarner Losh STATIC struct var **hashvar(char *);
1275134c3f7SWarner Losh STATIC int varequal(char *, char *);
1285134c3f7SWarner Losh STATIC int localevar(char *);
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes /*
1314b88c807SRodney W. Grimes  * Initialize the varable symbol tables and import the environment
1324b88c807SRodney W. Grimes  */
1334b88c807SRodney W. Grimes 
1344b88c807SRodney W. Grimes #ifdef mkinit
1354b88c807SRodney W. Grimes INCLUDE "var.h"
1364b88c807SRodney W. Grimes INIT {
1374b88c807SRodney W. Grimes 	char **envp;
1384b88c807SRodney W. Grimes 	extern char **environ;
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes 	initvar();
1414b88c807SRodney W. Grimes 	for (envp = environ ; *envp ; envp++) {
1424b88c807SRodney W. Grimes 		if (strchr(*envp, '=')) {
1434b88c807SRodney W. Grimes 			setvareq(*envp, VEXPORT|VTEXTFIXED);
1444b88c807SRodney W. Grimes 		}
1454b88c807SRodney W. Grimes 	}
1464b88c807SRodney W. Grimes }
1474b88c807SRodney W. Grimes #endif
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes /*
1514b88c807SRodney W. Grimes  * This routine initializes the builtin variables.  It is called when the
1524b88c807SRodney W. Grimes  * shell is initialized and again when a shell procedure is spawned.
1534b88c807SRodney W. Grimes  */
1544b88c807SRodney W. Grimes 
1554b88c807SRodney W. Grimes void
1565134c3f7SWarner Losh initvar(void)
1575134c3f7SWarner Losh {
15839dccc6fSTim J. Robbins 	char ppid[20];
1594b88c807SRodney W. Grimes 	const struct varinit *ip;
1604b88c807SRodney W. Grimes 	struct var *vp;
1614b88c807SRodney W. Grimes 	struct var **vpp;
1624b88c807SRodney W. Grimes 
1634b88c807SRodney W. Grimes 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1644b88c807SRodney W. Grimes 		if ((vp->flags & VEXPORT) == 0) {
1654b88c807SRodney W. Grimes 			vpp = hashvar(ip->text);
1664b88c807SRodney W. Grimes 			vp->next = *vpp;
1674b88c807SRodney W. Grimes 			*vpp = vp;
1684b88c807SRodney W. Grimes 			vp->text = ip->text;
1694b88c807SRodney W. Grimes 			vp->flags = ip->flags;
170ab0a2172SSteve Price 			vp->func = ip->func;
1714b88c807SRodney W. Grimes 		}
1724b88c807SRodney W. Grimes 	}
1734b88c807SRodney W. Grimes 	/*
1744b88c807SRodney W. Grimes 	 * PS1 depends on uid
1754b88c807SRodney W. Grimes 	 */
1764b88c807SRodney W. Grimes 	if ((vps1.flags & VEXPORT) == 0) {
1774b88c807SRodney W. Grimes 		vpp = hashvar("PS1=");
1784b88c807SRodney W. Grimes 		vps1.next = *vpp;
1794b88c807SRodney W. Grimes 		*vpp = &vps1;
1804b88c807SRodney W. Grimes 		vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
1814b88c807SRodney W. Grimes 		vps1.flags = VSTRFIXED|VTEXTFIXED;
1824b88c807SRodney W. Grimes 	}
18339dccc6fSTim J. Robbins 	if ((vppid.flags & VEXPORT) == 0) {
18439dccc6fSTim J. Robbins 		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
18539dccc6fSTim J. Robbins 		setvarsafe("PPID", ppid, 0);
18639dccc6fSTim J. Robbins 	}
1874b88c807SRodney W. Grimes }
1884b88c807SRodney W. Grimes 
1894b88c807SRodney W. Grimes /*
190ab0a2172SSteve Price  * Safe version of setvar, returns 1 on success 0 on failure.
191ab0a2172SSteve Price  */
192ab0a2172SSteve Price 
193ab0a2172SSteve Price int
1945134c3f7SWarner Losh setvarsafe(char *name, char *val, int flags)
195ab0a2172SSteve Price {
196ab0a2172SSteve Price 	struct jmploc jmploc;
197ab0a2172SSteve Price 	struct jmploc *volatile savehandler = handler;
198ab0a2172SSteve Price 	int err = 0;
199ab0a2172SSteve Price #if __GNUC__
200ab0a2172SSteve Price 	/* Avoid longjmp clobbering */
201ab0a2172SSteve Price 	(void) &err;
202ab0a2172SSteve Price #endif
203ab0a2172SSteve Price 
204ab0a2172SSteve Price 	if (setjmp(jmploc.loc))
205ab0a2172SSteve Price 		err = 1;
206ab0a2172SSteve Price 	else {
207ab0a2172SSteve Price 		handler = &jmploc;
208ab0a2172SSteve Price 		setvar(name, val, flags);
209ab0a2172SSteve Price 	}
210ab0a2172SSteve Price 	handler = savehandler;
211ab0a2172SSteve Price 	return err;
212ab0a2172SSteve Price }
213ab0a2172SSteve Price 
214ab0a2172SSteve Price /*
2154c1b08b3SMartin Cracauer  * Set the value of a variable.  The flags argument is tored with the
2164b88c807SRodney W. Grimes  * flags of the variable.  If val is NULL, the variable is unset.
2174b88c807SRodney W. Grimes  */
2184b88c807SRodney W. Grimes 
2194b88c807SRodney W. Grimes void
2205134c3f7SWarner Losh setvar(char *name, char *val, int flags)
2214b88c807SRodney W. Grimes {
2224b88c807SRodney W. Grimes 	char *p, *q;
2234b88c807SRodney W. Grimes 	int len;
2244b88c807SRodney W. Grimes 	int namelen;
2254b88c807SRodney W. Grimes 	char *nameeq;
2264b88c807SRodney W. Grimes 	int isbad;
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes 	isbad = 0;
2294b88c807SRodney W. Grimes 	p = name;
230ba726b8aSAndrey A. Chernov 	if (! is_name(*p))
2314b88c807SRodney W. Grimes 		isbad = 1;
232ba726b8aSAndrey A. Chernov 	p++;
2334b88c807SRodney W. Grimes 	for (;;) {
2344b88c807SRodney W. Grimes 		if (! is_in_name(*p)) {
2354b88c807SRodney W. Grimes 			if (*p == '\0' || *p == '=')
2364b88c807SRodney W. Grimes 				break;
2374b88c807SRodney W. Grimes 			isbad = 1;
2384b88c807SRodney W. Grimes 		}
2394b88c807SRodney W. Grimes 		p++;
2404b88c807SRodney W. Grimes 	}
2414b88c807SRodney W. Grimes 	namelen = p - name;
2424b88c807SRodney W. Grimes 	if (isbad)
2434b88c807SRodney W. Grimes 		error("%.*s: bad variable name", namelen, name);
2444b88c807SRodney W. Grimes 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2454b88c807SRodney W. Grimes 	if (val == NULL) {
2464b88c807SRodney W. Grimes 		flags |= VUNSET;
2474b88c807SRodney W. Grimes 	} else {
2484b88c807SRodney W. Grimes 		len += strlen(val);
2494b88c807SRodney W. Grimes 	}
2504b88c807SRodney W. Grimes 	p = nameeq = ckmalloc(len);
2514b88c807SRodney W. Grimes 	q = name;
2524b88c807SRodney W. Grimes 	while (--namelen >= 0)
2534b88c807SRodney W. Grimes 		*p++ = *q++;
2544b88c807SRodney W. Grimes 	*p++ = '=';
2554b88c807SRodney W. Grimes 	*p = '\0';
2564b88c807SRodney W. Grimes 	if (val)
2574b88c807SRodney W. Grimes 		scopy(val, p);
2584b88c807SRodney W. Grimes 	setvareq(nameeq, flags);
2594b88c807SRodney W. Grimes }
2604b88c807SRodney W. Grimes 
261ba726b8aSAndrey A. Chernov STATIC int
2625134c3f7SWarner Losh localevar(char *s)
263ba726b8aSAndrey A. Chernov {
264ba726b8aSAndrey A. Chernov 	static char *lnames[7] = {
265ba726b8aSAndrey A. Chernov 		"ALL", "COLLATE", "CTYPE", "MONETARY",
266ba726b8aSAndrey A. Chernov 		"NUMERIC", "TIME", NULL
267ba726b8aSAndrey A. Chernov 	};
268ba726b8aSAndrey A. Chernov 	char **ss;
2694b88c807SRodney W. Grimes 
270ba726b8aSAndrey A. Chernov 	if (*s != 'L')
271ba726b8aSAndrey A. Chernov 		return 0;
272ba726b8aSAndrey A. Chernov 	if (varequal(s + 1, "ANG"))
273ba726b8aSAndrey A. Chernov 		return 1;
274ba726b8aSAndrey A. Chernov 	if (strncmp(s + 1, "C_", 2) != 0)
275ba726b8aSAndrey A. Chernov 		return 0;
276ba726b8aSAndrey A. Chernov 	for (ss = lnames; *ss ; ss++)
277ba726b8aSAndrey A. Chernov 		if (varequal(s + 3, *ss))
278ba726b8aSAndrey A. Chernov 			return 1;
279ba726b8aSAndrey A. Chernov 	return 0;
280ba726b8aSAndrey A. Chernov }
2814b88c807SRodney W. Grimes 
2824b88c807SRodney W. Grimes /*
2834b88c807SRodney W. Grimes  * Same as setvar except that the variable and value are passed in
2844b88c807SRodney W. Grimes  * the first argument as name=value.  Since the first argument will
2854b88c807SRodney W. Grimes  * be actually stored in the table, it should not be a string that
2864b88c807SRodney W. Grimes  * will go away.
2874b88c807SRodney W. Grimes  */
2884b88c807SRodney W. Grimes 
2894b88c807SRodney W. Grimes void
2905134c3f7SWarner Losh setvareq(char *s, int flags)
2914b88c807SRodney W. Grimes {
2924b88c807SRodney W. Grimes 	struct var *vp, **vpp;
29390e41fc4SWarner Losh 	int len;
2944b88c807SRodney W. Grimes 
295b8ec435eSMartin Cracauer 	if (aflag)
296b8ec435eSMartin Cracauer 		flags |= VEXPORT;
2974b88c807SRodney W. Grimes 	vpp = hashvar(s);
2984b88c807SRodney W. Grimes 	for (vp = *vpp ; vp ; vp = vp->next) {
2994b88c807SRodney W. Grimes 		if (varequal(s, vp->text)) {
3004b88c807SRodney W. Grimes 			if (vp->flags & VREADONLY) {
30190e41fc4SWarner Losh 				len = strchr(s, '=') - s;
3024b88c807SRodney W. Grimes 				error("%.*s: is read only", len, s);
3034b88c807SRodney W. Grimes 			}
3044b88c807SRodney W. Grimes 			INTOFF;
305ab0a2172SSteve Price 
306ab0a2172SSteve Price 			if (vp->func && (flags & VNOFUNC) == 0)
307ab0a2172SSteve Price 				(*vp->func)(strchr(s, '=') + 1);
308ab0a2172SSteve Price 
3094b88c807SRodney W. Grimes 			if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3104b88c807SRodney W. Grimes 				ckfree(vp->text);
311ab0a2172SSteve Price 
3124b88c807SRodney W. Grimes 			vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3134b88c807SRodney W. Grimes 			vp->flags |= flags;
3144b88c807SRodney W. Grimes 			vp->text = s;
315ab0a2172SSteve Price 
316ab0a2172SSteve Price 			/*
317ab0a2172SSteve Price 			 * We could roll this to a function, to handle it as
318ab0a2172SSteve Price 			 * a regular variable function callback, but why bother?
319ab0a2172SSteve Price 			 */
3204b88c807SRodney W. Grimes 			if (vp == &vmpath || (vp == &vmail && ! mpathset()))
3214b88c807SRodney W. Grimes 				chkmail(1);
322ba726b8aSAndrey A. Chernov 			if ((vp->flags & VEXPORT) && localevar(s)) {
323ba726b8aSAndrey A. Chernov 				putenv(s);
324ba726b8aSAndrey A. Chernov 				(void) setlocale(LC_ALL, "");
325ba726b8aSAndrey A. Chernov 			}
3264b88c807SRodney W. Grimes 			INTON;
3274b88c807SRodney W. Grimes 			return;
3284b88c807SRodney W. Grimes 		}
3294b88c807SRodney W. Grimes 	}
3304b88c807SRodney W. Grimes 	/* not found */
3314b88c807SRodney W. Grimes 	vp = ckmalloc(sizeof (*vp));
3324b88c807SRodney W. Grimes 	vp->flags = flags;
3334b88c807SRodney W. Grimes 	vp->text = s;
3344b88c807SRodney W. Grimes 	vp->next = *vpp;
335ab0a2172SSteve Price 	vp->func = NULL;
336ba726b8aSAndrey A. Chernov 	INTOFF;
3374b88c807SRodney W. Grimes 	*vpp = vp;
338ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(s)) {
339ba726b8aSAndrey A. Chernov 		putenv(s);
340ba726b8aSAndrey A. Chernov 		(void) setlocale(LC_ALL, "");
341ba726b8aSAndrey A. Chernov 	}
342ba726b8aSAndrey A. Chernov 	INTON;
3434b88c807SRodney W. Grimes }
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 
3464b88c807SRodney W. Grimes 
3474b88c807SRodney W. Grimes /*
3484b88c807SRodney W. Grimes  * Process a linked list of variable assignments.
3494b88c807SRodney W. Grimes  */
3504b88c807SRodney W. Grimes 
3514b88c807SRodney W. Grimes void
3525134c3f7SWarner Losh listsetvar(struct strlist *list)
3534b88c807SRodney W. Grimes {
3544b88c807SRodney W. Grimes 	struct strlist *lp;
3554b88c807SRodney W. Grimes 
3564b88c807SRodney W. Grimes 	INTOFF;
3574b88c807SRodney W. Grimes 	for (lp = list ; lp ; lp = lp->next) {
3584b88c807SRodney W. Grimes 		setvareq(savestr(lp->text), 0);
3594b88c807SRodney W. Grimes 	}
3604b88c807SRodney W. Grimes 	INTON;
3614b88c807SRodney W. Grimes }
3624b88c807SRodney W. Grimes 
3634b88c807SRodney W. Grimes 
3644b88c807SRodney W. Grimes 
3654b88c807SRodney W. Grimes /*
3664b88c807SRodney W. Grimes  * Find the value of a variable.  Returns NULL if not set.
3674b88c807SRodney W. Grimes  */
3684b88c807SRodney W. Grimes 
3694b88c807SRodney W. Grimes char *
3705134c3f7SWarner Losh lookupvar(char *name)
3714b88c807SRodney W. Grimes {
3724b88c807SRodney W. Grimes 	struct var *v;
3734b88c807SRodney W. Grimes 
3744b88c807SRodney W. Grimes 	for (v = *hashvar(name) ; v ; v = v->next) {
3754b88c807SRodney W. Grimes 		if (varequal(v->text, name)) {
3764b88c807SRodney W. Grimes 			if (v->flags & VUNSET)
3774b88c807SRodney W. Grimes 				return NULL;
3784b88c807SRodney W. Grimes 			return strchr(v->text, '=') + 1;
3794b88c807SRodney W. Grimes 		}
3804b88c807SRodney W. Grimes 	}
3814b88c807SRodney W. Grimes 	return NULL;
3824b88c807SRodney W. Grimes }
3834b88c807SRodney W. Grimes 
3844b88c807SRodney W. Grimes 
3854b88c807SRodney W. Grimes 
3864b88c807SRodney W. Grimes /*
3874b88c807SRodney W. Grimes  * Search the environment of a builtin command.  If the second argument
3884b88c807SRodney W. Grimes  * is nonzero, return the value of a variable even if it hasn't been
3894b88c807SRodney W. Grimes  * exported.
3904b88c807SRodney W. Grimes  */
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes char *
3935134c3f7SWarner Losh bltinlookup(char *name, int doall)
3944b88c807SRodney W. Grimes {
3954b88c807SRodney W. Grimes 	struct strlist *sp;
3964b88c807SRodney W. Grimes 	struct var *v;
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes 	for (sp = cmdenviron ; sp ; sp = sp->next) {
3994b88c807SRodney W. Grimes 		if (varequal(sp->text, name))
4004b88c807SRodney W. Grimes 			return strchr(sp->text, '=') + 1;
4014b88c807SRodney W. Grimes 	}
4024b88c807SRodney W. Grimes 	for (v = *hashvar(name) ; v ; v = v->next) {
4034b88c807SRodney W. Grimes 		if (varequal(v->text, name)) {
404aa9caaf6SPeter Wemm 			if ((v->flags & VUNSET)
405aa9caaf6SPeter Wemm 			 || (!doall && (v->flags & VEXPORT) == 0))
4064b88c807SRodney W. Grimes 				return NULL;
4074b88c807SRodney W. Grimes 			return strchr(v->text, '=') + 1;
4084b88c807SRodney W. Grimes 		}
4094b88c807SRodney W. Grimes 	}
4104b88c807SRodney W. Grimes 	return NULL;
4114b88c807SRodney W. Grimes }
4124b88c807SRodney W. Grimes 
4134b88c807SRodney W. Grimes 
4144b88c807SRodney W. Grimes 
4154b88c807SRodney W. Grimes /*
4164b88c807SRodney W. Grimes  * Generate a list of exported variables.  This routine is used to construct
4174b88c807SRodney W. Grimes  * the third argument to execve when executing a program.
4184b88c807SRodney W. Grimes  */
4194b88c807SRodney W. Grimes 
4204b88c807SRodney W. Grimes char **
4215134c3f7SWarner Losh environment(void)
4225134c3f7SWarner Losh {
4234b88c807SRodney W. Grimes 	int nenv;
4244b88c807SRodney W. Grimes 	struct var **vpp;
4254b88c807SRodney W. Grimes 	struct var *vp;
4264b88c807SRodney W. Grimes 	char **env, **ep;
4274b88c807SRodney W. Grimes 
4284b88c807SRodney W. Grimes 	nenv = 0;
4294b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4304b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
4314b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
4324b88c807SRodney W. Grimes 				nenv++;
4334b88c807SRodney W. Grimes 	}
4344b88c807SRodney W. Grimes 	ep = env = stalloc((nenv + 1) * sizeof *env);
4354b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4364b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
4374b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
4384b88c807SRodney W. Grimes 				*ep++ = vp->text;
4394b88c807SRodney W. Grimes 	}
4404b88c807SRodney W. Grimes 	*ep = NULL;
4414b88c807SRodney W. Grimes 	return env;
4424b88c807SRodney W. Grimes }
4434b88c807SRodney W. Grimes 
4444b88c807SRodney W. Grimes 
4454b88c807SRodney W. Grimes /*
4464b88c807SRodney W. Grimes  * Called when a shell procedure is invoked to clear out nonexported
4474b88c807SRodney W. Grimes  * variables.  It is also necessary to reallocate variables of with
4484b88c807SRodney W. Grimes  * VSTACK set since these are currently allocated on the stack.
4494b88c807SRodney W. Grimes  */
4504b88c807SRodney W. Grimes 
4514b88c807SRodney W. Grimes #ifdef mkinit
4524b88c807SRodney W. Grimes MKINIT void shprocvar();
4534b88c807SRodney W. Grimes 
4544b88c807SRodney W. Grimes SHELLPROC {
4554b88c807SRodney W. Grimes 	shprocvar();
4564b88c807SRodney W. Grimes }
4574b88c807SRodney W. Grimes #endif
4584b88c807SRodney W. Grimes 
4594b88c807SRodney W. Grimes void
4605134c3f7SWarner Losh shprocvar(void)
4615134c3f7SWarner Losh {
4624b88c807SRodney W. Grimes 	struct var **vpp;
4634b88c807SRodney W. Grimes 	struct var *vp, **prev;
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
4664b88c807SRodney W. Grimes 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
4674b88c807SRodney W. Grimes 			if ((vp->flags & VEXPORT) == 0) {
4684b88c807SRodney W. Grimes 				*prev = vp->next;
4694b88c807SRodney W. Grimes 				if ((vp->flags & VTEXTFIXED) == 0)
4704b88c807SRodney W. Grimes 					ckfree(vp->text);
4714b88c807SRodney W. Grimes 				if ((vp->flags & VSTRFIXED) == 0)
4724b88c807SRodney W. Grimes 					ckfree(vp);
4734b88c807SRodney W. Grimes 			} else {
4744b88c807SRodney W. Grimes 				if (vp->flags & VSTACK) {
4754b88c807SRodney W. Grimes 					vp->text = savestr(vp->text);
4764b88c807SRodney W. Grimes 					vp->flags &=~ VSTACK;
4774b88c807SRodney W. Grimes 				}
4784b88c807SRodney W. Grimes 				prev = &vp->next;
4794b88c807SRodney W. Grimes 			}
4804b88c807SRodney W. Grimes 		}
4814b88c807SRodney W. Grimes 	}
4824b88c807SRodney W. Grimes 	initvar();
4834b88c807SRodney W. Grimes }
4844b88c807SRodney W. Grimes 
4854b88c807SRodney W. Grimes 
4864b88c807SRodney W. Grimes 
4874b88c807SRodney W. Grimes /*
4884b88c807SRodney W. Grimes  * Command to list all variables which are set.  Currently this command
4894b88c807SRodney W. Grimes  * is invoked from the set command when the set command is called without
4904b88c807SRodney W. Grimes  * any variables.
4914b88c807SRodney W. Grimes  */
4924b88c807SRodney W. Grimes 
4934b88c807SRodney W. Grimes int
4945134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused)
495aa9caaf6SPeter Wemm {
4964b88c807SRodney W. Grimes 	struct var **vpp;
4974b88c807SRodney W. Grimes 	struct var *vp;
49859258844STim J. Robbins 	const char *s;
4994b88c807SRodney W. Grimes 
5004b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5014b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next) {
50259258844STim J. Robbins 			if (vp->flags & VUNSET)
50359258844STim J. Robbins 				continue;
50459258844STim J. Robbins 			for (s = vp->text; *s != '='; s++)
50559258844STim J. Robbins 				out1c(*s);
50659258844STim J. Robbins 			out1c('=');
50759258844STim J. Robbins 			out1qstr(s + 1);
50859258844STim J. Robbins 			out1c('\n');
5094b88c807SRodney W. Grimes 		}
5104b88c807SRodney W. Grimes 	}
5114b88c807SRodney W. Grimes 	return 0;
5124b88c807SRodney W. Grimes }
5134b88c807SRodney W. Grimes 
5144b88c807SRodney W. Grimes 
5154b88c807SRodney W. Grimes 
5164b88c807SRodney W. Grimes /*
5174b88c807SRodney W. Grimes  * The export and readonly commands.
5184b88c807SRodney W. Grimes  */
5194b88c807SRodney W. Grimes 
5204b88c807SRodney W. Grimes int
5215134c3f7SWarner Losh exportcmd(int argc, char **argv)
522aa9caaf6SPeter Wemm {
5234b88c807SRodney W. Grimes 	struct var **vpp;
5244b88c807SRodney W. Grimes 	struct var *vp;
5254b88c807SRodney W. Grimes 	char *name;
5264b88c807SRodney W. Grimes 	char *p;
52745086f8cSTim J. Robbins 	char *cmdname;
52845086f8cSTim J. Robbins 	int ch, values;
5294b88c807SRodney W. Grimes 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
5304b88c807SRodney W. Grimes 
53145086f8cSTim J. Robbins 	cmdname = argv[0];
53245086f8cSTim J. Robbins 	optreset = optind = 1;
533050f7913STim J. Robbins 	opterr = 0;
53445086f8cSTim J. Robbins 	values = 0;
53545086f8cSTim J. Robbins 	while ((ch = getopt(argc, argv, "p")) != -1) {
53645086f8cSTim J. Robbins 		switch (ch) {
53745086f8cSTim J. Robbins 		case 'p':
53845086f8cSTim J. Robbins 			values = 1;
53945086f8cSTim J. Robbins 			break;
54045086f8cSTim J. Robbins 		case '?':
54145086f8cSTim J. Robbins 		default:
54245086f8cSTim J. Robbins 			error("unknown option: -%c", optopt);
54345086f8cSTim J. Robbins 		}
54445086f8cSTim J. Robbins 	}
54545086f8cSTim J. Robbins 	argc -= optind;
54645086f8cSTim J. Robbins 	argv += optind;
54745086f8cSTim J. Robbins 
5484b88c807SRodney W. Grimes 	listsetvar(cmdenviron);
54945086f8cSTim J. Robbins 	if (argc != 0) {
5504b88c807SRodney W. Grimes 		while ((name = *argptr++) != NULL) {
5514b88c807SRodney W. Grimes 			if ((p = strchr(name, '=')) != NULL) {
5524b88c807SRodney W. Grimes 				p++;
5534b88c807SRodney W. Grimes 			} else {
5544b88c807SRodney W. Grimes 				vpp = hashvar(name);
5554b88c807SRodney W. Grimes 				for (vp = *vpp ; vp ; vp = vp->next) {
5564b88c807SRodney W. Grimes 					if (varequal(vp->text, name)) {
5575134c3f7SWarner Losh 
5584b88c807SRodney W. Grimes 						vp->flags |= flag;
559ba726b8aSAndrey A. Chernov 						if ((vp->flags & VEXPORT) && localevar(vp->text)) {
560ba726b8aSAndrey A. Chernov 							putenv(vp->text);
561ba726b8aSAndrey A. Chernov 							(void) setlocale(LC_ALL, "");
562ba726b8aSAndrey A. Chernov 						}
5634b88c807SRodney W. Grimes 						goto found;
5644b88c807SRodney W. Grimes 					}
5654b88c807SRodney W. Grimes 				}
5664b88c807SRodney W. Grimes 			}
5674b88c807SRodney W. Grimes 			setvar(name, p, flag);
5684b88c807SRodney W. Grimes found:;
5694b88c807SRodney W. Grimes 		}
5704b88c807SRodney W. Grimes 	} else {
5714b88c807SRodney W. Grimes 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5724b88c807SRodney W. Grimes 			for (vp = *vpp ; vp ; vp = vp->next) {
5734b88c807SRodney W. Grimes 				if (vp->flags & flag) {
57445086f8cSTim J. Robbins 					if (values) {
57545086f8cSTim J. Robbins 						out1str(cmdname);
57645086f8cSTim J. Robbins 						out1c(' ');
57745086f8cSTim J. Robbins 					}
5784b88c807SRodney W. Grimes 					for (p = vp->text ; *p != '=' ; p++)
5794b88c807SRodney W. Grimes 						out1c(*p);
58045086f8cSTim J. Robbins 					if (values && !(vp->flags & VUNSET)) {
58145086f8cSTim J. Robbins 						out1c('=');
58245086f8cSTim J. Robbins 						out1qstr(p + 1);
58345086f8cSTim J. Robbins 					}
5844b88c807SRodney W. Grimes 					out1c('\n');
5854b88c807SRodney W. Grimes 				}
5864b88c807SRodney W. Grimes 			}
5874b88c807SRodney W. Grimes 		}
5884b88c807SRodney W. Grimes 	}
5894b88c807SRodney W. Grimes 	return 0;
5904b88c807SRodney W. Grimes }
5914b88c807SRodney W. Grimes 
5924b88c807SRodney W. Grimes 
5934b88c807SRodney W. Grimes /*
5944b88c807SRodney W. Grimes  * The "local" command.
5954b88c807SRodney W. Grimes  */
5964b88c807SRodney W. Grimes 
597aa9caaf6SPeter Wemm int
5985134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused)
599aa9caaf6SPeter Wemm {
6004b88c807SRodney W. Grimes 	char *name;
6014b88c807SRodney W. Grimes 
6024b88c807SRodney W. Grimes 	if (! in_function())
6034b88c807SRodney W. Grimes 		error("Not in a function");
6044b88c807SRodney W. Grimes 	while ((name = *argptr++) != NULL) {
6054b88c807SRodney W. Grimes 		mklocal(name);
6064b88c807SRodney W. Grimes 	}
6074b88c807SRodney W. Grimes 	return 0;
6084b88c807SRodney W. Grimes }
6094b88c807SRodney W. Grimes 
6104b88c807SRodney W. Grimes 
6114b88c807SRodney W. Grimes /*
6124b88c807SRodney W. Grimes  * Make a variable a local variable.  When a variable is made local, it's
6134b88c807SRodney W. Grimes  * value and flags are saved in a localvar structure.  The saved values
6144b88c807SRodney W. Grimes  * will be restored when the shell function returns.  We handle the name
6154b88c807SRodney W. Grimes  * "-" as a special case.
6164b88c807SRodney W. Grimes  */
6174b88c807SRodney W. Grimes 
6184b88c807SRodney W. Grimes void
6195134c3f7SWarner Losh mklocal(char *name)
6204b88c807SRodney W. Grimes {
6214b88c807SRodney W. Grimes 	struct localvar *lvp;
6224b88c807SRodney W. Grimes 	struct var **vpp;
6234b88c807SRodney W. Grimes 	struct var *vp;
6244b88c807SRodney W. Grimes 
6254b88c807SRodney W. Grimes 	INTOFF;
6264b88c807SRodney W. Grimes 	lvp = ckmalloc(sizeof (struct localvar));
6274b88c807SRodney W. Grimes 	if (name[0] == '-' && name[1] == '\0') {
6284b88c807SRodney W. Grimes 		lvp->text = ckmalloc(sizeof optlist);
629aa9caaf6SPeter Wemm 		memcpy(lvp->text, optlist, sizeof optlist);
6304b88c807SRodney W. Grimes 		vp = NULL;
6314b88c807SRodney W. Grimes 	} else {
6324b88c807SRodney W. Grimes 		vpp = hashvar(name);
6334b88c807SRodney W. Grimes 		for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
6344b88c807SRodney W. Grimes 		if (vp == NULL) {
6354b88c807SRodney W. Grimes 			if (strchr(name, '='))
6364b88c807SRodney W. Grimes 				setvareq(savestr(name), VSTRFIXED);
6374b88c807SRodney W. Grimes 			else
6384b88c807SRodney W. Grimes 				setvar(name, NULL, VSTRFIXED);
6394b88c807SRodney W. Grimes 			vp = *vpp;	/* the new variable */
6404b88c807SRodney W. Grimes 			lvp->text = NULL;
6414b88c807SRodney W. Grimes 			lvp->flags = VUNSET;
6424b88c807SRodney W. Grimes 		} else {
6434b88c807SRodney W. Grimes 			lvp->text = vp->text;
6444b88c807SRodney W. Grimes 			lvp->flags = vp->flags;
6454b88c807SRodney W. Grimes 			vp->flags |= VSTRFIXED|VTEXTFIXED;
6464b88c807SRodney W. Grimes 			if (strchr(name, '='))
6474b88c807SRodney W. Grimes 				setvareq(savestr(name), 0);
6484b88c807SRodney W. Grimes 		}
6494b88c807SRodney W. Grimes 	}
6504b88c807SRodney W. Grimes 	lvp->vp = vp;
6514b88c807SRodney W. Grimes 	lvp->next = localvars;
6524b88c807SRodney W. Grimes 	localvars = lvp;
6534b88c807SRodney W. Grimes 	INTON;
6544b88c807SRodney W. Grimes }
6554b88c807SRodney W. Grimes 
6564b88c807SRodney W. Grimes 
6574b88c807SRodney W. Grimes /*
6584b88c807SRodney W. Grimes  * Called after a function returns.
6594b88c807SRodney W. Grimes  */
6604b88c807SRodney W. Grimes 
6614b88c807SRodney W. Grimes void
6625134c3f7SWarner Losh poplocalvars(void)
6635134c3f7SWarner Losh {
6644b88c807SRodney W. Grimes 	struct localvar *lvp;
6654b88c807SRodney W. Grimes 	struct var *vp;
6664b88c807SRodney W. Grimes 
6674b88c807SRodney W. Grimes 	while ((lvp = localvars) != NULL) {
6684b88c807SRodney W. Grimes 		localvars = lvp->next;
6694b88c807SRodney W. Grimes 		vp = lvp->vp;
6704b88c807SRodney W. Grimes 		if (vp == NULL) {	/* $- saved */
671aa9caaf6SPeter Wemm 			memcpy(optlist, lvp->text, sizeof optlist);
6724b88c807SRodney W. Grimes 			ckfree(lvp->text);
6734b88c807SRodney W. Grimes 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
6744b88c807SRodney W. Grimes 			(void)unsetvar(vp->text);
6754b88c807SRodney W. Grimes 		} else {
6764b88c807SRodney W. Grimes 			if ((vp->flags & VTEXTFIXED) == 0)
6774b88c807SRodney W. Grimes 				ckfree(vp->text);
6784b88c807SRodney W. Grimes 			vp->flags = lvp->flags;
6794b88c807SRodney W. Grimes 			vp->text = lvp->text;
6804b88c807SRodney W. Grimes 		}
6814b88c807SRodney W. Grimes 		ckfree(lvp);
6824b88c807SRodney W. Grimes 	}
6834b88c807SRodney W. Grimes }
6844b88c807SRodney W. Grimes 
6854b88c807SRodney W. Grimes 
686aa9caaf6SPeter Wemm int
6875134c3f7SWarner Losh setvarcmd(int argc, char **argv)
688aa9caaf6SPeter Wemm {
6894b88c807SRodney W. Grimes 	if (argc <= 2)
6904b88c807SRodney W. Grimes 		return unsetcmd(argc, argv);
6914b88c807SRodney W. Grimes 	else if (argc == 3)
6924b88c807SRodney W. Grimes 		setvar(argv[1], argv[2], 0);
6934b88c807SRodney W. Grimes 	else
6944b88c807SRodney W. Grimes 		error("List assignment not implemented");
6954b88c807SRodney W. Grimes 	return 0;
6964b88c807SRodney W. Grimes }
6974b88c807SRodney W. Grimes 
6984b88c807SRodney W. Grimes 
6994b88c807SRodney W. Grimes /*
7004b88c807SRodney W. Grimes  * The unset builtin command.  We unset the function before we unset the
7014b88c807SRodney W. Grimes  * variable to allow a function to be unset when there is a readonly variable
7024b88c807SRodney W. Grimes  * with the same name.
7034b88c807SRodney W. Grimes  */
7044b88c807SRodney W. Grimes 
705aa9caaf6SPeter Wemm int
7065134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused)
707aa9caaf6SPeter Wemm {
7084b88c807SRodney W. Grimes 	char **ap;
7094b88c807SRodney W. Grimes 	int i;
7104b88c807SRodney W. Grimes 	int flg_func = 0;
7114b88c807SRodney W. Grimes 	int flg_var = 0;
7124b88c807SRodney W. Grimes 	int ret = 0;
7134b88c807SRodney W. Grimes 
7144b88c807SRodney W. Grimes 	while ((i = nextopt("vf")) != '\0') {
7154b88c807SRodney W. Grimes 		if (i == 'f')
7164b88c807SRodney W. Grimes 			flg_func = 1;
7174b88c807SRodney W. Grimes 		else
7184b88c807SRodney W. Grimes 			flg_var = 1;
7194b88c807SRodney W. Grimes 	}
7204b88c807SRodney W. Grimes 	if (flg_func == 0 && flg_var == 0)
7214b88c807SRodney W. Grimes 		flg_var = 1;
7224b88c807SRodney W. Grimes 
7234b88c807SRodney W. Grimes 	for (ap = argptr; *ap ; ap++) {
7244b88c807SRodney W. Grimes 		if (flg_func)
7254b88c807SRodney W. Grimes 			ret |= unsetfunc(*ap);
7264b88c807SRodney W. Grimes 		if (flg_var)
7274b88c807SRodney W. Grimes 			ret |= unsetvar(*ap);
7284b88c807SRodney W. Grimes 	}
7294b88c807SRodney W. Grimes 	return ret;
7304b88c807SRodney W. Grimes }
7314b88c807SRodney W. Grimes 
7324b88c807SRodney W. Grimes 
7334b88c807SRodney W. Grimes /*
7344b88c807SRodney W. Grimes  * Unset the specified variable.
7354b88c807SRodney W. Grimes  */
7364b88c807SRodney W. Grimes 
737ab0a2172SSteve Price int
7385134c3f7SWarner Losh unsetvar(char *s)
7394b88c807SRodney W. Grimes {
7404b88c807SRodney W. Grimes 	struct var **vpp;
7414b88c807SRodney W. Grimes 	struct var *vp;
7424b88c807SRodney W. Grimes 
7434b88c807SRodney W. Grimes 	vpp = hashvar(s);
7444b88c807SRodney W. Grimes 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
7454b88c807SRodney W. Grimes 		if (varequal(vp->text, s)) {
7464b88c807SRodney W. Grimes 			if (vp->flags & VREADONLY)
7474b88c807SRodney W. Grimes 				return (1);
7484b88c807SRodney W. Grimes 			INTOFF;
7494b88c807SRodney W. Grimes 			if (*(strchr(vp->text, '=') + 1) != '\0')
7504b88c807SRodney W. Grimes 				setvar(s, nullstr, 0);
751ba726b8aSAndrey A. Chernov 			if ((vp->flags & VEXPORT) && localevar(vp->text)) {
752ba726b8aSAndrey A. Chernov 				unsetenv(s);
753ba726b8aSAndrey A. Chernov 				setlocale(LC_ALL, "");
754ba726b8aSAndrey A. Chernov 			}
7554b88c807SRodney W. Grimes 			vp->flags &= ~VEXPORT;
7564b88c807SRodney W. Grimes 			vp->flags |= VUNSET;
7574b88c807SRodney W. Grimes 			if ((vp->flags & VSTRFIXED) == 0) {
7584b88c807SRodney W. Grimes 				if ((vp->flags & VTEXTFIXED) == 0)
7594b88c807SRodney W. Grimes 					ckfree(vp->text);
7604b88c807SRodney W. Grimes 				*vpp = vp->next;
7614b88c807SRodney W. Grimes 				ckfree(vp);
7624b88c807SRodney W. Grimes 			}
7634b88c807SRodney W. Grimes 			INTON;
7644b88c807SRodney W. Grimes 			return (0);
7654b88c807SRodney W. Grimes 		}
7664b88c807SRodney W. Grimes 	}
7674b88c807SRodney W. Grimes 
7684b88c807SRodney W. Grimes 	return (1);
7694b88c807SRodney W. Grimes }
7704b88c807SRodney W. Grimes 
7714b88c807SRodney W. Grimes 
7724b88c807SRodney W. Grimes 
7734b88c807SRodney W. Grimes /*
7744b88c807SRodney W. Grimes  * Find the appropriate entry in the hash table from the name.
7754b88c807SRodney W. Grimes  */
7764b88c807SRodney W. Grimes 
7774b88c807SRodney W. Grimes STATIC struct var **
7785134c3f7SWarner Losh hashvar(char *p)
7794b88c807SRodney W. Grimes {
7804b88c807SRodney W. Grimes 	unsigned int hashval;
7814b88c807SRodney W. Grimes 
782ba726b8aSAndrey A. Chernov 	hashval = ((unsigned char) *p) << 4;
7834b88c807SRodney W. Grimes 	while (*p && *p != '=')
784ba726b8aSAndrey A. Chernov 		hashval += (unsigned char) *p++;
7854b88c807SRodney W. Grimes 	return &vartab[hashval % VTABSIZE];
7864b88c807SRodney W. Grimes }
7874b88c807SRodney W. Grimes 
7884b88c807SRodney W. Grimes 
7894b88c807SRodney W. Grimes 
7904b88c807SRodney W. Grimes /*
7914b88c807SRodney W. Grimes  * Returns true if the two strings specify the same varable.  The first
7924b88c807SRodney W. Grimes  * variable name is terminated by '='; the second may be terminated by
7934b88c807SRodney W. Grimes  * either '=' or '\0'.
7944b88c807SRodney W. Grimes  */
7954b88c807SRodney W. Grimes 
7964b88c807SRodney W. Grimes STATIC int
7975134c3f7SWarner Losh varequal(char *p, char *q)
7984b88c807SRodney W. Grimes {
7994b88c807SRodney W. Grimes 	while (*p == *q++) {
8004b88c807SRodney W. Grimes 		if (*p++ == '=')
8014b88c807SRodney W. Grimes 			return 1;
8024b88c807SRodney W. Grimes 	}
8034b88c807SRodney W. Grimes 	if (*p == '=' && *(q - 1) == '\0')
8044b88c807SRodney W. Grimes 		return 1;
8054b88c807SRodney W. Grimes 	return 0;
8064b88c807SRodney W. Grimes }
807