fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes Altmanningershare: use "status is-*" instead of old-school "status --is-*"1e0ff8712

master
2.2 KiB72 linesraw
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."
3    set -l options -x 'f,F' h/help f/file F/fifo 's/suffix=' T-testing
4    argparse -n psub --max-args=0 $options -- $argv
5    or return
6
7    if set -q _flag_help
8        __fish_print_help psub
9        return 0
10    end
11
12    set -l dirname
13    set -l filename
14    set -l funcname
15
16    if not status is-command-substitution
17        {
18            printf (_ "%s: Not inside of command substitution") psub
19            echo
20        } >&2
21        return 1
22    end
23
24    if 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.
28        set dirname (__fish_mktemp_relative -d .psub)
29        or return 1
30        set filename $dirname/psub.fifo"$_flag_suffix"
31        command 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.
35        command tee $filename >/dev/null &
36    else if test -z "$_flag_suffix"
37        set filename (__fish_mktemp_relative .psub)
38        or return 1
39        command cat >$filename
40    else
41        set dirname (__fish_mktemp_relative -d .psub)
42        or return 1
43        set filename "$dirname/psub$_flag_suffix"
44        command cat >$filename
45    end
46
47    # Write filename to stdout
48    echo $filename
49
50    # This flag isn't documented. It's strictly for our unit tests.
51    if set -q _flag_testing
52        return
53    end
54
55    # Find unique function name
56    while true
57        set funcname __fish_psub_(random)
58        if not functions $funcname >/dev/null 2>/dev/null
59            break
60        end
61    end
62
63    # Make sure we erase file when caller exits
64    function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable dirname --inherit-variable funcname
65        command rm $filename
66        if test -n "$dirname"
67            command rmdir $dirname
68        end
69        functions -e $funcname
70    end
71
72end