[U-Boot] [PATCH v2 08/19] env: Distinguish finer between source of env change

Joe Hershberger joe.hershberger at ni.com
Wed Apr 29 07:50:55 CEST 2015


We already could tell the difference in the callback between an import
and "other" which we called interactive. Now add further distinction
between interactive (i.e. running env set / env edit / env ask / etc.
from the U-Boot command line) and programmatic (i.e. when u-boot source
calls any variant of setenv() ).

Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
Reviewed-by: Simon Glass <sjg at chromium.org>
---

Changes in v2: None

 common/cmd_nvedit.c | 26 +++++++++++++++++++-------
 include/search.h    |  2 ++
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 6ca5a2e..f4c2523 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -208,12 +208,11 @@ DONE:
  * Set a new environment variable,
  * or replace or delete an existing one.
  */
-static int _do_env_set(int flag, int argc, char * const argv[])
+static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
 {
 	int   i, len;
 	char  *name, *value, *s;
 	ENTRY e, *ep;
-	int env_flag = H_INTERACTIVE;
 
 	debug("Initial value for argc=%d\n", argc);
 	while (argc > 1 && **(argv + 1) == '-') {
@@ -291,9 +290,9 @@ int setenv(const char *varname, const char *varvalue)
 		return 1;
 
 	if (varvalue == NULL || varvalue[0] == '\0')
-		return _do_env_set(0, 2, (char * const *)argv);
+		return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
 	else
-		return _do_env_set(0, 3, (char * const *)argv);
+		return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
 }
 
 /**
@@ -347,7 +346,7 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	if (argc < 2)
 		return CMD_RET_USAGE;
 
-	return _do_env_set(flag, argc, argv);
+	return _do_env_set(flag, argc, argv, H_INTERACTIVE);
 }
 
 /*
@@ -422,7 +421,7 @@ int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	}
 
 	/* Continue calling setenv code */
-	return _do_env_set(flag, len, local_args);
+	return _do_env_set(flag, len, local_args, H_INTERACTIVE);
 }
 #endif
 
@@ -588,6 +587,10 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
 	if (argc < 2)
 		return CMD_RET_USAGE;
 
+	/* before import into hashtable */
+	if (!(gd->flags & GD_FLG_ENV_READY))
+		return 1;
+
 	/* Set read buffer to initial value or empty sting */
 	init_val = getenv(argv[1]);
 	if (init_val)
@@ -598,7 +601,16 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
 	if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
 		return 1;
 
-	return setenv(argv[1], buffer);
+	if (buffer[0] == '\0') {
+		const char * const _argv[3] = { "setenv", argv[1], NULL };
+
+		return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
+	} else {
+		const char * const _argv[4] = { "setenv", argv[1], buffer,
+			NULL };
+
+		return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
+	}
 }
 #endif /* CONFIG_CMD_EDITENV */
 #endif /* CONFIG_SPL_BUILD */
diff --git a/include/search.h b/include/search.h
index 9701efb..343dbc3 100644
--- a/include/search.h
+++ b/include/search.h
@@ -120,5 +120,7 @@ extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
 #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)
+#define H_PROGRAMMATIC	(1 << 9) /* indicate that an import is from setenv() */
+#define H_ORIGIN_FLAGS	(H_INTERACTIVE | H_PROGRAMMATIC)
 
 #endif /* search.h */
-- 
1.7.11.5



More information about the U-Boot mailing list