From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18092 invoked from network); 10 Jul 2009 22:06:22 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from new-brage.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.254.104) by ns1.primenet.com.au with SMTP; 10 Jul 2009 22:06:22 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 20570 invoked from network); 10 Jul 2009 22:06:14 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 10 Jul 2009 22:06:14 -0000 Received: (qmail 3060 invoked by alias); 10 Jul 2009 22:06:05 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 27126 Received: (qmail 3041 invoked from network); 10 Jul 2009 22:06:04 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 10 Jul 2009 22:06:04 -0000 Received: from mtaout01-winn.ispmail.ntl.com (mtaout01-winn.ispmail.ntl.com [81.103.221.47]) by bifrost.dotsrc.org (Postfix) with ESMTP id 28CC68027106 for ; Sat, 11 Jul 2009 00:06:01 +0200 (CEST) Received: from aamtaout03-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout01-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20090710220600.NRHL6742.mtaout01-winn.ispmail.ntl.com@aamtaout03-winn.ispmail.ntl.com> for ; Fri, 10 Jul 2009 23:06:00 +0100 Received: from pws-pc ([81.107.42.185]) by aamtaout03-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20090710220600.TXEX2093.aamtaout03-winn.ispmail.ntl.com@pws-pc> for ; Fri, 10 Jul 2009 23:06:00 +0100 Date: Fri, 10 Jul 2009 23:05:54 +0100 From: Peter Stephenson To: zsh-workers@sunsite.dk Subject: Re: bug: $? after empty command Message-ID: <20090710230554.5760d429@pws-pc> In-Reply-To: <20090710100228.783de399@news01> References: <20090709154124.2cd2a227@news01> <20090710100228.783de399@news01> X-Mailer: Claws Mail 3.7.1 (GTK+ 2.16.2; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Cloudmark-Analysis: v=1.0 c=1 a=4UtWO5riAAAA:8 a=9vXuR5GGAAAA:8 a=NLZqzBF-AAAA:8 a=Q8LQ1NmjzK-i7Z6DnXsA:9 a=A0ksaaoUDj-N7jt-8LkA:7 a=nge3PSw144Tlxhw3Ydj_mnz0RRQA:4 a=Mo27u2NpdeAA:10 a=Shd8Sdw-9eQA:10 a=eDFNAWYWrCwA:10 a=_dQi-Dcv4p4A:10 X-Virus-Scanned: ClamAV 0.94.2/9554/Fri Jul 10 19:05:16 2009 on bifrost X-Virus-Status: Clean On Fri, 10 Jul 2009 10:02:28 +0100 Peter Stephenson wrote: > On Thu, 9 Jul 2009 21:41:51 +0000 (UTC) > Eric Blake wrote: >> Eric Blake 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 Web page now at http://homepage.ntlworld.com/p.w.stephenson/