X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Fparse.c;h=14c7afbb083b3a0cc9eb5e5f88d46ba105464ebd;hp=228808dd4a6707b73e37173575ca25e36e62a1ac;hb=4f09cf20ca00fe38b0702e0556bbad2341595ed0;hpb=9056c31b46452c92c7dd9c276664f514720a84c6 diff --git a/ccan/opt/parse.c b/ccan/opt/parse.c index 228808dd..14c7afbb 100644 --- a/ccan/opt/parse.c +++ b/ccan/opt/parse.c @@ -1,3 +1,4 @@ +/* Licensed under GPLv3+ - see LICENSE file for details */ /* Actual code to parse commandline. */ #include #include @@ -28,12 +29,12 @@ static void consume_option(int *argc, char *argv[], unsigned optnum) } /* Returns 1 if argument consumed, 0 if all done, -1 on error. */ -int parse_one(int *argc, char *argv[], unsigned *offset, +int parse_one(int *argc, char *argv[], enum opt_type is_early, unsigned *offset, void (*errlog)(const char *fmt, ...)) { unsigned i, arg, len; const char *o, *optarg = NULL; - char *problem; + char *problem = NULL; if (getenv("POSIXLY_CORRECT")) { /* Don't find options after non-options. */ @@ -90,11 +91,12 @@ int parse_one(int *argc, char *argv[], unsigned *offset, len = 2; } - if (opt_table[i].type == OPT_NOARG) { + if ((opt_table[i].type & ~OPT_EARLY) == OPT_NOARG) { if (optarg) return parse_err(errlog, argv[0], o, len, "doesn't allow an argument"); - problem = opt_table[i].cb(opt_table[i].arg); + if ((opt_table[i].type & OPT_EARLY) == is_early) + problem = opt_table[i].cb(opt_table[i].u.arg); } else { if (!optarg) { /* Swallow any short options as optarg, eg -afile */ @@ -107,12 +109,14 @@ int parse_one(int *argc, char *argv[], unsigned *offset, if (!optarg) return parse_err(errlog, argv[0], o, len, "requires an argument"); - problem = opt_table[i].cb_arg(optarg, opt_table[i].arg); + if ((opt_table[i].type & OPT_EARLY) == is_early) + problem = opt_table[i].cb_arg(optarg, + opt_table[i].u.arg); } if (problem) { parse_err(errlog, argv[0], o, len, problem); - free(problem); + opt_alloc.free(problem); return -1; }