oils-for-unix / oils

Oils is our upgrade path from bash to a better language and runtime. It's also for Python and JavaScript users who avoid shell!
http://www.oilshell.org/
Other
2.84k stars 157 forks source link

Trap does not check for the first argument being an unsigned integer #2055

Open jasom opened 2 months ago

jasom commented 2 months ago

From the POSIX spec:

If the first operand is an unsigned decimal integer, the shell shall treat all operands as conditions, and shall reset each condition to the default value. Otherwise, if there are operands, the first is treated as an action and the remaining as conditions.

I really don't want to know what buggy ill-specified behavior in an old Unix shell caused it to be standardized this way, but other shells appear to implement this:

$ for sh in bash dash ksh osh; do $sh -c 'trap "echo hi" EXIT; trap 1 EXIT'; done
  1
  ^
[ trap word at ? ]:1

[??? no location ???] '1' not found (OILS-ERR-100)
andychu commented 2 months ago

Oh weird, never heard that rule ...

This may need some more work in https://github.com/oils-for-unix/oils/blob/master/frontend/signal_gen.py

jasom commented 2 months ago

This should only require work in https://github.com/oils-for-unix/oils/blob/master/builtin/trap_osh.py to call RemoveUser{Trap|Hook} when the first argument is an unsigned integer, no?

andychu commented 2 months ago

Oh maybe ... the spec tests can answer that. The text is hard for me to parse

Are you interested in adding some tests and maybe implementing it? If it is only clearing traps it should be straightforward

$ test/spec.sh builtin-trap
builtin-trap.test.sh
case    line    dash    bash    mksh    ash     osh
  0       6     pass    pass    pass    pass    pass    trap accepts/ignores --
  1      14     pass    pass    pass    pass    ok      trap 'echo hi' KILL (regression test, caught by smoosh suite)
  2      40     pass    pass    pass    pass    pass    Register invalid trap
  3      44     pass    pass    pass    pass    pass    Remove invalid trap

in spec/builtin-trap.test.sh

https://github.com/oils-for-unix/oils/wiki/Spec-Tests