var.c revision 263847
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/var.c 263847 2014-03-27 22:57:23Z jilles $");
401556Srgrimes
4117987Speter#include <unistd.h>
4217987Speter#include <stdlib.h>
43114763Sobrien#include <paths.h>
4417987Speter
451556Srgrimes/*
461556Srgrimes * Shell variables.
471556Srgrimes */
481556Srgrimes
4917525Sache#include <locale.h>
50221559Sjilles#include <langinfo.h>
5117525Sache
521556Srgrimes#include "shell.h"
531556Srgrimes#include "output.h"
541556Srgrimes#include "expand.h"
551556Srgrimes#include "nodes.h"	/* for other headers */
561556Srgrimes#include "eval.h"	/* defines cmdenviron */
571556Srgrimes#include "exec.h"
581556Srgrimes#include "syntax.h"
591556Srgrimes#include "options.h"
601556Srgrimes#include "mail.h"
611556Srgrimes#include "var.h"
621556Srgrimes#include "memalloc.h"
631556Srgrimes#include "error.h"
641556Srgrimes#include "mystring.h"
6520425Ssteve#include "parser.h"
66223060Sjilles#include "builtins.h"
6717987Speter#ifndef NO_HISTORY
6817987Speter#include "myhistedit.h"
6917987Speter#endif
701556Srgrimes
711556Srgrimes
721556Srgrimes#define VTABSIZE 39
731556Srgrimes
741556Srgrimes
751556Srgrimesstruct varinit {
761556Srgrimes	struct var *var;
771556Srgrimes	int flags;
78201056Sjilles	const char *text;
7990111Simp	void (*func)(const char *);
801556Srgrimes};
811556Srgrimes
821556Srgrimes
8317987Speter#ifndef NO_HISTORY
841556Srgrimesstruct var vhistsize;
85208755Sjillesstruct var vterm;
8617987Speter#endif
871556Srgrimesstruct var vifs;
881556Srgrimesstruct var vmail;
891556Srgrimesstruct var vmpath;
901556Srgrimesstruct var vpath;
911556Srgrimesstruct var vps1;
921556Srgrimesstruct var vps2;
93159632Sstefanfstruct var vps4;
94213760Sobrienstatic struct var voptind;
95230998Sjillesstruct var vdisvfork;
961556Srgrimes
97223024Sjillesint forcelocal;
98223024Sjilles
99213760Sobrienstatic const struct varinit varinit[] = {
10017987Speter#ifndef NO_HISTORY
101201056Sjilles	{ &vhistsize,	VUNSET,				"HISTSIZE=",
10220425Ssteve	  sethistsize },
10317987Speter#endif
104201056Sjilles	{ &vifs,	0,				"IFS= \t\n",
10520425Ssteve	  NULL },
106201056Sjilles	{ &vmail,	VUNSET,				"MAIL=",
10720425Ssteve	  NULL },
108201056Sjilles	{ &vmpath,	VUNSET,				"MAILPATH=",
10920425Ssteve	  NULL },
110201056Sjilles	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
11120425Ssteve	  changepath },
1128855Srgrimes	/*
1131556Srgrimes	 * vps1 depends on uid
1141556Srgrimes	 */
115201056Sjilles	{ &vps2,	0,				"PS2=> ",
11620425Ssteve	  NULL },
117201056Sjilles	{ &vps4,	0,				"PS4=+ ",
118159632Sstefanf	  NULL },
119208755Sjilles#ifndef NO_HISTORY
120208755Sjilles	{ &vterm,	VUNSET,				"TERM=",
121208755Sjilles	  setterm },
122208755Sjilles#endif
123201056Sjilles	{ &voptind,	0,				"OPTIND=1",
12420425Ssteve	  getoptsreset },
125230998Sjilles	{ &vdisvfork,	VUNSET,				"SH_DISABLE_VFORK=",
126230998Sjilles	  NULL },
12720425Ssteve	{ NULL,	0,				NULL,
12820425Ssteve	  NULL }
1291556Srgrimes};
1301556Srgrimes
131213760Sobrienstatic struct var *vartab[VTABSIZE];
1321556Srgrimes
133213760Sobrienstatic const char *const locale_names[7] = {
134207678Sjilles	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
135207678Sjilles	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
136207678Sjilles};
137213760Sobrienstatic const int locale_categories[7] = {
138207678Sjilles	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
139207678Sjilles};
140207678Sjilles
141213811Sobrienstatic int varequal(const char *, const char *);
142221668Sjillesstatic struct var *find_var(const char *, struct var ***, int *);
143213811Sobrienstatic int localevar(const char *);
1441556Srgrimes
145245689Sjillesextern char **environ;
1461556Srgrimes
1471556Srgrimes/*
148245689Sjilles * This routine initializes the builtin variables and imports the environment.
149245689Sjilles * It is called when the shell is initialized.
1501556Srgrimes */
1511556Srgrimes
1521556Srgrimesvoid
15390111Simpinitvar(void)
15490111Simp{
15597689Stjr	char ppid[20];
1561556Srgrimes	const struct varinit *ip;
1571556Srgrimes	struct var *vp;
1581556Srgrimes	struct var **vpp;
159245689Sjilles	char **envp;
1601556Srgrimes
1611556Srgrimes	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
162221668Sjilles		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
163221668Sjilles			continue;
164221668Sjilles		vp->next = *vpp;
165221668Sjilles		*vpp = vp;
166221668Sjilles		vp->text = __DECONST(char *, ip->text);
167221668Sjilles		vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
168221668Sjilles		vp->func = ip->func;
1691556Srgrimes	}
1701556Srgrimes	/*
1711556Srgrimes	 * PS1 depends on uid
1721556Srgrimes	 */
173221668Sjilles	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
1741556Srgrimes		vps1.next = *vpp;
1751556Srgrimes		*vpp = &vps1;
176201056Sjilles		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
1771556Srgrimes		vps1.flags = VSTRFIXED|VTEXTFIXED;
1781556Srgrimes	}
179259874Sjilles	fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
180259874Sjilles	setvarsafe("PPID", ppid, 0);
181245689Sjilles	for (envp = environ ; *envp ; envp++) {
182245689Sjilles		if (strchr(*envp, '=')) {
183245689Sjilles			setvareq(*envp, VEXPORT|VTEXTFIXED);
184245689Sjilles		}
185245689Sjilles	}
186259846Sjilles	setvareq("OPTIND=1", VTEXTFIXED);
1871556Srgrimes}
1881556Srgrimes
1891556Srgrimes/*
19020425Ssteve * Safe version of setvar, returns 1 on success 0 on failure.
19120425Ssteve */
19220425Ssteve
19320425Ssteveint
194200956Sjillessetvarsafe(const char *name, const char *val, int flags)
19520425Ssteve{
19620425Ssteve	struct jmploc jmploc;
197194765Sjilles	struct jmploc *const savehandler = handler;
19820425Ssteve	int err = 0;
199199660Sjilles	int inton;
20020425Ssteve
201199660Sjilles	inton = is_int_on();
20220425Ssteve	if (setjmp(jmploc.loc))
20320425Ssteve		err = 1;
20420425Ssteve	else {
20520425Ssteve		handler = &jmploc;
20620425Ssteve		setvar(name, val, flags);
20720425Ssteve	}
20820425Ssteve	handler = savehandler;
209199660Sjilles	SETINTON(inton);
21020425Ssteve	return err;
21120425Ssteve}
21220425Ssteve
21320425Ssteve/*
214155302Sschweikh * Set the value of a variable.  The flags argument is stored with the
2151556Srgrimes * flags of the variable.  If val is NULL, the variable is unset.
2161556Srgrimes */
2171556Srgrimes
2181556Srgrimesvoid
219200956Sjillessetvar(const char *name, const char *val, int flags)
22017987Speter{
221200956Sjilles	const char *p;
222258776Sjilles	size_t len;
223258776Sjilles	size_t namelen;
224258776Sjilles	size_t vallen;
2251556Srgrimes	char *nameeq;
2261556Srgrimes	int isbad;
2271556Srgrimes
2281556Srgrimes	isbad = 0;
2291556Srgrimes	p = name;
23017525Sache	if (! is_name(*p))
2311556Srgrimes		isbad = 1;
23217525Sache	p++;
2331556Srgrimes	for (;;) {
2341556Srgrimes		if (! is_in_name(*p)) {
2351556Srgrimes			if (*p == '\0' || *p == '=')
2361556Srgrimes				break;
2371556Srgrimes			isbad = 1;
2381556Srgrimes		}
2391556Srgrimes		p++;
2401556Srgrimes	}
2411556Srgrimes	namelen = p - name;
2421556Srgrimes	if (isbad)
243258776Sjilles		error("%.*s: bad variable name", (int)namelen, name);
2441556Srgrimes	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2451556Srgrimes	if (val == NULL) {
2461556Srgrimes		flags |= VUNSET;
247258776Sjilles		vallen = 0;
2481556Srgrimes	} else {
249258776Sjilles		vallen = strlen(val);
250258776Sjilles		len += vallen;
2511556Srgrimes	}
252263777Sjilles	INTOFF;
253200956Sjilles	nameeq = ckmalloc(len);
254200956Sjilles	memcpy(nameeq, name, namelen);
255200956Sjilles	nameeq[namelen] = '=';
2561556Srgrimes	if (val)
257258776Sjilles		memcpy(nameeq + namelen + 1, val, vallen + 1);
258200956Sjilles	else
259200956Sjilles		nameeq[namelen + 1] = '\0';
2601556Srgrimes	setvareq(nameeq, flags);
261263777Sjilles	INTON;
2621556Srgrimes}
2631556Srgrimes
264213811Sobrienstatic int
265200956Sjilleslocalevar(const char *s)
26690111Simp{
267207678Sjilles	const char *const *ss;
2681556Srgrimes
26917525Sache	if (*s != 'L')
27017525Sache		return 0;
27117525Sache	if (varequal(s + 1, "ANG"))
27217525Sache		return 1;
27317525Sache	if (strncmp(s + 1, "C_", 2) != 0)
27417525Sache		return 0;
275207678Sjilles	if (varequal(s + 3, "ALL"))
276207678Sjilles		return 1;
277207678Sjilles	for (ss = locale_names; *ss ; ss++)
278207678Sjilles		if (varequal(s + 3, *ss + 3))
27917525Sache			return 1;
28017525Sache	return 0;
28117525Sache}
2821556Srgrimes
283171268Sscf
2841556Srgrimes/*
285171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a
286171268Sscf * pointer into environ where the string should not be manipulated.
287171268Sscf */
288213811Sobrienstatic void
289200956Sjilleschange_env(const char *s, int set)
290171268Sscf{
291171268Sscf	char *eqp;
292171268Sscf	char *ss;
293171268Sscf
294263777Sjilles	INTOFF;
295171268Sscf	ss = savestr(s);
296171268Sscf	if ((eqp = strchr(ss, '=')) != NULL)
297171268Sscf		*eqp = '\0';
298171268Sscf	if (set && eqp != NULL)
299171268Sscf		(void) setenv(ss, eqp + 1, 1);
300171268Sscf	else
301171268Sscf		(void) unsetenv(ss);
302171268Sscf	ckfree(ss);
303263777Sjilles	INTON;
304171268Sscf
305171268Sscf	return;
306171268Sscf}
307171268Sscf
308171268Sscf
309171268Sscf/*
3101556Srgrimes * Same as setvar except that the variable and value are passed in
3111556Srgrimes * the first argument as name=value.  Since the first argument will
3121556Srgrimes * be actually stored in the table, it should not be a string that
3131556Srgrimes * will go away.
3141556Srgrimes */
3151556Srgrimes
3161556Srgrimesvoid
31790111Simpsetvareq(char *s, int flags)
31817987Speter{
3191556Srgrimes	struct var *vp, **vpp;
320221668Sjilles	int nlen;
3211556Srgrimes
32245263Scracauer	if (aflag)
32345263Scracauer		flags |= VEXPORT;
324223024Sjilles	if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
325223024Sjilles		mklocal(s);
326221668Sjilles	vp = find_var(s, &vpp, &nlen);
327221668Sjilles	if (vp != NULL) {
328263846Sjilles		if (vp->flags & VREADONLY) {
329263846Sjilles			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
330263846Sjilles				ckfree(s);
331221668Sjilles			error("%.*s: is read only", vp->name_len, s);
332263846Sjilles		}
333263847Sjilles		if (flags & VNOSET) {
334263847Sjilles			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
335263847Sjilles				ckfree(s);
336221668Sjilles			return;
337263847Sjilles		}
338221668Sjilles		INTOFF;
33920425Ssteve
340221668Sjilles		if (vp->func && (flags & VNOFUNC) == 0)
341221668Sjilles			(*vp->func)(s + vp->name_len + 1);
34220425Ssteve
343221668Sjilles		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
344221668Sjilles			ckfree(vp->text);
34520425Ssteve
346221668Sjilles		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
347221668Sjilles		vp->flags |= flags;
348221668Sjilles		vp->text = s;
34920425Ssteve
350221668Sjilles		/*
351221668Sjilles		 * We could roll this to a function, to handle it as
352221668Sjilles		 * a regular variable function callback, but why bother?
353221668Sjilles		 *
354221668Sjilles		 * Note: this assumes iflag is not set to 1 initially.
355245689Sjilles		 * As part of initvar(), this is called before arguments
356221668Sjilles		 * are looked at.
357221668Sjilles		 */
358221668Sjilles		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
359221668Sjilles		    iflag == 1)
360221668Sjilles			chkmail(1);
361221668Sjilles		if ((vp->flags & VEXPORT) && localevar(s)) {
362221668Sjilles			change_env(s, 1);
363221668Sjilles			(void) setlocale(LC_ALL, "");
364221668Sjilles			updatecharset();
3651556Srgrimes		}
366221668Sjilles		INTON;
367221668Sjilles		return;
3681556Srgrimes	}
3691556Srgrimes	/* not found */
370263847Sjilles	if (flags & VNOSET) {
371263847Sjilles		if ((flags & (VTEXTFIXED|VSTACK)) == 0)
372263847Sjilles			ckfree(s);
373216870Sjilles		return;
374263847Sjilles	}
375263777Sjilles	INTOFF;
3761556Srgrimes	vp = ckmalloc(sizeof (*vp));
3771556Srgrimes	vp->flags = flags;
3781556Srgrimes	vp->text = s;
379221668Sjilles	vp->name_len = nlen;
3801556Srgrimes	vp->next = *vpp;
38120425Ssteve	vp->func = NULL;
3821556Srgrimes	*vpp = vp;
38317525Sache	if ((vp->flags & VEXPORT) && localevar(s)) {
384171268Sscf		change_env(s, 1);
38517525Sache		(void) setlocale(LC_ALL, "");
386221559Sjilles		updatecharset();
38717525Sache	}
38817525Sache	INTON;
3891556Srgrimes}
3901556Srgrimes
3911556Srgrimes
3921556Srgrimes
3931556Srgrimes/*
3941556Srgrimes * Process a linked list of variable assignments.
3951556Srgrimes */
3961556Srgrimes
3971556Srgrimesvoid
398216870Sjilleslistsetvar(struct strlist *list, int flags)
39990111Simp{
4001556Srgrimes	struct strlist *lp;
4011556Srgrimes
4021556Srgrimes	INTOFF;
4031556Srgrimes	for (lp = list ; lp ; lp = lp->next) {
404216870Sjilles		setvareq(savestr(lp->text), flags);
4051556Srgrimes	}
4061556Srgrimes	INTON;
4071556Srgrimes}
4081556Srgrimes
4091556Srgrimes
4101556Srgrimes
4111556Srgrimes/*
4121556Srgrimes * Find the value of a variable.  Returns NULL if not set.
4131556Srgrimes */
4141556Srgrimes
4151556Srgrimeschar *
416200956Sjilleslookupvar(const char *name)
41790111Simp{
4181556Srgrimes	struct var *v;
4191556Srgrimes
420221668Sjilles	v = find_var(name, NULL, NULL);
421221668Sjilles	if (v == NULL || v->flags & VUNSET)
422221668Sjilles		return NULL;
423221668Sjilles	return v->text + v->name_len + 1;
4241556Srgrimes}
4251556Srgrimes
4261556Srgrimes
4271556Srgrimes
4281556Srgrimes/*
4291556Srgrimes * Search the environment of a builtin command.  If the second argument
4301556Srgrimes * is nonzero, return the value of a variable even if it hasn't been
4311556Srgrimes * exported.
4321556Srgrimes */
4331556Srgrimes
4341556Srgrimeschar *
435200956Sjillesbltinlookup(const char *name, int doall)
43617987Speter{
4371556Srgrimes	struct strlist *sp;
4381556Srgrimes	struct var *v;
439212467Sjilles	char *result;
4401556Srgrimes
441212467Sjilles	result = NULL;
4421556Srgrimes	for (sp = cmdenviron ; sp ; sp = sp->next) {
4431556Srgrimes		if (varequal(sp->text, name))
444212467Sjilles			result = strchr(sp->text, '=') + 1;
4451556Srgrimes	}
446212467Sjilles	if (result != NULL)
447212467Sjilles		return result;
448221668Sjilles
449221668Sjilles	v = find_var(name, NULL, NULL);
450221668Sjilles	if (v == NULL || v->flags & VUNSET ||
451221668Sjilles	    (!doall && (v->flags & VEXPORT) == 0))
452221668Sjilles		return NULL;
453221668Sjilles	return v->text + v->name_len + 1;
4541556Srgrimes}
4551556Srgrimes
4561556Srgrimes
457207678Sjilles/*
458207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments).
459207678Sjilles */
460207678Sjillesvoid
461207678Sjillesbltinsetlocale(void)
462207678Sjilles{
463207678Sjilles	struct strlist *lp;
464207678Sjilles	int act = 0;
465207678Sjilles	char *loc, *locdef;
466207678Sjilles	int i;
4671556Srgrimes
468207678Sjilles	for (lp = cmdenviron ; lp ; lp = lp->next) {
469207678Sjilles		if (localevar(lp->text)) {
470207678Sjilles			act = 1;
471207678Sjilles			break;
472207678Sjilles		}
473207678Sjilles	}
474207678Sjilles	if (!act)
475207678Sjilles		return;
476207678Sjilles	loc = bltinlookup("LC_ALL", 0);
477207678Sjilles	INTOFF;
478207678Sjilles	if (loc != NULL) {
479207678Sjilles		setlocale(LC_ALL, loc);
480207678Sjilles		INTON;
481221559Sjilles		updatecharset();
482207678Sjilles		return;
483207678Sjilles	}
484207678Sjilles	locdef = bltinlookup("LANG", 0);
485207678Sjilles	for (i = 0; locale_names[i] != NULL; i++) {
486207678Sjilles		loc = bltinlookup(locale_names[i], 0);
487207678Sjilles		if (loc == NULL)
488207678Sjilles			loc = locdef;
489207678Sjilles		if (loc != NULL)
490207678Sjilles			setlocale(locale_categories[i], loc);
491207678Sjilles	}
492207678Sjilles	INTON;
493221559Sjilles	updatecharset();
494207678Sjilles}
495207678Sjilles
4961556Srgrimes/*
497207678Sjilles * Undo the effect of bltinlocaleset().
498207678Sjilles */
499207678Sjillesvoid
500207678Sjillesbltinunsetlocale(void)
501207678Sjilles{
502207678Sjilles	struct strlist *lp;
503207678Sjilles
504207678Sjilles	INTOFF;
505207678Sjilles	for (lp = cmdenviron ; lp ; lp = lp->next) {
506207678Sjilles		if (localevar(lp->text)) {
507207678Sjilles			setlocale(LC_ALL, "");
508221559Sjilles			updatecharset();
509207678Sjilles			return;
510207678Sjilles		}
511207678Sjilles	}
512207678Sjilles	INTON;
513207678Sjilles}
514207678Sjilles
515221559Sjilles/*
516221559Sjilles * Update the localeisutf8 flag.
517221559Sjilles */
518221559Sjillesvoid
519221559Sjillesupdatecharset(void)
520221559Sjilles{
521221559Sjilles	char *charset;
522207678Sjilles
523221559Sjilles	charset = nl_langinfo(CODESET);
524221559Sjilles	localeisutf8 = !strcmp(charset, "UTF-8");
525221559Sjilles}
526221559Sjilles
527221669Sjillesvoid
528221669Sjillesinitcharset(void)
529221669Sjilles{
530221669Sjilles	updatecharset();
531221669Sjilles	initial_localeisutf8 = localeisutf8;
532221669Sjilles}
533221669Sjilles
534207678Sjilles/*
5351556Srgrimes * Generate a list of exported variables.  This routine is used to construct
5361556Srgrimes * the third argument to execve when executing a program.
5371556Srgrimes */
5381556Srgrimes
5391556Srgrimeschar **
54090111Simpenvironment(void)
54190111Simp{
5421556Srgrimes	int nenv;
5431556Srgrimes	struct var **vpp;
5441556Srgrimes	struct var *vp;
5451556Srgrimes	char **env, **ep;
5461556Srgrimes
5471556Srgrimes	nenv = 0;
5481556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5491556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next)
5501556Srgrimes			if (vp->flags & VEXPORT)
5511556Srgrimes				nenv++;
5521556Srgrimes	}
5531556Srgrimes	ep = env = stalloc((nenv + 1) * sizeof *env);
5541556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5551556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next)
5561556Srgrimes			if (vp->flags & VEXPORT)
5571556Srgrimes				*ep++ = vp->text;
5581556Srgrimes	}
5591556Srgrimes	*ep = NULL;
5601556Srgrimes	return env;
5611556Srgrimes}
5621556Srgrimes
5631556Srgrimes
564213811Sobrienstatic int
565158145Sstefanfvar_compare(const void *a, const void *b)
566158145Sstefanf{
567158145Sstefanf	const char *const *sa, *const *sb;
5681556Srgrimes
569158145Sstefanf	sa = a;
570158145Sstefanf	sb = b;
571158145Sstefanf	/*
572158145Sstefanf	 * This compares two var=value strings which creates a different
573158145Sstefanf	 * order from what you would probably expect.  POSIX is somewhat
574158145Sstefanf	 * ambiguous on what should be sorted exactly.
575158145Sstefanf	 */
576158145Sstefanf	return strcoll(*sa, *sb);
577158145Sstefanf}
578158145Sstefanf
579158145Sstefanf
5801556Srgrimes/*
581217847Sjilles * Command to list all variables which are set.  This is invoked from the
582217847Sjilles * set command when it is called without any options or operands.
5831556Srgrimes */
5841556Srgrimes
5851556Srgrimesint
58690111Simpshowvarscmd(int argc __unused, char **argv __unused)
58717987Speter{
5881556Srgrimes	struct var **vpp;
5891556Srgrimes	struct var *vp;
59097915Stjr	const char *s;
591158145Sstefanf	const char **vars;
592158145Sstefanf	int i, n;
5931556Srgrimes
594158145Sstefanf	/*
595158145Sstefanf	 * POSIX requires us to sort the variables.
596158145Sstefanf	 */
597158145Sstefanf	n = 0;
598158145Sstefanf	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
599158145Sstefanf		for (vp = *vpp; vp; vp = vp->next) {
600158145Sstefanf			if (!(vp->flags & VUNSET))
601158145Sstefanf				n++;
6021556Srgrimes		}
6031556Srgrimes	}
604158145Sstefanf
605231001Sjilles	INTOFF;
606158145Sstefanf	vars = ckmalloc(n * sizeof(*vars));
607158145Sstefanf	i = 0;
608158145Sstefanf	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
609158145Sstefanf		for (vp = *vpp; vp; vp = vp->next) {
610158145Sstefanf			if (!(vp->flags & VUNSET))
611158145Sstefanf				vars[i++] = vp->text;
612158145Sstefanf		}
613158145Sstefanf	}
614158145Sstefanf
615158145Sstefanf	qsort(vars, n, sizeof(*vars), var_compare);
616158145Sstefanf	for (i = 0; i < n; i++) {
617223183Sjilles		/*
618223183Sjilles		 * Skip improper variable names so the output remains usable as
619223183Sjilles		 * shell input.
620223183Sjilles		 */
621223183Sjilles		if (!isassignment(vars[i]))
622223183Sjilles			continue;
623215567Sjilles		s = strchr(vars[i], '=');
624215567Sjilles		s++;
625215567Sjilles		outbin(vars[i], s - vars[i], out1);
626215567Sjilles		out1qstr(s);
627158145Sstefanf		out1c('\n');
628158145Sstefanf	}
629158145Sstefanf	ckfree(vars);
630231001Sjilles	INTON;
631158145Sstefanf
6321556Srgrimes	return 0;
6331556Srgrimes}
6341556Srgrimes
6351556Srgrimes
6361556Srgrimes
6371556Srgrimes/*
6381556Srgrimes * The export and readonly commands.
6391556Srgrimes */
6401556Srgrimes
6411556Srgrimesint
642240541Sjillesexportcmd(int argc __unused, char **argv)
64317987Speter{
6441556Srgrimes	struct var **vpp;
6451556Srgrimes	struct var *vp;
646240541Sjilles	char **ap;
6471556Srgrimes	char *name;
6481556Srgrimes	char *p;
64997914Stjr	char *cmdname;
65097914Stjr	int ch, values;
6511556Srgrimes	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
6521556Srgrimes
65397914Stjr	cmdname = argv[0];
65497914Stjr	values = 0;
655240541Sjilles	while ((ch = nextopt("p")) != '\0') {
65697914Stjr		switch (ch) {
65797914Stjr		case 'p':
65897914Stjr			values = 1;
65997914Stjr			break;
66097914Stjr		}
66197914Stjr	}
66297914Stjr
663240541Sjilles	if (values && *argptr != NULL)
664149919Sstefanf		error("-p requires no arguments");
665240541Sjilles	if (*argptr != NULL) {
666240541Sjilles		for (ap = argptr; (name = *ap) != NULL; ap++) {
6671556Srgrimes			if ((p = strchr(name, '=')) != NULL) {
6681556Srgrimes				p++;
6691556Srgrimes			} else {
670221668Sjilles				vp = find_var(name, NULL, NULL);
671221668Sjilles				if (vp != NULL) {
672221668Sjilles					vp->flags |= flag;
673221668Sjilles					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
674221668Sjilles						change_env(vp->text, 1);
675221668Sjilles						(void) setlocale(LC_ALL, "");
676221668Sjilles						updatecharset();
6771556Srgrimes					}
678221668Sjilles					continue;
6791556Srgrimes				}
6801556Srgrimes			}
6811556Srgrimes			setvar(name, p, flag);
6821556Srgrimes		}
6831556Srgrimes	} else {
6841556Srgrimes		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6851556Srgrimes			for (vp = *vpp ; vp ; vp = vp->next) {
6861556Srgrimes				if (vp->flags & flag) {
68797914Stjr					if (values) {
688223183Sjilles						/*
689223183Sjilles						 * Skip improper variable names
690223183Sjilles						 * so the output remains usable
691223183Sjilles						 * as shell input.
692223183Sjilles						 */
693223183Sjilles						if (!isassignment(vp->text))
694223183Sjilles							continue;
69597914Stjr						out1str(cmdname);
69697914Stjr						out1c(' ');
69797914Stjr					}
69897914Stjr					if (values && !(vp->flags & VUNSET)) {
699221975Sjilles						outbin(vp->text,
700221975Sjilles						    vp->name_len + 1, out1);
701221975Sjilles						out1qstr(vp->text +
702221975Sjilles						    vp->name_len + 1);
703215567Sjilles					} else
704221975Sjilles						outbin(vp->text, vp->name_len,
705215567Sjilles						    out1);
7061556Srgrimes					out1c('\n');
7071556Srgrimes				}
7081556Srgrimes			}
7091556Srgrimes		}
7101556Srgrimes	}
7111556Srgrimes	return 0;
7121556Srgrimes}
7131556Srgrimes
7141556Srgrimes
7151556Srgrimes/*
7161556Srgrimes * The "local" command.
7171556Srgrimes */
7181556Srgrimes
71917987Speterint
72090111Simplocalcmd(int argc __unused, char **argv __unused)
72117987Speter{
7221556Srgrimes	char *name;
7231556Srgrimes
724254339Sjilles	nextopt("");
7251556Srgrimes	if (! in_function())
7261556Srgrimes		error("Not in a function");
7271556Srgrimes	while ((name = *argptr++) != NULL) {
7281556Srgrimes		mklocal(name);
7291556Srgrimes	}
7301556Srgrimes	return 0;
7311556Srgrimes}
7321556Srgrimes
7331556Srgrimes
7341556Srgrimes/*
7351556Srgrimes * Make a variable a local variable.  When a variable is made local, it's
7361556Srgrimes * value and flags are saved in a localvar structure.  The saved values
7371556Srgrimes * will be restored when the shell function returns.  We handle the name
7381556Srgrimes * "-" as a special case.
7391556Srgrimes */
7401556Srgrimes
7411556Srgrimesvoid
74290111Simpmklocal(char *name)
74390111Simp{
7441556Srgrimes	struct localvar *lvp;
7451556Srgrimes	struct var **vpp;
7461556Srgrimes	struct var *vp;
7471556Srgrimes
7481556Srgrimes	INTOFF;
7491556Srgrimes	lvp = ckmalloc(sizeof (struct localvar));
7501556Srgrimes	if (name[0] == '-' && name[1] == '\0') {
7511556Srgrimes		lvp->text = ckmalloc(sizeof optlist);
75217987Speter		memcpy(lvp->text, optlist, sizeof optlist);
7531556Srgrimes		vp = NULL;
7541556Srgrimes	} else {
755221668Sjilles		vp = find_var(name, &vpp, NULL);
7561556Srgrimes		if (vp == NULL) {
7571556Srgrimes			if (strchr(name, '='))
758223024Sjilles				setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
7591556Srgrimes			else
760223024Sjilles				setvar(name, NULL, VSTRFIXED | VNOLOCAL);
7611556Srgrimes			vp = *vpp;	/* the new variable */
7621556Srgrimes			lvp->text = NULL;
7631556Srgrimes			lvp->flags = VUNSET;
7641556Srgrimes		} else {
7651556Srgrimes			lvp->text = vp->text;
7661556Srgrimes			lvp->flags = vp->flags;
7671556Srgrimes			vp->flags |= VSTRFIXED|VTEXTFIXED;
768221668Sjilles			if (name[vp->name_len] == '=')
769223024Sjilles				setvareq(savestr(name), VNOLOCAL);
7701556Srgrimes		}
7711556Srgrimes	}
7721556Srgrimes	lvp->vp = vp;
7731556Srgrimes	lvp->next = localvars;
7741556Srgrimes	localvars = lvp;
7751556Srgrimes	INTON;
7761556Srgrimes}
7771556Srgrimes
7781556Srgrimes
7791556Srgrimes/*
7801556Srgrimes * Called after a function returns.
7811556Srgrimes */
7821556Srgrimes
7831556Srgrimesvoid
78490111Simppoplocalvars(void)
78590111Simp{
7861556Srgrimes	struct localvar *lvp;
7871556Srgrimes	struct var *vp;
7881556Srgrimes
789263777Sjilles	INTOFF;
7901556Srgrimes	while ((lvp = localvars) != NULL) {
7911556Srgrimes		localvars = lvp->next;
7921556Srgrimes		vp = lvp->vp;
7931556Srgrimes		if (vp == NULL) {	/* $- saved */
79417987Speter			memcpy(optlist, lvp->text, sizeof optlist);
7951556Srgrimes			ckfree(lvp->text);
796215266Sjilles			optschanged();
7971556Srgrimes		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
7981556Srgrimes			(void)unsetvar(vp->text);
7991556Srgrimes		} else {
8001556Srgrimes			if ((vp->flags & VTEXTFIXED) == 0)
8011556Srgrimes				ckfree(vp->text);
8021556Srgrimes			vp->flags = lvp->flags;
8031556Srgrimes			vp->text = lvp->text;
8041556Srgrimes		}
8051556Srgrimes		ckfree(lvp);
8061556Srgrimes	}
807263777Sjilles	INTON;
8081556Srgrimes}
8091556Srgrimes
8101556Srgrimes
81117987Speterint
81290111Simpsetvarcmd(int argc, char **argv)
81317987Speter{
8141556Srgrimes	if (argc <= 2)
8151556Srgrimes		return unsetcmd(argc, argv);
8161556Srgrimes	else if (argc == 3)
8171556Srgrimes		setvar(argv[1], argv[2], 0);
8181556Srgrimes	else
819214538Sjilles		error("too many arguments");
8201556Srgrimes	return 0;
8211556Srgrimes}
8221556Srgrimes
8231556Srgrimes
8241556Srgrimes/*
825217847Sjilles * The unset builtin command.
8261556Srgrimes */
8271556Srgrimes
82817987Speterint
82990111Simpunsetcmd(int argc __unused, char **argv __unused)
83017987Speter{
8311556Srgrimes	char **ap;
8321556Srgrimes	int i;
8331556Srgrimes	int flg_func = 0;
8341556Srgrimes	int flg_var = 0;
8351556Srgrimes	int ret = 0;
8361556Srgrimes
8371556Srgrimes	while ((i = nextopt("vf")) != '\0') {
8381556Srgrimes		if (i == 'f')
8391556Srgrimes			flg_func = 1;
8401556Srgrimes		else
8411556Srgrimes			flg_var = 1;
8421556Srgrimes	}
8431556Srgrimes	if (flg_func == 0 && flg_var == 0)
8441556Srgrimes		flg_var = 1;
8458855Srgrimes
846263777Sjilles	INTOFF;
8471556Srgrimes	for (ap = argptr; *ap ; ap++) {
8481556Srgrimes		if (flg_func)
8491556Srgrimes			ret |= unsetfunc(*ap);
8501556Srgrimes		if (flg_var)
8511556Srgrimes			ret |= unsetvar(*ap);
8521556Srgrimes	}
853263777Sjilles	INTON;
8541556Srgrimes	return ret;
8551556Srgrimes}
8561556Srgrimes
8571556Srgrimes
8581556Srgrimes/*
8591556Srgrimes * Unset the specified variable.
860263777Sjilles * Called with interrupts off.
8611556Srgrimes */
8621556Srgrimes
86320425Ssteveint
864200956Sjillesunsetvar(const char *s)
86590111Simp{
8661556Srgrimes	struct var **vpp;
8671556Srgrimes	struct var *vp;
8681556Srgrimes
869221668Sjilles	vp = find_var(s, &vpp, NULL);
870221668Sjilles	if (vp == NULL)
871221668Sjilles		return (0);
872221668Sjilles	if (vp->flags & VREADONLY)
873221668Sjilles		return (1);
874221668Sjilles	if (vp->text[vp->name_len + 1] != '\0')
875221668Sjilles		setvar(s, nullstr, 0);
876221668Sjilles	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
877221668Sjilles		change_env(s, 0);
878221668Sjilles		setlocale(LC_ALL, "");
879221668Sjilles		updatecharset();
8801556Srgrimes	}
881221668Sjilles	vp->flags &= ~VEXPORT;
882221668Sjilles	vp->flags |= VUNSET;
883221668Sjilles	if ((vp->flags & VSTRFIXED) == 0) {
884221668Sjilles		if ((vp->flags & VTEXTFIXED) == 0)
885221668Sjilles			ckfree(vp->text);
886221668Sjilles		*vpp = vp->next;
887221668Sjilles		ckfree(vp);
888221668Sjilles	}
889135856Sdes	return (0);
8901556Srgrimes}
8911556Srgrimes
8921556Srgrimes
8931556Srgrimes
8941556Srgrimes/*
895250422Seadler * Returns true if the two strings specify the same variable.  The first
8961556Srgrimes * variable name is terminated by '='; the second may be terminated by
8971556Srgrimes * either '=' or '\0'.
8981556Srgrimes */
8991556Srgrimes
900213811Sobrienstatic int
901200956Sjillesvarequal(const char *p, const char *q)
90290111Simp{
9031556Srgrimes	while (*p == *q++) {
9041556Srgrimes		if (*p++ == '=')
9051556Srgrimes			return 1;
9061556Srgrimes	}
9071556Srgrimes	if (*p == '=' && *(q - 1) == '\0')
9081556Srgrimes		return 1;
9091556Srgrimes	return 0;
9101556Srgrimes}
911221668Sjilles
912221668Sjilles/*
913221668Sjilles * Search for a variable.
914221668Sjilles * 'name' may be terminated by '=' or a NUL.
915221668Sjilles * vppp is set to the pointer to vp, or the list head if vp isn't found
916250422Seadler * lenp is set to the number of characters in 'name'
917221668Sjilles */
918221668Sjilles
919221668Sjillesstatic struct var *
920221668Sjillesfind_var(const char *name, struct var ***vppp, int *lenp)
921221668Sjilles{
922221668Sjilles	unsigned int hashval;
923221668Sjilles	int len;
924221668Sjilles	struct var *vp, **vpp;
925221668Sjilles	const char *p = name;
926221668Sjilles
927221668Sjilles	hashval = 0;
928221668Sjilles	while (*p && *p != '=')
929221668Sjilles		hashval = 2 * hashval + (unsigned char)*p++;
930221668Sjilles	len = p - name;
931221668Sjilles
932221668Sjilles	if (lenp)
933221668Sjilles		*lenp = len;
934221668Sjilles	vpp = &vartab[hashval % VTABSIZE];
935221668Sjilles	if (vppp)
936221668Sjilles		*vppp = vpp;
937221668Sjilles
938221668Sjilles	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
939221668Sjilles		if (vp->name_len != len)
940221668Sjilles			continue;
941221668Sjilles		if (memcmp(vp->text, name, len) != 0)
942221668Sjilles			continue;
943221668Sjilles		if (vppp)
944221668Sjilles			*vppp = vpp;
945221668Sjilles		return vp;
946221668Sjilles	}
947221668Sjilles	return NULL;
948221668Sjilles}
949