Skip to contentSkip to conversation

cmd_source: introduce run_script()

PATCH New Michael Walle 2 participants 5 messages Jan 10, 2012 – Jan 12, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd_source: introduce run_script()

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

---
 common/cmd_source.c |   71 ++++++++++++++++++++++++++++----------------------
 include/common.h    |    1 +
 2 files changed, 41 insertions(+), 31 deletions(-)
common/cmd_source.c +40 -31
index 16a627a..4b2740b 100644
@@ -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;
 }
include/common.h +1 -0
index 3df1def..dcfbed6 100644
@@ -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 */
Signature · 1 line
1.7.2.5
#1 lore raw

PATCH 1/1 cmd_source: introduce run_script()

common/cmd_source.c line 50

Quoted 8 lines · On Tuesday 10 January 2012 18:04:19 Michael Walle wrote:
common/cmd_source.c +5 -0
 
+/*
+ * 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120110/e15c4c09/attachment.pgp>

#2 lore raw

PATCH 1/1 cmd_source: introduce run_script()

Quoted 11 lines · Mike Frysinger

common/cmd_source.c line 50

Quoted 8 lines · On Tuesday 10 January 2012 18:04:19 Michael Walle wrote:
common/cmd_source.c +5 -0
 
+/*
+ * 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.

Quoted 5 lines

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

Signature · 1 line
michael
#3 lore raw

PATCH 1/1 cmd_source: introduce run_script()

In reply to Mike Frysinger

Am Mittwoch 11 Januar 2012, 00:40:04 schrieb Mike Frysinger:

Quoted 2 lines

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.

Signature · 1 line
michael
#4 lore raw

PATCH 1/1 cmd_source: introduce run_script()

Am Donnerstag 12 Januar 2012, 22:51:48 schrieb Michael Walle:

Quoted 1 line

Am Mittwoch 11 Januar 2012, 00:40:04 schrieb Mike

Frysinger:

Quoted 1 line
Quoted 1 line

although i wonder if it'd be possible to merge this

into existing

Quoted 1 line
Quoted 1 line

run_commands2() ... do we allow newlines now in

variables that get run ?

Quoted 2 lines

mh if the hush parser is enabled this seems to be true.

eg. source() and

Quoted 1 line

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.

Signature · 1 line
Michael