Update psub for the new --inherit-variable flag

Also do some minor formatting cleanup, make psub return 1 when executed
outside of a command substitution, and make it respect $TMPDIR.
This commit is contained in:
Kevin Ballard 2014-10-02 16:32:16 -07:00
parent cfc06203e7
commit 33a76e1f8e
3 changed files with 22 additions and 5 deletions

View file

@ -42,19 +42,24 @@ function psub --description "Read from stdin into a file and output the filename
if not status --is-command-substitution
echo psub: Not inside of command substitution >&2
return
return 1
end
set -l TMPDIR $TMPDIR
if test -z "$TMPDIR[1]"
set TMPDIR /tmp
end
if test use_fifo = 1
# Write output to pipe. This needs to be done in the background so
# that the command substitution exits without needing to wait for
# all the commands to exit
set dir (mktemp -d /tmp/.psub.XXXXXXXXXX); or return
set dir (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
set filename $dir/psub.fifo
mkfifo $filename
cat >$filename &
else
set filename (mktemp /tmp/.psub.XXXXXXXXXX)
set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
cat >$filename
end
@ -70,6 +75,9 @@ function psub --description "Read from stdin into a file and output the filename
end
# Make sure we erase file when caller exits
eval function $funcname --on-job-exit caller\; command rm $filename\; functions -e $funcname\; end
function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable funcname
command rm $filename
functions -e $funcname
end
end

View file

@ -1,3 +1,4 @@
# vim: set filetype=fish:
# ensure that builtins that produce no output can still truncate files
# (bug PCA almost reintroduced!)
echo "Testing that builtins can truncate files"
@ -89,4 +90,11 @@ cat (echo foo | psub)
cat (echo bar | psub)
cat (echo baz | psub)
set -l filename (echo foo | psub)
if test -e $filename
echo 'psub file was not deleted'
else
echo 'psub file was deleted'
end
false

View file

@ -16,3 +16,4 @@ Testing for loop
foo
bar
baz
psub file was deleted