[U-Boot] [PATCH v2] main: unify command parsing functions
Michael Walle
michael at walle.cc
Fri Jan 20 23:46:51 CET 2012
Introduce source_commands() which incorporates run_command2() and parts of
source().
All command script are now treated the same, that is newlines are accepted
within a command script and variable.
Signed-off-by: Michael Walle <michael at walle.cc>
Cc: Wolfgang Denk <wd at denx.de>
Cc: Mike Frysinger <vapier at gentoo.org>
---
changes v2:
- unconditionally include malloc.h, fixes compiler warning
common/cmd_pxe.c | 2 +-
common/cmd_source.c | 32 +-----------------------------
common/main.c | 54 +++++++++++++++++++++++++++++++++-----------------
include/common.h | 4 +--
4 files changed, 38 insertions(+), 54 deletions(-)
diff --git a/common/cmd_pxe.c b/common/cmd_pxe.c
index 7c0cb66..347fde1 100644
--- a/common/cmd_pxe.c
+++ b/common/cmd_pxe.c
@@ -537,7 +537,7 @@ static int label_localboot(struct pxe_label *label)
printf("running: %s\n", dupcmd);
- ret = run_command2(dupcmd, 0);
+ ret = source_commands(dupcmd, 0);
free(dupcmd);
diff --git a/common/cmd_source.c b/common/cmd_source.c
index 16a627a..33242df 100644
--- a/common/cmd_source.c
+++ b/common/cmd_source.c
@@ -160,38 +160,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 = source_commands(cmd, 0);
- /*
- * 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/common/main.c b/common/main.c
index e96c95a..7327821 100644
--- a/common/main.c
+++ b/common/main.c
@@ -31,9 +31,7 @@
#include <watchdog.h>
#include <command.h>
#include <version.h>
-#ifdef CONFIG_MODEM_SUPPORT
#include <malloc.h> /* for free() prototype */
-#endif
#ifdef CONFIG_SYS_HUSH_PARSER
#include <hush.h>
@@ -266,26 +264,44 @@ int abortboot(int bootdelay)
# endif /* CONFIG_AUTOBOOT_KEYED */
#endif /* CONFIG_BOOTDELAY >= 0 */
+static inline int _run_command(const char *cmd, int flag)
+{
+#ifndef CONFIG_SYS_HUSH_PARSER
+ return (run_command(cmd, flag) == -1);
+#else
+ flag = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
+ return parse_string_outer(cmd, flag);
+#endif
+}
+
/*
+ * Run a series of commands separated by '\n'.
+ *
* Return 0 on success, or != 0 on error.
*/
-#ifndef CONFIG_CMD_PXE
-static inline
-#endif
-int run_command2(const char *cmd, int flag)
+int source_commands(const char *commands, int flag)
{
-#ifndef CONFIG_SYS_HUSH_PARSER
+ int rcode = 0;
+ char *_commands = strdup(commands);
+ char *stringp = _commands;
+ char *line;
+
/*
- * run_command can return 0 or 1 for success, so clean up its result.
+ * break into individual lines and execute each line;
+ * terminate on error.
*/
- if (run_command(cmd, flag) == -1)
- return 1;
+ while ((line = strsep(&stringp, "\n"))) {
+ /* skip empty lines */
+ if (*line == '\0')
+ continue;
+ if (_run_command(line, flag)) {
+ rcode = 1;
+ break;
+ }
+ }
- return 0;
-#else
- return parse_string_outer(cmd,
- FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
-#endif
+ free(_commands);
+ return rcode;
}
/****************************************************************************/
@@ -354,7 +370,7 @@ void main_loop (void)
int prev = disable_ctrlc(1); /* disable Control C checking */
# endif
- run_command2(p, 0);
+ source_commands(p, 0);
# ifdef CONFIG_AUTOBOOT_KEYED
disable_ctrlc(prev); /* restore Control C checking */
@@ -399,7 +415,7 @@ void main_loop (void)
int prev = disable_ctrlc(1); /* disable Control C checking */
# endif
- run_command2(s, 0);
+ source_commands(s, 0);
# ifdef CONFIG_AUTOBOOT_KEYED
disable_ctrlc(prev); /* restore Control C checking */
@@ -410,7 +426,7 @@ void main_loop (void)
if (menukey == CONFIG_MENUKEY) {
s = getenv("menucmd");
if (s)
- run_command2(s, 0);
+ source_commands(s, 0);
}
#endif /* CONFIG_MENUKEY */
#endif /* CONFIG_BOOTDELAY */
@@ -1408,7 +1424,7 @@ int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
return 1;
}
- if (run_command2(arg, flag) != 0)
+ if (source_commands(arg, flag) != 0)
return 1;
}
return 0;
diff --git a/include/common.h b/include/common.h
index 3df1def..c2e7813 100644
--- a/include/common.h
+++ b/include/common.h
@@ -261,9 +261,7 @@ int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen);
/* common/main.c */
void main_loop (void);
int run_command (const char *cmd, int flag);
-#ifdef CONFIG_CMD_PXE
-int run_command2(const char *cmd, int flag);
-#endif
+int source_commands(const char *cmd, int flag);
int readline (const char *const prompt);
int readline_into_buffer (const char *const prompt, char * buffer);
int parse_line (char *, char *[]);
--
1.7.2.5
More information about the U-Boot
mailing list