Skip to content

Commit

Permalink
sh(1): autocomplete commands
Browse files Browse the repository at this point in the history
Without this patch, sh can autocomplete file names but not commands from
$PATH. Use libedit's facility to execute custom function for autocomplete,
but yield to the library's standard autocomplete function when cursor is
not at position 0.

Reviewed by:	bapt
Differential Revision:	https://reviews.freebsd.org/D29361
  • Loading branch information
pstef committed Mar 29, 2021
1 parent 1777720 commit b315a72
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
128 changes: 127 additions & 1 deletion bin/sh/histedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
Expand Down Expand Up @@ -72,6 +74,9 @@ static FILE *el_in, *el_out;
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 *, 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
Expand Down Expand Up @@ -122,7 +127,7 @@ histedit(void)
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");
Expand Down Expand Up @@ -519,3 +524,124 @@ bindcmd(int argc __unused, char **argv __unused)
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, void *thunk)
{
size_t curpos = (intptr_t)thunk;
return (strcmp(*(char *const *)a + curpos,
*(char *const *)b + curpos));
}

/*
* This function is passed to libedit's fn_complete2(). The library will
* use it instead of its standard function that finds matching 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, *path;
const char *dirname;
char **matches = NULL;
size_t i = 0, size = 16, j, k;
size_t curpos = end - start;

if (start > 0 || memchr("/.~", text[0], 3) != NULL)
return (NULL);
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;

dir = opendir(dirname[0] == '\0' ? "." : dirname);
if (dir == NULL)
continue;
if ((dfd = dirfd(dir)) == -1) {
closedir(dir);
continue;
}
while ((entry = readdir(dir)) != NULL) {
struct stat statb;
char **rmatches;

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;
matches[++i] = strdup(entry->d_name);
if (i < size - 1)
continue;
size *= 2;
rmatches = reallocarray(matches, size, sizeof(matches[0]));
if (rmatches == NULL) {
closedir(dir);
goto out;
}
matches = rmatches;
}
closedir(dir);
}
out:
free(free_path);
/*
* matches[0] is special: it's not a real matching file name but a common
* prefix for all matching names. It can't be null, unlike any other
* element of the array. When strings matches[0] and matches[1] compare
* equal and matches[2] is null that means to libedit that there is only
* a single match. It will then replace user input with possibly escaped
* string in matches[0] which is the reason to copy the full name of the
* only match.
*/
if (i == 0) {
free(matches);
return (NULL);
} else if (i == 1) {
matches[0] = strdup(matches[1]);
matches[2] = NULL;
if (matches[0] != NULL)
return (matches);
} else
matches[0] = strdup(text);
if (matches[0] == NULL) {
for (j = 1; j <= i; j++)
free(matches[j]);
free(matches);
return (NULL);
}
qsort_s(matches + 1, i, sizeof(matches[0]), comparator,
(void *)(intptr_t)curpos);
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[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_complete2(sel, NULL, sh_matches,
L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100,
NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH);
}
2 changes: 2 additions & 0 deletions bin/sh/myhistedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extern History *hist;
extern EditLine *el;
extern int displayhist;

#include <filecomplete.h>

void histedit(void);
void sethistsize(const char *);
void setterm(const char *);
Expand Down
2 changes: 1 addition & 1 deletion lib/libedit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ MLINKS= \
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
Expand Down

0 comments on commit b315a72

Please sign in to comment.