lineedit: support empty PATH entries in tab completion

Zero-length path prefixes can be specified in PATH as a leading or
trailing colon or two adjacent colons.  POSIX says that the use of
zero-length prefixes to refer to the current directory is a legacy
feature.  Nonetheless the shells in BusyBox respect this feature,
as does 'which'.

Tab-completion of executables using PATH should support this too.

function                                             old     new   delta
complete_cmd_dir_file                                934     931      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-3)               Total: -3 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2021-02-18 09:50:29 +00:00 committed by Denys Vlasenko
parent 858f8aafac
commit 760b627e2a

View File

@ -769,8 +769,6 @@ static unsigned path_parse(char ***p)
if (!tmp) if (!tmp)
break; break;
tmp++; tmp++;
if (*tmp == '\0')
break; /* :<empty> */
npth++; npth++;
} }
@ -782,8 +780,6 @@ static unsigned path_parse(char ***p)
if (!tmp) if (!tmp)
break; break;
*tmp++ = '\0'; /* ':' -> '\0' */ *tmp++ = '\0'; /* ':' -> '\0' */
if (*tmp == '\0')
break; /* :<empty> */
res[npth++] = tmp; res[npth++] = tmp;
} }
/* special case: "match subdirectories of the current directory" */ /* special case: "match subdirectories of the current directory" */
@ -854,6 +850,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
struct dirent *next; struct dirent *next;
struct stat st; struct stat st;
char *found; char *found;
const char *lpath;
if (paths[i] == NULL) { /* path_parse()'s last component? */ if (paths[i] == NULL) { /* path_parse()'s last component? */
/* in PATH completion, current dir's subdir names /* in PATH completion, current dir's subdir names
@ -863,7 +860,8 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
paths[i] = (char *)"."; paths[i] = (char *)".";
} }
dir = opendir(paths[i]); lpath = *paths[i] ? paths[i] : ".";
dir = opendir(lpath);
if (!dir) if (!dir)
continue; /* don't print an error */ continue; /* don't print an error */
@ -878,7 +876,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
if (strncmp(basecmd, name_found, baselen) != 0) if (strncmp(basecmd, name_found, baselen) != 0)
continue; /* no */ continue; /* no */
found = concat_path_file(paths[i], name_found); found = concat_path_file(lpath, name_found);
/* NB: stat() first so that we see is it a directory; /* NB: stat() first so that we see is it a directory;
* but if that fails, use lstat() so that * but if that fails, use lstat() so that
* we still match dangling links */ * we still match dangling links */