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 ]
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to