summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfgang Denk <wd@denx.de>2013-03-23 23:50:29 +0000
committerTom Rini <trini@ti.com>2013-05-01 16:24:00 -0400
commit5a31ea04c9ee5544fbb70ad7597ea4b294840eab (patch)
treea6a8c141675e4a4f712aa05b9d2aa5792a0309dd
parentea009d4743ab4b801703982086e053e74266ff4c (diff)
downloadu-boot-imx-5a31ea04c9ee5544fbb70ad7597ea4b294840eab.zip
u-boot-imx-5a31ea04c9ee5544fbb70ad7597ea4b294840eab.tar.gz
u-boot-imx-5a31ea04c9ee5544fbb70ad7597ea4b294840eab.tar.bz2
"env grep" - reimplement command using hexport_r()
Also drop hstrstr_r() which is not needed any more. The new code is way more flexible. Signed-off-by: Wolfgang Denk <wd@denx.de>
-rw-r--r--common/cmd_nvedit.c30
-rw-r--r--include/search.h6
-rw-r--r--lib/hashtable.c64
3 files changed, 40 insertions, 60 deletions
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 49b9d74..a4b71f8 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -164,31 +164,25 @@ static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
int argc, char * const argv[])
{
- ENTRY *match;
- unsigned char matched[env_htab.size / 8];
- int rcode = 1, arg = 1, idx;
+ char *res = NULL;
+ int len;
if (argc < 2)
return CMD_RET_USAGE;
- memset(matched, 0, env_htab.size / 8);
+ len = hexport_r(&env_htab, '\n',
+ flag | H_MATCH_BOTH | H_MATCH_SUBSTR,
+ &res, 0, argc, argv);
- while (arg <= argc) {
- idx = 0;
- while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
- if (!(matched[idx / 8] & (1 << (idx & 7)))) {
- puts(match->key);
- puts("=");
- puts(match->data);
- puts("\n");
- }
- matched[idx / 8] |= 1 << (idx & 7);
- rcode = 0;
- }
- arg++;
+ if (len > 0) {
+ puts(res);
+ free(res);
}
- return rcode;
+ if (len < 2)
+ return 1;
+
+ return 0;
}
#endif
#endif /* CONFIG_SPL_BUILD */
diff --git a/include/search.h b/include/search.h
index 9d9abd6..d06a201 100644
--- a/include/search.h
+++ b/include/search.h
@@ -98,12 +98,6 @@ extern int hsearch_r(ENTRY __item, ACTION __action, ENTRY ** __retval,
*/
extern int hmatch_r(const char *__match, int __last_idx, ENTRY ** __retval,
struct hsearch_data *__htab);
-/*
- * Search for an entry whose key or data contains `MATCH'. Otherwise,
- * Same semantics as hsearch_r().
- */
-extern int hstrstr_r(const char *__match, int __last_idx, ENTRY ** __retval,
- struct hsearch_data *__htab);
/* Search and delete entry matching ITEM.key in internal hash table. */
extern int hdelete_r(const char *__key, struct hsearch_data *__htab,
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 305824b..1703941 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -210,29 +210,6 @@ void hdestroy_r(struct hsearch_data *htab)
* example for functions like hdelete().
*/
-/*
- * hstrstr_r - return index to entry whose key and/or data contains match
- */
-int hstrstr_r(const char *match, int last_idx, ENTRY ** retval,
- struct hsearch_data *htab)
-{
- unsigned int idx;
-
- for (idx = last_idx + 1; idx < htab->size; ++idx) {
- if (htab->table[idx].used <= 0)
- continue;
- if (strstr(htab->table[idx].entry.key, match) ||
- strstr(htab->table[idx].entry.data, match)) {
- *retval = &htab->table[idx].entry;
- return idx;
- }
- }
-
- __set_errno(ESRCH);
- *retval = NULL;
- return 0;
-}
-
int hmatch_r(const char *match, int last_idx, ENTRY ** retval,
struct hsearch_data *htab)
{
@@ -563,23 +540,38 @@ static int cmpkey(const void *p1, const void *p2)
return (strcmp(e1->key, e2->key));
}
-static int match_strings(ENTRY *ep, int flag,
+static int match_string(int flag, const char *str, const char *pat)
+{
+ switch (flag & H_MATCH_METHOD) {
+ case H_MATCH_IDENT:
+ if (strcmp(str, pat) == 0)
+ return 1;
+ break;
+ case H_MATCH_SUBSTR:
+ if (strstr(str, pat))
+ return 1;
+ break;
+ default:
+ printf("## ERROR: unsupported match method: 0x%02x\n",
+ flag & H_MATCH_METHOD);
+ break;
+ }
+ return 0;
+}
+
+static int match_entry(ENTRY *ep, int flag,
int argc, char * const argv[])
{
int arg;
- for (arg = 0; arg < argc; ++arg) {
+ for (arg = 1; arg < argc; ++arg) {
if (flag & H_MATCH_KEY) {
- switch (flag & H_MATCH_METHOD) {
- case H_MATCH_IDENT:
- if (strcmp(argv[arg], ep->key) == 0)
- return 1;
- break;
- default:
- printf("## ERROR: unsupported match method: 0x%02x\n",
- flag & H_MATCH_METHOD);
- break;
- }
+ if (match_string(flag, ep->key, argv[arg]))
+ return 1;
+ }
+ if (flag & H_MATCH_DATA) {
+ if (match_string(flag, ep->data, argv[arg]))
+ return 1;
}
}
return 0;
@@ -611,7 +603,7 @@ ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag,
if (htab->table[i].used > 0) {
ENTRY *ep = &htab->table[i].entry;
- int found = match_strings(ep, flag, argc, argv);
+ int found = match_entry(ep, flag, argc, argv);
if ((argc > 0) && (found == 0))
continue;