Skip to content

Commit

Permalink
Add regression test for typeset -p output and update some documentation
Browse files Browse the repository at this point in the history
- local.sh: Added tests for the presence of the -D flag in output.
- sh.1: Removed this comment as this behavior is subject to change.
- xec.c: Update comment (the ksh93v- code is something I'm trying to
  salvage, but it certainly does seem unsalvageable.)
  • Loading branch information
JohnoKing committed Jan 18, 2024
1 parent bae563d commit b601a87
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
8 changes: 0 additions & 8 deletions src/cmd/ksh93/sh.1
Original file line number Diff line number Diff line change
Expand Up @@ -8705,14 +8705,6 @@ function uses.
Unlike static local scoping, variables given a
dynamic scope are made available to nested functions invoked
by the function the local variable is located in.
Do note however that,
like with statically scoped local variables,
unsetting the variable will only locally unset it in the function
it's scoped to.
Nested functions called after unset will not inherit
the locally unset state of the variable,
and will inherit the
variable of the same name from the global scope if it exists.
Dynamic local scoping is available regardless of whether the
function is defined with POSIX or KornShell syntax.
.TP
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/ksh93/sh/xec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3037,10 +3037,10 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg)
*
* The old method ksh93v- uses for its bash mode creates a viewport between
* sh.var_tree and sh.var_base with the help of a sh.st.var_local pointer.
* This could fake dynamic scoping convincingly for the bash mode, but in
* reality that only manages to create a global scope local to the top level
* function (i.e., nested functions have no scoping.) This design flaw in
* ksh93v- made its scoping code unsalvageable.
* This could fake dynamic scoping convincingly for the bash mode, but the
* ksh93v- code only manages to create a global scope local to the top level
* function (i.e., nested functions have no scoping at all and their variables
* leak into the calling function.)
*/
nv_scan(prevscope->save_tree, local_exports, NULL, 0, NV_EXPORT|NV_DYNAMIC|NV_NOSCOPE);
}
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/ksh93/tests/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,20 @@ for command in "" "command"; do
got=$("$SHELL" "$tst")
[[ $exp == "$got" ]] || err_exit "${prefix}KornShell functions executed directly by '.' shouldn't have scoping" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"

# Test 21: typeset -p should print the -D flag when a variable has dynamic scoping applied to it
cat > "$tst" <<-EOF
function foo {
$command declare foo=foo
$command local -l -i bar=2
$command typeset -p foo bar
}
foo
EOF
exp=$'typeset -D foo=foo\ntypeset -D -l -i bar=2'
got=$("$SHELL" "$tst")
[[ $exp == "$got" ]] || err_exit "${prefix}'typeset -p' cannot add the -D flag to output for variables given a dynamic scope" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
done

# ======
Expand Down

0 comments on commit b601a87

Please sign in to comment.