mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	 BAREBOX <barebox@lists.infradead.org>
Cc: Marco Felsch <m.felsch@pengutronix.de>
Subject: [PATCH v2 5/7] commands: make run_command variadic
Date: Thu, 12 Feb 2026 23:02:07 +0100	[thread overview]
Message-ID: <20260212-vmaster-customers-leicageo-system1600-v2-5-f590b7d5b0e9@pengutronix.de> (raw)
In-Reply-To: <20260212-vmaster-customers-leicageo-system1600-v2-0-f590b7d5b0e9@pengutronix.de>

This prepares run_command() to receive a format string and variadic
arguments which can become quite handy for board code. It also ensures
a proper cleanup for the command string and generalizes the error
message printing.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 common/hush.c     | 23 ++++++++++++++++++++++-
 common/parser.c   | 24 +++++++++++++++++++++++-
 include/command.h |  4 ++--
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/common/hush.c b/common/hush.c
index 2e0cc4229d3575dcf045f68ef8db3e943c41eedb..b46a42cc90c863376bb819755afc83271120200f 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -1922,7 +1922,7 @@ static char * make_string(char ** inp)
 	return str;
 }
 
-int run_command(const char *cmd)
+static int __run_command(const char *cmd)
 {
 	struct p_context ctx = {};
 	int ret;
@@ -1938,6 +1938,27 @@ int run_command(const char *cmd)
 	return ret;
 }
 
+int run_command(const char *fmt, ...)
+{
+	va_list vargs;
+	char *cmd;
+	int error;
+
+	va_start(vargs, fmt);
+	cmd = xvasprintf(fmt, vargs);
+	va_end(vargs);
+
+	error = __run_command(cmd);
+	if (error) {
+		error_msg("Failed to run CMD: '%s' with %d\n", cmd, error);
+		free(cmd);
+		return error;
+	}
+
+	free(cmd);
+	return 0;
+}
+
 static int execute_script(const char *path, int argc, char *argv[])
 {
 	int ret;
diff --git a/common/parser.c b/common/parser.c
index 16fff052cf63b7a0e237bc2de1188b27af1b9809..c8b247716af9da6e1ccb2b3a915c87784a6d6eda 100644
--- a/common/parser.c
+++ b/common/parser.c
@@ -6,6 +6,7 @@
 #include <environment.h>
 #include <shell.h>
 #include <security/config.h>
+#include <stdio.h>
 
 /*
  * not yet supported
@@ -180,7 +181,7 @@ static void process_macros (const char *input, char *output)
  * creates or modifies environment variables (like "bootp" does).
  */
 
-int run_command(const char *cmd)
+static int __run_command(const char *cmd)
 {
 	char cmdbuf[CONFIG_CBSIZE];	/* working copy of cmd		*/
 	char *token;			/* start of token in cmdbuf	*/
@@ -266,6 +267,27 @@ int run_command(const char *cmd)
 	return rc;
 }
 
+int run_command(const char *fmt, ...)
+{
+	va_list vargs;
+	char *cmd;
+	int error;
+
+	va_start(vargs, fmt);
+	cmd = xvasprintf(fmt, vargs);
+	va_end(vargs);
+
+	error = __run_command(cmd);
+	if (error) {
+		pr_err("Failed to run CMD: '%s' with %d\n", cmd, error);
+		free(cmd);
+		return error;
+	}
+
+	free(cmd);
+	return 0;
+}
+
 static char console_buffer[CONFIG_CBSIZE];		/* console I/O buffer	*/
 
 int run_shell(void)
diff --git a/include/command.h b/include/command.h
index fb140cb8e250ab620e3b3284ad2b79520d194dce..5c88a9d611667d81eca105bc7b5ff7c61e0e31e0 100644
--- a/include/command.h
+++ b/include/command.h
@@ -55,13 +55,13 @@ 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);
+__printf(1, 2) int run_command(const char *fmt, ...);
 #else
 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; }
+static inline __printf(1, 2) int run_command(const char *fmt, ...) { return -ENOSYS; }
 #endif
 
 #define COMMAND_SUCCESS		0

-- 
2.47.3




  parent reply	other threads:[~2026-02-12 22:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-12 22:02 [PATCH v2 0/7] Hexagon Geosystems GS05 Board Support Marco Felsch
2026-02-12 22:02 ` [PATCH v2 1/7] serdev: add proper error cleanup to serdev_device_open() Marco Felsch
2026-02-12 22:02 ` [PATCH v2 2/7] serdev: add serdev_device_close Marco Felsch
2026-02-12 22:02 ` [PATCH v2 3/7] mfd: Add Hexagon EFI driver Marco Felsch
2026-02-12 22:02 ` [PATCH v2 4/7] watchdog: Add Hexagon EFI watchdog driver Marco Felsch
2026-02-12 22:02 ` Marco Felsch [this message]
2026-02-12 22:02 ` [PATCH v2 6/7] treewide: make use of new run_command variadic Marco Felsch
2026-02-12 22:02 ` [PATCH v2 7/7] ARM: i.MX8MM: add Hexagon Geosystems GS05 Marco Felsch

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=20260212-vmaster-customers-leicageo-system1600-v2-5-f590b7d5b0e9@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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