[U-Boot] [PATCH] Handle environment variables like commands

Stefano Babic sbabic at denx.de
Wed Oct 15 12:09:44 CEST 2008


The command interpreter checks always if an environment variable
for that name exists and in this case the content of the variable
is executed. It becomes possible to redefine all U-Boot commands.
A new "builtin" command is added to be able to run builtin U-boot commands
even if they are redefined.

Signed-off-by : Stefano Babic <sbabic at denx.de>
---
 common/hush.c |    9 ++++++++-
 common/main.c |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/common/hush.c b/common/hush.c
index 093c428..68f8a6a 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -1677,9 +1677,16 @@ static int run_pipe_real(struct pipe *pi)
 					child->argv[i]);
 				return -1;
 			}
-			/* Look up command in command table */
 
+			/* Check if exists a variable with that name */
+			if ((p = getenv (child->argv[i])) != NULL )  {
+				int rcode;
+				rcode = (parse_string_outer(p,
+			                    FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0);
+				return rcode;
+			}
 
+			/* 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 */
diff --git a/common/main.c b/common/main.c
index c06ea07..b101b4f 100644
--- a/common/main.c
+++ b/common/main.c
@@ -1289,6 +1289,7 @@ int run_command (const char *cmd, int flag)
 	int argc, inquotes;
 	int repeatable = 1;
 	int rc = 0;
+	char *runenv;
 
 #ifdef DEBUG_PARSER
 	printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
@@ -1357,6 +1358,13 @@ int run_command (const char *cmd, int flag)
 			continue;
 		}
 
+		if ((runenv = getenv (argv[0])) != NULL )  {
+			if (run_command(runenv,flag) == -1) {
+				rc = -1;
+			}
+			continue;
+		}
+
 		/* Look up command in command table */
 		if ((cmdtp = find_cmd(argv[0])) == NULL) {
 			printf ("Unknown command '%s' - try 'help'\n", argv[0]);
@@ -1433,3 +1441,32 @@ int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 	return 0;
 }
 #endif
+
+int do_builtin (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+	int i;
+
+	if (argc < 2) {
+		printf ("Usage:\n%s\n", cmdtp->usage);
+		return 1;
+	}
+
+	/* Look up command in command table */
+	if ((cmdtp = find_cmd(argv[1])) == NULL) {
+		printf ("Unknown builtin command '%s' - try 'help'\n", argv[1]);
+		return 1;
+	}
+
+	/* Remove builtin from arg list */
+	argc--;
+
+	/* OK - call function to do the command */
+	if ((cmdtp->cmd) (cmdtp, flag, argc, &argv[1]) != 0) {
+		return 1;
+	}
+}
+
+U_BOOT_CMD(
+	builtin,	CFG_MAXARGS,	1,	do_builtin,
+	"builtin     - run builtin commands\n",
+);
-- 
1.5.4.3



More information about the U-Boot mailing list