Skip to contentSkip to conversation

Assignment with command: export only, or sets variable, too?

PATCH In review Herbert Xu 2 participants 12 messages May 26, 2010 – May 29, 2010 9 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Herbert Xu

author
#9 lore raw
Quoted 10 lines · Geoff Clare

Herbert Xu <[email protected]> wrote, on 09 Apr 2010:

Quoted 4 lines

While we're on this topic, what is the correct output for

	X=a Y=${X=b} Z=$X sh -c 'echo $Z'

My reading of the standard is that this should output "b" (assuming X is unset beforehand). But, as I mentioned earlier in the thread, this is a strict reading and might not be what was intended.

Thank you for your clarifications.

These four patches should help bring dash in-line with the POSIX requirements.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author
#10 lore raw

PATCH 1/4 Add localvars nesting

[VAR] Add localvars nesting

This patch adds localvars nesting infrastructure so we can reuse the localvars mechanism for command evaluation.

---

 ChangeLog  |    4 ++++
 src/eval.c |    7 ++-----
 src/var.c  |   50 ++++++++++++++++++++++++++++++++++++++++++++------
 src/var.h  |    1 +
 4 files changed, 51 insertions(+), 11 deletions(-)
ChangeLog +4 -0
index ee78154..7285a23 100644
@@ -1,3 +1,7 @@
+2010-05-24  Herbert Xu <[email protected]>
+
+	* Add localvars nesting.
+
 2010-05-03  Gerrit Pape <[email protected]>
 
 	* Fix command -- crash.
