Permalink
Browse files

trap: Allow INT to be an alias for SIGINT.

bash and mksh accept both SIGINT and INT; dash only allows INT.
  • Loading branch information...
Andy Chu
Andy Chu committed Jan 21, 2018
1 parent 8d3b93a commit 4666d3bb7818d79853ea838aade0a8a9e31a3f3c
Showing with 29 additions and 7 deletions.
  1. +14 −6 core/builtin.py
  2. +15 −1 spec/builtin-trap.test.sh
View
@@ -57,7 +57,7 @@
CD PUSHD POPD DIRS
EXPORT UNSET SET SHOPT
TRAP UMASK
EXIT SOURCE DOT EVAL EXEC WAIT JOBS
EXIT SOURCE DOT EVAL EXEC WAIT JOBS
COMPLETE COMPGEN DEBUG_LINE
TRUE FALSE
COLON
@@ -1017,10 +1017,18 @@ def _MakeSignals():
# don't want SIG_DFL or SIG_IGN
if name.startswith('SIG') and not name.startswith('SIG_'):
int_val = getattr(signal, name)
names[name] = int_val
abbrev = name[3:]
names[abbrev] = int_val
return names
def _GetSignalValue(sig_spec):
# INT is an alias for SIGINT
if sig_spec.startswith('SIG'):
sig_spec = sig_spec[3:]
return _SIGNAL_NAMES.get(sig_spec)
_SIGNAL_NAMES = _MakeSignals()
_HOOK_NAMES = ('EXIT', 'ERR', 'RETURN', 'DEBUG')
@@ -1037,7 +1045,7 @@ def _MakeSignals():
# trap -- '' SIGTTIN
# trap -- '' SIGTTOU
#
# CPython registers different default handlers. Wait until C++ rewrite to
# CPython registers different default handlers. Wait until C++ rewrite to
def Trap(argv, traps, ex):
arg, i = TRAP_SPEC.Parse(argv)
@@ -1058,7 +1066,7 @@ def Trap(argv, traps, ex):
for name in _HOOK_NAMES:
print(' %s' % name)
for name, int_val in ordered:
print('%02d %s' % (int_val, name))
print('%2d %s' % (int_val, name))
sys.stdout.flush()
return 0
@@ -1079,7 +1087,7 @@ def Trap(argv, traps, ex):
pass
return 0
sig_val = _SIGNAL_NAMES.get(sig_spec)
sig_val = _GetSignalValue(sig_spec)
if sig_val is not None:
try:
del traps[sig_spec]
@@ -1106,7 +1114,7 @@ def Trap(argv, traps, ex):
return 0
# Register a signal.
sig_val = _SIGNAL_NAMES.get(sig_spec)
sig_val = _GetSignalValue(sig_spec)
if sig_val is not None:
handler = _TrapThunk(ex, node)
# For signal handlers, the traps dictionary is used only for debugging.
View
@@ -1,7 +1,7 @@
#!/bin/bash
### trap -l
trap -l | grep SIGINT >/dev/null
trap -l | grep INT >/dev/null
## status: 0
## N-I dash/mksh status: 1
@@ -19,6 +19,20 @@ trap 'foo' SIGINVALID
trap - SIGINVALID
## status: 1
### SIGINT and INT are aliases
trap - SIGINT
echo $?
trap - INT
echo $?
## STDOUT:
0
0
## END
## N-I dash STDOUT:
1
0
## END
### Invalid trap invocation
trap 'foo'
echo status=$?

0 comments on commit 4666d3b

Please sign in to comment.