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 if not status --is-command-substitution
echo psub: Not inside of command substitution >&2 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 end
if test use_fifo = 1 if test use_fifo = 1
# Write output to pipe. This needs to be done in the background so # Write output to pipe. This needs to be done in the background so
# that the command substitution exits without needing to wait for # that the command substitution exits without needing to wait for
# all the commands to exit # 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 set filename $dir/psub.fifo
mkfifo $filename mkfifo $filename
cat >$filename & cat >$filename &
else else
set filename (mktemp /tmp/.psub.XXXXXXXXXX) set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
cat >$filename cat >$filename
end end
@ -70,6 +75,9 @@ function psub --description "Read from stdin into a file and output the filename
end end
# Make sure we erase file when caller exits # 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 end

View file

@ -1,3 +1,4 @@
# vim: set filetype=fish:
# ensure that builtins that produce no output can still truncate files # ensure that builtins that produce no output can still truncate files
# (bug PCA almost reintroduced!) # (bug PCA almost reintroduced!)
echo "Testing that builtins can truncate files" echo "Testing that builtins can truncate files"
@ -89,4 +90,11 @@ cat (echo foo | psub)
cat (echo bar | psub) cat (echo bar | psub)
cat (echo baz | 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 false

View file

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