Skip to contentSkip to conversation

env: setenv add resolve value option

PATCH Superseded Artem Lapkin 3 participants 5 messages Nov 2, 2021 – Nov 5, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 env: setenv add resolve value option

Add possibility setup env variable with additional resolving vars inside value.

Usage examples

=> setenv a hello
=> setenv b world
=> setenv c '${a} ${b}'
=> setenv -r d '${c}! ${a}...'
=> printenv d
d=hello world! hello...

/* internal usage example */
env_resolve("d", "${c}! ${a}...");
/* d="hello world! hello..." */

Notes

Resolving works only for ${var} "bracket" and didn't workd for "unbracket" $var

=> setenv -r d '$c! $a...'
=> printenv d
d=$c! $a...

---
 cmd/nvedit.c       | 42 +++++++++++++++++++++++++++++++++++++++++-
 include/_exports.h |  1 +
 include/env.h      | 11 +++++++++++
 include/exports.h  |  1 +
 4 files changed, 54 insertions(+), 1 deletion(-)
cmd/nvedit.c +41 -1
index 3bb6e764c0..6608932dc0 100644
@@ -229,6 +229,7 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
 	int   i, len;
 	char  *name, *value, *s;
 	struct env_entry e, *ep;
+	bool  resolve = 0;
 
 	debug("Initial value for argc=%d\n", argc);
 
@@ -246,6 +247,9 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
 			case 'f':		/* force */
 				env_flag |= H_FORCE;
 				break;
+			case 'r':		/* resolve */
+				resolve = 1;
+				break;
 			default:
 				return CMD_RET_USAGE;
 			}
@@ -291,6 +295,26 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
 	if (s != value)
 		*--s = '\0';
 
+	/*
+	 * deep resolve value vars
+	 */
+	if (resolve) {
+		int max_loop = 32;
+		char value2[CONFIG_SYS_CBSIZE];
+
+		do {
+			cli_simple_process_macros(value, value2, CONFIG_SYS_CBSIZE);
+			if (!strcmp(value, value2))
+				break;
+			value = realloc(value, strlen(value2));
+			if (!value) {
+				printf("## Can't realloc %d bytes\n", len);
+				return 1;
+			}
+			strcpy(value, value2);
+		} while (max_loop--);
+	}
+
 	e.key	= name;
 	e.data	= value;
 	hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
@@ -304,6 +328,20 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
 	return 0;
 }
 
+int env_resolve(const char *varname, const char *varvalue)
+{
+	const char * const argv[5] = { "setenv", "-r", varname, varvalue, NULL };
+
+	/* before import into hashtable */
+	if (!(gd->flags & GD_FLG_ENV_READY))
+		return 1;
+
+	if (!varvalue || varvalue[0] == '\0')
+		return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
+	else
+		return _do_env_set(0, 4, (char * const *)argv, H_PROGRAMMATIC);
+}
+
 int env_set(const char *varname, const char *varvalue)
 {
 	const char * const argv[4] = { "setenv", varname, varvalue, NULL };
@@ -1371,7 +1409,9 @@ U_BOOT_CMD_COMPLETE(
 	"setenv [-f] name value ...\n"
 	"    - [forcibly] set environment variable 'name' to 'value ...'\n"
 	"setenv [-f] name\n"
-	"    - [forcibly] delete environment variable 'name'",
+	"    - [forcibly] delete environment variable 'name'\n"
+	"setenv [-r] name value ...\n"
+	"    - [resolve] resolve 'value ...' to environment variable\n",
 	var_complete
 );
 
include/_exports.h +1 -0
index 8030d70c0b..86bc07f051 100644
@@ -32,6 +32,7 @@
 	EXPORT_FUNC(do_reset, int, do_reset, struct cmd_tbl *,
 		    int , int , char * const [])
 	EXPORT_FUNC(env_get, char  *, env_get, const char*)
+	EXPORT_FUNC(env_resolve, int, env_resolve, const char *, const char *)
 	EXPORT_FUNC(env_set, int, env_set, const char *, const char *)
 	EXPORT_FUNC(simple_strtoul, unsigned long, simple_strtoul,
 		    const char *, char **, unsigned int)
include/env.h +11 -0
index ee5e30d036..c4efc5b7e5 100644
@@ -133,6 +133,17 @@ int env_get_f(const char *name, char *buf, unsigned int len);
  */
 int env_get_yesno(const char *var);
 
+/**
+ * env_resolve() - resolve to environment variable
+ *
+ * Same as env_set but make deep resolve for variable
+ *
+ * @varname: Variable to adjust
+ * @value: Value to resolve for the variable, or NULL or "" to delete the variable
+ * @return 0 if OK, 1 on error
+ */
+int env_resolve(const char *varname, const char *value);
+
 /**
  * env_set() - set an environment variable
  *
include/exports.h +1 -0
index 550cafdc7a..a54b997988 100644
@@ -44,6 +44,7 @@ int vprintf(const char *, va_list);
 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base);
 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
 char *env_get(const char *name);
+int env_resolve(const char *varname, const char *value);
 int env_set(const char *varname, const char *value);
 long simple_strtol(const char *cp, char **endp, unsigned int base);
 int strcmp(const char *cs, const char *ct);
Signature · 1 line
2.25.1
#1 lore raw

Hi Artem,

Quoted 13 lines · Artem Lapkin

Add possibility setup env variable with additional resolving vars inside value.

Usage examples

=> setenv a hello
=> setenv b world
=> setenv c '${a} ${b}'
=> setenv -r d '${c}! ${a}...'
=> printenv d
d=hello world! hello...

Quoted 8 lines

/* internal usage example */
env_resolve("d", "${c}! ${a}...");
/* d="hello world! hello..." */

Notes

Resolving works only for ${var} "bracket" and didn't workd for "unbracket" $var

how come?

Quoted 5 lines

=> setenv -r d '$c! $a...'
=> printenv d
d=$c! $a...

Quoted 7 lines
---
 cmd/nvedit.c       | 42 +++++++++++++++++++++++++++++++++++++++++-
 include/_exports.h |  1 +
 include/env.h      | 11 +++++++++++
 include/exports.h  |  1 +
 4 files changed, 54 insertions(+), 1 deletion(-)

See also this old patch:

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Quoted 24 lines
cmd/nvedit.c +4 -0
index 3bb6e764c0..6608932dc0 100644
@@ -229,6 +229,7 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
        int   i, len;
        char  *name, *value, *s;
        struct env_entry e, *ep;
+       bool  resolve = 0;
 
        debug("Initial value for argc=%d\n", argc);
 
@@ -246,6 +247,9 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
                        case 'f':               /* force */
                                env_flag |= H_FORCE;
                                break;
+                       case 'r':               /* resolve */
+                               resolve = 1;
+                               break;
                        default:
                                return CMD_RET_USAGE;
                        }

