zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: a 'require' function for zsh scripts and interactive functions
Date: Thu, 3 May 2012 21:37:25 +0100	[thread overview]
Message-ID: <20120503213725.751d7e7b@pws-pc.ntlworld.com> (raw)
In-Reply-To: <120503082329.ZM13981@torch.brasslantern.com>

On Thu, 03 May 2012 08:23:29 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
>   require () {
>     setopt localtraps
>     for UTIL
>     do
>       trap "msg 'No $UTIL found'" EXIT
>       : ${commands[$UTIL]:?require failed}
>     done
>     trap - EXIT
>   }
> 
> There's one more gotcha with that which may be a bug:  The EXIT trap is
> skipped when the function returns with 'require failed' (but still run
> by non-interactive shells when an actual exit occurs).

It's not entirely clear whether it's correct, since we don't explicitly
define what it should do and EXIT traps in functions are specific to
zsh, but it's plausible.  The ":?"  syntax causes immediate exit from
the shell, or back to the prompt: in other words, it's treated as an
immediate error, i.e. the same as e.g.:

% fn() { trap 'echo EXITING' EXIT; echo ${**}; }
% fn
fn: bad substitution
% fn() { trap 'echo EXITING' EXIT; echo ${foo:?nope, sorry}; }
% fn
fn: foo: nope, sorry

The cases are, however, different when you're exiting the shell:

% zsh -fc 'trap "echo EXITING" EXIT; echo ${foo:?nope, sorry}' 
zsh:1: foo: nope, sorry
EXITING
% zsh -fc 'trap "echo EXITING" EXIT; echo ${**}'              
zsh:1: bad substitution

Bash calls the trap in both cases.

You can argue the function and shell cases are different since the ":?"
might be considered to cause the function to abort, with nothing more in
the main body of the script executing, but then the shell to exit
normally (apart from the status), so the EXIT trap would be run.

So there are actually 16 possibilities, though the realistic ones are
where the trap occurs additionally in one or more of the three remaining
cases.

You might well consider the most useful behaviour is for the trap
to happen in all cases, since it's there to clear up.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


      reply	other threads:[~2012-05-03 20:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-03 13:56 TJ Luoma
2012-05-03 15:23 ` Bart Schaefer
2012-05-03 20:37   ` Peter Stephenson [this message]

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=20120503213725.751d7e7b@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.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).