Index: bin/sh/histedit.c =================================================================== --- bin/sh/histedit.c +++ bin/sh/histedit.c @@ -39,6 +39,8 @@ __FBSDID("$FreeBSD$"); #include +#include +#include #include #include #include @@ -68,10 +70,14 @@ EditLine *el; /* editline cookie */ int displayhist; static FILE *el_in, *el_out; +static int curpos; static char *fc_replace(const char *, char *, char *); static int not_fcnumber(const char *); static int str_to_event(const char *, int); +static int comparator(const void *, const void *); +static char **sh_matches(const char *, int, int); +static unsigned char sh_complete(EditLine *, int); /* * Set history and editing status. Called whenever the status may @@ -122,7 +128,7 @@ el_set(el, EL_PROMPT, getprompt); el_set(el, EL_ADDFN, "sh-complete", "Filename completion", - _el_fn_complete); + sh_complete); } else { bad: out2fmt_flush("sh: can't initialize editing\n"); @@ -519,3 +525,106 @@ return (0); } #endif + +/* + * Comparator function for qsort(). The use of curpos here is to skip + * characters that we already know to compare equal (common prefix). + */ +static int +comparator(const void *a, const void *b) +{ + return (strcmp(*(char *const *)a + curpos, + *(char *const *)b + curpos)); +} + +/* + * This function is passed to libedit's fn_complete(). The library will + * use it instead of its standard function to find matches, which + * searches for files in current directory. If we're at the start of the + * line, we want to look for available commands from all paths in $PATH. + */ +static char +**sh_matches(const char *text, int start, int end) +{ + char *free_path = NULL, *dirname, *path; + char **matches = NULL; + size_t i = 0, size = 16; + + if (start > 0) + return (NULL); + curpos = end - start; + if ((free_path = path = strdup(pathval())) == NULL) + goto out; + if ((matches = malloc(size * sizeof(matches[0]))) == NULL) + goto out; + while ((dirname = strsep(&path, ":")) != NULL) { + struct dirent *entry; + DIR *dir; + int dfd; + + if ((dir = opendir(dirname)) == NULL) + continue; + if ((dfd = dirfd(dir)) == -1) + continue; + while ((entry = readdir(dir)) != NULL) { + struct stat statb; + + if (strncmp(entry->d_name, text, curpos) != 0) + continue; + if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) { + if (fstatat(dfd, entry->d_name, &statb, 0) == -1) + continue; + if (!S_ISREG(statb.st_mode)) + continue; + } else if (entry->d_type != DT_REG) + continue; + if (++i >= size - 1) { + char **ramatches; + + size *= 2; + ramatches = reallocarray(matches, size, sizeof(char *)); + if (ramatches == NULL) { + closedir(dir); + goto out; + } + matches = ramatches; + } + matches[i] = strdup(entry->d_name); + } + closedir(dir); + } +out: + free(free_path); + if (i == 0) { + free(matches); + return (NULL); + } + if (i == 1) { + matches[0] = strdup(matches[1]); + matches[i + 1] = NULL; + } else { + size_t j, k; + + qsort(matches + 1, i, sizeof(matches[0]), comparator); + for (j = 1, k = 2; k <= i; k++) + if (strcmp(matches[j] + curpos, matches[k] + curpos) == 0) + free(matches[k]); + else + matches[++j] = matches[k]; + matches[0] = strdup(text); + matches[j + 1] = NULL; + } + return (matches); +} + +/* + * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to + * bind a key (tab by default) to execute the function. + */ +unsigned char +sh_complete(EditLine *sel, int ch __unused) +{ + return (unsigned char)fn_complete(sel, NULL, sh_matches, + L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100, + NULL, &((int) {0}), NULL, NULL); +} Index: bin/sh/myhistedit.h =================================================================== --- bin/sh/myhistedit.h +++ bin/sh/myhistedit.h @@ -38,6 +38,8 @@ extern EditLine *el; extern int displayhist; +#include + void histedit(void); void sethistsize(const char *); void setterm(const char *); Index: lib/libedit/Makefile =================================================================== --- lib/libedit/Makefile +++ lib/libedit/Makefile @@ -65,7 +65,7 @@ editline.3 tok_wreset.3 \ editline.3 tok_wstr.3 -INCS= histedit.h +INCS= histedit.h filecomplete.h SRCS+= common.h emacs.h fcns.h func.h help.h vi.h CLEANFILES+= common.h emacs.h fcns.h func.h help.h vi.h