Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: "continue -1" confuses zsh



On Sat, 30 Aug 2008 21:25:35 +0200
Frank Terbeck <ft@xxxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author