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

Re: command-spelling correction strangeness



On Sep 28, 11:56pm, Paul Kimoto wrote:
} Subject: command-spelling correction strangeness
}
} I have a non-executable, non-zero-size file "ps"
} (-rw-r--r--   1 kimoto   kimoto        268 Mar 30 16:44 ps)
} in a directory ahead of /bin in PATH.  When I have the "correct" and
} "autocd" options set, often zsh offers to correct "ps" to "ls" _after_ 
} "ps" has been run once.

A change was made about a year ago (zsh-workers/4404 and follow-ups) to
remove entries for non-executable files from the command hash table.
This happens only when "autocd" is set, because the only bad side effect
of bogus hash table entries is that if they're directories rather than
non-executable files, you can't autocd into them.

However, if you have the hashcmds option set, the rest of the path is
searched and the correct location gets immediately added back again --
so you do not set hashcmds.  Am I correct?

Here's what happens:

Running any external command causes the command hash table to be filled.
Because the non-executable "ps" is in $PATH ahead of /bin, it gets added
to the hash table and /bin/ps does not.

Later, you run "ps".  The hash entry for "ps" is found to point to the
non-executable file and is therefore deleted from the hash table.  Now
there is no entry for "ps" at all; "/bin/ps" is found by re-searching the
path, but this is not added to the hash table.

Finally, you run "ps" again.  Zsh looks in the hash table and doesn't
find it, so it picks the next closest thing (for a keyboard-oriented
definition of `closest') and asks whether it should correct to that.  On
my system it chooses "psf" which is some kind of postscript print filter,
but on your system it must be "ls".

So ... what you describe may or may not be a bug, but the following should
cause the command table to be updated more intelligently.  I'd be just as
happy with encouraging people not to use "correct" without "hashcmds" ...

Index: Src/exec.c
===================================================================
@@ -1697,14 +1697,16 @@
 	if (!hn) {
 	    /* Resolve external commands */
 	    char *cmdarg = (char *) peekfirst(args);
+	    int dohashcmd = isset(HASHCMDS);
 
 	    hn = cmdnamtab->getnode(cmdnamtab, cmdarg);
 	    if (hn && trycd && !isreallycom((Cmdnam)hn)) {
 		cmdnamtab->removenode(cmdnamtab, cmdarg);
 		cmdnamtab->freenode(hn);
 		hn = NULL;
+		dohashcmd = 1;
 	    }
-	    if (!hn && isset(HASHCMDS) && strcmp(cmdarg, "..")) {
+	    if (!hn && dohashcmd && strcmp(cmdarg, "..")) {
 		for (s = cmdarg; *s && *s != '/'; s++);
 		if (!*s)
 		    hn = (HashNode) hashcmd(cmdarg, pathchecked);


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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