zsh-users
 help / color / mirror / code / Atom feed
* priority of execution
@ 2022-10-25 15:47 Ray Andrews
  2022-10-25 16:08 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2022-10-25 15:47 UTC (permalink / raw)
  To: Zsh Users

So far I'm aware of these categories of actionable entities:

alias, autoload, builtin, function, executable script, binary.

Any others?  Or any other discriminations possible within those 
categories?  IOW, what's the total list of species that whence might 
report?  And when I have a complete list of such species, what's their 
precedence?  I believe aliases always come first but in case of name 
conflicts I'd like to be sure who has first go. And:

$ whence -v declare
declare is a reserved word

$ whence -av declare
declare is a reserved word
declare is a shell builtin

... isn't the main point that it's a builtin and thus it's a reserved 
word automatically?  I'm not sure that's correct, but it seems to me 
that the name of a builtin must be protected so I'd expect:

$ whence -v declare
declare is a shell builtin   # Which is what I really want to know, and 
that it's reserved is redundant.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: priority of execution
  2022-10-25 15:47 priority of execution Ray Andrews
@ 2022-10-25 16:08 ` Peter Stephenson
  2022-10-25 17:17   ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2022-10-25 16:08 UTC (permalink / raw)
  To: Ray Andrews, Zsh Users

> On 25/10/2022 16:47 Ray Andrews <rayandrews@eastlink.ca> wrote:
> 
>  
> So far I'm aware of these categories of actionable entities:
> 
> alias, autoload, builtin, function, executable script, binary.

The different categories known to whence, from looking at the source,
are aliases, reserved words, shell functions, builtins, and binaries
in the file system (in the case of whence, those found via $PATH).

Autoloadable functions are a special category of function --- the
difference only comes into play once the shell has decided it's
looking for a shell function, so it's hidden from top-level
execution.

The same goes for shell scripts as a special category of
binaries found in the file system.  The shell sees the script is
a binary in the path and tries to execute it just like anything
else marked as executable.  Typically the OS knows how to deal
specially with scripts, though the shell has this knowledge as
a fallback.

> $ whence -av declare
> declare is a reserved word
> declare is a shell builtin

Reserved words are different from builtins in that they have special
syntax --- the shell knows, for example, that "if" isn't just a command
with arguments, it's got to do a lot more work to handle it.  In
principle, if you turned  it off, the shell *could* have an "if"
builtin as well.  That would obviously be stupid.

You've actually hit a special case here where it's not stupid.
This dates from when declare and its relatives were extended to
handle arrays,

declare array=(bray cray dray)

That needs special parsing because the elements of the array have
to be treated as separate words.

The thing about reserved words is the shell has to be able to see
them in time to parse them.  But the following is perfectly valid
syntax, because "declare" can be used as a normal command:

cmd=declare
$cmd var1 var2 var3

The shell sees $cmd and treats it as the start of a normal command
line for expanding later.  By that time it's already parsed the
arguments as a normal command.  Instead, it just treats "declare"
here as a normal command with arguments.  That's why you see it
show up as a shell builtin as well as a reserved word.

That second case is rather special; you wouldn't encounter it very
often.  But people moan horribly if we break their special cases.

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: priority of execution
  2022-10-25 16:08 ` Peter Stephenson
@ 2022-10-25 17:17   ` Ray Andrews
  2022-10-25 18:36     ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2022-10-25 17:17 UTC (permalink / raw)
  To: zsh-users


On 2022-10-25 09:08, Peter Stephenson wrote:
> The different categories known to whence, from looking at the source,
> are aliases, reserved words, shell functions, builtins, and binaries
> in the file system (in the case of whence, those found via $PATH).
In that order?
> Autoloadable functions are a special category of function

But whence reports them specially:

$ whence -v zmv
zmv is an autoload shell function

... I'm just trying to extract as much information as whence can supply.

> --- the
> difference only comes into play once the shell has decided it's
> looking for a shell function, so it's hidden from top-level
> execution.
So when it comes to precedence would a 'normal' function and an 
autoload, if they shared the ... Ah! the can't share the same name 
because if you define a function with the same name as an autoload the 
former is ... how to say it ... overruled.  So that's no issue. But 
whence will report the difference very politely.
> The same goes for shell scripts as a special category of
> binaries found in the file system.  The shell sees the script is
> a binary in the path and tries to execute it just like anything
> else marked as executable.  Typically the OS knows how to deal
> specially with scripts, though the shell has this knowledge as
> a fallback.

So it would be first found, first executed?  Or will there be precedence 
rules?

>
>> $ whence -av declare
>> declare is a reserved word
>> declare is a shell builtin
> That second case is rather special; you wouldn't encounter it very
> often.  But people moan horribly if we break their special cases.

Too heavy for me, that's stuff than only happens in the engine room.

But I'd still say that this should be the output:

$ whence -v declare

declare is a shell builtin

... it might be naive but that's what would seem to be the ... hmmm ... 
if 'reserved word' really does have precedence then maybe that's what's 
got to show, and the fact that I want to see 'shell builtin' is too bad 
for me.  It's intuitive that whence is looking for commands of some 
sort, but maybe strictness is more important.  Nothing is simple.






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: priority of execution
  2022-10-25 17:17   ` Ray Andrews
