diff -r 576bab0a1c5a -r 4682ebbc5644 src/config.h --- a/src/config.h Wed Apr 08 01:28:46 2009 +0300 +++ b/src/config.h Wed Apr 08 02:25:30 2009 +0300 @@ -15,9 +15,15 @@ enum config_type { CONFIG_INVALID, + /** No value at all */ + CONFIG_NULL, + /** A plain NUL-terminated string */ CONFIG_STRING, + /** A user-defined type, further identified by a string */ + CONFIG_USER, + /** An IRC channel */ CONFIG_IRC_CHAN, }; @@ -36,6 +42,15 @@ /** Value for CONFIG_IRC_CHAN */ struct irc_chan *irc_chan; + + /** Value for CONFIG_USER */ + struct { + /** The specific user type */ + const char *type; + + /** A pointer to the user type */ + void *ptr; + } user; }; }; @@ -49,6 +64,9 @@ /** The type */ enum config_type type; + /** The specific type for CONFIG_USER */ + const char *user_type; + /** Description */ const char *description; @@ -60,6 +78,9 @@ /** Use handler function? */ bool is_handler; + + /** Optional value? */ + bool optional; }; /** @@ -83,38 +104,56 @@ const struct config_param params[CONFIG_PARAMS_MAX]; /** The handler function */ - err_t (*func) (void *ctx, const struct config_value values[], struct error_info *err); + err_t (*func) (void *ctx, const struct config_option *option, const struct config_value values[], struct error_info *err); /** Help text */ const char *help; }; -#define CONFIG_PARAM(name, type, desc) \ - { name, type, desc, { NULL }, false } +#define CONFIG_PARAM(name, type, desc, optional) \ + { name, type, NULL, desc, { NULL }, false, optional } + +#define CONFIG_PARAM_OPTIONAL(name, type, desc) \ + CONFIG_PARAM(name, type, desc, true) + +#define CONFIG_PARAM_USER(name, user_type, desc, optional) \ + { name, CONFIG_USER, user_type, desc, { NULL }, false, optional } + +#define CONFIG_PARAM_USER_OPTIONAL(name, user_type, desc) \ + CONFIG_PARAM_USER(name, user_type, desc, true) #define CONFIG_PARAM_HANDLER(name, type, desc, _func_name_, func) \ - { name, type, desc, {._func_name_ = func }, true } + { name, type, NULL, desc, {._func_name_ = func }, true, false } -#define CONFIG_OPT(name, params, func, help) \ - { name, params, func, help } +#define CONFIG_PARAM_END \ + { NULL, 0, NULL, NULL, { NULL }, false, false } + +#define CONFIG_OPT(name, func, help, ...) \ + { name, { __VA_ARGS__, CONFIG_PARAM_END }, func, help } #define CONFIG_OPT_STRING(name, func, desc, help) \ - { name, { CONFIG_PARAM_HANDLER(name, CONFIG_STRING, desc, string, func) }, NULL, help } + CONFIG_OPT(name, NULL, help, CONFIG_PARAM_HANDLER(name, CONFIG_STRING, desc, string, func)) #define CONFIG_OPT_IRC_CHAN(name, func, desc, help) \ - { name, { CONFIG_PARAM_HANDLER(name, CONFIG_IRC_CHAN, desc, irc_chan, func) }, NULL, help } + CONFIG_OPT(name, NULL, help, CONFIG_PARAM_HANDLER(name, CONFIG_IRC_CHAN, desc, irc_chan, func)) #define CONFIG_OPT_END \ { NULL, {}, NULL, NULL } +#define CONFIG_OPTIONS(...) \ + { __VA_ARGS__, CONFIG_OPT_END } + #define CONFIG_VALUE(type, _name_, value) \ - { (type), { ._name_ = (value) } } + { (type), { ._name_ = value } } #define CONFIG_VALUE_STRING(str_value) \ - CONFIG_VALUE(CONFIG_STRING, string, str_value) + CONFIG_VALUE(CONFIG_STRING, string, (str_value)) #define CONFIG_VALUE_IRC_CHAN(irc_chan_value) \ - CONFIG_VALUE(CONFIG_IRC_CHAN, irc_chan, irc_chan_value) + CONFIG_VALUE(CONFIG_IRC_CHAN, irc_chan, (irc_chan_value)) + +#define CONFIG_VALUE_USER(user_type, user_value) \ + CONFIG_VALUE(CONFIG_USER, user, { user_type, user_value }) #define CONFIG_VALUE_END \ { 0, { NULL } } @@ -206,6 +245,15 @@ * * @see config_parse */ -err_t config_apply_raw (const struct config_option *options, struct nexus *nexus, void *ctx, const char *name, char *raw_value, struct error_info *err); +err_t config_apply_raw (const struct config_option option[], struct nexus *nexus, void *ctx, const char *name, char *raw_value, struct error_info *err); + +/** + * Lookup a config_value by name from the given list of config_values for the given config_option + */ +const struct config_value* config_get_value (const struct config_option *option, const struct config_value values[], const char *name); + +const char* config_get_string (const struct config_option *option, const struct config_value values[], const char *name); +struct irc_chan* config_get_irc_chan (const struct config_option *option, const struct config_value values[], const char *name); +void* config_get_user (const struct config_option *option, const struct config_value values[], const char *name, const char *user_type); #endif