--- a/src/config.h Tue Apr 07 21:09:25 2009 +0300
+++ b/src/config.h Wed Apr 08 01:28:46 2009 +0300
@@ -7,6 +7,7 @@
* Support for module configuration parameters
*/
#include "error.h"
+#include <stdbool.h>
/**
* Different types of configuration parameters
@@ -39,36 +40,84 @@
};
/**
- * A single configuration option, with a name, type, handler function, etc.
+ * Structure to define a single parameter for an option
*/
-struct config_option {
- /** The name of the config option */
+struct config_param {
+ /** The name of the arg */
const char *name;
- /** The type of the value expected */
+ /** The type */
enum config_type type;
-
- /** The value handler func, by type */
+
+ /** Description */
+ const char *description;
+
+ /** Optional value handler function, by type */
union {
err_t (*string) (void *ctx, char *value, struct error_info *err);
err_t (*irc_chan) (void *ctx, struct irc_chan *chan, struct error_info *err);
} func;
- /** The value description */
- const char *description;
+ /** Use handler function? */
+ bool is_handler;
+};
+
+/**
+ * The maximum number of parameters that a single option can have, although this includes the terminating NULL
+ */
+#define CONFIG_PARAMS_MAX (15 + 1)
+
+/**
+ * The maximum number of values that an option can access, including the terminating NULL
+ */
+#define CONFIG_VALUES_MAX CONFIG_PARAMS_MAX
+
+/**
+ * A more complicated configuration option that can have multiple parameters
+ */
+struct config_option {
+ /** The name of the config option */
+ const char *name;
+
+ /** The list of parameters */
+ 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);
/** Help text */
const char *help;
};
+#define CONFIG_PARAM(name, type, desc) \
+ { name, type, desc, { NULL }, false }
+
+#define CONFIG_PARAM_HANDLER(name, type, desc, _func_name_, func) \
+ { name, type, desc, {._func_name_ = func }, true }
+
+#define CONFIG_OPT(name, params, func, help) \
+ { name, params, func, help }
+
#define CONFIG_OPT_STRING(name, func, desc, help) \
- { name, CONFIG_STRING, { .string = func }, desc, help }
+ { name, { CONFIG_PARAM_HANDLER(name, CONFIG_STRING, desc, string, func) }, NULL, help }
#define CONFIG_OPT_IRC_CHAN(name, func, desc, help) \
- { name, CONFIG_IRC_CHAN, { .irc_chan = func }, desc, help }
+ { name, { CONFIG_PARAM_HANDLER(name, CONFIG_IRC_CHAN, desc, irc_chan, func) }, NULL, help }
#define CONFIG_OPT_END \
- { NULL, CONFIG_INVALID, { NULL }, NULL, NULL }
+ { NULL, {}, NULL, NULL }
+
+#define CONFIG_VALUE(type, _name_, value) \
+ { (type), { ._name_ = (value) } }
+
+#define CONFIG_VALUE_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)
+
+#define CONFIG_VALUE_END \
+ { 0, { NULL } }
/**
* Lookup a config option by name.
@@ -83,7 +132,7 @@
// XXX: move this into nexus
#include "nexus.h"
/**
- * Parse a raw value into a suitable configuration value, based on the config option type.
+ * Parse a raw value into a suitable configuration value for a single-param option, based on the config option type.
*
* Since this needs to access the application state, you need to pass in the nexus as an argument.
*
@@ -99,27 +148,27 @@
err_t config_parse (const struct config_option *option, struct nexus *nexus, struct config_value *value, char *raw_value, struct error_info *err);
/**
- * Apply a parsed configuration value to the given option.
+ * Apply a list of parsed configuration values to the given config_option struct.
*
* The config option handlers take a context argument; the value of this depends on the implementor of the config_option.
*
* @param option the option to apply
* @param ctx the context pointer for the option handler
- * @param value the parsed value
+ * @param values the NULL-terminated array of parsed values
* @param err returned error info
*/
-err_t config_apply_opt (const struct config_option *option, void *ctx, const struct config_value *value, struct error_info *err);
+err_t config_apply_opt (const struct config_option *option, void *ctx, const struct config_value values[], struct error_info *err);
/**
- * Apply a parsed configuration value for the named config opt.
+ * Apply a list of parsed configuration values for the named config opt.
*
* @param options a CONFIG_OPT_END-terminated array of config_option's
* @param ctx the context pointer for the option handler
* @param name the config_option::name to look up
- * @param value the parsed value
+ * @param values the NULL-termianted array of parsed values
* @param err returned error info
*/
-err_t config_apply (const struct config_option *options, void *ctx, const char *name, const struct config_value *value, struct error_info *err);
+err_t config_apply (const struct config_option *options, void *ctx, const char *name, const struct config_value values[], struct error_info *err);
/**
* Apply a string value for the named config opt.