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

Re: command-not-found handler

On Wed, 11 Jul 2007 11:25:12 +0100
Peter Stephenson <pws@xxxxxxx> wrote:
> On Tue, 10 Jul 2007 21:52:23 -1100
> "Yakov Lerner" <iler.ml@xxxxxxxxx> wrote:
> > How do I handle the "command not found" event with my own function.
> > Like bash's command_not_found_handle().

(Yes, this does appear to be a Debian extension.)

> There's no direct way of doing this; you have to intercept whatever
> it is you're trying to do earlier on.  (If such a command was added
> it would be in a subshell forked to run the command; I'm not sure if
> that would be good enough.)

I've no idea how widely useful this is, or whether a more zsh-specific name
(or hook mechanism, consistent with precommand, periodic, etc.---this is a
little different since you can only handle the condition once) would be
better, but it's very easy to add.  Feedback appreciated.  I'll add a test
if this turns out to be useful.  (Note I called it _handle*r* since that
seemed more natural.  A handler is something that handles an error.  A
handle is a reference to a data structure.  If it means
handle_command_not_found it should say so.  However, that's just me being
picky and I'm not particularly bothered.)

It's an interesting project to see if we can trap "command not found"
earlier, before forking, so that you don't lose the shell on a failed
"exec".  This would be done by performing all the usual checks to find a
command earlier, if found remembering the result for use after forking, and
handling the error immediately if not.  I've never awakened quite enough
interest to do this.

Index: Doc/Zsh/exec.yo
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/exec.yo,v
retrieving revision
diff -u -r1.1.1.1 exec.yo
--- Doc/Zsh/exec.yo	15 Apr 1999 18:05:36 -0000
+++ Doc/Zsh/exec.yo	11 Jul 2007 10:58:54 -0000
@@ -5,6 +5,8 @@
 cindex(command execution)
 cindex(execution, of commands)
+cindex(command not found, handling of)
 If a command name contains no slashes, the shell attempts to locate
 it.  If there exists a shell function by that name, the function
 is invoked as described in noderef(Functions).  If there exists
@@ -23,3 +25,13 @@
 specifies an interpreter for the program.  The shell will
 execute the specified interpreter on operating systems that do
 not handle this executable format in the kernel.
+If no external command is found but a function tt(command_not_found_handler)
+exists the shell executes this function with all
+command line arguments.  The function should return status zero if it
+successfully handled the command, or non-zero status if it failed.
+In the latter case the standard handling is applied: `command not
+found' is printed to standard error and the shell exits with status 127.
+Note that the handler is executed in a subshell forked to execute
+an external command, hence changes to directories, shell parameters,
+etc. have no effect on the main shell.
Index: Src/exec.c
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.118
diff -u -r1.118 exec.c
--- Src/exec.c	6 Jul 2007 21:52:39 -0000	1.118
+++ Src/exec.c	11 Jul 2007 10:58:55 -0000
@@ -468,6 +468,25 @@
 	    e != ENOENT && e != ENOTDIR); 
+ * Attempt to handle command not found.
+ * Return 0 if the condition was handled, non-zero otherwise.
+ */
+static int
+commandnotfound(char *arg0, LinkList args)
+    Shfunc shf = (Shfunc)
+	shfunctab->getnode(shfunctab, "command_not_found_handler");
+    if (!shf)
+	return 127;
+    pushnode(args, arg0);
+    return doshfunc(shf->node.nam, shf->funcdef, args, shf->node.flags, 1);
 /* execute an external command */
@@ -562,6 +581,8 @@
 	if (!ps) {
+	    if (commandnotfound(arg0, args) == 0)
+		_exit(0);
 	    zerr("command not found: %s", arg0);
@@ -624,6 +645,8 @@
     if (eno)
 	zerr("%e: %s", eno, arg0);
+    else if (commandnotfound(arg0, args) == 0)
+	_exit(0);
 	zerr("command not found: %s", arg0);
     _exit((eno == EACCES || eno == ENOEXEC) ? 126 : 127);

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php

To get further information regarding CSR, please visit our Investor Relations page at http://ir.csr.com/csr/about/overview

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