2007-01-16 09:29:18 +08:00
|
|
|
function psub --description "Read from stdin into a file and output the filename. Remove the file when the command that called psub exits."
|
2020-03-10 02:36:12 +08:00
|
|
|
set -l options -x 'f,F' -x 'F,s' h/help f/file F/fifo 's/suffix=' T-testing
|
2017-07-14 05:12:20 +08:00
|
|
|
argparse -n psub --max-args=0 $options -- $argv
|
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help psub
|
|
|
|
return 0
|
|
|
|
end
|
2006-02-08 17:20:05 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
set -l dirname
|
|
|
|
set -l filename
|
|
|
|
set -l funcname
|
|
|
|
|
|
|
|
if not status --is-command-substitution
|
2017-07-14 05:12:20 +08:00
|
|
|
printf (_ "%s: Not inside of command substitution") psub >&2
|
2016-11-28 13:27:22 +08:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-07-05 07:35:32 +08:00
|
|
|
set -l tmpdir /tmp
|
|
|
|
set -q TMPDIR
|
|
|
|
and set tmpdir $TMPDIR
|
2016-11-28 13:27:22 +08:00
|
|
|
|
2017-07-18 05:33:51 +08:00
|
|
|
if set -q _flag_fifo
|
2016-11-28 13:27:22 +08:00
|
|
|
# Write output to pipe. This needs to be done in the background so
|
|
|
|
# that the command substitution exits without needing to wait for
|
2017-07-18 05:33:51 +08:00
|
|
|
# all the commands to exit.
|
2017-07-05 07:35:32 +08:00
|
|
|
set dirname (mktemp -d $tmpdir/.psub.XXXXXXXXXX)
|
2016-11-28 13:27:22 +08:00
|
|
|
or return
|
2017-07-14 05:12:20 +08:00
|
|
|
set filename $dirname/psub.fifo"$_flag_suffix"
|
2019-11-15 00:13:39 +08:00
|
|
|
command mkfifo $filename
|
2019-01-29 15:17:42 +08:00
|
|
|
# Note that if we were to do the obvious `cat >$filename &`, we would deadlock
|
|
|
|
# because $filename may be opened before the fork. Use tee to ensure it is opened
|
|
|
|
# after the fork.
|
2019-11-15 00:13:39 +08:00
|
|
|
command tee $filename >/dev/null &
|
2017-07-14 05:12:20 +08:00
|
|
|
else if test -z "$_flag_suffix"
|
2017-07-05 07:35:32 +08:00
|
|
|
set filename (mktemp $tmpdir/.psub.XXXXXXXXXX)
|
2019-11-15 00:13:39 +08:00
|
|
|
command cat >$filename
|
2016-11-28 13:27:22 +08:00
|
|
|
else
|
2017-07-05 07:35:32 +08:00
|
|
|
set dirname (mktemp -d $tmpdir/.psub.XXXXXXXXXX)
|
2017-07-18 05:33:51 +08:00
|
|
|
set filename "$dirname/psub$_flag_suffix"
|
2019-11-15 00:13:39 +08:00
|
|
|
command cat >$filename
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Write filename to stdout
|
|
|
|
echo $filename
|
|
|
|
|
2017-07-18 05:33:51 +08:00
|
|
|
# This flag isn't documented. It's strictly for our unit tests.
|
|
|
|
if set -q _flag_testing
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# Find unique function name
|
|
|
|
while true
|
|
|
|
set funcname __fish_psub_(random)
|
2018-04-02 04:42:38 +08:00
|
|
|
if not functions $funcname >/dev/null 2>/dev/null
|
2016-11-28 13:27:22 +08:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make sure we erase file when caller exits
|
|
|
|
function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable dirname --inherit-variable funcname
|
|
|
|
command rm $filename
|
2017-07-18 05:33:51 +08:00
|
|
|
if test -n "$dirname"
|
2016-11-28 13:27:22 +08:00
|
|
|
command rmdir $dirname
|
|
|
|
end
|
|
|
|
functions -e $funcname
|
|
|
|
end
|
2006-02-08 17:20:05 +08:00
|
|
|
|
|
|
|
end
|