From patchwork Fri Mar 22 21:44:55 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfgang Denk X-Patchwork-Id: 230273 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 2307C2C00CF for ; Sat, 23 Mar 2013 08:46:15 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B09C94A2A0; Fri, 22 Mar 2013 22:46:00 +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 T6tTLv1uPItE; Fri, 22 Mar 2013 22:46:00 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 25AE74A2A1; Fri, 22 Mar 2013 22:45:25 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BE7484A243 for ; Fri, 22 Mar 2013 22:45:18 +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 R2hwdCFM6rxq for ; Fri, 22 Mar 2013 22:45:17 +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.9]) by theia.denx.de (Postfix) with ESMTPS id 07CCD4A258 for ; Fri, 22 Mar 2013 22:45:13 +0100 (CET) Received: from frontend1.mail.m-online.net (unknown [192.168.8.180]) by mail-out.m-online.net (Postfix) with ESMTP id 3ZXdh91kJJz4KK64 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 3ZXdh91dkZzbdXB 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 PWS_afrc5t1R for ; Fri, 22 Mar 2013 22:45:11 +0100 (CET) X-Auth-Info: PQu9WrkgBolmosZnOJIhqWKZo+ZKiEphv9BdCoSQ7FA= 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:11 +0100 (CET) Received: from gemini.denx.de (unknown [10.0.0.2]) by diddl.denx.de (Postfix) with ESMTP id B65501A761E for ; Fri, 22 Mar 2013 22:45:09 +0100 (CET) Received: by gemini.denx.de (Postfix, from userid 500) id E751B2014D5; 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:55 +0100 Message-Id: <1363988699-6410-6-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 5/9] "env grep" - add support for regular expression matches 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 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 --- common/cmd_nvedit.c | 29 +++++++++++++++++++++++------ include/search.h | 5 +++-- lib/hashtable.c | 29 ++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c index 441bf40..aecc803 100644 --- a/common/cmd_nvedit.c +++ b/common/cmd_nvedit.c @@ -163,12 +163,13 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { char *res = NULL; - int len, grep_flags; + int len, grep_how, grep_what; if (argc < 2) return CMD_RET_USAGE; - grep_flags = H_MATCH_BOTH; + grep_how = H_MATCH_SUBSTR; /* default: substring search */ + grep_what = H_MATCH_BOTH; /* default: grep names and values */ while (argc > 1 && **(argv + 1) == '-') { char *arg = *++argv; @@ -176,14 +177,19 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag, --argc; while (*++arg) { switch (*arg) { +#ifdef CONFIG_REGEX + case 'e': /* use regex matching */ + grep_how = H_MATCH_REGEX; + break; +#endif case 'n': /* grep for name */ - grep_flags = H_MATCH_KEY; + grep_what = H_MATCH_KEY; break; case 'v': /* grep for value */ - grep_flags = H_MATCH_DATA; + grep_what = H_MATCH_DATA; break; case 'b': /* grep for both */ - grep_flags = H_MATCH_BOTH; + grep_what = H_MATCH_BOTH; break; case '-': goto DONE; @@ -195,7 +201,7 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag, DONE: len = hexport_r(&env_htab, '\n', - flag | grep_flags | H_MATCH_SUBSTR, + flag | grep_what | grep_how, &res, 0, argc, argv); if (len > 0) { @@ -1132,8 +1138,12 @@ static char env_help_text[] = "env flags - print variables that have non-default flags\n" #endif #if defined(CONFIG_CMD_GREPENV) +#ifdef CONFIG_REGEX + "env grep [-e] [-n | -v | -b] string [...] - search environment\n" +#else "env grep [-n | -v | -b] string [...] - search environment\n" #endif +#endif #if defined(CONFIG_CMD_IMPORTENV) "env import [-d] [-t | -b | -c] addr [size] - import environment\n" #endif @@ -1179,8 +1189,15 @@ U_BOOT_CMD_COMPLETE( U_BOOT_CMD_COMPLETE( grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, "search environment variables", +#ifdef CONFIG_REGEX + "[-e] [-n | -v | -b] string ...\n" +#else "[-n | -v | -b] string ...\n" +#endif " - list environment name=value pairs matching 'string'\n" +#ifdef CONFIG_REGEX + " \"-e\": enable regular expressions;\n" +#endif " \"-n\": search variable names; \"-v\": search values;\n" " \"-b\": search both names and values (default)", var_complete diff --git a/include/search.h b/include/search.h index d06a201..d9ac8df 100644 --- a/include/search.h +++ b/include/search.h @@ -129,7 +129,8 @@ extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *)); #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) +#define H_MATCH_SUBSTR (1 << 7) /* search for substring matches */ +#define H_MATCH_REGEX (1 << 8) /* search for regular expression matches */ +#define H_MATCH_METHOD (H_MATCH_IDENT | H_MATCH_SUBSTR | H_MATCH_REGEX) #endif /* search.h */ diff --git a/lib/hashtable.c b/lib/hashtable.c index 8ad56ec..e0d3a19 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -57,6 +57,7 @@ #include #include #include +#include /* * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986 @@ -540,7 +541,7 @@ static int cmpkey(const void *p1, const void *p2) return (strcmp(e1->key, e2->key)); } -static int match_string(int flag, const char * str, const char * pat) +static int match_string(int flag, const char *str, const char *pat, void *priv) { switch (flag & H_MATCH_METHOD) { case H_MATCH_IDENT: @@ -551,6 +552,17 @@ static int match_string(int flag, const char * str, const char * pat) if (strstr(str, pat)) return 1; break; +#ifdef CONFIG_REGEX + case H_MATCH_REGEX: + { + struct slre *slrep = (struct slre *)priv; + struct cap caps[slrep->num_caps + 2]; + + if (slre_match(slrep, str, strlen(str), caps)) + return 1; + } + break; +#endif default: printf("## ERROR: unsupported match method: 0x%02x\n", flag & H_MATCH_METHOD); @@ -563,14 +575,25 @@ static int match_entry(ENTRY *ep, int flag, int argc, char * const argv[]) { int arg; + void *priv = NULL; for (arg = 1; arg < argc; ++arg) { +#ifdef CONFIG_REGEX + struct slre slre; + + if (slre_compile(&slre, argv[arg]) == 0) { + printf("Error compiling regex: %s\n", slre.err_str); + return 0; + } + + priv = (void *)&slre; +#endif if (flag & H_MATCH_KEY) { - if (match_string(flag, ep->key, argv[arg])) + if (match_string(flag, ep->key, argv[arg], priv)) return 1; } if (flag & H_MATCH_DATA) { - if (match_string(flag, ep->data, argv[arg])) + if (match_string(flag, ep->data, argv[arg], priv)) return 1; } }