Propagate Error Status to the Shell on fw_printenv Errors

Changed implementation such that fw_printenv returns failure status
when one or more specified variables do not exist or when incorrect
command syntax is used.

This aids scripting fw_printenv such that the script can key of the
return status rather than relying on standard error "scraping".

Signed-off-by: Grant Erickson <gerickson@nuovations.com>
Signed-off-by: Wolfgang Denk <wd@denx.de>
This commit is contained in:
Grant Erickson 2008-05-06 20:16:15 -07:00 committed by Wolfgang Denk
parent f3b6d528e4
commit bc11756daf
3 changed files with 29 additions and 21 deletions

21
tools/env/fw_env.c vendored
View File

@ -1,5 +1,5 @@
/* /*
* (C) Copyright 2000-2003 * (C) Copyright 2000-2008
* Wolfgang Denk, DENX Software Engineering, wd@denx.de. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
* *
* See file CREDITS for list of people who contributed to this * See file CREDITS for list of people who contributed to this
@ -209,13 +209,14 @@ char *fw_getenv (char *name)
* Print the current definition of one, or more, or all * Print the current definition of one, or more, or all
* environment variables * environment variables
*/ */
void fw_printenv (int argc, char *argv[]) int fw_printenv (int argc, char *argv[])
{ {
char *env, *nxt; char *env, *nxt;
int i, n_flag; int i, n_flag;
int rc = 0;
if (env_init ()) if (env_init ())
return; return (-1);
if (argc == 1) { /* Print all env variables */ if (argc == 1) { /* Print all env variables */
for (env = environment.data; *env; env = nxt + 1) { for (env = environment.data; *env; env = nxt + 1) {
@ -223,13 +224,13 @@ void fw_printenv (int argc, char *argv[])
if (nxt >= &environment.data[ENV_SIZE]) { if (nxt >= &environment.data[ENV_SIZE]) {
fprintf (stderr, "## Error: " fprintf (stderr, "## Error: "
"environment not terminated\n"); "environment not terminated\n");
return; return (-1);
} }
} }
printf ("%s\n", env); printf ("%s\n", env);
} }
return; return (0);
} }
if (strcmp (argv[1], "-n") == 0) { if (strcmp (argv[1], "-n") == 0) {
@ -239,7 +240,7 @@ void fw_printenv (int argc, char *argv[])
if (argc != 2) { if (argc != 2) {
fprintf (stderr, "## Error: " fprintf (stderr, "## Error: "
"`-n' option requires exactly one argument\n"); "`-n' option requires exactly one argument\n");
return; return (-1);
} }
} else { } else {
n_flag = 0; n_flag = 0;
@ -255,7 +256,7 @@ void fw_printenv (int argc, char *argv[])
if (nxt >= &environment.data[ENV_SIZE]) { if (nxt >= &environment.data[ENV_SIZE]) {
fprintf (stderr, "## Error: " fprintf (stderr, "## Error: "
"environment not terminated\n"); "environment not terminated\n");
return; return (-1);
} }
} }
val = envmatch (name, env); val = envmatch (name, env);
@ -268,9 +269,13 @@ void fw_printenv (int argc, char *argv[])
break; break;
} }
} }
if (!val) if (!val) {
fprintf (stderr, "## Error: \"%s\" not defined\n", name); fprintf (stderr, "## Error: \"%s\" not defined\n", name);
rc = -1;
}
} }
return (rc);
} }
/* /*

4
tools/env/fw_env.h vendored
View File

@ -1,5 +1,5 @@
/* /*
* (C) Copyright 2002 * (C) Copyright 2002-2008
* Wolfgang Denk, DENX Software Engineering, wd@denx.de. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
* *
* See file CREDITS for list of people who contributed to this * See file CREDITS for list of people who contributed to this
@ -47,7 +47,7 @@
"ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; " \ "ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; " \
"bootm" "bootm"
extern void fw_printenv(int argc, char *argv[]); extern int fw_printenv(int argc, char *argv[]);
extern char *fw_getenv (char *name); extern char *fw_getenv (char *name);
extern int fw_setenv (int argc, char *argv[]); extern int fw_setenv (int argc, char *argv[]);

View File

@ -1,5 +1,5 @@
/* /*
* (C) Copyright 2000 * (C) Copyright 2000-2008
* Wolfgang Denk, DENX Software Engineering, wd@denx.de. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
* *
* See file CREDITS for list of people who contributed to this * See file CREDITS for list of people who contributed to this
@ -25,15 +25,16 @@
* Command line user interface to firmware (=U-Boot) environment. * Command line user interface to firmware (=U-Boot) environment.
* *
* Implements: * Implements:
* fw_printenv [ name ... ] * fw_printenv [[ -n name ] | [ name ... ]]
* - prints the values of the environment variables * - prints the value of a single environment variable
* "name", or the whole environment if no names are * "name", the ``name=value'' pairs of one or more
* specified * environment variables "name", or the whole
* environment if no names are specified.
* fw_setenv name [ value ... ] * fw_setenv name [ value ... ]
* - If a name without any values is given, the variable * - If a name without any values is given, the variable
* with this name is deleted from the environment; * with this name is deleted from the environment;
* otherwise, all "value" arguments are concatenated, * otherwise, all "value" arguments are concatenated,
* separated by sinlge blank characters, and the * separated by single blank characters, and the
* resulting string is assigned to the environment * resulting string is assigned to the environment
* variable "name" * variable "name"
*/ */
@ -58,16 +59,18 @@ main(int argc, char *argv[])
if (strcmp(cmdname, CMD_PRINTENV) == 0) { if (strcmp(cmdname, CMD_PRINTENV) == 0) {
fw_printenv (argc, argv); if (fw_printenv (argc, argv) != 0)
return (EXIT_FAILURE);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} else if (strcmp(cmdname, CMD_SETENV) == 0) { } else if (strcmp(cmdname, CMD_SETENV) == 0) {
if (fw_setenv (argc, argv) != 0) if (fw_setenv (argc, argv) != 0)
return (EXIT_FAILURE); return (EXIT_FAILURE);
return (EXIT_SUCCESS);
return (EXIT_SUCCESS);
} }
fprintf (stderr, fprintf (stderr,