mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/3] commands: test: simplify argv handling
@ 2023-07-04 11:29 Marco Felsch
  2023-07-04 11:29 ` [PATCH v2 2/3] commands: test: replace printf with pr_err Marco Felsch
  2023-07-04 11:29 ` [PATCH v2 3/3] commands: test: add based support for bash-test style Marco Felsch
  0 siblings, 2 replies; 5+ messages in thread
From: Marco Felsch @ 2023-07-04 11:29 UTC (permalink / raw)
  To: barebox

Decrement argc first before check the closing ']' to avoid the
*argv[argc - 1]. No functional change.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 commands/test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commands/test.c b/commands/test.c
index c845cec017..c1b84c42ef 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -75,11 +75,11 @@ static int do_test(int argc, char *argv[])
 	struct stat statbuf;
 
 	if (*argv[0] == '[') {
-		if (*argv[argc - 1] != ']') {
+		argc--;
+		if (*argv[argc] != ']') {
 			printf("[: missing `]'\n");
 			return 1;
 		}
-		argc--;
 	}
 
 	/* args? */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 2/3] commands: test: replace printf with pr_err
  2023-07-04 11:29 [PATCH v2 1/3] commands: test: simplify argv handling Marco Felsch
@ 2023-07-04 11:29 ` Marco Felsch
  2023-07-04 11:38   ` Sascha Hauer
  2023-07-04 11:29 ` [PATCH v2 3/3] commands: test: add based support for bash-test style Marco Felsch
  1 sibling, 1 reply; 5+ messages in thread
From: Marco Felsch @ 2023-07-04 11:29 UTC (permalink / raw)
  To: barebox

Replace the printf() with pr_err() to support:
 - color formated prints
 - logbuffer storing (dmesg)
 - loglevels

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v2:
 - new patch

 commands/test.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commands/test.c b/commands/test.c
index c1b84c42ef..5478011185 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -77,7 +77,7 @@ static int do_test(int argc, char *argv[])
 	if (*argv[0] == '[') {
 		argc--;
 		if (*argv[argc] != ']') {
-			printf("[: missing `]'\n");
+			pr_err("[: missing `]'\n");
 			return 1;
 		}
 	}
@@ -213,7 +213,7 @@ static int do_test(int argc, char *argv[])
 		}
 
 		if (left < adv) {
-			printf("test: failed to parse arguments\n");
+			pr_err("test: failed to parse arguments\n");
 			return 1;
 		}
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 3/3] commands: test: add based support for bash-test style
  2023-07-04 11:29 [PATCH v2 1/3] commands: test: simplify argv handling Marco Felsch
  2023-07-04 11:29 ` [PATCH v2 2/3] commands: test: replace printf with pr_err Marco Felsch
@ 2023-07-04 11:29 ` Marco Felsch
  1 sibling, 0 replies; 5+ messages in thread
From: Marco Felsch @ 2023-07-04 11:29 UTC (permalink / raw)
  To: barebox

Add bash-style test [[ expression ]] to support pattern matching like:

  - [[ "foo1" == "foo*" ]]
  - [[ "foo1" != "bar*" ]]

For more information see man 1 bash.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v2:
 - drop addittional Kconfig switch
 - fix FNMATCH select
 - drop conditional compilation
 - adapt commit message

 commands/Kconfig |  6 ++++--
 commands/test.c  | 33 +++++++++++++++++++++++++++------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index 7ca7b56fa5..17ad5ff87c 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1208,15 +1208,17 @@ config CMD_SLEEP
 config CMD_TEST
 	tristate
 	depends on SHELL_HUSH
+	select FNMATCH
 	default y
 	prompt "test"
 	help
-	  Minimal test command like in /bin/sh
+	  Minimal test command like in /bin/sh with support for bash-style
+	  [[ expression ]] tests as well.
 
 	  Usage: test [EXPR]
 
 	  Options:
-		  !, =, !=, -eq, -ne, -ge, -gt, -le, -lt, -o, -a, -z, -n, -d, -e,
+		  !, =, ==, !=, -eq, -ne, -ge, -gt, -le, -lt, -o, -a, -z, -n, -d, -e,
 		  -f, -L; see 'man test' on your PC for more information.
 
 config CMD_TRUE
diff --git a/commands/test.c b/commands/test.c
index 5478011185..b62ef2c65c 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -9,11 +9,13 @@
  */
 #include <common.h>
 #include <command.h>
