Skip to contentSkip to conversation

common: add a grepenv command

PATCH New Kim Phillips 5 participants 10 messages Jul 16, 2010 – Jul 18, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common: add a grepenv command

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.

---
 common/cmd_nvedit.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 common/command.c    |    1 +
 2 files changed, 55 insertions(+), 1 deletions(-)
common/cmd_nvedit.c +54 -1
index 13325bc..c4bde27 100644
@@ -4,7 +4,9 @@
  *
  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  * Andreas Heppel <[email protected]>
-
+ *
+ * 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"
common/command.c +1 -0
index a1fc592..305bda0 100644
@@ -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)
Signature · 1 line
1.7.1.1
#1 lore raw

PATCH 1/1 common: add a grepenv command

Dear Kim Phillips,

Quoted 6 lines · In message <[email protected]> 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.

Thanks, but I will not apply thjis.

I'm in the process of resturcturing the whole environment handling. The new implementation will use a hash table for internal storage, so this code would no longer apply then.

Please wait a bit for me to publish the new code, and then try to reimplement it in the new environment.

So thanks, but sorry...

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The use of COBOL cripples the mind; its teaching  should,  therefore,
be regarded as a criminal offense.                   - E. W. Dijkstra
#2 lore raw

PATCH 1/1 common: add a grepenv command

Quoted 9 lines · Wolfgang Denk

Dear Kim Phillips,

Quoted 6 lines · In message <[email protected]> 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.

My five cents to this: this addition command should be optional (#ifdef) anyway...

Quoted 9 lines

Thanks, but I will not apply thjis.

I'm in the process of resturcturing the whole environment handling. The new implementation will use a hash table for internal storage, so this code would no longer apply then.

Please wait a bit for me to publish the new code, and then try to reimplement it in the new environment.

Will be there a means to mark variables as non-saveable, example if one does a bootp/dhcp and a saveenv afterwards, ip-address, filesize etc. are saved as well, which makes absolutely no sense. Neither does saving the ethaddr when it is created from VPD data :)

Reinhard

#3 lore raw

PATCH 1/1 common: add a grepenv command

Dear "Reinhard Meyer (-VC)",

Quoted 7 lines · In message <[email protected]> you wrote:
Quoted 5 lines
Quoted 5 lines

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.

My five cents to this: this addition command should be optional (#ifdef) anyway...

Agreed...

Quoted 11 lines
Quoted 6 lines

I'm in the process of resturcturing the whole environment handling. The new implementation will use a hash table for internal storage, so this code would no longer apply then.

Please wait a bit for me to publish the new code, and then try to reimplement it in the new environment.

Will be there a means to mark variables as non-saveable, example if one does a bootp/dhcp and a saveenv afterwards, ip-address, filesize etc. are saved as well, which makes absolutely no sense. Neither does saving the ethaddr when it is created from VPD data :)

No, this is not part of my current work yet. However, once the hash table based code is in place and working, it should be pretty straightforward to extend the "struct entry", which currently holds only two fields for name and value, by one or more additional fields that can be used to store flags holding for example information like what you mentioned.

But this is step 2 - at best.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"Whoever undertakes to set himself up as a judge of Truth  and  Know-
ledge is shipwrecked by the laughter of the gods."  - Albert Einstein
#4 lore raw

PATCH 1/1 common: add a grepenv command

Quoted 4 lines · On Fri, 16 Jul 2010 12:55:42 +0200 Wolfgang Denk <[email protected]> wrote:

Dear "Reinhard Meyer (-VC)",

Quoted 1 line · In message <[email protected]> you wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

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.

My five cents to this: this addition command should be optional (#ifdef) anyway...

Agreed...

hmm - it adds ~450bytes text+data on powerpc. Is that the concern?

I can make it optional, but, if so, I'd also like to add it to config_cmd_default.h instead of it being added to 100s of board config files.

Kim

#5 lore raw

PATCH 1/1 common: add a grepenv command

Dear Kim Phillips,

Quoted 6 lines · In message <[email protected]> you wrote:

hmm - it adds ~450bytes text+data on powerpc. Is that the concern?

I can make it optional, but, if so, I'd also like to add it to config_cmd_default.h instead of it being added to 100s of board config files.

I think it should be optional, and not be enabled by default.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Ninety-Ninety Rule of Project Schedules:
        The first ninety percent of the task takes ninety percent of
the time, and the last ten percent takes the other ninety percent.
#6 lore raw

PATCH 1/1 common: add a grepenv command

In reply to Wolfgang Denk
Quoted 10 lines · On Fri, 16 Jul 2010 11:27:03 +0200 Wolfgang Denk <[email protected]> wrote:

Dear Kim Phillips,

Quoted 6 lines · In message <[email protected]> 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.

Quoted 5 lines

Thanks, but I will not apply thjis.

I'm in the process of resturcturing the whole environment handling. The new implementation will use a hash table for internal storage, so this code would no longer apply then.

it would still apply, in that the environment should still be greppable, but, yes, if the internal structures are changed, this patch would need updating, along the same lines as with printenv and friends.

Quoted 2 lines

Please wait a bit for me to publish the new code, and then try to reimplement it in the new environment.

technically this should be allowed in, because it was submitted first and it's need is overdue IMHO (I have already gotten off-list thanks for this patch). Subsequent patches need to honour valid patches posted to the list first.

Having said that, would you like to send me your hash table patches?

Kim

#7 lore raw

PATCH 1/1 common: add a grepenv command

Dear Kim,

Quoted 8 lines · In message <[email protected]> you wrote:
Quoted 4 lines

I'm in the process of resturcturing the whole environment handling. The new implementation will use a hash table for internal storage, so this code would no longer apply then.

it would still apply, in that the environment should still be greppable, but, yes, if the internal structures are changed, this patch would need updating, along the same lines as with printenv and friends.

I don;t see how you would grep the entries in a hash table - except by walking through all entries, which works, but is slow. But then, the new code will print the variable alphabetically sorted, so looking up a setting will be much easier anyway :-)

Quoted 4 lines

technically this should be allowed in, because it was submitted first and it's need is overdue IMHO (I have already gotten off-list thanks for this patch). Subsequent patches need to honour valid patches posted to the list first.

Yes, you are right. Let's put this patch on hold for now; eventually we can have both this and the new env code.

Quoted 1 line

Having said that, would you like to send me your hash table patches?

I'm still working on fixes and cleanup. I intend to post the patches before the MW closes, though (and provide a branch to pull from).

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
No man knows what true happiness is until he gets married.  By  then,
of course, its too late.
#8 lore raw

PATCH 1/1 common: add a grepenv command

In reply to Kim Phillips
Quoted 6 lines · On Thu, 15 Jul 2010 22:15:22 -0500 Kim Phillips <[email protected]>…

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.

Since it doesn't actually handle regular expressions, I'd call it something like findenv. Or fgrepenv. :-)

-Scott

#9 lore raw

PATCH 1/1 common: add a grepenv command

Quoted 10 lines · Scott Wood
Quoted 6 lines · On Thu, 15 Jul 2010 22:15:22 -0500 Kim Phillips<[email protected]>…

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.

Quoted 4 lines

Since it doesn't actually handle regular expressions, I'd call it something like findenv. Or fgrepenv. :-)

-Scott

Me too on "findenv". I don't like fgrepenv since it has the same problem as "grepenv". It actually is a gssp (global substring print) :-P.

gvb