zsh-workers
 help / color / mirror / code / Atom feed
* [BUG] 'command' + special built-in exits shell on error
@ 2015-12-23 11:53 Martijn Dekker
  2015-12-25  8:12 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Martijn Dekker @ 2015-12-23 11:53 UTC (permalink / raw)
  To: zsh-workers

Special built-ins may cause the shell to exit on error. Sometimes this
is inconvenient as you cannot test for errors without forking a subshell.

POSIX provides a way around this: the 'command' builtin disables the two
"special built-in" properties of special built-ins, including exiting
from the shell on error.

This means:
    set -o nonexistent_shell_option || echo oops
should produce only an error message and cause the shell to exit, whereas
    command set -o nonexistent_shell_option || echo oops
should output "oops" after the error message and script execution should
continue.

My bug report is that zsh still exits even when 'command' is used, and
even when emulating sh.

bash, ksh93, mksh (as of R51), dash, FreeBSD /bin/sh, NetBSD /bin/sh,
Busybox ash, and yash all do the right thing, suggesting that this
should be fixed in zsh at least in 'emulate sh' mode.

References:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html#tag_20_22
> If the command_name is the same as the name of one of the special
> built-in utilities, the special properties in the enumerated list at
> the beginning of Special Built-In Utilities shall not occur.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_14
> [...] the special built-in utilities described here differ from
> regular built-in utilities in two respects:
> 
> 1.  A syntax error in a special built-in utility may cause a shell
> executing that utility to abort, while a syntax error in a regular
> built-in utility shall not cause a shell executing that utility to
> abort. (See Consequences of Shell Errors for the consequences of
> errors on interactive and non-interactive shells.) If a special
> built-in utility encountering a syntax error does not abort the
> shell, its exit value shall be non-zero.
> 
> 2.  Variable assignments specified with special built-in utilities
> remain in effect after the built-in completes; this shall not be the
> case with a regular built-in or other utility.

Thanks,

- M.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [BUG] 'command' + special built-in exits shell on error
  2015-12-23 11:53 [BUG] 'command' + special built-in exits shell on error Martijn Dekker
@ 2015-12-25  8:12 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2015-12-25  8:12 UTC (permalink / raw)
  To: zsh-workers

On Dec 23, 11:53am, Martijn Dekker wrote:
}
} My bug report is that zsh still exits even when 'command' is used, and
} even when emulating sh.

When not emulating sh, "command" can't be used in front of a special
builtin at all -- in zsh mode it always forces the command to be NOT
built-in, i.e., it's implicitly "command -p".

Consequently we only care about POSIXBUILTINS mode.


diff --git a/Src/exec.c b/Src/exec.c
index acc867c..f14fc27 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3475,10 +3475,10 @@ execcmd(Estate state, int input, int output, int how, int last1)
 	    restore_queue_signals(q);
 	} else if (is_builtin || is_shfunc) {
 	    LinkList restorelist = 0, removelist = 0;
+	    int do_save = 0;
 	    /* builtin or shell function */
 
-	    if (!forked && varspc) {
-		int do_save = 0;
+	    if (!forked) {
 		if (isset(POSIXBUILTINS)) {
 		    /*
 		     * If it's a function or special builtin --- save
@@ -3497,7 +3497,7 @@ execcmd(Estate state, int input, int output, int how, int last1)
 		    if ((cflags & BINF_COMMAND) || !assign)
 			do_save = 1;
 		}
-		if (do_save)
+		if (do_save && varspc)
 		    save_params(state, varspc, &restorelist, &removelist);
 	    }
 	    if (varspc) {
@@ -3643,6 +3643,8 @@ execcmd(Estate state, int input, int output, int how, int last1)
 		}
 		dont_queue_signals();
 		lastval = execbuiltin(args, assigns, (Builtin) hn);
+		if (do_save & BINF_COMMAND)
+		    errflag &= ~ERRFLAG_ERROR;
 		restore_queue_signals(q);
 		fflush(stdout);
 		if (save[1] == -2) {


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-12-25  8:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-23 11:53 [BUG] 'command' + special built-in exits shell on error Martijn Dekker
2015-12-25  8:12 ` Bart Schaefer

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