diff mbox series

cmd: env: Add `indirect` to indirectly set values

Message ID 20211220183156.1910e71b.samuel@dionne-riel.com
State Accepted
Delegated to: Tom Rini
Headers show
Series cmd: env: Add `indirect` to indirectly set values | expand

Commit Message

Samuel Dionne-Riel Dec. 20, 2021, 11:31 p.m. UTC
This allows an ergonomic-enough approximation of ${!variable} expansion.
This could be used the following way:

```
for target in ${boot_targets}; do
   env indirect target_name target_name_${target}
   # ...
done
```

Assuming `target_name_mmc0` and similar are set appropriately.

A default value can be optionally provided.

Note: this acts on environment variables, not hush variables.

Signed-off-by: Samuel Dionne-Riel <samuel@dionne-riel.com>
Cc: Simon Glass <sjg@chromium.org>
Cc: "Marek Behún" <marek.behun@nic.cz>
---
 cmd/Kconfig  |  4 ++++
 cmd/nvedit.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

Comments

Simon Glass Dec. 28, 2021, 8:33 a.m. UTC | #1
Hi Samuel,

On Mon, 20 Dec 2021 at 16:32, Samuel Dionne-Riel <samuel@dionne-riel.com> wrote:
>
> This allows an ergonomic-enough approximation of ${!variable} expansion.
> This could be used the following way:
>
> ```
> for target in ${boot_targets}; do
>    env indirect target_name target_name_${target}
>    # ...
> done
> ```

How is this different from:

for target in ${boot_targets}; do
   env set target_name target_name_${target}
   # ...
done

Can you add a few more details?

Also needs some sort of update to doc/

> Assuming `target_name_mmc0` and similar are set appropriately.
>
> A default value can be optionally provided.
>
> Note: this acts on environment variables, not hush variables.
>
> Signed-off-by: Samuel Dionne-Riel <samuel@dionne-riel.com>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: "Marek Behún" <marek.behun@nic.cz>
> ---
>  cmd/Kconfig  |  4 ++++
>  cmd/nvedit.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 49 insertions(+)


Regards,
Simon
Tom Rini April 8, 2022, 12:31 p.m. UTC | #2
On Mon, Dec 20, 2021 at 06:31:56PM -0500, Samuel Dionne-Riel wrote:

> This allows an ergonomic-enough approximation of ${!variable} expansion.
> This could be used the following way:
> 
> ```
> for target in ${boot_targets}; do
>    env indirect target_name target_name_${target}
>    # ...
> done
> ```
> 
> Assuming `target_name_mmc0` and similar are set appropriately.
> 
> A default value can be optionally provided.
> 
> Note: this acts on environment variables, not hush variables.
> 
> Signed-off-by: Samuel Dionne-Riel <samuel@dionne-riel.com>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: "Marek Behún" <marek.behun@nic.cz>

After making this not default y, applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 26d5707f75..e538e69a11 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -508,6 +508,10 @@  config CMD_NVEDIT_EFI
 	  If enabled, we are allowed to set/print UEFI variables using
 	  "env" command with "-e" option without knowing details.
 
+config CMD_NVEDIT_INDIRECT
+	bool "env indirect - Sets environment value from another"
+	default y
+
 config CMD_NVEDIT_INFO
 	bool "env info - print or evaluate environment information"
 	help
diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index 3bb6e764c0..53e6b57b60 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -1018,6 +1018,45 @@  sep_err:
 }
 #endif
 
+#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
+static int do_env_indirect(struct cmd_tbl *cmdtp, int flag,
+		       int argc, char *const argv[])
+{
+	char *to = argv[1];
+	char *from = argv[2];
+	char *default_value = NULL;
+	int ret = 0;
+
+	if (argc < 3 || argc > 4) {
+		return CMD_RET_USAGE;
+	}
+
+	if (argc == 4) {
+		default_value = argv[3];
+	}
+
+	if (env_get(from) == NULL && default_value == NULL) {
+		printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from);
+
+		return CMD_RET_FAILURE;
+	}
+
+	if (env_get(from) == NULL) {
+		ret = env_set(to, default_value);
+	}
+	else {
+		ret = env_set(to, env_get(from));
+	}
+
+	if (ret == 0) {
+		return CMD_RET_SUCCESS;
+	}
+	else {
+		return CMD_RET_FAILURE;
+	}
+}
+#endif
+
 #if defined(CONFIG_CMD_NVEDIT_INFO)
 /*
  * print_env_info - print environment information
@@ -1181,6 +1220,9 @@  static struct cmd_tbl cmd_env_sub[] = {
 #if defined(CONFIG_CMD_IMPORTENV)
 	U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
 #endif
+#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
+	U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""),
+#endif
 #if defined(CONFIG_CMD_NVEDIT_INFO)
 	U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
 #endif
@@ -1265,6 +1307,9 @@  static char env_help_text[] =
 #if defined(CONFIG_CMD_IMPORTENV)
 	"env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
 #endif
+#if defined(CONFIG_CMD_NVEDIT_INDIRECT)
+	"env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n"
+#endif
 #if defined(CONFIG_CMD_NVEDIT_INFO)
 	"env info - display environment information\n"
 	"env info [-d] [-p] [-q] - evaluate environment information\n"