diff options
Diffstat (limited to 'common/command.c')
-rw-r--r-- | common/command.c | 100 |
1 files changed, 84 insertions, 16 deletions
diff --git a/common/command.c b/common/command.c index 2433a89e0a..e14d1fa1d6 100644 --- a/common/command.c +++ b/common/command.c @@ -142,30 +142,45 @@ 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); +} + /*************************************************************************************/ -static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) +int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc, + char * const argv[], char last_char, + int maxv, char *cmdv[]) { #ifdef CONFIG_CMDLINE - cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd); - const int count = ll_entry_count(cmd_tbl_t, cmd); const cmd_tbl_t *cmdend = cmdtp + count; const char *p; int len, clen; @@ -193,7 +208,7 @@ static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv /* more than one arg or one but the start of the next */ if (argc > 1 || last_char == '\0' || isblank(last_char)) { - cmdtp = find_cmd(argv[0]); + cmdtp = find_cmd_tbl(argv[0], cmdtp, count); if (cmdtp == NULL || cmdtp->complete == NULL) { cmdv[0] = NULL; return 0; @@ -238,6 +253,18 @@ static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv #endif } +static int complete_cmdv(int argc, char * const argv[], char last_char, + int maxv, char *cmdv[]) +{ +#ifdef CONFIG_CMDLINE + return complete_subcmdv(ll_entry_start(cmd_tbl_t, cmd), + ll_entry_count(cmd_tbl_t, cmd), argc, argv, + last_char, maxv, cmdv); +#else + return 0; +#endif +} + static int make_argv(char *s, int argvsz, char *argv[]) { int argc = 0; @@ -345,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) { @@ -362,13 +394,21 @@ int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) sep = NULL; seplen = 0; if (i == 1) { /* one match; perfect */ - k = strlen(argv[argc - 1]); + if (last_char != '\0' && !isblank(last_char)) + k = strlen(argv[argc - 1]); + else + k = 0; + s = cmdv[0] + k; len = strlen(s); sep = " "; seplen = 1; } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ - k = strlen(argv[argc - 1]); + if (last_char != '\0' && !isblank(last_char)) + k = strlen(argv[argc - 1]); + else + k = 0; + j -= k; if (j > 0) { s = cmdv[0] + k; @@ -481,6 +521,30 @@ void fixup_cmdtable(cmd_tbl_t *cmdtp, int size) } #endif +int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[], int *repeatable) +{ + *repeatable = 1; + + return cmdtp->cmd(cmdtp, flag, argc, argv); +} + +int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[], int *repeatable) +{ + *repeatable = 0; + + return cmdtp->cmd(cmdtp, flag, argc, argv); +} + +int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + int repeatable; + + return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable); +} + /** * Call a command function. This should be the only route in U-Boot to call * a command, so that we can track whether we are waiting for input or @@ -490,13 +554,15 @@ void fixup_cmdtable(cmd_tbl_t *cmdtp, int size) * @param flag Some flags normally 0 (see CMD_FLAG_.. above) * @param argc Number of arguments (arg 0 must be the command text) * @param argv Arguments + * @param repeatable Can the command be repeated * @return 0 if command succeeded, else non-zero (CMD_RET_...) */ -static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], + int *repeatable) { int result; - result = (cmdtp->cmd)(cmdtp, flag, argc, argv); + result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable); if (result) debug("Command failed, result=%d\n", result); return result; @@ -533,12 +599,14 @@ enum command_ret_t cmd_process(int flag, int argc, char * const argv[], /* If OK so far, then do the command */ if (!rc) { + int newrep; + if (ticks) *ticks = get_timer(0); - rc = cmd_call(cmdtp, flag, argc, argv); + rc = cmd_call(cmdtp, flag, argc, argv, &newrep); if (ticks) *ticks = get_timer(*ticks); - *repeatable &= cmdtp->repeatable; + *repeatable &= newrep; } if (rc == CMD_RET_USAGE) rc = cmd_usage(cmdtp); |