From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: stdbuf -o0 -i0 via a Zsh native interface?
Date: Thu, 25 May 2023 15:16:07 +0100 (BST) [thread overview]
Message-ID: <1297534493.4668003.1685024167868@mail.virginmedia.com> (raw)
In-Reply-To: <CAKc7PVAh95GL_kx1FiACaexEHsK8PVLrJ+T4KxnKTZr=5KDXMw@mail.gmail.com>
> On 25/05/2023 12:21 Sebastian Gniazdowski <sgniazdowski@gmail.com> 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);
}
next prev parent reply other threads:[~2023-05-25 14:16 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-25 11:21 Sebastian Gniazdowski
2023-05-25 14:16 ` Peter Stephenson [this message]
2023-05-25 18:35 ` Bart Schaefer
2023-05-25 19:02 ` Sebastian Gniazdowski
2023-05-25 19:09 ` Bart Schaefer
2023-05-25 19:28 ` Sebastian Gniazdowski
2023-05-25 21:21 ` Bart Schaefer
2023-05-26 2:17 ` Sebastian Gniazdowski
2023-05-26 10:23 ` Sebastian Gniazdowski
2023-05-26 9:30 ` Roman Perepelitsa
2023-05-26 20:52 ` Bart Schaefer
2023-05-30 11:17 ` Peter Stephenson
2023-05-31 12:38 ` Sebastian Gniazdowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1297534493.4668003.1685024167868@mail.virginmedia.com \
--to=p.w.stephenson@ntlworld.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).