Add run_command_list() to run a list of commands
PATCH v3 1/4 Add run_command_list() to run a list of commands
This new function runs a list of commands separated by semicolon or newline. We move this out of cmd_source so that it can be used by other code. The PXE code also uses the new function.
---
Changes in v3:
- Added a few more comments on the need for malloc()
- Change PXE's printf() to a debug()
- Fix bug in built-in run_command()
- Fix commit message to mention ; and \n
- Move run_command_list() comment to header file
common/cmd_pxe.c | 20 ++---------- common/cmd_source.c | 49 +---------------------------- common/main.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/common.h | 13 ++++++++ 4 files changed, 102 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 ); +debug ("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 +85 -0
97 lines not on this page · raw message
include/common.h +13 -0
@@ -268,6 +268,19 @@ 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); + +/** +* Run a list of commands separated by ; or even \0 +* +* Note that if 'len' is not -1, then the command does not need to be nul +* 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 commands excluding terminator if known (-1 if not) +* @param flag Execution flags (CMD_FLAG_...) +* @return 0 on success, or != 0 on error. +*/ +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 v3 2/4 Allow newlines within command environment vars
Any 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
Changes in v3:
- Remove unneeded len parameter in calls to run_command_list
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,-1 ,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,-1 ,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,-1 ,0 ); }#endif /* CONFIG_MENUKEY */ #endif /* CONFIG_BOOTDELAY */
Signature · 1 line
1.7.7.3
Wolfgang Denk
AppliedPATCH v3 2/4 Allow newlines within command environment vars
Dear Simon Glass,
Quoted 13 lines · In message <[email protected]> you wrote:
Any 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 ofrun_command(). This change permits these variables to have embedded newlines so that they work the same as the 'source' command.Signed-off-by Simon Glass <[email protected]>---
Changes in v2:
- Update commit message to be less confusing
Quoted 5 lines
Changes in v3:
- Remove unneeded len parameter in calls torun_command_listcommon/main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
Applied, thanks.
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 The human mind treats a new idea the way the body treats a strange protein - it rejects it. - P. Medawar
PATCH v3 3/4 sandbox: Use the new run_command()
In reply to Simon GlassNow that run_command() handles both parsers, clean up sandbox to use it. This fixes a build error.
---
Changes in v3:
- Add new patch to clean up sandbox's run_command() usage
arch/sandbox/cpu/start.c | 6 ------ 1 files changed, 0 insertions(+), 6 deletions(-)
arch/sandbox/cpu/start.c +0 -6
@@ -90,13 +90,7 @@ int sandbox_main_loop_init(void)/* Execute command if required */ if (state->cmd) { -/* TODO: redo this when cmd tidy-up series lands */ -#ifdef CONFIG_SYS_HUSH_PARSER run_command (state->cmd,0 ); -#else -parse_string_outer (state->cmd,FLAG_PARSE_SEMICOLON | -FLAG_EXIT_FROM_LOOP ); -#endif os_exit (state->exit_type); }
Signature · 1 line
1.7.7.3
PATCH v3 3/4 sandbox: Use the new run_command()
Quoted 2 lines · On Saturday 31 March 2012 03:30:57 Simon Glass wrote:
Now that
run_command()handles both parsers, clean up sandbox to use it. This fixes a build error.
-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/20120401/25f6088a/attachment.pgp>
Wolfgang Denk
AppliedPATCH v3 3/4 sandbox: Use the new run_command()
In reply to Simon GlassDear Simon Glass,
Quoted 10 lines · In message <[email protected]> you wrote:
Now that
run_command()handles both parsers, clean up sandbox to use it. This fixes a build error.Signed-off-by Simon Glass <[email protected]>---
Changes in v3:
- Add new patch to clean up sandbox'srun_command()usagearch/sandbox/cpu/start.c | 6 ------ 1 files changed, 0 insertions(+), 6 deletions(-)
Applied, thanks.
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
If the facts don't fit the theory, change the facts.
-- Albert EinsteinPATCH v3 4/4 sandbox: Add basic test for command execution
In reply to Simon GlassSince run_command() and run_command_list() are important and a little confusing, add some basic tests to check that the behaviour is correct.
Note: I am not sure that this should be committed, nor where it should go in the source tree. Comments welcome.
To run the unit tests use the ut_cmd command available in sandbox:
make sandbox_config
make
./u-boot -c ut_cmd
(To test both hush and built-in parsers, you need to manually change CONFIG_SYS_HUSH_PARSER in include/configs/sandbox.h and build/run again)
---
Changes in v3:
- Add some unit tests for run_command() and run_command_list()
Makefile | 1 + test/Makefile | 45 ++++++++++++++++++++++++++++ test/command_ut.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 0 deletions(-) create mode 100644 test/Makefile create mode 100644 test/command_ut.c
Makefile +1 -0
@@ -301,6 +301,7 @@ LIBS += common/libcommon.o LIBS += lib/libfdt/libfdt.o LIBS += api/libapi.o LIBS += post/libpost.o +LIBS += test/libtest.o ifneq ($(CONFIG_AM33XX)$(CONFIG_OMAP34XX)$(CONFIG_OMAP44XX)$(CONFIG_OMAP54XX),) LIBS += $(CPUDIR)/omap-common/libomap-common.o
test/Makefile new file +45 -0
45 lines not on this page · raw message
test/command_ut.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
1.7.7.3
PATCH v3 4/4 sandbox: Add basic test for command execution
test/Makefile line 29
Quoted 10 lines · On Saturday 31 March 2012 03:30:58 Simon Glass wrote:
test/Makefile new file +7
-0+include $(TOPDIR)/config.mk + +LIB = $(obj)libtest.o + +COBJS-$(CONFIG_SANDBOX) += command_ut.o + +COBJS := $(sort $(COBJS-y))
that sort actually needed ?
test/Makefile line 33
Quoted 4 lines
test/Makefile new file +4
-0+SRCS := $(COBJS:.o=.c) +OBJS := $(addprefix $(obj),$(COBJS)) + +all: $(LIB) $(XOBJS)
$(XOBJS) is dead code
test/command_ut.c
Quoted 4 lines
test/command_ut.c new file +1
-0+#define DEBUG
should comment why you've always defined this
test/command_ut.c line 29
Quoted 3 lines
test/command_ut.c new file +3
-0+static const char test_cmd[] ="setenv list 1\n setenv list ${list}2; " +"setenv list ${list}3\0" +"setenv list ${list}4" ;
i'd put the first string on a new line too to make it easier to read
Quoted 1 line
+static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const
what is "ut" supposed to stand for ?
-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/20120401/c96a3750/attachment.pgp>
PATCH v3 4/4 sandbox: Add basic test for command execution
Hi Mike,
Quoted 11 lines · Mike Frysinger
test/Makefile
Quoted 10 lines · On Saturday 31 March 2012 03:30:58 Simon Glass wrote:
test/Makefile new file +7
-0+include $(TOPDIR)/config.mk + +LIB ?= $(obj)libtest.o + +COBJS-$(CONFIG_SANDBOX) += command_ut.o + +COBJS ? ? ? ?:= $(sort $(COBJS-y))
Not yet :-) I like what it does to object files, but it is pointless at least for now. Will drop it.
Quoted 9 lines
that sort actually needed ?
test/Makefile line 33
Quoted 4 lines
test/Makefile new file +4
-0+SRCS := $(COBJS:.o=.c) +OBJS := $(addprefix $(obj),$(COBJS)) + +all: $(LIB) $(XOBJS)$(XOBJS) is dead code
Removed
Quoted 7 lines
test/command_ut.c
Quoted 5 lines
test/command_ut.c new file +1
-0+#define DEBUG should comment why you've always defined this
done
Quoted 6 lines
Quoted 4 lines
+static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; "
+ ? ? ? ? ? ? "setenv list ${list}3\0"
+ ? ? ? ? ? ? "setenv list ${list}4";i'd put the first string on a new line too to make it easier to read
Done
Quoted 4 lines
Quoted 2 lines
+static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const
what is "ut" supposed to stand for ?
Unit test, as normally needed by code that is utterly tortuous.
Quoted 1 line
-mike
Regards,
Simon
PATCH v3 4/4 sandbox: Add basic test for command execution
Quoted 7 lines · On Wednesday 04 April 2012 03:18:30 Simon Glass wrote:
Quoted 4 lines · Mike Frysinger
Quoted 1 line · On Saturday 31 March 2012 03:30:58 Simon Glass wrote:
+static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const
what is "ut" supposed to stand for ?
Unit test, as normally needed by code that is utterly tortuous.
i'd use "unit test" in the cmd help string then
-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/20120408/4d1efa41/attachment.pgp>
Wolfgang Denk
AppliedPATCH v3 4/4 sandbox: Add basic test for command execution
In reply to Simon GlassDear Simon Glass,
Quoted 20 lines · In message <[email protected]> you wrote:
Since
run_command()andrun_command_list()are important and a little confusing, add some basic tests to check that the behaviour is correct.Note: I am not sure that this should be committed, nor where it should go in the source tree. Comments welcome.
To run the unit tests use the
ut_cmdcommand available in sandbox:make
sandbox_config
make
./u-boot -cut_cmd(To test both hush and built-in parsers, you need to manually change
CONFIG_SYS_HUSH_PARSERininclude/configs/sandbox.hand build/run again)Signed-off-by Simon Glass <[email protected]>---
Changes in v3:
- Add some unit tests forrun_command()andrun_command_list()
Quoted 6 lines
Makefile | 1 + test/Makefile | 45 ++++++++++++++++++++++++++++ test/command_ut.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 0 deletions(-) create mode 100644 test/Makefile create mode 100644 test/command_ut.c
Applied, thanks.
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 It usually takes more than three weeks to prepare a good impromptu speech. - Mark Twain
PATCH v3 1/4 Add run_command_list() to run a list of commands
In reply to Simon Glasscommon/cmd_pxe.c static void label_print(void *data) · line 530
Quoted 4 lines · On Saturday 31 March 2012 03:30:55 Simon Glass wrote:
common/cmd_pxe.c +1
-0+return run_command_list (localcmd,strlen (localcmd),0 );
should be -1 instead of strlen()
Quoted 6 lines
common/cmd_pxe.c +6
-0+int run_command_list (const char *cmd,int len,int flag) +{ +int need_buff =1 ; +char *buff = (char *)cmd;/* cast away const */ +int rcode =0 ; +
common/cmd_pxe.c
Quoted 10 lines
common/cmd_pxe.c +10
-0+if (len ==-1 ) { + len =strlen (cmd); +#ifdef CONFIG_SYS_HUSH_PARSER +/* hush will never change our string */ + need_buff =0 ; +#else +/* the built-in parser will change our string if it sees \n */ + need_buff =strchr (cmd,'\n' ) !=NULL ; +#endif + }
we have memchr(), so you should be able to split the len==-1 and the need_buff logic into two sep steps
also, should you handle the case where '\n' is the very last char ? or not bother ?
-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/20120401/846a2a5e/attachment.pgp>
PATCH v3 1/4 Add run_command_list() to run a list of commands
Hi Mike,
Quoted 7 lines · Mike Frysinger
common/cmd_pxe.c
Quoted 4 lines · On Saturday 31 March 2012 03:30:55 Simon Glass wrote:
common/cmd_pxe.c +1
-0+ ? ? return run_command_list(localcmd, strlen(localcmd), 0);should be -1 instead of
strlen()
done
Quoted 12 lines
Quoted 12 lines
common/cmd_pxe.c +11
-0+int run_command_list (const char *cmd,int len,int flag) +{ + ? ? int need_buff = 1; + ? ? char *buff = (char *)cmd; ? ? ? /* cast away const */ + ? ? int rcode = 0; + + ? ? if (len == -1) { + ? ? ? ? ? ? len = strlen(cmd); +#ifdef CONFIG_SYS_HUSH_PARSER + ? ? ? ? ? ? /* hush will never change our string */ + ? ? ? ? ? ? need_buff = 0;
Quoted 8 lines
common/cmd_pxe.c
Quoted 5 lines
common/cmd_pxe.c +5
-0+#else + ? ? ? ? ? ? /* the built-in parser will change our string if it sees \n */ + ? ? ? ? ? ? need_buff = strchr(cmd, '\n') != NULL; +#endif + ? ? }we have
memchr(), so you should be able to split the len==-1 and theneed_bufflogic into two sep steps
Are you saying that we should do this check if len is not -1 also?
I am only doing it when len is not specified, since if a length is provided we must allocate. This is used by the source command, which does not necessary have a nul-terminated string.
* Note that if 'len' is not -1, then the command does not need to be nul
* terminated, Memory will be allocated for the command in that case.
Sorry if I am missing your point.
Quoted 3 lines
also, should you handle the case where '\n' is the very last char ? ?or not bother ?
Do you mean not allocate in that case? I don't think it is a common situation is it?
Quoted 1 line
-mike
Regards,
Simon
PATCH v3 1/4 Add run_command_list() to run a list of commands
Quoted 25 lines · On Wednesday 04 April 2012 03:12:27 Simon Glass wrote:
Quoted 15 lines · Mike Frysinger
Quoted 14 lines · On Saturday 31 March 2012 03:30:55 Simon Glass wrote:
common/cmd_pxe.c +11
-0+int run_command_list (const char *cmd,int len,int flag) +{ +int need_buff =1 ; +char *buff = (char *)cmd;/* cast away const */ +int rcode =0 ; + +if (len ==-1 ) { + len =strlen (cmd); +#ifdef CONFIG_SYS_HUSH_PARSER +/* hush will never change our string */ + need_buff =0 ;Quoted 8 lines
common/cmd_pxe.c
Quoted 5 lines
common/cmd_pxe.c +2
-0+#else +/* the built-in parser will change our string if it sees \n */ +
need_buff=strchr(cmd, '\n')!= NULL;common/cmd_pxe.c +2
-0+#endif + }we have
memchr(), so you should be able to split the len==-1 and theneed_bufflogic into two sep steps
Quoted 5 lines
Are you saying that we should do this check if len is not -1 also?
I am only doing it when len is not specified, since if a length is provided we must allocate. This is used by the source command, which does not necessary have a nul-terminated string.
looks to me like it should be:
int need_buff;
if (len == -1)
len = strlen(cmd);
#ifdef CONFIG_SYS_HUSH_PARSER
need_buff = 0;
#else
need_buff = memchr(cmd, '\n', len) != NULL;
#endif
if (need_buff) {
...
}Quoted 5 lines
Quoted 2 lines
also, should you handle the case where '\n' is the very last char ? or not bother ?
Do you mean not allocate in that case? I don't think it is a common situation is it?
i don't know how commands get passed down (as i've never poked that area before) which is why i'm asking
-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/20120408/a54f35ff/attachment.pgp>
PATCH v3 1/4 Add run_command_list() to run a list of commands
Hi Mike,
Quoted 35 lines · Mike Frysinger
Quoted 25 lines · On Wednesday 04 April 2012 03:12:27 Simon Glass wrote:
Quoted 15 lines · Mike Frysinger
Quoted 14 lines · On Saturday 31 March 2012 03:30:55 Simon Glass wrote:
common/cmd_pxe.c +11
-0+int run_command_list (const char *cmd,int len,int flag) +{ + ? ? int need_buff = 1; + ? ? char *buff = (char *)cmd; ? ? ? /* cast away const */ + ? ? int rcode = 0; + + ? ? if (len == -1) { + ? ? ? ? ? ? len = strlen(cmd); +#ifdef CONFIG_SYS_HUSH_PARSER + ? ? ? ? ? ? /* hush will never change our string */ + ? ? ? ? ? ? need_buff = 0;Quoted 8 lines
Quoted 5 lines
+#else
+ ? ? ? ? ? ? /* the built-in parser will change our string if it sees \n */ + ? ? ? ? ? ?need_buff=strchr(cmd, '\n')!= NULL; +#endif
+ ? ? }we have
memchr(), so you should be able to split the len==-1 and theneed_bufflogic into two sep stepsQuoted 5 lines
Are you saying that we should do this check if len is not -1 also?
I am only doing it when len is not specified, since if a length is provided we must allocate. This is used by the source command, which does not necessary have a nul-terminated string.
looks to me like it should be:
? ? ? ?intneed_buff;
Quoted 11 lines
? ? ? ?if (len == -1)
? ? ? ? ? ? ? ?len = strlen(cmd);
#ifdefCONFIG_SYS_HUSH_PARSER
? ? ? ?need_buff = 0;
#else
? ? ? ?need_buff =memchr(cmd, '\n', len)!= NULL; #endif
? ? ? ?if (need_buff) {
? ? ? ? ? ? ? ?...
? ? ? ?}
This logic is different. For example it won't nul terminate a terminate a command string that is passed in with no terminator. I think this is required by the 'source' command.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
also, should you handle the case where '\n' is the very last char ? ?or not bother ?
Do you mean not allocate in that case? I don't think it is a common situation is it?
i don't know how commands get passed down (as i've never poked that area before) which is why i'm asking
Well within the normal command parses run_command() is called. They split up the commands for us. We only need run_command_list() when we think we might have a raw list of commands, on which no processing has been done.
Regards,
Simon
Quoted 1 line
-mike
Wolfgang Denk
AppliedPATCH v3 1/4 Add run_command_list() to run a list of commands
In reply to Simon GlassDear Simon Glass,
Quoted 14 lines · In message <[email protected]> you wrote:
This new function runs a list of commands separated by semicolon or newline. We move this out of
cmd_sourceso that it can be used by other code. The PXE code also uses the new function.Suggested-by Michael Walle <[email protected]>Signed-off-by Simon Glass <[email protected]>---
Changes in v3:
- Added a few more comments on the need formalloc()
- Change PXE'sprintf()to adebug()
- Fix bug in built-inrun_command()
- Fix commit message to mention ; and \n
- Moverun_command_list()comment to header file
Quoted 5 lines
common/cmd_pxe.c | 20 ++---------- common/cmd_source.c | 49 +---------------------------- common/main.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/common.h | 13 ++++++++ 4 files changed, 102 insertions(+), 65 deletions(-)
Applied, thanks.
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 Swap read error. You lose your mind.