diff mbox

[U-Boot] cmd_source: introduce run_script()

Message ID 1326236659-6257-1-git-send-email-michael@walle.cc
State Changes Requested
Headers show

Commit Message

Michael Walle Jan. 10, 2012, 11:04 p.m. UTC
Move actual script execution into a new function run_script(), which then
can be called from other modules.

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

Comments

Mike Frysinger Jan. 10, 2012, 11:40 p.m. UTC | #1
On Tuesday 10 January 2012 18:04:19 Michael Walle wrote:
> --- a/common/cmd_source.c
> +++ b/common/cmd_source.c
> 
> +/*
> + * 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)

const

also, we already have "run_command" and "run_command2", and they're both in 
common/main.c.  how about naming this "source_commands" and also putting it 
into common/main.c ?

although i wonder if it'd be possible to merge this into existing 
run_commands2() ... do we allow newlines now in variables that get run ?
-mike
Michael Walle Jan. 11, 2012, 5:45 p.m. UTC | #2
On Wed, January 11, 2012 00:40, Mike Frysinger wrote:
> On Tuesday 10 January 2012 18:04:19 Michael Walle wrote:
>> --- a/common/cmd_source.c
>> +++ b/common/cmd_source.c
>>
>> +/*
>> + * 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)
>
> const
const? const char *script? script is modified while parsing, see comment
above. I wanted to avoid copying.

> also, we already have "run_command" and "run_command2", and they're both
> in
> common/main.c.  how about naming this "source_commands" and also putting
> it
> into common/main.c ?
i'm fine with that
Michael Walle Jan. 12, 2012, 9:51 p.m. UTC | #3
Am Mittwoch 11 Januar 2012, 00:40:04 schrieb Mike Frysinger:
> although i wonder if it'd be possible to merge this into existing
> run_commands2() ... do we allow newlines now in variables that get run ?
mh if the hush parser is enabled this seems to be true. eg. source() and 
run_commands2() use the same parse_string_outer().

btw. shouldnt set source() FLAG_EXIT_FROM_LOOP, too? i'm trying to unify 
things and just noticed this.
Michael Walle Jan. 12, 2012, 10:54 p.m. UTC | #4
Am Donnerstag 12 Januar 2012, 22:51:48 schrieb Michael 
Walle:
> Am Mittwoch 11 Januar 2012, 00:40:04 schrieb Mike 
Frysinger:
> > although i wonder if it'd be possible to merge this 
into existing
> > run_commands2() ... do we allow newlines now in 
variables that get run ?
> 
> mh if the hush parser is enabled this seems to be true. 
eg. source() and
> run_commands2() use the same parse_string_outer().

Actually parse_string_outer() terminates on '\n'. So i 
think the current version of source() is broken.
diff mbox

Patch

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 */