aboutsummaryrefslogtreecommitdiffhomepage
path: root/share/functions/eval.fish
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-01-17 15:24:47 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2015-01-17 15:36:30 -0800
commit75a76c596cfd79f755f0f505a4eb47136e9dd1d0 (patch)
treeb1875cbd52f2b6f19468d073d274ace51689ba7a /share/functions/eval.fish
parent7164769d33ae0403a0a73ff1d0eb5427e3af09fa (diff)
Clean up recent fix for #1892
Restore 906d235 and simplify how __fish_restore_status works
Diffstat (limited to 'share/functions/eval.fish')
-rw-r--r--share/functions/eval.fish41
1 files changed, 24 insertions, 17 deletions
diff --git a/share/functions/eval.fish b/share/functions/eval.fish
index 5f06320f..052d4171 100644
--- a/share/functions/eval.fish
+++ b/share/functions/eval.fish
@@ -3,8 +3,8 @@ function eval -S -d "Evaluate parameters as a command"
# to preserve the status in case the block that is evaluated
# does not modify the status itself.
set -l status_copy $status
- function -S __fish_restore_status
- return $status_copy
+ function __fish_restore_status
+ return $argv[1]
end
if not set -q argv[2]
@@ -35,22 +35,29 @@ function eval -S -d "Evaluate parameters as a command"
if status --is-interactive
status --job-control full
end
+ __fish_restore_status $status_copy
- # rfish: To eval 'foo', we construct a block "begin ; foo; end <&3 3<&-"
- # The 'eval2_inner' is a param to 'begin' itself; I believe it does nothing.
- # Note the redirections are also within the quotes.
- #
- # We then pipe this to 'source 3<&0' which dup2's 3 to stdin.
- #
- # You might expect that the dup2(3, stdin) should overwrite stdin,
- # and therefore prevent 'source' from reading the piped-in block. This doesn't happen
- # because when you pipe to a builtin, we don't overwrite stdin with the read end
- # of the block; instead we set a separate fd in a variable 'builtin_stdin', which is
- # what it reads from. So builtins are magic in that, in pipes, their stdin
- # is not fd 0.
-
- __fish_restore_status
- echo "begin; $argv "\n" ;end eval2_inner <&3 3<&-" | source 3<&0
+ # To eval 'foo', we construct a block "begin ; foo; end <&3 3<&-"
+ # Note the redirections are also within the quotes.
+ #
+ # We then pipe this to 'source 3<&0’.
+ #
+ # You might expect that the dup2(3, stdin) should overwrite stdin,
+ # and therefore prevent 'source' from reading the piped-in block. This doesn't happen
+ # because when you pipe to a builtin, we don't overwrite stdin with the read end
+ # of the block; instead we set a separate fd in a variable 'builtin_stdin', which is
+ # what it reads from. So builtins are magic in that, in pipes, their stdin
+ # is not fd 0.
+ #
+ # ‘source’ does not apply the redirections to itself. Instead it saves them and passes
+ # them as block-level redirections to parser.eval(). Ultimately the eval’d code sees
+ # the following redirections (in the following order):
+ # dup2 0 -> 3
+ # dup2 pipe -> 0
+ # dup2 3 -> 0
+ # where the pipe is the pipe we get from piping ‘echo’ to ‘source’. Thus the redirection
+ # effectively makes stdin fd0, instead of the thing that was piped to ‘source’
+ echo "begin; $argv "\n" ;end <&3 3<&-" | source 3<&0
set -l res $status
status --job-control $mode