From: Sascha Hauer <sha@pengutronix.de>
To: Marco Felsch <m.felsch@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 2/2] commands: test: add based support for bash-test style
Date: Tue, 4 Jul 2023 10:51:19 +0200 [thread overview]
Message-ID: <20230704085119.GX18491@pengutronix.de> (raw)
In-Reply-To: <20230703225813.2828815-2-m.felsch@pengutronix.de>
On Tue, Jul 04, 2023 at 12:58:13AM +0200, Marco Felsch wrote:
> Add [[ expression ]] support which allow pattern matching like:
>
> - [[ "foo1" == "foo*" ]]
It's a bit unfortunate that in barebox we require putting the pattern in
"", whereas in bash it only works without the "".
I guess there's not much we can do about this unless we turn the '['
command into a shell builtin.
> - [[ "foo" != "bar" ]]
>
> Signed-off-by: Marco Felsch <m.felsch@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
No need to include this conditionally.
>
> 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;
The if() is unnecessary.
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 |
prev parent reply other threads:[~2023-07-04 8:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-03 22:58 [PATCH 1/2] commands: test: simplify argv handling Marco Felsch
2023-07-03 22:58 ` [PATCH 2/2] commands: test: add based support for bash-test style Marco Felsch
2023-07-04 7:33 ` Ahmad Fatoum
2023-07-04 8:32 ` Marco Felsch
2023-07-04 8:46 ` Sascha Hauer
2023-07-04 10:07 ` Marco Felsch
2023-07-04 8:51 ` Sascha Hauer [this message]
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=20230704085119.GX18491@pengutronix.de \
--to=sha@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=m.felsch@pengutronix.de \
/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