vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 92fe606349bf4a0a58ad5521001da271bcc87215
parent a22794b402334e99a245c3565bcc17bc653c308c
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Fri, 19 May 2017 09:58:23 +0200

vis: improve `:<` command implementation

When we have nothing to write to an external process, redirect stdin to
/dev/null instead of using a pipe which is immediately closed.

Some commands change their behavior when used in a shell pipeline.
As an example the following did not work as expected:

 :< ag pattern

Fix #556

Diffstat:
Mvis.c | 16+++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/vis.c b/vis.c @@ -1724,14 +1724,24 @@ int vis_pipe(Vis *vis, File *file, Filerange *range, const char *argv[], exit(EXIT_FAILURE); } - int null = open("/dev/null", O_WRONLY); + int null = open("/dev/null", O_RDWR); if (null == -1) { fprintf(stderr, "failed to open /dev/null"); exit(EXIT_FAILURE); } - if (!interactive) - dup2(pin[0], STDIN_FILENO); + if (!interactive) { + /* If we have nothing to write, let stdin point to + * /dev/null instead of a pipe which is immediately + * closed. Some programs behave differently when used + * in a pipeline. + */ + if (text_range_size(range) == 0) + dup2(null, STDIN_FILENO); + else + dup2(pin[0], STDIN_FILENO); + } + close(pin[0]); close(pin[1]); if (interactive) {