From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24767 invoked from network); 11 Jul 2007 11:16:49 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.1 (2007-05-02) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.1 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 11 Jul 2007 11:16:49 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 11260 invoked from network); 11 Jul 2007 11:16:43 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 11 Jul 2007 11:16:43 -0000 Received: (qmail 22191 invoked by alias); 11 Jul 2007 11:16:41 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 23671 Received: (qmail 22180 invoked from network); 11 Jul 2007 11:16:40 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 11 Jul 2007 11:16:40 -0000 Received: (qmail 10979 invoked from network); 11 Jul 2007 11:16:40 -0000 Received: from cluster-g.mailcontrol.com (85.115.41.190) by a.mx.sunsite.dk with SMTP; 11 Jul 2007 11:16:35 -0000 Received: from cameurexb01.EUROPE.ROOT.PRI ([62.189.241.200]) by rly04g.srv.mailcontrol.com (MailControl) with ESMTP id l6BBGWEJ020215 for ; Wed, 11 Jul 2007 12:16:33 +0100 Received: from news01.csr.com ([10.103.143.38]) by cameurexb01.EUROPE.ROOT.PRI with Microsoft SMTPSVC(6.0.3790.1830); Wed, 11 Jul 2007 12:16:32 +0100 Date: Wed, 11 Jul 2007 12:16:32 +0100 From: Peter Stephenson To: "Zsh Hackers' List" Subject: Re: command-not-found handler Message-ID: <20070711121632.58a8ecdf@news01.csr.com> In-Reply-To: <20070711112512.3abd2809@news01.csr.com> References: <20070711112512.3abd2809@news01.csr.com> Organization: CSR X-Mailer: Claws Mail 2.9.1 (GTK+ 2.10.12; i386-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 11 Jul 2007 11:16:32.0221 (UTC) FILETIME=[EF6FA0D0:01C7C3AC] X-Scanned-By: MailControl A-07-07-05 (www.mailcontrol.com) on 10.71.0.114 On Wed, 11 Jul 2007 11:25:12 +0100 Peter Stephenson wrote: > On Tue, 10 Jul 2007 21:52:23 -1100 > "Yakov Lerner" 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 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