[PATCH 2/2] commands: test: add based support for bash-test style

Marco Felsch m.felsch at pengutronix.de
Mon Jul 3 15:58:13 PDT 2023


Add [[ expression ]] support which allow pattern matching like:

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

Signed-off-by: Marco Felsch <m.felsch at pengutronix.de>
---
 commands/Kconfig | 13 +++++++++++++
 commands/test.c  | 39 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index 4d3ff631a8..615e96aa9d 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1219,6 +1219,19 @@ config CMD_TEST
 		  !, =, !=, -eq, -ne, -ge, -gt, -le, -lt, -o, -a, -z, -n, -d, -e,
 		  -f, -L; see 'man test' on your PC for more information.
 
+if CMD_TEST
+
+config CMD_TEST_BASH_COMP
+	tristate
+	select CONFIG_FNMATCH
+	prompt "test bash-compatibility"
+	help
+	  Enable minimal support for bash compatible tests: [[ ]] to allow
+	  pattern matching via: == and !=
+
+# end CMD_TEST
+endif
+
 config CMD_TRUE
 	tristate
 	default y
diff --git a/commands/test.c b/commands/test.c
index c1b84c42ef..77e15aeb03 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -11,9 +11,13 @@
 #include <command.h>
 #include <fs.h>
 #include <linux/stat.h>
+#ifdef CONFIG_CMD_TEST_BASH_COMP
+#include <fnmatch.h>
+#endif
 
 typedef enum {
 	OPT_EQUAL,
+	OPT_EQUAL_BASH,
 	OPT_NOT_EQUAL,
 	OPT_ARITH_EQUAL,
 	OPT_ARITH_NOT_EQUAL,
@@ -36,6 +40,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 +72,37 @@ static int parse_opt(const char *opt)
 	return -1;
 }
 
+static int string_comp(const char *left_op, const char *right_op, bool bash_test)
+{
+#ifdef CONFIG_CMD_TEST_BASH_COMP
+	if (bash_test)
+		return fnmatch(right_op, left_op, 0);
+#endif
+	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] != ']') {
-			printf("[: missing `]'\n");
-			return 1;
+		if (!strncmp(argv[0], "[[", 2)) {
+			if (strncmp(argv[argc], "]]", 2) != 0) {
+				printf("[[: missing `]]'\n");
+				return 1;
+			}
+			if (IS_ENABLED(CONFIG_CMD_TEST_BASH_COMP))
+				bash_test = true;
+		} else {
+			if (*argv[argc] != ']') {
+				printf("[: missing `]'\n");
+				return 1;
+			}
 		}
 	}
 
@@ -183,10 +207,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 +258,11 @@ static int do_test(int argc, char *argv[])
 	return expr;
 }
 
+#ifdef CONFIG_CMD_TEST_BASH_COMP
+static const char * const test_aliases[] = { "[", "[[", NULL};
+#else
 static const char * const test_aliases[] = { "[", NULL};
+#endif
 
 BAREBOX_CMD_HELP_START(test)
 BAREBOX_CMD_HELP_TEXT("Options:")
-- 
2.39.2




More information about the barebox mailing list