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

Re: stdbuf -o0 -i0 via a Zsh native interface?



> On 25/05/2023 12:21 Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> I'm using coproc to continuously read command's output, to have the
> effect of `CMD | fzf` – i.e. of instantly available contents while
> reading all of it eventually, However, I've had problem with buffering
> – no output from the CMD in `coproc CMD` was available for a long
> time, and I've found that `coproc { stdbuf -o0 -i0 CMD; }` helps.
> 
> However, this is sub-optimal solution because I have to hideously
> prepend the stdbuf command before each user CMD, meaning that entering
> builtin commands will not work (e.g.: `stdbuf -i0 -o0 print smthg`).
> 
> I wonder if the infamous buffering problem, solved by the hacky
> ld-preload stdbuf program could be fixed on the level of Zsh? Like,
> e.g.: special array, say: zsh_buffer=( 0 0 0 )? For stdin, stdout and
> stderr buffers.

Something like this?  It looks like a good fit for zsystem.

This is deliberately fairly restricted functionality; anything more would
be starting to be a support headache, but up to this point it ought to be
very much WYSIWYG.

pws

diff --git a/Doc/Zsh/mod_system.yo b/Doc/Zsh/mod_system.yo
index e25201faa..73611540f 100644
--- a/Doc/Zsh/mod_system.yo
+++ b/Doc/Zsh/mod_system.yo
@@ -228,6 +228,13 @@ If the option tt(-r) is given, the lock is only for reading, otherwise
 it is for reading and writing.  The file descriptor is opened
 accordingly.
 )
+item(tt(zsystem setbuffer) var(fd) tt(L)var(|)tt(N))(
+Set the file descriptor var(fd), which may be 0, 1 or 2, to
+either line buffering (tt(L)) or no buffering (tt(N)).  It is
+not possible to set an explicit buffer.  Caution should be
+exercised as the resulting mode may not be entirely compatible
+with normal shell operation.
+)
 item(tt(zsystem supports) var(subcommand))(
 The builtin tt(zsystem)'s subcommand tt(supports) tests whether a
 given subcommand is supported.  It returns status 0 if so, else
diff --git a/Src/Modules/system.c b/Src/Modules/system.c
index 929a8b002..033c84d27 100644
--- a/Src/Modules/system.c
+++ b/Src/Modules/system.c
@@ -772,6 +772,53 @@ bin_zsystem_flock(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
 }
 
 
+/*
+ * Set up buffering on a given file descriptor.
+ */
+/**/
+static int
+bin_zsystem_setbuffer(char *nam, char **args,
+		      UNUSED(Options ops), UNUSED(int func))
+{
+    int fd;
+    FILE *strm;
+
+    if (!args[0] || !args[1]) {
+	zwarnnam(nam, "setbuffer: not enough arguemnts");
+	return 1;
+    }
+    if (args[2]) {
+	zwarnnam(nam, "setbuffer: too many arguments");
+	return 1;
+    }
+
+    fd = getposint(args[0], nam);
+    if (fd < 0)
+	return 1;
+    if (fd > 2) {
+	zwarnnam(nam, "setbuffer: file descriptor 0, 1 or 2 expected");
+	return 1;
+    }
+    strm = (fd == 0) ? stdin : (fd == 1) ? stdout : stderr;
+
+    if (!strcmp(args[1], "L")) {
+	if (setvbuf(strm, NULL, _IOLBF, 0)) {
+	    zwarnnam(nam, "setting line buffer failed: %e");
+	    return 1;
+	}
+    } else if (!strcmp(args[1], "N")) {
+	if (setvbuf(strm, NULL, _IONBF, 0)) {
+	    zwarnnam(nam, "setting no buffer failed: %e");
+	    return 1;
+	}
+    } else {
+       zwarnanm(nam, "setbuffer: argument L or N expected");
+       return 1;
+    }
+
+    return 0;
+}
+
 /*
  * Return status zero if the zsystem feature is supported, else 1.
  * Operates silently for future-proofing.
@@ -808,6 +855,8 @@ bin_zsystem(char *nam, char **args, Options ops, int func)
     /* If more commands are implemented, this can be more sophisticated */
     if (!strcmp(*args, "flock")) {
 	return bin_zsystem_flock(nam, args+1, ops, func);
+    } else if (!strcmp(*args, "setbuffer")) {
+	return bin_zsystem_setbuffer(nam, args+1, ops, func);
     } else if (!strcmp(*args, "supports")) {
 	return bin_zsystem_supports(nam, args+1, ops, func);
     }




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