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

Re: sh compatibility issue



On Sun, 20 Feb 2011 19:11:59 +0000
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Sun, 20 Feb 2011 00:05:40 +0100
> Jilles Tjoelker <jilles@xxxxxxxx> wrote:
> > On a related note, here is another quite insidious sh compatibility
> > issue:
> >   sh -c 'exec </nonexistent/a; echo wrong'
> > This should not print "wrong" because exec is a special builtin and
> > redirection errors on special builtins are fatal. Most shells get this
> > right nowadays (bash only in POSIX mode) but zsh gets it wrong. Even
> >   set -o posixbuiltins
> > does not help.
> 
> I think it does need to be fatal, but not because it's a special builtin
> --- it looks like that's only a "may" rather than a "shall" --- but
> because there is a rule for exec:
> 
>   If a redirection error occurs (see Consequences of Shell Errors ), the
>   shell shall exit with a value in the range 1-125
> 
> which is certainly unambiguous on the subject.  This will need looking at.

I think this is unproblematic.  The relevant hooks are already mostly
there.

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.99
diff -p -u -r1.99 options.yo
--- Doc/Zsh/options.yo	16 Dec 2010 13:55:35 -0000	1.99
+++ Doc/Zsh/options.yo	20 Feb 2011 20:11:45 -0000
@@ -1866,6 +1870,10 @@ tt(source),
 tt(times),
 tt(trap) and
 tt(unset).
+
+In addition, a failed redirection after tt(exec) causes a non-interactive
+shell to exit and an interactive shell to return to its top-level
+processing.
 )
 pindex(POSIX_IDENTIFIERS)
 pindex(NO_POSIX_IDENTIFIERS)
Index: Test/A04redirect.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A04redirect.ztst,v
retrieving revision 1.15
diff -p -u -r1.15 A04redirect.ztst
--- Test/A04redirect.ztst	14 Sep 2010 14:46:26 -0000	1.15
+++ Test/A04redirect.ztst	20 Feb 2011 20:11:45 -0000
@@ -366,3 +366,15 @@
 >more output:
 >This file contains data.
 >This file contains data.
+
+  $ZTST_testdir/../Src/zsh -fc 'exec >/nonexistent/nonexistent
+  echo output'
+0:failed exec redir, no POSIX_BUILTINS
+>output
+?zsh:1: no such file or directory: /nonexistent/nonexistent
+
+  $ZTST_testdir/../Src/zsh -f -o POSIX_BUILTINS -c '
+  exec >/nonexistent/nonexistent
+  echo output'
+1:failed exec redir, POSIX_BUILTINS
+?zsh:2: no such file or directory: /nonexistent/nonexistent
Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.188
diff -p -u -r1.188 exec.c
--- Src/exec.c	17 Feb 2011 19:52:42 -0000	1.188
+++ Src/exec.c	20 Feb 2011 20:11:46 -0000
@@ -181,7 +181,15 @@ struct execstack *exstack;
 /**/
 mod_export Funcstack funcstack;
 
-#define execerr() if (!forked) { lastval = 1; goto done; } else _exit(1)
+#define execerr()				\
+    do {					\
+	if (!forked) {				\
+	    redir_err = lastval = 1;		\
+	    goto done;				\
+	} else {				\
+	    _exit(1);				\
+	}					\
+    } while (0)
 
 static int doneps4;
 static char *STTYval;
@@ -2312,7 +2320,7 @@ execcmd(Estate state, int input, int out
     struct multio *mfds[10];
     char *text;
     int save[10];
-    int fil, dfil, is_cursh, type, do_exec = 0, i, htok = 0;
+    int fil, dfil, is_cursh, type, do_exec = 0, redir_err = 0, i, htok = 0;
     int nullexec = 0, assign = 0, forked = 0;
     int is_shfunc = 0, is_builtin = 0, is_exec = 0, use_defpath = 0;
     /* Various flags to the command. */
@@ -3289,6 +3297,13 @@ execcmd(Estate state, int input, int out
     fixfds(save);
 
  done:
+    if (redir_err && isset(POSIXBUILTINS)) {
+	if (!isset(INTERACTIVE)) {
+	    /* We've already _exit'ed if forked */
+	    exit(1);
+	}
+	errflag = 1;
+    }
     if (newxtrerr) {
 	fil = fileno(newxtrerr);
 	fclose(newxtrerr);


-- 
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