From 7421fd3c8aba3cd6cb5503185d86ac13a8a6fcd0 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 28 Jan 2014 17:45:31 +0100 Subject: [PATCH] Opt: User-defined types are specified as CT_USER, not OPT_CL_USER --- ucw/doc/opt.txt | 4 +++- ucw/opt.c | 18 ++++++++---------- ucw/opt.h | 7 ++----- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/ucw/doc/opt.txt b/ucw/doc/opt.txt index 7a61c87e..74b6b9db 100644 --- a/ucw/doc/opt.txt +++ b/ucw/doc/opt.txt @@ -64,7 +64,9 @@ Most options have the following properties: Alternatively, the short name can refer to a <>. - Long name: an arbitrary string. Set to NULL if the option has no long form. - Variable, where the value of the option shall be stored, together with - its <>. + its <>. The type is either one of the conventional + types (`int`, `uns`, etc.), or a user-defined type providing its own parser + function via <>. - <> further specifying behavior of the option (whether it is mandatory, whether it carries a value, whether it can be set repeatedly, etc.). - Help text, from which the help displayed to the user is constructed. diff --git a/ucw/opt.c b/ucw/opt.c index 1a94f7c0..0709e820 100644 --- a/ucw/opt.c +++ b/ucw/opt.c @@ -23,7 +23,6 @@ static uns opt_default_value_flags[] = { [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 }; @@ -63,7 +62,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]; } @@ -187,6 +186,13 @@ 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, item->ptr); + if (e) + opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e); + break; + } default: ASSERT(0); } @@ -210,14 +216,6 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op item->u.call(item, value, data); 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); - break; - } default: ASSERT(0); } diff --git a/ucw/opt.h b/ucw/opt.h index f6604e8c..38046136 100644 --- a/ucw/opt.h +++ b/ucw/opt.h @@ -56,8 +56,6 @@ * `OPT_NEGATIVE` flag is set). * - `OPT_CL_CALL`: instead of setting a variable, call a function * and pass the value of the option to it. - * - `OPT_CL_USER`: like `OPT_CL_STATIC`, but with user-defined value - * syntax, specified as <>. * - `OPT_CL_SECTION`: not a real option, but an instruction to insert * contents of another list of options. * - `OPT_CL_HELP`: no option, just print a help text. @@ -71,7 +69,6 @@ enum opt_class { OPT_CL_SWITCH, OPT_CL_INC, OPT_CL_CALL, - OPT_CL_USER, OPT_CL_SECTION, OPT_CL_HELP, OPT_CL_HOOK, @@ -105,7 +102,7 @@ struct opt_item { 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, uns 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 OPT_CL_USER + 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) byte cls; // enum opt_class @@ -197,7 +194,7 @@ struct opt_item { * An option with user-defined syntax. @ttype is a <> * describing the syntax, @target is a variable of the corresponding type. **/ -#define OPT_USER(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.utype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_USER, .type = CT_USER } +#define OPT_USER(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.utype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_STATIC, .type = CT_USER } /** A sub-section. **/ #define OPT_SECTION(sec) { .cls = OPT_CL_SECTION, .u.section = &sec } -- 2.39.2