From 0ea0e4e31b7c97c0986d9fd3099c5aab0d2e3392 Mon Sep 17 00:00:00 2001 From: Josh Boyer Date: Mon, 11 Dec 2006 14:34:23 +0100 Subject: [PATCH 11/13] Convert ubiupdatevol to use getopt option parsing Signed-off-by: Josh Boyer Acked-by: Frank Haverkamp --- ubi-utils/src/ubiupdatevol.c | 185 +++++++++++++++++++------------------------ 1 file changed, 80 insertions(+), 105 deletions(-) diff --git a/ubi-utils/src/ubiupdatevol.c b/ubi-utils/src/ubiupdatevol.c index dcb7399..e57dbee 100644 --- a/ubi-utils/src/ubiupdatevol.c +++ b/ubi-utils/src/ubiupdatevol.c @@ -24,7 +24,6 @@ * 1.0 Reworked the userinterface to use argp. */ -#include #include #include #include @@ -47,6 +46,9 @@ #define BUFSIZE 128 * 1024 #define MIN(x,y) ((x)<(y)?(x):(y)) +extern char *optarg; +extern int optind; + struct args { int devn; int vol_id; @@ -69,57 +71,35 @@ static struct args myargs = { .options = NULL, }; -static error_t parse_opt (int key, char *arg, struct argp_state *state); - static int verbose = 0; -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" "\nWrite to UBI Volume.\n"; -static struct argp_option options[] = { - { .name = "devn", - .key = 'd', - .arg = "", - .flags = 0, - .doc = "UBI device", - .group = OPTION_ARG_OPTIONAL }, - - { .name = "vol_id", - .key = 'n', - .arg = "", - .flags = 0, - .doc = "UBI volume id", - .group = OPTION_ARG_OPTIONAL }, - - { .name = "truncate", - .key = 't', - .arg = NULL, - .flags = 0, - .doc = "truncate volume", - .group = OPTION_ARG_OPTIONAL }, - - { .name = "broken-update", - .key = 'B', - .arg = NULL, - .flags = 0, - .doc = "broken update, this is for testing", - .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 = +" -B, --broken-update broken update, this is for testing\n" +" -d, --devn= UBI device\n" +" -n, --vol_id= UBI volume id\n" +" -t, --truncate truncate volume\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: ubiupdatevol [-Bt?V] [-d ] [-n ] [--broken-update]\n" +" [--devn=] [--vol_id=] [--truncate] [--help]\n" +" [--usage] [--version]\n"; + +struct option long_options[] = { + { .name = "broken-update", .has_arg = 0, .flag = NULL, .val = 'B' }, + { .name = "devn", .has_arg = 1, .flag = NULL, .val = 'd' }, + { .name = "vol_id", .has_arg = 1, .flag = NULL, .val = 'n' }, + { .name = "truncate", .has_arg = 0, .flag = NULL, .val = 't' }, + { .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} }; /* @@ -134,65 +114,60 @@ 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 *arg, struct argp_state *state) +static int +parse_opt(int argc, char **argv, struct args *args) { - struct args *args = state->input; - - switch (key) { - case 'v': /* --verbose= */ - verbose = strtoul(arg, (char **)NULL, 0); - break; - - case 'n': /* --vol_id= */ - args->vol_id = strtol(arg, (char **)NULL, 0); - break; - - case 'd': /* --devn= */ - args->devn = strtol(arg, (char **)NULL, 0); - break; - - case 'b': /* --bufsize= */ - args->bufsize = strtol(arg, (char **)NULL, 0); - if (args->bufsize <= 0) - args->bufsize = BUFSIZE; - break; - - case 't': /* --truncate */ - args->truncate = 1; - break; - - case 'B': /* --broken-update */ - args->broken_update = 1; - break; - - case ARGP_KEY_NO_ARGS: - /* argp_usage(state); */ - break; - - case ARGP_KEY_ARG: - args->arg1 = arg; - /* 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, "Bd:n:t?V", long_options, NULL); + if (key == -1) + break; + + switch (key) { + case 'v': /* --verbose= */ + verbose = strtoul(optarg, (char **)NULL, 0); + break; + + case 'n': /* --vol_id= */ + args->vol_id = strtol(optarg, (char **)NULL, 0); + break; + + case 'd': /* --devn= */ + args->devn = strtol(optarg, (char **)NULL, 0); + break; + + case 'b': /* --bufsize= */ + args->bufsize = strtol(optarg, (char **)NULL, 0); + if (args->bufsize <= 0) + args->bufsize = BUFSIZE; + break; + + case 't': /* --truncate */ + args->truncate = 1; + break; + + case 'B': /* --broken-update */ + args->broken_update = 1; + break; + + case '?': /* help */ + fprintf(stderr, "Usage: ubiupdatevol [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; @@ -335,7 +310,7 @@ main(int argc, char *argv[]) { int rc; - argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, &myargs); + parse_opt(argc, argv, &myargs); if (myargs.truncate) { rc = ubi_truncate_volume(&myargs, 0LL); -- cgit v1.2.3