var.c revision 264643
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: stable/9/bin/sh/var.c 264643 2014-04-18 12:34:10Z 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;
9197689Stjrstruct var vppid;
921556Srgrimesstruct var vps1;
931556Srgrimesstruct var vps2;
94159632Sstefanfstruct var vps4;
951556Srgrimesstruct var vvers;
96213760Sobrienstatic struct var voptind;
97249242Sjillesstruct var vdisvfork;
981556Srgrimes
99223024Sjillesint forcelocal;
100223024Sjilles
101213760Sobrienstatic const struct varinit varinit[] = {
10217987Speter#ifndef NO_HISTORY
103201056Sjilles	{ &vhistsize,	VUNSET,				"HISTSIZE=",
10420425Ssteve	  sethistsize },
10517987Speter#endif
106201056Sjilles	{ &vifs,	0,				"IFS= \t\n",
10720425Ssteve	  NULL },
108201056Sjilles	{ &vmail,	VUNSET,				"MAIL=",
10920425Ssteve	  NULL },
110201056Sjilles	{ &vmpath,	VUNSET,				"MAILPATH=",
11120425Ssteve	  NULL },
112201056Sjilles	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
11320425Ssteve	  changepath },
114201056Sjilles	{ &vppid,	VUNSET,				"PPID=",
11597689Stjr	  NULL },
1168855Srgrimes	/*
1171556Srgrimes	 * vps1 depends on uid
1181556Srgrimes	 */
119201056Sjilles	{ &vps2,	0,				"PS2=> ",
12020425Ssteve	  NULL },
121201056Sjilles	{ &vps4,	0,				"PS4=+ ",
122159632Sstefanf	  NULL },
123208755Sjilles#ifndef NO_HISTORY
124208755Sjilles	{ &vterm,	VUNSET,				"TERM=",
125208755Sjilles	  setterm },
126208755Sjilles#endif
127201056Sjilles	{ &voptind,	0,				"OPTIND=1",
12820425Ssteve	  getoptsreset },
129249242Sjilles	{ &vdisvfork,	VUNSET,				"SH_DISABLE_VFORK=",
130249242Sjilles	  NULL },
13120425Ssteve	{ NULL,	0,				NULL,
13220425Ssteve	  NULL }
1331556Srgrimes};
1341556Srgrimes
135213760Sobrienstatic struct var *vartab[VTABSIZE];
1361556Srgrimes
137213760Sobrienstatic const char *const locale_names[7] = {
138207678Sjilles	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
139207678Sjilles	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
140207678Sjilles};
141213760Sobrienstatic const int locale_categories[7] = {
142207678Sjilles	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
143207678Sjilles};
144207678Sjilles
145213811Sobrienstatic int varequal(const char *, const char *);
146221668Sjillesstatic struct var *find_var(const char *, struct var ***, int *);
147213811Sobrienstatic int localevar(const char *);
1481556Srgrimes
1491556Srgrimes/*
150155302Sschweikh * Initialize the variable symbol tables and import the environment.
1511556Srgrimes */
1521556Srgrimes
1531556Srgrimes#ifdef mkinit
1541556SrgrimesINCLUDE "var.h"
155201053SjillesMKINIT char **environ;
1561556SrgrimesINIT {
1571556Srgrimes	char **envp;
1581556Srgrimes
1591556Srgrimes	initvar();
1601556Srgrimes	for (envp = environ ; *envp ; envp++) {
1611556Srgrimes		if (strchr(*envp, '=')) {
1621556Srgrimes			setvareq(*envp, VEXPORT|VTEXTFIXED);
1631556Srgrimes		}
1641556Srgrimes	}
1651556Srgrimes}
1661556Srgrimes#endif
1671556Srgrimes
1681556Srgrimes
1691556Srgrimes/*
1701556Srgrimes * This routine initializes the builtin variables.  It is called when the
171218306Sjilles * shell is initialized.
1721556Srgrimes */
1731556Srgrimes
1741556Srgrimesvoid
17590111Simpinitvar(void)
17690111Simp{
17797689Stjr	char ppid[20];
1781556Srgrimes	const struct varinit *ip;
1791556Srgrimes	struct var *vp;
1801556Srgrimes	struct var **vpp;
1811556Srgrimes
1821556Srgrimes	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
183221668Sjilles		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
184221668Sjilles			continue;
185221668Sjilles		vp->next = *vpp;
186221668Sjilles		*vpp = vp;
187221668Sjilles		vp->text = __DECONST(char *, ip->text);
188221668Sjilles		vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
189221668Sjilles		vp->func = ip->func;
1901556Srgrimes	}
1911556Srgrimes	/*
1921556Srgrimes	 * PS1 depends on uid
1931556Srgrimes	 */
194221668Sjilles	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
1951556Srgrimes		vps1.next = *vpp;
1961556Srgrimes		*vpp = &vps1;
197201056Sjilles		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
1981556Srgrimes		vps1.flags = VSTRFIXED|VTEXTFIXED;
1991556Srgrimes	}
20097689Stjr	if ((vppid.flags & VEXPORT) == 0) {
20197689Stjr		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
20297689Stjr		setvarsafe("PPID", ppid, 0);
20397689Stjr	}
2041556Srgrimes}
2051556Srgrimes
2061556Srgrimes/*
20720425Ssteve * Safe version of setvar, returns 1 on success 0 on failure.
20820425Ssteve */
20920425Ssteve
21020425Ssteveint
211200956Sjillessetvarsafe(const char *name, const char *val, int flags)
21220425Ssteve{
21320425Ssteve	struct jmploc jmploc;
214194765Sjilles	struct jmploc *const savehandler = handler;
21520425Ssteve	int err = 0;
216199660Sjilles	int inton;
21720425Ssteve
218199660Sjilles	inton = is_int_on();
21920425Ssteve	if (setjmp(jmploc.loc))
22020425Ssteve		err = 1;
22120425Ssteve	else {
22220425Ssteve		handler = &jmploc;
22320425Ssteve		setvar(name, val, flags);
22420425Ssteve	}
22520425Ssteve	handler = savehandler;
226199660Sjilles	SETINTON(inton);
22720425Ssteve	return err;
22820425Ssteve}
22920425Ssteve
23020425Ssteve/*
231155302Sschweikh * Set the value of a variable.  The flags argument is stored with the
2321556Srgrimes * flags of the variable.  If val is NULL, the variable is unset.
2331556Srgrimes */
2341556Srgrimes
2351556Srgrimesvoid
236200956Sjillessetvar(const char *name, const char *val, int flags)
23717987Speter{
238200956Sjilles	const char *p;
2391556Srgrimes	int len;
2401556Srgrimes	int namelen;
2411556Srgrimes	char *nameeq;
2421556Srgrimes	int isbad;
2431556Srgrimes
2441556Srgrimes	isbad = 0;
2451556Srgrimes	p = name;
24617525Sache	if (! is_name(*p))
2471556Srgrimes		isbad = 1;
24817525Sache	p++;
2491556Srgrimes	for (;;) {
2501556Srgrimes		if (! is_in_name(*p)) {
2511556Srgrimes			if (*p == '\0' || *p == '=')
2521556Srgrimes				break;
2531556Srgrimes			isbad = 1;
2541556Srgrimes		}
2551556Srgrimes		p++;
2561556Srgrimes	}
2571556Srgrimes	namelen = p - name;
2581556Srgrimes	if (isbad)
2591556Srgrimes		error("%.*s: bad variable name", namelen, name);
2601556Srgrimes	len = namelen + 2;		/* 2 is space for '=' and '\0' */
2611556Srgrimes	if (val == NULL) {
2621556Srgrimes		flags |= VUNSET;
2631556Srgrimes	} else {
2641556Srgrimes		len += strlen(val);
2651556Srgrimes	}
266264629Sjilles	INTOFF;
267200956Sjilles	nameeq = ckmalloc(len);
268200956Sjilles	memcpy(nameeq, name, namelen);
269200956Sjilles	nameeq[namelen] = '=';
2701556Srgrimes	if (val)
271200956Sjilles		scopy(val, nameeq + namelen + 1);
272200956Sjilles	else
273200956Sjilles		nameeq[namelen + 1] = '\0';
2741556Srgrimes	setvareq(nameeq, flags);
275264629Sjilles	INTON;
2761556Srgrimes}
2771556Srgrimes
278213811Sobrienstatic int
279200956Sjilleslocalevar(const char *s)
28090111Simp{
281207678Sjilles	const char *const *ss;
2821556Srgrimes
28317525Sache	if (*s != 'L')
28417525Sache		return 0;
28517525Sache	if (varequal(s + 1, "ANG"))
28617525Sache		return 1;
28717525Sache	if (strncmp(s + 1, "C_", 2) != 0)
28817525Sache		return 0;
289207678Sjilles	if (varequal(s + 3, "ALL"))
290207678Sjilles		return 1;
291207678Sjilles	for (ss = locale_names; *ss ; ss++)
292207678Sjilles		if (varequal(s + 3, *ss + 3))
29317525Sache			return 1;
29417525Sache	return 0;
29517525Sache}
2961556Srgrimes
297171268Sscf
2981556Srgrimes/*
299171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a
300171268Sscf * pointer into environ where the string should not be manipulated.
301171268Sscf */
302213811Sobrienstatic void
303200956Sjilleschange_env(const char *s, int set)
304171268Sscf{
305171268Sscf	char *eqp;
306171268Sscf	char *ss;
307171268Sscf
308264629Sjilles	INTOFF;
309171268Sscf	ss = savestr(s);
310171268Sscf	if ((eqp = strchr(ss, '=')) != NULL)
311171268Sscf		*eqp = '\0';
312171268Sscf	if (set && eqp != NULL)
313171268Sscf		(void) setenv(ss, eqp + 1, 1);
314171268Sscf	else
315171268Sscf		(void) unsetenv(ss);
316171268Sscf	ckfree(ss);
317264629Sjilles	INTON;
318171268Sscf
319171268Sscf	return;
320171268Sscf}
321171268Sscf
322171268Sscf
323171268Sscf/*
3241556Srgrimes * Same as setvar except that the variable and value are passed in
3251556Srgrimes * the first argument as name=value.  Since the first argument will
3261556Srgrimes * be actually stored in the table, it should not be a string that
3271556Srgrimes * will go away.
3281556Srgrimes */
3291556Srgrimes
3301556Srgrimesvoid
33190111Simpsetvareq(char *s, int flags)
33217987Speter{
3331556Srgrimes	struct var *vp, **vpp;
334221668Sjilles	int nlen;
3351556Srgrimes
33645263Scracauer	if (aflag)
33745263Scracauer		flags |= VEXPORT;
338223024Sjilles	if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
339223024Sjilles		mklocal(s);
340221668Sjilles	vp = find_var(s, &vpp, &nlen);
341221668Sjilles	if (vp != NULL) {
342264643Sjilles		if (vp->flags & VREADONLY) {
343264643Sjilles			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
344264643Sjilles				ckfree(s);
345221668Sjilles			error("%.*s: is read only", vp->name_len, s);
346264643Sjilles		}
347221668Sjilles		if (flags & VNOSET)
348221668Sjilles			return;
349221668Sjilles		INTOFF;
35020425Ssteve
351221668Sjilles		if (vp->func && (flags & VNOFUNC) == 0)
352221668Sjilles			(*vp->func)(s + vp->name_len + 1);
35320425Ssteve
354221668Sjilles		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
355221668Sjilles			ckfree(vp->text);
35620425Ssteve
357221668Sjilles		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
358221668Sjilles		vp->flags |= flags;
359221668Sjilles		vp->text = s;
36020425Ssteve
361221668Sjilles		/*
362221668Sjilles		 * We could roll this to a function, to handle it as
363221668Sjilles		 * a regular variable function callback, but why bother?
364221668Sjilles		 *
365221668Sjilles		 * Note: this assumes iflag is not set to 1 initially.
366221668Sjilles		 * As part of init(), this is called before arguments
367221668Sjilles		 * are looked at.
368221668Sjilles		 */
369221668Sjilles		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
370221668Sjilles		    iflag == 1)
371221668Sjilles			chkmail(1);
372221668Sjilles		if ((vp->flags & VEXPORT) && localevar(s)) {
373221668Sjilles			change_env(s, 1);
374221668Sjilles			(void) setlocale(LC_ALL, "");
375221668Sjilles			updatecharset();
3761556Srgrimes		}
377221668Sjilles		INTON;
378221668Sjilles		return;
3791556Srgrimes	}
3801556Srgrimes	/* not found */
381216870Sjilles	if (flags & VNOSET)
382216870Sjilles		return;
383264629Sjilles	INTOFF;
3841556Srgrimes	vp = ckmalloc(sizeof (*vp));
3851556Srgrimes	vp->flags = flags;
3861556Srgrimes	vp->text = s;
387221668Sjilles	vp->name_len = nlen;
3881556Srgrimes	vp->next = *vpp;
38920425Ssteve	vp->func = NULL;
3901556Srgrimes	*vpp = vp;
39117525Sache	if ((vp->flags & VEXPORT) && localevar(s)) {
392171268Sscf		change_env(s, 1);
39317525Sache		(void) setlocale(LC_ALL, "");
394221559Sjilles		updatecharset();
39517525Sache	}
39617525Sache	INTON;
3971556Srgrimes}
3981556Srgrimes
3991556Srgrimes
4001556Srgrimes
4011556Srgrimes/*
4021556Srgrimes * Process a linked list of variable assignments.
4031556Srgrimes */
4041556Srgrimes
4051556Srgrimesvoid
406216870Sjilleslistsetvar(struct strlist *list, int flags)
40790111Simp{
4081556Srgrimes	struct strlist *lp;
4091556Srgrimes
4101556Srgrimes	INTOFF;
4111556Srgrimes	for (lp = list ; lp ; lp = lp->next) {
412216870Sjilles		setvareq(savestr(lp->text), flags);
4131556Srgrimes	}
4141556Srgrimes	INTON;
4151556Srgrimes}
4161556Srgrimes
4171556Srgrimes
4181556Srgrimes
4191556Srgrimes/*
4201556Srgrimes * Find the value of a variable.  Returns NULL if not set.
4211556Srgrimes */
4221556Srgrimes
4231556Srgrimeschar *
424200956Sjilleslookupvar(const char *name)
42590111Simp{
4261556Srgrimes	struct var *v;
4271556Srgrimes
428221668Sjilles	v = find_var(name, NULL, NULL);
429221668Sjilles	if (v == NULL || v->flags & VUNSET)
430221668Sjilles		return NULL;
431221668Sjilles	return v->text + v->name_len + 1;
4321556Srgrimes}
4331556Srgrimes
4341556Srgrimes
4351556Srgrimes
4361556Srgrimes/*
4371556Srgrimes * Search the environment of a builtin command.  If the second argument
4381556Srgrimes * is nonzero, return the value of a variable even if it hasn't been
4391556Srgrimes * exported.
4401556Srgrimes */
4411556Srgrimes
4421556Srgrimeschar *
443200956Sjillesbltinlookup(const char *name, int doall)
44417987Speter{
4451556Srgrimes	struct strlist *sp;
4461556Srgrimes	struct var *v;
447212467Sjilles	char *result;
4481556Srgrimes
449212467Sjilles	result = NULL;
4501556Srgrimes	for (sp = cmdenviron ; sp ; sp = sp->next) {
4511556Srgrimes		if (varequal(sp->text, name))
452212467Sjilles			result = strchr(sp->text, '=') + 1;
4531556Srgrimes	}
454212467Sjilles	if (result != NULL)
455212467Sjilles		return result;
456221668Sjilles
457221668Sjilles	v = find_var(name, NULL, NULL);
458221668Sjilles	if (v == NULL || v->flags & VUNSET ||
459221668Sjilles	    (!doall && (v->flags & VEXPORT) == 0))
460221668Sjilles		return NULL;
461221668Sjilles	return v->text + v->name_len + 1;
4621556Srgrimes}
4631556Srgrimes
4641556Srgrimes
465207678Sjilles/*
466207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments).
467207678Sjilles */
468207678Sjillesvoid
469207678Sjillesbltinsetlocale(void)
470207678Sjilles{
471207678Sjilles	struct strlist *lp;
472207678Sjilles	int act = 0;
473207678Sjilles	char *loc, *locdef;
474207678Sjilles	int i;
4751556Srgrimes
476207678Sjilles	for (lp = cmdenviron ; lp ; lp = lp->next) {
477207678Sjilles		if (localevar(lp->text)) {
478207678Sjilles			act = 1;
479207678Sjilles			break;
480207678Sjilles		}
481207678Sjilles	}
482207678Sjilles	if (!act)
483207678Sjilles		return;
484207678Sjilles	loc = bltinlookup("LC_ALL", 0);
485207678Sjilles	INTOFF;
486207678Sjilles	if (loc != NULL) {
487207678Sjilles		setlocale(LC_ALL, loc);
488207678Sjilles		INTON;
489221559Sjilles		updatecharset();
490207678Sjilles		return;
491207678Sjilles	}
492207678Sjilles	locdef = bltinlookup("LANG", 0);
493207678Sjilles	for (i = 0; locale_names[i] != NULL; i++) {
494207678Sjilles		loc = bltinlookup(locale_names[i], 0);
495207678Sjilles		if (loc == NULL)
496207678Sjilles			loc = locdef;
497207678Sjilles		if (loc != NULL)
498207678Sjilles			setlocale(locale_categories[i], loc);
499207678Sjilles	}
500207678Sjilles	INTON;
501221559Sjilles	updatecharset();
502207678Sjilles}
503207678Sjilles
5041556Srgrimes/*
505207678Sjilles * Undo the effect of bltinlocaleset().
506207678Sjilles */
507207678Sjillesvoid
508207678Sjillesbltinunsetlocale(void)
509207678Sjilles{
510207678Sjilles	struct strlist *lp;
511207678Sjilles
512207678Sjilles	INTOFF;
513207678Sjilles	for (lp = cmdenviron ; lp ; lp = lp->next) {
514207678Sjilles		if (localevar(lp->text)) {
515207678Sjilles			setlocale(LC_ALL, "");
516221559Sjilles			updatecharset();
517207678Sjilles			return;
518207678Sjilles		}
519207678Sjilles	}
520207678Sjilles	INTON;
521207678Sjilles}
522207678Sjilles
523221559Sjilles/*
524221559Sjilles * Update the localeisutf8 flag.
525221559Sjilles */
526221559Sjillesvoid
527221559Sjillesupdatecharset(void)
528221559Sjilles{
529221559Sjilles	char *charset;
530207678Sjilles
531221559Sjilles	charset = nl_langinfo(CODESET);
532221559Sjilles	localeisutf8 = !strcmp(charset, "UTF-8");
533221559Sjilles}
534221559Sjilles
535221669Sjillesvoid
536221669Sjillesinitcharset(void)
537221669Sjilles{
538221669Sjilles	updatecharset();
539221669Sjilles	initial_localeisutf8 = localeisutf8;
540221669Sjilles}
541221669Sjilles
542207678Sjilles/*
5431556Srgrimes * Generate a list of exported variables.  This routine is used to construct
5441556Srgrimes * the third argument to execve when executing a program.
5451556Srgrimes */
5461556Srgrimes
5471556Srgrimeschar **
54890111Simpenvironment(void)
54990111Simp{
5501556Srgrimes	int nenv;
5511556Srgrimes	struct var **vpp;
5521556Srgrimes	struct var *vp;
5531556Srgrimes	char **env, **ep;
5541556Srgrimes
5551556Srgrimes	nenv = 0;
5561556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5571556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next)
5581556Srgrimes			if (vp->flags & VEXPORT)
5591556Srgrimes				nenv++;
5601556Srgrimes	}
5611556Srgrimes	ep = env = stalloc((nenv + 1) * sizeof *env);
5621556Srgrimes	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5631556Srgrimes		for (vp = *vpp ; vp ; vp = vp->next)
5641556Srgrimes			if (vp->flags & VEXPORT)
5651556Srgrimes				*ep++ = vp->text;
5661556Srgrimes	}
5671556Srgrimes	*ep = NULL;
5681556Srgrimes	return env;
5691556Srgrimes}
5701556Srgrimes
5711556Srgrimes
572213811Sobrienstatic int
573158145Sstefanfvar_compare(const void *a, const void *b)
574158145Sstefanf{
575158145Sstefanf	const char *const *sa, *const *sb;
5761556Srgrimes
577158145Sstefanf	sa = a;
578158145Sstefanf	sb = b;
579158145Sstefanf	/*
580158145Sstefanf	 * This compares two var=value strings which creates a different
581158145Sstefanf	 * order from what you would probably expect.  POSIX is somewhat
582158145Sstefanf	 * ambiguous on what should be sorted exactly.
583158145Sstefanf	 */
584158145Sstefanf	return strcoll(*sa, *sb);
585158145Sstefanf}
586158145Sstefanf
587158145Sstefanf
5881556Srgrimes/*
589217847Sjilles * Command to list all variables which are set.  This is invoked from the
590217847Sjilles * set command when it is called without any options or operands.
5911556Srgrimes */
5921556Srgrimes
5931556Srgrimesint
59490111Simpshowvarscmd(int argc __unused, char **argv __unused)
59517987Speter{
5961556Srgrimes	struct var **vpp;
5971556Srgrimes	struct var *vp;
59897915Stjr	const char *s;
599158145Sstefanf	const char **vars;
600158145Sstefanf	int i, n;
6011556Srgrimes
602158145Sstefanf	/*
603158145Sstefanf	 * POSIX requires us to sort the variables.
604158145Sstefanf	 */
605158145Sstefanf	n = 0;
606158145Sstefanf	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
607158145Sstefanf		for (vp = *vpp; vp; vp = vp->next) {
608158145Sstefanf			if (!(vp->flags & VUNSET))
609158145Sstefanf				n++;
6101556Srgrimes		}
6111556Srgrimes	}
612158145Sstefanf
613231529Sjilles	INTOFF;
614158145Sstefanf	vars = ckmalloc(n * sizeof(*vars));
615158145Sstefanf	i = 0;
616158145Sstefanf	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
617158145Sstefanf		for (vp = *vpp; vp; vp = vp->next) {
618158145Sstefanf			if (!(vp->flags & VUNSET))
619158145Sstefanf				vars[i++] = vp->text;
620158145Sstefanf		}
621158145Sstefanf	}
622158145Sstefanf
623158145Sstefanf	qsort(vars, n, sizeof(*vars), var_compare);
624158145Sstefanf	for (i = 0; i < n; i++) {
625223183Sjilles		/*
626223183Sjilles		 * Skip improper variable names so the output remains usable as
627223183Sjilles		 * shell input.
628223183Sjilles		 */
629223183Sjilles		if (!isassignment(vars[i]))
630223183Sjilles			continue;
631215567Sjilles		s = strchr(vars[i], '=');
632215567Sjilles		s++;
633215567Sjilles		outbin(vars[i], s - vars[i], out1);
634215567Sjilles		out1qstr(s);
635158145Sstefanf		out1c('\n');
636158145Sstefanf	}
637158145Sstefanf	ckfree(vars);
638231529Sjilles	INTON;
639158145Sstefanf
6401556Srgrimes	return 0;
6411556Srgrimes}
6421556Srgrimes
6431556Srgrimes
6441556Srgrimes
6451556Srgrimes/*
6461556Srgrimes * The export and readonly commands.
6471556Srgrimes */
6481556Srgrimes
6491556Srgrimesint
65090111Simpexportcmd(int argc, char **argv)
65117987Speter{
6521556Srgrimes	struct var **vpp;
6531556Srgrimes	struct var *vp;
6541556Srgrimes	char *name;
6551556Srgrimes	char *p;
65697914Stjr	char *cmdname;
65797914Stjr	int ch, values;
6581556Srgrimes	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
6591556Srgrimes
66097914Stjr	cmdname = argv[0];
66197914Stjr	optreset = optind = 1;
662100663Stjr	opterr = 0;
66397914Stjr	values = 0;
66497914Stjr	while ((ch = getopt(argc, argv, "p")) != -1) {
66597914Stjr		switch (ch) {
66697914Stjr		case 'p':
66797914Stjr			values = 1;
66897914Stjr			break;
66997914Stjr		case '?':
67097914Stjr		default:
67197914Stjr			error("unknown option: -%c", optopt);
67297914Stjr		}
67397914Stjr	}
67497914Stjr	argc -= optind;
67597914Stjr	argv += optind;
67697914Stjr
677149919Sstefanf	if (values && argc != 0)
678149919Sstefanf		error("-p requires no arguments");
67997914Stjr	if (argc != 0) {
680149919Sstefanf		while ((name = *argv++) != NULL) {
6811556Srgrimes			if ((p = strchr(name, '=')) != NULL) {
6821556Srgrimes				p++;
6831556Srgrimes			} else {
684221668Sjilles				vp = find_var(name, NULL, NULL);
685221668Sjilles				if (vp != NULL) {
686221668Sjilles					vp->flags |= flag;
687221668Sjilles					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
688221668Sjilles						change_env(vp->text, 1);
689221668Sjilles						(void) setlocale(LC_ALL, "");
690221668Sjilles						updatecharset();
6911556Srgrimes					}
692221668Sjilles					continue;
6931556Srgrimes				}
6941556Srgrimes			}
6951556Srgrimes			setvar(name, p, flag);
6961556Srgrimes		}
6971556Srgrimes	} else {
6981556Srgrimes		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6991556Srgrimes			for (vp = *vpp ; vp ; vp = vp->next) {
7001556Srgrimes				if (vp->flags & flag) {
70197914Stjr					if (values) {
702223183Sjilles						/*
703223183Sjilles						 * Skip improper variable names
704223183Sjilles						 * so the output remains usable
705223183Sjilles						 * as shell input.
706223183Sjilles						 */
707223183Sjilles						if (!isassignment(vp->text))
708223183Sjilles							continue;
70997914Stjr						out1str(cmdname);
71097914Stjr						out1c(' ');
71197914Stjr					}
71297914Stjr					if (values && !(vp->flags & VUNSET)) {
713221975Sjilles						outbin(vp->text,
714221975Sjilles						    vp->name_len + 1, out1);
715221975Sjilles						out1qstr(vp->text +
716221975Sjilles						    vp->name_len + 1);
717215567Sjilles					} else
718221975Sjilles						outbin(vp->text, vp->name_len,
719215567Sjilles						    out1);
7201556Srgrimes					out1c('\n');
7211556Srgrimes				}
7221556Srgrimes			}
7231556Srgrimes		}
7241556Srgrimes	}
7251556Srgrimes	return 0;
7261556Srgrimes}
7271556Srgrimes
7281556Srgrimes
7291556Srgrimes/*
7301556Srgrimes * The "local" command.
7311556Srgrimes */
7321556Srgrimes
73317987Speterint
73490111Simplocalcmd(int argc __unused, char **argv __unused)
73517987Speter{
7361556Srgrimes	char *name;
7371556Srgrimes
7381556Srgrimes	if (! in_function())
7391556Srgrimes		error("Not in a function");
7401556Srgrimes	while ((name = *argptr++) != NULL) {
7411556Srgrimes		mklocal(name);
7421556Srgrimes	}
7431556Srgrimes	return 0;
7441556Srgrimes}
7451556Srgrimes
7461556Srgrimes
7471556Srgrimes/*
7481556Srgrimes * Make a variable a local variable.  When a variable is made local, it's
7491556Srgrimes * value and flags are saved in a localvar structure.  The saved values
7501556Srgrimes * will be restored when the shell function returns.  We handle the name
7511556Srgrimes * "-" as a special case.
7521556Srgrimes */
7531556Srgrimes
7541556Srgrimesvoid
75590111Simpmklocal(char *name)
75690111Simp{
7571556Srgrimes	struct localvar *lvp;
7581556Srgrimes	struct var **vpp;
7591556Srgrimes	struct var *vp;
7601556Srgrimes
7611556Srgrimes	INTOFF;
7621556Srgrimes	lvp = ckmalloc(sizeof (struct localvar));
7631556Srgrimes	if (name[0] == '-' && name[1] == '\0') {
7641556Srgrimes		lvp->text = ckmalloc(sizeof optlist);
76517987Speter		memcpy(lvp->text, optlist, sizeof optlist);
7661556Srgrimes		vp = NULL;
7671556Srgrimes	} else {
768221668Sjilles		vp = find_var(name, &vpp, NULL);
7691556Srgrimes		if (vp == NULL) {
7701556Srgrimes			if (strchr(name, '='))
771223024Sjilles				setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
7721556Srgrimes			else
773223024Sjilles				setvar(name, NULL, VSTRFIXED | VNOLOCAL);
7741556Srgrimes			vp = *vpp;	/* the new variable */
7751556Srgrimes			lvp->text = NULL;
7761556Srgrimes			lvp->flags = VUNSET;
7771556Srgrimes		} else {
7781556Srgrimes			lvp->text = vp->text;
7791556Srgrimes			lvp->flags = vp->flags;
7801556Srgrimes			vp->flags |= VSTRFIXED|VTEXTFIXED;
781221668Sjilles			if (name[vp->name_len] == '=')
782223024Sjilles				setvareq(savestr(name), VNOLOCAL);
7831556Srgrimes		}
7841556Srgrimes	}
7851556Srgrimes	lvp->vp = vp;
7861556Srgrimes	lvp->next = localvars;
7871556Srgrimes	localvars = lvp;
7881556Srgrimes	INTON;
7891556Srgrimes}
7901556Srgrimes
7911556Srgrimes
7921556Srgrimes/*
7931556Srgrimes * Called after a function returns.
7941556Srgrimes */
7951556Srgrimes
7961556Srgrimesvoid
79790111Simppoplocalvars(void)
79890111Simp{
7991556Srgrimes	struct localvar *lvp;
8001556Srgrimes	struct var *vp;
8011556Srgrimes
802264629Sjilles	INTOFF;
8031556Srgrimes	while ((lvp = localvars) != NULL) {
8041556Srgrimes		localvars = lvp->next;
8051556Srgrimes		vp = lvp->vp;
8061556Srgrimes		if (vp == NULL) {	/* $- saved */
80717987Speter			memcpy(optlist, lvp->text, sizeof optlist);
8081556Srgrimes			ckfree(lvp->text);
809215266Sjilles			optschanged();
8101556Srgrimes		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
8111556Srgrimes			(void)unsetvar(vp->text);
8121556Srgrimes		} else {
8131556Srgrimes			if ((vp->flags & VTEXTFIXED) == 0)
8141556Srgrimes				ckfree(vp->text);
8151556Srgrimes			vp->flags = lvp->flags;
8161556Srgrimes			vp->text = lvp->text;
8171556Srgrimes		}
8181556Srgrimes		ckfree(lvp);
8191556Srgrimes	}
820264629Sjilles	INTON;
8211556Srgrimes}
8221556Srgrimes
8231556Srgrimes
82417987Speterint
82590111Simpsetvarcmd(int argc, char **argv)
82617987Speter{
8271556Srgrimes	if (argc <= 2)
8281556Srgrimes		return unsetcmd(argc, argv);
8291556Srgrimes	else if (argc == 3)
8301556Srgrimes		setvar(argv[1], argv[2], 0);
8311556Srgrimes	else
832214538Sjilles		error("too many arguments");
8331556Srgrimes	return 0;
8341556Srgrimes}
8351556Srgrimes
8361556Srgrimes
8371556Srgrimes/*
838217847Sjilles * The unset builtin command.
8391556Srgrimes */
8401556Srgrimes
84117987Speterint
84290111Simpunsetcmd(int argc __unused, char **argv __unused)
84317987Speter{
8441556Srgrimes	char **ap;
8451556Srgrimes	int i;
8461556Srgrimes	int flg_func = 0;
8471556Srgrimes	int flg_var = 0;
8481556Srgrimes	int ret = 0;
8491556Srgrimes
8501556Srgrimes	while ((i = nextopt("vf")) != '\0') {
8511556Srgrimes		if (i == 'f')
8521556Srgrimes			flg_func = 1;
8531556Srgrimes		else
8541556Srgrimes			flg_var = 1;
8551556Srgrimes	}
8561556Srgrimes	if (flg_func == 0 && flg_var == 0)
8571556Srgrimes		flg_var = 1;
8588855Srgrimes
859264629Sjilles	INTOFF;
8601556Srgrimes	for (ap = argptr; *ap ; ap++) {
8611556Srgrimes		if (flg_func)
8621556Srgrimes			ret |= unsetfunc(*ap);
8631556Srgrimes		if (flg_var)
8641556Srgrimes			ret |= unsetvar(*ap);
8651556Srgrimes	}
866264629Sjilles	INTON;
8671556Srgrimes	return ret;
8681556Srgrimes}
8691556Srgrimes
8701556Srgrimes
8711556Srgrimes/*
8721556Srgrimes * Unset the specified variable.
873264629Sjilles * Called with interrupts off.
8741556Srgrimes */
8751556Srgrimes
87620425Ssteveint
877200956Sjillesunsetvar(const char *s)
87890111Simp{
8791556Srgrimes	struct var **vpp;
8801556Srgrimes	struct var *vp;
8811556Srgrimes
882221668Sjilles	vp = find_var(s, &vpp, NULL);
883221668Sjilles	if (vp == NULL)
884221668Sjilles		return (0);
885221668Sjilles	if (vp->flags & VREADONLY)
886221668Sjilles		return (1);
887221668Sjilles	if (vp->text[vp->name_len + 1] != '\0')
888221668Sjilles		setvar(s, nullstr, 0);
889221668Sjilles	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
890221668Sjilles		change_env(s, 0);
891221668Sjilles		setlocale(LC_ALL, "");
892221668Sjilles		updatecharset();
8931556Srgrimes	}
894221668Sjilles	vp->flags &= ~VEXPORT;
895221668Sjilles	vp->flags |= VUNSET;
896221668Sjilles	if ((vp->flags & VSTRFIXED) == 0) {
897221668Sjilles		if ((vp->flags & VTEXTFIXED) == 0)
898221668Sjilles			ckfree(vp->text);
899221668Sjilles		*vpp = vp->next;
900221668Sjilles		ckfree(vp);
901221668Sjilles	}
902135856Sdes	return (0);
9031556Srgrimes}
9041556Srgrimes
9051556Srgrimes
9061556Srgrimes
9071556Srgrimes/*
9081556Srgrimes * Returns true if the two strings specify the same varable.  The first
9091556Srgrimes * variable name is terminated by '='; the second may be terminated by
9101556Srgrimes * either '=' or '\0'.
9111556Srgrimes */
9121556Srgrimes
913213811Sobrienstatic int
914200956Sjillesvarequal(const char *p, const char *q)
91590111Simp{
9161556Srgrimes	while (*p == *q++) {
9171556Srgrimes		if (*p++ == '=')
9181556Srgrimes			return 1;
9191556Srgrimes	}
9201556Srgrimes	if (*p == '=' && *(q - 1) == '\0')
9211556Srgrimes		return 1;
9221556Srgrimes	return 0;
9231556Srgrimes}
924221668Sjilles
925221668Sjilles/*
926221668Sjilles * Search for a variable.
927221668Sjilles * 'name' may be terminated by '=' or a NUL.
928221668Sjilles * vppp is set to the pointer to vp, or the list head if vp isn't found
929221668Sjilles * lenp is set to the number of charactets in 'name'
930221668Sjilles */
931221668Sjilles
932221668Sjillesstatic struct var *
933221668Sjillesfind_var(const char *name, struct var ***vppp, int *lenp)
934221668Sjilles{
935221668Sjilles	unsigned int hashval;
936221668Sjilles	int len;
937221668Sjilles	struct var *vp, **vpp;
938221668Sjilles	const char *p = name;
939221668Sjilles
940221668Sjilles	hashval = 0;
941221668Sjilles	while (*p && *p != '=')
942221668Sjilles		hashval = 2 * hashval + (unsigned char)*p++;
943221668Sjilles	len = p - name;
944221668Sjilles
945221668Sjilles	if (lenp)
946221668Sjilles		*lenp = len;
947221668Sjilles	vpp = &vartab[hashval % VTABSIZE];
948221668Sjilles	if (vppp)
949221668Sjilles		*vppp = vpp;
950221668Sjilles
951221668Sjilles	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
952221668Sjilles		if (vp->name_len != len)
953221668Sjilles			continue;
954221668Sjilles		if (memcmp(vp->text, name, len) != 0)
955221668Sjilles			continue;
956221668Sjilles		if (vppp)
957221668Sjilles			*vppp = vpp;
958221668Sjilles		return vp;
959221668Sjilles	}
960221668Sjilles	return NULL;
961221668Sjilles}
962