Skip to content

Commit

Permalink
lib: add automatic xpath-based completion
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Mar 3, 2024
1 parent 3fa5a77 commit 29dba44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -694,7 +709,10 @@ void cmd_variable_complete(struct cmd_token *token, const char *arg,
if (cvh->varname && (!token->varname
|| strcmp(cvh->varname, token->varname)))
continue;
cvh->completions(tmpcomps, token);
if (cvh->xpath)
__get_list_keys(tmpcomps, cvh->xpath);
if (cvh->completions)
cvh->completions(tmpcomps, token);
break;
}

Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions lib/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit 29dba44

Please sign in to comment.