2006-02-08 09:20:05 +00:00
|
|
|
|
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."
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2015-02-16 12:28:11 +00:00
|
|
|
set -l dirname
|
2006-02-08 09:20:05 +00:00
|
|
|
set -l filename
|
|
|
|
set -l funcname
|
2015-02-16 12:28:11 +00:00
|
|
|
set -l suffix
|
2007-01-15 17:43:30 +00:00
|
|
|
set -l use_fifo 1
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2015-02-16 12:28:11 +00:00
|
|
|
while count $argv >/dev/null
|
2007-01-15 17:43:30 +00:00
|
|
|
|
2015-02-16 12:28:11 +00:00
|
|
|
switch $argv[1]
|
2007-01-15 17:43:30 +00:00
|
|
|
case -h --help
|
2006-11-17 16:24:38 +00:00
|
|
|
__fish_print_help psub
|
2006-02-08 09:20:05 +00:00
|
|
|
return 0
|
|
|
|
|
2007-01-15 17:43:30 +00:00
|
|
|
case -f --file
|
|
|
|
set use_fifo 0
|
2015-02-16 12:28:11 +00:00
|
|
|
set -e argv[1]
|
|
|
|
|
|
|
|
case -s --suffix
|
|
|
|
if not set -q argv[2]
|
|
|
|
printf "psub: missing operand\n"
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
set suffix $argv[2]
|
|
|
|
set -e argv[1..2]
|
2007-01-15 17:43:30 +00:00
|
|
|
|
|
|
|
case --
|
2015-02-16 12:28:11 +00:00
|
|
|
set -e argv[1]
|
2007-01-15 17:43:30 +00:00
|
|
|
break
|
|
|
|
|
2015-02-16 12:28:11 +00:00
|
|
|
case "-?" "--*"
|
|
|
|
printf "psub: invalid option: '%s'\n" $argv[1]
|
|
|
|
return 1
|
2007-01-15 17:43:30 +00:00
|
|
|
|
2015-02-16 12:28:11 +00:00
|
|
|
case "-*"
|
|
|
|
# Ungroup short options: -hfs => -h -f -s
|
|
|
|
set opts "-"(string sub -s 2 -- $argv[1] | string split "")
|
|
|
|
set -e argv[1]
|
|
|
|
set argv $opts $argv
|
2007-01-15 17:43:30 +00:00
|
|
|
|
2015-02-16 12:28:11 +00:00
|
|
|
case "*"
|
|
|
|
printf "psub: extra operand: '%s'\n" $argv[1]
|
|
|
|
return 1
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not status --is-command-substitution
|
|
|
|
echo psub: Not inside of command substitution >&2
|
2014-10-02 23:32:16 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l TMPDIR $TMPDIR
|
|
|
|
if test -z "$TMPDIR[1]"
|
|
|
|
set TMPDIR /tmp
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
2015-08-31 17:32:28 +00:00
|
|
|
if test use_fifo = 1
|
2007-01-15 17:43:30 +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
|
|
|
|
# all the commands to exit
|
2015-02-16 12:28:11 +00:00
|
|
|
set dirname (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
|
|
|
|
set filename $dirname/psub.fifo"$suffix"
|
2010-09-18 02:18:26 +00:00
|
|
|
mkfifo $filename
|
2007-01-15 17:43:30 +00:00
|
|
|
cat >$filename &
|
2015-02-16 12:28:11 +00:00
|
|
|
else if test -z $suffix
|
2014-10-02 23:32:16 +00:00
|
|
|
set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
|
2007-01-15 17:43:30 +00:00
|
|
|
cat >$filename
|
2015-02-16 12:28:11 +00:00
|
|
|
else
|
|
|
|
set dirname (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX)
|
|
|
|
set filename $dirname/psub"$suffix"
|
|
|
|
cat >$filename
|
2007-01-15 17:43:30 +00:00
|
|
|
end
|
2006-02-08 09:20:05 +00: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
|
2015-02-16 12:28:11 +00:00
|
|
|
function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable dirname --inherit-variable funcname
|
2014-10-02 23:32:16 +00:00
|
|
|
command rm $filename
|
2015-02-16 12:28:11 +00:00
|
|
|
if count $dirname >/dev/null
|
|
|
|
command rmdir $dirname
|
|
|
|
end
|
2014-10-02 23:32:16 +00:00
|
|
|
functions -e $funcname
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
end
|