public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Kim Phillips <kim.phillips@freescale.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] common: add a grepenv command
Date: Thu, 15 Jul 2010 22:15:22 -0500	[thread overview]
Message-ID: <20100715221522.1dd4eecb.kim.phillips@freescale.com> (raw)

u-boot environments, esp. when boards are shared across multiple users,
can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching.  grepenv works like printenv but limits its output
only to environment strings (variable name and value pairs) that match the
user specified substring.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
---
 common/cmd_nvedit.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 common/command.c    |    1 +
 2 files changed, 55 insertions(+), 1 deletions(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 13325bc..c4bde27 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -4,7 +4,9 @@
  *
  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  * Andreas Heppel <aheppel@sysgo.de>
-
+ *
+ * Copyright 2010 Freescale Semiconductor, Inc.
+ *
  * See file CREDITS for list of people who contributed to this
  * project.
  *
@@ -139,6 +141,50 @@ static int printenv(char *name, int state)
 	return i;
 }
 
+int do_grepenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	char buf[CONFIG_SYS_CBSIZE], *searchstr;
+	int i = 0, j = 0, k = 0;
+	int rcode = 1;
+
+	if (argc != 2) {
+		cmd_usage(cmdtp);
+		return 1;
+	}
+
+	searchstr = argv[1];
+
+	/* find and print matching env vars */
+	do {
+		buf[j] = env_get_char(i);
+		if (buf[j] == searchstr[k]) {
+			k++;
+			if (searchstr[k] == '\0') {
+				/* match complete */
+				rcode = 0;
+				do {
+					i++; j++;
+					buf[j] = env_get_char(i);
+				} while (buf[j] != '\0');
+				puts(buf); puts("\n");
+				j = 0; k = 0;
+			} else
+				j++;
+		} else {
+			k = 0;
+			if (buf[j] == '\0') {
+				j = 0;
+				if (ctrlc())
+					return -1;
+			} else
+				j++;
+		}
+		i++;
+	} while (!(j == 0 && env_get_char(i) == '\0'));
+
+	return rcode;
+}
+
 int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	int i;
@@ -634,6 +680,13 @@ U_BOOT_CMD(
 );
 
 U_BOOT_CMD(
+	grepenv, 2, 0,	do_grepenv,
+	"search environment",
+	"fixed-string\n"
+	"    - list environment name and value pairs matching 'fixed-string'"
+);
+
+U_BOOT_CMD(
 	setenv, CONFIG_SYS_MAXARGS, 0,	do_setenv,
 	"set environment variables",
 	"name value ...\n"
diff --git a/common/command.c b/common/command.c
index a1fc592..305bda0 100644
--- a/common/command.c
+++ b/common/command.c
@@ -191,6 +191,7 @@ void install_auto_complete(void)
 #if defined(CONFIG_CMD_EDITENV)
 	install_auto_complete_handler("editenv", var_complete);
 #endif
+	install_auto_complete_handler("grepenv", var_complete);
 	install_auto_complete_handler("printenv", var_complete);
 	install_auto_complete_handler("setenv", var_complete);
 #if defined(CONFIG_CMD_RUN)
-- 
1.7.1.1

             reply	other threads:[~2010-07-16  3:15 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-16  3:15 Kim Phillips [this message]
2010-07-16  9:27 ` [U-Boot] [PATCH] common: add a grepenv command Wolfgang Denk
2010-07-16 10:29   ` Reinhard Meyer
2010-07-16 10:55     ` Wolfgang Denk
2010-07-16 19:02       ` Kim Phillips
2010-07-16 19:13         ` Wolfgang Denk
2010-07-16 18:42   ` Kim Phillips
2010-07-16 19:12     ` Wolfgang Denk
2010-07-16 20:41 ` Scott Wood
2010-07-18  3:32   ` Jerry Van Baren

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=20100715221522.1dd4eecb.kim.phillips@freescale.com \
    --to=kim.phillips@freescale.com \
    --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