Add an helper to auto-complete arguments starting with a '$' with what's available in the environment.
Signed-off-by: Boris Brezillon <boris.brezil...@bootlin.com> --- common/command.c | 22 ++++++++++++++++++--- env/common.c | 50 ++++++++++++++++++++++++++++++++++++++++++++--- include/command.h | 2 ++ include/common.h | 3 ++- 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/common/command.c b/common/command.c index 19f0534a76ea..754ab9bbc396 100644 --- a/common/command.c +++ b/common/command.c @@ -143,22 +143,38 @@ int cmd_usage(const cmd_tbl_t *cmdtp) #ifdef CONFIG_AUTO_COMPLETE +static char env_complete_buf[512]; + int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) { - static char tmp_buf[512]; int space; space = last_char == '\0' || isblank(last_char); if (space && argc == 1) - return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf); + return env_complete("", maxv, cmdv, sizeof(env_complete_buf), + env_complete_buf, false); if (!space && argc == 2) - return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf); + return env_complete(argv[1], maxv, cmdv, + sizeof(env_complete_buf), + env_complete_buf, false); return 0; } +int dollar_complete(int argc, char * const argv[], char last_char, int maxv, + char *cmdv[]) +{ + /* Make sure the last argument starts with a $. */ + if (argc < 1 || argv[argc - 1][0] != '$' || + last_char == '\0' || isblank(last_char)) + return 0; + + return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf), + env_complete_buf, true); +} + /*************************************************************************************/ int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc, diff --git a/env/common.c b/env/common.c index 3317cef35522..aa9a097bced0 100644 --- a/env/common.c +++ b/env/common.c @@ -241,31 +241,75 @@ void env_relocate(void) } #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) -int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) +int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf, + bool dollar_comp) { ENTRY *match; int found, idx; + if (dollar_comp) { + /* + * When doing $ completion, the first character should + * obviously be a '$'. + */ + if (var[0] != '$') + return 0; + + var++; + + /* + * The second one, if present, should be a '{', as some + * configuration of the u-boot shell expand ${var} but not + * $var. + */ + if (var[0] == '{') + var++; + else if (var[0] != '\0') + return 0; + } + idx = 0; found = 0; cmdv[0] = NULL; + while ((idx = hmatch_r(var, idx, &match, &env_htab))) { int vallen = strlen(match->key) + 1; - if (found >= maxv - 2 || bufsz < vallen) + if (found >= maxv - 2 || + bufsz < vallen + (dollar_comp ? 3 : 0)) break; cmdv[found++] = buf; + + /* Add the '${' prefix to each var when doing $ completion. */ + if (dollar_comp) { + strcpy(buf, "${"); + buf += 2; + bufsz -= 3; + } + memcpy(buf, match->key, vallen); buf += vallen; bufsz -= vallen; + + if (dollar_comp) { + /* + * This one is a bit odd: vallen already contains the + * '\0' character but we need to add the '}' suffix, + * hence the buf - 1 here. strcpy() will add the '\0' + * character just after '}'. buf is then incremented + * to account for the extra '}' we just added. + */ + strcpy(buf - 1, "}"); + buf++; + } } qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); if (idx) - cmdv[found++] = "..."; + cmdv[found++] = dollar_comp ? "${...}" : "..."; cmdv[found] = NULL; return found; diff --git a/include/command.h b/include/command.h index 461b17447c0d..c512ec6854d0 100644 --- a/include/command.h +++ b/include/command.h @@ -84,6 +84,8 @@ static inline bool cmd_is_repeatable(cmd_tbl_t *cmdtp) #ifdef CONFIG_AUTO_COMPLETE extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]); +int dollar_complete(int argc, char * const argv[], char last_char, int maxv, + char *cmdv[]); extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp); #endif diff --git a/include/common.h b/include/common.h index 8b561370326f..2c776adc5fe0 100644 --- a/include/common.h +++ b/include/common.h @@ -237,7 +237,8 @@ static inline int env_set_addr(const char *varname, const void *addr) } #ifdef CONFIG_AUTO_COMPLETE -int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf); +int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf, + bool dollar_comp); #endif int get_env_id (void); -- 2.17.1 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de https://lists.denx.de/listinfo/u-boot