@ 2022-10-25 18:36     ` Peter Stephenson
  2022-10-25 21:58       ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2022-10-25 18:36 UTC (permalink / raw)
  To: zsh-users

On Tue, 2022-10-25 at 10:17 -0700, Ray Andrews wrote:
> On 2022-10-25 09:08, Peter Stephenson wrote:
> > The different categories known to whence, from looking at the source,
> > are aliases, reserved words, shell functions, builtins, and binaries
> > in the file system (in the case of whence, those found via $PATH).
> 
> In that order?

Aliases are parsed while stuff is still being read in.  This is why
they're the only type that allow you to use the name of the alias as
some other type of command.

Reserved words are handled after the line has been read in, but still
quite early in parsing, because, as I explained, they need to know about
the structure of what follows.

Everying else is only examined during execution (so if a function
contains the name of one of the following types, it won't be decided
until the function's run which sort it's using --- that's not true of
aliases and reserved words, their effect gets baked into the function).

Shell functions are looked for first.  This means you can replace a
builtin or an external command with a function of the same name, and
still call e.g.

whence() { print This is a special whence; builtin whence "$@"; }
ls() { print Tthis is a special ls; command ls "$@"; }

Builtins are next, which means a builtin with the same name as an
external command is called in preference.  This is usually useful as its
faster, e.g. "test", but sometimes an annoyance, e.g. "enable" is a
shell command but may be an external printer command.  You can disable
builtins you don't need, or use a shell function workaround ("command"
always calls an external command in zsh, unlike other shells, unless
you're in compatibility mode).

External commands are last, with their own rules about the use of $PATH
or absolute paths or those relative to the current directory.

> > Autoloadable functions are a special category of function
> 
> But whence reports them specially:

Yes, just to be helpful.  You'll see once the function has been
autoloaded, it's just a normal function, and if you define a normal
function with that name the autoload flag just vanishes.  It's
really ordering rather than precedence.  If the function's already defined
it's not autoloadable, and it'll ignore attempts to make it so unless
you undefine it first.  Autoloading is just an instruction about how to
get the function if it's not there, it's not really a type of function
at all.

pws



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: priority of execution
  2022-10-25 18:36     ` Peter Stephenson
@ 2022-10-25 21:58       ` Ray Andrews
  0 siblings, 0 replies; 5+ messages in thread
From: Ray Andrews @ 2022-10-25 21:58 UTC (permalink / raw)
  To: zsh-users


On 2022-10-25 11:36, Peter Stephenson wrote:
> Yes, just to be helpful. You'll see once the function has been
> autoloaded, it's just a normal function, and if you define a normal
> function with that name the autoload flag just vanishes.

Right, I've done the experiment, whence is exactly up to date on that.  
But it is helpful.


> It's
> really ordering rather than precedence.  If the function's already defined
> it's not autoloadable, and it'll ignore attempts to make it so unless
> you undefine it first.  Autoloading is just an instruction about how to
> get the function if it's not there, it's not really a type of function
> at all.

Thanks Peter, that's all quite clear and logical.



>
> pws
>
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-10-25 21:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-25 15:47 priority of execution Ray Andrews
2022-10-25 16:08 ` Peter Stephenson
2022-10-25 17:17   ` Ray Andrews
2022-10-25 18:36     ` Peter Stephenson
2022-10-25 21:58       ` Ray Andrews

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).