zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Hackers' List <zsh-workers@sunsite.dk>
Subject: Re: "continue -1" confuses zsh
Date: Sun, 31 Aug 2008 14:23:39 +0100	[thread overview]
Message-ID: <20080831142339.300a0003@pws-pc> (raw)
In-Reply-To: <20080830192535.GR6330@fsst.voodoo.lan>

On Sat, 30 Aug 2008 21:25:35 +0200
Frank Terbeck <ft@bewatermyfriend.org> wrote:
> > I don't know what the actual reason for this reaction is, but:
> > 
> > As per SUSv3 for break and continue:
> > 
> > EXIT STATUS
> >     0  Successful completion.
> >    >0  The n value was not an unsigned decimal integer greater than or
> >        equal to 1.

(Moved to zsh-workers.)

Thanks, although I think we might take the opportunity to be compatible
and actually raise an error (with a message) on an invalid loop
counter, which will also pick up any ocurrences of "continue 0" that
previously would have been ignored with status 0.

I've added a few basic tests for control commands, too.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.202
diff -u -r1.202 builtin.c
--- Src/builtin.c	7 Aug 2008 16:25:16 -0000	1.202
+++ Src/builtin.c	31 Aug 2008 13:19:00 -0000
@@ -4443,6 +4443,11 @@
 	nump = 1;
     }
 
+    if (nump > 0 && (func == BIN_CONTINUE || func == BIN_BREAK) && num <= 0) {
+	zerrnam(name, "argument is not positive: %d", num);
+	return 1;
+    }
+
     switch (func) {
     case BIN_CONTINUE:
 	if (!loops) {   /* continue is only permitted in loops */
Index: Test/.distfiles
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/.distfiles,v
retrieving revision 1.22
diff -u -r1.22 .distfiles
--- Test/.distfiles	11 Aug 2008 19:22:54 -0000	1.22
+++ Test/.distfiles	31 Aug 2008 13:19:02 -0000
@@ -7,6 +7,7 @@
 A04redirect.ztst
 A05execution.ztst
 A06assign.ztst
+A07control.ztst
 B01cd.ztst
 B02typeset.ztst
 B03print.ztst
Index: Test/A07control.ztst
===================================================================
RCS file: Test/A07control.ztst
diff -N Test/A07control.ztst
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Test/A07control.ztst	31 Aug 2008 13:19:02 -0000
@@ -0,0 +1,112 @@
+# Test control commands for loops and functions.
+
+%test
+
+  fn3() { return $1; print Error }
+  fn2() { fn3 $1 }
+  fn() {
+    print start $1
+    fn2 $1
+    return
+    print Error
+  }
+  for val in -1 0 1 255; do
+    fn $val; print $?
+  done
+0:Passing of return values back through functions
+>start -1
+>-1
+>start 0
+>0
+>start 1
+>1
+>start 255
+>255
+
+  fn() {
+    continue
+  }
+  fn
+1:continue outside loop
+?fn:continue:1 not in while, until, select, or repeat loop
+
+  for outer in 0 1 2 3; do
+    print outer $outer
+    for inner in 0 1 2 3; do
+      print inner $inner
+      continue $(( (outer & 1) ? 2 : 1 ))
+      print error
+    done
+    print outer end
+  done
+0:continue with valid argument
+>outer 0
+>inner 0
+>inner 1
+>inner 2
+>inner 3
+>outer end
+>outer 1
+>inner 0
+>outer 2
+>inner 0
+>inner 1
+>inner 2
+>inner 3
+>outer end
+>outer 3
+>inner 0
+
+  for outer in 0 1; do
+    continue 0
+    print -- $outer got here, status $?
+  done
+1:continue error case 0
+?(eval):continue:2: argument is not positive: 0
+
+  for outer in 0 1; do
+    continue -1
+    print -- $outer got here, status $?
+  done
+1:continue error case -1
+?(eval):continue:2: argument is not positive: -1
+
+  fn() {
+    break
+  }
+  for outer in 0 1; do
+    print $outer
+    fn
+  done
+0:break from within function (this is a feature, I disovered)
+>0
+
+  for outer in 0 1 2 3; do
+    print outer $outer
+    for inner in 0 1 2 3; do
+      print inner $inner
+      break $(( (outer & 1) ? 2 : 1 ))
+      print error
+    done
+    print outer end
+  done
+0:break with valid argument
+>outer 0
+>inner 0
+>outer end
+>outer 1
+>inner 0
+
+  for outer in 0 1; do
+    break 0
+    print -- $outer got here, status $?
+  done
+1:break error case 0
+?(eval):break:2: argument is not positive: 0
+
+  for outer in 0 1; do
+    break -1
+    print -- $outer got here, status $?
+  done
+1:break error case -1
+?(eval):break:2: argument is not positive: -1


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


           reply	other threads:[~2008-08-31 13:24 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20080830192535.GR6330@fsst.voodoo.lan>]

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=20080831142339.300a0003@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).