Complete option names as well.

This commit is contained in:
Nicholas Marriott
2009-01-06 17:04:56 +00:00
parent 9cddd796ff
commit 4af8db90e8
5 changed files with 118 additions and 70 deletions

53
cmd.c
View File

@ -1,4 +1,4 @@
/* $Id: cmd.c,v 1.67 2008-12-15 21:21:56 nicm Exp $ */
/* $Id: cmd.c,v 1.68 2009-01-06 17:04:56 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -74,57 +74,6 @@ const struct cmd_entry *cmd_table[] = {
NULL
};
char *
cmd_complete(const char *s)
{
const struct cmd_entry **entryp;
ARRAY_DECL(, const char *) list;
char *prefix, *s2;
u_int i;
size_t j;
if (*s == '\0')
return (xstrdup(s));
/* First, build a list of all the possible matches. */
ARRAY_INIT(&list);
for (entryp = cmd_table; *entryp != NULL; entryp++) {
if (strncmp((*entryp)->name, s, strlen(s)) != 0)
continue;
ARRAY_ADD(&list, (*entryp)->name);
}
/* If none, bail now with the original string. */
if (ARRAY_LENGTH(&list) == 0) {
ARRAY_FREE(&list);
return (xstrdup(s));
}
/* If an exact match, return it, with a trailing space. */
if (ARRAY_LENGTH(&list) == 1) {
xasprintf(&s2, "%s ", ARRAY_FIRST(&list));
ARRAY_FREE(&list);
return (s2);
}
/* Now loop through the list and find the longest common prefix. */
prefix = xstrdup(ARRAY_FIRST(&list));
for (i = 1; i < ARRAY_LENGTH(&list); i++) {
s = ARRAY_ITEM(&list, i);
j = strlen(s);
if (j > strlen(prefix))
j = strlen(prefix);
for (; j > 0; j--) {
if (prefix[j - 1] != s[j - 1])
prefix[j - 1] = '\0';
}
}
ARRAY_FREE(&list);
return (prefix);
}
struct cmd *
cmd_parse(int argc, char **argv, char **cause)
{