grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC][PATCH v1 2/4] Add command to append to existing environment variab


From: Denis 'GNUtoo' Carikli
Subject: [RFC][PATCH v1 2/4] Add command to append to existing environment variables.
Date: Sun, 30 Jun 2024 18:25:17 +0200

This can be used to easily filter out the content of an environment
variable with multiple elements:

    for elm in $list ; do
        if regexp ^grub $elm ; then
           append results=" $elm"
        fi
    done

The goal is to use it to be able to be able to filter devices being
found once we add support for --set=VARNAME inside the ls command.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
---
 grub-core/kern/corecmd.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/grub-core/kern/corecmd.c b/grub-core/kern/corecmd.c
index 62d434ba9..88cf6e4d4 100644
--- a/grub-core/kern/corecmd.c
+++ b/grub-core/kern/corecmd.c
@@ -59,6 +59,28 @@ grub_core_cmd_set (struct grub_command *cmd __attribute__ 
((unused)),
   return 0;
 }
 
+/* Append VALUE to ENVVAR content */
+static grub_err_t
+grub_core_cmd_append (struct grub_command *cmd __attribute__ ((unused)),
+                      int argc, char *argv[])
+{
+  char *var;
+  char *val;
+
+  if (argc == 0)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+  var = argv[0];
+  val = grub_strchr (var, '=');
+  if (! val)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
+
+  val[0] = 0;
+  grub_env_append (var, val + 1);
+
+  return 0;
+}
+
 static grub_err_t
 grub_core_cmd_unset (struct grub_command *cmd __attribute__ ((unused)),
                     int argc, char *argv[])
@@ -182,6 +204,9 @@ grub_register_core_commands (void)
                               N_("Set an environment variable."));
   if (cmd)
     cmd->flags |= GRUB_COMMAND_FLAG_EXTRACTOR;
+  grub_register_command ("append", grub_core_cmd_append,
+                        N_("[ENVVAR=VALUE]"),
+                        N_("Set an environment variable."));
   grub_register_command ("unset", grub_core_cmd_unset,
                         N_("ENVVAR"),
                         N_("Remove an environment variable."));
-- 
2.45.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]