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

Re: default command function



Gregory D. Fast wrote:
>Perl has an amusing feature whereby the sub named AUTOLOAD, if defined
>in the current package, gets called in place of a "subroutine not
>found" error.  The idea is that AUTOLOAD reads the sub definition from
>somewhere, breathes life into it, and then calls it.  It basically allows
>you to create default behavior for undefined subroutines.
>
>I like it a lot, and wished that I could somehow trick zsh into acting
>likewise, so I modified exec.c so that if a command is not found, it execs
>(assuming it's defined) _default_cmd(), with the (failed) command line
>as its args.

Unfortunately, by the time the command failure is detected, the shell has
already forked, so the most useful application of this sort of feature
-- defining a shell function -- is not possible.  In fact, very little
of any utility is possible.  Therefore this patch is not going into the
next zefram release.

I'd like to have some way for the main shell process to detect reliably
whether external command execution failed, so that this could be done
properly.  Perhaps a close-on-exec pipe, with an error number being sent
down the pipe in case of error?  This would open up other possibilities:
the command search mechanism could be put completely under user control,
with the possibility of user- (or module-) defined forms of command
execution.

Perhaps the command search order (builtin, function, external command,
...) could go into a shell parameter, making `builtin' and `command'
just special cases of this more general facility.  (I think this
sort of feature was first suggested to me by Richard, and I have
recently implemented it, in a very general form, in a shell I wrote
for a non-Unix OS.)

I envision having a new hash table, for command executors; these would
be defined the way builtin commands are (some in the base executable,
with modules being able to add to the table).  When simple commands are
executed then the command executors named by an array parameter would be
tried in sequence.  Really wacky idea: a module could provide a builtin
that provides the capability to invent new command executors based on
a shell function, which gets run with the relevant parameter modified.

-zefram



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