[PATCH v2 1/3] commands: devlookup: factor out cmd_export_val

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Apr 4 11:23:43 PDT 2025


Until we have support for capturing command output into a variable,
future commands will continue to have a -v parameter or similar for
printing the output of a command into a variable.

Add a helper for this that either prints it to the variable or to
stdout. This will allow easy migration to command output capture once we
have it.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/devlookup.c | 16 ++--------------
 common/command.c     | 13 +++++++++++++
 include/command.h    |  2 ++
 3 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/commands/devlookup.c b/commands/devlookup.c
index bc9bd9461443..4a2c9b773661 100644
--- a/commands/devlookup.c
+++ b/commands/devlookup.c
@@ -9,18 +9,6 @@
 #include <linux/ctype.h>
 #include <environment.h>
 
-static int report(const char *variable, const char *val)
-{
-	if (!val)
-		return -(errno ?: EINVAL);
-
-	if (variable)
-		return setenv(variable, val);
-
-	printf("%s\n", val);
-	return 0;
-}
-
 static int do_devlookup(int argc, char *argv[])
 {
 	const char *variable = NULL, *devicefile, *paramname;
@@ -56,9 +44,9 @@ static int do_devlookup(int argc, char *argv[])
 	}
 
 	if (paramname)
-		ret = report(variable, dev_get_param(cdev->dev, paramname));
+		ret = cmd_export_val(variable, dev_get_param(cdev->dev, paramname));
 	else
-		ret = report(variable, dev_name(cdev->dev));
+		ret = cmd_export_val(variable, dev_name(cdev->dev));
 out:
 	cdev_close(cdev);
 
diff --git a/common/command.c b/common/command.c
index 014b85f9a35f..a119b0f41b3a 100644
--- a/common/command.c
+++ b/common/command.c
@@ -128,6 +128,19 @@ struct command *find_cmd (const char *cmd)
 }
 EXPORT_SYMBOL(find_cmd);
 
+int cmd_export_val(const char *variable, const char *val)
+{
+	if (!val)
+		return -EINVAL;
+
+	if (variable)
+		return setenv(variable, val);
+
+	printf("%s\n", val);
+	return 0;
+}
+EXPORT_SYMBOL(cmd_export_val);
+
 /*
  * Put all commands into a linked list. Without module support we could use
  * the raw command array, but with module support a list is easier to handle.
diff --git a/include/command.h b/include/command.h
index 26e06077b071..03ac270d1305 100644
--- a/include/command.h
+++ b/include/command.h
@@ -52,6 +52,7 @@ extern struct command * const __barebox_cmd_end[];
 /* common/command.c */
 #ifdef CONFIG_COMMAND_SUPPORT
 struct command *find_cmd(const char *cmd);
+int cmd_export_val(const char *variable, const char *val);
 int execute_command(int argc, char **argv);
 void barebox_cmd_usage(struct command *cmdtp);
 int run_command(const char *cmd);
@@ -59,6 +60,7 @@ int run_command(const char *cmd);
 static inline struct command *find_cmd(const char *cmd) { return NULL; }
 static inline int execute_command(int argc, char **argv) { return -ENOSYS; }
 static inline void barebox_cmd_usage(struct command *cmdtp) {}
+static inline int cmd_export_val(const char *variable, const char *val) { return -ENOSYS; }
 static inline int run_command(const char *cmd) { return -ENOSYS; }
 #endif
 
-- 
2.39.5




More information about the barebox mailing list