client: add cmd line token finder

This allows to inspect the cmd line for the existence of a provided
token. This enables the completers to look back to what was entered
before them and make decisions based on that information. For
example, this can be used in completion of the property values
to identify the property for which the value is being completed.
This commit is contained in:
Tim Kourt 2018-07-24 16:17:24 -07:00 committed by Denis Kenzior
parent bb1a0adec7
commit c5cf3b083f
2 changed files with 33 additions and 0 deletions

View File

@ -130,6 +130,38 @@ static bool cmd_completion_cmd_has_arg(const char *cmd,
return status;
}
static bool find_next_token(int *i, const char *token, int token_len)
{
char *line = rl_line_buffer;
while (*i && line[*i] == ' ')
(*i)--;
while (*i && line[*i] != ' ')
(*i)--;
return !strncmp(line + (line[*i] == ' ' ? *i + 1 : *i),
token, token_len);
}
bool command_line_find_token(const char *token, uint8_t num_to_inspect)
{
int i = rl_point - 1;
int len = strlen(token);
if (!len)
return false;
while (i && num_to_inspect) {
if (find_next_token(&i, token, len))
return true;
num_to_inspect--;
}
return false;
}
static char **cmd_completion_match_entity_cmd(const char *cmd, const char *text,
const struct command *cmd_list)
{

View File

@ -49,6 +49,7 @@ struct command_family {
void (*set_default_entity)(const char *entity);
};
bool command_line_find_token(const char *token, uint8_t num_to_inspect);
char **command_completion(const char *text, int start, int end);
char *command_entity_arg_completion(const char *text, int state,
const struct command *command_list);