xref: /freebsd/bin/sh/var.c (revision 454a02b3728543503f2f30771a867148a27f71b3)
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>
506ed74a0aSJilles Tjoelker #include <langinfo.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"
614b88c807SRodney W. Grimes #include "var.h"
624b88c807SRodney W. Grimes #include "memalloc.h"
634b88c807SRodney W. Grimes #include "error.h"
644b88c807SRodney W. Grimes #include "mystring.h"
65ab0a2172SSteve Price #include "parser.h"
66*454a02b3SJilles Tjoelker #include "builtins.h"
67aa9caaf6SPeter Wemm #ifndef NO_HISTORY
68aa9caaf6SPeter Wemm #include "myhistedit.h"
69aa9caaf6SPeter Wemm #endif
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes #define VTABSIZE 39
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes struct varinit {
764b88c807SRodney W. Grimes 	struct var *var;
774b88c807SRodney W. Grimes 	int flags;
78c6c5dd37SJilles Tjoelker 	const char *text;
795134c3f7SWarner Losh 	void (*func)(const char *);
804b88c807SRodney W. Grimes };
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes 
83aa9caaf6SPeter Wemm #ifndef NO_HISTORY
844b88c807SRodney W. Grimes struct var vhistsize;
85580eefdfSJilles Tjoelker struct var vterm;
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;
9139dccc6fSTim J. Robbins struct var vppid;
924b88c807SRodney W. Grimes struct var vps1;
934b88c807SRodney W. Grimes struct var vps2;
94120c8e6cSStefan Farfeleder struct var vps4;
954b88c807SRodney W. Grimes struct var vvers;
96aa7b6f82SDavid E. O'Brien static struct var voptind;
974b88c807SRodney W. Grimes 
98c543e1aeSJilles Tjoelker int forcelocal;
99c543e1aeSJilles Tjoelker 
100aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = {
101aa9caaf6SPeter Wemm #ifndef NO_HISTORY
102c6c5dd37SJilles Tjoelker 	{ &vhistsize,	VUNSET,				"HISTSIZE=",
103ab0a2172SSteve Price 	  sethistsize },
104aa9caaf6SPeter Wemm #endif
105c6c5dd37SJilles Tjoelker 	{ &vifs,	0,				"IFS= \t\n",
106ab0a2172SSteve Price 	  NULL },
107c6c5dd37SJilles Tjoelker 	{ &vmail,	VUNSET,				"MAIL=",
108ab0a2172SSteve Price 	  NULL },
109c6c5dd37SJilles Tjoelker 	{ &vmpath,	VUNSET,				"MAILPATH=",
110ab0a2172SSteve Price 	  NULL },
111c6c5dd37SJilles Tjoelker 	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
112ab0a2172SSteve Price 	  changepath },
113c6c5dd37SJilles Tjoelker 	{ &vppid,	VUNSET,				"PPID=",
11439dccc6fSTim J. Robbins 	  NULL },
1154b88c807SRodney W. Grimes 	/*
1164b88c807SRodney W. Grimes 	 * vps1 depends on uid
1174b88c807SRodney W. Grimes 	 */
118c6c5dd37SJilles Tjoelker 	{ &vps2,	0,				"PS2=> ",
119ab0a2172SSteve Price 	  NULL },
120c6c5dd37SJilles Tjoelker 	{ &vps4,	0,				"PS4=+ ",
121120c8e6cSStefan Farfeleder 	  NULL },
122580eefdfSJilles Tjoelker #ifndef NO_HISTORY
123580eefdfSJilles Tjoelker 	{ &vterm,	VUNSET,				"TERM=",
124580eefdfSJilles Tjoelker 	  setterm },
125580eefdfSJilles Tjoelker #endif
126c6c5dd37SJilles Tjoelker 	{ &voptind,	0,				"OPTIND=1",
127ab0a2172SSteve Price 	  getoptsreset },
128ab0a2172SSteve Price 	{ NULL,	0,				NULL,
129ab0a2172SSteve Price 	  NULL }
1304b88c807SRodney W. Grimes };
1314b88c807SRodney W. Grimes 
132aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE];
1334b88c807SRodney W. Grimes 
134aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = {
135e4b50334SJilles Tjoelker 	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
136e4b50334SJilles Tjoelker 	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
137e4b50334SJilles Tjoelker };
138aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = {
139e4b50334SJilles Tjoelker 	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
140e4b50334SJilles Tjoelker };
141e4b50334SJilles Tjoelker 
14288328642SDavid E. O'Brien static int varequal(const char *, const char *);
1433a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *);
14488328642SDavid E. O'Brien static int localevar(const char *);
1454b88c807SRodney W. Grimes 
1464b88c807SRodney W. Grimes /*
147b3decf89SJens Schweikhardt  * Initialize the variable symbol tables and import the environment.
1484b88c807SRodney W. Grimes  */
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes #ifdef mkinit
1514b88c807SRodney W. Grimes INCLUDE "var.h"
152384aedabSJilles Tjoelker MKINIT char **environ;
1534b88c807SRodney W. Grimes INIT {
1544b88c807SRodney W. Grimes 	char **envp;
1554b88c807SRodney W. Grimes 
1564b88c807SRodney W. Grimes 	initvar();
1574b88c807SRodney W. Grimes 	for (envp = environ ; *envp ; envp++) {
1584b88c807SRodney W. Grimes 		if (strchr(*envp, '=')) {
1594b88c807SRodney W. Grimes 			setvareq(*envp, VEXPORT|VTEXTFIXED);
1604b88c807SRodney W. Grimes 		}
1614b88c807SRodney W. Grimes 	}
1624b88c807SRodney W. Grimes }
1634b88c807SRodney W. Grimes #endif
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes /*
1674b88c807SRodney W. Grimes  * This routine initializes the builtin variables.  It is called when the
1683835f47cSJilles Tjoelker  * shell is initialized.
1694b88c807SRodney W. Grimes  */
1704b88c807SRodney W. Grimes 
1714b88c807SRodney W. Grimes void
1725134c3f7SWarner Losh initvar(void)
1735134c3f7SWarner Losh {
17439dccc6fSTim J. Robbins 	char ppid[20];
1754b88c807SRodney W. Grimes 	const struct varinit *ip;
1764b88c807SRodney W. Grimes 	struct var *vp;
1774b88c807SRodney W. Grimes 	struct var **vpp;
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1803a99ed46SJilles Tjoelker 		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
1813a99ed46SJilles Tjoelker 			continue;
1824b88c807SRodney W. Grimes 		vp->next = *vpp;
1834b88c807SRodney W. Grimes 		*vpp = vp;
184c6c5dd37SJilles Tjoelker 		vp->text = __DECONST(char *, ip->text);
185c6c5dd37SJilles Tjoelker 		vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
186ab0a2172SSteve Price 		vp->func = ip->func;
1874b88c807SRodney W. Grimes 	}
1884b88c807SRodney W. Grimes 	/*
1894b88c807SRodney W. Grimes 	 * PS1 depends on uid
1904b88c807SRodney W. Grimes 	 */
1913a99ed46SJilles Tjoelker 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
1924b88c807SRodney W. Grimes 		vps1.next = *vpp;
1934b88c807SRodney W. Grimes 		*vpp = &vps1;
194c6c5dd37SJilles Tjoelker 		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
1954b88c807SRodney W. Grimes 		vps1.flags = VSTRFIXED|VTEXTFIXED;
1964b88c807SRodney W. Grimes 	}
19739dccc6fSTim J. Robbins 	if ((vppid.flags & VEXPORT) == 0) {
19839dccc6fSTim J. Robbins 		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
19939dccc6fSTim J. Robbins 		setvarsafe("PPID", ppid, 0);
20039dccc6fSTim J. Robbins 	}
2014b88c807SRodney W. Grimes }
2024b88c807SRodney W. Grimes 
2034b88c807SRodney W. Grimes /*
204ab0a2172SSteve Price  * Safe version of setvar, returns 1 on success 0 on failure.
205ab0a2172SSteve Price  */
206ab0a2172SSteve Price 
207ab0a2172SSteve Price int
2082cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags)
209ab0a2172SSteve Price {
210ab0a2172SSteve Price 	struct jmploc jmploc;
211224fbf9fSJilles Tjoelker 	struct jmploc *const savehandler = handler;
212ab0a2172SSteve Price 	int err = 0;
2139922c6d2SJilles Tjoelker 	int inton;
214ab0a2172SSteve Price 
2159922c6d2SJilles Tjoelker 	inton = is_int_on();
216ab0a2172SSteve Price 	if (setjmp(jmploc.loc))
217ab0a2172SSteve Price 		err = 1;
218ab0a2172SSteve Price 	else {
219ab0a2172SSteve Price 		handler = &jmploc;
220ab0a2172SSteve Price 		setvar(name, val, flags);
221ab0a2172SSteve Price 	}
222ab0a2172SSteve Price 	handler = savehandler;
2239922c6d2SJilles Tjoelker 	SETINTON(inton);
224ab0a2172SSteve Price 	return err;
225ab0a2172SSteve Price }
226ab0a2172SSteve Price 
227ab0a2172SSteve Price /*
228b3decf89SJens Schweikhardt  * Set the value of a variable.  The flags argument is stored with the
2294b88c807SRodney W. Grimes  * flags of the variable.  If val is NULL, the variable is unset.
2304b88c807SRodney W. Grimes  */
2314b88c807SRodney W. Grimes 
2324b88c807SRodney W. Grimes void
2332cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags)
2344b88c807SRodney W. Grimes {
2352cac6e36SJilles Tjoelker 	const char *p;
2364b88c807SRodney W. Grimes 	int len;
2374b88c807SRodney W. Grimes 	int namelen;
2384b88c807SRodney W. Grimes 	char *nameeq;
2394b88c807SRodney W. Grimes 	int isbad;
2404b88c807SRodney W. Grimes 
2414b88c807SRodney W. Grimes 	isbad = 0;
2424b88c807SRodney W. Grimes 	p = name;
243ba726b8aSAndrey A. Chernov 	if (! is_name(*p))
2444b88c807SRodney W. Grimes 		isbad = 1;
245ba726b8aSAndrey A. Chernov 	p++;
2464b88c807SRodney W. Grimes 	for (;;) {
2474b88c807SRodney W. Grimes 		if (! is_in_name(*p)) {
2484b88c807SRodney W. Grimes 			if (*p == '\0' || *p == '=')
2494b88c807SRodney W. Grimes 				break;
2504b88c807SRodney W. Grimes 			isbad = 1;
2514b88c807SRodney W. Grimes 		}
2524b88c807SRodney W. Grimes 		p++;
2534b88c807SRodney W. Grimes 	}
2544b88c807SRodney W. Grimes 	namelen = p - name;
2554b88c807SRodney W. Grimes 	if (isbad)
2564b88c807SRodney W. Grimes 		error("%.*s: bad variable name", namelen, name);
2574b88c807SRodney W. Grimes 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2584b88c807SRodney W. Grimes 	if (val == NULL) {
2594b88c807SRodney W. Grimes 		flags |= VUNSET;
2604b88c807SRodney W. Grimes 	} else {
2614b88c807SRodney W. Grimes 		len += strlen(val);
2624b88c807SRodney W. Grimes 	}
2632cac6e36SJilles Tjoelker 	nameeq = ckmalloc(len);
2642cac6e36SJilles Tjoelker 	memcpy(nameeq, name, namelen);
2652cac6e36SJilles Tjoelker 	nameeq[namelen] = '=';
2664b88c807SRodney W. Grimes 	if (val)
2672cac6e36SJilles Tjoelker 		scopy(val, nameeq + namelen + 1);
2682cac6e36SJilles Tjoelker 	else
2692cac6e36SJilles Tjoelker 		nameeq[namelen + 1] = '\0';
2704b88c807SRodney W. Grimes 	setvareq(nameeq, flags);
2714b88c807SRodney W. Grimes }
2724b88c807SRodney W. Grimes 
27388328642SDavid E. O'Brien static int
2742cac6e36SJilles Tjoelker localevar(const char *s)
275ba726b8aSAndrey A. Chernov {
276e4b50334SJilles Tjoelker 	const char *const *ss;
2774b88c807SRodney W. Grimes 
278ba726b8aSAndrey A. Chernov 	if (*s != 'L')
279ba726b8aSAndrey A. Chernov 		return 0;
280ba726b8aSAndrey A. Chernov 	if (varequal(s + 1, "ANG"))
281ba726b8aSAndrey A. Chernov 		return 1;
282ba726b8aSAndrey A. Chernov 	if (strncmp(s + 1, "C_", 2) != 0)
283ba726b8aSAndrey A. Chernov 		return 0;
284e4b50334SJilles Tjoelker 	if (varequal(s + 3, "ALL"))
285e4b50334SJilles Tjoelker 		return 1;
286e4b50334SJilles Tjoelker 	for (ss = locale_names; *ss ; ss++)
287e4b50334SJilles Tjoelker 		if (varequal(s + 3, *ss + 3))
288ba726b8aSAndrey A. Chernov 			return 1;
289ba726b8aSAndrey A. Chernov 	return 0;
290ba726b8aSAndrey A. Chernov }
2914b88c807SRodney W. Grimes 
2925b78c6c2SSean Farley 
2935b78c6c2SSean Farley /*
2945b78c6c2SSean Farley  * Sets/unsets an environment variable from a pointer that may actually be a
2955b78c6c2SSean Farley  * pointer into environ where the string should not be manipulated.
2965b78c6c2SSean Farley  */
29788328642SDavid E. O'Brien static void
2982cac6e36SJilles Tjoelker change_env(const char *s, int set)
2995b78c6c2SSean Farley {
3005b78c6c2SSean Farley 	char *eqp;
3015b78c6c2SSean Farley 	char *ss;
3025b78c6c2SSean Farley 
3035b78c6c2SSean Farley 	ss = savestr(s);
3045b78c6c2SSean Farley 	if ((eqp = strchr(ss, '=')) != NULL)
3055b78c6c2SSean Farley 		*eqp = '\0';
3065b78c6c2SSean Farley 	if (set && eqp != NULL)
3075b78c6c2SSean Farley 		(void) setenv(ss, eqp + 1, 1);
3085b78c6c2SSean Farley 	else
3095b78c6c2SSean Farley 		(void) unsetenv(ss);
3105b78c6c2SSean Farley 	ckfree(ss);
3115b78c6c2SSean Farley 
3125b78c6c2SSean Farley 	return;
3135b78c6c2SSean Farley }
3145b78c6c2SSean Farley 
3155b78c6c2SSean Farley 
3164b88c807SRodney W. Grimes /*
3174b88c807SRodney W. Grimes  * Same as setvar except that the variable and value are passed in
3184b88c807SRodney W. Grimes  * the first argument as name=value.  Since the first argument will
3194b88c807SRodney W. Grimes  * be actually stored in the table, it should not be a string that
3204b88c807SRodney W. Grimes  * will go away.
3214b88c807SRodney W. Grimes  */
3224b88c807SRodney W. Grimes 
3234b88c807SRodney W. Grimes void
3245134c3f7SWarner Losh setvareq(char *s, int flags)
3254b88c807SRodney W. Grimes {
3264b88c807SRodney W. Grimes 	struct var *vp, **vpp;
3273a99ed46SJilles Tjoelker 	int nlen;
3284b88c807SRodney W. Grimes 
329b8ec435eSMartin Cracauer 	if (aflag)
330b8ec435eSMartin Cracauer 		flags |= VEXPORT;
331c543e1aeSJilles Tjoelker 	if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
332c543e1aeSJilles Tjoelker 		mklocal(s);
3333a99ed46SJilles Tjoelker 	vp = find_var(s, &vpp, &nlen);
3343a99ed46SJilles Tjoelker 	if (vp != NULL) {
3353a99ed46SJilles Tjoelker 		if (vp->flags & VREADONLY)
3363a99ed46SJilles Tjoelker 			error("%.*s: is read only", vp->name_len, s);
337850460c0SJilles Tjoelker 		if (flags & VNOSET)
338850460c0SJilles Tjoelker 			return;
3394b88c807SRodney W. Grimes 		INTOFF;
340ab0a2172SSteve Price 
341ab0a2172SSteve Price 		if (vp->func && (flags & VNOFUNC) == 0)
3423a99ed46SJilles Tjoelker 			(*vp->func)(s + vp->name_len + 1);
343ab0a2172SSteve Price 
3444b88c807SRodney W. Grimes 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3454b88c807SRodney W. Grimes 			ckfree(vp->text);
346ab0a2172SSteve Price 
3474b88c807SRodney W. Grimes 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3484b88c807SRodney W. Grimes 		vp->flags |= flags;
3494b88c807SRodney W. Grimes 		vp->text = s;
350ab0a2172SSteve Price 
351ab0a2172SSteve Price 		/*
352ab0a2172SSteve Price 		 * We could roll this to a function, to handle it as
353ab0a2172SSteve Price 		 * a regular variable function callback, but why bother?
35457063576SJilles Tjoelker 		 *
35557063576SJilles Tjoelker 		 * Note: this assumes iflag is not set to 1 initially.
35657063576SJilles Tjoelker 		 * As part of init(), this is called before arguments
35757063576SJilles Tjoelker 		 * are looked at.
358ab0a2172SSteve Price 		 */
35957063576SJilles Tjoelker 		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
36057063576SJilles Tjoelker 		    iflag == 1)
3614b88c807SRodney W. Grimes 			chkmail(1);
362ba726b8aSAndrey A. Chernov 		if ((vp->flags & VEXPORT) && localevar(s)) {
3635b78c6c2SSean Farley 			change_env(s, 1);
364ba726b8aSAndrey A. Chernov 			(void) setlocale(LC_ALL, "");
3656ed74a0aSJilles Tjoelker 			updatecharset();
366ba726b8aSAndrey A. Chernov 		}
3674b88c807SRodney W. Grimes 		INTON;
3684b88c807SRodney W. Grimes 		return;
3694b88c807SRodney W. Grimes 	}
3704b88c807SRodney W. Grimes 	/* not found */
371850460c0SJilles Tjoelker 	if (flags & VNOSET)
372850460c0SJilles Tjoelker 		return;
3734b88c807SRodney W. Grimes 	vp = ckmalloc(sizeof (*vp));
3744b88c807SRodney W. Grimes 	vp->flags = flags;
3754b88c807SRodney W. Grimes 	vp->text = s;
3763a99ed46SJilles Tjoelker 	vp->name_len = nlen;
3774b88c807SRodney W. Grimes 	vp->next = *vpp;
378ab0a2172SSteve Price 	vp->func = NULL;
379ba726b8aSAndrey A. Chernov 	INTOFF;
3804b88c807SRodney W. Grimes 	*vpp = vp;
381ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(s)) {
3825b78c6c2SSean Farley 		change_env(s, 1);
383ba726b8aSAndrey A. Chernov 		(void) setlocale(LC_ALL, "");
3846ed74a0aSJilles Tjoelker 		updatecharset();
385ba726b8aSAndrey A. Chernov 	}
386ba726b8aSAndrey A. Chernov 	INTON;
3874b88c807SRodney W. Grimes }
3884b88c807SRodney W. Grimes 
3894b88c807SRodney W. Grimes 
3904b88c807SRodney W. Grimes 
3914b88c807SRodney W. Grimes /*
3924b88c807SRodney W. Grimes  * Process a linked list of variable assignments.
3934b88c807SRodney W. Grimes  */
3944b88c807SRodney W. Grimes 
3954b88c807SRodney W. Grimes void
396850460c0SJilles Tjoelker listsetvar(struct strlist *list, int flags)
3974b88c807SRodney W. Grimes {
3984b88c807SRodney W. Grimes 	struct strlist *lp;
3994b88c807SRodney W. Grimes 
4004b88c807SRodney W. Grimes 	INTOFF;
4014b88c807SRodney W. Grimes 	for (lp = list ; lp ; lp = lp->next) {
402850460c0SJilles Tjoelker 		setvareq(savestr(lp->text), flags);
4034b88c807SRodney W. Grimes 	}
4044b88c807SRodney W. Grimes 	INTON;
4054b88c807SRodney W. Grimes }
4064b88c807SRodney W. Grimes 
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes 
4094b88c807SRodney W. Grimes /*
4104b88c807SRodney W. Grimes  * Find the value of a variable.  Returns NULL if not set.
4114b88c807SRodney W. Grimes  */
4124b88c807SRodney W. Grimes 
4134b88c807SRodney W. Grimes char *
4142cac6e36SJilles Tjoelker lookupvar(const char *name)
4154b88c807SRodney W. Grimes {
4164b88c807SRodney W. Grimes 	struct var *v;
4174b88c807SRodney W. Grimes 
4183a99ed46SJilles Tjoelker 	v = find_var(name, NULL, NULL);
4193a99ed46SJilles Tjoelker 	if (v == NULL || v->flags & VUNSET)
4204b88c807SRodney W. Grimes 		return NULL;
4213a99ed46SJilles Tjoelker 	return v->text + v->name_len + 1;
4224b88c807SRodney W. Grimes }
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 
4254b88c807SRodney W. Grimes 
4264b88c807SRodney W. Grimes /*
4274b88c807SRodney W. Grimes  * Search the environment of a builtin command.  If the second argument
4284b88c807SRodney W. Grimes  * is nonzero, return the value of a variable even if it hasn't been
4294b88c807SRodney W. Grimes  * exported.
4304b88c807SRodney W. Grimes  */
4314b88c807SRodney W. Grimes 
4324b88c807SRodney W. Grimes char *
4332cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall)
4344b88c807SRodney W. Grimes {
4354b88c807SRodney W. Grimes 	struct strlist *sp;
4364b88c807SRodney W. Grimes 	struct var *v;
437011d162dSJilles Tjoelker 	char *result;
4384b88c807SRodney W. Grimes 
439011d162dSJilles Tjoelker 	result = NULL;
4404b88c807SRodney W. Grimes 	for (sp = cmdenviron ; sp ; sp = sp->next) {
4414b88c807SRodney W. Grimes 		if (varequal(sp->text, name))
442011d162dSJilles Tjoelker 			result = strchr(sp->text, '=') + 1;
4434b88c807SRodney W. Grimes 	}
444011d162dSJilles Tjoelker 	if (result != NULL)
445011d162dSJilles Tjoelker 		return result;
4463a99ed46SJilles Tjoelker 
4473a99ed46SJilles Tjoelker 	v = find_var(name, NULL, NULL);
4483a99ed46SJilles Tjoelker 	if (v == NULL || v->flags & VUNSET ||
4493a99ed46SJilles Tjoelker 	    (!doall && (v->flags & VEXPORT) == 0))
4504b88c807SRodney W. Grimes 		return NULL;
4513a99ed46SJilles Tjoelker 	return v->text + v->name_len + 1;
4524b88c807SRodney W. Grimes }
4534b88c807SRodney W. Grimes 
4544b88c807SRodney W. Grimes 
455e4b50334SJilles Tjoelker /*
456e4b50334SJilles Tjoelker  * Set up locale for a builtin (LANG/LC_* assignments).
457e4b50334SJilles Tjoelker  */
458e4b50334SJilles Tjoelker void
459e4b50334SJilles Tjoelker bltinsetlocale(void)
460e4b50334SJilles Tjoelker {
461e4b50334SJilles Tjoelker 	struct strlist *lp;
462e4b50334SJilles Tjoelker 	int act = 0;
463e4b50334SJilles Tjoelker 	char *loc, *locdef;
464e4b50334SJilles Tjoelker 	int i;
465e4b50334SJilles Tjoelker 
466e4b50334SJilles Tjoelker 	for (lp = cmdenviron ; lp ; lp = lp->next) {
467e4b50334SJilles Tjoelker 		if (localevar(lp->text)) {
468e4b50334SJilles Tjoelker 			act = 1;
469e4b50334SJilles Tjoelker 			break;
470e4b50334SJilles Tjoelker 		}
471e4b50334SJilles Tjoelker 	}
472e4b50334SJilles Tjoelker 	if (!act)
473e4b50334SJilles Tjoelker 		return;
474e4b50334SJilles Tjoelker 	loc = bltinlookup("LC_ALL", 0);
475e4b50334SJilles Tjoelker 	INTOFF;
476e4b50334SJilles Tjoelker 	if (loc != NULL) {
477e4b50334SJilles Tjoelker 		setlocale(LC_ALL, loc);
478e4b50334SJilles Tjoelker 		INTON;
4796ed74a0aSJilles Tjoelker 		updatecharset();
480e4b50334SJilles Tjoelker 		return;
481e4b50334SJilles Tjoelker 	}
482e4b50334SJilles Tjoelker 	locdef = bltinlookup("LANG", 0);
483e4b50334SJilles Tjoelker 	for (i = 0; locale_names[i] != NULL; i++) {
484e4b50334SJilles Tjoelker 		loc = bltinlookup(locale_names[i], 0);
485e4b50334SJilles Tjoelker 		if (loc == NULL)
486e4b50334SJilles Tjoelker 			loc = locdef;
487e4b50334SJilles Tjoelker 		if (loc != NULL)
488e4b50334SJilles Tjoelker 			setlocale(locale_categories[i], loc);
489e4b50334SJilles Tjoelker 	}
490e4b50334SJilles Tjoelker 	INTON;
4916ed74a0aSJilles Tjoelker 	updatecharset();
492e4b50334SJilles Tjoelker }
493e4b50334SJilles Tjoelker 
494e4b50334SJilles Tjoelker /*
495e4b50334SJilles Tjoelker  * Undo the effect of bltinlocaleset().
496e4b50334SJilles Tjoelker  */
497e4b50334SJilles Tjoelker void
498e4b50334SJilles Tjoelker bltinunsetlocale(void)
499e4b50334SJilles Tjoelker {
500e4b50334SJilles Tjoelker 	struct strlist *lp;
501e4b50334SJilles Tjoelker 
502e4b50334SJilles Tjoelker 	INTOFF;
503e4b50334SJilles Tjoelker 	for (lp = cmdenviron ; lp ; lp = lp->next) {
504e4b50334SJilles Tjoelker 		if (localevar(lp->text)) {
505e4b50334SJilles Tjoelker 			setlocale(LC_ALL, "");
5066ed74a0aSJilles Tjoelker 			updatecharset();
507e4b50334SJilles Tjoelker 			return;
508e4b50334SJilles Tjoelker 		}
509e4b50334SJilles Tjoelker 	}
510e4b50334SJilles Tjoelker 	INTON;
511e4b50334SJilles Tjoelker }
512e4b50334SJilles Tjoelker 
5136ed74a0aSJilles Tjoelker /*
5146ed74a0aSJilles Tjoelker  * Update the localeisutf8 flag.
5156ed74a0aSJilles Tjoelker  */
5166ed74a0aSJilles Tjoelker void
5176ed74a0aSJilles Tjoelker updatecharset(void)
5186ed74a0aSJilles Tjoelker {
5196ed74a0aSJilles Tjoelker 	char *charset;
5206ed74a0aSJilles Tjoelker 
5216ed74a0aSJilles Tjoelker 	charset = nl_langinfo(CODESET);
5226ed74a0aSJilles Tjoelker 	localeisutf8 = !strcmp(charset, "UTF-8");
5236ed74a0aSJilles Tjoelker }
5244b88c807SRodney W. Grimes 
52507eb7033SJilles Tjoelker void
52607eb7033SJilles Tjoelker initcharset(void)
52707eb7033SJilles Tjoelker {
52807eb7033SJilles Tjoelker 	updatecharset();
52907eb7033SJilles Tjoelker 	initial_localeisutf8 = localeisutf8;
53007eb7033SJilles Tjoelker }
53107eb7033SJilles Tjoelker 
5324b88c807SRodney W. Grimes /*
5334b88c807SRodney W. Grimes  * Generate a list of exported variables.  This routine is used to construct
5344b88c807SRodney W. Grimes  * the third argument to execve when executing a program.
5354b88c807SRodney W. Grimes  */
5364b88c807SRodney W. Grimes 
5374b88c807SRodney W. Grimes char **
5385134c3f7SWarner Losh environment(void)
5395134c3f7SWarner Losh {
5404b88c807SRodney W. Grimes 	int nenv;
5414b88c807SRodney W. Grimes 	struct var **vpp;
5424b88c807SRodney W. Grimes 	struct var *vp;
5434b88c807SRodney W. Grimes 	char **env, **ep;
5444b88c807SRodney W. Grimes 
5454b88c807SRodney W. Grimes 	nenv = 0;
5464b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5474b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
5484b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
5494b88c807SRodney W. Grimes 				nenv++;
5504b88c807SRodney W. Grimes 	}
5514b88c807SRodney W. Grimes 	ep = env = stalloc((nenv + 1) * sizeof *env);
5524b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5534b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
5544b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
5554b88c807SRodney W. Grimes 				*ep++ = vp->text;
5564b88c807SRodney W. Grimes 	}
5574b88c807SRodney W. Grimes 	*ep = NULL;
5584b88c807SRodney W. Grimes 	return env;
5594b88c807SRodney W. Grimes }
5604b88c807SRodney W. Grimes 
5614b88c807SRodney W. Grimes 
56288328642SDavid E. O'Brien static int
563692f35fdSStefan Farfeleder var_compare(const void *a, const void *b)
564692f35fdSStefan Farfeleder {
565692f35fdSStefan Farfeleder 	const char *const *sa, *const *sb;
566692f35fdSStefan Farfeleder 
567692f35fdSStefan Farfeleder 	sa = a;
568692f35fdSStefan Farfeleder 	sb = b;
569692f35fdSStefan Farfeleder 	/*
570692f35fdSStefan Farfeleder 	 * This compares two var=value strings which creates a different
571692f35fdSStefan Farfeleder 	 * order from what you would probably expect.  POSIX is somewhat
572692f35fdSStefan Farfeleder 	 * ambiguous on what should be sorted exactly.
573692f35fdSStefan Farfeleder 	 */
574692f35fdSStefan Farfeleder 	return strcoll(*sa, *sb);
575692f35fdSStefan Farfeleder }
576692f35fdSStefan Farfeleder 
5774b88c807SRodney W. Grimes 
5784b88c807SRodney W. Grimes /*
579cff1d849SJilles Tjoelker  * Command to list all variables which are set.  This is invoked from the
580cff1d849SJilles Tjoelker  * set command when it is called without any options or operands.
5814b88c807SRodney W. Grimes  */
5824b88c807SRodney W. Grimes 
5834b88c807SRodney W. Grimes int
5845134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused)
585aa9caaf6SPeter Wemm {
5864b88c807SRodney W. Grimes 	struct var **vpp;
5874b88c807SRodney W. Grimes 	struct var *vp;
58859258844STim J. Robbins 	const char *s;
589692f35fdSStefan Farfeleder 	const char **vars;
590692f35fdSStefan Farfeleder 	int i, n;
5914b88c807SRodney W. Grimes 
592692f35fdSStefan Farfeleder 	/*
593692f35fdSStefan Farfeleder 	 * POSIX requires us to sort the variables.
594692f35fdSStefan Farfeleder 	 */
595692f35fdSStefan Farfeleder 	n = 0;
5964b88c807SRodney W. Grimes 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
5974b88c807SRodney W. Grimes 		for (vp = *vpp; vp; vp = vp->next) {
598692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
599692f35fdSStefan Farfeleder 				n++;
600692f35fdSStefan Farfeleder 		}
601692f35fdSStefan Farfeleder 	}
602692f35fdSStefan Farfeleder 
603692f35fdSStefan Farfeleder 	INTON;
604692f35fdSStefan Farfeleder 	vars = ckmalloc(n * sizeof(*vars));
605692f35fdSStefan Farfeleder 	i = 0;
606692f35fdSStefan Farfeleder 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
607692f35fdSStefan Farfeleder 		for (vp = *vpp; vp; vp = vp->next) {
608692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
609692f35fdSStefan Farfeleder 				vars[i++] = vp->text;
610692f35fdSStefan Farfeleder 		}
611692f35fdSStefan Farfeleder 	}
612692f35fdSStefan Farfeleder 
613692f35fdSStefan Farfeleder 	qsort(vars, n, sizeof(*vars), var_compare);
614692f35fdSStefan Farfeleder 	for (i = 0; i < n; i++) {
615aeb5d065SJilles Tjoelker 		s = strchr(vars[i], '=');
616aeb5d065SJilles Tjoelker 		s++;
617aeb5d065SJilles Tjoelker 		outbin(vars[i], s - vars[i], out1);
618aeb5d065SJilles Tjoelker 		out1qstr(s);
61959258844STim J. Robbins 		out1c('\n');
6204b88c807SRodney W. Grimes 	}
621692f35fdSStefan Farfeleder 	ckfree(vars);
622692f35fdSStefan Farfeleder 	INTOFF;
623692f35fdSStefan Farfeleder 
6244b88c807SRodney W. Grimes 	return 0;
6254b88c807SRodney W. Grimes }
6264b88c807SRodney W. Grimes 
6274b88c807SRodney W. Grimes 
6284b88c807SRodney W. Grimes 
6294b88c807SRodney W. Grimes /*
6304b88c807SRodney W. Grimes  * The export and readonly commands.
6314b88c807SRodney W. Grimes  */
6324b88c807SRodney W. Grimes 
6334b88c807SRodney W. Grimes int
6345134c3f7SWarner Losh exportcmd(int argc, char **argv)
635aa9caaf6SPeter Wemm {
6364b88c807SRodney W. Grimes 	struct var **vpp;
6374b88c807SRodney W. Grimes 	struct var *vp;
6384b88c807SRodney W. Grimes 	char *name;
6394b88c807SRodney W. Grimes 	char *p;
64045086f8cSTim J. Robbins 	char *cmdname;
64145086f8cSTim J. Robbins 	int ch, values;
6424b88c807SRodney W. Grimes 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
6434b88c807SRodney W. Grimes 
64445086f8cSTim J. Robbins 	cmdname = argv[0];
64545086f8cSTim J. Robbins 	optreset = optind = 1;
646050f7913STim J. Robbins 	opterr = 0;
64745086f8cSTim J. Robbins 	values = 0;
64845086f8cSTim J. Robbins 	while ((ch = getopt(argc, argv, "p")) != -1) {
64945086f8cSTim J. Robbins 		switch (ch) {
65045086f8cSTim J. Robbins 		case 'p':
65145086f8cSTim J. Robbins 			values = 1;
65245086f8cSTim J. Robbins 			break;
65345086f8cSTim J. Robbins 		case '?':
65445086f8cSTim J. Robbins 		default:
65545086f8cSTim J. Robbins 			error("unknown option: -%c", optopt);
65645086f8cSTim J. Robbins 		}
65745086f8cSTim J. Robbins 	}
65845086f8cSTim J. Robbins 	argc -= optind;
65945086f8cSTim J. Robbins 	argv += optind;
66045086f8cSTim J. Robbins 
6613fda45ecSStefan Farfeleder 	if (values && argc != 0)
6623fda45ecSStefan Farfeleder 		error("-p requires no arguments");
66345086f8cSTim J. Robbins 	if (argc != 0) {
6643fda45ecSStefan Farfeleder 		while ((name = *argv++) != NULL) {
6654b88c807SRodney W. Grimes 			if ((p = strchr(name, '=')) != NULL) {
6664b88c807SRodney W. Grimes 				p++;
6674b88c807SRodney W. Grimes 			} else {
6683a99ed46SJilles Tjoelker 				vp = find_var(name, NULL, NULL);
6693a99ed46SJilles Tjoelker 				if (vp != NULL) {
6704b88c807SRodney W. Grimes 					vp->flags |= flag;
671ba726b8aSAndrey A. Chernov 					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
6725b78c6c2SSean Farley 						change_env(vp->text, 1);
673ba726b8aSAndrey A. Chernov 						(void) setlocale(LC_ALL, "");
6746ed74a0aSJilles Tjoelker 						updatecharset();
675ba726b8aSAndrey A. Chernov 					}
6763a99ed46SJilles Tjoelker 					continue;
6774b88c807SRodney W. Grimes 				}
6784b88c807SRodney W. Grimes 			}
6794b88c807SRodney W. Grimes 			setvar(name, p, flag);
6804b88c807SRodney W. Grimes 		}
6814b88c807SRodney W. Grimes 	} else {
6824b88c807SRodney W. Grimes 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6834b88c807SRodney W. Grimes 			for (vp = *vpp ; vp ; vp = vp->next) {
6844b88c807SRodney W. Grimes 				if (vp->flags & flag) {
68545086f8cSTim J. Robbins 					if (values) {
68645086f8cSTim J. Robbins 						out1str(cmdname);
68745086f8cSTim J. Robbins 						out1c(' ');
68845086f8cSTim J. Robbins 					}
68945086f8cSTim J. Robbins 					if (values && !(vp->flags & VUNSET)) {
690258ef734SJilles Tjoelker 						outbin(vp->text,
691258ef734SJilles Tjoelker 						    vp->name_len + 1, out1);
692258ef734SJilles Tjoelker 						out1qstr(vp->text +
693258ef734SJilles Tjoelker 						    vp->name_len + 1);
694aeb5d065SJilles Tjoelker 					} else
695258ef734SJilles Tjoelker 						outbin(vp->text, vp->name_len,
696aeb5d065SJilles Tjoelker 						    out1);
6974b88c807SRodney W. Grimes 					out1c('\n');
6984b88c807SRodney W. Grimes 				}
6994b88c807SRodney W. Grimes 			}
7004b88c807SRodney W. Grimes 		}
7014b88c807SRodney W. Grimes 	}
7024b88c807SRodney W. Grimes 	return 0;
7034b88c807SRodney W. Grimes }
7044b88c807SRodney W. Grimes 
7054b88c807SRodney W. Grimes 
7064b88c807SRodney W. Grimes /*
7074b88c807SRodney W. Grimes  * The "local" command.
7084b88c807SRodney W. Grimes  */
7094b88c807SRodney W. Grimes 
710aa9caaf6SPeter Wemm int
7115134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused)
712aa9caaf6SPeter Wemm {
7134b88c807SRodney W. Grimes 	char *name;
7144b88c807SRodney W. Grimes 
7154b88c807SRodney W. Grimes 	if (! in_function())
7164b88c807SRodney W. Grimes 		error("Not in a function");
7174b88c807SRodney W. Grimes 	while ((name = *argptr++) != NULL) {
7184b88c807SRodney W. Grimes 		mklocal(name);
7194b88c807SRodney W. Grimes 	}
7204b88c807SRodney W. Grimes 	return 0;
7214b88c807SRodney W. Grimes }
7224b88c807SRodney W. Grimes 
7234b88c807SRodney W. Grimes 
7244b88c807SRodney W. Grimes /*
7254b88c807SRodney W. Grimes  * Make a variable a local variable.  When a variable is made local, it's
7264b88c807SRodney W. Grimes  * value and flags are saved in a localvar structure.  The saved values
7274b88c807SRodney W. Grimes  * will be restored when the shell function returns.  We handle the name
7284b88c807SRodney W. Grimes  * "-" as a special case.
7294b88c807SRodney W. Grimes  */
7304b88c807SRodney W. Grimes 
7314b88c807SRodney W. Grimes void
7325134c3f7SWarner Losh mklocal(char *name)
7334b88c807SRodney W. Grimes {
7344b88c807SRodney W. Grimes 	struct localvar *lvp;
7354b88c807SRodney W. Grimes 	struct var **vpp;
7364b88c807SRodney W. Grimes 	struct var *vp;
7374b88c807SRodney W. Grimes 
7384b88c807SRodney W. Grimes 	INTOFF;
7394b88c807SRodney W. Grimes 	lvp = ckmalloc(sizeof (struct localvar));
7404b88c807SRodney W. Grimes 	if (name[0] == '-' && name[1] == '\0') {
7414b88c807SRodney W. Grimes 		lvp->text = ckmalloc(sizeof optlist);
742aa9caaf6SPeter Wemm 		memcpy(lvp->text, optlist, sizeof optlist);
7434b88c807SRodney W. Grimes 		vp = NULL;
7444b88c807SRodney W. Grimes 	} else {
7453a99ed46SJilles Tjoelker 		vp = find_var(name, &vpp, NULL);
7464b88c807SRodney W. Grimes 		if (vp == NULL) {
7474b88c807SRodney W. Grimes 			if (strchr(name, '='))
748c543e1aeSJilles Tjoelker 				setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
7494b88c807SRodney W. Grimes 			else
750c543e1aeSJilles Tjoelker 				setvar(name, NULL, VSTRFIXED | VNOLOCAL);
7514b88c807SRodney W. Grimes 			vp = *vpp;	/* the new variable */
7524b88c807SRodney W. Grimes 			lvp->text = NULL;
7534b88c807SRodney W. Grimes 			lvp->flags = VUNSET;
7544b88c807SRodney W. Grimes 		} else {
7554b88c807SRodney W. Grimes 			lvp->text = vp->text;
7564b88c807SRodney W. Grimes 			lvp->flags = vp->flags;
7574b88c807SRodney W. Grimes 			vp->flags |= VSTRFIXED|VTEXTFIXED;
7583a99ed46SJilles Tjoelker 			if (name[vp->name_len] == '=')
759c543e1aeSJilles Tjoelker 				setvareq(savestr(name), VNOLOCAL);
7604b88c807SRodney W. Grimes 		}
7614b88c807SRodney W. Grimes 	}
7624b88c807SRodney W. Grimes 	lvp->vp = vp;
7634b88c807SRodney W. Grimes 	lvp->next = localvars;
7644b88c807SRodney W. Grimes 	localvars = lvp;
7654b88c807SRodney W. Grimes 	INTON;
7664b88c807SRodney W. Grimes }
7674b88c807SRodney W. Grimes 
7684b88c807SRodney W. Grimes 
7694b88c807SRodney W. Grimes /*
7704b88c807SRodney W. Grimes  * Called after a function returns.
7714b88c807SRodney W. Grimes  */
7724b88c807SRodney W. Grimes 
7734b88c807SRodney W. Grimes void
7745134c3f7SWarner Losh poplocalvars(void)
7755134c3f7SWarner Losh {
7764b88c807SRodney W. Grimes 	struct localvar *lvp;
7774b88c807SRodney W. Grimes 	struct var *vp;
7784b88c807SRodney W. Grimes 
7794b88c807SRodney W. Grimes 	while ((lvp = localvars) != NULL) {
7804b88c807SRodney W. Grimes 		localvars = lvp->next;
7814b88c807SRodney W. Grimes 		vp = lvp->vp;
7824b88c807SRodney W. Grimes 		if (vp == NULL) {	/* $- saved */
783aa9caaf6SPeter Wemm 			memcpy(optlist, lvp->text, sizeof optlist);
7844b88c807SRodney W. Grimes 			ckfree(lvp->text);
7854a7b1013SJilles Tjoelker 			optschanged();
7864b88c807SRodney W. Grimes 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
7874b88c807SRodney W. Grimes 			(void)unsetvar(vp->text);
7884b88c807SRodney W. Grimes 		} else {
7894b88c807SRodney W. Grimes 			if ((vp->flags & VTEXTFIXED) == 0)
7904b88c807SRodney W. Grimes 				ckfree(vp->text);
7914b88c807SRodney W. Grimes 			vp->flags = lvp->flags;
7924b88c807SRodney W. Grimes 			vp->text = lvp->text;
7934b88c807SRodney W. Grimes 		}
7944b88c807SRodney W. Grimes 		ckfree(lvp);
7954b88c807SRodney W. Grimes 	}
7964b88c807SRodney W. Grimes }
7974b88c807SRodney W. Grimes 
7984b88c807SRodney W. Grimes 
799aa9caaf6SPeter Wemm int
8005134c3f7SWarner Losh setvarcmd(int argc, char **argv)
801aa9caaf6SPeter Wemm {
8024b88c807SRodney W. Grimes 	if (argc <= 2)
8034b88c807SRodney W. Grimes 		return unsetcmd(argc, argv);
8044b88c807SRodney W. Grimes 	else if (argc == 3)
8054b88c807SRodney W. Grimes 		setvar(argv[1], argv[2], 0);
8064b88c807SRodney W. Grimes 	else
807274110dfSJilles Tjoelker 		error("too many arguments");
8084b88c807SRodney W. Grimes 	return 0;
8094b88c807SRodney W. Grimes }
8104b88c807SRodney W. Grimes 
8114b88c807SRodney W. Grimes 
8124b88c807SRodney W. Grimes /*
813cff1d849SJilles Tjoelker  * The unset builtin command.
8144b88c807SRodney W. Grimes  */
8154b88c807SRodney W. Grimes 
816aa9caaf6SPeter Wemm int
8175134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused)
818aa9caaf6SPeter Wemm {
8194b88c807SRodney W. Grimes 	char **ap;
8204b88c807SRodney W. Grimes 	int i;
8214b88c807SRodney W. Grimes 	int flg_func = 0;
8224b88c807SRodney W. Grimes 	int flg_var = 0;
8234b88c807SRodney W. Grimes 	int ret = 0;
8244b88c807SRodney W. Grimes 
8254b88c807SRodney W. Grimes 	while ((i = nextopt("vf")) != '\0') {
8264b88c807SRodney W. Grimes 		if (i == 'f')
8274b88c807SRodney W. Grimes 			flg_func = 1;
8284b88c807SRodney W. Grimes 		else
8294b88c807SRodney W. Grimes 			flg_var = 1;
8304b88c807SRodney W. Grimes 	}
8314b88c807SRodney W. Grimes 	if (flg_func == 0 && flg_var == 0)
8324b88c807SRodney W. Grimes 		flg_var = 1;
8334b88c807SRodney W. Grimes 
8344b88c807SRodney W. Grimes 	for (ap = argptr; *ap ; ap++) {
8354b88c807SRodney W. Grimes 		if (flg_func)
8364b88c807SRodney W. Grimes 			ret |= unsetfunc(*ap);
8374b88c807SRodney W. Grimes 		if (flg_var)
8384b88c807SRodney W. Grimes 			ret |= unsetvar(*ap);
8394b88c807SRodney W. Grimes 	}
8404b88c807SRodney W. Grimes 	return ret;
8414b88c807SRodney W. Grimes }
8424b88c807SRodney W. Grimes 
8434b88c807SRodney W. Grimes 
8444b88c807SRodney W. Grimes /*
8454b88c807SRodney W. Grimes  * Unset the specified variable.
8464b88c807SRodney W. Grimes  */
8474b88c807SRodney W. Grimes 
848ab0a2172SSteve Price int
8492cac6e36SJilles Tjoelker unsetvar(const char *s)
8504b88c807SRodney W. Grimes {
8514b88c807SRodney W. Grimes 	struct var **vpp;
8524b88c807SRodney W. Grimes 	struct var *vp;
8534b88c807SRodney W. Grimes 
8543a99ed46SJilles Tjoelker 	vp = find_var(s, &vpp, NULL);
8553a99ed46SJilles Tjoelker 	if (vp == NULL)
8563a99ed46SJilles Tjoelker 		return (0);
8574b88c807SRodney W. Grimes 	if (vp->flags & VREADONLY)
8584b88c807SRodney W. Grimes 		return (1);
8594b88c807SRodney W. Grimes 	INTOFF;
8603a99ed46SJilles Tjoelker 	if (vp->text[vp->name_len + 1] != '\0')
8614b88c807SRodney W. Grimes 		setvar(s, nullstr, 0);
862ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
8635b78c6c2SSean Farley 		change_env(s, 0);
864ba726b8aSAndrey A. Chernov 		setlocale(LC_ALL, "");
8656ed74a0aSJilles Tjoelker 		updatecharset();
866ba726b8aSAndrey A. Chernov 	}
8674b88c807SRodney W. Grimes 	vp->flags &= ~VEXPORT;
8684b88c807SRodney W. Grimes 	vp->flags |= VUNSET;
8694b88c807SRodney W. Grimes 	if ((vp->flags & VSTRFIXED) == 0) {
8704b88c807SRodney W. Grimes 		if ((vp->flags & VTEXTFIXED) == 0)
8714b88c807SRodney W. Grimes 			ckfree(vp->text);
8724b88c807SRodney W. Grimes 		*vpp = vp->next;
8734b88c807SRodney W. Grimes 		ckfree(vp);
8744b88c807SRodney W. Grimes 	}
8754b88c807SRodney W. Grimes 	INTON;
8764b88c807SRodney W. Grimes 	return (0);
8774b88c807SRodney W. Grimes }
8784b88c807SRodney W. Grimes 
8794b88c807SRodney W. Grimes 
8804b88c807SRodney W. Grimes 
8814b88c807SRodney W. Grimes /*
8824b88c807SRodney W. Grimes  * Returns true if the two strings specify the same varable.  The first
8834b88c807SRodney W. Grimes  * variable name is terminated by '='; the second may be terminated by
8844b88c807SRodney W. Grimes  * either '=' or '\0'.
8854b88c807SRodney W. Grimes  */
8864b88c807SRodney W. Grimes 
88788328642SDavid E. O'Brien static int
8882cac6e36SJilles Tjoelker varequal(const char *p, const char *q)
8894b88c807SRodney W. Grimes {
8904b88c807SRodney W. Grimes 	while (*p == *q++) {
8914b88c807SRodney W. Grimes 		if (*p++ == '=')
8924b88c807SRodney W. Grimes 			return 1;
8934b88c807SRodney W. Grimes 	}
8944b88c807SRodney W. Grimes 	if (*p == '=' && *(q - 1) == '\0')
8954b88c807SRodney W. Grimes 		return 1;
8964b88c807SRodney W. Grimes 	return 0;
8974b88c807SRodney W. Grimes }
8983a99ed46SJilles Tjoelker 
8993a99ed46SJilles Tjoelker /*
9003a99ed46SJilles Tjoelker  * Search for a variable.
9013a99ed46SJilles Tjoelker  * 'name' may be terminated by '=' or a NUL.
9023a99ed46SJilles Tjoelker  * vppp is set to the pointer to vp, or the list head if vp isn't found
9033a99ed46SJilles Tjoelker  * lenp is set to the number of charactets in 'name'
9043a99ed46SJilles Tjoelker  */
9053a99ed46SJilles Tjoelker 
9063a99ed46SJilles Tjoelker static struct var *
9073a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp)
9083a99ed46SJilles Tjoelker {
9093a99ed46SJilles Tjoelker 	unsigned int hashval;
9103a99ed46SJilles Tjoelker 	int len;
9113a99ed46SJilles Tjoelker 	struct var *vp, **vpp;
9123a99ed46SJilles Tjoelker 	const char *p = name;
9133a99ed46SJilles Tjoelker 
9143a99ed46SJilles Tjoelker 	hashval = 0;
9153a99ed46SJilles Tjoelker 	while (*p && *p != '=')
9163a99ed46SJilles Tjoelker 		hashval = 2 * hashval + (unsigned char)*p++;
9173a99ed46SJilles Tjoelker 	len = p - name;
9183a99ed46SJilles Tjoelker 
9193a99ed46SJilles Tjoelker 	if (lenp)
9203a99ed46SJilles Tjoelker 		*lenp = len;
9213a99ed46SJilles Tjoelker 	vpp = &vartab[hashval % VTABSIZE];
9223a99ed46SJilles Tjoelker 	if (vppp)
9233a99ed46SJilles Tjoelker 		*vppp = vpp;
9243a99ed46SJilles Tjoelker 
9253a99ed46SJilles Tjoelker 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
9263a99ed46SJilles Tjoelker 		if (vp->name_len != len)
9273a99ed46SJilles Tjoelker 			continue;
9283a99ed46SJilles Tjoelker 		if (memcmp(vp->text, name, len) != 0)
9293a99ed46SJilles Tjoelker 			continue;
9303a99ed46SJilles Tjoelker 		if (vppp)
9313a99ed46SJilles Tjoelker 			*vppp = vpp;
9323a99ed46SJilles Tjoelker 		return vp;
9333a99ed46SJilles Tjoelker 	}
9343a99ed46SJilles Tjoelker 	return NULL;
9353a99ed46SJilles Tjoelker }
936