cmd/nvedit.c static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) · line 300

Quoted 6 lines
cmd/nvedit.c +3 -0
@@ -291,6 +295,26 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
        if (s != value)
                *--s = '\0';
 
+       /*
+        * deep resolve value vars
+        */

single-line comment style is /*... */

cmd/nvedit.c static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) · line 309

Quoted 9 lines
cmd/nvedit.c +9 -0
+       if (resolve) {
+               int max_loop = 32;
+               char value2[CONFIG_SYS_CBSIZE];
+
+               do {
+                       cli_simple_process_macros(value, value2, CONFIG_SYS_CBSIZE);
+                       if (!strcmp(value, value2))
+                               break;
+                       value = realloc(value, strlen(value2));

strdup() ?

Quoted 14 lines
cmd/nvedit.c +8 -0
+                       if (!value) {
+                               printf("## Can't realloc %d bytes\n", len);
+                               return 1;
+                       }
+                       strcpy(value, value2);
+               } while (max_loop--);
+       }
+
        e.key   = name;
        e.data  = value;
        hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
@@ -304,6 +328,20 @@ static int _do_env_set(int flag, int argc, char *const argv[], int env_flag)
        return 0;
 }

cmd/nvedit.c static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) · line 339

Quoted 10 lines
cmd/nvedit.c +9 -0
 
