xref: /freebsd/bin/sh/var.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Kenneth Almquist.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
354b88c807SRodney W. Grimes #ifndef lint
363d7b5b93SPhilippe Charnier #if 0
373d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
383d7b5b93SPhilippe Charnier #endif
394b88c807SRodney W. Grimes #endif /* not lint */
402749b141SDavid E. O'Brien #include <sys/cdefs.h>
412749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
424b88c807SRodney W. Grimes 
43aa9caaf6SPeter Wemm #include <unistd.h>
44aa9caaf6SPeter Wemm #include <stdlib.h>
458d5c19ffSDavid E. O'Brien #include <paths.h>
46aa9caaf6SPeter Wemm 
474b88c807SRodney W. Grimes /*
484b88c807SRodney W. Grimes  * Shell variables.
494b88c807SRodney W. Grimes  */
504b88c807SRodney W. Grimes 
51ba726b8aSAndrey A. Chernov #include <locale.h>
526ed74a0aSJilles Tjoelker #include <langinfo.h>
53ba726b8aSAndrey A. Chernov 
544b88c807SRodney W. Grimes #include "shell.h"
554b88c807SRodney W. Grimes #include "output.h"
564b88c807SRodney W. Grimes #include "expand.h"
574b88c807SRodney W. Grimes #include "nodes.h"	/* for other headers */
584b88c807SRodney W. Grimes #include "eval.h"	/* defines cmdenviron */
594b88c807SRodney W. Grimes #include "exec.h"
604b88c807SRodney W. Grimes #include "syntax.h"
614b88c807SRodney W. Grimes #include "options.h"
624b88c807SRodney W. Grimes #include "mail.h"
634b88c807SRodney W. Grimes #include "var.h"
644b88c807SRodney W. Grimes #include "memalloc.h"
654b88c807SRodney W. Grimes #include "error.h"
664b88c807SRodney W. Grimes #include "mystring.h"
67ab0a2172SSteve Price #include "parser.h"
68454a02b3SJilles Tjoelker #include "builtins.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;
80c6c5dd37SJilles Tjoelker 	const 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;
87580eefdfSJilles Tjoelker struct var vterm;
88aa9caaf6SPeter Wemm #endif
894b88c807SRodney W. Grimes struct var vifs;
904b88c807SRodney W. Grimes struct var vmail;
914b88c807SRodney W. Grimes struct var vmpath;
924b88c807SRodney W. Grimes struct var vpath;
934b88c807SRodney W. Grimes struct var vps1;
944b88c807SRodney W. Grimes struct var vps2;
95120c8e6cSStefan Farfeleder struct var vps4;
96aa7b6f82SDavid E. O'Brien static struct var voptind;
97caf29fabSJilles Tjoelker struct var vdisvfork;
984b88c807SRodney W. Grimes 
9958bdb076SJilles Tjoelker struct localvar *localvars;
100c543e1aeSJilles Tjoelker int forcelocal;
101c543e1aeSJilles Tjoelker 
102aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = {
103aa9caaf6SPeter Wemm #ifndef NO_HISTORY
104c6c5dd37SJilles Tjoelker 	{ &vhistsize,	VUNSET,				"HISTSIZE=",
105ab0a2172SSteve Price 	  sethistsize },
106aa9caaf6SPeter Wemm #endif
107c6c5dd37SJilles Tjoelker 	{ &vifs,	0,				"IFS= \t\n",
108ab0a2172SSteve Price 	  NULL },
109c6c5dd37SJilles Tjoelker 	{ &vmail,	VUNSET,				"MAIL=",
110ab0a2172SSteve Price 	  NULL },
111c6c5dd37SJilles Tjoelker 	{ &vmpath,	VUNSET,				"MAILPATH=",
112ab0a2172SSteve Price 	  NULL },
113c6c5dd37SJilles Tjoelker 	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
114ab0a2172SSteve Price 	  changepath },
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 },
128caf29fabSJilles Tjoelker 	{ &vdisvfork,	VUNSET,				"SH_DISABLE_VFORK=",
129caf29fabSJilles Tjoelker 	  NULL },
130ab0a2172SSteve Price 	{ NULL,	0,				NULL,
131ab0a2172SSteve Price 	  NULL }
1324b88c807SRodney W. Grimes };
1334b88c807SRodney W. Grimes 
134aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE];
1354b88c807SRodney W. Grimes 
136aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = {
137e4b50334SJilles Tjoelker 	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
138e4b50334SJilles Tjoelker 	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
139e4b50334SJilles Tjoelker };
140aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = {
141e4b50334SJilles Tjoelker 	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
142e4b50334SJilles Tjoelker };
143e4b50334SJilles Tjoelker 
14488328642SDavid E. O'Brien static int varequal(const char *, const char *);
1453a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *);
14688328642SDavid E. O'Brien static int localevar(const char *);
14722afca9bSJilles Tjoelker static void setvareq_const(const char *s, int flags);
1484b88c807SRodney W. Grimes 
14959e0cc8eSJilles Tjoelker extern char **environ;
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes /*
15259e0cc8eSJilles Tjoelker  * This routine initializes the builtin variables and imports the environment.
15359e0cc8eSJilles Tjoelker  * It is called when the shell is initialized.
1544b88c807SRodney W. Grimes  */
1554b88c807SRodney W. Grimes 
1564b88c807SRodney W. Grimes void
1575134c3f7SWarner Losh initvar(void)
1585134c3f7SWarner Losh {
15939dccc6fSTim J. Robbins 	char ppid[20];
1604b88c807SRodney W. Grimes 	const struct varinit *ip;
1614b88c807SRodney W. Grimes 	struct var *vp;
1624b88c807SRodney W. Grimes 	struct var **vpp;
16359e0cc8eSJilles Tjoelker 	char **envp;
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1663a99ed46SJilles Tjoelker 		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
1673a99ed46SJilles Tjoelker 			continue;
1684b88c807SRodney W. Grimes 		vp->next = *vpp;
1694b88c807SRodney W. Grimes 		*vpp = vp;
170c6c5dd37SJilles Tjoelker 		vp->text = __DECONST(char *, ip->text);
171c6c5dd37SJilles Tjoelker 		vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
172ab0a2172SSteve Price 		vp->func = ip->func;
1734b88c807SRodney W. Grimes 	}
1744b88c807SRodney W. Grimes 	/*
1754b88c807SRodney W. Grimes 	 * PS1 depends on uid
1764b88c807SRodney W. Grimes 	 */
1773a99ed46SJilles Tjoelker 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
1784b88c807SRodney W. Grimes 		vps1.next = *vpp;
1794b88c807SRodney W. Grimes 		*vpp = &vps1;
180c6c5dd37SJilles Tjoelker 		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
1814b88c807SRodney W. Grimes 		vps1.flags = VSTRFIXED|VTEXTFIXED;
1824b88c807SRodney W. Grimes 	}
18339dccc6fSTim J. Robbins 	fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
18439dccc6fSTim J. Robbins 	setvarsafe("PPID", ppid, 0);
18559e0cc8eSJilles Tjoelker 	for (envp = environ ; *envp ; envp++) {
18659e0cc8eSJilles Tjoelker 		if (strchr(*envp, '=')) {
18759e0cc8eSJilles Tjoelker 			setvareq(*envp, VEXPORT|VTEXTFIXED);
18859e0cc8eSJilles Tjoelker 		}
18959e0cc8eSJilles Tjoelker 	}
19022afca9bSJilles Tjoelker 	setvareq_const("OPTIND=1", 0);
1917cca93e6SJilles Tjoelker 	setvareq_const("IFS= \t\n", 0);
1924b88c807SRodney W. Grimes }
1934b88c807SRodney W. Grimes 
1944b88c807SRodney W. Grimes /*
195ab0a2172SSteve Price  * Safe version of setvar, returns 1 on success 0 on failure.
196ab0a2172SSteve Price  */
197ab0a2172SSteve Price 
198ab0a2172SSteve Price int
1992cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags)
200ab0a2172SSteve Price {
201ab0a2172SSteve Price 	struct jmploc jmploc;
202224fbf9fSJilles Tjoelker 	struct jmploc *const savehandler = handler;
203ab0a2172SSteve Price 	int err = 0;
2049922c6d2SJilles Tjoelker 	int inton;
205ab0a2172SSteve Price 
2069922c6d2SJilles Tjoelker 	inton = is_int_on();
207ab0a2172SSteve Price 	if (setjmp(jmploc.loc))
208ab0a2172SSteve Price 		err = 1;
209ab0a2172SSteve Price 	else {
210ab0a2172SSteve Price 		handler = &jmploc;
211ab0a2172SSteve Price 		setvar(name, val, flags);
212ab0a2172SSteve Price 	}
213ab0a2172SSteve Price 	handler = savehandler;
2149922c6d2SJilles Tjoelker 	SETINTON(inton);
215ab0a2172SSteve Price 	return err;
216ab0a2172SSteve Price }
217ab0a2172SSteve Price 
218ab0a2172SSteve Price /*
219b3decf89SJens Schweikhardt  * Set the value of a variable.  The flags argument is stored with the
2204b88c807SRodney W. Grimes  * flags of the variable.  If val is NULL, the variable is unset.
2214b88c807SRodney W. Grimes  */
2224b88c807SRodney W. Grimes 
2234b88c807SRodney W. Grimes void
2242cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags)
2254b88c807SRodney W. Grimes {
2262cac6e36SJilles Tjoelker 	const char *p;
227670dd3f0SJilles Tjoelker 	size_t len;
228670dd3f0SJilles Tjoelker 	size_t namelen;
229670dd3f0SJilles Tjoelker 	size_t vallen;
2304b88c807SRodney W. Grimes 	char *nameeq;
2314b88c807SRodney W. Grimes 	int isbad;
2324b88c807SRodney W. Grimes 
2334b88c807SRodney W. Grimes 	isbad = 0;
2344b88c807SRodney W. Grimes 	p = name;
235ba726b8aSAndrey A. Chernov 	if (! is_name(*p))
2364b88c807SRodney W. Grimes 		isbad = 1;
237ba726b8aSAndrey A. Chernov 	p++;
2384b88c807SRodney W. Grimes 	for (;;) {
2394b88c807SRodney W. Grimes 		if (! is_in_name(*p)) {
2404b88c807SRodney W. Grimes 			if (*p == '\0' || *p == '=')
2414b88c807SRodney W. Grimes 				break;
2424b88c807SRodney W. Grimes 			isbad = 1;
2434b88c807SRodney W. Grimes 		}
2444b88c807SRodney W. Grimes 		p++;
2454b88c807SRodney W. Grimes 	}
2464b88c807SRodney W. Grimes 	namelen = p - name;
2474b88c807SRodney W. Grimes 	if (isbad)
248670dd3f0SJilles Tjoelker 		error("%.*s: bad variable name", (int)namelen, name);
2494b88c807SRodney W. Grimes 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2504b88c807SRodney W. Grimes 	if (val == NULL) {
2514b88c807SRodney W. Grimes 		flags |= VUNSET;
252670dd3f0SJilles Tjoelker 		vallen = 0;
2534b88c807SRodney W. Grimes 	} else {
254670dd3f0SJilles Tjoelker 		vallen = strlen(val);
255670dd3f0SJilles Tjoelker 		len += vallen;
2564b88c807SRodney W. Grimes 	}
2571632bf1aSJilles Tjoelker 	INTOFF;
2582cac6e36SJilles Tjoelker 	nameeq = ckmalloc(len);
2592cac6e36SJilles Tjoelker 	memcpy(nameeq, name, namelen);
2602cac6e36SJilles Tjoelker 	nameeq[namelen] = '=';
2614b88c807SRodney W. Grimes 	if (val)
262670dd3f0SJilles Tjoelker 		memcpy(nameeq + namelen + 1, val, vallen + 1);
2632cac6e36SJilles Tjoelker 	else
2642cac6e36SJilles Tjoelker 		nameeq[namelen + 1] = '\0';
2654b88c807SRodney W. Grimes 	setvareq(nameeq, flags);
2661632bf1aSJilles Tjoelker 	INTON;
2674b88c807SRodney W. Grimes }
2684b88c807SRodney W. Grimes 
26988328642SDavid E. O'Brien static int
2702cac6e36SJilles Tjoelker localevar(const char *s)
271ba726b8aSAndrey A. Chernov {
272e4b50334SJilles Tjoelker 	const char *const *ss;
2734b88c807SRodney W. Grimes 
274ba726b8aSAndrey A. Chernov 	if (*s != 'L')
275ba726b8aSAndrey A. Chernov 		return 0;
276ba726b8aSAndrey A. Chernov 	if (varequal(s + 1, "ANG"))
277ba726b8aSAndrey A. Chernov 		return 1;
278ba726b8aSAndrey A. Chernov 	if (strncmp(s + 1, "C_", 2) != 0)
279ba726b8aSAndrey A. Chernov 		return 0;
280e4b50334SJilles Tjoelker 	if (varequal(s + 3, "ALL"))
281e4b50334SJilles Tjoelker 		return 1;
282e4b50334SJilles Tjoelker 	for (ss = locale_names; *ss ; ss++)
283e4b50334SJilles Tjoelker 		if (varequal(s + 3, *ss + 3))
284ba726b8aSAndrey A. Chernov 			return 1;
285ba726b8aSAndrey A. Chernov 	return 0;
286ba726b8aSAndrey A. Chernov }
2874b88c807SRodney W. Grimes 
2885b78c6c2SSean Farley 
2895b78c6c2SSean Farley /*
2905b78c6c2SSean Farley  * Sets/unsets an environment variable from a pointer that may actually be a
2915b78c6c2SSean Farley  * pointer into environ where the string should not be manipulated.
2925b78c6c2SSean Farley  */
29388328642SDavid E. O'Brien static void
2942cac6e36SJilles Tjoelker change_env(const char *s, int set)
2955b78c6c2SSean Farley {
2965b78c6c2SSean Farley 	char *eqp;
2975b78c6c2SSean Farley 	char *ss;
2985b78c6c2SSean Farley 
2991632bf1aSJilles Tjoelker 	INTOFF;
3005b78c6c2SSean Farley 	ss = savestr(s);
3015b78c6c2SSean Farley 	if ((eqp = strchr(ss, '=')) != NULL)
3025b78c6c2SSean Farley 		*eqp = '\0';
3035b78c6c2SSean Farley 	if (set && eqp != NULL)
3045b78c6c2SSean Farley 		(void) setenv(ss, eqp + 1, 1);
3055b78c6c2SSean Farley 	else
3065b78c6c2SSean Farley 		(void) unsetenv(ss);
3075b78c6c2SSean Farley 	ckfree(ss);
3081632bf1aSJilles Tjoelker 	INTON;
3095b78c6c2SSean Farley 
3105b78c6c2SSean Farley 	return;
3115b78c6c2SSean Farley }
3125b78c6c2SSean Farley 
3135b78c6c2SSean Farley 
3144b88c807SRodney W. Grimes /*
3154b88c807SRodney W. Grimes  * Same as setvar except that the variable and value are passed in
3164b88c807SRodney W. Grimes  * the first argument as name=value.  Since the first argument will
3174b88c807SRodney W. Grimes  * be actually stored in the table, it should not be a string that
3184b88c807SRodney W. Grimes  * will go away.
3194b88c807SRodney W. Grimes  */
3204b88c807SRodney W. Grimes 
3214b88c807SRodney W. Grimes void
3225134c3f7SWarner Losh setvareq(char *s, int flags)
3234b88c807SRodney W. Grimes {
3244b88c807SRodney W. Grimes 	struct var *vp, **vpp;
3253a99ed46SJilles Tjoelker 	int nlen;
3264b88c807SRodney W. Grimes 
327b8ec435eSMartin Cracauer 	if (aflag)
328b8ec435eSMartin Cracauer 		flags |= VEXPORT;
329c543e1aeSJilles Tjoelker 	if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
330c543e1aeSJilles Tjoelker 		mklocal(s);
3313a99ed46SJilles Tjoelker 	vp = find_var(s, &vpp, &nlen);
3323a99ed46SJilles Tjoelker 	if (vp != NULL) {
33389d4f883SJilles Tjoelker 		if (vp->flags & VREADONLY) {
33489d4f883SJilles Tjoelker 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
33589d4f883SJilles Tjoelker 				ckfree(s);
336d41b2be1SJilles Tjoelker 			error("%.*s: is read only", vp->name_len, vp->text);
33789d4f883SJilles Tjoelker 		}
338d73dba75SJilles Tjoelker 		if (flags & VNOSET) {
339d73dba75SJilles Tjoelker 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
340d73dba75SJilles Tjoelker 				ckfree(s);
341850460c0SJilles Tjoelker 			return;
342d73dba75SJilles Tjoelker 		}
3434b88c807SRodney W. Grimes 		INTOFF;
344ab0a2172SSteve Price 
345ab0a2172SSteve Price 		if (vp->func && (flags & VNOFUNC) == 0)
3463a99ed46SJilles Tjoelker 			(*vp->func)(s + vp->name_len + 1);
347ab0a2172SSteve Price 
3484b88c807SRodney W. Grimes 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3494b88c807SRodney W. Grimes 			ckfree(vp->text);
350ab0a2172SSteve Price 
3514b88c807SRodney W. Grimes 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3524b88c807SRodney W. Grimes 		vp->flags |= flags;
3534b88c807SRodney W. Grimes 		vp->text = s;
354ab0a2172SSteve Price 
355ab0a2172SSteve Price 		/*
356ab0a2172SSteve Price 		 * We could roll this to a function, to handle it as
357ab0a2172SSteve Price 		 * a regular variable function callback, but why bother?
35857063576SJilles Tjoelker 		 *
35957063576SJilles Tjoelker 		 * Note: this assumes iflag is not set to 1 initially.
36059e0cc8eSJilles Tjoelker 		 * As part of initvar(), this is called before arguments
36157063576SJilles Tjoelker 		 * are looked at.
362ab0a2172SSteve Price 		 */
36357063576SJilles Tjoelker 		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
36457063576SJilles Tjoelker 		    iflag == 1)
3654b88c807SRodney W. Grimes 			chkmail(1);
366ba726b8aSAndrey A. Chernov 		if ((vp->flags & VEXPORT) && localevar(s)) {
3675b78c6c2SSean Farley 			change_env(s, 1);
368ba726b8aSAndrey A. Chernov 			(void) setlocale(LC_ALL, "");
3696ed74a0aSJilles Tjoelker 			updatecharset();
370ba726b8aSAndrey A. Chernov 		}
3714b88c807SRodney W. Grimes 		INTON;
3724b88c807SRodney W. Grimes 		return;
3734b88c807SRodney W. Grimes 	}
3744b88c807SRodney W. Grimes 	/* not found */
375d73dba75SJilles Tjoelker 	if (flags & VNOSET) {
376d73dba75SJilles Tjoelker 		if ((flags & (VTEXTFIXED|VSTACK)) == 0)
377d73dba75SJilles Tjoelker 			ckfree(s);
378850460c0SJilles Tjoelker 		return;
379d73dba75SJilles Tjoelker 	}
3801632bf1aSJilles Tjoelker 	INTOFF;
3814b88c807SRodney W. Grimes 	vp = ckmalloc(sizeof (*vp));
3824b88c807SRodney W. Grimes 	vp->flags = flags;
3834b88c807SRodney W. Grimes 	vp->text = s;
3843a99ed46SJilles Tjoelker 	vp->name_len = nlen;
3854b88c807SRodney W. Grimes 	vp->next = *vpp;
386ab0a2172SSteve Price 	vp->func = NULL;
3874b88c807SRodney W. Grimes 	*vpp = vp;
388ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(s)) {
3895b78c6c2SSean Farley 		change_env(s, 1);
390ba726b8aSAndrey A. Chernov 		(void) setlocale(LC_ALL, "");
3916ed74a0aSJilles Tjoelker 		updatecharset();
392ba726b8aSAndrey A. Chernov 	}
393ba726b8aSAndrey A. Chernov 	INTON;
3944b88c807SRodney W. Grimes }
3954b88c807SRodney W. Grimes 
3964b88c807SRodney W. Grimes 
39722afca9bSJilles Tjoelker static void
39822afca9bSJilles Tjoelker setvareq_const(const char *s, int flags)
39922afca9bSJilles Tjoelker {
40022afca9bSJilles Tjoelker 	setvareq(__DECONST(char *, s), flags | VTEXTFIXED);
40122afca9bSJilles Tjoelker }
40222afca9bSJilles Tjoelker 
4034b88c807SRodney W. Grimes 
4044b88c807SRodney W. Grimes /*
4054b88c807SRodney W. Grimes  * Process a linked list of variable assignments.
4064b88c807SRodney W. Grimes  */
4074b88c807SRodney W. Grimes 
4084b88c807SRodney W. Grimes void
4098ef0ae8aSJilles Tjoelker listsetvar(struct arglist *list, int flags)
4104b88c807SRodney W. Grimes {
4118ef0ae8aSJilles Tjoelker 	int i;
4124b88c807SRodney W. Grimes 
4134b88c807SRodney W. Grimes 	INTOFF;
4148ef0ae8aSJilles Tjoelker 	for (i = 0; i < list->count; i++)
4158ef0ae8aSJilles Tjoelker 		setvareq(savestr(list->args[i]), flags);
4164b88c807SRodney W. Grimes 	INTON;
4174b88c807SRodney W. Grimes }
4184b88c807SRodney W. Grimes 
4194b88c807SRodney W. Grimes 
4204b88c807SRodney W. Grimes 
4214b88c807SRodney W. Grimes /*
4224b88c807SRodney W. Grimes  * Find the value of a variable.  Returns NULL if not set.
4234b88c807SRodney W. Grimes  */
4244b88c807SRodney W. Grimes 
4254b88c807SRodney W. Grimes char *
4262cac6e36SJilles Tjoelker lookupvar(const char *name)
4274b88c807SRodney W. Grimes {
4284b88c807SRodney W. Grimes 	struct var *v;
4294b88c807SRodney W. Grimes 
4303a99ed46SJilles Tjoelker 	v = find_var(name, NULL, NULL);
4313a99ed46SJilles Tjoelker 	if (v == NULL || v->flags & VUNSET)
4324b88c807SRodney W. Grimes 		return NULL;
4333a99ed46SJilles Tjoelker 	return v->text + v->name_len + 1;
4344b88c807SRodney W. Grimes }
4354b88c807SRodney W. Grimes 
4364b88c807SRodney W. Grimes 
4374b88c807SRodney W. Grimes 
4384b88c807SRodney W. Grimes /*
4394b88c807SRodney W. Grimes  * Search the environment of a builtin command.  If the second argument
4404b88c807SRodney W. Grimes  * is nonzero, return the value of a variable even if it hasn't been
4414b88c807SRodney W. Grimes  * exported.
4424b88c807SRodney W. Grimes  */
4434b88c807SRodney W. Grimes 
4444b88c807SRodney W. Grimes char *
4452cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall)
4464b88c807SRodney W. Grimes {
4474b88c807SRodney W. Grimes 	struct var *v;
448011d162dSJilles Tjoelker 	char *result;
4498ef0ae8aSJilles Tjoelker 	int i;
4504b88c807SRodney W. Grimes 
451011d162dSJilles Tjoelker 	result = NULL;
4528ef0ae8aSJilles Tjoelker 	if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
4538ef0ae8aSJilles Tjoelker 		if (varequal(cmdenviron->args[i], name))
4548ef0ae8aSJilles Tjoelker 			result = strchr(cmdenviron->args[i], '=') + 1;
4554b88c807SRodney W. Grimes 	}
456011d162dSJilles Tjoelker 	if (result != NULL)
457011d162dSJilles Tjoelker 		return result;
4583a99ed46SJilles Tjoelker 
4593a99ed46SJilles Tjoelker 	v = find_var(name, NULL, NULL);
4603a99ed46SJilles Tjoelker 	if (v == NULL || v->flags & VUNSET ||
4613a99ed46SJilles Tjoelker 	    (!doall && (v->flags & VEXPORT) == 0))
4624b88c807SRodney W. Grimes 		return NULL;
4633a99ed46SJilles Tjoelker 	return v->text + v->name_len + 1;
4644b88c807SRodney W. Grimes }
4654b88c807SRodney W. Grimes 
4664b88c807SRodney W. Grimes 
467e4b50334SJilles Tjoelker /*
468e4b50334SJilles Tjoelker  * Set up locale for a builtin (LANG/LC_* assignments).
469e4b50334SJilles Tjoelker  */
470e4b50334SJilles Tjoelker void
471e4b50334SJilles Tjoelker bltinsetlocale(void)
472e4b50334SJilles Tjoelker {
473e4b50334SJilles Tjoelker 	int act = 0;
474e4b50334SJilles Tjoelker 	char *loc, *locdef;
475e4b50334SJilles Tjoelker 	int i;
476e4b50334SJilles Tjoelker 
4778ef0ae8aSJilles Tjoelker 	if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
4788ef0ae8aSJilles Tjoelker 		if (localevar(cmdenviron->args[i])) {
479e4b50334SJilles Tjoelker 			act = 1;
480e4b50334SJilles Tjoelker 			break;
481e4b50334SJilles Tjoelker 		}
482e4b50334SJilles Tjoelker 	}
483e4b50334SJilles Tjoelker 	if (!act)
484e4b50334SJilles Tjoelker 		return;
485e4b50334SJilles Tjoelker 	loc = bltinlookup("LC_ALL", 0);
486e4b50334SJilles Tjoelker 	INTOFF;
487e4b50334SJilles Tjoelker 	if (loc != NULL) {
488e4b50334SJilles Tjoelker 		setlocale(LC_ALL, loc);
489e4b50334SJilles Tjoelker 		INTON;
4906ed74a0aSJilles Tjoelker 		updatecharset();
491e4b50334SJilles Tjoelker 		return;
492e4b50334SJilles Tjoelker 	}
493e4b50334SJilles Tjoelker 	locdef = bltinlookup("LANG", 0);
494e4b50334SJilles Tjoelker 	for (i = 0; locale_names[i] != NULL; i++) {
495e4b50334SJilles Tjoelker 		loc = bltinlookup(locale_names[i], 0);
496e4b50334SJilles Tjoelker 		if (loc == NULL)
497e4b50334SJilles Tjoelker 			loc = locdef;
498e4b50334SJilles Tjoelker 		if (loc != NULL)
499e4b50334SJilles Tjoelker 			setlocale(locale_categories[i], loc);
500e4b50334SJilles Tjoelker 	}
501e4b50334SJilles Tjoelker 	INTON;
5026ed74a0aSJilles Tjoelker 	updatecharset();
503e4b50334SJilles Tjoelker }
504e4b50334SJilles Tjoelker 
505e4b50334SJilles Tjoelker /*
506e4b50334SJilles Tjoelker  * Undo the effect of bltinlocaleset().
507e4b50334SJilles Tjoelker  */
508e4b50334SJilles Tjoelker void
509e4b50334SJilles Tjoelker bltinunsetlocale(void)
510e4b50334SJilles Tjoelker {
5118ef0ae8aSJilles Tjoelker 	int i;
512e4b50334SJilles Tjoelker 
513e4b50334SJilles Tjoelker 	INTOFF;
5148ef0ae8aSJilles Tjoelker 	if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
5158ef0ae8aSJilles Tjoelker 		if (localevar(cmdenviron->args[i])) {
516e4b50334SJilles Tjoelker 			setlocale(LC_ALL, "");
5176ed74a0aSJilles Tjoelker 			updatecharset();
5183f2da875SJilles Tjoelker 			break;
519e4b50334SJilles Tjoelker 		}
520e4b50334SJilles Tjoelker 	}
521e4b50334SJilles Tjoelker 	INTON;
522e4b50334SJilles Tjoelker }
523e4b50334SJilles Tjoelker 
5246ed74a0aSJilles Tjoelker /*
5256ed74a0aSJilles Tjoelker  * Update the localeisutf8 flag.
5266ed74a0aSJilles Tjoelker  */
5276ed74a0aSJilles Tjoelker void
5286ed74a0aSJilles Tjoelker updatecharset(void)
5296ed74a0aSJilles Tjoelker {
5306ed74a0aSJilles Tjoelker 	char *charset;
5316ed74a0aSJilles Tjoelker 
5326ed74a0aSJilles Tjoelker 	charset = nl_langinfo(CODESET);
5336ed74a0aSJilles Tjoelker 	localeisutf8 = !strcmp(charset, "UTF-8");
5346ed74a0aSJilles Tjoelker }
5354b88c807SRodney W. Grimes 
53607eb7033SJilles Tjoelker void
53707eb7033SJilles Tjoelker initcharset(void)
53807eb7033SJilles Tjoelker {
53907eb7033SJilles Tjoelker 	updatecharset();
54007eb7033SJilles Tjoelker 	initial_localeisutf8 = localeisutf8;
54107eb7033SJilles Tjoelker }
54207eb7033SJilles Tjoelker 
5434b88c807SRodney W. Grimes /*
5444b88c807SRodney W. Grimes  * Generate a list of exported variables.  This routine is used to construct
5454b88c807SRodney W. Grimes  * the third argument to execve when executing a program.
5464b88c807SRodney W. Grimes  */
5474b88c807SRodney W. Grimes 
5484b88c807SRodney W. Grimes char **
5495134c3f7SWarner Losh environment(void)
5505134c3f7SWarner Losh {
5514b88c807SRodney W. Grimes 	int nenv;
5524b88c807SRodney W. Grimes 	struct var **vpp;
5534b88c807SRodney W. Grimes 	struct var *vp;
5544b88c807SRodney W. Grimes 	char **env, **ep;
5554b88c807SRodney W. Grimes 
5564b88c807SRodney W. Grimes 	nenv = 0;
5574b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5584b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
5594b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
5604b88c807SRodney W. Grimes 				nenv++;
5614b88c807SRodney W. Grimes 	}
5624b88c807SRodney W. Grimes 	ep = env = stalloc((nenv + 1) * sizeof *env);
5634b88c807SRodney W. Grimes 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5644b88c807SRodney W. Grimes 		for (vp = *vpp ; vp ; vp = vp->next)
5654b88c807SRodney W. Grimes 			if (vp->flags & VEXPORT)
5664b88c807SRodney W. Grimes 				*ep++ = vp->text;
5674b88c807SRodney W. Grimes 	}
5684b88c807SRodney W. Grimes 	*ep = NULL;
5694b88c807SRodney W. Grimes 	return env;
5704b88c807SRodney W. Grimes }
5714b88c807SRodney W. Grimes 
5724b88c807SRodney W. Grimes 
57388328642SDavid E. O'Brien static int
574692f35fdSStefan Farfeleder var_compare(const void *a, const void *b)
575692f35fdSStefan Farfeleder {
576692f35fdSStefan Farfeleder 	const char *const *sa, *const *sb;
577692f35fdSStefan Farfeleder 
578692f35fdSStefan Farfeleder 	sa = a;
579692f35fdSStefan Farfeleder 	sb = b;
580692f35fdSStefan Farfeleder 	/*
581692f35fdSStefan Farfeleder 	 * This compares two var=value strings which creates a different
582692f35fdSStefan Farfeleder 	 * order from what you would probably expect.  POSIX is somewhat
583692f35fdSStefan Farfeleder 	 * ambiguous on what should be sorted exactly.
584692f35fdSStefan Farfeleder 	 */
585692f35fdSStefan Farfeleder 	return strcoll(*sa, *sb);
586692f35fdSStefan Farfeleder }
587692f35fdSStefan Farfeleder 
5884b88c807SRodney W. Grimes 
5894b88c807SRodney W. Grimes /*
590cff1d849SJilles Tjoelker  * Command to list all variables which are set.  This is invoked from the
591cff1d849SJilles Tjoelker  * set command when it is called without any options or operands.
5924b88c807SRodney W. Grimes  */
5934b88c807SRodney W. Grimes 
5944b88c807SRodney W. Grimes int
5955134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused)
596aa9caaf6SPeter Wemm {
5974b88c807SRodney W. Grimes 	struct var **vpp;
5984b88c807SRodney W. Grimes 	struct var *vp;
59959258844STim J. Robbins 	const char *s;
600692f35fdSStefan Farfeleder 	const char **vars;
601692f35fdSStefan Farfeleder 	int i, n;
6024b88c807SRodney W. Grimes 
603692f35fdSStefan Farfeleder 	/*
604692f35fdSStefan Farfeleder 	 * POSIX requires us to sort the variables.
605692f35fdSStefan Farfeleder 	 */
606692f35fdSStefan Farfeleder 	n = 0;
6074b88c807SRodney W. Grimes 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
6084b88c807SRodney W. Grimes 		for (vp = *vpp; vp; vp = vp->next) {
609692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
610692f35fdSStefan Farfeleder 				n++;
611692f35fdSStefan Farfeleder 		}
612692f35fdSStefan Farfeleder 	}
613692f35fdSStefan Farfeleder 
61433233ec7SJilles Tjoelker 	INTOFF;
615692f35fdSStefan Farfeleder 	vars = ckmalloc(n * sizeof(*vars));
616692f35fdSStefan Farfeleder 	i = 0;
617692f35fdSStefan Farfeleder 	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
618692f35fdSStefan Farfeleder 		for (vp = *vpp; vp; vp = vp->next) {
619692f35fdSStefan Farfeleder 			if (!(vp->flags & VUNSET))
620692f35fdSStefan Farfeleder 				vars[i++] = vp->text;
621692f35fdSStefan Farfeleder 		}
622692f35fdSStefan Farfeleder 	}
623692f35fdSStefan Farfeleder 
624692f35fdSStefan Farfeleder 	qsort(vars, n, sizeof(*vars), var_compare);
625692f35fdSStefan Farfeleder 	for (i = 0; i < n; i++) {
626f5f215e2SJilles Tjoelker 		/*
627f5f215e2SJilles Tjoelker 		 * Skip improper variable names so the output remains usable as
628f5f215e2SJilles Tjoelker 		 * shell input.
629f5f215e2SJilles Tjoelker 		 */
630f5f215e2SJilles Tjoelker 		if (!isassignment(vars[i]))
631f5f215e2SJilles Tjoelker 			continue;
632aeb5d065SJilles Tjoelker 		s = strchr(vars[i], '=');
633aeb5d065SJilles Tjoelker 		s++;
634aeb5d065SJilles Tjoelker 		outbin(vars[i], s - vars[i], out1);
635aeb5d065SJilles Tjoelker 		out1qstr(s);
63659258844STim J. Robbins 		out1c('\n');
6374b88c807SRodney W. Grimes 	}
638692f35fdSStefan Farfeleder 	ckfree(vars);
63933233ec7SJilles Tjoelker 	INTON;
640692f35fdSStefan Farfeleder 
6414b88c807SRodney W. Grimes 	return 0;
6424b88c807SRodney W. Grimes }
6434b88c807SRodney W. Grimes 
6444b88c807SRodney W. Grimes 
6454b88c807SRodney W. Grimes 
6464b88c807SRodney W. Grimes /*
6474b88c807SRodney W. Grimes  * The export and readonly commands.
6484b88c807SRodney W. Grimes  */
6494b88c807SRodney W. Grimes 
6504b88c807SRodney W. Grimes int
6517cbda738SJilles Tjoelker exportcmd(int argc __unused, char **argv)
652aa9caaf6SPeter Wemm {
6534b88c807SRodney W. Grimes 	struct var **vpp;
6544b88c807SRodney W. Grimes 	struct var *vp;
6557cbda738SJilles Tjoelker 	char **ap;
6564b88c807SRodney W. Grimes 	char *name;
6574b88c807SRodney W. Grimes 	char *p;
65845086f8cSTim J. Robbins 	char *cmdname;
65945086f8cSTim J. Robbins 	int ch, values;
6604b88c807SRodney W. Grimes 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
6614b88c807SRodney W. Grimes 
66245086f8cSTim J. Robbins 	cmdname = argv[0];
66345086f8cSTim J. Robbins 	values = 0;
6647cbda738SJilles Tjoelker 	while ((ch = nextopt("p")) != '\0') {
66545086f8cSTim J. Robbins 		switch (ch) {
66645086f8cSTim J. Robbins 		case 'p':
66745086f8cSTim J. Robbins 			values = 1;
66845086f8cSTim J. Robbins 			break;
66945086f8cSTim J. Robbins 		}
67045086f8cSTim J. Robbins 	}
67145086f8cSTim J. Robbins 
6727cbda738SJilles Tjoelker 	if (values && *argptr != NULL)
6733fda45ecSStefan Farfeleder 		error("-p requires no arguments");
6747cbda738SJilles Tjoelker 	if (*argptr != NULL) {
6757cbda738SJilles Tjoelker 		for (ap = argptr; (name = *ap) != NULL; ap++) {
6764b88c807SRodney W. Grimes 			if ((p = strchr(name, '=')) != NULL) {
6774b88c807SRodney W. Grimes 				p++;
6784b88c807SRodney W. Grimes 			} else {
6793a99ed46SJilles Tjoelker 				vp = find_var(name, NULL, NULL);
6803a99ed46SJilles Tjoelker 				if (vp != NULL) {
6814b88c807SRodney W. Grimes 					vp->flags |= flag;
682ba726b8aSAndrey A. Chernov 					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
6835b78c6c2SSean Farley 						change_env(vp->text, 1);
684ba726b8aSAndrey A. Chernov 						(void) setlocale(LC_ALL, "");
6856ed74a0aSJilles Tjoelker 						updatecharset();
686ba726b8aSAndrey A. Chernov 					}
6873a99ed46SJilles Tjoelker 					continue;
6884b88c807SRodney W. Grimes 				}
6894b88c807SRodney W. Grimes 			}
6904b88c807SRodney W. Grimes 			setvar(name, p, flag);
6914b88c807SRodney W. Grimes 		}
6924b88c807SRodney W. Grimes 	} else {
6934b88c807SRodney W. Grimes 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6944b88c807SRodney W. Grimes 			for (vp = *vpp ; vp ; vp = vp->next) {
6954b88c807SRodney W. Grimes 				if (vp->flags & flag) {
69645086f8cSTim J. Robbins 					if (values) {
697f5f215e2SJilles Tjoelker 						/*
698f5f215e2SJilles Tjoelker 						 * Skip improper variable names
699f5f215e2SJilles Tjoelker 						 * so the output remains usable
700f5f215e2SJilles Tjoelker 						 * as shell input.
701f5f215e2SJilles Tjoelker 						 */
702f5f215e2SJilles Tjoelker 						if (!isassignment(vp->text))
703f5f215e2SJilles Tjoelker 							continue;
70445086f8cSTim J. Robbins 						out1str(cmdname);
70545086f8cSTim J. Robbins 						out1c(' ');
70645086f8cSTim J. Robbins 					}
70745086f8cSTim J. Robbins 					if (values && !(vp->flags & VUNSET)) {
708258ef734SJilles Tjoelker 						outbin(vp->text,
709258ef734SJilles Tjoelker 						    vp->name_len + 1, out1);
710258ef734SJilles Tjoelker 						out1qstr(vp->text +
711258ef734SJilles Tjoelker 						    vp->name_len + 1);
712aeb5d065SJilles Tjoelker 					} else
713258ef734SJilles Tjoelker 						outbin(vp->text, vp->name_len,
714aeb5d065SJilles Tjoelker 						    out1);
7154b88c807SRodney W. Grimes 					out1c('\n');
7164b88c807SRodney W. Grimes 				}
7174b88c807SRodney W. Grimes 			}
7184b88c807SRodney W. Grimes 		}
7194b88c807SRodney W. Grimes 	}
7204b88c807SRodney W. Grimes 	return 0;
7214b88c807SRodney W. Grimes }
7224b88c807SRodney W. Grimes 
7234b88c807SRodney W. Grimes 
7244b88c807SRodney W. Grimes /*
7254b88c807SRodney W. Grimes  * The "local" command.
7264b88c807SRodney W. Grimes  */
7274b88c807SRodney W. Grimes 
728aa9caaf6SPeter Wemm int
7295134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused)
730aa9caaf6SPeter Wemm {
7314b88c807SRodney W. Grimes 	char *name;
7324b88c807SRodney W. Grimes 
733056fd329SJilles Tjoelker 	nextopt("");
7344b88c807SRodney W. Grimes 	if (! in_function())
7354b88c807SRodney W. Grimes 		error("Not in a function");
7364b88c807SRodney W. Grimes 	while ((name = *argptr++) != NULL) {
7374b88c807SRodney W. Grimes 		mklocal(name);
7384b88c807SRodney W. Grimes 	}
7394b88c807SRodney W. Grimes 	return 0;
7404b88c807SRodney W. Grimes }
7414b88c807SRodney W. Grimes 
7424b88c807SRodney W. Grimes 
7434b88c807SRodney W. Grimes /*
7444b88c807SRodney W. Grimes  * Make a variable a local variable.  When a variable is made local, it's
7454b88c807SRodney W. Grimes  * value and flags are saved in a localvar structure.  The saved values
7464b88c807SRodney W. Grimes  * will be restored when the shell function returns.  We handle the name
7474b88c807SRodney W. Grimes  * "-" as a special case.
7484b88c807SRodney W. Grimes  */
7494b88c807SRodney W. Grimes 
7504b88c807SRodney W. Grimes void
7515134c3f7SWarner Losh mklocal(char *name)
7524b88c807SRodney W. Grimes {
7534b88c807SRodney W. Grimes 	struct localvar *lvp;
7544b88c807SRodney W. Grimes 	struct var **vpp;
7554b88c807SRodney W. Grimes 	struct var *vp;
7564b88c807SRodney W. Grimes 
7574b88c807SRodney W. Grimes 	INTOFF;
7584b88c807SRodney W. Grimes 	lvp = ckmalloc(sizeof (struct localvar));
7594b88c807SRodney W. Grimes 	if (name[0] == '-' && name[1] == '\0') {
7603da40d4aSJilles Tjoelker 		lvp->text = ckmalloc(sizeof optval);
7613da40d4aSJilles Tjoelker 		memcpy(lvp->text, optval, sizeof optval);
7624b88c807SRodney W. Grimes 		vp = NULL;
7634b88c807SRodney W. Grimes 	} else {
7643a99ed46SJilles Tjoelker 		vp = find_var(name, &vpp, NULL);
7654b88c807SRodney W. Grimes 		if (vp == NULL) {
7664b88c807SRodney W. Grimes 			if (strchr(name, '='))
767c543e1aeSJilles Tjoelker 				setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
7684b88c807SRodney W. Grimes 			else
769c543e1aeSJilles Tjoelker 				setvar(name, NULL, VSTRFIXED | VNOLOCAL);
7704b88c807SRodney W. Grimes 			vp = *vpp;	/* the new variable */
7714b88c807SRodney W. Grimes 			lvp->text = NULL;
7724b88c807SRodney W. Grimes 			lvp->flags = VUNSET;
7734b88c807SRodney W. Grimes 		} else {
7744b88c807SRodney W. Grimes 			lvp->text = vp->text;
7754b88c807SRodney W. Grimes 			lvp->flags = vp->flags;
7764b88c807SRodney W. Grimes 			vp->flags |= VSTRFIXED|VTEXTFIXED;
7773a99ed46SJilles Tjoelker 			if (name[vp->name_len] == '=')
778c543e1aeSJilles Tjoelker 				setvareq(savestr(name), VNOLOCAL);
7794b88c807SRodney W. Grimes 		}
7804b88c807SRodney W. Grimes 	}
7814b88c807SRodney W. Grimes 	lvp->vp = vp;
7824b88c807SRodney W. Grimes 	lvp->next = localvars;
7834b88c807SRodney W. Grimes 	localvars = lvp;
7844b88c807SRodney W. Grimes 	INTON;
7854b88c807SRodney W. Grimes }
7864b88c807SRodney W. Grimes 
7874b88c807SRodney W. Grimes 
7884b88c807SRodney W. Grimes /*
7894b88c807SRodney W. Grimes  * Called after a function returns.
7904b88c807SRodney W. Grimes  */
7914b88c807SRodney W. Grimes 
7924b88c807SRodney W. Grimes void
7935134c3f7SWarner Losh poplocalvars(void)
7945134c3f7SWarner Losh {
7954b88c807SRodney W. Grimes 	struct localvar *lvp;
7964b88c807SRodney W. Grimes 	struct var *vp;
797cf45f124SJilles Tjoelker 	int islocalevar;
7984b88c807SRodney W. Grimes 
7991632bf1aSJilles Tjoelker 	INTOFF;
8004b88c807SRodney W. Grimes 	while ((lvp = localvars) != NULL) {
8014b88c807SRodney W. Grimes 		localvars = lvp->next;
8024b88c807SRodney W. Grimes 		vp = lvp->vp;
8034b88c807SRodney W. Grimes 		if (vp == NULL) {	/* $- saved */
8043da40d4aSJilles Tjoelker 			memcpy(optval, lvp->text, sizeof optval);
8054b88c807SRodney W. Grimes 			ckfree(lvp->text);
8064a7b1013SJilles Tjoelker 			optschanged();
8074b88c807SRodney W. Grimes 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
808a9bdd616SJilles Tjoelker 			vp->flags &= ~VREADONLY;
8094b88c807SRodney W. Grimes 			(void)unsetvar(vp->text);
8104b88c807SRodney W. Grimes 		} else {
811cf45f124SJilles Tjoelker 			islocalevar = (vp->flags | lvp->flags) & VEXPORT &&
812cf45f124SJilles Tjoelker 			    localevar(lvp->text);
8134b88c807SRodney W. Grimes 			if ((vp->flags & VTEXTFIXED) == 0)
8144b88c807SRodney W. Grimes 				ckfree(vp->text);
8154b88c807SRodney W. Grimes 			vp->flags = lvp->flags;
8164b88c807SRodney W. Grimes 			vp->text = lvp->text;
817cf45f124SJilles Tjoelker 			if (vp->func)
818cf45f124SJilles Tjoelker 				(*vp->func)(vp->text + vp->name_len + 1);
819cf45f124SJilles Tjoelker 			if (islocalevar) {
820cf45f124SJilles Tjoelker 				change_env(vp->text, vp->flags & VEXPORT &&
821cf45f124SJilles Tjoelker 				    (vp->flags & VUNSET) == 0);
822cf45f124SJilles Tjoelker 				setlocale(LC_ALL, "");
823cf45f124SJilles Tjoelker 				updatecharset();
824cf45f124SJilles Tjoelker 			}
8254b88c807SRodney W. Grimes 		}
8264b88c807SRodney W. Grimes 		ckfree(lvp);
8274b88c807SRodney W. Grimes 	}
8281632bf1aSJilles Tjoelker 	INTON;
8294b88c807SRodney W. Grimes }
8304b88c807SRodney W. Grimes 
8314b88c807SRodney W. Grimes 
832aa9caaf6SPeter Wemm int
8335134c3f7SWarner Losh setvarcmd(int argc, char **argv)
834aa9caaf6SPeter Wemm {
8354b88c807SRodney W. Grimes 	if (argc <= 2)
8364b88c807SRodney W. Grimes 		return unsetcmd(argc, argv);
8374b88c807SRodney W. Grimes 	else if (argc == 3)
8384b88c807SRodney W. Grimes 		setvar(argv[1], argv[2], 0);
8394b88c807SRodney W. Grimes 	else
840274110dfSJilles Tjoelker 		error("too many arguments");
8414b88c807SRodney W. Grimes 	return 0;
8424b88c807SRodney W. Grimes }
8434b88c807SRodney W. Grimes 
8444b88c807SRodney W. Grimes 
8454b88c807SRodney W. Grimes /*
846cff1d849SJilles Tjoelker  * The unset builtin command.
8474b88c807SRodney W. Grimes  */
8484b88c807SRodney W. Grimes 
849aa9caaf6SPeter Wemm int
8505134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused)
851aa9caaf6SPeter Wemm {
8524b88c807SRodney W. Grimes 	char **ap;
8534b88c807SRodney W. Grimes 	int i;
8544b88c807SRodney W. Grimes 	int flg_func = 0;
8554b88c807SRodney W. Grimes 	int flg_var = 0;
8564b88c807SRodney W. Grimes 	int ret = 0;
8574b88c807SRodney W. Grimes 
8584b88c807SRodney W. Grimes 	while ((i = nextopt("vf")) != '\0') {
8594b88c807SRodney W. Grimes 		if (i == 'f')
8604b88c807SRodney W. Grimes 			flg_func = 1;
8614b88c807SRodney W. Grimes 		else
8624b88c807SRodney W. Grimes 			flg_var = 1;
8634b88c807SRodney W. Grimes 	}
8644b88c807SRodney W. Grimes 	if (flg_func == 0 && flg_var == 0)
8654b88c807SRodney W. Grimes 		flg_var = 1;
8664b88c807SRodney W. Grimes 
8671632bf1aSJilles Tjoelker 	INTOFF;
8684b88c807SRodney W. Grimes 	for (ap = argptr; *ap ; ap++) {
8694b88c807SRodney W. Grimes 		if (flg_func)
8704b88c807SRodney W. Grimes 			ret |= unsetfunc(*ap);
8714b88c807SRodney W. Grimes 		if (flg_var)
8724b88c807SRodney W. Grimes 			ret |= unsetvar(*ap);
8734b88c807SRodney W. Grimes 	}
8741632bf1aSJilles Tjoelker 	INTON;
8754b88c807SRodney W. Grimes 	return ret;
8764b88c807SRodney W. Grimes }
8774b88c807SRodney W. Grimes 
8784b88c807SRodney W. Grimes 
8794b88c807SRodney W. Grimes /*
8804b88c807SRodney W. Grimes  * Unset the specified variable.
8811632bf1aSJilles Tjoelker  * Called with interrupts off.
8824b88c807SRodney W. Grimes  */
8834b88c807SRodney W. Grimes 
884ab0a2172SSteve Price int
8852cac6e36SJilles Tjoelker unsetvar(const char *s)
8864b88c807SRodney W. Grimes {
8874b88c807SRodney W. Grimes 	struct var **vpp;
8884b88c807SRodney W. Grimes 	struct var *vp;
8894b88c807SRodney W. Grimes 
8903a99ed46SJilles Tjoelker 	vp = find_var(s, &vpp, NULL);
8913a99ed46SJilles Tjoelker 	if (vp == NULL)
8923a99ed46SJilles Tjoelker 		return (0);
8934b88c807SRodney W. Grimes 	if (vp->flags & VREADONLY)
8944b88c807SRodney W. Grimes 		return (1);
8953a99ed46SJilles Tjoelker 	if (vp->text[vp->name_len + 1] != '\0')
896781bfb5aSJilles Tjoelker 		setvar(s, "", 0);
897ba726b8aSAndrey A. Chernov 	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
8985b78c6c2SSean Farley 		change_env(s, 0);
899ba726b8aSAndrey A. Chernov 		setlocale(LC_ALL, "");
9006ed74a0aSJilles Tjoelker 		updatecharset();
901ba726b8aSAndrey A. Chernov 	}
9024b88c807SRodney W. Grimes 	vp->flags &= ~VEXPORT;
9034b88c807SRodney W. Grimes 	vp->flags |= VUNSET;
9044b88c807SRodney W. Grimes 	if ((vp->flags & VSTRFIXED) == 0) {
9054b88c807SRodney W. Grimes 		if ((vp->flags & VTEXTFIXED) == 0)
9064b88c807SRodney W. Grimes 			ckfree(vp->text);
9074b88c807SRodney W. Grimes 		*vpp = vp->next;
9084b88c807SRodney W. Grimes 		ckfree(vp);
9094b88c807SRodney W. Grimes 	}
9104b88c807SRodney W. Grimes 	return (0);
9114b88c807SRodney W. Grimes }
9124b88c807SRodney W. Grimes 
9134b88c807SRodney W. Grimes 
9144b88c807SRodney W. Grimes 
9154b88c807SRodney W. Grimes /*
9166c7d8328SEitan Adler  * Returns true if the two strings specify the same variable.  The first
9174b88c807SRodney W. Grimes  * variable name is terminated by '='; the second may be terminated by
9184b88c807SRodney W. Grimes  * either '=' or '\0'.
9194b88c807SRodney W. Grimes  */
9204b88c807SRodney W. Grimes 
92188328642SDavid E. O'Brien static int
9222cac6e36SJilles Tjoelker varequal(const char *p, const char *q)
9234b88c807SRodney W. Grimes {
9244b88c807SRodney W. Grimes 	while (*p == *q++) {
9254b88c807SRodney W. Grimes 		if (*p++ == '=')
9264b88c807SRodney W. Grimes 			return 1;
9274b88c807SRodney W. Grimes 	}
9284b88c807SRodney W. Grimes 	if (*p == '=' && *(q - 1) == '\0')
9294b88c807SRodney W. Grimes 		return 1;
9304b88c807SRodney W. Grimes 	return 0;
9314b88c807SRodney W. Grimes }
9323a99ed46SJilles Tjoelker 
9333a99ed46SJilles Tjoelker /*
9343a99ed46SJilles Tjoelker  * Search for a variable.
9353a99ed46SJilles Tjoelker  * 'name' may be terminated by '=' or a NUL.
9363a99ed46SJilles Tjoelker  * vppp is set to the pointer to vp, or the list head if vp isn't found
9376c7d8328SEitan Adler  * lenp is set to the number of characters in 'name'
9383a99ed46SJilles Tjoelker  */
9393a99ed46SJilles Tjoelker 
9403a99ed46SJilles Tjoelker static struct var *
9413a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp)
9423a99ed46SJilles Tjoelker {
9433a99ed46SJilles Tjoelker 	unsigned int hashval;
9443a99ed46SJilles Tjoelker 	int len;
9453a99ed46SJilles Tjoelker 	struct var *vp, **vpp;
9463a99ed46SJilles Tjoelker 	const char *p = name;
9473a99ed46SJilles Tjoelker 
9483a99ed46SJilles Tjoelker 	hashval = 0;
9493a99ed46SJilles Tjoelker 	while (*p && *p != '=')
9503a99ed46SJilles Tjoelker 		hashval = 2 * hashval + (unsigned char)*p++;
9513a99ed46SJilles Tjoelker 	len = p - name;
9523a99ed46SJilles Tjoelker 
9533a99ed46SJilles Tjoelker 	if (lenp)
9543a99ed46SJilles Tjoelker 		*lenp = len;
9553a99ed46SJilles Tjoelker 	vpp = &vartab[hashval % VTABSIZE];
9563a99ed46SJilles Tjoelker 	if (vppp)
9573a99ed46SJilles Tjoelker 		*vppp = vpp;
9583a99ed46SJilles Tjoelker 
9593a99ed46SJilles Tjoelker 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
9603a99ed46SJilles Tjoelker 		if (vp->name_len != len)
9613a99ed46SJilles Tjoelker 			continue;
9623a99ed46SJilles Tjoelker 		if (memcmp(vp->text, name, len) != 0)
9633a99ed46SJilles Tjoelker 			continue;
9643a99ed46SJilles Tjoelker 		if (vppp)
9653a99ed46SJilles Tjoelker 			*vppp = vpp;
9663a99ed46SJilles Tjoelker 		return vp;
9673a99ed46SJilles Tjoelker 	}
9683a99ed46SJilles Tjoelker 	return NULL;
9693a99ed46SJilles Tjoelker }
970