X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fopt.c;h=5598a13b340bdbff9402495d8c38781feebfefa6;hb=1418d52e7f200f8e63c701dbcae3ddd811705676;hp=f7bae0d847c314c0ae0ebc1b3cfc948ea0068fb6;hpb=bdccfb6b49a17a2ac0e1fb3063df3766cd4abd5c;p=libucw.git diff --git a/ucw/opt.c b/ucw/opt.c index f7bae0d8..5598a13b 100644 --- a/ucw/opt.c +++ b/ucw/opt.c @@ -11,51 +11,29 @@ #include #include #include +#include #include #include #include #include -#include -/*** - * Value flags defaults - * ~~~~~~~~~~~~~~~~~~~~ - * - * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC - * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT - * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE) - * must be specified for OPT_CALL and OPT_USER. - ***/ static uns opt_default_value_flags[] = { [OPT_CL_BOOL] = OPT_NO_VALUE, [OPT_CL_STATIC] = OPT_MAYBE_VALUE, + [OPT_CL_MULTIPLE] = OPT_REQUIRED_VALUE, [OPT_CL_SWITCH] = OPT_NO_VALUE, [OPT_CL_INC] = OPT_NO_VALUE, [OPT_CL_CALL] = 0, - [OPT_CL_USER] = 0, [OPT_CL_SECTION] = 0, [OPT_CL_HELP] = 0 }; -struct opt_context { - struct opt_precomputed * opts; - struct opt_precomputed ** shortopt; - struct opt_item ** hooks; - int opt_count; - int hook_count; - int positional_max; - int positional_count; - bool stop_parsing; -}; - -// FIXME: Make public? void opt_failure(const char * mesg, ...) { va_list args; va_start(args, mesg); vfprintf(stderr, mesg, args); - fprintf(stderr, "\n"); - opt_usage(); + fprintf(stderr, "\nRun with --help for more information.\n"); exit(OPT_EXIT_BAD_ARGS); } @@ -86,7 +64,7 @@ void opt_precompute(struct opt_precomputed *opt, struct opt_item *item) flags |= OPT_REQUIRED_VALUE; } if (!(flags & OPT_VALUE_FLAGS)) { - ASSERT(item->cls != OPT_CL_CALL && item->cls != OPT_CL_USER); + ASSERT(item->cls != OPT_CL_CALL); flags |= opt_default_value_flags[item->cls]; } @@ -97,8 +75,10 @@ static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item { for (int i = 0; i < oc->hook_count; i++) { struct opt_item *hook = oc->hooks[i]; - if (hook->flags & event) - hook->u.hook(item, event, value, hook->ptr); + if (hook->flags & event) { + void *data = (hook->flags & OPT_HOOK_INTERNAL) ? oc : hook->ptr; + hook->u.hook(item, event, value, data); + } } } @@ -132,20 +112,6 @@ static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, c opt_failure("Invalid option --%s.", str); } -// FIXME: Use simple-lists? -#define OPT_PTR(type) ({ \ - type * ptr; \ - if (item->flags & OPT_MULTIPLE) { \ - struct { \ - cnode n; \ - type v; \ - } * n = xmalloc(sizeof(*n)); \ - clist_add_tail(item->ptr, &(n->n)); \ - ptr = &(n->v); \ - } else \ - ptr = item->ptr; \ - ptr; }) - static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * opt, char * value) { struct opt_item * item = opt->item; @@ -167,8 +133,15 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): 1/0, y/n, yes/no, true/false.", THIS_OPT); break; case OPT_CL_STATIC: + case OPT_CL_MULTIPLE: { char * e = NULL; + void * ptr; + if (item->cls == OPT_CL_STATIC) + ptr = item->ptr; + else + ptr = GARY_PUSH_GENERIC(*(void **)item->ptr); +#define OPT_PTR(type) ((type *) ptr) switch (item->type) { case CT_INT: if (!value) @@ -208,9 +181,17 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op else *OPT_PTR(const char *) = xstrdup(value); break; + case CT_USER: + { + char * e = item->u.utype->parser(value, ptr); + if (e) + opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e); + break; + } default: ASSERT(0); } +#undef OPT_PTR break; } case OPT_CL_SWITCH: @@ -226,14 +207,9 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op (*((int *)item->ptr))++; break; case OPT_CL_CALL: - item->u.call(item, value, item->ptr); - break; - case OPT_CL_USER: { - char * e = NULL; - e = item->u.utype->parser(value, OPT_PTR(void*)); - if (e) - opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e); + void *data = (opt->flags & OPT_INTERNAL) ? oc : item->ptr; + item->u.call(item, value, data); break; } default: @@ -383,6 +359,7 @@ static void opt_check_required(struct opt_context *oc) int opt_parse(const struct opt_section * options, char ** argv) { struct opt_context * oc = alloca(sizeof(*oc)); memset(oc, 0, sizeof (*oc)); + oc->options = options; opt_count_items(oc, options); oc->opts = alloca(sizeof(*oc->opts) * oc->opt_count);