src/config.h
changeset 121 4682ebbc5644
parent 120 576bab0a1c5a
child 122 52ffbdb6bba1
equal deleted inserted replaced
120:576bab0a1c5a 121:4682ebbc5644
    13  * Different types of configuration parameters
    13  * Different types of configuration parameters
    14  */
    14  */
    15 enum config_type {
    15 enum config_type {
    16     CONFIG_INVALID,
    16     CONFIG_INVALID,
    17 
    17 
       
    18     /** No value at all */
       
    19     CONFIG_NULL,
       
    20 
    18     /** A plain NUL-terminated string */
    21     /** A plain NUL-terminated string */
    19     CONFIG_STRING,
    22     CONFIG_STRING,
       
    23 
       
    24     /** A user-defined type, further identified by a string */
       
    25     CONFIG_USER,
    20 
    26 
    21     /** An IRC channel */
    27     /** An IRC channel */
    22     CONFIG_IRC_CHAN,
    28     CONFIG_IRC_CHAN,
    23 };
    29 };
    24 
    30 
    34         /** Value for CONFIG_STRING */
    40         /** Value for CONFIG_STRING */
    35         char *string;
    41         char *string;
    36 
    42 
    37         /** Value for CONFIG_IRC_CHAN */
    43         /** Value for CONFIG_IRC_CHAN */
    38         struct irc_chan *irc_chan;
    44         struct irc_chan *irc_chan;
       
    45         
       
    46         /** Value for CONFIG_USER */
       
    47         struct {
       
    48             /** The specific user type */
       
    49             const char *type;
       
    50 
       
    51             /** A pointer to the user type */
       
    52             void *ptr;
       
    53         } user;
    39     };
    54     };
    40 };
    55 };
    41 
    56 
    42 /**
    57 /**
    43  * Structure to define a single parameter for an option
    58  * Structure to define a single parameter for an option
    46     /** The name of the arg */
    61     /** The name of the arg */
    47     const char *name;
    62     const char *name;
    48 
    63 
    49     /** The type */
    64     /** The type */
    50     enum config_type type;
    65     enum config_type type;
       
    66 
       
    67     /** The specific type for CONFIG_USER */
       
    68     const char *user_type;
    51 
    69 
    52     /** Description */
    70     /** Description */
    53     const char *description;
    71     const char *description;
    54 
    72 
    55     /** Optional value handler function, by type */
    73     /** Optional value handler function, by type */
    58         err_t (*irc_chan) (void *ctx, struct irc_chan *chan, struct error_info *err);
    76         err_t (*irc_chan) (void *ctx, struct irc_chan *chan, struct error_info *err);
    59     } func;
    77     } func;
    60 
    78 
    61     /** Use handler function? */
    79     /** Use handler function? */
    62     bool is_handler;
    80     bool is_handler;
       
    81 
       
    82     /** Optional value? */
       
    83     bool optional;
    63 };
    84 };
    64 
    85 
    65 /**
    86 /**
    66  * The maximum number of parameters that a single option can have, although this includes the terminating NULL
    87  * The maximum number of parameters that a single option can have, although this includes the terminating NULL
    67  */
    88  */
    81 
   102 
    82     /** The list of parameters */
   103     /** The list of parameters */
    83     const struct config_param params[CONFIG_PARAMS_MAX];
   104     const struct config_param params[CONFIG_PARAMS_MAX];
    84     
   105     
    85     /** The handler function */
   106     /** The handler function */
    86     err_t (*func) (void *ctx, const struct config_value values[], struct error_info *err);
   107     err_t (*func) (void *ctx, const struct config_option *option, const struct config_value values[], struct error_info *err);
    87 
   108 
    88     /** Help text */
   109     /** Help text */
    89     const char *help;
   110     const char *help;
    90 };
   111 };
    91 
   112 
    92 #define CONFIG_PARAM(name, type, desc) \
   113 #define CONFIG_PARAM(name, type, desc, optional) \
    93     {   name, type, desc, { NULL }, false }
   114     {   name, type, NULL, desc, { NULL }, false, optional }
       
   115 
       
   116 #define CONFIG_PARAM_OPTIONAL(name, type, desc) \
       
   117     CONFIG_PARAM(name, type, desc, true)
       
   118 
       
   119 #define CONFIG_PARAM_USER(name, user_type, desc, optional) \
       
   120     {   name, CONFIG_USER, user_type, desc, { NULL }, false, optional }
       
   121 
       
   122 #define CONFIG_PARAM_USER_OPTIONAL(name, user_type, desc) \
       
   123     CONFIG_PARAM_USER(name, user_type, desc, true) 
    94 
   124 
    95 #define CONFIG_PARAM_HANDLER(name, type, desc, _func_name_, func) \
   125 #define CONFIG_PARAM_HANDLER(name, type, desc, _func_name_, func) \
    96     {   name, type, desc, {._func_name_ = func }, true }
   126     {   name, type, NULL, desc, {._func_name_ = func }, true, false }
    97 
   127 
    98 #define CONFIG_OPT(name, params, func, help) \
   128 #define CONFIG_PARAM_END \
    99     {   name, params, func, help }
   129     {   NULL, 0, NULL, NULL, { NULL }, false, false }
       
   130 
       
   131 #define CONFIG_OPT(name, func, help, ...) \
       
   132     {   name, { __VA_ARGS__, CONFIG_PARAM_END }, func, help }
   100 
   133 
   101 #define CONFIG_OPT_STRING(name, func, desc, help) \
   134 #define CONFIG_OPT_STRING(name, func, desc, help) \
   102     {   name, { CONFIG_PARAM_HANDLER(name, CONFIG_STRING, desc, string, func) }, NULL, help }
   135     CONFIG_OPT(name, NULL, help, CONFIG_PARAM_HANDLER(name, CONFIG_STRING, desc, string, func))
   103 
   136 
   104 #define CONFIG_OPT_IRC_CHAN(name, func, desc, help) \
   137 #define CONFIG_OPT_IRC_CHAN(name, func, desc, help) \
   105     {   name, { CONFIG_PARAM_HANDLER(name, CONFIG_IRC_CHAN, desc, irc_chan, func) }, NULL, help }
   138     CONFIG_OPT(name, NULL, help, CONFIG_PARAM_HANDLER(name, CONFIG_IRC_CHAN, desc, irc_chan, func))
   106 
   139 
   107 #define CONFIG_OPT_END \
   140 #define CONFIG_OPT_END \
   108     {   NULL,   {}, NULL, NULL }
   141     {   NULL,   {}, NULL, NULL }
   109 
   142 
       
   143 #define CONFIG_OPTIONS(...) \
       
   144     { __VA_ARGS__, CONFIG_OPT_END }
       
   145 
   110 #define CONFIG_VALUE(type, _name_, value) \
   146 #define CONFIG_VALUE(type, _name_, value) \
   111     {   (type), { ._name_ = (value) } }
   147     {   (type), { ._name_ = value } }
   112 
   148 
   113 #define CONFIG_VALUE_STRING(str_value) \
   149 #define CONFIG_VALUE_STRING(str_value) \
   114     CONFIG_VALUE(CONFIG_STRING, string, str_value)
   150     CONFIG_VALUE(CONFIG_STRING, string, (str_value))
   115 
   151 
   116 #define CONFIG_VALUE_IRC_CHAN(irc_chan_value) \
   152 #define CONFIG_VALUE_IRC_CHAN(irc_chan_value) \
   117     CONFIG_VALUE(CONFIG_IRC_CHAN, irc_chan, irc_chan_value)
   153     CONFIG_VALUE(CONFIG_IRC_CHAN, irc_chan, (irc_chan_value))
       
   154 
       
   155 #define CONFIG_VALUE_USER(user_type, user_value) \
       
   156     CONFIG_VALUE(CONFIG_USER, user, { user_type, user_value })
   118 
   157 
   119 #define CONFIG_VALUE_END \
   158 #define CONFIG_VALUE_END \
   120     {   0, { NULL } }
   159     {   0, { NULL } }
   121 
   160 
   122 /**
   161 /**
   204  * @param raw_value the raw value to parse
   243  * @param raw_value the raw value to parse
   205  * @param err returned error info
   244  * @param err returned error info
   206  *
   245  *
   207  * @see config_parse
   246  * @see config_parse
   208  */
   247  */
   209 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);
   248 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);
       
   249 
       
   250 /**
       
   251  * Lookup a config_value by name from the given list of config_values for the given config_option
       
   252  */
       
   253 const struct config_value* config_get_value (const struct config_option *option, const struct config_value values[], const char *name);
       
   254 
       
   255 const char* config_get_string (const struct config_option *option, const struct config_value values[], const char *name);
       
   256 struct irc_chan* config_get_irc_chan (const struct config_option *option, const struct config_value values[], const char *name);
       
   257 void* config_get_user (const struct config_option *option, const struct config_value values[], const char *name, const char *user_type);
   210 
   258 
   211 #endif
   259 #endif