From 29dba445b4e6d762e80e1bd015a70cb362a7906e Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Fri, 1 Mar 2024 08:48:47 -0500 Subject: [PATCH] lib: add automatic xpath-based completion Signed-off-by: Christian Hopps --- lib/command.c | 22 ++++++++++++++++++++-- lib/command.h | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/command.c b/lib/command.c index fa260721dcb2..0288ab75995b 100644 --- a/lib/command.c +++ b/lib/command.c @@ -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,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; } @@ -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); } diff --git a/lib/command.h b/lib/command.h index 04c66adb2688..ef1815c0bd51 100644 --- a/lib/command.h +++ b/lib/command.h @@ -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); };