Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: chpwd, precmd hooks have "zsh" in $0

On Tue, Aug 10, 2021 at 4:33 AM Roman Neuhauser <neuhauser@xxxxxxxxxx> wrote:
> how did this happen?

I return to what I told Marlon the other day:

The answer to "why doesn't feature A take advantage of feature B" is
often "because A was developed long before B".

The code for calling the hook functions has been around since long
before the FUNCTION_ARGZERO option was added, and nobody thought to
update all the entry points.  When the code for the arrays of hooks
was further added, it just passed through what the base function was
already getting.

> come to think of it, if so taxing to implement fully, why is it built
> into the shell in the first place?

It's not "taxing", it's just forgotten.  It was built into the C code
so that people's existing hook functions (precmd, preexec, chpwd,
etc.) wouldn't have to be changed/overwritten to "know about" the new
special arrays, and so that e.g. module writers could plug into those
arrays without fear that a user could change the calling protocol.

> and is it too late to reverse the
> course, gut the half-assed implementation with one written using shell
> functions?

There's no need to be offensive about it, but yes, it's too late.  The
implementation actually has all the interfaces it needs to populate
FUNCTION_ARGZERO properly, it was just never noticed/found necessary
to do so.

> > I'm not sure what the desired behavior is here.  callhookfunc() could
> > dummy up a LinkNode any time its lnklst argument is empty, and then
> > all the hooks would behave in the manner preexec behaves when history
> > is enabled; or doshfunc() could do the same, in which case every
> > hook-array function would have its own name in $0; or neither of the
> > above is appropriate and we should document the current behavior.
> what are the downsides of (2) for users of the shell?

Theoretically, a function might prefer to know that it was being
called as a consequence of being found in a given hook list, vs. being
called directly.  That might argue for (1).  However, since the
current situation is (3), the only instance where that could exist in
practice is for the preexec hooks, so we're probably safe choosing

I will note, to avoid yet again having an "A forgot B" situation, that
Zftp module hooks also behave like (3) and would begin to have their
own name in $0 if we change doshfunc().

>     function a b { ... } > ab.log
>     precmd_functions=(a)
>     preexec_functions=(b)
> calls the function with $0 == "a" in precmd, "b" in preexec,
> and that their runtime output goes to ab.log.  hooks do neither
> and TRAPNAL functions only the first. :(

I'm not sure what "the first" is here?

Anyway, having output redirections on function definitions is another
thing that was added quite late in the long history of the shell --
originally, the redirect would have been connected to the "function"
command itself -- so there may be circumstances where you have to

  function a b { { ... } > ab.log }

to be sure of getting what you meant there.  PWS may have more to say about it.

Messages sorted by: Reverse Date, Date, Thread, Author