+int env_resolve(const char *varname, const char *varvalue)
+{
+       const char * const argv[5] = { "setenv", "-r", varname, varvalue, NULL };
+
+       /* before import into hashtable */
+       if (!(gd->flags & GD_FLG_ENV_READY))
+               return 1;
+
+       if (!varvalue || varvalue[0] == '\0')

Could put that in a var to avoid repeating in the next 3 lines:

Quoted 58 lines
cmd/nvedit.c +8 -1
+               return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
+       else
+               return _do_env_set(0, 4, (char * const *)argv, H_PROGRAMMATIC);
+}
+
 int env_set(const char *varname, const char *varvalue)
 {
        const char * const argv[4] = { "setenv", varname, varvalue, NULL };
@@ -1371,7 +1409,9 @@ U_BOOT_CMD_COMPLETE(
        "setenv [-f] name value ...\n"
        "    - [forcibly] set environment variable 'name' to 'value ...'\n"
        "setenv [-f] name\n"
-       "    - [forcibly] delete environment variable 'name'",
+       "    - [forcibly] delete environment variable 'name'\n"
+       "setenv [-r] name value ...\n"
+       "    - [resolve] resolve 'value ...' to environment variable\n",
        var_complete
 );
 
include/_exports.h +1 -0
index 8030d70c0b..86bc07f051 100644
@@ -32,6 +32,7 @@
        EXPORT_FUNC(do_reset, int, do_reset, struct cmd_tbl *,
                    int , int , char * const [])
        EXPORT_FUNC(env_get, char  *, env_get, const char*)
+       EXPORT_FUNC(env_resolve, int, env_resolve, const char *, const char *)
        EXPORT_FUNC(env_set, int, env_set, const char *, const char *)
        EXPORT_FUNC(simple_strtoul, unsigned long, simple_strtoul,
                    const char *, char **, unsigned int)
include/env.h +11 -0
index ee5e30d036..c4efc5b7e5 100644
@@ -133,6 +133,17 @@ int env_get_f(const char *name, char *buf, unsigned int len);
  */
 int env_get_yesno(const char *var);
 
+/**
+ * env_resolve() - resolve to environment variable
+ *
+ * Same as env_set but make deep resolve for variable
+ *
+ * @varname: Variable to adjust
+ * @value: Value to resolve for the variable, or NULL or "" to delete the variable
+ * @return 0 if OK, 1 on error
+ */
+int env_resolve(const char *varname, const char *value);
+
 /**
  * env_set() - set an environment variable
  *
include/exports.h +0 -0
index 550cafdc7a..a54b997988 100644

include/exports.h int vprintf(const char *, va_list); · line 47

Quoted 4 lines
include/exports.h +1 -0
@@ -44,6 +44,7 @@ int vprintf(const char *, va_list);
 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base);
 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
 char *env_get(const char *name);
+int env_resolve(const char *varname, const char *value);

please add function comment

Quoted 6 lines
include/exports.h +0 -1
 int env_set(const char *varname, const char *value);
 long simple_strtol(const char *cp, char **endp, unsigned int base);
 int strcmp(const char *cs, const char *ct);
--

2.25.1

Can you please add to the env tests?

test/py/tests/test_env.py
Regards,
Simon

#2 lore raw
In reply to Artem Lapkin
Quoted 18 lines · Artem Lapkin

Add possibility setup env variable with additional resolving vars inside value.

Usage examples

=> setenv a hello
=> setenv b world
=> setenv c '${a} ${b}'
=> setenv -r d '${c}! ${a}...'
=> printenv d
d=hello world! hello...

/* internal usage example */
env_resolve("d", "${c}! ${a}...");
/* d="hello world! hello..." */

Notes

Quoted 8 lines

Resolving works only for ${var} "bracket" and didn't workd for "unbracket" $var

=> setenv -r d '$c! $a...'
=> printenv d
d=$c! $a...

The high level problem I have with this patch is that we keep going back to "we really need to update to a modern hush (or other shell) rather than patching new features in to our ancient fork".

Signature · 1 line
Tom
author
#3 lore raw
Quoted 3 lines

The high level problem I have with this patch is that we keep going back to "we really need to update to a modern hush (or other shell) rather than patching new features in to our ancient fork".

Yes it will be fine ! Does anybody have information about these plans ? but in any case my patch didn't broke compatibility like next patch

Quoted 6 lines

See also this old patch:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Can you please add to the env tests?
please add function comment
...

tnx for suggestions ...
i can make v2 variant for my patch , if no one is against this idea

Quoted 36 lines · Tom Rini
Quoted 18 lines · Artem Lapkin

Add possibility setup env variable with additional resolving vars inside value.

Usage examples

=> setenv a hello
=> setenv b world
=> setenv c '${a} ${b}'
=> setenv -r d '${c}! ${a}...'
=> printenv d
d=hello world! hello...

/* internal usage example */
env_resolve("d", "${c}! ${a}...");
/* d="hello world! hello..." */

Notes

Quoted 8 lines

Resolving works only for ${var} "bracket" and didn't workd for "unbracket" $var

=> setenv -r d '$c! $a...'
=> printenv d
d=$c! $a...

The high level problem I have with this patch is that we keep going back to "we really need to update to a modern hush (or other shell) rather than patching new features in to our ancient fork".

--
Tom

#4 lore raw

Hi Art,

Quoted 6 lines · Art Nikpal
Quoted 4 lines

The high level problem I have with this patch is that we keep going back to "we really need to update to a modern hush (or other shell) rather than patching new features in to our ancient fork".

Yes it will be fine ! Does anybody have information about these plans ?

Yes see here: https://docs.google.com/document/d/1YBOMsbM19uSFyoJWnt7-PsOLBaevzQUgV-hiR88a5-o/edit#bookmark=id.j6h2xzste5sy

We could ask for an update on progress.

Quoted 11 lines

but in any case my patch didn't broke compatibility like next patch

Quoted 6 lines

See also this old patch:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Can you please add to the env tests?
please add function comment
...

tnx for suggestions ...
i can make v2 variant for my patch , if no one is against this idea

I am not against it. But see Tom's comment.

Quoted 30 lines · Tom Rini
Quoted 28 lines · Artem Lapkin
Quoted 26 lines

Add possibility setup env variable with additional resolving vars inside value.

Usage examples

=> setenv a hello
=> setenv b world
=> setenv c '${a} ${b}'
=> setenv -r d '${c}! ${a}...'
=> printenv d
d=hello world! hello...

/* internal usage example */
env_resolve("d", "${c}! ${a}...");
/* d="hello world! hello..." */

Notes

Resolving works only for ${var} "bracket" and didn't workd for "unbracket" $var

=> setenv -r d '$c! $a...'
=> printenv d
d=$c! $a...

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

The high level problem I have with this patch is that we keep going back to "we really need to update to a modern hush (or other shell) rather than patching new features in to our ancient fork".

It is orthogonal to the shell, so far as I can tell.

Regards,
Simon