zsh-workers
 help / color / mirror / code / Atom feed
* Re: command-not-found handler
       [not found] ` <20070711112512.3abd2809@news01.csr.com>
@ 2007-07-11 11:16   ` Peter Stephenson
  2007-07-11 17:44     ` Phil Pennock
  2007-07-13 10:13     ` Peter Stephenson
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Stephenson @ 2007-07-11 11:16 UTC (permalink / raw)
  To: Zsh Hackers' List

On Wed, 11 Jul 2007 11:25:12 +0100
Peter Stephenson <pws@csr.com> wrote:
> On Tue, 10 Jul 2007 21:52:23 -1100
> "Yakov Lerner" <iler.ml@gmail.com> 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 1.1.1.1
diff -u -r1.1.1.1 exec.yo
--- Doc/Zsh/exec.yo	15 Apr 1999 18:05:36 -0000	1.1.1.1
+++ 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)
+findex(command_not_found_handler)
 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);
 	    _exit(127);
 	}
@@ -624,6 +645,8 @@
 
     if (eno)
 	zerr("%e: %s", eno, arg0);
+    else if (commandnotfound(arg0, args) == 0)
+	_exit(0);
     else
 	zerr("command not found: %s", arg0);
     _exit((eno == EACCES || eno == ENOEXEC) ? 126 : 127);


-- 
Peter Stephenson <pws@csr.com>                  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


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

* Re: command-not-found handler
  2007-07-11 11:16   ` command-not-found handler Peter Stephenson
@ 2007-07-11 17:44     ` Phil Pennock
  2007-07-13 17:52       ` Phil Pennock
  2007-07-13 10:13     ` Peter Stephenson
  1 sibling, 1 reply; 4+ messages in thread
From: Phil Pennock @ 2007-07-11 17:44 UTC (permalink / raw)
  To: Zsh Hackers' List

On 2007-07-11 at 12:16 +0100, Peter Stephenson wrote:
> 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.)

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

I think that the Debian bash extender was just as picky.

  command_not_found_handle
    The name of a shell function to be called if a command cannot be
    found. The return value of this function  should be  0,  if the
    command is available after execution of the function, otherwise 127
    (EX_NOTFOUND).  Enabled only in interactive, non POSIX mode shells.
    This is a Debian extension.

So I interpret that as meaning:
  unsplat_my_goofs() { blah; return 127; }
  command_not_found_handle=unsplat_my_goofs

However, I'm unable to get this to actually work on the Ubuntu-derived
distribution I'm using.

-Phil


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

* Re: command-not-found handler
  2007-07-11 11:16   ` command-not-found handler Peter Stephenson
  2007-07-11 17:44     ` Phil Pennock
@ 2007-07-13 10:13     ` Peter Stephenson
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2007-07-13 10:13 UTC (permalink / raw)
  To: Zsh Hackers' List

On Wed, 11 Jul 2007 12:16:32 +0100
Peter Stephenson <pws@csr.com> wrote:
> +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.

I've committed this: it's small, potentially useful, and didn't generate
any controversy.

-- 
Peter Stephenson <pws@csr.com>                  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


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

* Re: command-not-found handler
  2007-07-11 17:44     ` Phil Pennock
@ 2007-07-13 17:52       ` Phil Pennock
  0 siblings, 0 replies; 4+ messages in thread
From: Phil Pennock @ 2007-07-13 17:52 UTC (permalink / raw)
  To: Zsh Hackers' List

On 2007-07-11 at 10:44 -0700, Phil Pennock wrote:
> I think that the Debian bash extender was just as picky.

Looked again; nope, Peter implemented exactly what bash does, the
bash/Debian man-page was badly worded.  Sorry.

-Phil


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

end of thread, other threads:[~2007-07-13 17:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <f36b08ee0707110152m7f7b7215j364d469209793c27@mail.gmail.com>
     [not found] ` <20070711112512.3abd2809@news01.csr.com>
2007-07-11 11:16   ` command-not-found handler Peter Stephenson
2007-07-11 17:44     ` Phil Pennock
2007-07-13 17:52       ` Phil Pennock
2007-07-13 10:13     ` Peter Stephenson

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