From patchwork Fri Mar 22 21:44:52 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfgang Denk X-Patchwork-Id: 230275 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 97EBB2C00D3 for ; Sat, 23 Mar 2013 08:46:31 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9E4B74A2CC; Fri, 22 Mar 2013 22:46:13 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id YhLPImMILKfQ; Fri, 22 Mar 2013 22:46:13 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id EBB8C4A2AF; Fri, 22 Mar 2013 22:45:26 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 842944A258 for ; Fri, 22 Mar 2013 22:45:19 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VTcgM6xTF7DS for ; Fri, 22 Mar 2013 22:45:18 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-out.m-online.net (mail-out.m-online.net [212.18.0.10]) by theia.denx.de (Postfix) with ESMTPS id 2B28A4A26E for ; Fri, 22 Mar 2013 22:45:13 +0100 (CET) Received: from frontend1.mail.m-online.net (frontend1.mail.intern.m-online.net [192.168.8.180]) by mail-out.m-online.net (Postfix) with ESMTP id 3ZXdh93y0Lz3hhrg for ; Fri, 22 Mar 2013 22:45:13 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 3ZXdh93sJ8zbdXB for ; Fri, 22 Mar 2013 22:45:13 +0100 (CET) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from smtp-auth.mnet-online.de ([192.168.8.180]) by localhost (dynscan1.mail.m-online.net [192.168.6.68]) (amavisd-new, port 10024) with ESMTP id UaigzPDLtEnK for ; Fri, 22 Mar 2013 22:45:10 +0100 (CET) X-Auth-Info: JdcwpYP6f9DhuvUDTcoiwvCeoPxbFyoeEj099VgrqHk= Received: from diddl.denx.de (host-80-81-18-216.customer.m-online.net [80.81.18.216]) by smtp-auth.mnet-online.de (Postfix) with ESMTPA for ; Fri, 22 Mar 2013 22:45:10 +0100 (CET) Received: from gemini.denx.de (unknown [10.0.0.2]) by diddl.denx.de (Postfix) with ESMTP id A10541A761B for ; Fri, 22 Mar 2013 22:45:09 +0100 (CET) Received: by gemini.denx.de (Postfix, from userid 500) id AC7002014D0; Fri, 22 Mar 2013 22:45:08 +0100 (CET) From: Wolfgang Denk To: u-boot@lists.denx.de Date: Fri, 22 Mar 2013 22:44:52 +0100 Message-Id: <1363988699-6410-3-git-send-email-wd@denx.de> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1363988699-6410-1-git-send-email-wd@denx.de> References: <1320609172-3157-1-git-send-email-wd@denx.de>, <1320609326-3259-1-git-send-email-wd@denx.de> <1363988699-6410-1-git-send-email-wd@denx.de> Subject: [U-Boot] [PATCH 2/9] "env grep" - reimplement command using hexport_r() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Also drop hstrstr_r() which is not needed any more. The new code is way more flexible. Signed-off-by: Wolfgang Denk --- 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;