[U-Boot] [PATCH] env: fix "env ask" command

Wolfgang Denk wd at denx.de
Sun Nov 6 20:52:52 CET 2011


The "env ask" traditionally uses a somewhat awkward syntax:

	env ask name [message ...] [size]

So far, when a mesage was given, you always also had to enter a size.
If you forgot to do that, the command would terminate without any
indication of the problem.

To avoid incompatible changes of the interface, we now check the last
argument if it can be converted into a decimal number.  If this is the
case, we assume it is a size; otherwise we treat it as part of the
message.

Also, add a space after the message fore easier reading.

Signed-off-by: Wolfgang Denk <wd at denx.de>
---
Note: this patch was generated on top of Gerlando Falauto's
"generalization of env interface" patch series.  THis should be
applied before.

 common/cmd_nvedit.c |   46 ++++++++++++++++++++++++++++------------------
 1 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 5507922..2dd4eba 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -465,40 +465,50 @@ int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	extern char console_buffer[CONFIG_SYS_CBSIZE];
 	char message[CONFIG_SYS_CBSIZE];
-	int size = CONFIG_SYS_CBSIZE - 1;
-	int i, len, pos;
+	int i, len, pos, size;
 	char *local_args[4];
+	char *endptr;
 
 	local_args[0] = argv[0];
 	local_args[1] = argv[1];
 	local_args[2] = NULL;
 	local_args[3] = NULL;
 
-	/* Check the syntax */
-	switch (argc) {
-	case 1:
+	/*
+	 * Check the syntax:
+	 *
+	 * env_ask envname [message1 ...] [size]
+	 */
+	if (argc == 1)
 		return cmd_usage(cmdtp);
 
-	case 2:		/* env_ask envname */
-		sprintf(message, "Please enter '%s':", argv[1]);
-		break;
-
-	case 3:		/* env_ask envname size */
-		sprintf(message, "Please enter '%s':", argv[1]);
-		size = simple_strtoul(argv[2], NULL, 10);
-		break;
-
-	default:	/* env_ask envname message1 ... messagen size */
-		for (i = 2, pos = 0; i < argc - 1; i++) {
+	/*
+	 * We test the last argument if it can be converted
+	 * into a decimal number.  If yes, we assume it's
+	 * the size.  Otherwise we echo it as part of the
+	 * message.
+	 */
+	i = simple_strtoul(argv[argc - 1], &endptr, 10);
+	if (*endptr != '\0') {			/* no size */
+		size = CONFIG_SYS_CBSIZE - 1;
+	} else {				/* size given */
+		size = i;
+		--argc;
+	}
+	
+	if (argc <= 2) {
+		sprintf(message, "Please enter '%s': ", argv[1]);
+	} else {
+		/* env_ask envname message1 ... messagen [size] */
+		for (i = 2, pos = 0; i < argc; i++) {
 			if (pos)
 				message[pos++] = ' ';
 
 			strcpy(message+pos, argv[i]);
 			pos += strlen(argv[i]);
 		}
+		message[pos++] = ' ';
 		message[pos] = '\0';
-		size = simple_strtoul(argv[argc - 1], NULL, 10);
-		break;
 	}
 
 	if (size >= CONFIG_SYS_CBSIZE)
-- 
1.7.6.4



More information about the U-Boot mailing list