Skip to content

Commit

Permalink
[osh-language] Fix default value of 'exit' and 'return'.
Browse files Browse the repository at this point in the history
Addresses issue #423.
  • Loading branch information
Andy Chu committed Jul 12, 2019
1 parent ab3043c commit 9947939
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
8 changes: 6 additions & 2 deletions osh/cmd_exec.py
Expand Up @@ -878,6 +878,8 @@ def _Dispatch(self, node, fork_external):
# false; echo $?; local f=x; echo $?

elif node.tag == command_e.ControlFlow:
tok = node.token

if node.arg_word: # Evaluate the argument
val = self.word_ev.EvalWordToString(node.arg_word)
assert val.tag == value_e.Str
Expand All @@ -887,12 +889,14 @@ def _Dispatch(self, node, fork_external):
e_die('%r expected a number, got %r',
node.token.val, val.s, word=node.arg_word)
else:
arg = 0 # return 0, exit 0, break 0 levels, etc.
if tok.id in (Id.ControlFlow_Exit, Id.ControlFlow_Return):
arg = self.mem.LastStatus()
else:
arg = 0 # break 0 levels, nothing for continue

# NOTE: A top-level 'return' is OK, unlike in bash. If you can return
# from a sourced script, it makes sense to return from a main script.
ok = True
tok = node.token
if (tok.id in (Id.ControlFlow_Break, Id.ControlFlow_Continue) and
self.loop_level == 0):
ok = False
Expand Down
11 changes: 11 additions & 0 deletions spec/func.test.sh
Expand Up @@ -70,3 +70,14 @@ f
echo "[$mylocal $myglobal]"
## stdout-json: "[L G]\n[ G]\n"
## status: 0

#### Return without args gives previous
f() {
( exit 42 )
return
}
f
echo status=$?
## STDOUT:
status=42
## END
33 changes: 0 additions & 33 deletions spec/sh-options.test.sh
Expand Up @@ -27,39 +27,6 @@ $SH -i -c 'echo $-' | grep -q i && echo TRUE
FALSE
TRUE
## END

#### sh -c
$SH -c 'echo hi'
## stdout: hi
## status: 0

#### empty -c input
# had a bug here
$SH -c ''
## stdout-json: ""
## status: 0

#### empty stdin
# had a bug here
echo -n '' | $SH
## stdout-json: ""
## status: 0

#### args are passed
$SH -c 'argv.py "$@"' dummy a b
## stdout: ['a', 'b']

#### args that look like flags are passed after script
script=$TMP/sh1.sh
echo 'argv.py "$@"' > $script
chmod +x $script
$SH $script --help --help -h
## stdout: ['--help', '--help', '-h']

#### args that look like flags are passed after -c
$SH -c 'argv.py "$@"' --help --help -h
## stdout: ['--help', '-h']

#### pass short options on command line
$SH -e -c 'false; echo status=$?'
## stdout-json: ""
Expand Down
5 changes: 5 additions & 0 deletions test/spec.sh
Expand Up @@ -507,6 +507,11 @@ var-sub-quote() {
${REF_SHELLS[@]} $OSH_LIST "$@"
}

sh-usage() {
sh-spec spec/sh-usage.test.sh \
${REF_SHELLS[@]} $OSH_LIST "$@"
}

sh-options() {
sh-spec spec/sh-options.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH_LIST "$@"
Expand Down

0 comments on commit 9947939

Please sign in to comment.