tools/env: fail on invalid options
[platform/kernel/u-boot.git] / tools / env / fw_env_main.c
index 7f631c4..c654057 100644 (file)
 #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] [variable name]\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, "ns:h",
+               long_options, NULL)) != EOF) {
+               switch (c) {
+               case 'n':
+                       /* handled in fw_printenv */
+                       break;
+               case 's':
+                       script_file = optarg;
+                       break;
+               case 'h':
+                       usage();
+                       return EXIT_SUCCESS;
+               default: /* '?' */
+                       fprintf(stderr, "Try `%s --help' for more information."
+                               "\n", cmdname);
+                       return EXIT_FAILURE;
+               }
+       }
+
+
        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 +137,5 @@ main(int argc, char *argv[])
                "Identity crisis - may be called as `" CMD_PRINTENV
                "' or as `" CMD_SETENV "' but not as `%s'\n",
                cmdname);
-       return (EXIT_FAILURE);
+       return EXIT_FAILURE;
 }