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.
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
function __trap_translate_signal
|
2016-11-28 05:27:22 +00:00
|
|
|
set upper (echo $argv[1]|tr a-z A-Z)
|
|
|
|
string replace -r '^SIG' '' -- $upper
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __trap_switch
|
2016-11-28 05:27:22 +00:00
|
|
|
switch $argv[1]
|
2017-03-15 04:02:02 +00:00
|
|
|
case EXIT exit
|
2018-02-06 16:41:08 +00:00
|
|
|
echo --on-event fish_exit
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
case '*'
|
|
|
|
echo --on-signal $argv[1]
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
2013-04-01 17:49:02 +00:00
|
|
|
function trap -d 'Perform an action when the shell receives a signal'
|
2017-07-09 22:36:17 +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
|
2017-07-09 22:36:17 +00:00
|
|
|
for i in $argv
|
2016-11-28 05:27:22 +00:00
|
|
|
set sig (__trap_translate_signal $i)
|
|
|
|
if test $sig
|
|
|
|
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
|
|
|
|
2017-07-09 22:36:17 +00:00
|
|
|
for i in $argv
|
2016-11-28 05:27:22 +00:00
|
|
|
set -l sig (__trap_translate_signal $i)
|
|
|
|
set sw (__trap_switch $sig)
|
|
|
|
|
|
|
|
if test $sig
|
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
|
2017-07-09 22:36:17 +00:00
|
|
|
set names (functions -na | string match "__trap_handler_*" | string replace '__trap_handler_' '')
|
2016-11-28 05:27:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for i in $names
|
|
|
|
set sig (__trap_translate_signal $i)
|
|
|
|
|
|
|
|
if test sig
|
|
|
|
functions __trap_handler_$i
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case list
|
|
|
|
kill -l
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|