All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v2 11/12] cli: Support macro processing with a fixed-size buffer
Date: Thu,  5 Nov 2020 10:33:47 -0700	[thread overview]
Message-ID: <20201105173349.903603-12-sjg@chromium.org> (raw)
In-Reply-To: <20201105173349.903603-1-sjg@chromium.org>

At present cli_simple_process_macros() requires that the caller provide
an output buffer that is exactly CONFIG_SYS_CBSIZE bytes in length. This
makes sense since it is designed to be used from the command line. But we
also want to use it for bootargs substitution.

Update the function to allow the caller to specify the buffer size. Also
return an error if the buffer is exhausted. The caller can ignore that if
preferred.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2:
- Add a new patch to support macro processing with a fixed-size buffer

 cmd/pxe_utils.c     |  6 ++++--
 common/cli_simple.c | 17 ++++++++++++-----
 include/cli.h       |  4 +++-
 3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/cmd/pxe_utils.c b/cmd/pxe_utils.c
index 8716e782f6a..69cac149c0c 100644
--- a/cmd/pxe_utils.c
+++ b/cmd/pxe_utils.c
@@ -322,7 +322,8 @@ static int label_localboot(struct pxe_label *label)
 	if (label->append) {
 		char bootargs[CONFIG_SYS_CBSIZE];
 
-		cli_simple_process_macros(label->append, bootargs);
+		cli_simple_process_macros(label->append, bootargs,
+					  sizeof(bootargs));
 		env_set("bootargs", bootargs);
 	}
 
@@ -430,7 +431,8 @@ static int label_boot(struct cmd_tbl *cmdtp, struct pxe_label *label)
 		strcat(bootargs, ip_str);
 		strcat(bootargs, mac_str);
 
-		cli_simple_process_macros(bootargs, finalbootargs);
+		cli_simple_process_macros(bootargs, finalbootargs,
+					  sizeof(finalbootargs));
 		env_set("bootargs", finalbootargs);
 		printf("append: %s\n", finalbootargs);
 	}
diff --git a/common/cli_simple.c b/common/cli_simple.c
index 7d91316a0fb..e80ba488a5e 100644
--- a/common/cli_simple.c
+++ b/common/cli_simple.c
@@ -60,13 +60,14 @@ int cli_simple_parse_line(char *line, char *argv[])
 	return nargs;
 }
 
-void cli_simple_process_macros(const char *input, char *output)
+int cli_simple_process_macros(const char *input, char *output, int max_size)
 {
 	char c, prev;
 	const char *varname_start = NULL;
 	int inputcnt = strlen(input);
-	int outputcnt = CONFIG_SYS_CBSIZE;
+	int outputcnt = max_size;
 	int state = 0;		/* 0 = waiting for '$'  */
+	int ret;
 
 	/* 1 = waiting for '(' or '{' */
 	/* 2 = waiting for ')' or '}' */
@@ -157,13 +158,18 @@ void cli_simple_process_macros(const char *input, char *output)
 		prev = c;
 	}
 
-	if (outputcnt)
+	ret = inputcnt ? -ENOSPC : 0;
+	if (outputcnt) {
 		*output = 0;
-	else
+	} else {
 		*(output - 1) = 0;
+		ret = -ENOSPC;
+	}
 
 	debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n",
 		     strlen(output_start), output_start);
+
+	return ret;
 }
 
  /*
@@ -239,7 +245,8 @@ int cli_simple_run_command(const char *cmd, int flag)
 		debug_parser("token: \"%s\"\n", token);
 
 		/* find macros in this token and replace them */
-		cli_simple_process_macros(token, finaltoken);
+		cli_simple_process_macros(token, finaltoken,
+					  sizeof(finaltoken));
 
 		/* Extract arguments */
 		argc = cli_simple_parse_line(finaltoken, argv);
diff --git a/include/cli.h b/include/cli.h
index 39b913743b5..3449fa6ae72 100644
--- a/include/cli.h
+++ b/include/cli.h
@@ -34,8 +34,10 @@ int cli_simple_run_command(const char *cmd, int flag);
  *
  * @param input		Input string possible containing $() / ${} vars
  * @param output	Output string with $() / ${} vars expanded
+ * @param max_size	Maximum size of @output (including terminator)
+ * @return 0 if OK, -ENOSPC if we ran out of space in @output
  */
-void cli_simple_process_macros(const char *input, char *output);
+int cli_simple_process_macros(const char *input, char *output, int max_size);
 
 /**
  * cli_simple_run_command_list() - Execute a list of command
-- 
2.29.1.341.ge80a0c044ae-goog

  parent reply	other threads:[~2020-11-05 17:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-05 17:33 [PATCH v2 00/12] bootm: Support substitions in bootargs and add tests Simon Glass
2020-11-05 17:33 ` [PATCH v2 01/12] env: Allow returning errors from hdelete_r() Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 02/12] bootm: Add tests for fixup_silent_linux() Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 03/12] bootm: Update fixup_silent_linux() to return an error Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 04/12] bootm: Rename fixup_silent_linux() Simon Glass
2020-12-07 22:18   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 05/12] bootm: Add a bool parameter to bootm_process_cmdline_env() Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 06/12] bootm: Use size rather than length for CONSOLE_ARG Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 07/12] bootm: Split out bootargs environment reading / writing Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 08/12] bootm: Update bootm_process_cmdline_env() to use flags Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 09/12] bootm: Allow updating the bootargs in a buffer Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` [PATCH v2 10/12] x86: zimage: Add silent-console processing Simon Glass
2020-12-07 22:19   ` Tom Rini
2020-11-05 17:33 ` Simon Glass [this message]
2020-12-07 22:19   ` [PATCH v2 11/12] cli: Support macro processing with a fixed-size buffer Tom Rini
2020-11-05 17:33 ` [PATCH v2 12/12] bootm: Support string substitution in bootargs Simon Glass
2020-12-07 22:19   ` Tom Rini

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=20201105173349.903603-12-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.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 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.