Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Slurping a file (was: more spllitting travails)
- X-seq: zsh-users 29648
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- Cc: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Slurping a file (was: more spllitting travails)
- Date: Sat, 10 Feb 2024 20:48:03 +0000
- Archived-at: <https://zsh.org/users/29648>
- In-reply-to: <CAN=4vMq=E4s2a0sDFq-Mc8=pVzPnYOM9NaTmesgXQqi+O+mHpw@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- References: <CAH+w=7ZJsr7hGRvD8f-wUogPcGt0DMOcPyiYMpcwCsbBNkRwuQ@mail.gmail.com> <CAA=-s3zc5a+PA7draaA=FmXtwU9K8RrHbb70HbQN8MhmuXTYrQ@mail.gmail.com> <CAH+w=7bAWOF-v36hdNjaxBB-5rhjsp97mAtyESyR2OcojcEFUQ@mail.gmail.com> <205735b2-11e1-4b5e-baa2-7418753f591f@eastlink.ca> <CAH+w=7Y5_oQL20z7mkMUGSLnsdc9ceJ3=QqdAHVRF9jDZ_hZoQ@mail.gmail.com> <CAA=-s3x4nkLST56mhpWqb9OXUQR8081ew63p+5sEsyw5QmMdpw@mail.gmail.com> <CAH+w=7Yi+M1vthseF3Awp9JJh5KuFoCbFjLa--a22BGJgEJK_g@mail.gmail.com> <CAN=4vMpexntEq=hZcmsiXySy-2ptXMvBKunJ1knDkkS+4sYYLA@mail.gmail.com> <CAH+w=7aT-gbt7PRo=uvPK5=+rR3X-PE7nEssOkh+=fxwdeG_7w@mail.gmail.com> <CAN=4vMq=E4s2a0sDFq-Mc8=pVzPnYOM9NaTmesgXQqi+O+mHpw@mail.gmail.com>
2024-01-14 11:34:00 +0100, Roman Perepelitsa:
[...]
> function slurp() {
> emulate -L zsh -o no_multibyte
> zmodload zsh/system || return
> local -a content
> local -i i
> while true; do
> sysread 'content[++i]' && continue
> (( $? == 5 )) || return
> break
> done
> typeset -g REPLY=${(j::)content}
> }
[...]
IMO, it would be more useful if the result was returned in the
variable whose name was given as argument (defaulting to REPLY
if none was given like for read or sysread).
And would be better if upon error the returned variable
contained either what was successfully read or nothing (like
read but unlikely sysread).
Maybe something like:
zslurp() {
emulate -L zsh -o no_multibyte
typeset -n _zslurp_var=${1-REPLY}
_zslurp_var=
zmodload zsh/system || return
local -a _zslurp_content
local -i _zslurp_i _zslurp_ret
while true; do
sysread '_zslurp_content[++_zslurp_i]' && continue
_zslurp_ret=$?
break
done
_zslurp_var=${(j::)_zslurp_content}
(( _zslurp_ret == 5 ))
}
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author