zsh-users
 help / color / mirror / code / Atom feed
* Exclude non-executables from command completion
@ 2009-09-17 16:08 Eike von Seggern
  2009-09-18 10:27 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Eike von Seggern @ 2009-09-17 16:08 UTC (permalink / raw)
  To: zsh-users

Dear list,

I just changed my default shell from bash to zsh and I haven't regretted
it so far. There's only one thing that comes to my mind every now and
then:

Is it possible, that command completion only completes on commands that
are actually executable? As it happens I have to work on machines that
have non-executables in directories in $PATH.

I already tried something like

zstyle ':completion:*:complete:-command-::commands' ignored-patterns '*(f:u-x:)'

but it didn't work.

Cheers,
    eike


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

* Re: Exclude non-executables from command completion
  2009-09-17 16:08 Exclude non-executables from command completion Eike von Seggern
@ 2009-09-18 10:27 ` Peter Stephenson
  2009-10-02  9:13   ` Eike von Seggern
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2009-09-18 10:27 UTC (permalink / raw)
  To: zsh-users

On Thu, 17 Sep 2009 18:08:26 +0200
Eike von Seggern <eikevons@yahoo.de> wrote:
> Is it possible, that command completion only completes on commands that
> are actually executable? As it happens I have to work on machines that
> have non-executables in directories in $PATH.

I'm not sure why we don't check that commands are executables, was there
ever a reason?

P.S. we are woefully dependent on PATH_MAX.  That's one of those things it
would be really good to fix that's not going to change without a ten-fold
increase in people working on the shell.

