2017-03-15 04:02:02 +00:00
|
|
|
# This defines a compatibility shim for the `trap` command found in other shells like bash and zsh.
|
2013-04-01 17:49:02 +00:00
|
|
|
function trap -d 'Perform an action when the shell receives a signal'
|
2020-03-09 18:36:12 +00:00
|
|
|
set -l options h/help l/list-signals p/print
|
2017-07-09 23:55:16 +00:00
|
|
|
argparse -n trap $options -- $argv
|
2017-07-09 22:36:17 +00:00
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help trap
|
|
|
|
return 0
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
set -l mode
|
|
|
|
set -l cmd
|
|
|
|
set -l sig
|
|
|
|
|
2017-07-09 22:36:17 +00:00
|
|
|
# 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
|
2016-11-28 05:27:22 +00:00
|
|
|
else
|
2017-07-09 22:36:17 +00:00
|
|
|
switch (count $argv)
|
2016-11-28 05:27:22 +00:00
|
|
|
case 0
|
|
|
|
set mode print
|
|
|
|
case 1
|
|
|
|
set mode clear
|
|
|
|
case '*'
|
2017-07-09 22:36:17 +00:00
|
|
|
if test $argv[1] = -
|
|
|
|
set -e argv[1]
|
2016-11-28 05:27:22 +00:00
|
|
|
set mode clear
|
|
|
|
else
|
|
|
|
set mode set
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
switch $mode
|
|
|
|
case clear
|
2022-07-22 10:09:30 +00:00
|
|
|
for sig in (string upper -- $argv | string replace -r '^SIG' '')
|
2019-01-27 13:20:56 +00:00
|
|
|
if test -n "$sig"
|
2016-11-28 05:27:22 +00:00
|
|
|
functions -e __trap_handler_$sig
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case set
|
2017-07-09 22:36:17 +00:00
|
|
|
set -l cmd $argv[1]
|
|
|
|
set -e argv[1]
|
2016-11-28 05:27:22 +00:00
|
|
|
|
2022-07-22 10:09:30 +00:00
|
|
|
for sig in (string upper -- $argv | string replace -r '^SIG' '')
|
2019-01-27 13:20:56 +00:00
|
|
|
if test -n "$sig"
|
2022-07-22 10:09:30 +00:00
|
|
|
set -l sw --on-signal $sig
|
|
|
|
if string match -qi exit -- $sig
|
|
|
|
set sw --on-event fish_exit
|
|
|
|
end
|
2017-03-15 04:02:02 +00:00
|
|
|
echo "function __trap_handler_$sig $sw; $cmd; end" | source
|
2016-11-28 05:27:22 +00:00
|
|
|
else
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case print
|
|
|
|
set -l names
|
2017-07-09 22:36:17 +00:00
|
|
|
if set -q argv[1]
|
|
|
|
set names $argv
|
2016-11-28 05:27:22 +00:00
|
|
|
else
|
2022-10-24 20:07:29 +00:00
|
|
|
set names (functions -a | string split ',' | string match "__trap_handler_*" | string replace '__trap_handler_' '')
|
2016-11-28 05:27:22 +00:00
|
|
|
end
|
|
|
|
|
2022-07-22 10:09:30 +00:00
|
|
|
for sig in (string upper -- $names | string replace -r '^SIG' '')
|
2019-01-27 13:20:56 +00:00
|
|
|
if test -n "$sig"
|
2022-10-24 20:07:29 +00:00
|
|
|
functions __trap_handler_$sig
|
2022-10-24 20:27:53 +00:00
|
|
|
echo ""
|
2016-11-28 05:27:22 +00:00
|
|
|
else
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case list
|
|
|
|
kill -l
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|