add [ as alias for test, fix 0/1 argument handling
v2 cover add [ as alias for test, fix 0/1 argument handling
Make 'test' behave a little more like its cousins in other shells, by allowing the [ ... ] spelling, and while here, fix up the handling of a single, non-empty argument to comply with POSIX.
v2: update documentation, add a few more test cases.
CI is happy: https://github.com/u-boot/u-boot/pull/905
v1: https://lore.kernel.org/u-boot/[email protected]/
Rasmus Villemoes (4):
cmd: test: allow using [ as alias for test
doc: test: document [ ] spelling of test
test: add tests for left-bracket alias for 'test' command
cmd: test: fix handling of single-argument form of test
cmd/test.c | 30 +++++++++++++++-- doc/usage/cmd/test.rst | 5 ++- test/hush/if.c | 73 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 3 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/4 cmd: test: allow using [ as alias for test
I often find myself writing something like
if [ "$somevar" = 123 ] ; then ...
only to realize that that syntax doesn't work in U-Boot shell, and must be spelled
if test "$somevar" = 123 ; then
It only takes a few lines of code to support this POSIX-standardized alias for test, and helps developers focus on their actual problems instead of dealing with such unexpected quirks of the shell.
--- cmd/test.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
cmd/test.c +19 -0
@@ -63,6 +63,14 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,char *const *ap;int i, op, left, adv, expr, last_expr, last_unop, last_binop; +if (!strcmp (argv[0 ],"[" )) { +if (strcmp (argv[argc -1 ],"]" )) { +printf ("[: missing terminating ]\n" ); +return 1 ; + } + argc--; + } +/* args? */ if (argc <3 )return 1 ;
@@ -212,6 +220,17 @@ U_BOOT_CMD("[args..]" ); +/* +* This does not use the U_BOOT_CMD macro as [ can't be used in symbol names +*/ +ll_entry_declare(struct cmd_tbl, lbracket, cmd) = { +"[" ,CONFIG_SYS_MAXARGS , cmd_always_repeatable, do_test, +"alias for 'test'" , +#ifdef CONFIG_SYS_LONGHELP +" <test expression> ]" +#endif /* CONFIG_SYS_LONGHELP */ +}; +static int do_false (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]) {
Signature · 1 line
2.53.0
PATCH v2 2/4 doc: test: document [ ] spelling of test
In reply to Rasmus Villemoes--- doc/usage/cmd/test.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
doc/usage/cmd/test.rst +4 -1
@@ -20,11 +20,14 @@ Synopsis test -e <interface> <dev[:part]> <path> test <s> =~ <re> + [ <test expression> ] + Description ----------- The ``test`` command is similar to the ordinary shell built-in by the -same name. Unlike in ordinary shells, it cannot be spelled ``[``. +same name. Like in ordinary shells, it can also be spelled ``[``, +provided the test expression is followed by a separate ``]`` argument. Strings ~~~~~~~
Signature · 1 line
2.53.0
PATCH v2 3/4 test: add tests for left-bracket alias for 'test' command
In reply to Rasmus VillemoesDuplicate a few of the existing test cases, using the [ spelling, and also ensure that the presence of a matching ] as a separate and last argument is enforced.
--- test/hush/if.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
test/hush/if.c +43 -0
@@ -315,3 +315,46 @@ static int hush_test_if_z_operator(struct unit_test_state *uts)return 0 ; }HUSH_TEST (hush_test_if_z_operator,0 ); + +static int hush_test_lbracket_alias (struct unit_test_state *uts) +{ +char if_formatted[128 ]; +const char *missing_rbracket_error ="[: missing terminating ]" ; + +sprintf (if_formatted, if_format,"[ aaa = aaa ]" ); +ut_assertok (run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ aaa = bbb ]" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ aaa = aaa" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); + +sprintf (if_formatted, if_format,"[ aaa = bbb" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); + +sprintf (if_formatted, if_format,"[ aaa = aaa]" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); + +sprintf (if_formatted, if_format,"[ aaa = bbb]" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); + +sprintf (if_formatted, if_format,"[ aaa != aaa -o bbb != bbb ]" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ aaa != aaa -o bbb = bbb ]" ); +ut_assertok (run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ ! aaa != aaa -o ! bbb != bbb ]" ); +ut_assertok (run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ ! aaa != aaa -o ! bbb = bbb ]" ); +ut_assertok (run_command (if_formatted,0 )); + +return 0 ; +} +HUSH_TEST (hush_test_lbracket_alias,UTF_CONSOLE );
Signature · 1 line
2.53.0
PATCH v2 4/4 cmd: test: fix handling of single-argument form of test
In reply to Rasmus VillemoesPOSIX states that
0 arguments:
Exit false (1).
1 argument:
Exit true (0) if $1 is not null; otherwise, exit false.
and at least bash and busybox sh behave that way.
The current 'argc < 3' does the right thing for a non-existing or empty argv[1], but not for a non-empty argv[1]. Fix that and add corresponding test cases.
--- cmd/test.c | 11 +++++++++-- test/hush/if.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-)
cmd/test.c +9 -2
@@ -71,10 +71,17 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc, argc--; } -/* args? */ -if (argc <3 ) +/* +* Per POSIX, 'test' with 0 arguments should return 1, while +* 'test <arg>' should be equivalent to 'test -n <arg>', +* i.e. true if and only if <arg> is not empty. +*/ +if (argc <2 )return 1 ; +if (argc ==2 ) +return !strcmp (argv[1 ],"" ); +#ifdef DEBUG {debug ("test(%d):" , argc);
test/hush/if.c +30 -0
@@ -32,6 +32,15 @@ static int hush_test_if_base(struct unit_test_state *uts)sprintf (if_formatted, if_format,"false" );ut_asserteq (1 ,run_command (if_formatted,0 )); +sprintf (if_formatted, if_format,"test" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"test ''" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"test 'abc'" ); +ut_assertok (run_command (if_formatted,0 )); +return 0 ; }HUSH_TEST (hush_test_if_base,0 );
@@ -355,6 +364,27 @@ static int hush_test_lbracket_alias(struct unit_test_state *uts)sprintf (if_formatted, if_format,"[ ! aaa != aaa -o ! bbb = bbb ]" );ut_assertok (run_command (if_formatted,0 )); +sprintf (if_formatted, if_format,"[ ]" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); + +sprintf (if_formatted, if_format,"[ '' ]" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ ''" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); + +sprintf (if_formatted, if_format,"[ 'abc' ]" ); +ut_assertok (run_command (if_formatted,0 )); + +sprintf (if_formatted, if_format,"[ 'abc'" ); +ut_asserteq (1 ,run_command (if_formatted,0 )); +ut_assert_nextline (missing_rbracket_error); +return 0 ; }HUSH_TEST (hush_test_lbracket_alias,UTF_CONSOLE );
Signature · 1 line
2.53.0
Quoted 9 lines · Rasmus Villemoes
Make 'test' behave a little more like its cousins in other shells, by allowing the [ ... ] spelling, and while here, fix up the handling of a single, non-empty argument to comply with POSIX.
v2: update documentation, add a few more test cases.
CI is happy: https://github.com/u-boot/u-boot/pull/905
Quoted 11 lines
Rasmus Villemoes (4):
cmd: test: allow using [ as alias for test
doc: test: document [ ] spelling of test
test: add tests for left-bracket alias for 'test' command
cmd: test: fix handling of single-argument form of testcmd/test.c | 30 +++++++++++++++-- doc/usage/cmd/test.rst | 5 ++- test/hush/if.c | 73 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 3 deletions(-)
Quoted 9 lines · Rasmus Villemoes
Make 'test' behave a little more like its cousins in other shells, by allowing the [ ... ] spelling, and while here, fix up the handling of a single, non-empty argument to comply with POSIX.
v2: update documentation, add a few more test cases.
CI is happy: https://github.com/u-boot/u-boot/pull/905
[...]
Applied to u-boot/next, thanks!
[1/4] cmd: test: allow using [ as alias for test
commit: fc8bf9a984c118d551b3f93c66f1ae2733b9b588
[2/4] doc: test: document [ ] spelling of test
commit: d44f61582947564991cce7501f2e5db0b29ebad6
[3/4] test: add tests for left-bracket alias for 'test' command
commit: 6f9cc3310a764aaae4478b5a8a0c0cae3b2be4a1
[4/4] cmd: test: fix handling of single-argument form of test
commit: 8b0619579b2282050e7fb0d92fbc645b79d18baeSignature · 1 line
Tom
Quoted 14 lines · Tom Rini
Quoted 9 lines · Rasmus Villemoes
Make 'test' behave a little more like its cousins in other shells, by allowing the [ ... ] spelling, and while here, fix up the handling of a single, non-empty argument to comply with POSIX.
v2: update documentation, add a few more test cases.
CI is happy: https://github.com/u-boot/u-boot/pull/905
[...]
Applied to u-boot/next, thanks!
Quoted 12 lines
[1/4] cmd: test: allow using [ as alias for test
commit: fc8bf9a984c118d551b3f93c66f1ae2733b9b588 [2/4] doc: test: document [ ] spelling of test commit: d44f61582947564991cce7501f2e5db0b29ebad6 [3/4] test: add tests for left-bracket alias for 'test' command commit: 6f9cc3310a764aaae4478b5a8a0c0cae3b2be4a1 [4/4] cmd: test: fix handling of single-argument form of test commit: 8b0619579b2282050e7fb0d92fbc645b79d18baeSignature · 1 line
Tom
Hello,
While preparing a patch series based on u-boot/next, I realised that the bootflow on our module broke.
I discovered that the issue was introduced with
commit 8b0619579b2 ("cmd: test: fix handling of single-argument form of test")
This change breaks our boot script as it relies on the behaviour of test with unquoted empty expansions. For example:
```
test -n ${m4boot} || env set m4boot ';'
```Is now test -n, as there are no quotation marks. It now returns true, so the fallback assignment is skipped and it breaks our boot flow.
Any ideas? I think that we will not be the only ones affected by this once it's merged into master.
Best regards
Franz Schnyder
Quoted 37 lines · Franz Schnyder
Quoted 14 lines · Tom Rini
Quoted 9 lines · Rasmus Villemoes
Make 'test' behave a little more like its cousins in other shells, by allowing the [ ... ] spelling, and while here, fix up the handling of a single, non-empty argument to comply with POSIX.
v2: update documentation, add a few more test cases.
CI is happy: https://github.com/u-boot/u-boot/pull/905
[...]
Applied to u-boot/next, thanks!
Quoted 12 lines
[1/4] cmd: test: allow using [ as alias for test
commit: fc8bf9a984c118d551b3f93c66f1ae2733b9b588 [2/4] doc: test: document [ ] spelling of test commit: d44f61582947564991cce7501f2e5db0b29ebad6 [3/4] test: add tests for left-bracket alias for 'test' command commit: 6f9cc3310a764aaae4478b5a8a0c0cae3b2be4a1 [4/4] cmd: test: fix handling of single-argument form of test commit: 8b0619579b2282050e7fb0d92fbc645b79d18baeSignature · 1 line
TomHello,
While preparing a patch series based on u-boot/next, I realised that the bootflow on our module broke.
I discovered that the issue was introduced with
commit8b0619579b2("cmd: test: fix handling of single-argument form of test")This change breaks our boot script as it relies on the behaviour of test with unquoted empty expansions. For example:
Quoted 8 lines
``` test -n ${m4boot} || env set m4boot ';' ```Is now
test -n, as there are no quotation marks. It now returns true, so the fallback assignment is skipped and it breaks our boot flow.Any ideas? I think that we will not be the only ones affected by this once it's merged into master.
Urrgh, sorry about that. That snippet is arguably fragile and buggy, but yes, similar code can exist elsewhere.
I don't really like it, but I suppose one could special-case the single-argument case and interpret
test -n
as
test -n ""
with all other single-argument strings behaving as they do now. This would need a separate test case and explicit documentation. It's probably quite rare that one would test a string for emptiness where a possible non-empty value is exactly "-n", and since the single-argument case hasn't really been supported in U-Boot until now, I don't think such code really exists. Moreover, I don't think one should ever rely on that single-argument behaviour anyway;
test "$str"
should always be spelled
test -n "$str"
Single-argument 'test -z' does not need special treatment, because both 'test -z' and 'test -z ""' are true, but we could add the former as a test case as well if we do implement the above.
Rasmus