Commit be29df6a1ac286e6c482828db28ca96e187c7e00
Committed by
Tom Rini
1 parent
a5ecbe62c2
Exists in
master
and in
53 other branches
"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>
Showing 3 changed files with 52 additions and 11 deletions Side-by-side Diff
common/cmd_nvedit.c
... | ... | @@ -165,12 +165,13 @@ |
165 | 165 | int argc, char * const argv[]) |
166 | 166 | { |
167 | 167 | char *res = NULL; |
168 | - int len, grep_flags; | |
168 | + int len, grep_how, grep_what; | |
169 | 169 | |
170 | 170 | if (argc < 2) |
171 | 171 | return CMD_RET_USAGE; |
172 | 172 | |
173 | - grep_flags = H_MATCH_BOTH; | |
173 | + grep_how = H_MATCH_SUBSTR; /* default: substring search */ | |
174 | + grep_what = H_MATCH_BOTH; /* default: grep names and values */ | |
174 | 175 | |
175 | 176 | while (argc > 1 && **(argv + 1) == '-') { |
176 | 177 | char *arg = *++argv; |
177 | 178 | |
178 | 179 | |
179 | 180 | |
... | ... | @@ -178,14 +179,19 @@ |
178 | 179 | --argc; |
179 | 180 | while (*++arg) { |
180 | 181 | switch (*arg) { |
182 | +#ifdef CONFIG_REGEX | |
183 | + case 'e': /* use regex matching */ | |
184 | + grep_how = H_MATCH_REGEX; | |
185 | + break; | |
186 | +#endif | |
181 | 187 | case 'n': /* grep for name */ |
182 | - grep_flags = H_MATCH_KEY; | |
188 | + grep_what = H_MATCH_KEY; | |
183 | 189 | break; |
184 | 190 | case 'v': /* grep for value */ |
185 | - grep_flags = H_MATCH_DATA; | |
191 | + grep_what = H_MATCH_DATA; | |
186 | 192 | break; |
187 | 193 | case 'b': /* grep for both */ |
188 | - grep_flags = H_MATCH_BOTH; | |
194 | + grep_what = H_MATCH_BOTH; | |
189 | 195 | break; |
190 | 196 | case '-': |
191 | 197 | goto DONE; |
... | ... | @@ -197,7 +203,7 @@ |
197 | 203 | |
198 | 204 | DONE: |
199 | 205 | len = hexport_r(&env_htab, '\n', |
200 | - flag | grep_flags | H_MATCH_SUBSTR, | |
206 | + flag | grep_what | grep_how, | |
201 | 207 | &res, 0, argc, argv); |
202 | 208 | |
203 | 209 | if (len > 0) { |
204 | 210 | |
... | ... | @@ -1153,8 +1159,12 @@ |
1153 | 1159 | "env flags - print variables that have non-default flags\n" |
1154 | 1160 | #endif |
1155 | 1161 | #if defined(CONFIG_CMD_GREPENV) |
1162 | +#ifdef CONFIG_REGEX | |
1163 | + "env grep [-e] [-n | -v | -b] string [...] - search environment\n" | |
1164 | +#else | |
1156 | 1165 | "env grep [-n | -v | -b] string [...] - search environment\n" |
1157 | 1166 | #endif |
1167 | +#endif | |
1158 | 1168 | #if defined(CONFIG_CMD_IMPORTENV) |
1159 | 1169 | "env import [-d] [-t | -b | -c] addr [size] - import environment\n" |
1160 | 1170 | #endif |
1161 | 1171 | |
1162 | 1172 | |
... | ... | @@ -1200,8 +1210,15 @@ |
1200 | 1210 | U_BOOT_CMD_COMPLETE( |
1201 | 1211 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, |
1202 | 1212 | "search environment variables", |
1213 | +#ifdef CONFIG_REGEX | |
1214 | + "[-e] [-n | -v | -b] string ...\n" | |
1215 | +#else | |
1203 | 1216 | "[-n | -v | -b] string ...\n" |
1217 | +#endif | |
1204 | 1218 | " - list environment name=value pairs matching 'string'\n" |
1219 | +#ifdef CONFIG_REGEX | |
1220 | + " \"-e\": enable regular expressions;\n" | |
1221 | +#endif | |
1205 | 1222 | " \"-n\": search variable names; \"-v\": search values;\n" |
1206 | 1223 | " \"-b\": search both names and values (default)", |
1207 | 1224 | var_complete |
include/search.h
... | ... | @@ -129,8 +129,9 @@ |
129 | 129 | #define H_MATCH_DATA (1 << 5) /* search/grep data = variable values */ |
130 | 130 | #define H_MATCH_BOTH (H_MATCH_KEY | H_MATCH_DATA) /* search/grep both */ |
131 | 131 | #define H_MATCH_IDENT (1 << 6) /* search for indentical strings */ |
132 | -#define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */ | |
133 | -#define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR) | |
132 | +#define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */ | |
133 | +#define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */ | |
134 | +#define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX) | |
134 | 135 | |
135 | 136 | #endif /* search.h */ |
lib/hashtable.c
... | ... | @@ -57,6 +57,7 @@ |
57 | 57 | #include <env_callback.h> |
58 | 58 | #include <env_flags.h> |
59 | 59 | #include <search.h> |
60 | +#include <slre.h> | |
60 | 61 | |
61 | 62 | /* |
62 | 63 | * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986 |
... | ... | @@ -540,7 +541,7 @@ |
540 | 541 | return (strcmp(e1->key, e2->key)); |
541 | 542 | } |
542 | 543 | |
543 | -static int match_string(int flag, const char *str, const char *pat) | |
544 | +static int match_string(int flag, const char *str, const char *pat, void *priv) | |
544 | 545 | { |
545 | 546 | switch (flag & H_MATCH_METHOD) { |
546 | 547 | case H_MATCH_IDENT: |
... | ... | @@ -551,6 +552,17 @@ |
551 | 552 | if (strstr(str, pat)) |
552 | 553 | return 1; |
553 | 554 | break; |
555 | +#ifdef CONFIG_REGEX | |
556 | + case H_MATCH_REGEX: | |
557 | + { | |
558 | + struct slre *slrep = (struct slre *)priv; | |
559 | + struct cap caps[slrep->num_caps + 2]; | |
560 | + | |
561 | + if (slre_match(slrep, str, strlen(str), caps)) | |
562 | + return 1; | |
563 | + } | |
564 | + break; | |
565 | +#endif | |
554 | 566 | default: |
555 | 567 | printf("## ERROR: unsupported match method: 0x%02x\n", |
556 | 568 | flag & H_MATCH_METHOD); |
557 | 569 | |
558 | 570 | |
559 | 571 | |
... | ... | @@ -563,14 +575,25 @@ |
563 | 575 | int argc, char * const argv[]) |
564 | 576 | { |
565 | 577 | int arg; |
578 | + void *priv = NULL; | |
566 | 579 | |
567 | 580 | for (arg = 1; arg < argc; ++arg) { |
581 | +#ifdef CONFIG_REGEX | |
582 | + struct slre slre; | |
583 | + | |
584 | + if (slre_compile(&slre, argv[arg]) == 0) { | |
585 | + printf("Error compiling regex: %s\n", slre.err_str); | |
586 | + return 0; | |
587 | + } | |
588 | + | |
589 | + priv = (void *)&slre; | |
590 | +#endif | |
568 | 591 | if (flag & H_MATCH_KEY) { |
569 | - if (match_string(flag, ep->key, argv[arg])) | |
592 | + if (match_string(flag, ep->key, argv[arg], priv)) | |
570 | 593 | return 1; |
571 | 594 | } |
572 | 595 | if (flag & H_MATCH_DATA) { |
573 | - if (match_string(flag, ep->data, argv[arg])) | |
596 | + if (match_string(flag, ep->data, argv[arg], priv)) | |
574 | 597 | return 1; |
575 | 598 | } |
576 | 599 | } |