]> www.infradead.org Git - mtd-utils.git/commitdiff
[PATCH 10/13] Convert ubirmvol to use getopt option parsing
authorJosh Boyer <jwboyer@linux.ibm.com>
Mon, 11 Dec 2006 13:34:23 +0000 (14:34 +0100)
committerFrank Haverkamp <haver@vnet.ibm.com>
Mon, 11 Dec 2006 13:34:23 +0000 (14:34 +0100)
Signed-off-by: Josh Boyer <jwboyer@linux.vnet.ibm.com>
Acked-by: Frank Haverkamp <haver@vnet.ibm.com>
ubi-utils/src/ubirmvol.c

index 43679bc0148a868f852f727176e8fd19eda3c54a..69e4d0e598ba21bf732380e6eff06fc115831f45 100644 (file)
@@ -25,7 +25,6 @@
  * 1.1 Reworked the userinterface to use argp.
  */
 
-#include <argp.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <getopt.h>
@@ -38,6 +37,9 @@
 
 #define PROGRAM_VERSION "1.1"
 
+extern char *optarg;
+extern int optind;
+
 /*
  * The below variables are set by command line options.
  */
@@ -59,43 +61,30 @@ static struct args myargs = {
 };
 
 static int param_sanity_check(struct args *args, ubi_lib_t lib);
-static error_t parse_opt(int key, char *optarg, struct argp_state *state);
-
-const char *argp_program_version = PROGRAM_VERSION;
-const char *argp_program_bug_address = PACKAGE_BUGREPORT;
 
 static char doc[] = "\nVersion: " PROGRAM_VERSION "\n\t"
        BUILD_OS" "BUILD_CPU" at "__DATE__" "__TIME__"\n"
        "\nMake UBI Volume.\n";
 
