From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22659 invoked by alias); 13 Oct 2016 09:30:43 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 39625 Received: (qmail 28457 invoked from network); 13 Oct 2016 09:30:43 -0000 X-Qmail-Scanner-Diagnostics: from kahlil.inlv.org by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(37.59.109.123):SA:0(-0.3/5.0):. Processed in 0.514646 secs); 13 Oct 2016 09:30:43 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=RP_MATCHES_RCVD autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: martijn@inlv.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: none (ns1.primenet.com.au: domain at inlv.org does not designate permitted sender hosts) Subject: Re: 'case' zeroes "$?" To: Zsh hackers list References: <3493e3d3-e578-31d1-27de-06d8d0c35944@inlv.org> <20161010095522.5778c2fd@pwslap01u.europe.root.pri> From: Martijn Dekker Message-ID: <3131288f-1b9e-b3bc-8ac5-f71260e84033@inlv.org> Date: Thu, 13 Oct 2016 11:30:32 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <20161010095522.5778c2fd@pwslap01u.europe.root.pri> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Op 10-10-16 om 10:55 schreef Peter Stephenson: > On Sun, 9 Oct 2016 23:01:35 +0200 > Martijn Dekker wrote: >> > Unlike on other shells, 'case' on zsh zeroes "$?" before executing a >> > command within 'case', making it more inconvenient to test exit status. >> > >> > Example: >> > >> > (exit 37) || case $? in 37) echo "$?";; esac > This looks straightforward. Perhaps not quite. The patch has introduced a new bug: instead of prematurely resetting the exit status, it's now not reset at all. Test script: false case stuff in (nomatch) foo ;; esac echo $? Expected output: 0 Actual output: 1 Reference: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_05 "The exit status of case shall be zero if no patterns are matched. Otherwise, the exit status shall be the exit status of the last command executed in the compound-list." So we need a flag to remember if any patterns are matched. Let me try my hand at a patch for this: diff --git a/Src/loop.c b/Src/loop.c index 94b61b7..0b9ab15 100644 --- a/Src/loop.c +++ b/Src/loop.c @@ -584,7 +584,7 @@ execcase(Estate state, int do_exec) Wordcode end, next; wordcode code = state->pc[-1]; char *word, *pat; - int npat, save, nalts, ialt, patok; + int npat, save, nalts, ialt, patok, anypatok; Patprog *spprog, pprog; end = state->pc + WC_CASE_SKIP(code); @@ -592,6 +592,7 @@ execcase(Estate state, int do_exec) word = ecgetstr(state, EC_DUP, NULL); singsub(&word); untokenize(word); + anypatok = 0; cmdpush(CS_CASE); while (state->pc < end) { @@ -648,7 +649,7 @@ execcase(Estate state, int do_exec) *spprog = pprog; } if (pprog && pattry(pprog, word)) - patok = 1; + patok = anypatok = 1; state->pc += 2; nalts--; } @@ -679,6 +680,9 @@ execcase(Estate state, int do_exec) state->pc = end; + if (!anypatok) + lastval = 0; + return lastval; } diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst index 0e77f3e..e4b6870 100644 --- a/Test/A01grammar.ztst +++ b/Test/A01grammar.ztst @@ -765,6 +765,23 @@ 0:case retains exit status for execution of cases >37 + false + case stuff in + (nomatch) foo + ;; + esac + echo $? +0:case sets exit status to zero if no patterns are matched +>0 + + case match in + (match) true; false; (exit 37) + ;; + esac + echo $? +0:case keeps exit status of last command executed in compound-list +>37 + x=1 x=2 | echo $x echo $x