From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 12362 invoked from network); 25 May 2023 14:16:35 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 25 May 2023 14:16:35 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1685024195; b=Rg4cl9+AsEY4FuyQo/LwR2tCrL3q64f1fMfmfdK/Sj57QHzTXvgd3NuOKMPnkmFcs50UkBvSMt aWpiAvJcNPTlwlArzYnK0cuaEejol3Iv6ir2ptHNH8B0DuE2HCw2abZS3hCsbb0SUddIVr93Nb Fdih7N2nHBuNspzApYIHqgcjcN/NR4SZHjhjaouhffvGxJb65fjHYd6ozLYPVhLj4Ukmr/0TfF 6iUkjmQq2rdMK/DsM6lz5HWgCoMuMJWq6fsXwbofdhmcLDiujfu+tMdmNe1dxizgIk/XuEFOVL VpJ3Kl1qIoYmGMQKwiOk9GgRFm2gIqsJVGm2uF+5XnDSFQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (smtpq2.tb.ukmail.iss.as9143.net) smtp.remote-ip=212.54.57.97; dkim=pass header.d=ntlworld.com header.s=meg.feb2017 header.a=rsa-sha256; dmarc=pass header.from=ntlworld.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1685024195; bh=Jp+RSDNrdqMBobFbcZ2Z9JWEeLN2HY/3fH+nlhAiLKw=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject: References:In-Reply-To:Message-ID:To:From:Date:DKIM-Signature: DKIM-Signature; b=ACgJdgKVh89UuUuQ/tAHxxRvQHHhUdLWyEeUmw6cvscgaU49NAW8aVBtBsYqVVMRaC3XrJnUm6 CWzwaXQEnIXQ1cWv/f98oMqqURvW+yQluyUTJiHQRqPLh4rh2Vpf1d4qUHKoYgtijtRtwm+iuE THGassbXUBMtK6fM56wk3SvWA2B0XkPwZcNQfFPN4BR62LzWkMMEF01rWTmsgEXJmPJoQDoP7z 6PLeAZmQ41zgH305OJ20TSU5ukB3PuWIsNaA8zJ4dwg+S93BozHHk8rDIyHtvNAzg7kFCIrGCg CXeW+LgA1FQs3PG7o5i2k2FOwtAhqTsOWqbE1PYG9GwmjA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:References:In-Reply-To:Message-ID:To:From: Date:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=jEhq33LCKTiYG0Kh9XIRm+bDv5HUvWnGJpLwuR+4s4I=; b=SbL9OoJTLusMDdgkhsTswJqzMq d4PtWCNQ513HAGSFlXD0gRNVhSSkNJJOiBZ2kqrIDUwmj5TvDAwMUgpWhZg2P/zlTpjzFGcSeLb5+ p2Qkgdq6H5VEUHpCjqUdv2/SILFwghXhCJQzQWCpFv7k4BxcXYPfrd80EV/6cLwoEB9d9Py8LeK0A O0J2EcBN4+GmDnm9ESC06OIMXpmQFr16mZVtpAaMfgpD+UsgYpvd57RZSc6TvyygsRXjUelpocT8t 5b9CTIT0beKsMfTOeiK7JW2IyknYhPowozzXUD/w7wM/P3FGI40MGSfs/mtGQXE9jinelTtuzqiBK GIJ7J3cA==; Received: by zero.zsh.org with local id 1q2Blb-000PCB-De; Thu, 25 May 2023 14:16:35 +0000 Authentication-Results: zsh.org; iprev=pass (smtpq2.tb.ukmail.iss.as9143.net) smtp.remote-ip=212.54.57.97; dkim=pass header.d=ntlworld.com header.s=meg.feb2017 header.a=rsa-sha256; dmarc=pass header.from=ntlworld.com; arc=none Received: from smtpq2.tb.ukmail.iss.as9143.net ([212.54.57.97]:52474) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1q2BlA-000Osy-Ko; Thu, 25 May 2023 14:16:16 +0000 Received: from [212.54.57.80] (helo=smtp1.tb.ukmail.iss.as9143.net) by smtpq2.tb.ukmail.iss.as9143.net with esmtp (Exim 4.90_1) (envelope-from ) id 1q2Bl9-00044i-VI for zsh-workers@zsh.org; Thu, 25 May 2023 16:16:07 +0200 Received: from oxbe12.tb.ukmail.iss.as9143.net ([172.25.160.143]) by smtp1.tb.ukmail.iss.as9143.net with ESMTP id 2Bl9qGSsdhvLN2Bl9qeBlO; Thu, 25 May 2023 16:16:07 +0200 X-Env-Mailfrom: p.w.stephenson@ntlworld.com X-Env-Rcptto: zsh-workers@zsh.org X-SourceIP: 172.25.160.143 X-CNFS-Analysis: v=2.4 cv=SZoxNNdu c=1 sm=1 tr=0 ts=646f6da7 cx=a_exe a=pVlFXI3Q25jgZXAaIqG4JA==:117 a=1DWFKdCB1IcA:10 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=iGElUXweiC_9usx1AekA:9 a=QEXdDO2ut3YA:10 a=75Kt_H3ikK-EkTT1woid:22 X-Authenticated-Sender: p.w.stephenson@ntlworld.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ntlworld.com; s=meg.feb2017; t=1685024167; bh=Jp+RSDNrdqMBobFbcZ2Z9JWEeLN2HY/3fH+nlhAiLKw=; h=Date:From:To:In-Reply-To:References:Subject; b=Tgw22GLHbGdg5TFdOAaj9c0pl14EysGXlsWnidbVhXF6EEdLaQ8tLMnUHwF9eUhdF 0/4WSOscTSzXwhWIcydfxYREGF+7LmoLKg9dc/C+hhLJbaMjn1ZYhHyg9+6T6d+8cr p/JfToItIoCnC6o+kT4NwPsbjOBlsPoTbdYe2U3wVQesssirgc/WtlicEqQ9Itbmky U+fhYeXHnTU67skpsrD9kWCZshe2BKTAJSZhpht3zJfRSOWodq/f0IvZhnPdJUlTH8 Vtt/hK5Za0CCShjBhtdT/n2POjWLA0eSQ94ywuZUVpk6fw6i1d4r3vty133KL+uNo1 Fbgo0ZhbFNSRQ== Date: Thu, 25 May 2023 15:16:07 +0100 (BST) From: Peter Stephenson To: Zsh hackers list Message-ID: <1297534493.4668003.1685024167868@mail.virginmedia.com> In-Reply-To: References: Subject: Re: stdbuf -o0 -i0 via a Zsh native interface? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer X-Originating-IP: 147.161.224.167 X-Originating-Client: open-xchange-appsuite X-CMAE-Envelope: MS4xfFXNNrmWX4fZ7Rw8RK5FQN7Hz1wtXEI3/Qa7uMPEsQPZbQS+t+Y3ABEuecft4yZs0T/PrLMcE+qgUIGG06qDC+5dzXsg/JAcYlr1xCF3sQNtqO9ijQE7 qzlxWilFHrHHRWZsdUk5nni2296NMCTtQUbh8evb5orxNKHhC5GTAoZhsivrZQ/0aOcEF1/KmSefHziNcl6cPHbel/UYTptTKuTf+Fj1LCGnSE4jL9msZzcJ X-Seq: 51786 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: > On 25/05/2023 12:21 Sebastian Gniazdowski wrote: > I'm using coproc to continuously read command's output, to have the > effect of `CMD | fzf` =E2=80=93 i.e. of instantly available contents whil= e > reading all of it eventually, However, I've had problem with buffering > =E2=80=93 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. >=20 > 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`). >=20 > 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=3D( 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 re= ading, 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(Optio= ns ops), UNUSED(int func)) } =20 =20 +/* + * Set up buffering on a given file descriptor. + */ +/**/ +static int +bin_zsystem_setbuffer(char *nam, char **args, +=09=09 UNUSED(Options ops), UNUSED(int func)) +{ + int fd; + FILE *strm; + + if (!args[0] || !args[1]) { +=09zwarnnam(nam, "setbuffer: not enough arguemnts"); +=09return 1; + } + if (args[2]) { +=09zwarnnam(nam, "setbuffer: too many arguments"); +=09return 1; + } + + fd =3D getposint(args[0], nam); + if (fd < 0) +=09return 1; + if (fd > 2) { +=09zwarnnam(nam, "setbuffer: file descriptor 0, 1 or 2 expected"); +=09return 1; + } + strm =3D (fd =3D=3D 0) ? stdin : (fd =3D=3D 1) ? stdout : stderr; + + if (!strcmp(args[1], "L")) { +=09if (setvbuf(strm, NULL, _IOLBF, 0)) { +=09 zwarnnam(nam, "setting line buffer failed: %e"); +=09 return 1; +=09} + } else if (!strcmp(args[1], "N")) { +=09if (setvbuf(strm, NULL, _IONBF, 0)) { +=09 zwarnnam(nam, "setting no buffer failed: %e"); +=09 return 1; +=09} + } 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 fu= nc) /* If more commands are implemented, this can be more sophisticated */ if (!strcmp(*args, "flock")) { =09return bin_zsystem_flock(nam, args+1, ops, func); + } else if (!strcmp(*args, "setbuffer")) { +=09return bin_zsystem_setbuffer(nam, args+1, ops, func); } else if (!strcmp(*args, "supports")) { =09return bin_zsystem_supports(nam, args+1, ops, func); }