vis

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

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

commit 3a379514b6a6f805532c68a27b14c026e29912c3
parent 98dfc31d42a5c428d6f5720ab502447373c79e5f
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 26 Nov 2016 17:23:19 +0100

vis: redirect stdout and stderr streams to /dev/null when lacking a consumer

If the caller of vis_pipe is not interested in the output, redirect it
to /dev/null and close the pipe. Otherwise we would wait for possible
output (which might never arrive) only to throw it away.

As a consequence background processes can now be started with:

 :> { plumber <&3 3<&- & } 3<&0 2>&-

whereas before one also had to explicitly close stdout:

 :> { plumber <&3 3<&- & } 3<&0 1>&- 2>&-

Diffstat:
Mvis.c | 26+++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/vis.c b/vis.c @@ -1297,8 +1297,8 @@ int vis_pipe(Vis *vis, Filerange *range, const char *argv[], * through the external command. */ Text *text = vis->win->file->text; int pin[2], pout[2], perr[2], status = -1; - bool valid_range = text_range_valid(range); - Filerange rout = valid_range ? *range : text_range_new(0, 0); + bool interactive = !text_range_valid(range); + Filerange rout = interactive ? text_range_new(0, 0) : *range; if (pipe(pin) == -1) return -1; @@ -1329,20 +1329,32 @@ int vis_pipe(Vis *vis, Filerange *range, const char *argv[], vis_info_show(vis, "fork failure: %s", strerror(errno)); return -1; } else if (pid == 0) { /* child i.e filter */ - if (valid_range) + int null = open("/dev/null", O_WRONLY); + if (null == -1) { + fprintf(stderr, "failed to open /dev/null"); + exit(EXIT_FAILURE); + } + if (!interactive) dup2(pin[0], STDIN_FILENO); close(pin[0]); close(pin[1]); - if (valid_range) + if (interactive) + dup2(STDERR_FILENO, STDOUT_FILENO); + else if (read_stdout) dup2(pout[1], STDOUT_FILENO); else - dup2(STDERR_FILENO, STDOUT_FILENO); + dup2(null, STDOUT_FILENO); close(pout[1]); close(pout[0]); - if (valid_range) - dup2(perr[1], STDERR_FILENO); + if (!interactive) { + if (read_stderr) + dup2(perr[1], STDERR_FILENO); + else + dup2(null, STDERR_FILENO); + } close(perr[0]); close(perr[1]); + close(null); if (!argv[1]) execlp(vis->shell, vis->shell, "-c", argv[0], (char*)NULL); else