Permalink
Please sign in to comment.
Browse files
Make the behavior of top-level return/break/continue follow dash/mksh.
Bash's behavior is inconsistent. If you can return from a source'd script, you should be able to return from the main script. This behavior is OK for running Aboriginal Linux. But the old behavior of disallowing return at the top level was not OK, and it didn't match that of any shell. Also add set -o strict-control-flow -- the first STRICT option. It disallows 'continue' and 'break' at the top level, but allows 'return'.
- Loading branch information...
Showing
with
183 additions
and 85 deletions.
- +3 −0 README.md
- +2 −0 core/args.py
- +1 −2 core/builtin.py
- +80 −41 core/cmd_exec.py
- +10 −1 core/state.py
- +0 −7 gold/return-top-level.sh
- +0 −7 spec/builtins.test.sh
- +0 −9 spec/func.test.sh
- +0 −17 spec/loop.test.sh
- +8 −0 spec/osh-only.test.sh
- +46 −0 spec/strict-options.test.sh
- 0 spec/{ → testdata}/return-helper.sh
- +27 −0 spec/testdata/top-level-control-flow.sh
- +6 −1 test/spec.sh
| @@ -0,0 +1,46 @@ | ||
| #!/usr/bin/env bash | ||
| ### Sourcing a script that returns is allowed no matter what | ||
| echo one | ||
| . spec/testdata/return-helper.sh | ||
| echo $? | ||
| echo two | ||
| # stdout-json: "one\nreturn-helper.sh\n42\ntwo\n" | ||
| ### top level control flow | ||
| $SH spec/testdata/top-level-control-flow.sh | ||
| # stdout-json: "SUBSHELL\nBREAK\nCONTINUE\nRETURN\n" | ||
| # OK bash stdout-json: "SUBSHELL\nBREAK\nCONTINUE\nRETURN\nDONE\n" | ||
| # status: 0 | ||
| ### errexit and top-level control flow | ||
| $SH -o errexit spec/testdata/top-level-control-flow.sh | ||
| # stdout-json: "SUBSHELL\n" | ||
| # status: 2 | ||
| # OK bash status: 1 | ||
| ### set -o strict-control-flow | ||
| $SH -o strict-control-flow -c 'echo break; break; echo hi' | ||
| # stdout: break | ||
| # status: 1 | ||
| # N-I dash/bash status: 2 | ||
| # N-I dash/bash stdout-json: "" | ||
| # N-I mksh status: 1 | ||
| # N-I mksh stdout-json: "" | ||
| ### return at top level is an error | ||
| return | ||
| echo "status=$?" | ||
| # stdout-json: "" | ||
| # OK bash stdout-json: "status=1\n" | ||
| ### continue at top level is NOT an error | ||
| # NOTE: bash and mksh both print warnings, but don't exit with an error. | ||
| continue | ||
| echo status=$? | ||
| # stdout: status=0 | ||
| ### break at top level is NOT an error | ||
| break | ||
| echo status=$? | ||
| # stdout: status=0 |
File renamed without changes.
Oops, something went wrong.
0 comments on commit
6841a09