From 1bc3bb66e47ec02003658fb3040aef0ffd7b7540 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 27 Jun 2014 16:52:28 +0200 Subject: [PATCH] Opt: Constify --- ucw/opt-help.c | 4 ++-- ucw/opt-internal.h | 6 +++--- ucw/opt.c | 14 +++++++------- ucw/opt.h | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ucw/opt-help.c b/ucw/opt-help.c index 64e14394..ff982238 100644 --- a/ucw/opt-help.c +++ b/ucw/opt-help.c @@ -28,7 +28,7 @@ struct help_line { static void opt_help_scan_item(struct help *h, struct opt_precomputed *opt) { - struct opt_item *item = opt->item; + const struct opt_item *item = opt->item; if (!item->help) return; @@ -90,7 +90,7 @@ static void opt_help_scan_item(struct help *h, struct opt_precomputed *opt) static void opt_help_scan(struct help *h, 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_help_scan(h, item->u.section); else if (item->cls == OPT_CL_HOOK) diff --git a/ucw/opt-internal.h b/ucw/opt-internal.h index 77434d7a..99514c29 100644 --- a/ucw/opt-internal.h +++ b/ucw/opt-internal.h @@ -25,7 +25,7 @@ struct opt_context { const struct opt_section * options; struct opt_precomputed * opts; struct opt_precomputed ** shortopt; - struct opt_item ** hooks; + const struct opt_item ** hooks; int opt_count; int hook_count; int positional_max; @@ -35,12 +35,12 @@ struct opt_context { }; struct opt_precomputed { - struct opt_item * item; + const struct opt_item * item; const char * name; short flags; short count; }; -void opt_precompute(struct opt_precomputed *opt, struct opt_item *item); +void opt_precompute(struct opt_precomputed *opt, const struct opt_item *item); #endif diff --git a/ucw/opt.c b/ucw/opt.c index 9ef721d6..e8d7cbee 100644 --- a/ucw/opt.c +++ b/ucw/opt.c @@ -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,7 +52,7 @@ 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; @@ -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, uint 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); @@ -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); @@ -338,7 +338,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 +357,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) diff --git a/ucw/opt.h b/ucw/opt.h index 557cadb1..82c76bba 100644 --- a/ucw/opt.h +++ b/ucw/opt.h @@ -92,7 +92,7 @@ enum opt_class { /** A section of option list. **/ struct opt_section { - struct opt_item * opt; + const struct opt_item * opt; }; /** A definition of a single option item. **/ @@ -102,10 +102,10 @@ struct opt_item { void * ptr; // variable to store the value to const char * help; // description in --help (NULL to omit the option from the help) union opt_union { - struct opt_section * section; // subsection for OPT_CL_SECTION + const struct opt_section * section; // subsection for OPT_CL_SECTION int value; // value for OPT_CL_SWITCH - void (* call)(struct opt_item * opt, const char * value, void * data); // function to call for OPT_CL_CALL - void (* hook)(struct opt_item * opt, uint event, const char * value, void * data); // function to call for OPT_CL_HOOK + void (* call)(const struct opt_item * opt, const char * value, void * data); // function to call for OPT_CL_CALL + void (* hook)(const struct opt_item * opt, uint event, const char * value, void * data); // function to call for OPT_CL_HOOK struct cf_user_type * utype; // specification of the user-defined type for CT_USER } u; u16 flags; // as defined below (for hooks, event mask is stored instead) -- 2.39.5