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