All of lore.kernel.org
 help / color / mirror / Atom feed
From: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
To: grub-devel@gnu.org
Cc: Adrien 'neox' Bourmault <neox@gnu.org>,
	Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
Subject: [RFC][PATCH v1 1/4] Add grub_env_append function.
Date: Sun, 30 Jun 2024 18:25:16 +0200	[thread overview]
Message-ID: <20240630162519.25173-2-GNUtoo@cyberdimension.org> (raw)
In-Reply-To: <20240630162519.25173-1-GNUtoo@cyberdimension.org>

If the given environment variable doesn't exist, grub_env_append will
have the same effect than grub_env_set. But if the variable do exist,
using grub_env_append will append the given content to the variable
content.

This can be used to build a command that can append data to an
existing variable.

The goal here is to more easily add --set=VARNAME arguments to current
commands like it is done in the probe command for instance.

This is because in the code of some commands (like ls) GRUB start
printing information directly to the output instead of building a big
string and only printing the information when done building it.

And so having something like grub_env_append that is closer to this
behavior helps adding --set=VARNAME to various commands (like ls).

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
---
 grub-core/kern/env.c | 38 ++++++++++++++++++++++++++++++++++++++
 include/grub/env.h   |  1 +
 2 files changed, 39 insertions(+)

diff --git a/grub-core/kern/env.c b/grub-core/kern/env.c
index 764068896..24ba42bb8 100644
--- a/grub-core/kern/env.c
+++ b/grub-core/kern/env.c
@@ -129,6 +129,44 @@ grub_env_set (const char *name, const char *val)
   return grub_errno;
 }
 
+grub_err_t
+grub_env_append (const char *name, const char *val)
+{
+  struct grub_env_var *var;
+
+  /* If the variable does already exist, append val to the variable content.  */
+  var = grub_env_find (name);
+  if (var)
+    {
+      char *old = var->value;
+      char *new;
+
+      new = grub_zalloc (grub_strlen(old) + grub_strlen(val) + 1);
+      if (!new)
+        return grub_errno;
+
+      grub_strcpy (new, old);
+      grub_strcpy (new + grub_strlen(new), val);
+
+      if (var->write_hook)
+        var->value = var->write_hook (var, new);
+      else
+        var->value = grub_strdup (new);
+
+      if (! var->value)
+        {
+          var->value = old;
+          grub_free (new);
+          return grub_errno;
+        }
+
+      grub_free (old);
+      return GRUB_ERR_NONE;
+    }
+
+  return grub_env_set (name, val);
+}
+
 const char *
 grub_env_get (const char *name)
 {
diff --git a/include/grub/env.h b/include/grub/env.h
index 6b9379a30..e62786006 100644
--- a/include/grub/env.h
+++ b/include/grub/env.h
@@ -44,6 +44,7 @@ struct grub_env_var
 };
 
 grub_err_t EXPORT_FUNC(grub_env_set) (const char *name, const char *val);
+grub_err_t EXPORT_FUNC(grub_env_append) (const char *name, const char *val);
 const char *EXPORT_FUNC(grub_env_get) (const char *name);
 bool EXPORT_FUNC(grub_env_get_bool) (const char *name, bool if_unset);
 void EXPORT_FUNC(grub_env_unset) (const char *name);
-- 
2.45.1


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

  reply	other threads:[~2024-06-30 16:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-30 16:25 [RFC][PATCH v1 0/4] How to add --set=VARNAME to the ls command? Denis 'GNUtoo' Carikli
2024-06-30 16:25 ` Denis 'GNUtoo' Carikli [this message]
2024-06-30 16:25 ` [RFC][PATCH v1 2/4] Add command to append to existing environment variables Denis 'GNUtoo' Carikli
2024-06-30 16:25 ` [RFC][PATCH v1 3/4] commands/ls: add --set=VARNAME Denis 'GNUtoo' Carikli
2024-06-30 16:25 ` [RFC][PATCH v1 4/4] commands/ls: support --set for files/directories Denis 'GNUtoo' Carikli
2024-06-30 17:05 ` [RFC][PATCH v1 0/4] How to add --set=VARNAME to the ls command? Vladimir 'phcoder' Serbinenko
2024-06-30 22:44   ` Denis 'GNUtoo' Carikli

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=20240630162519.25173-2-GNUtoo@cyberdimension.org \
    --to=gnutoo@cyberdimension.org \
    --cc=grub-devel@gnu.org \
    --cc=neox@gnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.