Add run_command_list() to run a list of commands
PATCH 1/2 Add run_command_list() to run a list of commands
This new function runs a list of commands separated by semicolon. We move this out of cmd_source so that it can be used by other code. The PXE also uses the new function.
--- common/cmd_pxe.c | 20 ++------------ common/cmd_source.c | 49 +---------------------------------- common/main.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/common.h | 1 + 4 files changed, 77 insertions(+), 65 deletions(-)
common/cmd_pxe.c +3 -17
@@ -515,33 +515,19 @@ static void label_print(void *data)*/ static int label_localboot (struct pxe_label *label) { -char *localcmd, *dupcmd; -int ret; +char *localcmd; localcmd =from_env ("localcmd" );if (!localcmd)return -ENOENT ; -/* -* dup the command to avoid any issues with the version of it existing -* in the environment changing during the execution of the command. -*/ - dupcmd =strdup (localcmd); - -if (!dupcmd) -return -ENOMEM ; -if (label->append)setenv ("bootargs" , label->append); -printf ("running: %s\n" , dupcmd); - - ret =run_command (dupcmd,0 ); +printf ("running: %s\n" , localcmd); -free (dupcmd); - -return ret; +return run_command_list (localcmd,strlen (localcmd),0 ); }/*
common/cmd_source.c +1 -48
67 lines not on this page · raw message
common/main.c +72 -0
84 lines not on this page · raw message
include/common.h +1 -0
@@ -261,6 +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); +int run_command_list (const char *cmd,int len,int flag);int readline (const char *const prompt);int readline_into_buffer (const char *const prompt,char *buffer,int timeout);
Signature · 1 line
1.7.7.3
PATCH 2/2 Allow newlines within command environment vars
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
--- common/main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
common/main.c +3 -3
@@ -334,7 +334,7 @@ void main_loop (void)int prev =disable_ctrlc (1 );/* disable Control C checking */ # endif -run_command (p,0 ); +run_command_list (p,strlen (p),0 );# ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc (prev);/* restore Control C checking */
@@ -382,7 +382,7 @@ void main_loop (void)int prev =disable_ctrlc (1 );/* disable Control C checking */ # endif -run_command (s,0 ); +run_command_list (s,strlen (s),0 );# ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc (prev);/* restore Control C checking */
@@ -393,7 +393,7 @@ void main_loop (void)if (menukey ==CONFIG_MENUKEY ) { s =getenv ("menucmd" );if (s) -run_command (s,0 ); +run_command_list (s,strlen (s),0 ); }#endif /* CONFIG_MENUKEY */ #endif /* CONFIG_BOOTDELAY */
Signature · 1 line
1.7.7.3
PATCH 2/2 Allow newlines within command environment vars
Dear Simon Glass,
Quoted 8 lines · In message <[email protected]> you wrote:
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
Signed-off-by Simon Glass <[email protected]>--- common/main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
Please squash this into the patch that introduces run_command_list() [assuming this is the patch which breaks behaviour?]
Best regards,
Wolfgang Denk
Signature · 7 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The word "fit", as I understand it, means "appropriate to a purpose",
and I would say the body of the Dean is supremely appropriate to the
purpose of sitting around all day and eating big heavy meals.
- Terry Pratchett, _Moving Pictures_PATCH 2/2 Allow newlines within command environment vars
Hi Wolfgang,
Quoted 7 lines · Wolfgang Denk
Dear Simon Glass,
Quoted 4 lines · In message <[email protected]> you wrote:
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Simon Glass <[email protected]>--- common/main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)Please squash this into the patch that introduces
run_command_list()[assuming this is the patch which breaks behaviour?]
The first patch is just a clean-up and should not change any behaviour. This second patch changes the behaviour of the named env variables.
I will combine them.
Regards,
Simon
Quoted 14 lines
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de The word "fit", as I understand it, means "appropriate to a purpose", and I would say the body of the Dean is supremely appropriate to the purpose of sitting around all day and eating big heavy meals.
- Terry Pratchett,_MovingPictures_
PATCH 2/2 Allow newlines within command environment vars
Dear Simon Glass,
Quoted 11 lines · In message <CAPnjgZ0XueZukodaNSdQSYvuevyiH5Z-=3-2pkcC7OekddG-JA@mail.gmail.com>…
Quoted 11 lines
Quoted 8 lines · In message <[email protected]> you wrote:
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
Signed-off-by Simon Glass <[email protected]>--- common/main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
Quoted 6 lines
Quoted 3 lines
Please squash this into the patch that introduces
run_command_list()[assuming this is the patch which breaks behaviour?]The first patch is just a clean-up and should not change any behaviour. This second patch changes the behaviour of the named env variables.
So which commit introduced the breakage, then?
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de A stone was placed at a ford in a river with the inscription: "When this stone is covered it is dangerous to ford here."
PATCH 2/2 Allow newlines within command environment vars
Hi Wolfgang,
Quoted 14 lines · Wolfgang Denk
Dear Simon Glass,
Quoted 11 lines · In message <CAPnjgZ0XueZukodaNSdQSYvuevyiH5Z-=3-2pkcC7OekddG-JA@mail.gmail.com>…
Quoted 11 lines
Quoted 8 lines · In message <[email protected]> you wrote:
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
Signed-off-by Simon Glass <[email protected]>---
?common/main.c | ? ?6 +++---
?1 files changed, 3 insertions(+), 3 deletions(-)
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Please squash this into the patch that introduces
run_command_list()[assuming this is the patch which breaks behaviour?]The first patch is just a clean-up and should not change any behaviour. This second patch changes the behaviour of the named env variables.
So which commit introduced the breakage, then?
Which breakage are you referring to? The intent of these two patches is to add a new feature (proposed by Michael Walle <[email protected]> who also provided a patch) on top of the command refactor series.
Sorry, there is some confusion here but I'm not sure what it is.
Regards,
Simon
Quoted 11 lines
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, ? ? MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de A stone was placed at a ford in a river with the inscription:
"When this stone is covered it is dangerous to ford here."
PATCH 2/2 Allow newlines within command environment vars
Dear Simon Glass,
In message <CAPnjgZ1BMMrN-+bHFr9GcH-5wF4WSz_7Qa53C_cHPLEbL2ycqA@mail.gmail.com> you wrote:
...
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
...
Quoted 10 lines
Quoted 4 lines
Quoted 2 lines
The first patch is just a clean-up and should not change any behaviour. This second patch changes the behaviour of the named env variables.
So which commit introduced the breakage, then?
Which breakage are you referring to? The intent of these two patches is to add a new feature (proposed by Michael Walle <[email protected]> who also provided a patch) on top of the command refactor series.
Sorry, there is some confusion here but I'm not sure what it is.
Guess I'm confused.
I don't understand in which way the variables listed here (preboot, bootcmd and menucmd) are special - actually all variables can hold commands, that then can be executed using the "run" command.
If newline is a valid command separator (and I think it is), this should work for _all_ variable.
Best regards,
Wolfgang Denk
Signature · 4 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Backed up the system lately?
PATCH 2/2 Allow newlines within command environment vars
Hi Wolfgang,
Quoted 19 lines · Wolfgang Denk
Dear Simon Glass,
In message <CAPnjgZ1BMMrN-+bHFr9GcH-5wF4WSz_7Qa53C_cHPLEbL2ycqA@mail.gmail.com> you wrote:
...Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
...
Quoted 10 lines
Quoted 4 lines
Quoted 2 lines
The first patch is just a clean-up and should not change any behaviour. This second patch changes the behaviour of the named env variables.
So which commit introduced the breakage, then?
Which breakage are you referring to? The intent of these two patches is to add a new feature (proposed by Michael Walle <[email protected]> who also provided a patch) on top of the command refactor series.
Sorry, there is some confusion here but I'm not sure what it is.
Quoted 8 lines
Guess I'm confused.
I don't understand in which way the variables listed here (preboot, bootcmd and menucmd) are special - actually all variables can hold commands, that then can be executed using the "run" command.
If newline is a valid command separator (and I think it is), this should work for _all_ variable.
OK I see. It is my commit message that is confusing, sorry. I didn't mean that only those env variables can hold a command, just that this patch affects the code which makes those environment variables work.
I will update the commit message and resend this patch.
Regards,
Simon
Quoted 12 lines
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, ? ? MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Backed up the system lately?
PATCH v2 2/2 Allow newlines within command environment vars
In reply to Simon GlassAny environment variable can hold commands to be executed by the 'run' command. The environment variables preboot, bootcmd and menucmd have special code for triggering execution in certain circumstances.
We adjust these calls to use run_command_list() instead of run_command(). This change permits these variables to have embedded newlines so that they work the same as the 'source' command.
---
Changes in v2:
- Update commit message to be less confusing
common/main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
common/main.c +3 -3
@@ -334,7 +334,7 @@ void main_loop (void)int prev =disable_ctrlc (1 );/* disable Control C checking */ # endif -run_command (p,0 ); +run_command_list (p,strlen (p),0 );# ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc (prev);/* restore Control C checking */
@@ -382,7 +382,7 @@ void main_loop (void)int prev =disable_ctrlc (1 );/* disable Control C checking */ # endif -run_command (s,0 ); +run_command_list (s,strlen (s),0 );# ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc (prev);/* restore Control C checking */
@@ -393,7 +393,7 @@ void main_loop (void)if (menukey ==CONFIG_MENUKEY ) { s =getenv ("menucmd" );if (s) -run_command (s,0 ); +run_command_list (s,strlen (s),0 ); }#endif /* CONFIG_MENUKEY */ #endif /* CONFIG_BOOTDELAY */
Signature · 1 line
1.7.7.3
PATCH 2/2 Allow newlines within command environment vars
In reply to Simon GlassDear Simon Glass,
Quoted 3 lines · In message <[email protected]> you wrote:
The environment variables preboot, bootcmd and menucmd can hold a command to execute. This change permits these variables to have newlines so that they work the same as the 'source' command.
This comment needs to be fixed. Not only these variables are effected, and from the patch it is not visible what it has to do with newlines.
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Man did not weave the web of life; he is merely a strand in it. Whatever he does to the web, he does to himself. - Seattle [1854]
PATCH 1/2 Add run_command_list() to run a list of commands
In reply to Simon GlassAm Mittwoch 15 Februar 2012, 07:07:09 schrieb Simon Glass:
Quoted 14 lines
This new function runs a list of commands separated by semicolon. We move this out of
cmd_sourceso that it can be used by other code. The PXE also uses the new function.Suggested-by Michael Walle <[email protected]>Signed-off-by Simon Glass <[email protected]>--- common/cmd_pxe.c | 20 ++------------ common/cmd_source.c | 49 +---------------------------------- common/main.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/common.h | 1 + 4 files changed, 77 insertions(+), 65 deletions(-)
[..snip..]
Quoted 41 lines
common/main.c +26
-0@@ -30,6 +30,7 @@#include <common.h> #include <watchdog.h> #include <command.h> +#include <malloc.h> #include <version.h> #ifdef CONFIG_MODEM_SUPPORT #include <malloc.h> /* for free() prototype */ @@ -1373,6 +1374,77 @@ int run_command(const char *cmd, int flag)#endif } +#ifndef CONFIG_SYS_HUSH_PARSER +static int builtin_run_command_list (char *cmd,int flag) +{ +char *line, *next; +int rcode =0 ; + +/* +* Break into individual lines, and execute each line; terminate on +* error. +*/ + line = next = cmd; +while (*next) { +if (*next =='\n' ) { + *next ='\0' ; +/* run only non-empty commands */ +if (*line) { +debug ("** exec: \"%s\"\n" , line); +if (builtin_run_command (line,0 ) <0 ) { + rcode =1 ; +break ; + } + } + line = next +1 ; + } + ++next;
common/main.c int run_command(const char *cmd, int flag) · line 1411
Quoted 10 lines
common/main.c +10
-0+ } +if (rcode ==0 && *cmd) + rcode = (builtin_run_command (cmd,0 ) >=0 ); + +return rcode; +} +#endif + +/* +* Run a list of commands separated by ;
mh, where is the command string seperated by ';' if no hush is used?
Quoted 6 lines
common/main.c +2
-0+* +* Note that if 'len' is not -1, then the command may not be \0 terminated, + * Memory will be allocated for the command in that case.
+ *
+ * @param cmd List of commands to run, each separated bu semicolon
+ * @param len Length of command excluding terminator if known (-1 if
not)
common/main.c int run_command(const char *cmd, int flag) · line 1424
Quoted 7 lines
+ * @param flag Execution flags (
CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */common/main.c +4
-0+int run_command_list (const char *cmd,int len,int flag) +{ +int need_buff =1 ; +char *buff = (char *)cmd;
mhhh, builtin_run_command_list will modify buff. what do you think about always copying the buffer if no hush parser is used?
include/common.h int print_buffer (ulong addr, void* data, uint
Quoted 32 lines
common/main.c +23
-0+int rcode =0 ; + +if (len ==-1 ) { + len =strlen (cmd); + need_buff =strchr (cmd,'\n' ) !=NULL ; + } +if (need_buff) { + buff =malloc (len +1 ); +if (!buff) +return 1 ; +memcpy (buff, cmd, len +1 ); + } +#ifdef CONFIG_SYS_HUSH_PARSER + rcode =parse_string_outer (buff,FLAG_PARSE_SEMICOLON ); +#else + rcode =builtin_run_command_list (buff, flag); +#endif +if (need_buff) +free (buff); + +return rcode; +} +/************************************************************************* ***/
#if
defined(CONFIG_CMD_RUN)include/common.h +0
-0@@ -261,6 +261,7 @@ int print_buffer (ulong addr, void* data, uint
width,
Quoted 7 lines
uint count, uint linelen); /*
common/main.c*/
voidmain_loop(void);
intrun_command(const char *cmd, int flag);include/common.h +1
-0+int run_command_list (const char *cmd,int len,int flag);int readline (const char *const prompt);int readline_into_buffer (const char *const prompt,char *buffer,int timeout);
Signature · 1 line
Michael
PATCH 1/2 Add run_command_list() to run a list of commands
Hi Michael,
Quoted 61 lines · Michael Walle
Am Mittwoch 15 Februar 2012, 07:07:09 schrieb Simon Glass:
Quoted 14 lines
This new function runs a list of commands separated by semicolon. We move this out of
cmd_sourceso that it can be used by other code. The PXE also uses the new function.Suggested-by Michael Walle <[email protected]>Signed-off-by Simon Glass <[email protected]>---
?common/cmd_pxe.c ? ?| ? 20 ++------------ ?common/cmd_source.c | ? 49 +---------------------------------- ?common/main.c ? ? ? | ? 72+++++++++++++++++++++++++++++++++++++++++++++++++++
include/common.h? ?| ? 1 +
?4 files changed, 77 insertions(+), 65 deletions(-)[..snip..]
Quoted 44 lines
common/main.c +0
-0@@ -30,6 +30,7 @@?#include <
common.h>
?#include <watchdog.h>
?#include <command.h>
+#include <malloc.h>
?#include <version.h>
?#ifdefCONFIG_MODEM_SUPPORT
?#include <malloc.h> ? ? ? ? ?/* forfree()prototype */common/main.c +0
-0@@ -1373,6 +1374,77 @@ int run_command(const char *cmd, int flag)?#endif
?}common/main.c +28
-0+#ifndef CONFIG_SYS_HUSH_PARSER +static int builtin_run_command_list (char *cmd,int flag) +{ + ? ? char *line, *next; + ? ? int rcode = 0; + + ? ? /* + ? ? ?* Break into individual lines, and execute each line; terminate on + ? ? ?* error. + ? ? ?*/ + ? ? line = next = cmd; + ? ? while (*next) { + ? ? ? ? ? ? if (*next == '\n') { + ? ? ? ? ? ? ? ? ? ? *next = '\0'; + ? ? ? ? ? ? ? ? ? ? /* run only non-empty commands */ + ? ? ? ? ? ? ? ? ? ? if (*line) { + ? ? ? ? ? ? ? ? ? ? ? ? ? ? debug("** exec: \"%s\"\n", line); + ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (builtin_run_command(line, 0) < 0) { + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? rcode = 1; + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; + ? ? ? ? ? ? ? ? ? ? ? ? ? ? } + ? ? ? ? ? ? ? ? ? ? } + ? ? ? ? ? ? ? ? ? ? line = next + 1; + ? ? ? ? ? ? } + ? ? ? ? ? ? ++next; + ? ? } + ? ? if (rcode == 0 && *cmd) + ? ? ? ? ? ? rcode = (builtin_run_command(cmd, 0) >= 0);
Quoted 8 lines
common/main.c int run_command(const char *cmd, int flag) · line 1411
Quoted 7 lines
common/main.c +7
-0+ + ? ? return rcode; +} +#endif + +/* +* Run a list of commands separated by ; mh, where is the command string seperated by ';' if no hush is used?
In that case it is handled by builtin_run_command() already.
Quoted 8 lines
Quoted 7 lines
+ *
+ * Note that if 'len' is not -1, then the command may not be \0 terminated, + * Memory will be allocated for the command in that case.
+ *
+ * @param cmd ? ? ? ?List of commands to run, each separated bu semicolon
+ * @param len ? ? ? ?Length of command excluding terminator if known (-1 ifnot)
Quoted 9 lines
common/main.c int run_command(const char *cmd, int flag)
Quoted 7 lines
+ * @param flag ? ? ? Execution flags (
CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */common/main.c +4
-0+int run_command_list (const char *cmd,int len,int flag) +{ + ? ? int need_buff = 1; + ? ? char *buff = (char *)cmd;mhhh,
builtin_run_command_listwill modify buff. what do you think about always copying the buffer if no hush parser is used?
Yes we could do - how would that help?
Quoted 42 lines
include/common.h int ? ? ? print_buffer (ulong addr, void* data, uint
Quoted 33 lines
+ ? ? int rcode = 0;
common/main.c +22
-0+ + ? ? if (len == -1) { + ? ? ? ? ? ? len = strlen(cmd); + ? ? ? ? ? ? need_buff = strchr(cmd, '\n') != NULL; + ? ? } + ? ? if (need_buff) { + ? ? ? ? ? ? buff = malloc(len + 1); + ? ? ? ? ? ? if (!buff) + ? ? ? ? ? ? ? ? ? ? return 1; + ? ? ? ? ? ? memcpy(buff, cmd, len + 1); + ? ? } +#ifdef CONFIG_SYS_HUSH_PARSER + ? ? rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON); +#else + ? ? rcode = builtin_run_command_list(buff, flag); +#endif + ? ? if (need_buff) + ? ? ? ? ? ? free(buff); + + ? ? return rcode; +} +?/************************************************************************* ***/
?#if
defined(CONFIG_CMD_RUN)include/common.h +0
-0@@ -261,6 +261,7 @@ int ? ? ? print_buffer (ulong addr, void* data, uintwidth,
Quoted 7 lines
uint count, uint linelen); /*
common/main.c*/
?voidmain_loop? ? ? (void);
?intrun_command(const char *cmd, int flag);
+intrun_command_list(const char *cmd, int len, int flag); ?int ?readline ? ? ? ?(const char *const prompt);
?int ?readline_into_buffer(const char *const prompt, char *buffer, ? ? ? ? ? ? ? ? ? ? ? int timeout);
Quoted 2 lines
--
Michael
Regards,
Simon
PATCH 1/2 Add run_command_list() to run a list of commands
Am Mittwoch 15 Februar 2012, 23:23:19 schrieb Simon Glass:
Quoted 97 lines
Hi Michael,
Quoted 62 lines · Michael Walle
Am Mittwoch 15 Februar 2012, 07:07:09 schrieb Simon Glass:
Quoted 13 lines
This new function runs a list of commands separated by semicolon. We move this out of
cmd_sourceso that it can be used by other code. The PXE also uses the new function.Suggested-by Michael Walle <[email protected]>Signed-off-by Simon Glass <[email protected]>--- common/cmd_pxe.c | 20 ++------------ common/cmd_source.c | 49 +---------------------------------- common/main.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/common.h | 1 + 4 files changed, 77 insertions(+), 65 deletions(-)[..snip..]
Quoted 45 lines
common/main.c +9
-0@@ -30,6 +30,7 @@#include <common.h> #include <watchdog.h> #include <command.h> +#include <malloc.h> #include <version.h> #ifdef CONFIG_MODEM_SUPPORT #include <malloc.h> /* for free() prototype */ @@ -1373,6 +1374,77 @@ int run_command(const char *cmd, int flag)#endif } +#ifndef CONFIG_SYS_HUSH_PARSER +static int builtin_run_command_list (char *cmd,int flag) +{ +char *line, *next; +int rcode =0 ; + +/* +* Break into individual lines, and execute each line; terminate on + * error.
common/main.c +20
-0+*/ + line = next = cmd; +while (*next) { +if (*next =='\n' ) { + *next ='\0' ; +/* run only non-empty commands */ +if (*line) { +debug ("** exec: \"%s\"\n" , line); +if (builtin_run_command (line,0 ) <0 ) { + rcode =1 ; +break ; + } + } + line = next +1 ; + } + ++next; + } +if (rcode ==0 && *cmd) + rcode = (builtin_run_command (cmd,0 ) >=0 ); +Quoted 8 lines
common/main.c int run_command(const char *cmd, int flag) · line 1411
Quoted 6 lines
common/main.c +6
-0+ return rcode; +} +#endif + +/* +* Run a list of commands separated by ; mh, where is the command string seperated by ';' if no hush is used?
In that case it is handled by
builtin_run_command()already.Quoted 10 lines
Quoted 7 lines
+ *
+ * Note that if 'len' is not -1, then the command may not be \0 terminated, + * Memory will be allocated for the command in that case.
+ *
+ * @param cmd List of commands to run, each separated bu
semicolon + * @param len Length of command excluding terminator
if known (-1 ifnot)
Quoted 10 lines
common/main.c int run_command(const char *cmd, int flag) · line 1424
Quoted 7 lines
+ * @param flag Execution flags (
CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */common/main.c +4
-0+int run_command_list (const char *cmd,int len,int flag) +{ +int need_buff =1 ; +char *buff = (char *)cmd;mhhh,
builtin_run_command_listwill modify buff. what do you think about always copying the buffer if no hush parser is used?
Quoted 1 line
Yes we could do - how would that help?
ah sorry, i hadn't understood your code. too late here ;)
copying the buffer everytime (in case no hush is available):
- removes the cast, which seems very odd on the first look
- makes run_command_list() more robust against furture changes in builtin_run_command_list(), eg. if it modifies the string not only if '\n' is found in the cmd.
at least you should add a comment to keep the check for need_buff in sync with builtin_run_command_list().
Signature · 1 line
Michael
PATCH 1/2 Add run_command_list() to run a list of commands
Hi Michael,
Quoted 98 lines · Michael Walle
Am Mittwoch 15 Februar 2012, 23:23:19 schrieb Simon Glass:
Quoted 97 lines
Hi Michael,
Quoted 62 lines · Michael Walle
Am Mittwoch 15 Februar 2012, 07:07:09 schrieb Simon Glass:
Quoted 13 lines
This new function runs a list of commands separated by semicolon. We move this out of
cmd_sourceso that it can be used by other code. The PXE also uses the new function.Suggested-by Michael Walle <[email protected]>Signed-off-by Simon Glass <[email protected]>---
?common/cmd_pxe.c ? ?| ? 20 ++------------ ?common/cmd_source.c | ? 49 +---------------------------------- ?common/main.c ? ? ? | ? 72+++++++++++++++++++++++++++++++++++++++++++++++++++
include/common.h?| 1 +
?4 files changed, 77 insertions(+), 65 deletions(-)[..snip..]
Quoted 45 lines
common/main.c +0
-0@@ -30,6 +30,7 @@?#include <
common.h>
?#include <watchdog.h>
?#include <command.h>
+#include <malloc.h>
?#include <version.h>
?#ifdefCONFIG_MODEM_SUPPORT
?#include <malloc.h> ? ? ? ? ?/* forfree()prototype */common/main.c +0
-0@@ -1373,6 +1374,77 @@ int run_command(const char *cmd, int flag)?#endif
?}common/main.c +8
-0+#ifndef CONFIG_SYS_HUSH_PARSER +static int builtin_run_command_list (char *cmd,int flag) +{ + ? ? char *line, *next; + ? ? int rcode = 0; + + ? ? /* +? ? ?* Break into individual lines, and execute each line; terminate on + ? ? ?* error.
+ ? ? ?*/
+ ? ? line = next = cmd;
+ ? ? while (*next) {
+ ? ? ? ? ? ? if (*next == '\n') {
+ ? ? ? ? ? ? ? ? ? ? *next = '\0';
+ ? ? ? ? ? ? ? ? ? ? /* run only non-empty commands */
+ ? ? ? ? ? ? ? ? ? ? if (*line) {
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ?debug("** exec: \"%s\"\n", line);
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (builtin_run_command(line, 0)< 0) {
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? rcode = 1;
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
+ ? ? ? ? ? ? ? ? ? ? }
+ ? ? ? ? ? ? ? ? ? ? line = next + 1;
+ ? ? ? ? ? ? }
+ ? ? ? ? ? ? ++next;
+ ? ? }
+ ? ? if (rcode == 0 && *cmd)
+ ? ? ? ? ? ? rcode = (builtin_run_command(cmd, 0)>= 0); +Quoted 8 lines
common/main.c int run_command(const char *cmd, int flag) · line 1411
Quoted 6 lines
+ ? ? return rcode;
common/main.c +5
-0+} +#endif + +/* +* Run a list of commands separated by ; mh, where is the command string seperated by ';' if no hush is used?
In that case it is handled by
builtin_run_command()already.Quoted 20 lines
Quoted 7 lines
+ *
+ * Note that if 'len' is not -1, then the command may not be \0 terminated, + * Memory will be allocated for the command in that case.
+ *
+ * @param cmd ? ? ? ?List of commands to run, each separated bu semicolon + * @param len ? ? ? ?Length of command excluding terminator if known (-1 ifnot)
common/main.c int run_command(const char *cmd, int flag)
Quoted 7 lines
+ * @param flag ? ? ? Execution flags (
CMD_FLAG_...)
+ * @return 0 on success, or != 0 on error.
+ */common/main.c +4
-0+int run_command_list (const char *cmd,int len,int flag) +{ + ? ? int need_buff = 1; + ? ? char *buff = (char *)cmd;mhhh,
builtin_run_command_listwill modify buff. what do you think about always copying the buffer if no hush parser is used?
Quoted 5 lines
Quoted 1 line
Yes we could do - how would that help?
ah sorry, i hadn't understood your code. too late here ;)
copying the buffer everytime (in case no hush is available):
?- removes the cast, which seems very odd on the first look
I can add a comment.
Quoted 3 lines
?- makes
run_command_list()more robust against furture changes in ?builtin_run_command_list(), eg. if it modifies the string not only if ? '\n' is found in the cmd.
Yes that's true, and I wasn't able to pass 'cmd' which would solve the problem at compile time.
Quoted 3 lines
at least you should add a comment to keep the check for
need_buffin sync withbuiltin_run_command_list().
Yes, will do that. I am a little reluctant to always malloc() since it introduces a malloc() even in the trivial case where we want to execute the contents of a simple env variable with no newline.
Quoted 4 lines
--
Michael
Regards,
Simon
PATCH 1/2 Add run_command_list() to run a list of commands
In reply to Simon GlassDear Simon Glass,
Quoted 3 lines · In message <[email protected]> you wrote:
This new function runs a list of commands separated by semicolon. We move this out of
cmd_sourceso that it can be used by other code. The PXE also uses the new function.
Separated by semicolon? What about newline here?
common/cmd_pxe.c static void label_print(void *data) · line 528
Quoted 1 line
common/cmd_pxe.c +1
-0+printf ("running: %s\n" , localcmd);
Should this be a debug() ?
Quoted 8 lines
common/main.c +8
-0+ line = next = cmd; +while (*next) { +if (*next =='\n' ) { + *next ='\0' ; +/* run only non-empty commands */ +if (*line) { +debug ("** exec: \"%s\"\n" , line); +if (builtin_run_command (line,0 ) <0 ) {
common/main.c int run_command(const char *cmd, int flag) · line 1404
Quoted 10 lines
common/main.c +10
-0+ rcode =1 ; +break ; + } + } + line = next +1 ; + } + ++next; + } +if (rcode ==0 && *cmd) + rcode = (builtin_run_command (cmd,0 ) >=0 );
This looks wrong to me. There you are re-executing the original command. Shoudl this not be
if (rcode == 0 && *line) rcode = (run_command(line, 0) >= 0);
??
Best regards,
Wolfgang Denk
Signature · 4 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Computers are not intelligent. They only think they are.
PATCH 1/2 Add run_command_list() to run a list of commands
Hi Wolfgang,
Quoted 6 lines · Wolfgang Denk
Dear Simon Glass,
Quoted 3 lines · In message <[email protected]> you wrote:
This new function runs a list of commands separated by semicolon. We move this out of
cmd_sourceso that it can be used by other code. The PXE also uses the new function.
This took a bit longer to get back to than I expected.
Quoted 6 lines
Separated by semicolon? ?What about newline here?
Quoted 1 line
+ ? ?
printf("running: %s\n", localcmd);Should this be a
debug()?
I'm not sure - the old code has printf() but I don't know why. I will change it.
Quoted 20 lines
Quoted 11 lines
+ ? ? line = next = cmd;
+ ? ? while (*next) {
+ ? ? ? ? ? ? if (*next == '\n') {
+ ? ? ? ? ? ? ? ? ? ? *next = '\0';
+ ? ? ? ? ? ? ? ? ? ? /* run only non-empty commands */
+ ? ? ? ? ? ? ? ? ? ? if (*line) {
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ?debug("** exec: \"%s\"\n", line);
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (builtin_run_command(line, 0)< 0) {
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? rcode = 1;
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;Quoted 8 lines
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
+ ? ? ? ? ? ? ? ? ? ? }
+ ? ? ? ? ? ? ? ? ? ? line = next + 1;
+ ? ? ? ? ? ? }
+ ? ? ? ? ? ? ++next;
+ ? ? }
+ ? ? if (rcode == 0 && *cmd)
+ ? ? ? ? ? ? rcode = (builtin_run_command(cmd, 0)>= 0);
Quoted 7 lines
This looks wrong to me. ?There you are re-executing the original command. ?Shoudl this not be
? ? ? ?if (rcode == 0 && *line)
? ? ? ? ? ? ? ?rcode = (run_command(line, 0)>= 0);??
Yes you are right of course. I created a few tests and found a few other nits as well, so will send a new series that tidies all this up.
Regards,
Simon
Quoted 10 lines
Best regards,
Wolfgang Denk
--
DENX Software Engineering GmbH, ? ? MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Computers are not intelligent. ?They only think they are.