2007-01-16 01:29:18 +00: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-09 18:36:12 +00:00
|
|
|
set -l options -x 'f,F' -x 'F,s' h/help f/file F/fifo 's/suffix=' T-testing
|
2017-07-13 21:12:20 +00: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 09:20:05 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
set -l dirname
|
|
|
|
set -l filename
|
|
|
|
set -l funcname
|
|
|
|
|
|
|
|
if not status --is-command-substitution
|
2017-07-13 21:12:20 +00:00
|
|
|
printf (_ "%s: Not inside of command substitution") psub >&2
|
2016-11-28 05:27:22 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-07-04 23:35:32 +00:00
|
|
|
set -l tmpdir /tmp
|
|
|
|
set -q TMPDIR
|
|
|
|
and set tmpdir $TMPDIR
|
2016-11-28 05:27:22 +00:00
|
|
|
|
2017-07-17 21:33:51 +00:00
|
|
|
if set -q _flag_fifo
|
2016-11-28 05:27:22 +00: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-17 21:33:51 +00:00
|
|
|
# all the commands to exit.
|
2017-07-04 23:35:32 +00:00
|
|
|
set dirname (mktemp -d $tmpdir/.psub.XXXXXXXXXX)
|
2020-11-13 15:58:45 +00:00
|
|
|
or return 1
|
2017-07-13 21:12:20 +00:00
|
|
|
set filename $dirname/psub.fifo"$_flag_suffix"
|
2019-11-14 16:13:39 +00:00
|
|
|
command mkfifo $filename
|
2019-01-29 07:17:42 +00: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-14 16:13:39 +00:00
|
|
|
command tee $filename >/dev/null &
|
2017-07-13 21:12:20 +00:00
|
|
|
else if test -z "$_flag_suffix"
|
2017-07-04 23:35:32 +00:00
|
|
|
set filename (mktemp $tmpdir/.psub.XXXXXXXXXX)
|
2020-11-13 15:58:45 +00:00
|
|
|
or return 1
|
2019-11-14 16:13:39 +00:00
|
|
|
command cat >$filename
|
2016-11-28 05:27:22 +00:00
|
|
|
else
|
2017-07-04 23:35:32 +00:00
|
|
|
set dirname (mktemp -d $tmpdir/.psub.XXXXXXXXXX)
|
2020-11-13 15:58:45 +00:00
|
|
|
or return 1
|
2017-07-17 21:33:51 +00:00
|
|
|
set filename "$dirname/psub$_flag_suffix"
|
2019-11-14 16:13:39 +00:00
|
|
|
command cat >$filename
|
2016-11-28 05:27:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Write filename to stdout
|
|
|
|
echo $filename
|
|
|
|
|
2017-07-17 21:33:51 +00:00
|
|
|
# This flag isn't documented. It's strictly for our unit tests.
|
|
|
|
if set -q _flag_testing
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# Find unique function name
|
|
|
|
while true
|
|
|
|
set funcname __fish_psub_(random)
|
2018-04-01 20:42:38 +00:00
|
|
|
if not functions $funcname >/dev/null 2>/dev/null
|
2016-11-28 05:27:22 +00: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-17 21:33:51 +00:00
|
|
|
if test -n "$dirname"
|
2016-11-28 05:27:22 +00:00
|
|
|
command rmdir $dirname
|
|
|
|
end
|
|
|
|
functions -e $funcname
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
end
|