-static struct argp_option options[] = {
-       { .name = "devn",
-         .key = 'd',
-         .arg = "<devn>",
-         .flags = 0,
-         .doc = "UBI device",
-         .group = OPTION_ARG_OPTIONAL },
-
-       { .name = "vol_id",
-         .key = 'n',
-         .arg = "<volume id>",
-         .flags = 0,
-         .doc = "UBI volume id, if not specified, the volume ID will be "
-                "assigned automatically",
-         .group = OPTION_ARG_OPTIONAL },
-
-       { .name = NULL, .key = 0, .arg = NULL, .flags = 0,
-         .doc = NULL, .group = 0 },
-};
-
-static struct argp argp = {
-       .options = options,
-       .parser = parse_opt,
-       .args_doc = 0,
-       .doc =  doc,
-       .children = NULL,
-       .help_filter = NULL,
-       .argp_domain = NULL,
+static const char *optionsstr =
+"  -d, --devn=<devn>          UBI device\n"
+"  -n, --vol_id=<volume id>   UBI volume id, if not specified, the volume ID\n"
+"                             will be assigned automatically\n"
+"  -?, --help                 Give this help list\n"
+"      --usage                Give a short usage message\n"
+"  -V, --version              Print program version\n";
+
+static const char *usage =
+"Usage: ubirmvol [-?V] [-d <devn>] [-n <volume id>] [--devn=<devn>]\n"
+"            [--vol_id=<volume id>] [--help] [--usage] [--version]\n";
+
+struct option long_options[] = {
+       { .name = "devn", .has_arg = 1, .flag = NULL, .val = 'd' },
+       { .name = "vol_id", .has_arg = 1, .flag = NULL, .val = 'n' },
+       { .name = "help", .has_arg = 0, .flag = NULL, .val = '?' },
+       { .name = "usage", .has_arg = 0, .flag = NULL, .val = 0 },
+       { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
+       { NULL, 0, NULL, 0}
 };
 
 /*
@@ -110,66 +99,59 @@ static struct argp argp = {
  * Get the `input' argument from `argp_parse', which we know is a
  * pointer to our arguments structure.
  */
-static error_t
-parse_opt(int key, char *optarg, struct argp_state *state)
+static int
+parse_opt(int argc, char **argv, struct args *args)
 {
        char *endp;
-       struct args *args = state->input;
-
-       switch (key) {
-       case 'd': /* --devn=<device number> */
-               args->devn = strtoul(optarg, &endp, 0);
-               if (*endp != '\0' || endp == optarg || args->devn < 0) {
-                       fprintf(stderr, "Bad UBI device number: "
-                               "\"%s\"\n", optarg);
-                       goto out;
-               }
-               break;
-       case 'n': /* --volid=<volume id> */
-               args->vol_id = strtoul(optarg, &endp, 0);
-               if (*endp != '\0' || endp == optarg ||
-                   (args->vol_id < 0 && args->vol_id != UBI_DYNAMIC_VOLUME)) {
-                       fprintf(stderr, "Bad volume ID: "
-                               "\"%s\"\n", optarg);
-                       goto out;
-               }
-               break;
-       case ':':
-               fprintf(stderr, "Parameter is missing\n");
-               goto out;
 
-       case ARGP_KEY_NO_ARGS:
-               /* argp_usage(state); */
-               break;
-
-       case ARGP_KEY_ARG:
-               args->arg1 = optarg;
-               /* Now we consume all the rest of the arguments.
-                  `state->next' is the index in `state->argv' of the
-                  next argument to be parsed, which is the first STRING
-                  we're interested in, so we can just use
-                  `&state->argv[state->next]' as the value for
-                  arguments->strings.
-
-                  _In addition_, by setting `state->next' to the end
-                  of the arguments, we can force argp to stop parsing
-                  here and return. */
-
-               args->options = &state->argv[state->next];
-               state->next = state->argc;
-               break;
-
-       case ARGP_KEY_END:
-               /* argp_usage(state); */
-               break;
-
-       default:
-               return(ARGP_ERR_UNKNOWN);
+       while (1) {
+               int key;
+
+               key = getopt_long(argc, argv, "d:n:?V", long_options, NULL);
+               if (key == -1)
+                       break;
+
+               switch (key) {
+                       case 'd': /* --devn=<device number> */
+                               args->devn = strtoul(optarg, &endp, 0);
+                               if (*endp != '\0' || endp == optarg || args->devn < 0) {
+                                       fprintf(stderr, "Bad UBI device number: "
+                                                       "\"%s\"\n", optarg);
+                                       goto out;
+                               }
+                               break;
+                       case 'n': /* --volid=<volume id> */
+                               args->vol_id = strtoul(optarg, &endp, 0);
+                               if (*endp != '\0' || endp == optarg ||
+                                               (args->vol_id < 0 && args->vol_id != UBI_DYNAMIC_VOLUME)) {
+                                       fprintf(stderr, "Bad volume ID: "
+                                                       "\"%s\"\n", optarg);
+                                       goto out;
+                               }
+                               break;
+                       case ':':
+                               fprintf(stderr, "Parameter is missing\n");
+                               goto out;
+                       case '?': /* help */
+                               fprintf(stderr, "Usage: ubirmvol [OPTION...]\n");
+                               fprintf(stderr, "%s", doc);
+                               fprintf(stderr, "%s", optionsstr);
+                               fprintf(stderr, "\nReport bugs to %s\n", PACKAGE_BUGREPORT);
+                               exit(0);
+                               break;
+                       case 'V':
+                               fprintf(stderr, "%s\n", PACKAGE_VERSION);
+                               exit(0);
+                               break;
+                       default:
+                               fprintf(stderr, "%s", usage);
+                               exit(-1);
+               }
        }
 
        return 0;
  out:
-       return(ARGP_ERR_UNKNOWN);
+       return -1;
 }
 
 static int param_sanity_check(struct args *args, ubi_lib_t lib)
@@ -203,8 +185,7 @@ int main(int argc, char * const argv[])
        int err, old_errno;
        ubi_lib_t lib;
 
-       err = argp_parse(&argp, argc, (char **)argv, ARGP_IN_ORDER, 0,
-                        &myargs);
+       err = parse_opt(argc, (char **)argv, &myargs);
        if (err)
                return err == 1 ? 0 : -1;