src/eval.c +2 -5
index 62d9d5d..8d2767c 100644
@@ -928,20 +928,17 @@ STATIC int
 evalfun(struct funcnode *func, int argc, char **argv, int flags)
 {
 	volatile struct shparam saveparam;
-	struct localvar *volatile savelocalvars;
 	struct jmploc *volatile savehandler;
 	struct jmploc jmploc;
 	int e;
 
 	saveparam = shellparam;
-	savelocalvars = localvars;
 	if ((e = setjmp(jmploc.loc))) {
 		goto funcdone;
 	}
 	INTOFF;
 	savehandler = handler;
 	handler = &jmploc;
-	localvars = NULL;
 	shellparam.malloc = 0;
 	func->count++;
 	funcnest++;
@@ -950,13 +947,13 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
 	shellparam.p = argv + 1;
 	shellparam.optind = 1;
 	shellparam.optoff = -1;
+	pushlocalvars();
 	evaltree(&func->n, flags & EV_TESTED);
+	poplocalvars();
 funcdone:
 	INTOFF;
 	funcnest--;
 	freefunc(func);
-	poplocalvars();
-	localvars = savelocalvars;
 	freeparam(&shellparam);
 	shellparam = saveparam;
 	handler = savehandler;
src/var.c +44 -6

88 lines not on this page · raw message

src/var.h +1 -0
index e4e2cff..32b0dde 100644
@@ -139,6 +139,7 @@ char **listvars(int, int, char ***);
 int showvars(const char *, int, int);
 int exportcmd(int, char **);
 int localcmd(int, char **);
+void pushlocalvars(void);
 void poplocalvars(void);
 int unsetcmd(int, char **);
 int unsetvar(const char *);
#11 lore raw

Re: [PATCH 1/4] [VAR] Add localvars nesting

Quoted 4 lines · Herbert Xu

[VAR] Add localvars nesting

This patch adds localvars nesting infrastructure so we can reuse the localvars mechanism for command evaluation.

localvars could already nest, it's just that this change makes the nesting explicit instead of on the C stack.

Further comments inline.

Quoted 52 lines

---

 ChangeLog  |    4 ++++
 src/eval.c |    7 ++-----
 src/var.c  |   50 ++++++++++++++++++++++++++++++++++++++++++++------
 src/var.h  |    1 +
 4 files changed, 51 insertions(+), 11 deletions(-)
ChangeLog +4 -0
index ee78154..7285a23 100644
@@ -1,3 +1,7 @@
+2010-05-24  Herbert Xu <[email protected]>
+
+	* Add localvars nesting.
+
 2010-05-03  Gerrit Pape <[email protected]>
 
 	* Fix command -- crash.
src/eval.c +1 -3
index 62d9d5d..8d2767c 100644
@@ -928,20 +928,17 @@ STATIC int
 evalfun(struct funcnode *func, int argc, char **argv, int flags)
 {
 	volatile struct shparam saveparam;
-	struct localvar *volatile savelocalvars;
 	struct jmploc *volatile savehandler;
 	struct jmploc jmploc;
 	int e;
 
 	saveparam = shellparam;
-	savelocalvars = localvars;
 	if ((e = setjmp(jmploc.loc))) {
 		goto funcdone;
 	}
 	INTOFF;
 	savehandler = handler;
 	handler = &jmploc;
-	localvars = NULL;
 	shellparam.malloc = 0;
 	func->count++;
 	funcnest++;
@@ -950,13 +947,13 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
 	shellparam.p = argv + 1;
 	shellparam.optind = 1;
 	shellparam.optoff = -1;
+	pushlocalvars();
 	evaltree(&func->n, flags & EV_TESTED);

src/eval.c evalfun(struct funcnode *func, int argc, char **argv, int flags) · line 959

Quoted 10 lines
src/eval.c +1 -2
+	poplocalvars();
 funcdone:
 	INTOFF;
 	funcnest--;
 	freefunc(func);
-	poplocalvars();
-	localvars = savelocalvars;
 	freeparam(&shellparam);
 	shellparam = saveparam;
 	handler = savehandler;

This change does not do poplocalvars() if the function was exited with an error. While this is normally not a problem because the shell exits or executes the RESET actions, 'command eval', 'command .' and 'fc' will "catch" any errors and continue normally.

Example (with the below "disallow local outside a function" change):
dash -c 'f() { unset xyz; ${xyz?}; }; command eval f; local i'

src/var.c localcmd(int argc, char **argv) · line 464

Quoted 13 lines
src/var.c +3 -0
index 2737fb1..de1a5f5 100644
[...]
@@ -446,6 +456,9 @@ localcmd(int argc, char **argv)
 {
 	char *name;
 
+	if (!localvar_stack)
+		sh_error("not in a function");
+
 	argv = argptr;
 	while ((name = *argv++) != NULL) {
 		mklocal(name);

This change (failing local outside functions), while a good idea, should be mentioned in the commit message/changelog, as it might break certain scripts.

(Note that in mksh local is an alias for typeset, which will work outside functions.)

Signature · 1 line
Jilles Tjoelker
author
#12 lore raw

Re: [PATCH 1/4] [VAR] Add localvars nesting

Quoted 23 lines · Jilles Tjoelker
Quoted 7 lines
(quoted diff) +1 -0
@@ -950,13 +947,13 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
 	shellparam.p = argv + 1;
 	shellparam.optind = 1;
 	shellparam.optoff = -1;
+	pushlocalvars();
 	evaltree(&func->n, flags & EV_TESTED);

Review evalfun(struct funcnode *func, int argc, char **argv, int flags) · line 959

Quoted 10 lines
(quoted diff) +1 -2
+	poplocalvars();
 funcdone:
 	INTOFF;
 	funcnest--;
 	freefunc(func);
-	poplocalvars();
-	localvars = savelocalvars;
 	freeparam(&shellparam);
 	shellparam = saveparam;
 	handler = savehandler;

This change does not do poplocalvars() if the function was exited with an error. While this is normally not a problem because the shell exits or executes the RESET actions, 'command eval', 'command .' and 'fc' will "catch" any errors and continue normally.

Quoted 2 lines

Example (with the below "disallow local outside a function" change):
dash -c 'f() { unset xyz; ${xyz?}; }; command eval f; local i'

Thanks for catching this! The following patch should fix that problem. BTW, the same issue has existed for redirections for quite some time. I will fix this in a subsequent patch.

Quoted 3 lines

This change (failing local outside functions), while a good idea, should be mentioned in the commit message/changelog, as it might break certain scripts.

I will modify the Changelog to highlight this change.

commit 127788364951212c356aadc39deb21e01b0161c8

Author: Herbert Xu <[email protected]>
Date:   Thu May 27 11:32:55 2010 +0800

[VAR] Fix poplocalvar on abnormal exit from function

The new localvar code broke the abnormal exit from functions and built-ins by not restoring the original localvar state.

This patch fixes this by storing the previous localvar state so that we always unwind correctly in case of an abnormal exit.

ChangeLog +4 -0
index bf1f13c..c9b5e75 100644
@@ -1,3 +1,7 @@
+2010-05-27  Herbert Xu <[email protected]>
+
+	* Fix poplocalvar on abnormal exit from function.
+
 2010-05-26  Herbert Xu <[email protected]>
 
 	* Replace cmdenviron with localvars.
src/eval.c +3 -2
index a6981a9..2cd931b 100644
@@ -681,6 +681,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
 evalcommand(union node *cmd, int flags)
 #endif
 {
+	struct localvar_list *localvar_stop;
 	struct stackmark smark;
 	union node *argp;
 	struct arglist arglist;
@@ -703,7 +704,7 @@ evalcommand(union node *cmd, int flags)
 	/* First expand the arguments. */
 	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
 	setstackmark(&smark);
-	pushlocalvars();
+	localvar_stop = pushlocalvars();
 	back_exitstatus = 0;
 
 	cmdentry.cmdtype = CMDBUILTIN;
@@ -837,7 +838,6 @@ bail:
 			if (forkshell(jp, cmd, FORK_FG) != 0) {
 				exitstatus = waitforjob(jp);
 				INTON;
-				poplocalvars(0);
 				break;
 			}
 			FORCEINTON;
@@ -878,6 +878,7 @@ raise:
 
 out:
 	popredir(execcmd);
+	unwindlocalvars(localvar_stop);
 	if (lastarg)
 		/* dsl: I think this is intended to be used to support
 		 * '_' in 'vi' command mode during line editing...
src/var.c +11 -3
index 40bd3fd..f456fbd 100644
@@ -144,8 +144,7 @@ INIT {
 }
 
 RESET {
-	while (localvar_stack)
-		poplocalvars(0);
+	unwindlocalvars(0);
 }
 #endif
 
@@ -570,7 +569,7 @@ poplocalvars(int keep)
 /*
  * Create a new localvar environment.
  */
-void pushlocalvars(void)
+struct localvar_list *pushlocalvars(void)
 {
 	struct localvar_list *ll;
 
@@ -580,6 +579,15 @@ void pushlocalvars(void)
 	ll->next = localvar_stack;
 	localvar_stack = ll;
 	INTON;
+
+	return ll->next;
+}
+
+
+void unwindlocalvars(struct localvar_list *stop)
+{
+	while (localvar_stack != stop)
+		poplocalvars(0);
 }
 
 
src/var.h +4 -1
index ef6d583..7e7e505 100644
@@ -69,6 +69,8 @@ struct localvar {
 	const char *text;		/* saved text */
 };
 
+struct localvar_list;
+
 
 extern struct localvar *localvars;
 extern struct var varinit[];
@@ -139,8 +141,9 @@ int showvars(const char *, int, int);
 int exportcmd(int, char **);
 int localcmd(int, char **);
 void mklocal(char *);
-void pushlocalvars(void);
+struct localvar_list *pushlocalvars(void);
 void poplocalvars(int);
+void unwindlocalvars(struct localvar_list *stop);
 int unsetcmd(int, char **);
 void unsetvar(const char *);
 int varcmp(const char *, const char *);

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author
#13 lore raw

PATCH 1/2 Move null redirect checks into caller

[REDIR] Move null redirect checks into caller

The null redirect checks were added as an optimisation to avoid unnecessary memory allocations. However, we could avoid this completely by simply making the caller avoid making a redirection unless it is not null.

---

 ChangeLog   |    1 +
 src/eval.c  |    6 ++++--
 src/redir.c |   14 +-------------
 3 files changed, 6 insertions(+), 15 deletions(-)
ChangeLog +1 -0
index 4fc35a6..1fd184b 100644
@@ -2,6 +2,7 @@
 
 	* Fix poplocalvar on abnormal exit from function.
 	* Do not poplocalvars prematurely on regular utilities.
+	* Move null redirect checks into caller.
 
 2010-05-26  Herbert Xu <[email protected]>
 
src/eval.c +4 -2
index 337667f..59bded9 100644
@@ -224,7 +224,8 @@ evaltree(union node *n, int flags)
 			evaltree(n->nredir.n, flags & EV_TESTED);
 			status = exitstatus;
 		}
-		popredir(0);
+		if (n->nredir.redirect)
+			popredir(0);
 		goto setstatus;
 	case NCMD:
 #ifdef notyet
@@ -879,7 +880,8 @@ raise:
 	}
 
 out:
-	popredir(execcmd);
+	if (cmd->ncmd.redirect)
+		popredir(execcmd);
 	unwindlocalvars(localvar_stop);
 	if (lastarg)
 		/* dsl: I think this is intended to be used to support
src/redir.c +1 -13
index 54af96b..16decfc 100644
@@ -72,12 +72,10 @@ MKINIT
 struct redirtab {
 	struct redirtab *next;
 	int renamed[10];
-	int nullredirs;
 };
 
 
 MKINIT struct redirtab *redirlist;
-MKINIT int nullredirs;
 
 STATIC int openredirect(union node *);
 #ifdef notyet
@@ -113,7 +111,6 @@ redirect(union node *redir, int flags)
 		memory[i] = 0;
 	memory[1] = flags & REDIR_BACKQ;
 #endif
-	nullredirs++;
 	if (!redir) {
 		return;
 	}
@@ -124,10 +121,8 @@ redirect(union node *redir, int flags)
 		q = ckmalloc(sizeof (struct redirtab));
 		q->next = redirlist;
 		redirlist = q;
-		q->nullredirs = nullredirs - 1;
 		for (i = 0 ; i < 10 ; i++)
 			q->renamed[i] = EMPTY;
-		nullredirs = 0;
 		sv = q;
 	}
 	n = redir;
@@ -343,8 +338,6 @@ popredir(int drop)
 	struct redirtab *rp;
 	int i;
 
-	if (--nullredirs >= 0)
-		return;
 	INTOFF;
 	rp = redirlist;
 	for (i = 0 ; i < 10 ; i++) {
@@ -364,7 +357,6 @@ popredir(int drop)
 		}
 	}
 	redirlist = rp->next;
-	nullredirs = rp->nullredirs;
 	ckfree(rp);
 	INTON;
 }
@@ -381,12 +373,8 @@ RESET {
 	/*
 	 * Discard all saved file descriptors.
 	 */
-	for (;;) {
-		nullredirs = 0;
-		if (!redirlist)
-			break;
+	while (redirlist)
 		popredir(0);
-	}
 }
 
 #endif
author
#14 lore raw

PATCH 2/2 Fix popredir on abnormal exit from built-in

In reply to Herbert Xu

[REDIR] Fix popredir on abnormal exit from built-in

Just like the poplocalvar problem recently fixed, redirections can also be leaked in case of an abnormal exit. This patch fixes it using the same method as poplocalvar, by storing the previous redirection state and restoring to that point.

---

 ChangeLog   |    1 +
 src/eval.c  |    4 ++++
 src/redir.c |   45 ++++++++++++++++++++++++++++++++-------------
 src/redir.h |    3 +++
 4 files changed, 40 insertions(+), 13 deletions(-)
ChangeLog +1 -0
index 1fd184b..650899a 100644
@@ -3,6 +3,7 @@
 	* Fix poplocalvar on abnormal exit from function.
 	* Do not poplocalvars prematurely on regular utilities.
 	* Move null redirect checks into caller.
+	* Fix popredir on abnormal exit from built-in.
 
 2010-05-26  Herbert Xu <[email protected]>
 
src/eval.c +4 -0
index 59bded9..d5c1e6c 100644
@@ -219,6 +219,7 @@ evaltree(union node *n, int flags)
 		goto setstatus;
 	case NREDIR:
 		expredir(n->nredir.redirect);
+		pushredir(n->nredir.redirect);
 		status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
 		if (!status) {
 			evaltree(n->nredir.n, flags & EV_TESTED);
@@ -683,6 +684,7 @@ evalcommand(union node *cmd, int flags)
 #endif
 {
 	struct localvar_list *localvar_stop;
+	struct redirtab *redir_stop;
 	struct stackmark smark;
 	union node *argp;
 	struct arglist arglist;
@@ -740,6 +742,7 @@ evalcommand(union node *cmd, int flags)
 
 	preverrout.fd = 2;
 	expredir(cmd->ncmd.redirect);
+	redir_stop = pushredir(cmd->ncmd.redirect);
 	status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH|REDIR_SAVEFD2);
 
 	path = vpath.text;
@@ -882,6 +885,7 @@ raise:
 out:
 	if (cmd->ncmd.redirect)
 		popredir(execcmd);
+	unwindredir(redir_stop);
 	unwindlocalvars(localvar_stop);
 	if (lastarg)
 		/* dsl: I think this is intended to be used to support
src/redir.c +32 -13

63 lines not on this page · raw message

src/redir.h +3 -0
index d1d160e..8e56995 100644
@@ -41,10 +41,13 @@
 #endif
 #define REDIR_SAVEFD2 03	/* set preverrout */
 
+struct redirtab;
 union node;
 void redirect(union node *, int);
 void popredir(int);
 void clearredir(void);
 int savefd(int, int);
 int redirectsafe(union node *, int);
+void unwindredir(struct redirtab *stop);
+struct redirtab *pushredir(union node *redir);
 
author
#15 lore raw

PATCH 2/4 Fix poplocalvar leak

In reply to Herbert Xu

[VAR] Fix poplocalvar leak

When a variable is marked as local, we set VSTRFIXED on its vp recored. However, poplocalvar never clears this flag for variables that were unset to begin with. Thus if you ever made an unset variable local, it would get the VSTRFIXED bit and stick around forever.

---

 ChangeLog |    4 ++++
 src/var.c |    8 +++-----
 2 files changed, 7 insertions(+), 5 deletions(-)
ChangeLog +4 -0
index 7285a23..f3c7701 100644
@@ -1,3 +1,7 @@
+2010-05-25  Herbert Xu <[email protected]>
+
+	* Fix poplocalvar leak.
+
 2010-05-24  Herbert Xu <[email protected]>
 
 	* Add localvars nesting.
src/var.c +3 -5
index de1a5f5..fc6d367 100644
@@ -543,7 +543,8 @@ poplocalvars(void)
 			memcpy(optlist, lvp->text, sizeof(optlist));
 			ckfree(lvp->text);
 			optschanged();
-		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
+		} else if (lvp->flags == VUNSET) {
+			vp->flags &= ~(VSTRFIXED|VREADONLY);
 			unsetvar(vp->text);
 		} else {
 			if (vp->func)
@@ -627,8 +628,6 @@ unsetvar(const char *s)
 		retval = 1;
 		if (flags & VREADONLY)
 			goto out;
-		if (flags & VUNSET)
-			goto ok;
 		if ((flags & VSTRFIXED) == 0) {
 			INTOFF;
 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
@@ -636,11 +635,10 @@ unsetvar(const char *s)
 			*vpp = vp->next;
 			ckfree(vp);
 			INTON;
-		} else {
+		} else if (!(flags & VUNSET)) {
 			setvar(s, 0, 0);
 			vp->flags &= ~VEXPORT;
 		}
-ok:
 		retval = 0;
 	}
 
author
#16 lore raw

PATCH 3/4 Move unsetvar functionality into setvareq

In reply to Herbert Xu

[VAR] Move unsetvar functionality into setvareq

This patch moves the unsetvar code into setvareq so that we can no have a pathological case of an unset variable hanging around unless it has a bit pinning it like VEXPORT.

---

 ChangeLog |    1 +
 src/var.c |   54 +++++++++++++++++-------------------------------------
 src/var.h |    2 +-
 3 files changed, 19 insertions(+), 38 deletions(-)
ChangeLog +1 -0
index f3c7701..1f7120f 100644
@@ -1,6 +1,7 @@
 2010-05-25  Herbert Xu <[email protected]>
 
 	* Fix poplocalvar leak.
+	* Move unsetvar functionality into setvareq.
 
 2010-05-24  Herbert Xu <[email protected]>
 
src/var.c +16 -34

80 lines not on this page · raw message

author
#17 lore raw

PATCH 4/4 Replace cmdenviron with localvars

In reply to Herbert Xu

[VAR] Replace cmdenviron with localvars

This patch replaces the cmdenviron mechanism for temporary command variables with the localvars mechanism used by functions.

This reduces code size, and more importantly, makes the variable assignment take effect immediately as required by POSIX.

---

 ChangeLog  |    4 ++++
 src/eval.c |   23 +++++++++--------------
 src/eval.h |    1 -
 src/var.c  |   45 ++++++++++++++++++++-------------------------
 src/var.h  |   15 +++++++++++++--
 5 files changed, 46 insertions(+), 42 deletions(-)
ChangeLog +4 -0
index 1f7120f..bf1f13c 100644
@@ -1,3 +1,7 @@
+2010-05-26  Herbert Xu <[email protected]>
+
+	* Replace cmdenviron with localvars.
+
 2010-05-25  Herbert Xu <[email protected]>
 
 	* Fix poplocalvar leak.
src/eval.c +9 -14
index 8d2767c..a6981a9 100644
@@ -77,7 +77,6 @@ static int funcnest;		/* depth of function calls */
 
 
 char *commandname;
-struct strlist *cmdenviron;
 int exitstatus;			/* exit status of last command */
 int back_exitstatus;		/* exit status of backquoted command */
 
@@ -704,6 +703,7 @@ evalcommand(union node *cmd, int flags)
 	/* First expand the arguments. */
 	TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
 	setstackmark(&smark);
+	pushlocalvars();
 	back_exitstatus = 0;
 
 	cmdentry.cmdtype = CMDBUILTIN;
@@ -748,6 +748,8 @@ evalcommand(union node *cmd, int flags)
 		spp = varlist.lastp;
 		expandarg(argp, &varlist, EXP_VARTILDE);
 
+		mklocal((*spp)->text);
+
 		/*
 		 * Modify the command lookup path, if a PATH= assignment
 		 * is present
@@ -835,6 +837,7 @@ bail:
 			if (forkshell(jp, cmd, FORK_FG) != 0) {
 				exitstatus = waitforjob(jp);
 				INTON;
+				poplocalvars(0);
 				break;
 			}
 			FORCEINTON;
@@ -844,17 +847,9 @@ bail:
 		/* NOTREACHED */
 
 	case CMDBUILTIN:
-		cmdenviron = varlist.list;
-		if (cmdenviron) {
-			struct strlist *list = cmdenviron;
-			int i = VNOSET;
-			if (spclbltin > 0 || argc == 0) {
-				i = 0;
-				if (execcmd && argc > 1)
-					i = VEXPORT;
-			}
-			listsetvar(list, i);
-		}
+		poplocalvars(spclbltin > 0 || argc == 0);
+		if (execcmd && argc > 1)
+			listsetvar(varlist.list, VEXPORT);
 		if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
 			int status;
 			int i;
@@ -875,7 +870,7 @@ raise:
 		break;
 
 	case CMDFUNCTION:
-		listsetvar(varlist.list, 0);
+		poplocalvars(1);
 		if (evalfun(cmdentry.u.func, argc, argv, flags))
 			goto raise;
 		break;
@@ -949,7 +944,7 @@ evalfun(struct funcnode *func, int argc, char **argv, int flags)
 	shellparam.optoff = -1;
 	pushlocalvars();
 	evaltree(&func->n, flags & EV_TESTED);
-	poplocalvars();
+	poplocalvars(0);
 funcdone:
 	INTOFF;
 	funcnest--;
src/eval.h +0 -1
index e190b28..ac394e8 100644
@@ -37,7 +37,6 @@
 extern char *commandname;	/* currently executing command */
 extern int exitstatus;		/* exit status of last command */
 extern int back_exitstatus;	/* exit status of backquoted command */
-extern struct strlist *cmdenviron;  /* environment for builtin command */
 
 
 struct backcmd {		/* result of evalbackcmd */
src/var.c +20 -25

87 lines not on this page · raw message

src/var.h +13 -2
index 2bb82b1..ef6d583 100644
@@ -133,14 +133,14 @@ struct strlist;
 void listsetvar(struct strlist *, int);
 char *lookupvar(const char *);
 intmax_t lookupvarint(const char *);
-char *bltinlookup(const char *);
 char **listvars(int, int, char ***);
 #define environment() listvars(VEXPORT, VUNSET, 0)
 int showvars(const char *, int, int);
 int exportcmd(int, char **);
 int localcmd(int, char **);
+void mklocal(char *);
 void pushlocalvars(void);
-void poplocalvars(void);
+void poplocalvars(int);
 int unsetcmd(int, char **);
 void unsetvar(const char *);
 int varcmp(const char *, const char *);
@@ -148,3 +148,14 @@ int varcmp(const char *, const char *);
 static inline int varequal(const char *a, const char *b) {
 	return !varcmp(a, b);
 }
+
+/*
+ * Search the environment of a builtin command.
+ */
+
+static inline char *bltinlookup(const char *name)
+{
+	return lookupvar(name);
+}
+
+
#18 lore raw

Re: [PATCH 4/4] [VAR] Replace cmdenviron with localvars

Quoted 7 lines · Herbert Xu

[VAR] Replace cmdenviron with localvars

This patch replaces the cmdenviron mechanism for temporary command variables with the localvars mechanism used by functions.

This reduces code size, and more importantly, makes the variable assignment take effect immediately as required by POSIX.

This change breaks passing variable assignments to regular builtins. For example,
dash -c 'HOME=/ cd; pwd'
This should print /. (For some reason, this does not work in ksh93 either.)
Or
dash -c 'cd /bin&&CDPATH=/ cd bin; pwd'
This should print /bin twice and not fail any command. (Again this fails in ksh93.)

A more common example,
IFS=: read -r x y z
is already broken in master before this change (due to git commit 55c46b7286f5d9f2d8291158203e2b61d2494420 which effectively replaced bltinlookup("IFS") with ifsval()).

Similar issues may pop up with
PATH=/foo command -V bar
PATH=/foo type bar
LC_NUMERIC=de_DE.UTF-8 printf '%f\n' 1,2

(Note: the latter is broken in bash, but in ksh93 and zsh it works, as well as with an external printf(1) program.)

'local' may need additional magic to avoid making things local to the temporary regular-builtin scope. I suppose only functions should induce scope for 'local', such that things like command eval 'local i' continue to create a variable local to the function.

Signature · 1 line
Jilles Tjoelker
author
#19 lore raw

Re: [PATCH 4/4] [VAR] Replace cmdenviron with localvars

Quoted 6 lines · Jilles Tjoelker

This change breaks passing variable assignments to regular builtins. For example,
dash -c 'HOME=/ cd; pwd'
This should print /. (For some reason, this does not work in ksh93 either.)

Thanks, I have fixed it as follows.

commit 1d806ac1fbafb867f6252e184e1be05c0829ab71

Author: Herbert Xu <[email protected]>
Date:   Thu May 27 11:50:19 2010 +0800

[VAR] Do not poplocalvars prematurely on regular utilities

The recent cmdenviron removal broke regular utilities by calling poplocalvars too early. This patch fixes that by postponing the poplocalvars for regular utilities until they have completed.

In order to ensure that local still works, it is now a special built-in.

ChangeLog +1 -0
index eb12538..4fc35a6 100644
@@ -1,6 +1,7 @@
 2010-05-27  Herbert Xu <[email protected]>
 
 	* Fix poplocalvar on abnormal exit from function.
+	* Do not poplocalvars prematurely on regular utilities.
 
 2010-05-26  Herbert Xu <[email protected]>
 
src/builtins.def.in +1 -1
index 266d0ec..4441fe4 100644
@@ -71,7 +71,7 @@ falsecmd	-u false
 getoptscmd	-u getopts
 hashcmd		hash
 jobscmd		-u jobs
-localcmd	-a local
+localcmd	-as local
 printfcmd	printf
 pwdcmd		pwd
 readcmd		-u read
src/eval.c +5 -3
index 2cd931b..337667f 100644
@@ -847,9 +847,11 @@ bail:
 		/* NOTREACHED */
 
 	case CMDBUILTIN:
-		poplocalvars(spclbltin > 0 || argc == 0);
-		if (execcmd && argc > 1)
-			listsetvar(varlist.list, VEXPORT);
+		if (spclbltin > 0 || argc == 0) {
+			poplocalvars(1);
+			if (execcmd && argc > 1)
+				listsetvar(varlist.list, VEXPORT);
+		}
 		if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
 			int status;
 			int i;

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#20 lore raw

Re: [PATCH 4/4] [VAR] Replace cmdenviron with localvars

Quoted 2 lines · Herbert Xu

In order to ensure that local still works, it is now a special built-in.

Although there are reasons to do this (a 'local' function containing 'command local' would not work, and export, readonly and ksh93's typeset are special as well), consensus seems to be for local to be a regular builtin. posh's local was special for a short time, but this was then reverted.

Nevertheless, it is very nice to have IFS=: read ... working again.

Signature · 1 line
Jilles Tjoelker