xref: /freebsd/bin/sh/var.c (revision 33233ec76ef2ee414980da0995f9b77a48a9281a)
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"
66454a02b3SJilles 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;
97caf29fabSJilles Tjoelker struct var vdisvfork;
984b88c807SRodney W. Grimes 
99c543e1aeSJilles Tjoelker int forcelocal;
100c543e1aeSJilles Tjoelker 
101aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = {
102aa9caaf6SPeter Wemm #ifndef NO_HISTORY
103c6c5dd37SJilles Tjoelker 	{ &vhistsize,	VUNSET,				"HISTSIZE=",
104ab0a2172SSteve Price 	  sethistsize },
105aa9caaf6SPeter Wemm #endif
106c6c5dd37SJilles Tjoelker 	{ &vifs,	0,				"IFS= \t\n",
107ab0a2172SSteve Price 	  NULL },
108c6c5dd37SJilles Tjoelker 	{ &vmail,	VUNSET,				"MAIL=",
109ab0a2172SSteve Price 	  NULL },
110c6c5dd37SJilles Tjoelker 	{ &vmpath,	VUNSET,				"MAILPATH=",
111ab0a2172SSteve Price 	  NULL },
112c6c5dd37SJilles Tjoelker 	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
113ab0a2172SSteve Price 	  changepath },
114c6c5dd37SJilles Tjoelker 	{ &vppid,	VUNSET,				"PPID=",
11539dccc6fSTim J. Robbins 	  NULL },
1164b88c807SRodney W. Grimes 	/*
1174b88c807SRodney W. Grimes 	 * vps1 depends on uid
1184b88c807SRodney W. Grimes 	 */
119c6c5dd37SJilles Tjoelker 	{ &vps2,	0,				"PS2=> ",
120ab0a2172SSteve Price 	  NULL },
121c6c5dd37SJilles Tjoelker 	{ &vps4,	0,				"PS4=+ ",
122120c8e6cSStefan Farfeleder 	  NULL },
123580eefdfSJilles Tjoelker #ifndef NO_HISTORY
124580eefdfSJilles Tjoelker 	{ &vterm,	VUNSET,				"TERM=",
125580eefdfSJilles Tjoelker 	  setterm },
126580eefdfSJilles Tjoelker #endif
127c6c5dd37SJilles Tjoelker 	{ &voptind,	0,				"OPTIND=1",
128ab0a2172SSteve Price 	  getoptsreset },
129caf29fabSJilles Tjoelker 	{ &vdisvfork,	VUNSET,				"SH_DISABLE_VFORK=",
130caf29fabSJilles Tjoelker 	  NULL },
131ab0a2172SSteve Price 	{ NULL,	0,				NULL,
132ab0a2172SSteve Price 	  NULL }
1334b88c807SRodney W. Grimes };
1344b88c807SRodney W. Grimes 
135aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE];
1364b88c807SRodney W. Grimes 
137aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = {
138e4b50334SJilles Tjoelker 	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
139e4b50334SJilles Tjoelker 	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
140e4b50334SJilles Tjoelker };
141aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = {
142e4b50334SJilles Tjoelker 	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
143e4b50334SJilles Tjoelker };
144e4b50334SJilles Tjoelker 
14588328642SDavid E. O'Brien static int varequal(const char *, const char *);
1463a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *);
14788328642SDavid E. O'Brien static int localevar(const char *);
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes /*
150b3decf89SJens Schweikhardt  * Initialize the variable symbol tables and import the environment.
1514b88c807SRodney W. Grimes  */
1524b88c807SRodney W. Grimes 
1534b88c807SRodney W. Grimes #ifdef mkinit
1544b88c807SRodney W. Grimes INCLUDE "var.h"
155384aedabSJilles Tjoelker MKINIT char **environ;
1564b88c807SRodney W. Grimes INIT {
1574b88c807SRodney W. Grimes 	char **envp;
1584b88c807SRodney W. Grimes 
1594b88c807SRodney W. Grimes 	initvar();
1604b88c807SRodney W. Grimes 	for (envp = environ ; *envp ; envp++) {
1614b88c807SRodney W. Grimes 		if (strchr(*envp, '=')) {
1624b88c807SRodney W. Grimes 			setvareq(*envp, VEXPORT|VTEXTFIXED);
1634b88c807SRodney W. Grimes 		}
1644b88c807SRodney W. Grimes 	}
1654b88c807SRodney W. Grimes }
1664b88c807SRodney W. Grimes #endif
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 
1694b88c807SRodney W. Grimes /*
1704b88c807SRodney W. Grimes  * This routine initializes the builtin variables.  It is called when the
1713835f47cSJilles Tjoelker  * shell is initialized.
1724b88c807SRodney W. Grimes  */
1734b88c807SRodney W. Grimes 
1744b88c807SRodney W. Grimes void
1755134c3f7SWarner Losh initvar(void)
1765134c3f7SWarner Losh {
17739dccc6fSTim J. Robbins 	char ppid[20];
1784b88c807SRodney W. Grimes 	const struct varinit *ip;
1794b88c807SRodney W. Grimes 	struct var *vp;
1804b88c807SRodney W. Grimes 	struct var **vpp;
1814b88c807SRodney W. Grimes 
1824b88c807SRodney W. Grimes 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1833a99ed46SJilles Tjoelker 		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
1843a99ed46SJilles Tjoelker 			continue;
1854b88c807SRodney W. Grimes 		vp->next = *vpp;
1864b88c807SRodney W. Grimes 		*vpp = vp;
187c6c5dd37SJilles Tjoelker 		vp->text = __DECONST(char *, ip->text);
188c6c5dd37SJilles Tjoelker 		vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
189ab0a2172SSteve Price 		vp->func = ip->func;
1904b88c807SRodney W. Grimes 	}
1914b88c807SRodney W. Grimes 	/*
1924b88c807SRodney W. Grimes 	 * PS1 depends on uid
1934b88c807SRodney W. Grimes 	 */
1943a99ed46SJilles Tjoelker 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
1954b88c807SRodney W. Grimes 		vps1.next = *vpp;
1964b88c807SRodney W. Grimes 		*vpp = &vps1;
197c6c5dd37SJilles Tjoelker 		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
1984b88c807SRodney W. Grimes 		vps1.flags = VSTRFIXED|VTEXTFIXED;
1994b88c807SRodney W. Grimes 	}
20039dccc6fSTim J. Robbins 	if ((vppid.flags & VEXPORT) == 0) {
20139dccc6fSTim J. Robbins 		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
20239dccc6fSTim J. Robbins 		setvarsafe("PPID", ppid, 0);
20339dccc6fSTim J. Robbins 	}
2044b88c807SRodney W. Grimes }
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes /*
207ab0a2172SSteve Price  * Safe version of setvar, returns 1 on success 0 on failure.
208ab0a2172SSteve Price  */
209ab0a2172SSteve Price 
210ab0a2172SSteve Price int
2112cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags)
212ab0a2172SSteve Price {
213ab0a2172SSteve Price 	struct jmploc jmploc;
214224fbf9fSJilles Tjoelker 	struct jmploc *const savehandler = handler;
215ab0a2172SSteve Price 	int err = 0;
2169922c6d2SJilles Tjoelker 	int inton;
217ab0a2172SSteve Price 
2189922c6d2SJilles Tjoelker 	inton = is_int_on();
219ab0a2172SSteve Price 	if (setjmp(jmploc.loc))
220ab0a2172SSteve Price 		err = 1;
221ab0a2172SSteve Price 	else {
222ab0a2172SSteve Price 		handler = &jmploc;
223ab0a2172SSteve Price 		setvar(name, val, flags);
224ab0a2172SSteve Price 	}
225ab0a2172SSteve Price 	handler = savehandler;
2269922c6d2SJilles Tjoelker 	SETINTON(inton);
227ab0a2172SSteve Price 	return err;
228ab0a2172SSteve Price }
229ab0a2172SSteve Price 
230ab0a2172SSteve Price /*
231b3decf89SJens Schweikhardt  * Set the value of a variable.  The flags argument is stored with the
2324b88c807SRodney W. Grimes  * flags of the variable.  If val is NULL, the variable is unset.
2334b88c807SRodney W. Grimes  */
2344b88c807SRodney W. Grimes 
2354b88c807SRodney W. Grimes void
2362cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags)
2374b88c807SRodney W. Grimes {
2382cac6e36SJilles Tjoelker 	const char *p;
2394b88c807SRodney W. Grimes 	int len;
2404b88c807SRodney W. Grimes 	int namelen;
2414b88c807SRodney W. Grimes 	char *nameeq;
2424b88c807SRodney W. Grimes 	int isbad;
2434b88c807SRodney W. Grimes 
2444b88c807SRodney W. Grimes 	isbad = 0;
2454b88c807SRodney W. Grimes 	p = name;
246ba726b8aSAndrey A. Chernov 	if (! is_name(*p))
2474b88c807SRodney W. Grimes 		isbad = 1;
248ba726b8aSAndrey A. Chernov 	p++;
2494b88c807SRodney W. Grimes 	for (;;) {
2504b88c807SRodney W. Grimes 		if (! is_in_name(*p)) {
2514b88c807SRodney W. Grimes 			if (*p == '\0' || *p == '=')
2524b88c807SRodney W. Grimes 				break;
2534b88c807SRodney W. Grimes 			isbad = 1;
2544b88c807SRodney W. Grimes 		}
2554b88c807SRodney W. Grimes 		p++;
2564b88c807SRodney W. Grimes 	}
2574b88c807SRodney W. Grimes 	namelen = p - name;
2584b88c807SRodney W. Grimes 	if (isbad)
2594b88c807SRodney W. Grimes 		error("%.*s: bad variable name", namelen, name);
2604b88c807SRodney W. Grimes 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2614b88c807SRodney W. Grimes 	if (val == NULL) {
2624b88c807SRodney W. Grimes 		flags |= VUNSET;
2634b88c807SRodney W. Grimes 	} else {
2644b88c807SRodney W. Grimes 		len += strlen(val);
2654b88c807SRodney W. Grimes 	}
2662cac6e36SJilles Tjoelker 	nameeq = ckmalloc(len);
2672cac6e36SJilles Tjoelker 	memcpy(nameeq, name, namelen);
2682cac6e36SJilles Tjoelker 	nameeq[namelen] = '=';
2694b88c807SRodney W. Grimes 	if (val)
2702cac6e36SJilles Tjoelker 		scopy(val, nameeq + namelen + 1);
2712cac6e36SJilles Tjoelker 	else
2722cac6e36SJilles Tjoelker 		nameeq[namelen + 1] = '\0';
2734b88c807SRodney W. Grimes 	setvareq(nameeq, flags);
2744b88c807SRodney W. Grimes }
2754b88c807SRodney W. Grimes 
27688328642SDavid E. O'Brien static int
2772cac6e36SJilles Tjoelker localevar(const char *s)
278ba726b8aSAndrey A. Chernov {
279e4b50334SJilles Tjoelker 	const char *const *ss;
2804b88c807SRodney W. Grimes 
281ba726b8aSAndrey A. Chernov 	if (*s != 'L')
282ba726b8aSAndrey A. Chernov 		return 0;
283ba726b8aSAndrey A. Chernov 	if (varequal(s + 1, "ANG"))
284ba726b8aSAndrey A. Chernov 		return 1;
285ba726b8aSAndrey A. Chernov 	if (strncmp(s + 1, "C_", 2) != 0)
286ba726b8aSAndrey A. Chernov 		return 0;
287e4b50334SJilles Tjoelker 	if (varequal(s + 3, "ALL"))
288e4b50334SJilles Tjoelker 		return 1;
289e4b50334SJilles Tjoelker 	for (ss = locale_names; *ss ; ss++)
290e4b50334SJilles Tjoelker 		if (varequal(s + 3, *ss + 3))
291ba726b8aSAndrey A. Chernov 			return 1;
292ba726b8aSAndrey A. Chernov 	return 0;
293ba726b8aSAndrey A. Chernov }
2944b88c807SRodney W. Grimes 
2955b78c6c2SSean Farley 
2965b78c6c2SSean Farley /*
2975b78c6c2SSean Farley  * Sets/unsets an environment variable from a pointer that may actually be a
2985b78c6c2SSean Farley  * pointer into environ where the string should not be manipulated.
2995b78c6c2SSean Farley  */
30088328642SDavid E. O'Brien static void
3012cac6e36SJilles Tjoelker change_env(const char *s, int set)
3025b78c6c2SSean Farley {
3035b78c6c2SSean Farley 	char *eqp;
3045b78c6c2SSean Farley 	char *ss;
3055b78c6c2SSean Farley 
3065b78c6c2SSean Farley 	ss = savestr(s);
3075b78c6c2SSean Farley 	if ((eqp = strchr(ss, '=')) != NULL)
3085b78c6c2SSean Farley 		*eqp = '\0';
3095b78c6c2SSean Farley 	if (set && eqp != NULL)
3105b78c6c2SSean Farley 		(void) setenv(ss, eqp + 1, 1);
3115b78c6c2SSean Farley 	else
3125b78c6c2SSean Farley 		(void) unsetenv(ss);
3135b78c6c2SSean Farley 	ckfree(ss);
3145b78c6c2SSean Farley 
3155b78c6c2SSean Farley 	return;
3165b78c6c2SSean Farley }
3175b78c6c2SSean Farley 
3185b78c6c2SSean Farley 
3194b88c807SRodney W. Grimes /*
3204b88c807SRodney W. Grimes  * Same as setvar except that the variable and value are passed in
3214b88c807SRodney W. Grimes  * the first argument as name=value.  Since the first argument will
3224b88c807SRodney W. Grimes  * be actually stored in the table, it should not be a string that
3234b88c807SRodney W. Grimes  * will go away.
3244b88c807SRodney W. Grimes  */
3254b88c807SRodney W. Grimes 
3264b88c807SRodney W. Grimes void
3275134c3f7SWarner Losh setvareq(char *s, int flags)
3284b88c807SRodney W. Grimes {
3294b88c807SRodney W. Grimes 	struct var *vp, **vpp;
3303a99ed46SJilles Tjoelker 	int nlen;
3314b88c807SRodney W. Grimes 
332b8ec435eSMartin Cracauer 	if (aflag)
333b8ec435eSMartin Cracauer 		flags |= VEXPORT;
334c543e1aeSJilles Tjoelker 	if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
335c543e1aeSJilles Tjoelker 		mklocal(s);
3363a99ed46SJilles Tjoelker 	vp = find_var(s, &vpp, &nlen);
3373a99ed46SJilles Tjoelker 	if (vp != NULL) {
3383a99ed46SJilles Tjoelker 		if (vp->flags & VREADONLY)
3393a99ed46SJilles Tjoelker 			error("%.*s: is read only", vp->name_len, s);
340850460c0SJilles Tjoelker 		if (flags & VNOSET)
341850460c0SJilles Tjoelker 			return;
3424b88c807SRodney W. Grimes 		INTOFF;
343ab0a2172SSteve Price 
344ab0a2172SSteve Price 		if (vp->func && (flags & VNOFUNC) == 0)
3453a99ed46SJilles Tjoelker 			(*vp->func)(s + vp->name_len + 1);
346ab0a2172SSteve Price 
3474b88c807SRodney W. Grimes 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3484b88c807SRodney W. Grimes 			ckfree(vp->text);
349ab0a2172SSteve Price 
3504b88c807SRodney W. Grimes 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3514b88c807SRodney W. Grimes 		vp->flags |= flags;
3524b88c807SRodney W. Grimes 		vp->text = s;
353ab0a2172SSteve Price 
354ab0a2172SSteve Price 		/*
355ab0a2172SSteve Price 		 * We could roll this to a function, to handle it as
356ab0a2172SSteve Price 		 * a regular variable function callback, but why bother?
35757063576SJilles Tjoelker 		 *
35857063576SJilles Tjoelker 		 * Note: this assumes iflag is not set to 1 initially.
35957063576SJilles Tjoelker 		 * As part of init(), this is called before arguments
36057063576SJilles Tjoelker 		 * are looked at.
361ab0a2172SSteve Price 		 */
36257063576SJilles Tjoelker 		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
36357063576SJilles Tjoelker 		    iflag == 1)
3644b88c807SRodney W. Grimes 			chkmail(1);
365ba726b8aSAndrey A. Chernov 		if ((vp->flags & VEXPORT) && localevar(s)) {
3665b78c6c2SSean Farley 			change_env(s, 1);
367ba726b8aSAndrey A. Chernov 			(void) setlocale(LC_ALL, "");
3686ed74a0aSJilles Tjoelker 			updatecharset();
369ba726b8aSAndrey A. Chernov 		}
3704b88c807SRodney W. Grimes 		INTON;
3714b88c807SRodney W. Grimes 		return;
3724b88c807SRodney W. Grimes 	}
3734b88c807SRodney W. Grimes 	/* not found */
374850460c0SJilles Tjoelker 	if (flags & VNOSET)
375850460c0SJilles Tjoelker 		return;
3764b88c807SRodney W. Grimes 	vp = ckmalloc(sizeof (*vp));
3774b88c807SRodney W. Grimes 	vp->flags = flags;
3784b88c807SRodney W. Grimes 	vp->text = s;
3793a99ed46SJilles Tjoelker 	vp->name_len = nlen;
3804b88c807SRodney W. Grimes 	vp->next = *vpp;
381ab0a2172SSteve Price 	vp->func = NULL;
382ba726b8aSAndrey A. Chernov 	INTOFF;
3834b88c807SRodney W. Grimes 	*vpp = vp;
384ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(s)) {
3855b78c6c2SSean Farley 		change_env(s, 1);
386ba726b8aSAndrey A. Chernov 		(void) setlocale(LC_ALL, "");
3876ed74a0aSJilles Tjoelker 		updatecharset();
388ba726b8aSAndrey A. Chernov 	}
389ba726b8aSAndrey A. Chernov 	INTON;
3904b88c807SRodney W. Grimes }
3914b88c807SRodney W. Grimes 
3924b88c807SRodney W. Grimes 
3934b88c807SRodney W. Grimes 
3944b88c807SRodney W. Grimes /*
3954b88c807SRodney W. Grimes  * Process a linked list of variable assignments.
3964b88c807SRodney W. Grimes  */
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes void
399850460c0SJilles Tjoelker listsetvar(struct strlist *list, int flags)
4004b88c807SRodney W. Grimes {
4014b88c807SRodney W. Grimes 	struct strlist *lp;
4024b88c807SRodney W. Grimes 
4034b88c807SRodney W. Grimes 	INTOFF;
4044b88c807SRodney W. Grimes 	for (lp = list ; lp ; lp = lp->next) {
405850460c0SJilles Tjoelker 		setvareq(savestr(lp->text), flags);
4064b88c807SRodney W. Grimes 	}
4074b88c807SRodney W. Grimes 	INTON;
4084b88c807SRodney W. Grimes }
4094b88c807SRodney W. Grimes 
4104b88c807SRodney W. Grimes 
4114b88c807SRodney W. Grimes 
4124b88c807SRodney W. Grimes /*
4134b88c807SRodney W. Grimes  * Find the value of a variable.  Returns NULL if not set.
4144b88c807SRodney W. Grimes  */
4154b88c807SRodney W. Grimes 
4164b88c807SRodney W. Grimes char *
4172cac6e36SJilles Tjoelker lookupvar(const char *name)
4184b88c807SRodney W. Grimes {
4194b88c807SRodney W. Grimes 	struct var *v;
4204b88c807SRodney W. Grimes 
4213a99ed46SJilles Tjoelker 	v = find_var(name, NULL, NULL);
4223a99ed46SJilles Tjoelker 	if (v == NULL || v->flags & VUNSET)
4234b88c807SRodney W. Grimes 		return NULL;
4243a99ed46SJilles Tjoelker 	return v->text + v->name_len + 1;
4254b88c807SRodney W. Grimes }
4264b88c807SRodney W. Grimes 
4274b88c807SRodney W. Grimes 
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes /*
4304b88c807SRodney W. Grimes  * Search the environment of a builtin command.  If the second argument
4314b88c807SRodney W. Grimes  * is nonzero, return the value of a variable even if it hasn't been
4324b88c807SRodney W. Grimes  * exported.
4334b88c807SRodney W. Grimes  */
4344b88c807SRodney W. Grimes 
4354b88c807SRodney W. Grimes char *
4362cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall)
4374b88c807SRodney W. Grimes {
4384b88c807SRodney W. Grimes 	struct strlist *sp;
4394b88c807SRodney W. Grimes 	struct var *v;
440011d162dSJilles Tjoelker 	char *result;
4414b88c807SRodney W. Grimes 
442011d162dSJilles Tjoelker 	result = NULL;
4434b88c807SRodney W. Grimes 	for (sp = cmdenviron ; sp ; sp = sp->next) {
4444b88c807SRodney W. Grimes 		if (varequal(sp->text, name))
445011d162dSJilles Tjoelker 			result = strchr(sp->text, '=') + 1;
4464b88c807SRodney W. Grimes 	}
447011d162dSJilles Tjoelker 	if (result != NULL)
448011d162dSJilles Tjoelker 		return result;
4493a99ed46SJilles Tjoelker 
4503a99ed46SJilles Tjoelker 	v = find_var(name, NULL, NULL);
4513a99ed46SJilles Tjoelker 	if (v == NULL || v->flags & VUNSET ||
4523a99ed46SJilles Tjoelker 	    (!doall && (v->flags & VEXPORT) == 0))
4534b88c807SRodney W. Grimes 		return NULL;
4543a99ed46SJilles Tjoelker 	return v->text + v->name_len + 1;
4554b88c807SRodney W. Grimes }
4564b88c807SRodney W. Grimes 
4574b88c807SRodney W. Grimes 
458e4b50334SJilles Tjoelker /*
459e4b50334SJilles Tjoelker  * Set up locale for a builtin (LANG/LC_* assignments).
460e4b50334SJilles Tjoelker  */
461e4b50334SJilles Tjoelker void
462e4b50334SJilles Tjoelker bltinsetlocale(void)
463e4b50334SJilles Tjoelker {
464e4b50334SJilles Tjoelker 	struct strlist *lp;
465e4b50334SJilles Tjoelker 	int act = 0;
466e4b50334SJilles Tjoelker 	char *loc, *locdef;
467e4b50334SJilles Tjoelker 	int i;
468e4b50334SJilles Tjoelker 
469e4b50334SJilles Tjoelker 	for (lp = cmdenviron ; lp ; lp = lp->next) {
470e4b50334SJilles Tjoelker 		if (localevar(lp->text)) {
471e4b50334SJilles Tjoelker 			act = 1;
472e4b50334SJilles Tjoelker 			break;
473e4b50334SJilles Tjoelker 		}
474e4b50334SJilles Tjoelker 	}
475e4b50334SJilles Tjoelker 	if (!act)
476e4b50334SJilles Tjoelker 		return;
477e4b50334SJilles Tjoelker 	loc = bltinlookup("LC_ALL", 0);
478e4b50334SJilles Tjoelker 	INTOFF;
479e4b50334SJilles Tjoelker 	if (loc != NULL) {
480e4b50334SJilles Tjoelker 		setlocale(LC_ALL, loc);
481e4b50334SJilles Tjoelker 		INTON;
4826ed74a0aSJilles Tjoelker 		updatecharset();
483e4b50334SJilles Tjoelker 		return;
484e4b50334SJilles Tjoelker 	}
485e4b50334SJilles Tjoelker 	locdef = bltinlookup("LANG", 0);
486e4b50334SJilles Tjoelker 	for (i = 0; locale_names[i] != NULL; i++) {
487e4b50334SJilles Tjoelker 		loc = bltinlookup(locale_names[i], 0);
488e4b50334SJilles Tjoelker 		if (loc == NULL)
489e4b50334SJilles Tjoelker 			loc = locdef;
490e4b50334SJilles Tjoelker 		if (loc != NULL)
491e4b50334SJilles Tjoelker 			setlocale(locale_categories[i], loc);
492e4b50334SJilles Tjoelker 	}
493e4b50334SJilles Tjoelker 	INTON;
4946ed74a0aSJilles Tjoelker 	updatecharset();
495e4b50334SJilles Tjoelker }
496e4b50334SJilles Tjoelker 
497e4b50334SJilles Tjoelker /*
498e4b50334SJilles Tjoelker  * Undo the effect of bltinlocaleset().
499e4b50334SJilles Tjoelker  */
500e4b50334SJilles Tjoelker void
501e4b50334SJilles Tjoelker bltinunsetlocale(void)
502e4b50334SJilles Tjoelker {
503e4b50334SJilles Tjoelker 	struct strlist *lp;
504e4b50334SJilles Tjoelker 
505e4b50334SJilles Tjoelker 	INTOFF;
506e4b50334SJilles Tjoelker 	for (lp = cmdenviron ; lp ; lp = lp->next) {
507e4b50334SJilles Tjoelker 		if (localevar(lp->text)) {
508e4b50334SJilles Tjoelker 			setlocale(LC_ALL, "");
5096ed74a0aSJilles Tjoelker 			updatecharset();
510e4b50334SJilles Tjoelker 			return;
511e4b50334SJilles Tjoelker 		}
512e4b50334SJilles Tjoelker 	}
513e4b50334SJilles Tjoelker 	INTON;
514e4b50334SJilles Tjoelker }
515e4b50334SJilles Tjoelker 
5166ed74a0aSJilles Tjoelker /*
5176ed74a0aSJilles Tjoelker  * Update the localeisutf8 flag.
5186ed74a0aSJilles Tjoelker  */
5196ed74a0aSJilles Tjoelker void
5206ed74a0aSJilles Tjoelker updatecharset(void)
5216ed74a0aSJilles Tjoelker {
5226ed74a0aSJilles Tjoelker 	char *charset;
5236ed74a0aSJilles Tjoelker 
5246ed74a0aSJilles Tjoelker 	charset = nl_langinfo(CODESET);
5256ed74a0aSJilles Tjoelker 	localeisutf8 = !strcmp(charset, "UTF-8");
5266ed74a0aSJilles Tjoelker }
5274b88c807SRodney W. Grimes 
52807eb7033SJilles Tjoelker void
52907eb7033SJilles Tjoelker initcharset(void)
53007eb7033SJilles Tjoelker {
53107eb7033SJilles Tjoelker 	updatecharset();
53207eb7033SJilles Tjoelker 	initial_localeisutf8 = localeisutf8;
53307eb7033SJilles Tjoelker }
53407eb7033SJilles Tjoelker 
5354b88c807SRodney W. Grimes /*
5364b88c807SRodney W. Grimes  * Generate a list of exported variables.  This routine is used to construct
5374b88c807SRodney W. Grimes  * the third argument to execve when executing a program.
5384b88c807SRodney W. Grimes  */
5394b88c807SRodney W. Grimes 
5404b88c807SRodney W. Grimes char **
5415134c3f7SWarner Losh environment(void)
5425134c3f7SWarner Losh {
5434b88c807SRodney W. Grimes 	int nenv;
5444b88c807SRodney W. Grimes 	struct var **vpp;
5454b88c807SRodney W. Grimes 	struct var *vp;
5464b88c807SRodney W. Grimes 	char **env, **ep;
5474b88c807SRodney W. Grimes 
5484b88c807SRodney W. Grimes 	nenv = 0;
5494b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5504b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
5514b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
5524b88c807SRodney W. Grimes 				nenv++;
5534b88c807SRodney W. Grimes 	}
5544b88c807SRodney W. Grimes 	ep = env = stalloc((nenv + 1) * sizeof *env);
5554b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5564b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
5574b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
5584b88c807SRodney W. Grimes 				*ep++ = vp->text;
5594b88c807SRodney W. Grimes 	}
5604b88c807SRodney W. Grimes 	*ep = NULL;
5614b88c807SRodney W. Grimes 	return env;
5624b88c807SRodney W. Grimes }
5634b88c807SRodney W. Grimes 
5644b88c807SRodney W. Grimes 
56588328642SDavid E. O'Brien static int
566692f35fdSStefan Farfeleder var_compare(const void *a, const void *b)
567692f35fdSStefan Farfeleder {
568692f35fdSStefan Farfeleder 	const char *const *sa, *const *sb;
569692f35fdSStefan Farfeleder 
570692f35fdSStefan Farfeleder 	sa = a;
571692f35fdSStefan Farfeleder 	sb = b;
572692f35fdSStefan Farfeleder 	/*
573692f35fdSStefan Farfeleder 	 * This compares two var=value strings which creates a different
574692f35fdSStefan Farfeleder 	 * order from what you would probably expect.  POSIX is somewhat
575692f35fdSStefan Farfeleder 	 * ambiguous on what should be sorted exactly.
576692f35fdSStefan Farfeleder 	 */
577692f35fdSStefan Farfeleder 	return strcoll(*sa, *sb);
578692f35fdSStefan Farfeleder }
579692f35fdSStefan Farfeleder 
5804b88c807SRodney W. Grimes 
5814b88c807SRodney W. Grimes /*
582cff1d849SJilles Tjoelker  * Command to list all variables which are set.  This is invoked from the
583cff1d849SJilles Tjoelker  * set command when it is called without any options or operands.
5844b88c807SRodney W. Grimes  */
5854b88c807SRodney W. Grimes 
5864b88c807SRodney W. Grimes int
5875134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused)
588aa9caaf6SPeter Wemm {
5894b88c807SRodney W. Grimes 	struct var **vpp;
5904b88c807SRodney W. Grimes 	struct var *vp;
59159258844STim J. Robbins 	const char *s;
592692f35fdSStefan Farfeleder 	const char **vars;
593692f35fdSStefan Farfeleder 	int i, n;
5944b88c807SRodney W. Grimes 
595692f35fdSStefan Farfeleder 	/*
596692f35fdSStefan Farfeleder 	 * POSIX requires us to sort the variables.
597692f35fdSStefan Farfeleder 	 */
598692f35fdSStefan Farfeleder 	n = 0;
5994b88c807SRodney W. Grimes 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
6004b88c807SRodney W. Grimes 		for (vp = *vpp; vp; vp = vp->next) {
601692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
602692f35fdSStefan Farfeleder 				n++;
603692f35fdSStefan Farfeleder 		}
604692f35fdSStefan Farfeleder 	}
605692f35fdSStefan Farfeleder 
606*33233ec7SJilles Tjoelker 	INTOFF;
607692f35fdSStefan Farfeleder 	vars = ckmalloc(n * sizeof(*vars));
608692f35fdSStefan Farfeleder 	i = 0;
609692f35fdSStefan Farfeleder 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
610692f35fdSStefan Farfeleder 		for (vp = *vpp; vp; vp = vp->next) {
611692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
612692f35fdSStefan Farfeleder 				vars[i++] = vp->text;
613692f35fdSStefan Farfeleder 		}
614692f35fdSStefan Farfeleder 	}
615692f35fdSStefan Farfeleder 
616692f35fdSStefan Farfeleder 	qsort(vars, n, sizeof(*vars), var_compare);
617692f35fdSStefan Farfeleder 	for (i = 0; i < n; i++) {
618f5f215e2SJilles Tjoelker 		/*
619f5f215e2SJilles Tjoelker 		 * Skip improper variable names so the output remains usable as
620f5f215e2SJilles Tjoelker 		 * shell input.
621f5f215e2SJilles Tjoelker 		 */
622f5f215e2SJilles Tjoelker 		if (!isassignment(vars[i]))
623f5f215e2SJilles Tjoelker 			continue;
624aeb5d065SJilles Tjoelker 		s = strchr(vars[i], '=');
625aeb5d065SJilles Tjoelker 		s++;
626aeb5d065SJilles Tjoelker 		outbin(vars[i], s - vars[i], out1);
627aeb5d065SJilles Tjoelker 		out1qstr(s);
62859258844STim J. Robbins 		out1c('\n');
6294b88c807SRodney W. Grimes 	}
630692f35fdSStefan Farfeleder 	ckfree(vars);
631*33233ec7SJilles Tjoelker 	INTON;
632692f35fdSStefan Farfeleder 
6334b88c807SRodney W. Grimes 	return 0;
6344b88c807SRodney W. Grimes }
6354b88c807SRodney W. Grimes 
6364b88c807SRodney W. Grimes 
6374b88c807SRodney W. Grimes 
6384b88c807SRodney W. Grimes /*
6394b88c807SRodney W. Grimes  * The export and readonly commands.
6404b88c807SRodney W. Grimes  */
6414b88c807SRodney W. Grimes 
6424b88c807SRodney W. Grimes int
6435134c3f7SWarner Losh exportcmd(int argc, char **argv)
644aa9caaf6SPeter Wemm {
6454b88c807SRodney W. Grimes 	struct var **vpp;
6464b88c807SRodney W. Grimes 	struct var *vp;
6474b88c807SRodney W. Grimes 	char *name;
6484b88c807SRodney W. Grimes 	char *p;
64945086f8cSTim J. Robbins 	char *cmdname;
65045086f8cSTim J. Robbins 	int ch, values;
6514b88c807SRodney W. Grimes 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
6524b88c807SRodney W. Grimes 
65345086f8cSTim J. Robbins 	cmdname = argv[0];
65445086f8cSTim J. Robbins 	optreset = optind = 1;
655050f7913STim J. Robbins 	opterr = 0;
65645086f8cSTim J. Robbins 	values = 0;
65745086f8cSTim J. Robbins 	while ((ch = getopt(argc, argv, "p")) != -1) {
65845086f8cSTim J. Robbins 		switch (ch) {
65945086f8cSTim J. Robbins 		case 'p':
66045086f8cSTim J. Robbins 			values = 1;
66145086f8cSTim J. Robbins 			break;
66245086f8cSTim J. Robbins 		case '?':
66345086f8cSTim J. Robbins 		default:
66445086f8cSTim J. Robbins 			error("unknown option: -%c", optopt);
66545086f8cSTim J. Robbins 		}
66645086f8cSTim J. Robbins 	}
66745086f8cSTim J. Robbins 	argc -= optind;
66845086f8cSTim J. Robbins 	argv += optind;
66945086f8cSTim J. Robbins 
6703fda45ecSStefan Farfeleder 	if (values && argc != 0)
6713fda45ecSStefan Farfeleder 		error("-p requires no arguments");
67245086f8cSTim J. Robbins 	if (argc != 0) {
6733fda45ecSStefan Farfeleder 		while ((name = *argv++) != NULL) {
6744b88c807SRodney W. Grimes 			if ((p = strchr(name, '=')) != NULL) {
6754b88c807SRodney W. Grimes 				p++;
6764b88c807SRodney W. Grimes 			} else {
6773a99ed46SJilles Tjoelker 				vp = find_var(name, NULL, NULL);
6783a99ed46SJilles Tjoelker 				if (vp != NULL) {
6794b88c807SRodney W. Grimes 					vp->flags |= flag;
680ba726b8aSAndrey A. Chernov 					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
6815b78c6c2SSean Farley 						change_env(vp->text, 1);
682ba726b8aSAndrey A. Chernov 						(void) setlocale(LC_ALL, "");
6836ed74a0aSJilles Tjoelker 						updatecharset();
684ba726b8aSAndrey A. Chernov 					}
6853a99ed46SJilles Tjoelker 					continue;
6864b88c807SRodney W. Grimes 				}
6874b88c807SRodney W. Grimes 			}
6884b88c807SRodney W. Grimes 			setvar(name, p, flag);
6894b88c807SRodney W. Grimes 		}
6904b88c807SRodney W. Grimes 	} else {
6914b88c807SRodney W. Grimes 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6924b88c807SRodney W. Grimes 			for (vp = *vpp ; vp ; vp = vp->next) {
6934b88c807SRodney W. Grimes 				if (vp->flags & flag) {
69445086f8cSTim J. Robbins 					if (values) {
695f5f215e2SJilles Tjoelker 						/*
696f5f215e2SJilles Tjoelker 						 * Skip improper variable names
697f5f215e2SJilles Tjoelker 						 * so the output remains usable
698f5f215e2SJilles Tjoelker 						 * as shell input.
699f5f215e2SJilles Tjoelker 						 */
700f5f215e2SJilles Tjoelker 						if (!isassignment(vp->text))
701f5f215e2SJilles Tjoelker 							continue;
70245086f8cSTim J. Robbins 						out1str(cmdname);
70345086f8cSTim J. Robbins 						out1c(' ');
70445086f8cSTim J. Robbins 					}
70545086f8cSTim J. Robbins 					if (values && !(vp->flags & VUNSET)) {
706258ef734SJilles Tjoelker 						outbin(vp->text,
707258ef734SJilles Tjoelker 						    vp->name_len + 1, out1);
708258ef734SJilles Tjoelker 						out1qstr(vp->text +
709258ef734SJilles Tjoelker 						    vp->name_len + 1);
710aeb5d065SJilles Tjoelker 					} else
711258ef734SJilles Tjoelker 						outbin(vp->text, vp->name_len,
712aeb5d065SJilles Tjoelker 						    out1);
7134b88c807SRodney W. Grimes 					out1c('\n');
7144b88c807SRodney W. Grimes 				}
7154b88c807SRodney W. Grimes 			}
7164b88c807SRodney W. Grimes 		}
7174b88c807SRodney W. Grimes 	}
7184b88c807SRodney W. Grimes 	return 0;
7194b88c807SRodney W. Grimes }
7204b88c807SRodney W. Grimes 
7214b88c807SRodney W. Grimes 
7224b88c807SRodney W. Grimes /*
7234b88c807SRodney W. Grimes  * The "local" command.
7244b88c807SRodney W. Grimes  */
7254b88c807SRodney W. Grimes 
726aa9caaf6SPeter Wemm int
7275134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused)
728aa9caaf6SPeter Wemm {
7294b88c807SRodney W. Grimes 	char *name;
7304b88c807SRodney W. Grimes 
7314b88c807SRodney W. Grimes 	if (! in_function())
7324b88c807SRodney W. Grimes 		error("Not in a function");
7334b88c807SRodney W. Grimes 	while ((name = *argptr++) != NULL) {
7344b88c807SRodney W. Grimes 		mklocal(name);
7354b88c807SRodney W. Grimes 	}
7364b88c807SRodney W. Grimes 	return 0;
7374b88c807SRodney W. Grimes }
7384b88c807SRodney W. Grimes 
7394b88c807SRodney W. Grimes 
7404b88c807SRodney W. Grimes /*
7414b88c807SRodney W. Grimes  * Make a variable a local variable.  When a variable is made local, it's
7424b88c807SRodney W. Grimes  * value and flags are saved in a localvar structure.  The saved values
7434b88c807SRodney W. Grimes  * will be restored when the shell function returns.  We handle the name
7444b88c807SRodney W. Grimes  * "-" as a special case.
7454b88c807SRodney W. Grimes  */
7464b88c807SRodney W. Grimes 
7474b88c807SRodney W. Grimes void
7485134c3f7SWarner Losh mklocal(char *name)
7494b88c807SRodney W. Grimes {
7504b88c807SRodney W. Grimes 	struct localvar *lvp;
7514b88c807SRodney W. Grimes 	struct var **vpp;
7524b88c807SRodney W. Grimes 	struct var *vp;
7534b88c807SRodney W. Grimes 
7544b88c807SRodney W. Grimes 	INTOFF;
7554b88c807SRodney W. Grimes 	lvp = ckmalloc(sizeof (struct localvar));
7564b88c807SRodney W. Grimes 	if (name[0] == '-' && name[1] == '\0') {
7574b88c807SRodney W. Grimes 		lvp->text = ckmalloc(sizeof optlist);
758aa9caaf6SPeter Wemm 		memcpy(lvp->text, optlist, sizeof optlist);
7594b88c807SRodney W. Grimes 		vp = NULL;
7604b88c807SRodney W. Grimes 	} else {
7613a99ed46SJilles Tjoelker 		vp = find_var(name, &vpp, NULL);
7624b88c807SRodney W. Grimes 		if (vp == NULL) {
7634b88c807SRodney W. Grimes 			if (strchr(name, '='))
764c543e1aeSJilles Tjoelker 				setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
7654b88c807SRodney W. Grimes 			else
766c543e1aeSJilles Tjoelker 				setvar(name, NULL, VSTRFIXED | VNOLOCAL);
7674b88c807SRodney W. Grimes 			vp = *vpp;	/* the new variable */
7684b88c807SRodney W. Grimes 			lvp->text = NULL;
7694b88c807SRodney W. Grimes 			lvp->flags = VUNSET;
7704b88c807SRodney W. Grimes 		} else {
7714b88c807SRodney W. Grimes 			lvp->text = vp->text;
7724b88c807SRodney W. Grimes 			lvp->flags = vp->flags;
7734b88c807SRodney W. Grimes 			vp->flags |= VSTRFIXED|VTEXTFIXED;
7743a99ed46SJilles Tjoelker 			if (name[vp->name_len] == '=')
775c543e1aeSJilles Tjoelker 				setvareq(savestr(name), VNOLOCAL);
7764b88c807SRodney W. Grimes 		}
7774b88c807SRodney W. Grimes 	}
7784b88c807SRodney W. Grimes 	lvp->vp = vp;
7794b88c807SRodney W. Grimes 	lvp->next = localvars;
7804b88c807SRodney W. Grimes 	localvars = lvp;
7814b88c807SRodney W. Grimes 	INTON;
7824b88c807SRodney W. Grimes }
7834b88c807SRodney W. Grimes 
7844b88c807SRodney W. Grimes 
7854b88c807SRodney W. Grimes /*
7864b88c807SRodney W. Grimes  * Called after a function returns.
7874b88c807SRodney W. Grimes  */
7884b88c807SRodney W. Grimes 
7894b88c807SRodney W. Grimes void
7905134c3f7SWarner Losh poplocalvars(void)
7915134c3f7SWarner Losh {
7924b88c807SRodney W. Grimes 	struct localvar *lvp;
7934b88c807SRodney W. Grimes 	struct var *vp;
7944b88c807SRodney W. Grimes 
7954b88c807SRodney W. Grimes 	while ((lvp = localvars) != NULL) {
7964b88c807SRodney W. Grimes 		localvars = lvp->next;
7974b88c807SRodney W. Grimes 		vp = lvp->vp;
7984b88c807SRodney W. Grimes 		if (vp == NULL) {	/* $- saved */
799aa9caaf6SPeter Wemm 			memcpy(optlist, lvp->text, sizeof optlist);
8004b88c807SRodney W. Grimes 			ckfree(lvp->text);
8014a7b1013SJilles Tjoelker 			optschanged();
8024b88c807SRodney W. Grimes 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
8034b88c807SRodney W. Grimes 			(void)unsetvar(vp->text);
8044b88c807SRodney W. Grimes 		} else {
8054b88c807SRodney W. Grimes 			if ((vp->flags & VTEXTFIXED) == 0)
8064b88c807SRodney W. Grimes 				ckfree(vp->text);
8074b88c807SRodney W. Grimes 			vp->flags = lvp->flags;
8084b88c807SRodney W. Grimes 			vp->text = lvp->text;
8094b88c807SRodney W. Grimes 		}
8104b88c807SRodney W. Grimes 		ckfree(lvp);
8114b88c807SRodney W. Grimes 	}
8124b88c807SRodney W. Grimes }
8134b88c807SRodney W. Grimes 
8144b88c807SRodney W. Grimes 
815aa9caaf6SPeter Wemm int
8165134c3f7SWarner Losh setvarcmd(int argc, char **argv)
817aa9caaf6SPeter Wemm {
8184b88c807SRodney W. Grimes 	if (argc <= 2)
8194b88c807SRodney W. Grimes 		return unsetcmd(argc, argv);
8204b88c807SRodney W. Grimes 	else if (argc == 3)
8214b88c807SRodney W. Grimes 		setvar(argv[1], argv[2], 0);
8224b88c807SRodney W. Grimes 	else
823274110dfSJilles Tjoelker 		error("too many arguments");
8244b88c807SRodney W. Grimes 	return 0;
8254b88c807SRodney W. Grimes }
8264b88c807SRodney W. Grimes 
8274b88c807SRodney W. Grimes 
8284b88c807SRodney W. Grimes /*
829cff1d849SJilles Tjoelker  * The unset builtin command.
8304b88c807SRodney W. Grimes  */
8314b88c807SRodney W. Grimes 
832aa9caaf6SPeter Wemm int
8335134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused)
834aa9caaf6SPeter Wemm {
8354b88c807SRodney W. Grimes 	char **ap;
8364b88c807SRodney W. Grimes 	int i;
8374b88c807SRodney W. Grimes 	int flg_func = 0;
8384b88c807SRodney W. Grimes 	int flg_var = 0;
8394b88c807SRodney W. Grimes 	int ret = 0;
8404b88c807SRodney W. Grimes 
8414b88c807SRodney W. Grimes 	while ((i = nextopt("vf")) != '\0') {
8424b88c807SRodney W. Grimes 		if (i == 'f')
8434b88c807SRodney W. Grimes 			flg_func = 1;
8444b88c807SRodney W. Grimes 		else
8454b88c807SRodney W. Grimes 			flg_var = 1;
8464b88c807SRodney W. Grimes 	}
8474b88c807SRodney W. Grimes 	if (flg_func == 0 && flg_var == 0)
8484b88c807SRodney W. Grimes 		flg_var = 1;
8494b88c807SRodney W. Grimes 
8504b88c807SRodney W. Grimes 	for (ap = argptr; *ap ; ap++) {
8514b88c807SRodney W. Grimes 		if (flg_func)
8524b88c807SRodney W. Grimes 			ret |= unsetfunc(*ap);
8534b88c807SRodney W. Grimes 		if (flg_var)
8544b88c807SRodney W. Grimes 			ret |= unsetvar(*ap);
8554b88c807SRodney W. Grimes 	}
8564b88c807SRodney W. Grimes 	return ret;
8574b88c807SRodney W. Grimes }
8584b88c807SRodney W. Grimes 
8594b88c807SRodney W. Grimes 
8604b88c807SRodney W. Grimes /*
8614b88c807SRodney W. Grimes  * Unset the specified variable.
8624b88c807SRodney W. Grimes  */
8634b88c807SRodney W. Grimes 
864ab0a2172SSteve Price int
8652cac6e36SJilles Tjoelker unsetvar(const char *s)
8664b88c807SRodney W. Grimes {
8674b88c807SRodney W. Grimes 	struct var **vpp;
8684b88c807SRodney W. Grimes 	struct var *vp;
8694b88c807SRodney W. Grimes 
8703a99ed46SJilles Tjoelker 	vp = find_var(s, &vpp, NULL);
8713a99ed46SJilles Tjoelker 	if (vp == NULL)
8723a99ed46SJilles Tjoelker 		return (0);
8734b88c807SRodney W. Grimes 	if (vp->flags & VREADONLY)
8744b88c807SRodney W. Grimes 		return (1);
8754b88c807SRodney W. Grimes 	INTOFF;
8763a99ed46SJilles Tjoelker 	if (vp->text[vp->name_len + 1] != '\0')
8774b88c807SRodney W. Grimes 		setvar(s, nullstr, 0);
878ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
8795b78c6c2SSean Farley 		change_env(s, 0);
880ba726b8aSAndrey A. Chernov 		setlocale(LC_ALL, "");
8816ed74a0aSJilles Tjoelker 		updatecharset();
882ba726b8aSAndrey A. Chernov 	}
8834b88c807SRodney W. Grimes 	vp->flags &= ~VEXPORT;
8844b88c807SRodney W. Grimes 	vp->flags |= VUNSET;
8854b88c807SRodney W. Grimes 	if ((vp->flags & VSTRFIXED) == 0) {
8864b88c807SRodney W. Grimes 		if ((vp->flags & VTEXTFIXED) == 0)
8874b88c807SRodney W. Grimes 			ckfree(vp->text);
8884b88c807SRodney W. Grimes 		*vpp = vp->next;
8894b88c807SRodney W. Grimes 		ckfree(vp);
8904b88c807SRodney W. Grimes 	}
8914b88c807SRodney W. Grimes 	INTON;
8924b88c807SRodney W. Grimes 	return (0);
8934b88c807SRodney W. Grimes }
8944b88c807SRodney W. Grimes 
8954b88c807SRodney W. Grimes 
8964b88c807SRodney W. Grimes 
8974b88c807SRodney W. Grimes /*
8984b88c807SRodney W. Grimes  * Returns true if the two strings specify the same varable.  The first
8994b88c807SRodney W. Grimes  * variable name is terminated by '='; the second may be terminated by
9004b88c807SRodney W. Grimes  * either '=' or '\0'.
9014b88c807SRodney W. Grimes  */
9024b88c807SRodney W. Grimes 
90388328642SDavid E. O'Brien static int
9042cac6e36SJilles Tjoelker varequal(const char *p, const char *q)
9054b88c807SRodney W. Grimes {
9064b88c807SRodney W. Grimes 	while (*p == *q++) {
9074b88c807SRodney W. Grimes 		if (*p++ == '=')
9084b88c807SRodney W. Grimes 			return 1;
9094b88c807SRodney W. Grimes 	}
9104b88c807SRodney W. Grimes 	if (*p == '=' && *(q - 1) == '\0')
9114b88c807SRodney W. Grimes 		return 1;
9124b88c807SRodney W. Grimes 	return 0;
9134b88c807SRodney W. Grimes }
9143a99ed46SJilles Tjoelker 
9153a99ed46SJilles Tjoelker /*
9163a99ed46SJilles Tjoelker  * Search for a variable.
9173a99ed46SJilles Tjoelker  * 'name' may be terminated by '=' or a NUL.
9183a99ed46SJilles Tjoelker  * vppp is set to the pointer to vp, or the list head if vp isn't found
9193a99ed46SJilles Tjoelker  * lenp is set to the number of charactets in 'name'
9203a99ed46SJilles Tjoelker  */
9213a99ed46SJilles Tjoelker 
9223a99ed46SJilles Tjoelker static struct var *
9233a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp)
9243a99ed46SJilles Tjoelker {
9253a99ed46SJilles Tjoelker 	unsigned int hashval;
9263a99ed46SJilles Tjoelker 	int len;
9273a99ed46SJilles Tjoelker 	struct var *vp, **vpp;
9283a99ed46SJilles Tjoelker 	const char *p = name;
9293a99ed46SJilles Tjoelker 
9303a99ed46SJilles Tjoelker 	hashval = 0;
9313a99ed46SJilles Tjoelker 	while (*p && *p != '=')
9323a99ed46SJilles Tjoelker 		hashval = 2 * hashval + (unsigned char)*p++;
9333a99ed46SJilles Tjoelker 	len = p - name;
9343a99ed46SJilles Tjoelker 
9353a99ed46SJilles Tjoelker 	if (lenp)
9363a99ed46SJilles Tjoelker 		*lenp = len;
9373a99ed46SJilles Tjoelker 	vpp = &vartab[hashval % VTABSIZE];
9383a99ed46SJilles Tjoelker 	if (vppp)
9393a99ed46SJilles Tjoelker 		*vppp = vpp;
9403a99ed46SJilles Tjoelker 
9413a99ed46SJilles Tjoelker 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
9423a99ed46SJilles Tjoelker 		if (vp->name_len != len)
9433a99ed46SJilles Tjoelker 			continue;
9443a99ed46SJilles Tjoelker 		if (memcmp(vp->text, name, len) != 0)
9453a99ed46SJilles Tjoelker 			continue;
9463a99ed46SJilles Tjoelker 		if (vppp)
9473a99ed46SJilles Tjoelker 			*vppp = vpp;
9483a99ed46SJilles Tjoelker 		return vp;
9493a99ed46SJilles Tjoelker 	}
9503a99ed46SJilles Tjoelker 	return NULL;
9513a99ed46SJilles Tjoelker }
952