public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Wolfgang Denk <wd@denx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 2/9] "env grep" - reimplement command using hexport_r()
Date: Fri, 22 Mar 2013 22:44:52 +0100	[thread overview]
Message-ID: <1363988699-6410-3-git-send-email-wd@denx.de> (raw)
In-Reply-To: <1363988699-6410-1-git-send-email-wd@denx.de>

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>
---
 common/cmd_nvedit.c | 30 ++++++++++---------------
 include/search.h    |  6 -----
 lib/hashtable.c     | 64 +++++++++++++++++++++++------------------------------
 3 files changed, 40 insertions(+), 60 deletions(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 5c84795..40d9f7a 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -162,31 +162,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 1d18d15..8ad56ec 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;
-- 
1.8.1.4

  parent reply	other threads:[~2013-03-22 21:44 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-26 16:37 [U-Boot] [PATCH v0 0/4] env: reworking + default/import individual vars Gerlando Falauto
2011-10-26 16:37 ` [U-Boot] [PATCH v0 1/4] Groundwork for generalization of env interface Gerlando Falauto
2011-11-05 16:09   ` Wolfgang Denk
2011-11-07 21:06     ` Gerlando Falauto
2011-11-07 22:05       ` Wolfgang Denk
2011-11-07 23:02         ` [U-Boot] Continuation line alignment Scott Wood
2011-11-07 23:32           ` Gerlando Falauto
2011-11-07 23:44             ` Scott Wood
2011-11-07 23:58             ` Scott Wood
2011-11-08 10:20               ` Gerlando Falauto
2011-11-07 23:05         ` [U-Boot] [PATCH v0 1/4] Groundwork for generalization of env interface Gerlando Falauto
2011-11-07 23:30           ` Wolfgang Denk
2011-11-05 16:34   ` Wolfgang Denk
2011-10-26 16:37 ` [U-Boot] [PATCH v0 2/4] env: check and apply changes on delete/destroy Gerlando Falauto
2011-11-05 16:13   ` Wolfgang Denk
2011-10-26 16:37 ` [U-Boot] [PATCH v0 3/4] env: implement selective "env default" Gerlando Falauto
2011-11-05 16:40   ` Wolfgang Denk
2011-10-26 16:37 ` [U-Boot] [PATCH v0 4/4] env: implement "env import -n var[, var...]" Gerlando Falauto
2011-11-06 19:52 ` [U-Boot] [PATCH] env: fix "env ask" command Wolfgang Denk
2011-11-06 19:55   ` [U-Boot] [PATCH 1/2] Add SLRE - Super Light Regular Expression library Wolfgang Denk
2011-11-06 19:55     ` [U-Boot] [PATCH 2/2] env: add regex support for environment variables Wolfgang Denk
2011-11-07 11:07       ` Detlev Zundel
2013-03-22 21:44     ` [U-Boot] [PATCH 0/9] Add Regular Expressions Support Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 1/9] hashtable: preparations to use hexport_r() for "env grep" Wolfgang Denk
2013-03-22 21:44       ` Wolfgang Denk [this message]
2013-03-22 21:44       ` [U-Boot] [PATCH 3/9] "env grep" - add options to grep in name, value, or both Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 4/9] Add SLRE - Super Light Regular Expression library Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 5/9] "env grep" - add support for regular expression matches Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 6/9] setexpr: simplify code, improve help message Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 7/9] setexpr: add regex substring matching and substitution Wolfgang Denk
2013-03-22 22:36         ` Marek Vasut
2013-03-24  9:38           ` Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 8/9] m28evk: enable "env grep" and regexp support Wolfgang Denk
2013-03-22 21:44       ` [U-Boot] [PATCH 9/9] amcc-common.h: enable support for "env grep", "setexpr", and regex Wolfgang Denk
2013-03-22 21:50       ` [U-Boot] [PATCH 0/9] Add Regular Expressions Support Wolfgang Denk
2013-03-23 12:18       ` Otavio Salvador
2013-03-24  9:41         ` Wolfgang Denk
2013-02-20 14:53   ` [U-Boot] [PATCH] env: fix "env ask" command Wolfgang Denk
2011-11-06 19:57 ` [U-Boot] [PATCH v0 0/4] env: reworking + default/import individual vars Wolfgang Denk
2011-11-06 22:15 ` Wolfgang Denk
2011-11-08  9:33   ` Gerlando Falauto
2011-11-08 11:46     ` Wolfgang Denk
2011-11-08 12:04       ` Gerlando Falauto
2011-11-08 12:47         ` Wolfgang Denk
2011-11-08 13:33           ` Holger Brunck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1363988699-6410-3-git-send-email-wd@denx.de \
    --to=wd@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox