[U-Boot-Users] [PATCH] Propagate Error Status to the Shell on fw_printenv Errors

Grant Erickson gerickson at nuovations.com
Wed May 7 05:16:15 CEST 2008


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".

  Tested On:
    AMCC Kilauea

  Tests Run:
	root at kilauea# fw_printenv
	bootdelay=5
	baudrate=115200
	...
	root at kilauea# echo $?
	0
	
	root at kilauea# fw_printenv -n hostname
	kilauea
	root at kilauea# echo $?
	0
	
	root at kilauea# fw_printenv -n hostname ipaddr
	## Error: `-n' option requires exactly one argument
	root at kilauea# echo $?
	1
	
	root at kilauea# fw_printenv hostname ipaddr
	hostname=kilauea
	ipaddr=192.168.1.12
	root at kilauea# echo $?
	0
	
	root at kilauea# fw_printenv foobar
	## Error: "foobar" not defined
	root at kilauea# echo $?
	1 

	root at kilauea# fw_printenv hostname ipaddr foobar
	hostname=kilauea
	ipaddr=192.168.1.12
	## Error: "foobar" not defined
	root at kilauea# echo $?
	1

Signed-off-by: Grant Erickson <gerickson at nuovations.com>
---
 tools/env/fw_env.c      |   19 ++++++++++++-------
 tools/env/fw_env.h      |    2 +-
 tools/env/fw_env_main.c |   30 +++++++++++++++---------------
 3 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index e083a5b..d06844c 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -209,13 +209,14 @@ char *fw_getenv (char *name)
  * Print the current definition of one, or more, or all
  * environment variables
  */
-void fw_printenv (int argc, char *argv[])
+int fw_printenv (int argc, char *argv[])
 {
 	char *env, *nxt;
 	int i, n_flag;
+	int errflag = 0;
 
 	if (env_init ())
-		return;
+		return (-1);
 
 	if (argc == 1) {		/* Print all env variables  */
 		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]) {
 					fprintf (stderr, "## Error: "
 						"environment not terminated\n");
-					return;
+					return (-1);
 				}
 			}
 
 			printf ("%s\n", env);
 		}
-		return;
+		return (0);
 	}
 
 	if (strcmp (argv[1], "-n") == 0) {
@@ -239,7 +240,7 @@ void fw_printenv (int argc, char *argv[])
 		if (argc != 2) {
 			fprintf (stderr, "## Error: "
 				"`-n' option requires exactly one argument\n");
-			return;
+			return (-1);
 		}
 	} else {
 		n_flag = 0;
@@ -255,7 +256,7 @@ void fw_printenv (int argc, char *argv[])
 				if (nxt >= &environment.data[ENV_SIZE]) {
 					fprintf (stderr, "## Error: "
 						"environment not terminated\n");
-					return;
+					return (-1);
 				}
 			}
 			val = envmatch (name, env);
@@ -268,9 +269,13 @@ void fw_printenv (int argc, char *argv[])
 				break;
 			}
 		}
-		if (!val)
+		if (!val) {
 			fprintf (stderr, "## Error: \"%s\" not defined\n", name);
+			errflag++;
+		}
 	}
+
+	return (errflag ? -1 : 0);
 }
 
 /*
diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h
index 58607de..231dd86 100644
--- a/tools/env/fw_env.h
+++ b/tools/env/fw_env.h
@@ -47,7 +47,7 @@
 	"ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; " 	\
 	"bootm"
 
-extern void  fw_printenv(int argc, char *argv[]);
+extern int fw_printenv(int argc, char *argv[]);
 extern char *fw_getenv  (char *name);
 extern int fw_setenv  (int argc, char *argv[]);
 
diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
index 696e30e..f50b774 100644
--- a/tools/env/fw_env_main.c
+++ b/tools/env/fw_env_main.c
@@ -25,10 +25,11 @@
  * Command line user interface to firmware (=U-Boot) environment.
  *
  * Implements:
- *	fw_printenv [ name ... ]
- *		- prints the values of the environment variables
- *		  "name", or the whole environment if no names are
- *		  specified
+ *	fw_printenv [[ -n name ] | [ name ... ]]
+ *		- prints the value of a single environment variable
+ *		  "name", the values of one or more environment
+ *		  variables "name", or the whole environment if no names
+ *		  are specified.
  *	fw_setenv name [ value ... ]
  *		- If a name without any values is given, the variable
  *		  with this name is deleted from the environment;
@@ -51,6 +52,7 @@ main(int argc, char *argv[])
 {
 	char *p;
 	char *cmdname = *argv;
+	int errflag = 0;
 
 	if ((p = strrchr (cmdname, '/')) != NULL) {
 		cmdname = p + 1;
@@ -58,21 +60,19 @@ main(int argc, char *argv[])
 
 	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
 
-			fw_printenv (argc, argv);
-
-			return (EXIT_SUCCESS);
+			errflag += (fw_printenv (argc, argv) != 0);
 
 	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
 
-			if (fw_setenv (argc, argv) != 0)
-				return (EXIT_FAILURE);
+			errflag += (fw_setenv (argc, argv) != 0);
 
-			return (EXIT_SUCCESS);
+	} else {
+			fprintf (stderr,
+				 "Identity crisis - may be called as `"
+				 CMD_PRINTENV
+				 "' or as `" CMD_SETENV "' but not as `%s'\n",
+				 cmdname);
 	}
 
-	fprintf (stderr,
-		"Identity crisis - may be called as `" CMD_PRINTENV
-		"' or as `" CMD_SETENV "' but not as `%s'\n",
-		cmdname);
-	return (EXIT_FAILURE);
+	return (errflag ? EXIT_FAILURE : EXIT_SUCCESS);
 }
-- 
1.5.4.3





More information about the U-Boot mailing list