diff mbox

[U-Boot,v2] common: add a grepenv command

Message ID 20110401174658.eaa8d773.kim.phillips@freescale.com
State Changes Requested
Headers show

Commit Message

Kim Phillips April 1, 2011, 10:46 p.m. UTC
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>
---
v2: rebased

 common/cmd_nvedit.c |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)

Comments

Wolfgang Denk April 1, 2011, 10:59 p.m. UTC | #1
Dear Kim Phillips,

In message <20110401174658.eaa8d773.kim.phillips@freescale.com> you wrote:
> 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.

It would be nice if you gave an example of how to use this.

> +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;
> +	}

Why don't you allow for an arbitrary number of arguments here?

> +	/* find and print matching env vars */
> +	do {
> +		buf[j] = env_get_char(i);
...
> +	} while (!(j == 0 && env_get_char(i) == '\0'));

This does not work as you might expect.  The current environment
cannot be parsed like that, becuase it is not stored in a linear list
any more, but in a hash table instead.

You may be lucky on some systems (especially NOR flash based ones) to
read the last saved data from the persistent storage, but this is NOT
the current environment.



Best regards,

Wolfgang Denk
Mike Frysinger April 1, 2011, 11:06 p.m. UTC | #2
On Fri, Apr 1, 2011 at 6:46 PM, Kim Phillips wrote:
> 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.

please add a proper #ifdef like CONFIG_CMD_GREPENV and do not enable
it by default.

> +int do_grepenv (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])

static

> +       char buf[CONFIG_SYS_CBSIZE], *searchstr;

i dont think that is correct usage of this define
-mike
diff mbox

Patch

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index fb69c24..532167b 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -160,6 +160,49 @@  int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	return rcode;
 }
 
+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;
+}
 /*
  * Set a new environment variable,
  * or replace or delete an existing one.
@@ -904,6 +947,14 @@  U_BOOT_CMD_COMPLETE(
 );
 
 U_BOOT_CMD_COMPLETE(
+	grepenv, 2, 0,  do_grepenv,
+	"search environment",
+	"fixed-string\n"
+	"    - list environment name and value pairs matching 'fixed-string'",
+	var_complete
+);
+
+U_BOOT_CMD_COMPLETE(
 	setenv, CONFIG_SYS_MAXARGS, 0,	do_env_set,
 	"set environment variables",
 	"name value ...\n"