fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
1e0ff8712
master
1# localization: tier1 2function psub --description"Read from stdin into a file and output the filename. Remove the file when the command that called psub exits." 3set -l options -x'f,F' h/help f/file F/fifo's/suffix=' T-testing 4argparse -n psub --max-args=0$options --$argv 5or return 6 7if set -q _flag_help 8__fish_print_help psub 9return 0 10end 11 12set -l dirname 13set -l filename 14set -l funcname 15 16if not status is-command-substitution 17{ 18 printf( _ "%s: Not inside of command substitution" ) psub 19echo 20} >&2 21return 1 22end 23 24if set -q _flag_fifo 25# Write output to pipe. This needs to be done in the background so 26# that the command substitution exits without needing to wait for 27# all the commands to exit. 28set dirname( __fish_mktemp_relative -d .psub) 29or return 1 30set filename$dirname /psub.fifo" $_flag_suffix " 31command mkfifo$filename 32# Note that if we were to do the obvious `cat >$filename &`, we would deadlock 33# because $filename may be opened before the fork. Use tee to ensure it is opened 34# after the fork. 35command tee$filename > /dev/null& 36else if test -z " $_flag_suffix " 37set filename( __fish_mktemp_relative .psub) 38or return 1 39command cat> $filename 40else 41set dirname( __fish_mktemp_relative -d .psub) 42or return 1 43set filename" $dirname /psub $_flag_suffix " 44command cat> $filename 45end 46 47# Write filename to stdout 48echo $filename 49 50# This flag isn't documented. It's strictly for our unit tests. 51if set -q _flag_testing 52return 53end 54 55# Find unique function name 56while true 57set funcname __fish_psub_( random ) 58if not functions $funcname > /dev/null2> /dev/null 59break 60end 61end 62 63# Make sure we erase file when caller exits 64function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable dirname --inherit-variable funcname 65command rm$filename 66if test -n " $dirname " 67command rmdir$dirname 68end 69functions -e$funcname 70end 71 72end