summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/cmd_nvedit.c29
-rw-r--r--include/search.h5
-rw-r--r--lib/hashtable.c29
3 files changed, 52 insertions, 11 deletions
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 9158b96224..f8dc38e89a 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -165,12 +165,13 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
int argc, char * const argv[])
{
char *res = NULL;
- int len, grep_flags;
+ int len, grep_how, grep_what;
if (argc < 2)
return CMD_RET_USAGE;
- grep_flags = H_MATCH_BOTH;
+ grep_how = H_MATCH_SUBSTR; /* default: substring search */
+ grep_what = H_MATCH_BOTH; /* default: grep names and values */
while (argc > 1 && **(argv + 1) == '-') {
char *arg = *++argv;
@@ -178,14 +179,19 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
--argc;
while (*++arg) {
switch (*arg) {
+#ifdef CONFIG_REGEX
+ case 'e': /* use regex matching */
+ grep_how = H_MATCH_REGEX;
+ break;
+#endif
case 'n': /* grep for name */
- grep_flags = H_MATCH_KEY;
+ grep_what = H_MATCH_KEY;
break;
case 'v': /* grep for value */
- grep_flags = H_MATCH_DATA;
+ grep_what = H_MATCH_DATA;
break;
case 'b': /* grep for both */
- grep_flags = H_MATCH_BOTH;
+ grep_what = H_MATCH_BOTH;
break;
case '-':
goto DONE;
@@ -197,7 +203,7 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
DONE:
len = hexport_r(&env_htab, '\n',
- flag | grep_flags | H_MATCH_SUBSTR,
+ flag | grep_what | grep_how,
&res, 0, argc, argv);
if (len > 0) {
@@ -1153,8 +1159,12 @@ static char env_help_text[] =
"env flags - print variables that have non-default flags\n"
#endif
#if defined(CONFIG_CMD_GREPENV)
+#ifdef CONFIG_REGEX
+ "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
+#else
"env grep [-n | -v | -b] string [...] - search environment\n"
#endif
+#endif
#if defined(CONFIG_CMD_IMPORTENV)
"env import [-d] [-t | -b | -c] addr [size] - import environment\n"
#endif
@@ -1200,8 +1210,15 @@ U_BOOT_CMD_COMPLETE(
U_BOOT_CMD_COMPLETE(
grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
"search environment variables",
+#ifdef CONFIG_REGEX
+ "[-e] [-n | -v | -b] string ...\n"
+#else
"[-n | -v | -b] string ...\n"
+#endif
" - list environment name=value pairs matching 'string'\n"
+#ifdef CONFIG_REGEX
+ " \"-e\": enable regular expressions;\n"
+#endif
" \"-n\": search variable names; \"-v\": search values;\n"
" \"-b\": search both names and values (default)",
var_complete
diff --git a/include/search.h b/include/search.h
index d06a2017a5..d9ac8dfa07 100644
--- a/include/search.h
+++ b/include/search.h
@@ -129,7 +129,8 @@ extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
#define H_MATCH_DATA (1 << 5) /* search/grep data = variable values */
#define H_MATCH_BOTH (H_MATCH_KEY | H_MATCH_DATA) /* search/grep both */
#define H_MATCH_IDENT (1 << 6) /* search for indentical strings */
-#define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */
-#define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR)
+#define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */
+#define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */
+#define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX)
#endif /* search.h */
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 1703941a5a..6050dd0829 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -57,6 +57,7 @@
#include <env_callback.h>
#include <env_flags.h>
#include <search.h>
+#include <slre.h>
/*
* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
@@ -540,7 +541,7 @@ static int cmpkey(const void *p1, const void *p2)
return (strcmp(e1->key, e2->key));
}
-static int match_string(int flag, const char *str, const char *pat)
+static int match_string(int flag, const char *str, const char *pat, void *priv)
{
switch (flag & H_MATCH_METHOD) {
case H_MATCH_IDENT:
@@ -551,6 +552,17 @@ static int match_string(int flag, const char *str, const char *pat)
if (strstr(str, pat))
return 1;
break;
+#ifdef CONFIG_REGEX
+ case H_MATCH_REGEX:
+ {
+ struct slre *slrep = (struct slre *)priv;
+ struct cap caps[slrep->num_caps + 2];
+
+ if (slre_match(slrep, str, strlen(str), caps))
+ return 1;
+ }
+ break;
+#endif
default:
printf("## ERROR: unsupported match method: 0x%02x\n",
flag & H_MATCH_METHOD);
@@ -563,14 +575,25 @@ static int match_entry(ENTRY *ep, int flag,
int argc, char * const argv[])
{
int arg;
+ void *priv = NULL;
for (arg = 1; arg < argc; ++arg) {
+#ifdef CONFIG_REGEX
+ struct slre slre;
+
+ if (slre_compile(&slre, argv[arg]) == 0) {
+ printf("Error compiling regex: %s\n", slre.err_str);
+ return 0;
+ }
+
+ priv = (void *)&slre;
+#endif
if (flag & H_MATCH_KEY) {
- if (match_string(flag, ep->key, argv[arg]))
+ if (match_string(flag, ep->key, argv[arg], priv))
return 1;
}
if (flag & H_MATCH_DATA) {
- if (match_string(flag, ep->data, argv[arg]))
+ if (match_string(flag, ep->data, argv[arg], priv))
return 1;
}
}