common: command: Add support for $ auto-completion
PATCH v2 1/1 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.
---
Changes in v2:
- Call dollar_complete() from cmd_auto_complete() to provide $ auto-completion to everyone (suggested by Wolfgang)
--- common/command.c | 33 ++++++++++++++++++++++++++------ env/common.c | 50 +++++++++++++++++++++++++++++++++++++++++++++--- include/common.h | 3 ++- 3 files changed, 76 insertions(+), 10 deletions(-)
common/command.c +27 -6
@@ -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 ; } +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 +373,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 ) {
env/common.c +47 -3
78 lines not on this page · raw message
include/common.h +2 -1
@@ -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 );
Signature · 1 line
2.17.1
PATCH v2 1/1 common: command: Add support for $ auto-completion
Quoted 6 lines · Boris Brezillon
Add the
dollar_complete()function to auto-complete arguments starting with a '$' and use it in thecmd_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 <[email protected]>
Applied to u-boot/master, thanks!
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: not available URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190115/0af5000b/attachment.sig>