+#include <fnmatch.h>
 #include <fs.h>
 #include <linux/stat.h>
 
 typedef enum {
 	OPT_EQUAL,
+	OPT_EQUAL_BASH,
 	OPT_NOT_EQUAL,
 	OPT_ARITH_EQUAL,
 	OPT_ARITH_NOT_EQUAL,
@@ -36,6 +38,7 @@ typedef enum {
 
 static char *test_options[] = {
 	[OPT_EQUAL]			= "=",
+	[OPT_EQUAL_BASH]		= "==",
 	[OPT_NOT_EQUAL]			= "!=",
 	[OPT_ARITH_EQUAL]		= "-eq",
 	[OPT_ARITH_NOT_EQUAL]		= "-ne",
@@ -67,18 +70,35 @@ static int parse_opt(const char *opt)
 	return -1;
 }
 
+static int string_comp(const char *left_op, const char *right_op, bool bash_test)
+{
+	if (bash_test)
+		return fnmatch(right_op, left_op, 0);
+
+	return strcmp(left_op, right_op);
+}
+
 static int do_test(int argc, char *argv[])
 {
 	char **ap;
 	int left, adv, expr, last_expr, neg, last_cmp, opt, zero;
 	ulong a, b;
 	struct stat statbuf;
+	bool bash_test = false;
 
 	if (*argv[0] == '[') {
 		argc--;
-		if (*argv[argc] != ']') {
-			pr_err("[: missing `]'\n");
-			return 1;
+		if (!strncmp(argv[0], "[[", 2)) {
+			if (strncmp(argv[argc], "]]", 2) != 0) {
+				pr_err("[[: missing `]]'\n");
+				return 1;
+			}
+			bash_test = true;
+		} else {
+			if (*argv[argc] != ']') {
+				pr_err("[: missing `]'\n");
+				return 1;
+			}
 		}
 	}
 
@@ -183,10 +203,11 @@ static int do_test(int argc, char *argv[])
 			b = simple_strtol(ap[2], NULL, 0);
 			switch (parse_opt(ap[1])) {
 			case OPT_EQUAL:
-				expr = strcmp(ap[0], ap[2]) == 0;
+			case OPT_EQUAL_BASH:
+				expr = string_comp(ap[0], ap[2], bash_test) == 0;
 				break;
 			case OPT_NOT_EQUAL:
-				expr = strcmp(ap[0], ap[2]) != 0;
+				expr = string_comp(ap[0], ap[2], bash_test) != 0;
 				break;
 			case OPT_ARITH_EQUAL:
 				expr = a == b;
@@ -233,7 +254,7 @@ static int do_test(int argc, char *argv[])
 	return expr;
 }
 
-static const char * const test_aliases[] = { "[", NULL};
+static const char * const test_aliases[] = { "[", "[[", NULL};
 
 BAREBOX_CMD_HELP_START(test)
 BAREBOX_CMD_HELP_TEXT("Options:")
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/3] commands: test: replace printf with pr_err
  2023-07-04 11:29 ` [PATCH v2 2/3] commands: test: replace printf with pr_err Marco Felsch
@ 2023-07-04 11:38   ` Sascha Hauer
  2023-07-04 11:59     ` Marco Felsch
  0 siblings, 1 reply; 5+ messages in thread
From: Sascha Hauer @ 2023-07-04 11:38 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Jul 04, 2023 at 01:29:20PM +0200, Marco Felsch wrote:
> Replace the printf() with pr_err() to support:
>  - color formated prints
>  - logbuffer storing (dmesg)
>  - loglevels
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> ---
> v2:
>  - new patch
> 
>  commands/test.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/commands/test.c b/commands/test.c
> index c1b84c42ef..5478011185 100644
> --- a/commands/test.c
> +++ b/commands/test.c
> @@ -77,7 +77,7 @@ static int do_test(int argc, char *argv[])
>  	if (*argv[0] == '[') {
>  		argc--;
>  		if (*argv[argc] != ']') {
> -			printf("[: missing `]'\n");
> +			pr_err("[: missing `]'\n");

Logging functions pr_* and dev_* should be used for core and driver
messages, commands should use printf. The line between logging functions
and commands is not always clear in barebox, but here it is.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/3] commands: test: replace printf with pr_err
  2023-07-04 11:38   ` Sascha Hauer
@ 2023-07-04 11:59     ` Marco Felsch
  0 siblings, 0 replies; 5+ messages in thread
From: Marco Felsch @ 2023-07-04 11:59 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 23-07-04, Sascha Hauer wrote:
> On Tue, Jul 04, 2023 at 01:29:20PM +0200, Marco Felsch wrote:
> > Replace the printf() with pr_err() to support:
> >  - color formated prints
> >  - logbuffer storing (dmesg)
> >  - loglevels
> > 
> > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> > ---
> > v2:
> >  - new patch
> > 
> >  commands/test.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/commands/test.c b/commands/test.c
> > index c1b84c42ef..5478011185 100644
> > --- a/commands/test.c
> > +++ b/commands/test.c
> > @@ -77,7 +77,7 @@ static int do_test(int argc, char *argv[])
> >  	if (*argv[0] == '[') {
> >  		argc--;
> >  		if (*argv[argc] != ']') {
> > -			printf("[: missing `]'\n");
> > +			pr_err("[: missing `]'\n");
> 
> Logging functions pr_* and dev_* should be used for core and driver
> messages, commands should use printf. The line between logging functions
> and commands is not always clear in barebox, but here it is.

Okay, thanks for clarification. I will drop the patch and send a v3.

Regards,
  Marco

> 
> Sascha
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-04 12:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-04 11:29 [PATCH v2 1/3] commands: test: simplify argv handling Marco Felsch
2023-07-04 11:29 ` [PATCH v2 2/3] commands: test: replace printf with pr_err Marco Felsch
2023-07-04 11:38   ` Sascha Hauer
2023-07-04 11:59     ` Marco Felsch
2023-07-04 11:29 ` [PATCH v2 3/3] commands: test: add based support for bash-test style Marco Felsch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox