zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Assorted TRAPxxx / trap xxx observations
Date: Sat, 27 Jun 2015 12:34:26 -0700	[thread overview]
Message-ID: <150627123426.ZM3772@torch.brasslantern.com> (raw)
In-Reply-To: <150627113525.ZM3645@torch.brasslantern.com>

On Jun 27, 11:35am, Bart Schaefer wrote:
}
} ... they deliberately DID use the trap builtin when I added them, but
} it was determined that some side-effects didn't propagate correctly if
} functions were not used.

Incidentally, anonymous functions provide (inflict?) some interesting 
semantics for the "trap" command.

torch% unsetopt debug_before_cmd
torch% trap '() { print $LINENO $1 } $LINENO' DEBUG
0 2
torch% echo "What's my line?"   
What's my line?
0 3
torch% 

A couple of fun things there.

1. This is probably obvious, but it means you can get the benefits of a
separate function context in the trap without side-effect of the return
value acting as an interrupt.  Are there other non-obvious things?  Any
of this worth adding to the doc?

2. Installing a DEBUG trap with NODEBUG_BEFORE_CMD in effect causes the
setting of the trap to debug itself.

Regarding the return value acting as an interrupt, a nonzero return from
a DEBUG_BEFORE_CMD trap behaves exactly the same as "setopt ERR_EXIT" in
that it prevents the upcoming command from executing.  Either this is an
unintentional change, or we don't need the ERR_EXIT trick any more.

I also previously had not considered the interaction of $? with debug
traps, specifically DEBUG_BEFORE_CMD.  (Sorry, Peter, I'm about to make
your eyes glaze over, again.)  The upshot is that $? in the trap refers
to the previous command in one case, and to the command being debugged
in the second case.

torch% trap '() { print ERR $? }' ZERR
torch% trap '() { print DBG $? }' DEBUG
torch% false
DBG 0
ERR 1
torch% true
DBG 1
torch% unsetopt debugbeforecmd
DBG 0
torch% false
DBG 1
ERR 1
torch% true
DBG 0
torch% 

I suppose $? has to be the status of the previous command in the debug
trap when it runs before, because the upcoming command has to be able
to test the previous command's status.

Mostly this raises documentation issues.  It's not mentioned anywhere
that toggling DEBUG_BEFORE_CMD inside the trap will NOT cause the trap
to execute twice (something that might be interesting if you want to
examine $? both before and after).  It's also not explicitly called out
that the value of $? always refers to whatever executed right before
the trap, which differs with the state of DEBUG_BEFORE_CMD, but this may
be considered obvious.

That's all for now ...

-- 
Barton E. Schaefer


  reply	other threads:[~2015-06-27 19:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26 18:25 Tab completion error after upgrade Thorsten Kampe
2015-06-26 19:06 ` Peter A. Castro
2015-06-26 20:35   ` Thorsten Kampe
2015-06-26 20:42     ` Peter A. Castro
2015-06-26 20:50       ` Thorsten Kampe
2015-06-26 21:52     ` Bart Schaefer
2015-06-27 10:09       ` Thorsten Kampe
2015-06-27 10:12         ` Thorsten Kampe
2015-06-27 16:43           ` Peter Stephenson
2015-06-27 18:35             ` Bart Schaefer
2015-06-27 19:34               ` Bart Schaefer [this message]
2015-06-26 19:45 ` Frank Terbeck
2015-06-27 12:51 ` zzapper

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=150627123426.ZM3772@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).