mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
Add --functrace shell option (re: 2a835a2d)
A side effect of the bug fixed in 2a835a2d caused the DEBUG trap
action to appear to be inherited by subshells, but in a buggy way
that could crash the shell. After the fix, the trap is reset in
subshells along with all the others, as it should be. Nonetheless,
as that bug was present for years, some have come to rely on it.
This commit implements that functionality properly. When the new
--functrace option is turned on, DEBUG trap actions are now
inherited by subshells as well as ksh function scopes. In addition,
since it makes logical sense, the new option also causes the
-x/--xtrace option's state to be inherited by ksh function scopes.
Note that changes made within the scope do not propagate upwards;
this is different from bash.
(I've opted against adding a -T short-form equivalent as on bash,
because -T was formerly a different option on 93u+ (see 63c55ad7)
and on mksh it has yet anohter a different meaning. To minimise
confusion, I think it's best to have the long-form name only.)
src/cmd/ksh93/include/shell.h,
src/cmd/ksh93/data/options.c:
- Add new "functrace" (SH_FUNCTRACE) long-form shell option.
src/cmd/ksh93/sh/subshell.c: sh_subshell():
- When functrace is on, copy the parent's DEBUG trap action into
the virtual subshell scope after resetting the trap actions.
src/cmd/ksh93/sh/xec.c: sh_funscope():
- When functrace is on and xtrace is on in the parent scope, turn
it on in the child scope.
- Same DEBUG trap action handling as in sh_subshell().
Resolves: https://github.com/ksh93/ksh/issues/162
This commit is contained in:
parent
73cd1a9ee0
commit
e3aa32a129
10 changed files with 112 additions and 6 deletions
|
|
@ -797,8 +797,7 @@ trap - DEBUG # bug compat
|
|||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
|
||||
# Make sure the DEBUG trap still exits a POSIX function on exit status 255
|
||||
# TODO: same test for ksh function with -o functrace, once we add that option
|
||||
exp=$'one\ntwo'
|
||||
exp=$'one\ntwo\nEND'
|
||||
got=$(
|
||||
myfn()
|
||||
{
|
||||
|
|
@ -813,11 +812,74 @@ got=$(
|
|||
}
|
||||
trap '[[ ${.sh.command} == *three ]] && set255' DEBUG
|
||||
myfn
|
||||
echo END
|
||||
)
|
||||
trap - DEBUG # bug compat
|
||||
[[ $got == "$exp" ]] || err_exit "DEBUG trap did not trigger return from POSIX function on status 255" \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
|
||||
# Test the new --functrace option <https://github.com/ksh93/ksh/issues/162>
|
||||
if ! [[ -o ?functrace ]]
|
||||
then
|
||||
warning 'shell does not have --functrace; skipping those tests'
|
||||
else
|
||||
# Make sure the DEBUG trap is inherited by ksh functions if --functrace is on
|
||||
exp=$'Debug 0\nDebug 1\nDebugLocal 1\nDebugLocal 2\nFunction\nDebugLocal 1\nDebug 0\nNofunction'
|
||||
got=$(
|
||||
function entryfn
|
||||
{
|
||||
trap 'echo DebugLocal ${.sh.level}' DEBUG
|
||||
myfn
|
||||
:
|
||||
}
|
||||
function myfn
|
||||
{
|
||||
echo Function
|
||||
}
|
||||
set --functrace
|
||||
trap 'echo Debug ${.sh.level}' DEBUG
|
||||
entryfn
|
||||
echo Nofunction
|
||||
)
|
||||
[[ $got == "$exp" ]] || err_exit "DEBUG trap not inherited by ksh function with --functrace on" \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
|
||||
# Make sure the DEBUG trap exits a ksh function with --functrace on exit status 255
|
||||
exp=$'one\ntwo\nEND'
|
||||
got=$(
|
||||
function myfn
|
||||
{
|
||||
echo one
|
||||
echo two
|
||||
echo three
|
||||
echo four
|
||||
}
|
||||
function set255
|
||||
{
|
||||
return 255
|
||||
}
|
||||
set --functrace
|
||||
trap '[[ ${.sh.command} == *three ]] && set255' DEBUG
|
||||
myfn
|
||||
echo END
|
||||
)
|
||||
[[ $got == "$exp" ]] || err_exit "DEBUG trap did not trigger return from POSIX function on status 255" \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
|
||||
# Make sure --functrace causes subshells to inherit the DEBUG trap
|
||||
exp=$'[DEBUG] 1\nfoo:5\n[DEBUG] 2\nbar:6\n[DEBUG] 3\nbar:6a\n[DEBUG] 2\nbaz:7'
|
||||
got=$(
|
||||
typeset -i dbg=0
|
||||
set --functrace
|
||||
trap 'echo "[DEBUG] $((++dbg))"' DEBUG
|
||||
echo foo:5
|
||||
( echo bar:6; (echo bar:6a) )
|
||||
echo baz:7
|
||||
)
|
||||
[[ $got == "$exp" ]] || err_exit "DEBUG trap not inherited by subshell" \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
fi
|
||||
|
||||
# ======
|
||||
# In ksh93v- and ksh2020 EXIT traps don't work in forked subshells
|
||||
# https://github.com/att/ast/issues/1452
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue