From: Artem Lapkin <email2tema@gmail.com>
To: sjg@chromium.org
Cc: trini@konsulko.com, marek.behun@nic.cz, narmstrong@baylibre.com,
twarren@nvidia.com, andre.przywara@arm.com, u-boot@lists.denx.de,
u-boot-amlogic@groups.io, christianshewitt@gmail.com,
art@khadas.com, nick@khadas.com, gouwa@khadas.com
Subject: [PATCH v2 1/2] env: setenv add resolve value option
Date: Fri, 19 Nov 2021 12:36:46 +0800 [thread overview]
Message-ID: <20211119043647.1251416-2-art@khadas.com> (raw)
In-Reply-To: <20211119043647.1251416-1-art@khadas.com>
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..." */
Signed-off-by: Artem Lapkin <art@khadas.com>
---
V2 changes:
_ fix comments style
_ add comment include/exports.h
_ remake strcpy to strdup
_ env_resolve minimize
---
cmd/nvedit.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
include/_exports.h | 1 +
include/env.h | 11 +++++++++++
include/exports.h | 2 ++
4 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index 3bb6e764c0..6e1237df81 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -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,29 @@ 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];
+ char *v = NULL;
+
+ do {
+ cli_simple_process_macros(value, value2, CONFIG_SYS_CBSIZE);
+
+ if (!strcmp(value, value2))
+ break;
+
+ v = strdup(value2);
+ if (!v) {
+ printf("## Can't allocate memory\n");
+ return 1;
+ }
+
+ free(value);
+ value = v;
+ } while (max_loop--);
+ }
+
e.key = name;
e.data = value;
hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
@@ -304,6 +331,18 @@ 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;
+
+ return _do_env_set(0, !varvalue || varvalue[0] == '\0' ? 3 : 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 +1410,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
);
diff --git a/include/_exports.h b/include/_exports.h
index 8030d70c0b..86bc07f051 100644
--- a/include/_exports.h
+++ b/include/_exports.h
@@ -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)
diff --git a/include/env.h b/include/env.h
index ee5e30d036..73d1aca9ac 100644
--- a/include/env.h
+++ b/include/env.h
@@ -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 value
+ *
+ * @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
*
diff --git a/include/exports.h b/include/exports.h
index 550cafdc7a..02aac8047c 100644
--- a/include/exports.h
+++ b/include/exports.h
@@ -44,6 +44,8 @@ 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);
+/* deep resolve value to environment variable */
+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);
--
2.25.1
next prev parent reply other threads:[~2021-11-19 4:37 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-19 4:36 [PATCH v2 0/2] env: setenv add resolve value option Artem Lapkin
2021-11-19 4:36 ` Artem Lapkin [this message]
2021-11-25 0:13 ` [PATCH v2 1/2] " Simon Glass
2022-04-07 18:05 ` Tom Rini
2022-04-08 13:09 ` Sean Anderson
2022-04-08 13:11 ` Tom Rini
2021-11-19 4:36 ` [PATCH v2 2/2] test: env: deep resolve value testing Artem Lapkin
2021-11-25 0:13 ` Simon Glass
2021-11-19 7:48 ` [PATCH v2 0/2] env: setenv add resolve value option Wolfgang Denk
2021-11-19 9:06 ` Art Nikpal
2021-11-20 12:36 ` Wolfgang Denk
2021-11-22 8:25 ` Art Nikpal
[not found] ` <6198ebca.1c69fb81.fc50c.bf0bSMTPIN_ADDED_BROKEN@mx.google.com>
2021-11-22 8:09 ` Art Nikpal
2021-11-22 8:51 ` Wolfgang Denk
2021-11-22 10:06 ` Art Nikpal
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=20211119043647.1251416-2-art@khadas.com \
--to=email2tema@gmail.com \
--cc=andre.przywara@arm.com \
--cc=art@khadas.com \
--cc=christianshewitt@gmail.com \
--cc=gouwa@khadas.com \
--cc=marek.behun@nic.cz \
--cc=narmstrong@baylibre.com \
--cc=nick@khadas.com \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=twarren@nvidia.com \
--cc=u-boot-amlogic@groups.io \
--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