From: Marco Felsch <m.felsch@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v3 2/2] commands: test: add based support for bash-test style
Date: Tue, 4 Jul 2023 14:16:26 +0200 [thread overview]
Message-ID: <20230704121626.835344-2-m.felsch@pengutronix.de> (raw)
In-Reply-To: <20230704121626.835344-1-m.felsch@pengutronix.de>
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>
---
v3:
- replace pr_* with printf
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 c1b84c42ef..1130bf2e5b 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] != ']') {
- printf("[: missing `]'\n");
- return 1;
+ if (!strncmp(argv[0], "[[", 2)) {
+ if (strncmp(argv[argc], "]]", 2) != 0) {
+ printf("[[: missing `]]'\n");
+ return 1;
+ }
+ bash_test = true;
+ } else {
+ if (*argv[argc] != ']') {
+ printf("[: 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
next prev parent reply other threads:[~2023-07-04 12:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-04 12:16 [PATCH v3 1/2] commands: test: simplify argv handling Marco Felsch
2023-07-04 12:16 ` Marco Felsch [this message]
2023-07-28 6:41 ` Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230704121626.835344-2-m.felsch@pengutronix.de \
--to=m.felsch@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox