mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 2/3] env: add envvar_for_each helper for variable iteration
Date: Thu, 20 Mar 2025 06:20:58 +0100	[thread overview]
Message-ID: <20250320052059.1732184-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250320052059.1732184-1-a.fatoum@pengutronix.de>

Instead of opencoding the iteration where needed, implement a helper
invokes a callback on every variable.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 common/env.c          | 37 +++++++++++++++++++++++++++++++++++++
 include/environment.h |  7 +++++++
 2 files changed, 44 insertions(+)

diff --git a/common/env.c b/common/env.c
index e34e3ac078d6..fd01178aee54 100644
--- a/common/env.c
+++ b/common/env.c
@@ -123,6 +123,21 @@ static const char *getenv_raw(struct list_head *l, const char *name)
 	return NULL;
 }
 
+static int getenv_raw_call(int (*fn)(struct variable_d *v, void *data),
+			   struct list_head *l, void *data)
+{
+	struct variable_d *v;
+	int ret;
+
+	list_for_each_entry(v, l, list) {
+		ret = fn(v, data);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static const char *dev_getenv(const char *name)
 {
 	const char *pos, *val, *dot, *varname;
@@ -179,6 +194,28 @@ const char *getenv(const char *name)
 }
 EXPORT_SYMBOL(getenv);
 
+int envvar_for_each(int (*fn)(struct variable_d *v, void *data), void *data)
+{
+	struct env_context *c;
+	int ret;
+
+	c = context;
+
+	ret = getenv_raw_call(fn, &c->local, data);
+	if (ret)
+		return ret;
+
+	while (c) {
+		ret = getenv_raw_call(fn, &c->global, data);
+		if (ret)
+			return ret;
+		c = c->parent;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(envvar_for_each);
+
 static int setenv_raw(struct list_head *l, const char *name, const char *value)
 {
 	struct variable_d *v;
diff --git a/include/environment.h b/include/environment.h
index f26c68ba3e34..c24796427675 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -31,6 +31,7 @@ const char *var_name(struct variable_d *);
 
 #if IS_ENABLED(CONFIG_ENVIRONMENT_VARIABLES) && IN_PROPER
 const char *getenv(const char *);
+int envvar_for_each(int (*fn)(struct variable_d *v, void *data), void *data);
 int setenv(const char *, const char *);
 int pr_setenv(const char *, const char *fmt, ...)  __attribute__ ((format(__printf__, 2, 3)));
 void export_env_ull(const char *name, unsigned long long val);
@@ -46,6 +47,12 @@ static inline char *getenv(const char *var)
 	return NULL;
 }
 
+static inline int envvar_for_each(int (*fn)(struct list_head *l, void *data),
+				  void *data)
+{
+	return 0;
+}
+
 static inline int setenv(const char *var, const char *val)
 {
 	return 0;
-- 
2.39.5




  reply	other threads:[~2025-03-20  5:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20  5:20 [PATCH v2 1/3] complete: give device_param_complete a flags parameter Ahmad Fatoum
2025-03-20  5:20 ` Ahmad Fatoum [this message]
2025-03-20  5:20 ` [PATCH v2 3/3] commands: add varinfo command Ahmad Fatoum

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=20250320052059.1732184-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox