zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zach Riggle <zachriggle@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Differences between "TRAPERR()" and "trap foo ERR"
Date: Tue, 27 Jul 2021 22:00:17 -0700	[thread overview]
Message-ID: <CAH+w=7bP3d+Ju12+73DJpfEOjj+nsRctd75b=YcMC7k8w=19hw@mail.gmail.com> (raw)
In-Reply-To: <CAMP9c5kMuHeeUDRn1dnNLJYbK+6nY_c7jU8y7pikFsAGA5BzuQ@mail.gmail.com>

On Tue, Jul 27, 2021 at 8:47 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> Overall, there are two observations.
>
> 1. The behavior that TRAPNAL will receive NAL as its first argument,
> where NAL corresponds to a the system-defined signal values (e.g.
> SIGSEGV == 11).  The name for this signal can be gotten from
> ${signals[x+1]}.  This should be documented.

9.3.2 Trap Functions:

TRAPNAL
     If defined and non-null, this function will be executed whenever
     the shell catches a signal SIGNAL, where NAL is a signal name as
     specified for the kill builtin.  The signal number will be passed
     as the first parameter to the function.

> 2. This behavior does not expand to "trap func NAL" statements, even
> if we try to pass "$@" to some function.  This seems like a bug.

It's not a bug; it's the normal behavior of the "trap" in the shell
language.  The TRAPnal form and the argument it receives are a zsh
extension.

The "trap" builtin takes an arbitrary list of commands rather than a
function body.  There is therefore no "argument list" where the signal
number can be passed.  If you write

  trap 'echo in a trap' USR2
  functions

you will see that there is no TRAPUSR2 function created.  "unfunction
TRAPUSR2" removes the USR2 trap as a side-effect, but also complains
"no such hash table element".  That's a bit of an oddity, but is in
part why the doc for the "trap" builtin says:

     Defining a trap under either name causes any trap under an
     alternative name to be removed.  However, it is recommended that
     for consistency users stick exclusively to one name or another.

Other distinctions considered important are also mentioned there:

     Note that traps defined with the trap builtin are slightly
     different from those defined as 'TRAPNAL () { ...  }', as the
     latter have their own function environment (line numbers, local
     variables, etc.)  while the former use the environment of the
     command in which they were called.  For example,

          trap 'print $LINENO' DEBUG

     will print the line number of a command executed after it has run,
     while

          TRAPDEBUG() { print $LINENO; }

     will always print the number zero.

You might also read about the POSIX_TRAPS and LOCAL_TRAPS setopts.


  reply	other threads:[~2021-07-28  5:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-28  3:47 Zach Riggle
2021-07-28  5:00 ` Bart Schaefer [this message]
2021-07-28  5:04   ` Zach Riggle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7bP3d+Ju12+73DJpfEOjj+nsRctd75b=YcMC7k8w=19hw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zachriggle@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).