2007-09-23 03:30:55 +08:00
|
|
|
function eval -S -d "Evaluate parameters as a command"
|
2015-01-15 01:58:07 +08:00
|
|
|
# keep a copy of the previous $status and use restore_status
|
|
|
|
# to preserve the status in case the block that is evaluated
|
|
|
|
# does not modify the status itself.
|
|
|
|
set -l status_copy $status
|
2015-01-17 18:50:23 +08:00
|
|
|
function -S __fish_restore_status
|
2015-01-15 01:58:07 +08:00
|
|
|
return $status_copy
|
|
|
|
end
|
|
|
|
|
2014-10-10 09:07:57 +08:00
|
|
|
if not set -q argv[2]
|
|
|
|
# like most builtins, we only check for -h/--help
|
|
|
|
# if we only have a single argument
|
|
|
|
switch "$argv[1]"
|
|
|
|
case -h --help
|
|
|
|
__fish_print_help eval
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
2007-10-06 18:23:26 +08:00
|
|
|
|
|
|
|
# If we are in an interactive shell, eval should enable full
|
|
|
|
# job control since it should behave like the real code was
|
|
|
|
# executed. If we don't do this, commands that expect to be
|
|
|
|
# used interactively, like less, wont work using eval.
|
|
|
|
|
|
|
|
set -l mode
|
|
|
|
if status --is-interactive-job-control
|
|
|
|
set mode interactive
|
|
|
|
else
|
|
|
|
if status --is-full-job-control
|
|
|
|
set mode full
|
|
|
|
else
|
|
|
|
set mode none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if status --is-interactive
|
|
|
|
status --job-control full
|
|
|
|
end
|
|
|
|
|
2015-01-15 01:58:07 +08:00
|
|
|
# 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.
|
2013-08-19 07:55:01 +08:00
|
|
|
|
2015-01-17 18:50:23 +08:00
|
|
|
__fish_restore_status
|
2015-01-15 01:58:07 +08:00
|
|
|
echo "begin; $argv "\n" ;end eval2_inner <&3 3<&-" | source 3<&0
|
2010-11-22 19:36:42 +08:00
|
|
|
set -l res $status
|
2007-10-06 18:23:26 +08:00
|
|
|
|
|
|
|
status --job-control $mode
|
2010-11-22 19:36:42 +08:00
|
|
|
return $res
|
2007-04-23 06:18:06 +08:00
|
|
|
end
|