[U-Boot] [PATCH] cmd_source: introduce run_script()

Michael Walle michael at walle.cc
Wed Jan 11 00:04:19 CET 2012


Move actual script execution into a new function run_script(), which then
can be called from other modules.

Signed-off-by: Michael Walle <michael at walle.cc>
Cc: Wolfgang Denk <wd at denx.de>
---
 common/cmd_source.c |   71 ++++++++++++++++++++++++++++----------------------
 include/common.h    |    1 +
 2 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/common/cmd_source.c b/common/cmd_source.c
index 16a627a..4b2740b 100644
--- a/common/cmd_source.c
+++ b/common/cmd_source.c
@@ -43,6 +43,45 @@
 #include <hush.h>
 #endif
 
+/*
+ * Run a series of commands, separated by '\n'.
+ * Beware, the contents of script may be modified while it is parsed.
+ */
+int run_script(char *script)
+{
+#ifndef CONFIG_SYS_HUSH_PARSER /*?? */
+	int rcode;
+	char *line = script;
+	char *next = script;
+
+	/*
+	 * break into individual lines and execute each line;
+	 * terminate on error.
+	 */
+	while (*next) {
+		if (*next == '\n') {
+			*next = '\0';
+			/* run only non-empty commands */
+			if (*line) {
+				debug("** exec: \"%s\"\n", line);
+				if (run_command(line, 0) < 0) {
+					rcode = 1;
+					break;
+				}
+			}
+			line = next + 1;
+		}
+		++next;
+	}
+
+	if (rcode == 0 && *line)
+		rcode = (run_command(line, 0) >= 0);
+	return rcode;
+#else
+	return parse_string_outer(script, FLAG_PARSE_SEMICOLON);
+#endif
+}
+
 int
 source (ulong addr, const char *fit_uname)
 {
@@ -160,38 +199,8 @@ source (ulong addr, const char *fit_uname)
 	memmove (cmd, (char *)data, len);
 	*(cmd + len) = 0;
 
-#ifdef CONFIG_SYS_HUSH_PARSER /*?? */
-	rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON);
-#else
-	{
-		char *line = cmd;
-		char *next = cmd;
+	rcode = run_script(cmd);
 
-		/*
-		 * break into individual lines,
-		 * and execute each line;
-		 * terminate on error.
-		 */
-		while (*next) {
-			if (*next == '\n') {
-				*next = '\0';
-				/* run only non-empty commands */
-				if (*line) {
-					debug ("** exec: \"%s\"\n",
-						line);
-					if (run_command (line, 0) < 0) {
-						rcode = 1;
-						break;
-					}
-				}
-				line = next + 1;
-			}
-			++next;
-		}
-		if (rcode == 0 && *line)
-			rcode = (run_command(line, 0) >= 0);
-	}
-#endif
 	free (cmd);
 	return rcode;
 }
diff --git a/include/common.h b/include/common.h
index 3df1def..dcfbed6 100644
--- a/include/common.h
+++ b/include/common.h
@@ -296,6 +296,7 @@ void board_pre_console_putc(int ch);
 void flash_perror (int);
 
 /* common/cmd_source.c */
+int	run_script(char *script);
 int	source (ulong addr, const char *fit_uname);
 
 extern ulong load_addr;		/* Default Load Address */
-- 
1.7.2.5



More information about the U-Boot mailing list