Re: bug: can't break/continue from within eval (plus, status 0 on error)

2017-04-07 Thread Martijn Dekker
Op 08-04-17 om 00:36 schreef Chet Ramey:
> On 4/7/17 6:56 PM, Martijn Dekker wrote:
> 
>> A second thing:
>>
>> $ mksh -o posix -c 'break; echo $?'
>> mksh: break: can't break
>> 0
>> $ mksh -o posix -c 'continue; echo $?'
>> mksh: continue: can't continue
>> 0
>>
>> 'break' and 'continue' are POSIX "special builtins", meaning if they
>> fail they should cause the shell to exit, at least in POSIX mode.
> 
> Posix explicitly makes this case unspecified.

So it does. My bad.

Remains the question whether the current behaviour is actually sensible.

- M.



Re: bug: can't break/continue from within eval (plus, status 0 on error)

2017-04-07 Thread Chet Ramey
On 4/7/17 6:56 PM, Martijn Dekker wrote:

> A second thing:
> 
> $ mksh -o posix -c 'break; echo $?'
> mksh: break: can't break
> 0
> $ mksh -o posix -c 'continue; echo $?'
> mksh: continue: can't continue
> 0
> 
> 'break' and 'continue' are POSIX "special builtins", meaning if they
> fail they should cause the shell to exit, at least in POSIX mode.

Posix explicitly makes this case unspecified.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/


bug: can't break/continue from within eval (plus, status 0 on error)

2017-04-07 Thread Martijn Dekker
$ mksh -o posix -c 'for x in 1 2 3; do eval "break"; done'
mksh: break: can't break
mksh: break: can't break
mksh: break: can't break
$ mksh -o posix -c 'for x in 1 2 3; do eval "continue"; done'
mksh: continue: can't continue
mksh: continue: can't continue
mksh: continue: can't continue

The "break"/"continue" executed within the loop, so this should work (as
it does on every other shell except pdksh).

Actual use case, as requested: one of my shell functions contains a
loop, within which there is a 'case' construct wrapped in 'eval' so it
can use a variable referenced by another variable, and it needs to be
able to conditionally break out of the loop based on its value. Since
mksh won't tolerate the 'break' within the 'eval', I need to make the
code more awkward by moving the 'break' outside of the 'case' within the
'eval' while still keeping it conditional.

A second thing:

$ mksh -o posix -c 'break; echo $?'
mksh: break: can't break
0
$ mksh -o posix -c 'continue; echo $?'
mksh: continue: can't continue
0

'break' and 'continue' are POSIX "special builtins", meaning if they
fail they should cause the shell to exit, at least in POSIX mode.

And in this case I can see good reasons for that. A failing
'break'/'continue' is a strong indicator that the program is in an
inconsistent state where continuing would make no sense and may very
well be harmful.

In non-POSIX mode, it's your decision whether they should exit or not,
but they should at the very least return a nonzero exit status on error.

Thanks,

- M.