Skip to content
Snippets Groups Projects
Commit 7d85591d authored by Wolfgang Denk's avatar Wolfgang Denk Committed by Tom Rini
Browse files

env: fix "env ask" command


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,
and clean up help mesage.

Signed-off-by: default avatarWolfgang Denk <wd@denx.de>
parent 01fac041
No related branches found
No related tags found
No related merge requests found
...@@ -325,41 +325,50 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) ...@@ -325,41 +325,50 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{ {
char message[CONFIG_SYS_CBSIZE]; char message[CONFIG_SYS_CBSIZE];
int size = CONFIG_SYS_CBSIZE - 1; int i, len, pos, size;
int i, len, pos;
char *local_args[4]; char *local_args[4];
char *endptr;
local_args[0] = argv[0]; local_args[0] = argv[0];
local_args[1] = argv[1]; local_args[1] = argv[1];
local_args[2] = NULL; local_args[2] = NULL;
local_args[3] = NULL; local_args[3] = NULL;
/* Check the syntax */ /*
switch (argc) { * Check the syntax:
case 1: *
* env_ask envname [message1 ...] [size]
*/
if (argc == 1)
return CMD_RET_USAGE; return CMD_RET_USAGE;
case 2: /* env_ask envname */ /*
sprintf(message, "Please enter '%s':", argv[1]); * We test the last argument if it can be converted
break; * into a decimal number. If yes, we assume it's
* the size. Otherwise we echo it as part of the
case 3: /* env_ask envname size */ * message.
sprintf(message, "Please enter '%s':", argv[1]); */
size = simple_strtoul(argv[2], NULL, 10); i = simple_strtoul(argv[argc - 1], &endptr, 10);
break; if (*endptr != '\0') { /* no size */
size = CONFIG_SYS_CBSIZE - 1;
} else { /* size given */
size = i;
--argc;
}
default: /* env_ask envname message1 ... messagen size */ if (argc <= 2) {
for (i = 2, pos = 0; i < argc - 1; i++) { sprintf(message, "Please enter '%s': ", argv[1]);
} else {
/* env_ask envname message1 ... messagen [size] */
for (i = 2, pos = 0; i < argc; i++) {
if (pos) if (pos)
message[pos++] = ' '; message[pos++] = ' ';
strcpy(message + pos, argv[i]); strcpy(message + pos, argv[i]);
pos += strlen(argv[i]); pos += strlen(argv[i]);
} }
message[pos++] = ' ';
message[pos] = '\0'; message[pos] = '\0';
size = simple_strtoul(argv[argc - 1], NULL, 10);
break;
} }
if (size >= CONFIG_SYS_CBSIZE) if (size >= CONFIG_SYS_CBSIZE)
...@@ -1168,14 +1177,7 @@ U_BOOT_CMD( ...@@ -1168,14 +1177,7 @@ U_BOOT_CMD(
askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
"get environment variables from stdin", "get environment variables from stdin",
"name [message] [size]\n" "name [message] [size]\n"
" - get environment variable 'name' from stdin (max 'size' chars)\n" " - get environment variable 'name' from stdin (max 'size' chars)"
"askenv name\n"
" - get environment variable 'name' from stdin\n"
"askenv name size\n"
" - get environment variable 'name' from stdin (max 'size' chars)\n"
"askenv name [message] size\n"
" - display 'message' string and get environment variable 'name'"
"from stdin (max 'size' chars)"
); );
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment