]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
hashtable: preparations to use hexport_r() for "env grep"
authorWolfgang Denk <wd@denx.de>
Sat, 23 Mar 2013 23:50:28 +0000 (23:50 +0000)
committerTom Rini <trini@ti.com>
Wed, 1 May 2013 20:24:00 +0000 (16:24 -0400)
The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table.  We have all the
needed code in place in hexport_r() alsready, so let's use this
instead.  Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>
common/cmd_nvedit.c
include/search.h
lib/hashtable.c

index d893aa1854624899cef4eb3e274cc7d78c08253a..49b9d74a8cc37f2727c7a45d20880d3805724287 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (C) Copyright 2000-2010
+ * (C) Copyright 2000-2013
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
@@ -892,7 +892,9 @@ NXTARG:             ;
        argv++;
 
        if (sep) {              /* export as text file */
-               len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
+               len = hexport_r(&env_htab, sep,
+                               H_MATCH_KEY | H_MATCH_IDENT,
+                               &addr, size, argc, argv);
                if (len < 0) {
                        error("Cannot export environment: errno = %d\n", errno);
                        return 1;
@@ -910,7 +912,9 @@ NXTARG:             ;
        else                    /* export as raw binary data */
                res = addr;
 
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
+       len = hexport_r(&env_htab, '\0',
+                       H_MATCH_KEY | H_MATCH_IDENT,
+                       &res, ENV_SIZE, argc, argv);
        if (len < 0) {
                error("Cannot export environment: errno = %d\n", errno);
                return 1;
index 13d3be6291701a3170dbd3a15e873b6cc7378c82..9d9abd6403870212e849fc5e1a4db958789f7011 100644 (file)
@@ -22,7 +22,7 @@
 /*
  * Based on code from uClibc-0.9.30.3
  * Extensions for use within U-Boot
- * Copyright (C) 2010 Wolfgang Denk <wd@denx.de>
+ * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de>
  */
 
 #ifndef _SEARCH_H
@@ -131,5 +131,11 @@ extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
 #define H_FORCE                (1 << 1) /* overwrite read-only/write-once variables */
 #define H_INTERACTIVE  (1 << 2) /* indicate that an import is user directed */
 #define H_HIDE_DOT     (1 << 3) /* don't print env vars that begin with '.' */
+#define H_MATCH_KEY    (1 << 4) /* search/grep key  = variable names        */
+#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)
 
 #endif /* search.h */
index 07ebfb218f8a6f152d915e448a2c2304a53b9f02..305824b6eada591957171e5d321cffd359f24505 100644 (file)
@@ -2,7 +2,7 @@
  * This implementation is based on code from uClibc-0.9.30.3 but was
  * modified and extended for use within U-Boot.
  *
- * Copyright (C) 2010 Wolfgang Denk <wd@denx.de>
+ * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de>
  *
  * Original license header:
  *
@@ -563,6 +563,28 @@ static int cmpkey(const void *p1, const void *p2)
        return (strcmp(e1->key, e2->key));
 }
 
+static int match_strings(ENTRY *ep, int flag,
+                int argc, char * const argv[])
+{
+       int arg;
+
+       for (arg = 0; 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;
+                       }
+               }
+       }
+       return 0;
+}
+
 ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag,
                 char **resp, size_t size,
                 int argc, char * const argv[])
@@ -589,14 +611,8 @@ 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 arg, found = 0;
+                       int found = match_strings(ep, flag, argc, argv);
 
-                       for (arg = 0; arg < argc; ++arg) {
-                               if (strcmp(argv[arg], ep->key) == 0) {
-                                       found = 1;
-                                       break;
-                               }
-                       }
                        if ((argc > 0) && (found == 0))
                                continue;