about summary refs log tree commit diff
path: root/Functions/Misc/zslurp
blob: 84df0c948875a5769ce3a4e70a70978f37cff79d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/zsh -f

# Read stdin verbatim and as efficiently as possible into $REPLY,
# stopping without any change to $REPLY in the event of any error.
# Benchmarked by Roman Perepelitsa in zsh-users/29472

# Although this function faithfully records the input stream, later
# references to $REPLY with the multibyte option back in effect will
# (re-)interpret the content as multibyte characters.  This may not be
# what is desired.

emulate -L zsh -o no_multibyte

### Alternate formulation, faster on bigger files
# # /dev/fd/0 is treated specially by -f so also check /dev/fd
# if [[ -d /dev/fd && -f /dev/fd/0 ]] && zmodload zsh/mapfile
# then
#     local +h -Ar mapfile
#     typeset -g REPLY="${mapfile[/dev/fd/0]}" && return
# fi
# # else fall through to read from pipe/socket

zmodload zsh/system || return
local -a content
local -i i=0
while true; do
    sysread 'content[++i]' && continue
    (( $? == 5 )) || return
    break
done
typeset -g REPLY=${(j::)content}