2020-04-19 20:51 Robert Elz <k...@munnari.oz.au>:
> Before I make any other comments, I should point out that quite a
> bit of what is in the shell standard (and the rest of POSIX, and for
> that matter, many other standards) isn't there because there's a
> good reason for it, but just because the original shells implemented
> it that way, often for expediency purposes (the original Bourne
> shell had to run in the 64KB address space (code, data, and stack)
> of a PDP-11).

Thank you for pointing out this.  After reading your comment, I was
interested in the behavior of the Bourne shell, so I have tried with
the Heirloom Bourne shell (which is a port of the SVR4 Bourne shell
according to its page) in AUR.  Actually, the Heirloom Bourne shell
turns out to follow the interpretation (A) [ although it might be
possible that the behavior is changed by the port ].

2020-04-19 20:51 Robert Elz <k...@munnari.oz.au>:
> Note that another intrepretation that you didn't list is that this
> magic return applies to return in functions called directly from the
> trap action string (which are more likely to be functions written
> expressly with the intent of being called this way) but not to
> functions called indirectly.

Thank you for suggesting another possibility.  Let me call the above
one the interpretation (D).  I have updated the script to distinguish
(D) from (B):

  setexit() { return "$1"; }
  invoke() { kill -USR1 $$; return 222; }

  trap 'setexit 111; return' USR1
  invoke
  case $? in
  0)   echo 'In trap argument: last command preceding the trap action' ;;
  111) echo 'In trap argument: last command in the trap action' ;;
  222) echo 'In trap argument: (failed to exit the function)' ;;
  *)   echo 'In trap argument: (unexpected)' ;;
  esac

  stat=99
  handler() { setexit 111; return; }
  trap 'handler; stat=$?; return' USR1
  invoke
  case $stat in
  0)   echo 'In direct function call: last command preceding the trap action' ;;
  111) echo 'In direct function call: last command in the trap action' ;;
  *)   echo 'In direct function call: (unexpected)' ;;
  esac

  stat=99
  utility2() { setexit 111; return; }
  handler2() { utility2; stat=$?; }
  trap 'handler2' USR1
  invoke
  case $stat in
  0)   echo 'In indirect function call: last command preceding the
trap action' ;;
  111) echo 'In indirect function call: last command in the trap action' ;;
  *)   echo 'In indirect function call: (unexpected)' ;;
  esac

But there was actually no implementation that follows (D) among the
shells that I tested so far.  Here is the updated list:

  (A) `zsh', `dash', `busybox', `heirloom-sh'
  (B) `bash-4.4', `mksh', `ksh'
  (C) `yash'
  (D) none
  Not Implemented: `bash-4.3', `posh'

--
Koichi

Reply via email to