2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
function __trap_translate_signal
|
|
|
|
set upper (echo $argv[1]|tr a-z A-Z)
|
2016-02-03 22:23:04 +00:00
|
|
|
string replace -r '^SIG' '' -- $upper
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __trap_switch
|
|
|
|
|
|
|
|
switch $argv[1]
|
|
|
|
case EXIT
|
2015-07-09 03:56:20 +00:00
|
|
|
echo --on-process-exit %self
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
case '*'
|
|
|
|
echo --on-signal $argv[1]
|
2010-09-18 02:18:26 +00:00
|
|
|
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'
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
set -l mode
|
2010-09-18 02:18:26 +00:00
|
|
|
set -l cmd
|
|
|
|
set -l sig
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2013-04-28 23:31:25 +00:00
|
|
|
set -l options
|
2006-02-08 09:20:05 +00:00
|
|
|
set -l longopt
|
2013-04-28 23:31:25 +00:00
|
|
|
set -l shortopt lph
|
|
|
|
if not getopt -T > /dev/null
|
|
|
|
# GNU getopt
|
|
|
|
set longopt print,help,list-signals
|
|
|
|
set options -o $shortopt -l $longopt --
|
|
|
|
# Verify options
|
|
|
|
if not getopt -n type $options $argv >/dev/null
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Old getopt, used on OS X
|
|
|
|
set options $shortopt
|
|
|
|
# Verify options
|
|
|
|
if not getopt $options $argv >/dev/null
|
|
|
|
return 1
|
|
|
|
end
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
2013-04-28 23:31:25 +00:00
|
|
|
# Do the real getopt invocation
|
|
|
|
set -l tmp (getopt $options $argv)
|
2006-02-08 09:20:05 +00:00
|
|
|
|
2013-04-28 23:31:25 +00:00
|
|
|
# Break tmp up into an array
|
|
|
|
set -l opt
|
2006-02-08 09:20:05 +00:00
|
|
|
eval set opt $tmp
|
|
|
|
|
|
|
|
while count $opt >/dev/null
|
|
|
|
switch $opt[1]
|
|
|
|
case -h --help
|
2006-11-17 16:24:38 +00:00
|
|
|
__fish_print_help trap
|
2006-02-08 09:20:05 +00:00
|
|
|
return 0
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
case -p --print
|
|
|
|
set mode print
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
case -l --list-signals
|
|
|
|
set mode list
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
case --
|
|
|
|
set -e opt[1]
|
|
|
|
break
|
|
|
|
|
|
|
|
end
|
|
|
|
set -e opt[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if not count $mode >/dev/null
|
|
|
|
|
|
|
|
switch (count $opt)
|
|
|
|
|
|
|
|
case 0
|
|
|
|
set mode print
|
|
|
|
|
|
|
|
case 1
|
|
|
|
set mode clear
|
|
|
|
|
|
|
|
case '*'
|
|
|
|
if test opt[1] = -
|
|
|
|
set -e opt[1]
|
|
|
|
set mode clear
|
|
|
|
else
|
|
|
|
set mode set
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
switch $mode
|
|
|
|
case clear
|
|
|
|
for i in $opt
|
2006-12-12 18:18:41 +00:00
|
|
|
set sig (__trap_translate_signal $i)
|
2006-02-08 09:20:05 +00:00
|
|
|
if test $sig
|
2010-09-18 02:18:26 +00:00
|
|
|
functions -e __trap_handler_$sig
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case set
|
|
|
|
set -l cmd $opt[1]
|
|
|
|
set -e opt[1]
|
|
|
|
|
|
|
|
for i in $opt
|
|
|
|
|
2006-12-12 18:18:41 +00:00
|
|
|
set -l sig (__trap_translate_signal $i)
|
|
|
|
set sw (__trap_switch $sig)
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
if test $sig
|
|
|
|
eval "function __trap_handler_$sig $sw; $cmd; end"
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
case print
|
2010-09-18 02:18:26 +00:00
|
|
|
set -l names
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
if count $opt >/dev/null
|
2006-12-12 18:18:41 +00:00
|
|
|
set names $opt
|
2006-02-08 09:20:05 +00:00
|
|
|
else
|
2016-02-03 22:23:04 +00:00
|
|
|
set names (functions -na| string match "__trap_handler_*" | string replace '__trap_handler_' '')
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for i in $names
|
|
|
|
|
2006-12-12 18:18:41 +00:00
|
|
|
set sig (__trap_translate_signal $i)
|
2006-02-08 09:20:05 +00:00
|
|
|
|
|
|
|
if test sig
|
|
|
|
functions __trap_handler_$i
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
case list
|
|
|
|
kill -l
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2006-02-08 09:20:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|