Merge patch series "regex patches"

Rasmus Villemoes <ravi@prevas.dk> says:

This started as a rather simple patch, 1/12, adding the ability to
more conveniently do regex matching in shell.

But with that, it became very easy to see what the slre library can
and especially what it cannot do, and that way I found both outright
bugs and a "wow, doesn't it support that syntax" gotcha. I couldn't
find any tests ('git grep slre -- test/' was empty), so I added a
small test suite and tweaked slre.c.

Link: https://lore.kernel.org/r/20250513084034.654865-1-ravi@prevas.dk
This commit is contained in:
Tom Rini
2025-05-29 08:27:13 -06:00
9 changed files with 244 additions and 28 deletions

View File

@@ -7,6 +7,7 @@
#include <command.h>
#include <fs.h>
#include <log.h>
#include <slre.h>
#include <vsprintf.h>
#define OP_INVALID 0
@@ -26,6 +27,7 @@
#define OP_INT_GT 14
#define OP_INT_GE 15
#define OP_FILE_EXISTS 16
#define OP_REGEX 17
const struct {
int arg;
@@ -49,6 +51,9 @@ const struct {
{0, "-z", OP_STR_EMPTY, 2},
{0, "-n", OP_STR_NEMPTY, 2},
{0, "-e", OP_FILE_EXISTS, 4},
#ifdef CONFIG_REGEX
{1, "=~", OP_REGEX, 3},
#endif
};
static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
@@ -141,6 +146,20 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
case OP_FILE_EXISTS:
expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
break;
#ifdef CONFIG_REGEX
case OP_REGEX: {
struct slre slre;
if (slre_compile(&slre, ap[2]) == 0) {
printf("Error compiling regex: %s\n", slre.err_str);
expr = 0;
break;
}
expr = slre_match(&slre, ap[0], strlen(ap[0]), NULL);
break;
}
#endif
}
switch (op) {