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

Re: Parse errors don't cause a non-zero exit code?



Dan Nelson wrote:
> In the last episode (Mar 29), Dan Nelson said:
> > $ echo "'" > /tmp/test ; zsh -f /tmp/test ; echo $?
> > /tmp/test:2: unmatched '
> > 0
> > 
> > ksh93, ash, and bash all return non-zero here, and it looks like
> > http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag
> _02_08
> > wants a non-zero result also.
> 
> I'm thinking something like this, but I don't know enough about zsh
> internals to say that propagating errflag into lastval here is the
> right fix.  It doesn't break the testsuite at least:

I came up with this.  It uses the LEXERR to signal the error rather than
propagating errflag.

I found there was another case which still didn't return non-zero
status, when the same code came from stdin.  That needed handling at the
next level down.

I've added tests for both.

Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.49
diff -u -r1.49 init.c
--- Src/init.c	31 Mar 2005 09:54:59 -0000	1.49
+++ Src/init.c	1 Apr 2005 10:02:41 -0000
@@ -131,6 +131,8 @@
 		(tok == LEXERR && (!isset(SHINSTDIN) || !toplevel)) ||
 		justonce)
 		break;
+	    if (tok == LEXERR && !lastval)
+		lastval = 1;
 	    continue;
 	}
 	if (hend(prog)) {
@@ -1282,6 +1284,9 @@
 	    loop(1,0);
 	while (tok != ENDINPUT && (tok != LEXERR || isset(SHINSTDIN)));
 	if (tok == LEXERR) {
+	    /* Make sure a parse error exits with non-zero status */
+	    if (!lastval)
+		lastval = 1;
 	    stopmsg = 1;
 	    zexit(lastval, 0);
 	}
Index: Test/A01grammar.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A01grammar.ztst,v
retrieving revision 1.9
diff -u -r1.9 A01grammar.ztst
--- Test/A01grammar.ztst	22 Jun 2004 13:10:02 -0000	1.9
+++ Test/A01grammar.ztst	1 Apr 2005 10:02:41 -0000
@@ -7,6 +7,7 @@
   mkdir basic.tmp && cd basic.tmp
 
   touch foo bar
+  echo "'" >unmatched_quote.txt
 
 %test
 #
@@ -439,3 +440,11 @@
   done || print no
 0:Handling of &&'s and ||'s with a for loop in between
 >no
+
+  $ZTST_testdir/../Src/zsh -f unmatched_quote.txt
+1:Parse error with file causes non-zero exit status
+?unmatched_quote.txt:2: unmatched '
+
+  $ZTST_testdir/../Src/zsh -f <unmatched_quote.txt
+1:Parse error on standard input causes non-zero exit status
+?zsh: unmatched '

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************



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