From b38ff09f6e2465a1085ac718beb994628c5b7430 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Tue, 29 Nov 2022 08:16:10 -0800 Subject: [PATCH] Com: iocsh: Tab completion of variable names for "var" --- modules/libcom/src/iocsh/iocsh.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/modules/libcom/src/iocsh/iocsh.cpp b/modules/libcom/src/iocsh/iocsh.cpp index 07d3fc6fd..43fcb9cab 100644 --- a/modules/libcom/src/iocsh/iocsh.cpp +++ b/modules/libcom/src/iocsh/iocsh.cpp @@ -481,6 +481,29 @@ char* iocsh_complete_command(const char* word, int notfirst) return NULL; } +char* iocsh_complete_variable(const char* word, int notfirst) +{ + // ick! ... readline is not re-entrant anyway + static const iocshVariable *next; + + if(!notfirst) { // aka. first call + next = iocshVariableHead; + } + + const size_t wlen = strlen(word); + + while(next) { + const iocshVariable *cur = next; + next = next->next; + + if(strncmp(word, cur->pVarDef->name, wlen)==0) { + return strdup(cur->pVarDef->name); + } + } + + return NULL; +} + char** iocsh_attempt_completion(const char* word, int start, int end) { const char *line = rl_line_buffer; @@ -563,6 +586,9 @@ char** iocsh_attempt_completion(const char* word, int start, int end) } else if(arg==1 && strcmp(def->pFuncDef->name, "help")==0) { return rl_completion_matches(word, iocsh_complete_command); + + } else if(arg==1 && strcmp(def->pFuncDef->name, "var")==0) { + return rl_completion_matches(word, iocsh_complete_variable); } }