We currently have the same code in hush.c and main.c. This brings the
code into one place.
As an added feature, if the command function returns CMD_RET_USAGE then
cmd_process() will print a usage message for the command before
returning the standard failure code of 1.
ARM code size increases about 32 bytes with this clean-up.
Signed-off-by: Simon Glass
---
Changes in v4:
- Add a 'repeatable' parameter to cmd_process()
- Make cmd_process() return only success (0) or failure (1)
- Rationalise return codes to 0, 1 and usage
Changes in v5:
- Use existing #ifdef __ASSEMBLY__ rather than create a new one
common/command.c | 41 -
common/hush.c | 52
common/main.c | 37 +
include/command.h | 29 -
4 files changed, 81 insertions(+), 78 deletions(-)
diff --git a/common/command.c b/common/command.c
index fe29075..aa0fb0a 100644
--- a/common/command.c
+++ b/common/command.c
@@ -499,7 +499,7 @@ void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
* @param argv Arguments
* @return 0 if command succeeded, else non-zero (CMD_RET_...)
*/
-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 result;
@@ -508,3 +508,42 @@ int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char *
const argv[])
debug("Command failed, result=%d", result);
return result;
}
+
+enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
+ int *repeatable)
+{
+ enum command_ret_t rc = CMD_RET_SUCCESS;
+ cmd_tbl_t *cmdtp;
+
+ /* Look up command in command table */
+ cmdtp = find_cmd(argv[0]);
+ if (cmdtp == NULL) {
+ printf("Unknown command '%s' - try 'help'\n", argv[0]);
+ return 1;
+ }
+
+ /* found - check max args */
+ if (argc > cmdtp->maxargs)
+ rc = CMD_RET_USAGE;
+
+#if defined(CONFIG_CMD_BOOTD)
+ /* avoid "bootd" recursion */
+ else if (cmdtp->cmd == do_bootd) {
+ if (flag & CMD_FLAG_BOOTD) {
+ puts("'bootd' recursion detected\n");
+ rc = CMD_RET_FAILURE;
+ } else {
+ flag |= CMD_FLAG_BOOTD;
+ }
+ }
+#endif
+
+ /* If OK so far, then do the command */
+ if (!rc) {
+ rc = cmd_call(cmdtp, flag, argc, argv);
+ *repeatable &= cmdtp->repeatable;
+ }
+ if (rc == CMD_RET_USAGE)
+ rc = cmd_usage(cmdtp);
+ return rc;
+}
diff --git a/common/hush.c b/common/hush.c
index 3aa9d50..672ab9e 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -1538,7 +1538,6 @@ static int run_pipe_real(struct pipe *pi)
int nextin;
int flag = do_repeat ? CMD_FLAG_REPEAT : 0;
struct child_prog *child;
- cmd_tbl_t *cmdtp;
char *p;
# if __GNUC__
/* Avoid longjmp clobbering */
@@ -1652,47 +1651,20 @@ static int run_pipe_real(struct pipe *pi)
return rcode;
}
#else
- /* check ";", because ,example , argv consist from
-* "help;flinfo" must not execute
-*/
- if (strchr(child->argv[i], ';')) {
- printf ("Unknown command '%s' - try 'help' or
use 'run' command\n",
- child->argv[i]);
- return -1;
- }
- /* Look up command in command table */
-
-
- if ((cmdtp = find_cmd(child->argv[i])) == NULL) {
- printf ("Unknown command '%s' - try 'help'\n",
child->argv[i]);
- return -1; /* give up after bad command */
- } else {
- int rcode;
-#if defined(CONFIG_CMD_BOOTD)
- /* avoid "bootd" recursion */
- if (cmdtp->cmd == do_bootd) {
- if (flag & CMD_FLAG_BOOTD) {
- printf ("'bootd' recursion
detected\n");
- return -1;
- }
- else
- flag |= CMD_FLAG_BOOTD;
- }
-#endif
- /* found - check max args */
- if ((child->argc - i) > cmdtp->maxargs)
- return cmd_usage(cmdtp);
-#endif
- /* OK - call function to do the command */
- rcode