2006-02-08 17:20:05 +08:00
|
|
|
|
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."
|
2006-02-08 17:20:05 +08:00
|
|
|
|
|
|
|
set -l filename
|
|
|
|
set -l funcname
|
2007-01-16 01:43:30 +08:00
|
|
|
set -l use_fifo 1
|
|
|
|
set -l shortopt -o hf
|
|
|
|
set -l longopt -l help,file
|
2006-02-08 17:20:05 +08:00
|
|
|
|
2007-01-16 01:43:30 +08:00
|
|
|
if getopt -T >/dev/null
|
2010-09-18 10:18:26 +08:00
|
|
|
set longopt
|
2007-01-16 01:43:30 +08:00
|
|
|
end
|
|
|
|
|
2010-11-05 23:26:26 +08:00
|
|
|
if not getopt -n psub -Q $shortopt $longopt -- $argv >/dev/null
|
2007-01-16 01:43:30 +08:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l tmp (getopt $shortopt $longopt -- $argv)
|
|
|
|
|
|
|
|
eval set opt $tmp
|
|
|
|
|
|
|
|
while count $opt >/dev/null
|
|
|
|
|
|
|
|
switch $opt[1]
|
|
|
|
case -h --help
|
2006-11-18 00:24:38 +08:00
|
|
|
__fish_print_help psub
|
2006-02-08 17:20:05 +08:00
|
|
|
return 0
|
|
|
|
|
2007-01-16 01:43:30 +08:00
|
|
|
case -f --file
|
|
|
|
set use_fifo 0
|
|
|
|
|
|
|
|
case --
|
|
|
|
set -e opt[1]
|
|
|
|
break
|
|
|
|
|
2006-02-08 17:20:05 +08:00
|
|
|
end
|
2007-01-16 01:43:30 +08:00
|
|
|
|
|
|
|
set -e opt[1]
|
|
|
|
|
2006-02-08 17:20:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if not status --is-command-substitution
|
|
|
|
echo psub: Not inside of command substitution >&2
|
2014-10-03 07:32:16 +08:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l TMPDIR $TMPDIR
|
|
|
|
if test -z "$TMPDIR[1]"
|
|
|
|
set TMPDIR /tmp
|
2006-02-08 17:20:05 +08:00
|
|
|
end
|
|
|
|
|
2015-09-01 01:32:28 +08:00
|
|
|
if test use_fifo = 1
|
2007-01-16 01:43:30 +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
|
|
|
|
# all the commands to exit
|
2014-10-03 07:32:16 +08:00
|
|
|
set dir (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
|
|
|
|
set filename $dir/psub.fifo
|
2010-09-18 10:18:26 +08:00
|
|
|
mkfifo $filename
|
2007-01-16 01:43:30 +08:00
|
|
|
cat >$filename &
|
|
|
|
else
|
2014-10-03 07:32:16 +08:00
|
|
|
set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
|
2007-01-16 01:43:30 +08:00
|
|
|
cat >$filename
|
|
|
|
end
|
2006-02-08 17:20:05 +08:00
|
|
|
|
|
|
|
# Write filename to stdout
|
|
|
|
echo $filename
|
|
|
|
|
|
|
|
# Find unique function name
|
|
|
|
while true
|
|
|
|
set funcname __fish_psub_(random);
|
|
|
|
if not functions $funcname >/dev/null ^/dev/null
|
|
|
|
break;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make sure we erase file when caller exits
|
2014-10-03 07:32:16 +08:00
|
|
|
function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable funcname
|
|
|
|
command rm $filename
|
|
|
|
functions -e $funcname
|
|
|
|
end
|
2006-02-08 17:20:05 +08:00
|
|
|
|
|
|
|
end
|