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

[PATCH3] Re: avoid closed stdin() in zle widgets



Even though I can't imagine previous relying on stdin being
closed (and consider myself the previous behaviour a bug),
please find a 3rd version of the patch below with the requested
change:

diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index b65e3be..bd0252f 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -745,22 +745,22 @@ User-defined widgets are defined using `tt(zle -N)', and implemented
 as shell functions.  When the widget is executed, the corresponding
 shell function is executed, and can perform editing (or other) actions.
 It is recommended that user-defined widgets should not have names
 starting with `tt(.)'.
 sect(User-Defined Widgets)
 cindex(widgets, user-defined)
 User-defined widgets, being implemented as shell functions,
 can execute any normal shell command.  They can also run other widgets
-(whether built-in or user-defined) using the tt(zle) builtin command.
-The standard input of the function is closed to prevent external commands
-from unintentionally blocking ZLE by reading from the terminal, but
-tt(read -k) or tt(read -q) can be used to read characters.  Finally,
-they can examine and edit the ZLE buffer being edited by
-reading and setting the special parameters described below.
+(whether built-in or user-defined) using the tt(zle) builtin command. The
+standard input of the function is redirected from /dev/null to prevent
+external commands from unintentionally blocking ZLE by reading from the
+terminal, but tt(read -k) or tt(read -q) can be used to read characters.
+Finally, they can examine and edit the ZLE buffer being edited by reading
+and setting the special parameters described below.
 
 cindex(parameters, editor)
 cindex(parameters, zle)
 These special parameters are always available in widget functions, but
 are not in any way special outside ZLE.  If they have some normal value
 outside ZLE, that value is temporarily inaccessible, but will return
 when the widget function exits.  These special parameters in fact have
 local scope, like parameters created in a function using tt(local).
diff --git a/NEWS b/NEWS
index 568b160..e745750 100644
--- a/NEWS
+++ b/NEWS
@@ -9,16 +9,22 @@ Changes from 5.3.1 to 5.4
 
 The 'exec' and 'command' precommand modifiers, and options to them, are
 now parsed after parameter expansion.  Previously, both the modifier and
 any options to it were parsed between alias expansion and parameter
 expansion (see zshexpn(1)), so they could neither be quoted nor be the
 result of parameter expansion.  Examples: 's=command; $s -V ls' and
 '\command -V ls' now work as expected.
 
+Functions executed by ZLE widgets no longer have they standard input
+closed, but is now redirected from /dev/null instead. That still guards
+against user defined widgets inadvertently reading from the tty device,
+and addresses the antisocial behaviour of running a command with its
+stdin closed.
+
 
 Changes from 5.2 to 5.3.1
 -------------------------
 
 There are only minor compatibility fixes between 5.3 and 5.3.1.
 
 It is possible to enable character width support for Unicode 9 by
 configuring with `--enable-unicode9'; this compiles in some additional
diff --git a/README b/README
index 432a35e..30023da 100644
--- a/README
+++ b/README
@@ -69,16 +69,22 @@ patch-format string, to prevent literal `%' signs in the interpolated
 value from being interpreted as prompt escape sequences.  If you use
 ${vcs_info_msg_0_} in a context other than the shell prompt, you may need
 to undo the escaping with:
 
     print -v vcs_info_msg_0_ -Pr -- "${vcs_info_msg_0_}"
 
 This is also needed if $vcs_info_msg_0_ is used to set $psvar.
 
+4) functions executed by ZLE widgets no longer have they standard input
+closed, but is now redirected from /dev/null instead. That still guards
+against user defined widgets inadvertently reading from the tty device,
+and addresses the antisocial behaviour of running a command with its
+stdin closed.
+
 Incompatibilities between 5.0.8 and 5.3
 ----------------------------------------
 
 1) In character classes delimited by "[" and "]" within patterns, whether
 used for filename generation (globbing) or other forms of pattern
 matching, it used not to be possible to quote "-" when used for a range,
 or "^" and "!" when used for negating a character set.  The characters can
 now be quoted by any of the standard shell means, but note that
diff --git a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c
index 6c271b5..be2b062 100644
--- a/Src/Zle/zle_main.c
+++ b/Src/Zle/zle_main.c
@@ -1480,16 +1480,23 @@ execzlefunc(Thingy func, char **args, int set_bindk)
 	    ret = execimmortal(func, args);
 	} else {
 	    int osc = sfcontext, osi = movefd(0);
 	    int oxt = isset(XTRACE);
 	    LinkList largs = NULL;
 	    int inuse = w->flags & WIDGET_INUSE;
 	    w->flags |= WIDGET_INUSE;
 
+	    if (osi > 0) {
+		/*
+		 * Many commands don't like having a closed stdin, open on
+		 * /dev/null instead
+		 */
+		open("/dev/null", O_RDWR | O_NOCTTY); /* ignore failure */
+	    }
 	    if (*args) {
 		largs = newlinklist();
 		addlinknode(largs, dupstring(w->u.fnnam));
 		while (*args)
 		    addlinknode(largs, dupstring(*args++));
 	    }
 	    startparamscope();
 	    makezleparams(0);



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