Tools: set multiple variable with fw_setenv utility

Add a sort of batch mode to fw_setenv, allowing to set
multiple variables in one shot, without updating the flash after
each set as now. It is added the possibility to pass
a config file with a list of pairs <variable, value> to be set,
separated by a TAB character.

Signed-off-by: Stefano Babic <sbabic@denx.de>
diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
index 7f631c4..82116b4 100644
--- a/tools/env/fw_env_main.c
+++ b/tools/env/fw_env_main.c
@@ -42,34 +42,87 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <getopt.h>
 #include "fw_env.h"
 
 #define	CMD_PRINTENV	"fw_printenv"
 #define CMD_SETENV	"fw_setenv"
 
+static struct option long_options[] = {
+	{"script", required_argument, NULL, 's'},
+	{"help", no_argument, NULL, 'h'},
+	{NULL, 0, NULL, 0}
+};
+
+void usage(void)
+{
+
+	fprintf(stderr, "fw_printenv/fw_setenv, "
+		"a command line interface to U-Boot environment\n\n"
+		"usage:\tfw_printenv\n"
+		"\tfw_setenv [variable name] [variable value]\n"
+		"\tfw_setenv -s [ file ]\n"
+		"\tfw_setenv -s - < [ file ]\n\n"
+		"The file passed as argument contains only pairs "
+		"name / value\n"
+		"Example:\n"
+		"# Any line starting with # is treated as comment\n"
+		"\n"
+		"\t      netdev         eth0\n"
+		"\t      kernel_addr    400000\n"
+		"\t      var1\n"
+		"\t      var2          The quick brown fox jumps over the "
+		"lazy dog\n"
+		"\n"
+		"A variable without value will be dropped. It is possible\n"
+		"to put any number of spaces between the fields, but any\n"
+		"space inside the value is treated as part of the value "
+		"itself.\n\n"
+	);
+}
+
 int
 main(int argc, char *argv[])
 {
 	char *p;
 	char *cmdname = *argv;
+	char *script_file = NULL;
+	int c;
 
 	if ((p = strrchr (cmdname, '/')) != NULL) {
 		cmdname = p + 1;
 	}
 
+	while ((c = getopt_long (argc, argv, "s:h",
+		long_options, NULL)) != EOF) {
+		switch (c) {
+		case 's':
+			script_file = optarg;
+			break;
+		case 'h':
+			usage();
+			return EXIT_SUCCESS;
+		}
+	}
+
+
 	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
 
 		if (fw_printenv (argc, argv) != 0)
-			return (EXIT_FAILURE);
+			return EXIT_FAILURE;
 
-		return (EXIT_SUCCESS);
+		return EXIT_SUCCESS;
 
 	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
+		if (!script_file) {
+			if (fw_setenv(argc, argv) != 0)
+				return EXIT_FAILURE;
+		} else {
+			if (fw_parse_script(script_file) != 0)
+				return EXIT_FAILURE;
+		}
 
-		if (fw_setenv (argc, argv) != 0)
-			return (EXIT_FAILURE);
-
-		return (EXIT_SUCCESS);
+		return EXIT_SUCCESS;
 
 	}
 
@@ -77,5 +130,5 @@
 		"Identity crisis - may be called as `" CMD_PRINTENV
 		"' or as `" CMD_SETENV "' but not as `%s'\n",
 		cmdname);
-	return (EXIT_FAILURE);
+	return EXIT_FAILURE;
 }