X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fopt.c;h=713ee784d3f384bbc501553f8d4ab74781ea75ac;hb=a7b91de90e7c0a8b6aded23913bd80f47e0bcdc9;hp=229a93b7a3168cd275d1b3e25ae43507445b8e6b;hpb=760ae9e3a01c7dee03bb57d5dbdbeaa3b0670735;p=libucw.git diff --git a/ucw/opt.c b/ucw/opt.c index 229a93b7..713ee784 100644 --- a/ucw/opt.c +++ b/ucw/opt.c @@ -18,7 +18,7 @@ #include #include -static uns opt_default_value_flags[] = { +static uint opt_default_value_flags[] = { [OPT_CL_BOOL] = OPT_NO_VALUE, [OPT_CL_STATIC] = OPT_MAYBE_VALUE, [OPT_CL_MULTIPLE] = OPT_REQUIRED_VALUE, @@ -39,7 +39,7 @@ void opt_failure(const char * mesg, ...) { static char *opt_name(struct opt_context *oc, struct opt_precomputed *opt) { - struct opt_item *item = opt->item; + const struct opt_item *item = opt->item; char *res; if (item->letter >= OPT_POSITIONAL_TAIL) res = stk_printf("positional argument #%d", oc->positional_count); @@ -52,12 +52,12 @@ static char *opt_name(struct opt_context *oc, struct opt_precomputed *opt) #define THIS_OPT opt_name(oc, opt) -void opt_precompute(struct opt_precomputed *opt, struct opt_item *item) +void opt_precompute(struct opt_precomputed *opt, const struct opt_item *item) { opt->item = item; opt->count = 0; opt->name = item->name; - uns flags = item->flags; + uint flags = item->flags; if (item->letter >= OPT_POSITIONAL_TAIL) { flags &= ~OPT_VALUE_FLAGS; @@ -71,10 +71,10 @@ void opt_precompute(struct opt_precomputed *opt, struct opt_item *item) opt->flags = flags; } -static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item *item, char *value) +static void opt_invoke_hooks(struct opt_context *oc, uint event, const struct opt_item *item, char *value) { for (int i = 0; i < oc->hook_count; i++) { - struct opt_item *hook = oc->hooks[i]; + const struct opt_item *hook = oc->hooks[i]; if (hook->flags & event) { void *data = (hook->flags & OPT_HOOK_INTERNAL) ? oc : hook->ptr; hook->u.hook(item, event, value, data); @@ -83,7 +83,7 @@ static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item } static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, char * str) { - uns len = strlen(str); + uint len = strlen(str); struct opt_precomputed * candidate = NULL; for (int i = 0; i < oc->opt_count; i++) { @@ -126,7 +126,7 @@ static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, c } static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * opt, char * value) { - struct opt_item * item = opt->item; + const struct opt_item * item = opt->item; if (opt->count++ && (opt->flags & OPT_SINGLE)) opt_failure("Option %s must be specified at most once.", THIS_OPT); @@ -225,6 +225,9 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op item->u.call(item, value, data); break; } + case OPT_CL_BREAK: + oc->stop_parsing = 2; + break; default: ASSERT(0); } @@ -235,7 +238,7 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op static int opt_longopt(struct opt_context * oc, char ** argv, int index) { int eaten = 0; char * name_in = argv[index] + 2; // skipping the -- on the beginning - uns pos = strchrnul(name_in, '=') - name_in; + uint pos = strchrnul(name_in, '=') - name_in; struct opt_precomputed * opt = opt_find_item_longopt(oc, strndupa(name_in, pos)); char * value = NULL; @@ -311,7 +314,7 @@ static int opt_shortopt(struct opt_context * oc, char ** argv, int index) { static void opt_positional(struct opt_context * oc, char * value) { oc->positional_count++; - uns id = oc->positional_count > oc->positional_max ? OPT_POSITIONAL_TAIL : OPT_POSITIONAL(oc->positional_count); + uint id = oc->positional_count > oc->positional_max ? OPT_POSITIONAL_TAIL : OPT_POSITIONAL(oc->positional_count); struct opt_precomputed * opt = oc->shortopt[id]; if (!opt) opt_failure("Too many positional arguments."); @@ -338,7 +341,7 @@ static void opt_count_items(struct opt_context *oc, const struct opt_section *se static void opt_prepare_items(struct opt_context *oc, const struct opt_section *sec) { - for (struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) { + for (const struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) { if (item->cls == OPT_CL_SECTION) opt_prepare_items(oc, item->u.section); else if (item->cls == OPT_CL_HOOK) @@ -357,7 +360,7 @@ static void opt_check_required(struct opt_context *oc) for (int i = 0; i < oc->opt_count; i++) { struct opt_precomputed *opt = &oc->opts[i]; if (!opt->count && (opt->flags & OPT_REQUIRED)) { - struct opt_item *item = opt->item; + const struct opt_item *item = opt->item; if (item->letter > OPT_POSITIONAL_TAIL) opt_failure("Required positional argument #%d not found.", item->letter - OPT_POSITIONAL_TAIL); else if (item->letter == OPT_POSITIONAL_TAIL) @@ -388,8 +391,9 @@ int opt_parse(const struct opt_section * options, char ** argv) { opt_prepare_items(oc, options); int force_positional = 0; - int i; + int i, start_i = 0; for (i=0; argv[i] && !oc->stop_parsing; i++) { + start_i = i; char *arg = argv[i]; opt_invoke_hooks(oc, OPT_HOOK_BEFORE_ARG, NULL, NULL); if (arg[0] != '-' || force_positional) @@ -409,5 +413,5 @@ int opt_parse(const struct opt_section * options, char ** argv) { opt_check_required(oc); opt_invoke_hooks(oc, OPT_HOOK_FINAL, NULL, NULL); - return i; + return (oc->stop_parsing < 2 ? i : start_i); }