lib: add automatic xpath-based completion

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2024-03-01 08:48:47 -05:00
parent 3fa5a77def
commit 29dba445b4
2 changed files with 21 additions and 2 deletions

View file

@ -678,6 +678,21 @@ vector cmd_describe_command(vector vline, struct vty *vty, int *status)
static struct list *varhandlers = NULL;
static int __add_key_comp(const struct lyd_node *dnode, void *arg)
{
const char *key_value = yang_dnode_get_string(dnode, NULL);
vector_set((vector)arg, XSTRDUP(MTYPE_COMPLETION, key_value));
return YANG_ITER_CONTINUE;
}
static void __get_list_keys(vector comps, const char *xpath)
{
yang_dnode_iterate(__add_key_comp, comps,
vty_shared_candidate_config->dnode, "%s", xpath);
}
void cmd_variable_complete(struct cmd_token *token, const char *arg,
vector comps)
{
@ -694,6 +709,9 @@ void cmd_variable_complete(struct cmd_token *token, const char *arg,
if (cvh->varname && (!token->varname
|| strcmp(cvh->varname, token->varname)))
continue;
if (cvh->xpath)
__get_list_keys(tmpcomps, cvh->xpath);
if (cvh->completions)
cvh->completions(tmpcomps, token);
break;
}
@ -753,7 +771,7 @@ void cmd_variable_handler_register(const struct cmd_variable_handler *cvh)
if (!varhandlers)
return;
for (; cvh->completions; cvh++)
for (; cvh->completions || cvh->xpath; cvh++)
listnode_add(varhandlers, (void *)cvh);
}

View file

@ -636,6 +636,7 @@ extern void cmd_banner_motd_line(const char *line);
struct cmd_variable_handler {
const char *tokenname, *varname;
const char *xpath; /* fill comps from set of values at xpath */
void (*completions)(vector out, struct cmd_token *token);
};