File: trap.fish

package info (click to toggle)
fish 4.2.1-3.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,976 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (83 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# localization: tier1

# This defines a compatibility shim for the `trap` command found in other shells like bash and zsh.
function trap -d 'Perform an action when the shell receives a signal'
    set -l options h/help l/list-signals p/print
    argparse -n trap $options -- $argv
    or return

    if set -q _flag_help
        __fish_print_help trap
        return 0
    end

    set -l mode
    set -l cmd
    set -l sig

    # Determine the mode based on either an explicit flag or the non-flag args.
    if set -q _flag_print
        set mode print
    else if set -q _flag_list_signals
        set mode list
    else
        switch (count $argv)
            case 0
                set mode print
            case 1
                set mode clear
            case '*'
                if test $argv[1] = -
                    set -e argv[1]
                    set mode clear
                else
                    set mode set
                end
        end
    end

    switch $mode
        case clear
            for sig in (string upper -- $argv | string replace -r '^SIG' '')
                if test -n "$sig"
                    functions -e __trap_handler_$sig
                end
            end

        case set
            set -l cmd $argv[1]
            set -e argv[1]

            for sig in (string upper -- $argv | string replace -r '^SIG' '')
                if test -n "$sig"
                    set -l sw --on-signal $sig
                    if string match -qi exit -- $sig
                        set sw --on-event fish_exit
                    end
                    echo "function __trap_handler_$sig $sw; $cmd; end" | source
                else
                    return 1
                end
            end

        case print
            set -l names
            if set -q argv[1]
                set names $argv
            else
                set names (functions -a | string split ',' | string match "__trap_handler_*" | string replace '__trap_handler_' '')
            end

            for sig in (string upper -- $names | string replace -r '^SIG' '')
                if test -n "$sig"
                    functions __trap_handler_$sig
                    echo ""
                else
                    return 1
                end
            end

        case list
            kill -l
    end
end