src/config.h
changeset 100 cfb7776bd6f0
parent 87 f0db6ebf18b9
child 120 576bab0a1c5a
equal deleted inserted replaced
99:155a6c7d3886 100:cfb7776bd6f0
    14 enum config_type {
    14 enum config_type {
    15     CONFIG_INVALID,
    15     CONFIG_INVALID,
    16 
    16 
    17     /** A plain NUL-terminated string */
    17     /** A plain NUL-terminated string */
    18     CONFIG_STRING,
    18     CONFIG_STRING,
       
    19 
       
    20     /** An IRC channel */
       
    21     CONFIG_IRC_CHAN,
       
    22 };
       
    23 
       
    24 /**
       
    25  * Structure to hold a value as defined by config_type
       
    26  */
       
    27 struct config_value {
       
    28     /** The type of the value */
       
    29     enum config_type type;
       
    30     
       
    31     /** The typed value */
       
    32     union {
       
    33         /** Value for CONFIG_STRING */
       
    34         char *string;
       
    35 
       
    36         /** Value for CONFIG_IRC_CHAN */
       
    37         struct irc_chan *irc_chan;
       
    38     };
    19 };
    39 };
    20 
    40 
    21 /**
    41 /**
    22  * A single configuration option, with a name, type, handler function, etc.
    42  * A single configuration option, with a name, type, handler function, etc.
    23  */
    43  */
    24 struct config_option {
    44 struct config_option {
    25     /** The name of the config option */
    45     /** The name of the config option */
    26     const char *name;
    46     const char *name;
    27 
    47 
    28     /** The type of the value, XXX: unused */
    48     /** The type of the value expected */
    29     enum config_type type;
    49     enum config_type type;
    30     
    50     
    31     /** The value handler func */
    51     /** The value handler func, by type */
    32     err_t (*func) (void *ctx, char *value, struct error_info *err);
    52     union {
       
    53         err_t (*string) (void *ctx, char *value, struct error_info *err);
       
    54         err_t (*irc_chan) (void *ctx, struct irc_chan *chan, struct error_info *err);
       
    55     } func;
    33 
    56 
    34     /** The value description */
    57     /** The value description */
    35     const char *description;
    58     const char *description;
    36 
    59 
    37     /** Help text */
    60     /** Help text */
    38     const char *help;
    61     const char *help;
    39 };
    62 };
    40 
    63 
       
    64 #define CONFIG_OPT_STRING(name, func, desc, help) \
       
    65     {   name, CONFIG_STRING, { .string = func }, desc, help }
       
    66 
       
    67 #define CONFIG_OPT_IRC_CHAN(name, func, desc, help) \
       
    68     {   name, CONFIG_IRC_CHAN, { .irc_chan = func }, desc, help }
       
    69 
       
    70 #define CONFIG_OPT_END \
       
    71     {   NULL,   CONFIG_INVALID, { NULL }, NULL, NULL }
       
    72 
    41 /**
    73 /**
    42  * Apply a configuration name/value to an array of config_option's.
    74  * Lookup a config option by name.
    43  *
    75  *
    44  * This finds the appropriate config_option and calls its func.
    76  * @param options a CONFIG_OPT_END-terminated array of config_option's
       
    77  * @param name the config_option::name to look up
       
    78  * @param err returned error info if not found
       
    79  * @return a direct pointer to the config_option if found
    45  */
    80  */
    46 err_t config_apply (struct config_option *options, void *ctx, const char *name, char *value, struct error_info *err);
    81 const struct config_option* config_lookup (const struct config_option *options, const char *name, struct error_info *err);
       
    82 
       
    83 // XXX: move this into nexus
       
    84 #include "nexus.h"
       
    85 /**
       
    86  * Parse a raw value into a suitable configuration value, based on the config option type.
       
    87  *
       
    88  * Since this needs to access the application state, you need to pass in the nexus as an argument.
       
    89  *
       
    90  * Formats supported:
       
    91  *  CONFIG_IRC_CHAN         - uses a '<network>/<channel>' format and irc_client_get_chan
       
    92  *
       
    93  * @param option the option to parse the value for, use config_lookup to find it
       
    94  * @param nexus the application state
       
    95  * @param value the returned value, if succesfull
       
    96  * @param raw_value the raw value to parse based on the type
       
    97  * @param err returned error info
       
    98  */
       
    99 err_t config_parse (const struct config_option *option, struct nexus *nexus, struct config_value *value, char *raw_value, struct error_info *err);
       
   100 
       
   101 /**
       
   102  * Apply a parsed configuration value to the given option.
       
   103  *
       
   104  * The config option handlers take a context argument; the value of this depends on the implementor of the config_option.
       
   105  *
       
   106  * @param option the option to apply
       
   107  * @param ctx the context pointer for the option handler
       
   108  * @param value the parsed value
       
   109  * @param err returned error info
       
   110  */
       
   111 err_t config_apply_opt (const struct config_option *option, void *ctx, const struct config_value *value, struct error_info *err);
       
   112 
       
   113 /**
       
   114  * Apply a parsed configuration value for the named config opt.
       
   115  *
       
   116  * @param options a CONFIG_OPT_END-terminated array of config_option's
       
   117  * @param ctx the context pointer for the option handler
       
   118  * @param name the config_option::name to look up
       
   119  * @param value the parsed value
       
   120  * @param err returned error info
       
   121  */
       
   122 err_t config_apply (const struct config_option *options, void *ctx, const char *name, const struct config_value *value, struct error_info *err);
       
   123 
       
   124 /**
       
   125  * Apply a string value for the named config opt.
       
   126  *
       
   127  * @param options a CONFIG_OPT_END-terminated array of config_option's
       
   128  * @param ctx the context pointer for the option handler
       
   129  * @param name the config_option::name to look up
       
   130  * @param value the string value
       
   131  * @param err returned error info
       
   132  */
       
   133 err_t config_apply_string (const struct config_option *options, void *ctx, const char *name, char *value, struct error_info *err);
       
   134 
       
   135 /**
       
   136  * Apply an irc_chan value for the named config opt.
       
   137  *
       
   138  * @param options a CONFIG_OPT_END-terminated array of config_option's
       
   139  * @param ctx the context pointer for the option handler
       
   140  * @param name the config_option::name to look up
       
   141  * @param value the irc_chan value
       
   142  * @param err returned error info
       
   143  */
       
   144 err_t config_apply_irc_chan (const struct config_option *options, void *ctx, const char *name, struct irc_chan *value, struct error_info *err);
       
   145 
       
   146 /**
       
   147  * Parse and apply a configuration value for the named config opt.
       
   148  *
       
   149  * See config_parse() for more info.
       
   150  *
       
   151  * @param options a CONFIG_OPT_END-terminated array of config_option's
       
   152  * @param nexus the application state
       
   153  * @param ctx the context pointer for the option handler
       
   154  * @param name the config_option::name to look up
       
   155  * @param raw_value the raw value to parse
       
   156  * @param err returned error info
       
   157  *
       
   158  * @see config_parse
       
   159  */
       
   160 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);
    47 
   161 
    48 #endif
   162 #endif