Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 22 Jan 2012 14:00:33 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r230463 - in head: bin/sh tools/regression/bin/sh/builtins
Message-ID:  <201201221400.q0ME0XK4078846@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Sun Jan 22 14:00:33 2012
New Revision: 230463
URL: http://svn.freebsd.org/changeset/base/230463

Log:
  sh: Fix $? in the first command of a 'for'.
  
  In the first command of a 'for', $? should be the exit status of the last
  pipeline (command substitution in the word list or command before 'for'),
  not always 0.

Added:
  head/tools/regression/bin/sh/builtins/for2.0   (contents, props changed)
  head/tools/regression/bin/sh/builtins/for3.0   (contents, props changed)
Modified:
  head/bin/sh/eval.c

Modified: head/bin/sh/eval.c
==============================================================================
--- head/bin/sh/eval.c	Sun Jan 22 13:55:15 2012	(r230462)
+++ head/bin/sh/eval.c	Sun Jan 22 14:00:33 2012	(r230463)
@@ -348,6 +348,7 @@ evalfor(union node *n, int flags)
 	union node *argp;
 	struct strlist *sp;
 	struct stackmark smark;
+	int status;
 
 	setstackmark(&smark);
 	arglist.lastp = &arglist.list;
@@ -357,11 +358,12 @@ evalfor(union node *n, int flags)
 	}
 	*arglist.lastp = NULL;
 
-	exitstatus = 0;
 	loopnest++;
+	status = 0;
 	for (sp = arglist.list ; sp ; sp = sp->next) {
 		setvar(n->nfor.var, sp->text, 0);
 		evaltree(n->nfor.body, flags);
+		status = exitstatus;
 		if (evalskip) {
 			if (evalskip == SKIPCONT && --skipcount <= 0) {
 				evalskip = 0;
@@ -374,6 +376,7 @@ evalfor(union node *n, int flags)
 	}
 	loopnest--;
 	popstackmark(&smark);
+	exitstatus = status;
 }
 
 

Added: head/tools/regression/bin/sh/builtins/for2.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/bin/sh/builtins/for2.0	Sun Jan 22 14:00:33 2012	(r230463)
@@ -0,0 +1,9 @@
+# $FreeBSD$
+
+r=x
+f() { return 42; }
+f
+for i in x; do
+	r=$?
+done
+[ "$r" = 42 ]

Added: head/tools/regression/bin/sh/builtins/for3.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/bin/sh/builtins/for3.0	Sun Jan 22 14:00:33 2012	(r230463)
@@ -0,0 +1,8 @@
+# $FreeBSD$
+
+r=x
+f() { return 42; }
+for i in x`f`; do
+	r=$?
+done
+[ "$r" = 42 ]



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201201221400.q0ME0XK4078846>