zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-workers@sunsite.dk
Subject: Re: bug: $? after empty command
Date: Fri, 10 Jul 2009 23:05:54 +0100	[thread overview]
Message-ID: <20090710230554.5760d429@pws-pc> (raw)
In-Reply-To: <20090710100228.783de399@news01>

On Fri, 10 Jul 2009 10:02:28 +0100
Peter Stephenson <pws@csr.com> wrote:
> On Thu, 9 Jul 2009 21:41:51 +0000 (UTC)
> Eric Blake <ebb9@byu.net> wrote:
>> Eric Blake <ebb9 <at> byu.net> writes:
>> 
>>> But just from inspection,
>>> it looks like it does not cover these related issues, which are
>>> both required by POSIX to output 0:
>>> 
>>> $ zsh -c 'false; . /dev/null; echo $?'
>>> 1
>>> $ zsh -c 'false; ``; echo $?'
>>> 1
>> 
>> And another one that POSIX requires to output 0:
>> 
>> $ zsh -c 'false; sleep& echo $?'
>> 1
> 
> Also, I don't think it affects POSIX, because empty functions aren't
> required to work, but in zsh, "fn() { }" is valid, and I suspect "fn"
> logically ought to reset the status, too---basically anywhere where you've
> run a complex command with an empty list of commands in it.

These four were all fairly easy; in each case there is a local context
where the command in question is being executed in which the status can
be reset.  There may be a a more generic place, such as execlist(), for
these to go, but I like the transparency of resetting them locally;
execlist()/execode() are used in just too many places.  However, that's
a bit craven.

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.166
diff -u -r1.166 exec.c
--- Src/exec.c	10 Jul 2009 11:08:48 -0000	1.166
+++ Src/exec.c	10 Jul 2009 21:59:52 -0000
@@ -1372,7 +1372,8 @@
 	else
 	    spawnjob();
 	child_unblock();
-	return 0;
+	/* Executing background code resets shell status */
+	return lastval = 0;
     } else {
 	if (newjob != lastwj) {
 	    Job jn = jobtab + newjob;
@@ -3512,6 +3513,7 @@
 	return retval;
     }
     /* pid == 0 */
+    lastval = 0;		/* status of empty list is zero */
     child_unblock();
     zclose(pipes[0]);
     redup(pipes[1], 1);
@@ -4259,6 +4261,7 @@
     if (trap_state == TRAP_STATE_PRIMED)
 	trap_return--;
     oldlastval = lastval;
+    lastval = 0;		/* status of empty function is zero */
     oldnumpipestats = numpipestats;
     if (noreturnval) {
 	/*
Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.103
diff -u -r1.103 init.c
--- Src/init.c	9 Jul 2009 19:42:20 -0000	1.103
+++ Src/init.c	10 Jul 2009 21:59:52 -0000
@@ -1131,6 +1131,7 @@
     fstack.tp = FS_SOURCE;
     funcstack = &fstack;
 
+    lastval = 0;		/* status of empty file is zero */
     if (prog) {
 	pushheap();
 	errflag = 0;
Index: Test/A01grammar.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A01grammar.ztst,v
retrieving revision 1.23
diff -u -r1.23 A01grammar.ztst
--- Test/A01grammar.ztst	10 Jul 2009 11:08:48 -0000	1.23
+++ Test/A01grammar.ztst	10 Jul 2009 21:59:52 -0000
@@ -27,6 +27,18 @@
   $nonexistent_variable
 0:Executing command that evaluates to empty resets status
 
+  false
+  sleep 1 &
+  print $?
+  # a tidy test is a happy test
+  wait $!
+0:Starting background command resets status
+>0
+
+  false
+  . /dev/null
+0:Sourcing empty file resets status
+
   fn() { local foo; read foo; print $foo; }
   coproc fn
   print -p coproc test output
@@ -531,3 +543,13 @@
   . ./bad_syntax
 126: Attempt to "." file with bad syntax.
 ?./bad_syntax:2: parse error near `\n'
+
+  echo 'false' >dot_false
+  . ./dot_false
+  print $?
+  echo 'true' >dot_true
+  . ./dot_true
+  print $?
+0:Last status of successfully executed "." file is retained
+>1
+>0
Index: Test/C04funcdef.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C04funcdef.ztst,v
retrieving revision 1.5
diff -u -r1.5 C04funcdef.ztst
--- Test/C04funcdef.ztst	29 Dec 2008 04:24:36 -0000	1.5
+++ Test/C04funcdef.ztst	10 Jul 2009 21:59:52 -0000
@@ -1,5 +1,20 @@
 %test
 
+  fn1() { return 1; }
+  fn2() {
+      fn1
+      print $?
+      return 2
+  }
+  fn2
+2:Basic status returns from functions
+>1
+
+  fnz() { }
+  false
+  fnz
+0:Empty function body resets status
+
   function f$$ () {
     print regress expansion of function names
   }
Index: Test/D08cmdsubst.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D08cmdsubst.ztst,v
retrieving revision 1.2
diff -u -r1.2 D08cmdsubst.ztst
--- Test/D08cmdsubst.ztst	9 Dec 2007 23:53:33 -0000	1.2
+++ Test/D08cmdsubst.ztst	10 Jul 2009 21:59:52 -0000
@@ -89,3 +89,7 @@
   X=$(exit 2) $(exit 0) || print $?
 0:variable assignments processed after other substitutions
 >2
+
+ false
+ ``
+0:Empty command substitution resets status


-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


  reply	other threads:[~2009-07-10 22:06 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-09 13:12 Eric Blake
2009-07-09 14:41 ` Peter Stephenson
2009-07-09 21:17   ` Eric Blake
2009-07-09 21:41     ` Eric Blake
2009-07-10  9:02       ` Peter Stephenson
2009-07-10 22:05         ` Peter Stephenson [this message]
2009-07-11 16:39           ` Peter Stephenson
2009-07-11 18:43             ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090710230554.5760d429@pws-pc \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).