diff options
author | Boris Brezillon <boris.brezillon@bootlin.com> | 2018-12-05 09:26:50 +0100 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2019-01-15 15:38:28 -0500 |
commit | 03dcf17dba3dbd6f1cfe9ecaa0665ea8c11e0ef2 (patch) | |
tree | 73d8d68a2d4497ec6000f44d4ae8d4db80b40be2 /common/command.c | |
parent | 31a2cf1ca4968dcaf78aef222b6683fea4f2c72d (diff) | |
download | u-boot-03dcf17dba3dbd6f1cfe9ecaa0665ea8c11e0ef2.tar.gz |
common: command: Add support for $ auto-completion
Add the dollar_complete() function to auto-complete arguments starting
with a '$' and use it in the cmd_auto_complete() path such that all
args starting with a $ can be auto-completed based on the available env
vars.
Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
[trini: Fix some linking problems]
Signed-off-by: Tom Rini <trini@konsulko.com>
Diffstat (limited to 'common/command.c')
-rw-r--r-- | common/command.c | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/common/command.c b/common/command.c index 19f0534a76..e14d1fa1d6 100644 --- a/common/command.c +++ b/common/command.c @@ -142,23 +142,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; } +static 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, @@ -357,9 +372,14 @@ int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) /* separate into argv */ argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); - /* do the completion and return the possible completions */ - i = complete_cmdv(argc, argv, last_char, - sizeof(cmdv) / sizeof(cmdv[0]), cmdv); + /* first try a $ completion */ + i = dollar_complete(argc, argv, last_char, + sizeof(cmdv) / sizeof(cmdv[0]), cmdv); + if (!i) { + /* do the completion and return the possible completions */ + i = complete_cmdv(argc, argv, last_char, + sizeof(cmdv) / sizeof(cmdv[0]), cmdv); + } /* no match; bell and out */ if (i == 0) { |