diff options
| author | Wolfgang Denk <wd@denx.de> | 2013-03-23 23:50:32 +0000 | 
|---|---|---|
| committer | Tom Rini <trini@ti.com> | 2013-05-01 16:24:00 -0400 | 
| commit | be29df6a1ac286e6c482828db28ca96e187c7e00 (patch) | |
| tree | db89b8eb8ae6bb1baa93e4d778f82cfcd71580be /lib/hashtable.c | |
| parent | a5ecbe62c25c7c9d6ddd0c9eb4d5ec3350642614 (diff) | |
| download | olio-uboot-2014.01-be29df6a1ac286e6c482828db28ca96e187c7e00.tar.xz olio-uboot-2014.01-be29df6a1ac286e6c482828db28ca96e187c7e00.zip | |
"env grep" - add support for regular expression matches
When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used.  This allows for
example things like these:
- print all MAC addresses:
	=> env grep -e eth.*addr
	eth1addr=00:10:ec:80:c5:15
	ethaddr=00:10:ec:00:c5:15
- print all variables that have at least 2 colons in their value:
	=> env grep -v -e :.*:
	addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
	panic=1
	eth1addr=00:10:ec:80:c5:15
	ethaddr=00:10:ec:00:c5:15
	ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)
etc.
Signed-off-by: Wolfgang Denk <wd@denx.de>
Diffstat (limited to 'lib/hashtable.c')
| -rw-r--r-- | lib/hashtable.c | 29 | 
1 files changed, 26 insertions, 3 deletions
| diff --git a/lib/hashtable.c b/lib/hashtable.c index 1703941a5..6050dd082 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;  		}  	} |