diff mbox series

[3/5] cmd: Convert echo to use cmd_result

Message ID 20210228234718.1208376-4-seanga2@gmail.com
State New
Delegated to: Tom Rini
Headers show
Series cmd: Add support for command substitution | expand

Commit Message

Sean Anderson Feb. 28, 2021, 11:47 p.m. UTC
This adds some complexity, since we need to know how big the arguments are
ahead of time instead of finding out when we print them.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 cmd/echo.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

Comments

Heinrich Schuchardt March 1, 2021, 12:01 a.m. UTC | #1
Am 1. März 2021 00:47:16 MEZ schrieb Sean Anderson <seanga2@gmail.com>:
>This adds some complexity, since we need to know how big the arguments
>are
>ahead of time instead of finding out when we print them.

Why do you want to add complexity?

Where is the documentation update?

Best regards

Heinrich


>
>Signed-off-by: Sean Anderson <seanga2@gmail.com>
>---
>
> cmd/echo.c | 33 ++++++++++++++++++++++++++-------
> 1 file changed, 26 insertions(+), 7 deletions(-)
>
>diff --git a/cmd/echo.c b/cmd/echo.c
>index fda844ee9d..8f20e635ce 100644
>--- a/cmd/echo.c
>+++ b/cmd/echo.c
>@@ -6,11 +6,16 @@
> 
> #include <common.h>
> #include <command.h>
>+#include <malloc.h>
>+
>+DECLARE_GLOBAL_DATA_PTR;
> 
> static int do_echo(struct cmd_tbl *cmdtp, int flag, int argc,
> 		   char *const argv[])
> {
>-	int i = 1;
>+	char *result;
>+	int j, i = 1;
>+	size_t result_size, arglens[CONFIG_SYS_MAXARGS];
> 	bool space = false;
> 	bool newline = true;
> 
>@@ -21,18 +26,32 @@ static int do_echo(struct cmd_tbl *cmdtp, int flag,
>int argc,
> 		}
> 	}
> 
>+	result_size = 1 + newline; /* \0 + \n */
>+	result_size += argc - i - 1; /* spaces */
>+	for (j = i; j < argc; ++j) {
>+		arglens[j] = strlen(argv[j]);
>+		result_size += arglens[j];
>+	}
>+
>+	result = malloc(result_size);
>+	if (!result)
>+		return CMD_RET_FAILURE;
>+	gd->cmd_result = result;
>+
> 	for (; i < argc; ++i) {
>-		if (space) {
>-			putc(' ');
>-		}
>-		puts(argv[i]);
>+		if (space)
>+			*result++ = ' ';
>+
>+		memcpy(result, argv[i], arglens[i]);
>+		result += arglens[i];
> 		space = true;
> 	}
> 
> 	if (newline)
>-		putc('\n');
>+		*result++ = '\n';
>+	*result = '\0';
> 
>-	return 0;
>+	return CMD_RET_SUCCESS;
> }
> 
> U_BOOT_CMD(
diff mbox series

Patch

diff --git a/cmd/echo.c b/cmd/echo.c
index fda844ee9d..8f20e635ce 100644
--- a/cmd/echo.c
+++ b/cmd/echo.c
@@ -6,11 +6,16 @@ 
 
 #include <common.h>
 #include <command.h>
+#include <malloc.h>
+
+DECLARE_GLOBAL_DATA_PTR;
 
 static int do_echo(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[])
 {
-	int i = 1;
+	char *result;
+	int j, i = 1;
+	size_t result_size, arglens[CONFIG_SYS_MAXARGS];
 	bool space = false;
 	bool newline = true;
 
@@ -21,18 +26,32 @@  static int do_echo(struct cmd_tbl *cmdtp, int flag, int argc,
 		}
 	}
 
+	result_size = 1 + newline; /* \0 + \n */
+	result_size += argc - i - 1; /* spaces */
+	for (j = i; j < argc; ++j) {
+		arglens[j] = strlen(argv[j]);
+		result_size += arglens[j];
+	}
+
+	result = malloc(result_size);
+	if (!result)
+		return CMD_RET_FAILURE;
+	gd->cmd_result = result;
+
 	for (; i < argc; ++i) {
-		if (space) {
-			putc(' ');
-		}
-		puts(argv[i]);
+		if (space)
+			*result++ = ' ';
+
+		memcpy(result, argv[i], arglens[i]);
+		result += arglens[i];
 		space = true;
 	}
 
 	if (newline)
-		putc('\n');
+		*result++ = '\n';
+	*result = '\0';
 
-	return 0;
+	return CMD_RET_SUCCESS;
 }
 
 U_BOOT_CMD(