lib: explicitly support the case of empty input for completions

When the user tab- or ?-completes when the character prior to
the position of the cursor is a space, completion logic is
passed null. Explicitly handle this case instead of using
partly_match, which has special logic associated with it to
allow abbreviating certain tokens.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
This commit is contained in:
Quentin Young 2016-10-02 04:47:31 +00:00
parent 51d41d759b
commit a78596c42d
2 changed files with 11 additions and 20 deletions

View File

@ -303,6 +303,7 @@ command_complete (struct graph *graph,
struct cmd_token *token = gn->data; struct cmd_token *token = gn->data;
switch (match_token (token, input_token)) switch (match_token (token, input_token))
{ {
case trivial_match:
case partly_match: case partly_match:
if (idx == vector_active (vline) - 1) if (idx == vector_active (vline) - 1)
{ {
@ -526,6 +527,10 @@ del_arglist (struct list *list)
static enum match_type static enum match_type
match_token (struct cmd_token *token, char *input_token) match_token (struct cmd_token *token, char *input_token)
{ {
// nothing trivially matches everything
if (!input_token)
return trivial_match;
switch (token->type) { switch (token->type) {
case WORD_TKN: case WORD_TKN:
return match_word (token, input_token); return match_word (token, input_token);
@ -557,9 +562,6 @@ match_ipv4 (const char *str)
int dots = 0, nums = 0; int dots = 0, nums = 0;
char buf[4]; char buf[4];
if (str == NULL)
return partly_match;
for (;;) for (;;)
{ {
memset (buf, 0, sizeof (buf)); memset (buf, 0, sizeof (buf));
@ -614,9 +616,6 @@ match_ipv4_prefix (const char *str)
int dots = 0; int dots = 0;
char buf[4]; char buf[4];
if (str == NULL)
return partly_match;
for (;;) for (;;)
{ {
memset (buf, 0, sizeof (buf)); memset (buf, 0, sizeof (buf));
@ -696,9 +695,6 @@ match_ipv6 (const char *str)
struct sockaddr_in6 sin6_dummy; struct sockaddr_in6 sin6_dummy;
int ret; int ret;
if (str == NULL)
return partly_match;
if (strspn (str, IPV6_ADDR_STR) != strlen (str)) if (strspn (str, IPV6_ADDR_STR) != strlen (str))
return no_match; return no_match;
@ -718,9 +714,6 @@ match_ipv6_prefix (const char *str)
char *tofree, *dupe, *prefix, *mask, *endptr; char *tofree, *dupe, *prefix, *mask, *endptr;
int nmask = -1; int nmask = -1;
if (str == NULL)
return partly_match;
if (strspn (str, IPV6_PREFIX_STR) != strlen (str)) if (strspn (str, IPV6_PREFIX_STR) != strlen (str))
return no_match; return no_match;
@ -763,9 +756,6 @@ match_range (struct cmd_token *token, const char *str)
char *endptr = NULL; char *endptr = NULL;
long long val; long long val;
if (str == NULL)
return 1;
val = strtoll (str, &endptr, 10); val = strtoll (str, &endptr, 10);
if (*endptr != '\0') if (*endptr != '\0')
return 0; return 0;
@ -781,8 +771,8 @@ match_word (struct cmd_token *token, const char *word)
{ {
assert (token->type == WORD_TKN); assert (token->type == WORD_TKN);
// if the passed token is null or 0 length, partly match // if the passed token is 0 length, partly match
if (!word || !strlen(word)) if (!strlen(word))
return partly_match; return partly_match;
// if the passed token is strictly a prefix of the full word, partly match // if the passed token is strictly a prefix of the full word, partly match

View File

@ -50,9 +50,10 @@ enum matcher_rv
/* completion match types */ /* completion match types */
enum match_type enum match_type
{ {
no_match, trivial_match, // the input is null
partly_match, no_match, // the input does not match
exact_match partly_match, // the input matches but is incomplete
exact_match // the input matches and is complete
}; };
/* Defines which matcher_rv values constitute an error. Should be used with /* Defines which matcher_rv values constitute an error. Should be used with