Index: Src/hashtable.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/hashtable.c,v
retrieving revision 1.31
diff -u -r1.31 hashtable.c
--- Src/hashtable.c	11 Feb 2009 20:42:16 -0000	1.31
+++ Src/hashtable.c	18 Sep 2009 10:22:05 -0000
@@ -630,20 +630,50 @@
 {
     Cmdnam cn;
     DIR *dir;
-    char *fn;
+    char *fn, *unmetadir, *pathbuf, *pathptr;
+    int dirlen;
 #if defined(_WIN32) || defined(__CYGWIN__)
     char *exe;
 #endif /* _WIN32 || _CYGWIN__ */
 
-    if (isrelative(*dirp) || !(dir = opendir(unmeta(*dirp))))
+    if (isrelative(*dirp))
 	return;
+    unmetadir = unmeta(*dirp);
+    if (!(dir = opendir(unmetadir)))
+	return;
+
+    dirlen = strlen(unmetadir);
+    pathbuf = (char *)zalloc(dirlen + PATH_MAX + 2);
+    sprintf(pathbuf, "%s/", unmetadir);
+    pathptr = pathbuf + dirlen + 1;
 
     while ((fn = zreaddir(dir, 1))) {
 	if (!cmdnamtab->getnode(cmdnamtab, fn)) {
-	    cn = (Cmdnam) zshcalloc(sizeof *cn);
-	    cn->node.flags = 0;
-	    cn->u.name = dirp;
-	    cmdnamtab->addnode(cmdnamtab, ztrdup(fn), cn);
+	    char *fname = ztrdup(fn);
+	    struct stat statbuf;
+	    int add = 0, dummylen;
+
+	    unmetafy(fn, &dummylen);
+	    if (strlen(fn) > PATH_MAX) {
+		/* Too heavy to do all the allocation */
+		add = 1;
+	    } else {
+		strcpy(pathptr, fn);
+		/*
+		 * This is the same test as for the glob qualifier for
+		 * executable plain files.
+		 */
+		if (stat(pathbuf, &statbuf) == 0 &&
+		    S_ISREG(statbuf.st_mode) && (statbuf.st_mode & S_IXUGO))
+		    add = 1;
+	    }
+	    if (add) {
+		cn = (Cmdnam) zshcalloc(sizeof *cn);
+		cn->node.flags = 0;
+		cn->u.name = dirp;
+		cmdnamtab->addnode(cmdnamtab, fname, cn);
+	    } else
+		zsfree(fname);
 	}
 #if defined(_WIN32) || defined(__CYGWIN__)
 	/* Hash foo.exe as foo, since when no real foo exists, foo.exe
@@ -664,6 +694,7 @@
 #endif /* _WIN32 || __CYGWIN__ */
     }
     closedir(dir);
+    zfree(pathbuf, dirlen + PATH_MAX + 2);
 }
 
 /* Go through user's PATH and add everything to *


-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: Exclude non-executables from command completion
  2009-09-18 10:27 ` Peter Stephenson
@ 2009-10-02  9:13   ` Eike von Seggern
  2009-10-02 10:04     ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Eike von Seggern @ 2009-10-02  9:13 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

On Fri, Sep 18, 2009 at 11:27 +0100, Peter Stephenson wrote:
> On Thu, 17 Sep 2009 18:08:26 +0200
> Eike von Seggern <eikevons@yahoo.de> wrote:
> > Is it possible, that command completion only completes on commands that
> > are actually executable? As it happens I have to work on machines that
> > have non-executables in directories in $PATH.
> 
> I'm not sure why we don't check that commands are executables, was there
> ever a reason?
> 
> P.S. we are woefully dependent on PATH_MAX.  That's one of those things it
> would be really good to fix that's not going to change without a ten-fold
> increase in people working on the shell.
 
Hi Peter,

I've had a look in the current HEAD version of hashtable.c, though I
have not compiled and tried it hands-on. But if I'm right, only
regular&&executable files are added to the hashtable. At least on my
Debian system, there are a lot of symlinks in /usr/bin to keep track of
different versions. So I think you should keep symlinks in mind, aswell.
If that's already the case, sorry for this, but my libc knowledge is
really small.

Cheers,
    eike


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

* Re: Exclude non-executables from command completion
  2009-10-02  9:13   ` Eike von Seggern
@ 2009-10-02 10:04     ` Peter Stephenson
  2009-10-02 10:59       ` Eike von Seggern
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2009-10-02 10:04 UTC (permalink / raw)
  To: zsh-users

Eike von Seggern wrote:
> I've had a look in the current HEAD version of hashtable.c, though I
> have not compiled and tried it hands-on. But if I'm right, only
> regular&&executable files are added to the hashtable.

I think we're OK: we test using the information returned by stat whether
the file is regular and executable, but the information was retrieved
using the stat() call (not lstat()), so any symbolic links will have
been dereferenced (unless they're broken, of course).  Tests seem to
confirm this is working.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: Exclude non-executables from command completion
  2009-10-02 10:04     ` Peter Stephenson
@ 2009-10-02 10:59       ` Eike von Seggern
  0 siblings, 0 replies; 5+ messages in thread
From: Eike von Seggern @ 2009-10-02 10:59 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

On Fri, Oct 02, 2009 at 11:04 +0100, Peter Stephenson wrote:
> Eike von Seggern wrote:
> > I've had a look in the current HEAD version of hashtable.c, though I
> > have not compiled and tried it hands-on. But if I'm right, only
> > regular&&executable files are added to the hashtable.
> 
> I think we're OK: we test using the information returned by stat whether
> the file is regular and executable, but the information was retrieved
> using the stat() call (not lstat()), so any symbolic links will have
> been dereferenced (unless they're broken, of course).  Tests seem to
> confirm this is working.

Cool! And thanks a lot for all your effort!

Best
    eike


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

end of thread, other threads:[~2009-10-02 11:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-17 16:08 Exclude non-executables from command completion Eike von Seggern
2009-09-18 10:27 ` Peter Stephenson
2009-10-02  9:13   ` Eike von Seggern
2009-10-02 10:04     ` Peter Stephenson
2009-10-02 10:59       ` Eike von Seggern

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