00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 2012, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * \brief Configuration option-handling 00021 * \author Terry Wilson <twilson@digium.com> 00022 */ 00023 00024 #ifndef _ASTERISK_CONFIG_OPTIONS_H 00025 #define _ASTERISK_CONFIG_OPTIONS_H 00026 00027 #if defined(__cplusplus) || defined(c_plusplus) 00028 extern "C" { 00029 #endif 00030 00031 #include <regex.h> 00032 00033 #include "asterisk/config.h" 00034 #include "asterisk/astobj2.h" 00035 00036 struct aco_option; 00037 struct aco_info_internal; 00038 struct aco_type_internal; 00039 00040 enum aco_type_t { 00041 ACO_GLOBAL, 00042 ACO_ITEM, 00043 }; 00044 00045 /*! \brief Whether a category regex is a blackist or a whitelist */ 00046 enum aco_category_op { 00047 ACO_BLACKLIST = 0, 00048 ACO_WHITELIST, 00049 }; 00050 00051 /*! \brief What kind of matching should be done on an option name */ 00052 enum aco_matchtype { 00053 ACO_EXACT = 1, 00054 ACO_REGEX, 00055 }; 00056 00057 /*! Callback functions for option parsing via aco_process_config() */ 00058 00059 /*! \brief Allocate a configurable ao2 object 00060 * \param category The config category the object is being generated for 00061 * \retval NULL error 00062 * \retval non-NULL a new configurable ao2 object 00063 */ 00064 typedef void *(*aco_type_item_alloc)(const char *category); 00065 00066 /*! \brief Find a item given a category and container of items 00067 * \param container The container to search for the item 00068 * \param category The category associated with the item 00069 * \retval non-NULL item from the container 00070 * \retval NULL item does not exist in container 00071 */ 00072 typedef void *(*aco_type_item_find)(struct ao2_container *newcontainer, const char *category); 00073 00074 /*! \brief Callback function that is called after a config object is initialized with defaults 00075 * 00076 * \note This callback is called during config processing after a new config is allocated and 00077 * and defaults applied but before values from the config are read. This callback could be used 00078 * to merge in settings inherited from the global settings if necessary, despite that being a 00079 * bad thing to do! 00080 * 00081 * \param newitem The newly allocated config object with defaults populated 00082 * \retval 0 succes, continue processing 00083 * \retval non-zero failure, stop processing 00084 */ 00085 typedef int (*aco_type_item_pre_process)(void *newitem); 00086 00087 /*! \brief Callback function that is called after config processing, but before linking 00088 * 00089 * \note This callback is called after config processing, but before linking the object 00090 * in the config container. This callback can be used to verify that all settings make 00091 * sense together, that required options have been set, etc. 00092 * 00093 * \param newitem The newly configured object 00094 * \retval 0 success, continue processing 00095 * \retval non-zero failure, stop processing 00096 */ 00097 typedef int (*aco_type_prelink)(void *newitem); 00098 00099 /*! \brief A function for determining whether the value for the matchfield in an aco_type is sufficient for a match 00100 * \param text The value of the option 00101 * \retval -1 The value is sufficient for a match 00102 * \retval 0 The value is not sufficient for a match 00103 */ 00104 typedef int (*aco_matchvalue_func)(const char *text); 00105 00106 /*! \struct aco_type 00107 * \brief Type information about a category-level configurable object 00108 */ 00109 struct aco_type { 00110 /* common stuff */ 00111 enum aco_type_t type; /*!< Whether this is a global or item type */ 00112 const char *category; /*!< A regular expression for matching categories to be allowed or denied */ 00113 const char *matchfield; /*!< An option name to match for this type (i.e. a 'type'-like column) */ 00114 const char *matchvalue; /*!< The value of the option to require for matching (i.e. 'peer' for type= in sip.conf) */ 00115 aco_matchvalue_func matchfunc; /*!< A function for determing whether the option value matches (i.e. hassip= requires ast_true()) */ 00116 enum aco_category_op category_match; /*!< Whether the following category regex is a whitelist or blacklist */ 00117 size_t item_offset; /*!< The offset in the config snapshot for the global config or item config container */ 00118 00119 /* non-global callbacks */ 00120 aco_type_item_alloc item_alloc; /*!< An allocation function for item associated with this type */ 00121 aco_type_item_find item_find; /*!< A callback function to find an existing item in a particular container */ 00122 aco_type_item_pre_process item_pre_process; /*!< An optional callback function that is called after defaults are applied, but before config processing */ 00123 aco_type_prelink item_prelink; /*!< An optional callback function that is called after config processing, but before applying changes */ 00124 struct aco_type_internal *internal; 00125 }; 00126 00127 /*! \brief A callback function to run just prior to applying config changes 00128 * \retval 0 Success 00129 * \retval non-zero Failure. Changes not applied 00130 */ 00131 typedef int (*aco_pre_apply_config)(void); 00132 00133 /*! \brief A callback function called only if config changes have been applied 00134 * 00135 * \note If a config file has not been edited prior to performing a reload, this 00136 * callback will not be called. 00137 */ 00138 typedef void (*aco_post_apply_config)(void); 00139 00140 /*! \brief A callback function for allocating an object to hold all config objects 00141 * \retval NULL error 00142 * \retval non-NULL a config object container 00143 */ 00144 typedef void *(*aco_snapshot_alloc)(void); 00145 00146 /*! \brief The representation of a single configuration file to be processed */ 00147 struct aco_file { 00148 const char *filename; /*!< \brief The filename to be processed */ 00149 const char *alias; /*!< \brief An alias filename to be tried if 'filename' cannot be found */ 00150 const char **preload; /*!< \brief A null-terminated oredered array of categories to be loaded first */ 00151 struct aco_type *types[]; /*!< The list of types for this config. Required. Use a sentinel! */ 00152 }; 00153 00154 struct aco_info { 00155 const char *module; /*!< The name of the module whose config is being processed */ 00156 aco_pre_apply_config pre_apply_config; /*!< A callback called after processing, but before changes are applied */ 00157 aco_post_apply_config post_apply_config;/*!< A callback called after changes are applied */ 00158 aco_snapshot_alloc snapshot_alloc; /*!< Allocate an object to hold all global configs and item containers */ 00159 struct ao2_global_obj *global_obj; /*!< The global object array that holds the user-defined config object */ 00160 struct aco_info_internal *internal; 00161 struct aco_file *files[]; /*!< An array of aco_files to process */ 00162 }; 00163 00164 /*! \brief A helper macro to ensure that aco_info types always have a sentinel */ 00165 #define ACO_TYPES(...) { __VA_ARGS__, NULL, } 00166 #define ACO_FILES(...) { __VA_ARGS__, NULL, } 00167 00168 /*! \brief Get pending config changes 00169 * \note This will most likely be called from the pre_apply_config callback function 00170 * \param info An initialized aco_info 00171 * \retval NULL error 00172 * \retval non-NULL A pointer to the user-defined config object with un-applied changes 00173 */ 00174 void *aco_pending_config(struct aco_info *info); 00175 00176 /*! \def CONFIG_INFO_STANDARD 00177 * \brief Declare an aco_info struct with default module and preload values 00178 * \param name The name of the struct 00179 * \param fn The filename of the config 00180 * \param arr The global object array for holding the user-defined config object 00181 * \param alloc The allocater for the user-defined config object 00182 * 00183 * Example: 00184 * \code 00185 * static AO2_GLOBAL_OBJ_STATIC(globals, 1); 00186 * CONFIG_INFO_STANDARD(cfg_info, globals, skel_config_alloc, 00187 * .pre_apply_config = skel_pre_apply_config, 00188 * .files = { &app_skel_conf, NULL }, 00189 * ); 00190 * ... 00191 * if (aco_info_init(&cfg_info)) { 00192 * return AST_MODULE_LOAD_DECLINE; 00193 * } 00194 * ... 00195 * aco_info_destroy(&cfg_info); 00196 * \endcode 00197 */ 00198 #define CONFIG_INFO_STANDARD(name, arr, alloc, ...) \ 00199 static struct aco_info name = { \ 00200 .module = AST_MODULE, \ 00201 .global_obj = &arr, \ 00202 .snapshot_alloc = alloc, \ 00203 __VA_ARGS__ \ 00204 }; 00205 00206 /*! \brief Initialize an aco_info structure 00207 * \note aco_info_destroy must be called if this succeeds 00208 * \param info The address of an aco_info struct to initialize 00209 * \retval 0 Success 00210 * \retval non-zero Failure 00211 */ 00212 int aco_info_init(struct aco_info *info); 00213 00214 /*! \brief Destroy an initialized aco_info struct 00215 * \param info The address of the aco_info struct to destroy 00216 */ 00217 void aco_info_destroy(struct aco_info *info); 00218 00219 /*! \brief The option types 00220 * 00221 * \note aco_option_register takes an option type which is used 00222 * to look up the handler for that type. Each non-custom type requires 00223 * field names for specific types in the struct being configured. Each 00224 * option below is commented with the field types, additional arguments 00225 * and example usage with aco_option_register 00226 */ 00227 enum aco_option_type { 00228 /*! \brief Type for default option handler for ACLs 00229 * \note aco_option_register flags: 00230 * non-zero : "permit" 00231 * 0 : "deny" 00232 * aco_option_register varargs: 00233 * FLDSET macro with the field of type struct ast_ha *. 00234 * 00235 * Example: 00236 * {code} 00237 * struct test_item { 00238 * struct ast_ha *ha; 00239 * }; 00240 * aco_option_register(&cfg_info, "permit", ACO_EXACT, my_types, NULL, OPT_ACL_T, 1, FLDSET(struct test_item, ha)); 00241 * aco_option_register(&cfg_info, "deny", ACO_EXACT, my_types, NULL, OPT_ACL_T, 0, FLDSET(struct test_item, ha)); 00242 * {code} 00243 */ 00244 OPT_ACL_T, 00245 00246 /*! \brief Type for default option handler for bools (ast_true/ast_false) 00247 * \note aco_option_register flags: 00248 * non-zero : process via ast_true 00249 * 0 : process via ast_false 00250 * aco_option_register varargs: 00251 * FLDSET macro with the field of type int. It is important to note that the field 00252 * cannot be a bitfield. If bitfields are required, they must be set via a custom handler. 00253 * 00254 * Example: 00255 * {code} 00256 * struct test_item { 00257 * int enabled; 00258 * }; 00259 aco_option_register(&cfg_info, "enabled", ACO_EXACT, my_types, "no", OPT_BOOL_T, 1, FLDSET(struct test_item, enabled)); 00260 * {endcode} 00261 */ 00262 OPT_BOOL_T, 00263 00264 /*! \brief Type for default option handler for bools (ast_true/ast_false) that are stored in a flag 00265 * \note aco_option_register flags: 00266 * non-zero : process via ast_true 00267 * 0 : process via ast_false 00268 * aco_option_register varargs: 00269 * FLDSET macro with the field of type of unsigned int. 00270 * The flag to set 00271 * 00272 * Example: 00273 * {code} 00274 * #define MY_TYPE_ISQUIET 1 << 4 00275 * struct test_item { 00276 * unsigned int flags; 00277 * }; 00278 aco_option_register(&cfg_info, "quiet", ACO_EXACT, my_types, "no", OPT_BOOLFLAG_T, 1, FLDSET(struct test_item, flags), MY_TYPE_ISQUIET); 00279 * {endcode} 00280 */ 00281 00282 OPT_BOOLFLAG_T, 00283 00284 /*! \brief Type for default option handler for character arrays 00285 * \note aco_option_register varargs: 00286 * CHARFLDSET macro with a field of type char[] 00287 * 00288 * Example: 00289 * {code} 00290 * struct test_item { 00291 * char description[128]; 00292 * }; 00293 * aco_option_register(&cfg_info, "description", ACO_EXACT, my_types, "none", OPT_CHAR_ARRAY_T, CHARFLDSET(struct test_item, description)); 00294 * {endcode} 00295 */ 00296 OPT_CHAR_ARRAY_T, 00297 00298 /*! \brief Type for default option handler for codec preferences/capabilities 00299 * \note aco_option_register flags: 00300 * non-zero : This is an "allow" style option 00301 * 0 : This is a "disallow" style option 00302 * aco_option_register varargs: 00303 * FLDSET macro with fields representing a struct ast_codec_pref and a struct ast_format_cap * 00304 * 00305 * Example: 00306 * {code} 00307 * struct test_item { 00308 * struct ast_codec_pref pref; 00309 * struct ast_format cap *cap; 00310 * }; 00311 * aco_option_register(&cfg_info, "allow", ACO_EXACT, my_types, "ulaw,alaw", OPT_CODEC_T, 1, FLDSET(struct test_item, pref, cap)); 00312 * aco_option_register(&cfg_info, "disallow", ACO_EXACT, my_types, "all", OPT_CODEC_T, 0, FLDSET(struct test_item, pref, cap)); 00313 * {endcode} 00314 */ 00315 OPT_CODEC_T, 00316 00317 /*! \brief Type for a custom (user-defined) option handler */ 00318 OPT_CUSTOM_T, 00319 00320 /*! \brief Type for default option handler for doubles 00321 * 00322 * \note aco_option_register flags: 00323 * See flags available for use with the PARSE_DOUBLE type for the ast_parse_arg function 00324 * aco_option_register varargs: 00325 * FLDSET macro with the field of type double 00326 * 00327 * Example: 00328 * struct test_item { 00329 * double dub; 00330 * }; 00331 * {code} 00332 * aco_option_register(&cfg_info, "doubleopt", ACO_EXACT, my_types, "3", OPT_DOUBLE_T, FLDSET(struct test_item, dub)); 00333 * {endcode} 00334 */ 00335 OPT_DOUBLE_T, 00336 00337 /*! \brief Type for default option handler for signed integers 00338 * 00339 * \note aco_option_register flags: 00340 * See flags available for use with the PARSE_INT32 type for the ast_parse_arg function 00341 * aco_option_register varargs: 00342 * FLDSET macro with the field of type int32_t 00343 * The remaining varargs for should be arguments compatible with the varargs for the 00344 * ast_parse_arg function with the PARSE_INT32 type and the flags passed in the 00345 * aco_option_register flags parameter. 00346 * 00347 * \note In most situations, it is preferable to not pass the PARSE_DEFAULT flag. If a config 00348 * contains an invalid value, it is better to let the config loading fail with warnings so that 00349 * the problem is fixed by the administrator. 00350 * 00351 * Example: 00352 * struct test_item { 00353 * int32_t intopt; 00354 * }; 00355 * {code} 00356 * aco_option_register(&cfg_info, "intopt", ACO_EXACT, my_types, "3", OPT_INT_T, PARSE_IN_RANGE, FLDSET(struct test_item, intopt), -10, 10); 00357 * {endcode} 00358 */ 00359 OPT_INT_T, 00360 00361 /*! \brief Type for a default handler that should do nothing 00362 * 00363 * \note This might be useful for a "type" field that is valid, but doesn't 00364 * actually need to do anything 00365 */ 00366 OPT_NOOP_T, 00367 00368 /*! \brief Type for default handler for ast_sockaddrs 00369 * 00370 * \note aco_option_register flags: 00371 * See flags available for use with the PARSE_ADDR type for the ast_parse_arg function 00372 * aco_option_register varargs: 00373 * FLDSET macro with the field being of type struct ast_sockaddr. 00374 * 00375 * Example: 00376 * {code} 00377 * struct test_item { 00378 * struct ast_sockaddr addr; 00379 * }; 00380 * aco_option_register(&cfg_info, "sockaddropt", ACO_EXACT, my_types, "0.0.0.0:1234", OPT_SOCKADDR_T, 0, FLDSET(struct test_item, addr)); 00381 * {endcode} 00382 */ 00383 OPT_SOCKADDR_T, 00384 00385 /*! \brief Type for default option handler for stringfields 00386 * \note aco_option_register flags: 00387 * none 00388 * aco_option_register varargs: 00389 * STRFLDSET macro with the field being the field created by AST_STRING_FIELD 00390 * 00391 * Example: 00392 * {code} 00393 * struct test_item { 00394 * AST_DECLARE_STRING_FIELDS( 00395 * AST_STRING_FIELD(thing); 00396 * ); 00397 * }; 00398 * aco_option_register(&cfg_info, "thing", ACO_EXACT, my_types, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct test_item, thing)); 00399 * {endcode} 00400 */ 00401 OPT_STRINGFIELD_T, 00402 00403 /*! \brief Type for default option handler for unsigned integers 00404 * 00405 * \note aco_option_register flags: 00406 * See flags available for use with the PARSE_UINT32 type for the ast_parse_arg function 00407 * aco_option_register varargs: 00408 * FLDSET macro with the field of type uint32_t 00409 * The remaining varargs for should be arguments compatible with the varargs for the 00410 * ast_parse_arg function with the PARSE_UINT32 type and the flags passed in the 00411 * aco_option_register flags parameter. 00412 * 00413 * \note In most situations, it is preferable to not pass the PARSE_DEFAULT flag. If a config 00414 * contains an invalid value, it is better to let the config loading fail with warnings so that 00415 * the problem is fixed by the administrator. 00416 * 00417 * Example: 00418 * struct test_item { 00419 * int32_t intopt; 00420 * }; 00421 * {code} 00422 * aco_option_register(&cfg_info, "uintopt", ACO_EXACT, my_types, "3", OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct test_item, uintopt), 1, 10); 00423 * {endcode} 00424 */ 00425 OPT_UINT_T, 00426 }; 00427 00428 /*! \brief A callback function for handling a particular option 00429 * \param opt The option being configured 00430 * \param var The config variable to use to configure \a obj 00431 * \param obj The object to be configured 00432 * 00433 * \retval 0 Parsing and recording the config value succeeded 00434 * \retval non-zero Failure. Parsing should stop and no reload applied 00435 */ 00436 typedef int (*aco_option_handler)(const struct aco_option *opt, struct ast_variable *var, void *obj); 00437 00438 /*! \brief Allocate a container to hold config options */ 00439 struct ao2_container *aco_option_container_alloc(void); 00440 00441 /*! \brief Return values for the aco_process functions 00442 */ 00443 enum aco_process_status { 00444 ACO_PROCESS_OK, /*!< \brief The config was processed and applied */ 00445 ACO_PROCESS_UNCHANGED, /*!< \brief The config had not been edited and no changes applied */ 00446 ACO_PROCESS_ERROR, /*!< \brief Their was an error and no changes were applied */ 00447 }; 00448 00449 /*! \brief Process a config info via the options registered with an aco_info 00450 * 00451 * \param info The config_options_info to be used for handling the config 00452 * \param reload Whether or not this is a reload 00453 * 00454 * \retval ACO_PROCESS_OK Success 00455 * \retval ACO_PROCESS_ERROR Failure 00456 * \retval ACO_PROCESS_UNCHANGED No change due to unedited config file 00457 */ 00458 enum aco_process_status aco_process_config(struct aco_info *info, int reload); 00459 00460 /*! \brief Process config info from an ast_config via options registered with an aco_info 00461 * 00462 * \param info The aco_info to be used for handling the config 00463 * \param file The file attached to aco_info that the config represents 00464 * \param cfg A pointer to a loaded ast_config to parse 00465 * \param reload Whether or not this is a reload 00466 * 00467 * \retval ACO_PROCESS_OK Success 00468 * \retval ACO_PROCESS_ERROR Failure 00469 */ 00470 enum aco_process_status aco_process_ast_config(struct aco_info *info, struct aco_file *file, struct ast_config *cfg); 00471 00472 /*! \brief Parse a single ast_variable and apply it to an object 00473 * \note This function can be used to build up an object by repeatedly passing in 00474 * the config variable name and values that would be found in a config file. This can 00475 * be useful if the object is to be populated by a dialplan function, for example. 00476 * 00477 * \param type The aco_type associated with the object 00478 * \param cat The category to use 00479 * \param var A variable to apply to the object 00480 * \param obj A pointer to the object to be configured 00481 * 00482 * \retval 0 Success 00483 * \retval -1 Failure 00484 */ 00485 int aco_process_var(struct aco_type *type, const char *cat, struct ast_variable *var, void *obj); 00486 00487 /*! \brief Parse each option defined in a config category 00488 * \param type The aco_type with the options for parsing 00489 * \param cfg The ast_config being parsed 00490 * \param cat The config category being parsed 00491 * \param obj The user-defined config object that will store the parsed config items 00492 * 00493 * \retval 0 Success 00494 * \retval -1 Failure 00495 */ 00496 int aco_process_category_options(struct aco_type *type, struct ast_config *cfg, const char *cat, void *obj); 00497 00498 /*! \brief Set all default options of \a obj 00499 * \param info The aco_type with the options 00500 * \param category The configuration category from which \a obj is being configured 00501 * \param obj The object being configured 00502 * 00503 * \retval 0 Success 00504 * \retval -1 Failure 00505 */ 00506 int aco_set_defaults(struct aco_type *type, const char *category, void *obj); 00507 00508 /*! \brief register a config option 00509 * 00510 * \note this should probably only be called by one of the aco_option_register* macros 00511 * 00512 * \param info The aco_info holding this module's config information 00513 * \param name The name of the option 00514 * \param types An array of valid option types for matching categories to the correct struct type 00515 * \param default_val The default value of the option in the same format as defined in a config file 00516 * \param type The option type (only for default handlers) 00517 * \param handler The handler function for the option (only for non-default types) 00518 * \param flags \a type specific flags, stored in the option and available to the handler 00519 * \param argc The number for variadic arguments 00520 * \param ... field offsets to store for default handlers 00521 * 00522 * \retval 0 success 00523 * \retval -1 failure 00524 */ 00525 int __aco_option_register(struct aco_info *info, const char *name, enum aco_matchtype match_type, struct aco_type **types, 00526 const char *default_val, enum aco_option_type type, aco_option_handler handler, unsigned int flags, size_t argc, ...); 00527 00528 /*! \brief Register a config option 00529 * \param info A pointer to the aco_info struct 00530 * \param name The name of the option 00531 * \param types An array of valid option types for matching categories to the correct struct type 00532 * \param default_val The default value of the option in the same format as defined in a config file 00533 * \param opt_type The option type for default option type handling 00534 * \param flags \a type specific flags, stored in the option and available to the handler 00535 * 00536 * \retval 0 Success 00537 * \retval -1 Failure 00538 */ 00539 #define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags, ...) \ 00540 __aco_option_register(info, name, matchtype, types, default_val, opt_type, NULL, flags, VA_NARGS(__VA_ARGS__), __VA_ARGS__); 00541 00542 /*! \brief Register a config option 00543 * \param info A pointer to the aco_info struct 00544 * \param name The name of the option 00545 * \param types An array of valid option types for matching categories to the correct struct type 00546 * \param default_val The default value of the option in the same format as defined in a config file 00547 * \param handler The handler callback for the option 00548 * \param flags \a type specific flags, stored in the option and available to the handler 00549 * 00550 * \retval 0 Success 00551 * \retval -1 Failure 00552 */ 00553 #define aco_option_register_custom(info, name, matchtype, types, default_val, handler, flags) \ 00554 __aco_option_register(info, name, matchtype, types, default_val, OPT_CUSTOM_T, handler, flags, 0); 00555 00556 /*! \brief Register a deprecated (and aliased) config option 00557 * \param info A pointer to the aco_info struct 00558 * \param name The name of the deprecated option 00559 * \param types An array of valid option types for matching categories to the correct struct type 00560 * \param aliased_to The name of the option that this deprecated option matches to 00561 * 00562 * \retval 0 Success 00563 * \retval -1 Failure 00564 */ 00565 int aco_option_register_deprecated(struct aco_info *info, const char *name, struct aco_type **types, const char *aliased_to); 00566 00567 /*! \note Everything below this point is to handle converting varargs 00568 * containing field names, to varargs containing a count of args, followed 00569 * by the offset of each of the field names in the struct type that is 00570 * passed in. It is currently limited to 8 arguments, but 8 variadic 00571 * arguments, like 640K, should be good enough for anyone. If not, it is 00572 * easy to add more. 00573 * */ 00574 00575 /*! \def ARGMAP(func, func_arg, x, ...) 00576 * \brief Map \a func(\a func_arg, field) across all fields including \a x 00577 * \param func The function (almost certainly offsetof) to map across the fields 00578 * \param func_arg The first argument (almost certainly a type (e.g. "struct mystruct") 00579 * \param x The first field 00580 * \param varargs The rest of the fields 00581 * 00582 * Example usage: 00583 * \code 00584 * struct foo { 00585 * int a; 00586 * char *b; 00587 * foo *c; 00588 * }; 00589 * ARGMAP(offsetof, struct foo, a, c) 00590 * \endcode 00591 * produces the string: 00592 * \code 00593 * 2, offsetof(struct foo, a), offsetof(struct foo, b) 00594 * \encode 00595 * which can be passed as the varargs to some other function 00596 * 00597 * The macro isn't limited to offsetof, but that is the only purpose for 00598 * which it has been tested. 00599 * 00600 * As an example of how the processing works: 00601 * 00602 * ARGMAP(offsetof, struct foo, a, b, c) -> 00603 * ARGMAP_(3, offsetof, struct foo, a, b, c) -> 00604 * ARGMAP_3(offsetof, struct foo, 3, a, b, c) -> 00605 * ARGMAP_2(offsetof, struct foo, ARGIFY(3, offsetof(struct foo, a)), b, c) -> 00606 * ARGMAP_1(offsetof, struct foo, ARGIFY(3, offsetof(struct foo, a), offsetof(struct foo, b)), c) -> 00607 * ARGIFY(3, offsetof(struct foo, a), offsetof(struct foo, b), offsetof(struct foo, c)) -> 00608 * 3, offsetof(struct foo, a), offsetof(struct foo, b), offsetof(struct foo, c) 00609 */ 00610 #define ARGMAP(func, func_arg, x, ...) ARGMAP_(VA_NARGS(x, ##__VA_ARGS__), func, func_arg, x, __VA_ARGS__) 00611 00612 /*! \note This is sneaky. On the very first argument, we set "in" to N, the number of arguments, so 00613 * that the accumulation both works properly for the first argument (since "in" can't be empty) and 00614 * we get the number of arguments in our varargs as a bonus */ 00615 #define ARGMAP_(N, func, func_arg, x, ...) PASTE(ARGMAP_, N)(func, func_arg, N, x, __VA_ARGS__) 00616 00617 /*! \def PASTE(arg1, arg2) 00618 * \brief Paste two arguments together, even if they are macros themselves 00619 * \note Uses two levels to handle the case where arg1 and arg2 are macros themselves 00620 */ 00621 #define PASTE(arg1, arg2) PASTE1(arg1, arg2) 00622 #define PASTE1(arg1, arg2) arg1##arg2 00623 00624 /*! \brief Take a comma-separated list and allow it to be passed as a single argument to another macro */ 00625 #define ARGIFY(...) __VA_ARGS__ 00626 00627 /*! \brief The individual field handlers for ARGMAP 00628 * \param func The function (most likely offsetof) 00629 * \param func_arg The first argument to func (most likely a type e.g. "struct my_struct") 00630 * \param in The accumulated function-mapped field names so far 00631 * \param x The next field name 00632 * \param varargs The rest of the field names 00633 */ 00634 #define ARGMAP_1(func, func_arg, in, x, ...) ARGIFY(in, func(func_arg, x)) 00635 #define ARGMAP_2(func, func_arg, in, x, ...)\ 00636 ARGMAP_1(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00637 #define ARGMAP_3(func, func_arg, in, x, ...)\ 00638 ARGMAP_2(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00639 #define ARGMAP_4(func, func_arg, in, x, ...)\ 00640 ARGMAP_3(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00641 #define ARGMAP_5(func, func_arg, in, x, ...)\ 00642 ARGMAP_4(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00643 #define ARGMAP_6(func, func_arg, in, x, ...)\ 00644 ARGMAP_5(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00645 #define ARGMAP_7(func, func_arg, in, x, ...)\ 00646 ARGMAP_6(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00647 #define ARGMAP_8(func, func_arg, in, x, ...)\ 00648 ARGMAP_7(func, func_arg, ARGIFY(in, func(func_arg, x)), __VA_ARGS__) 00649 00650 /*! \def VA_NARGS(...) 00651 * \brief Results in the number of arguments passed to it 00652 * \note Currently only up to 8, but expanding is easy. This macro basically counts 00653 * commas + 1. To visualize: 00654 * 00655 * VA_NARGS(one, two, three) -> v 00656 * VA_NARGS1(one, two, three, 8, 7, 6, 5, 4, 3, 2, 1, 0) -> 00657 * VA_NARGS1( _1, _2, _3, _4, _5, _6, _7, _8, N, ... ) N -> 3 00658 * 00659 * Note that VA_NARGS *does not* work when there are no arguments passed. Pasting an empty 00660 * __VA_ARGS__ with a comma like ", ##__VA_ARGS__" will delete the leading comma, but it 00661 * does not work when __VA_ARGS__ is the first argument. Instead, 1 is returned instead of 0: 00662 * 00663 * VA_NARGS() -> v 00664 * VA_NARGS1( , 8, 7, 6, 5, 4, 3, 2, 1, 0) -> 00665 * VA_NARGS1(_1, _2, _3, _4, _5, _6, _7, _8, N) -> 1 00666 */ 00667 #define VA_NARGS(...) VA_NARGS1(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) 00668 #define VA_NARGS1(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N 00669 00670 /*! \def FLDSET(type, ...) 00671 * \brief Convert a struct and list of fields to an argument list of field offsets 00672 * \param type The type with the fields (e.g. "struct my_struct") 00673 * \param varags The fields in the struct whose offsets are needed as arguments 00674 * 00675 * For example: 00676 * \code 00677 * struct foo {int a, char b[128], char *c}; 00678 * FLDSET(struct foo, a, c) 00679 * \endcode 00680 * 00681 * produces 00682 * \code 00683 * offsetof(struct foo, a), offsetof(struct foo, c) 00684 * \endcode 00685 */ 00686 #define FLDSET(type, ...) FLDSET1(type, ##__VA_ARGS__) 00687 #define FLDSET1(type, ...) POPPED(ARGMAP(offsetof, type, ##__VA_ARGS__)) 00688 00689 /*! \def STRFLDSET(type, ...) 00690 * \brief Convert a struct and a list of stringfield fields to an argument list of field offsets 00691 * \note Stringfields require the passing of the field manager pool, and field manager to the 00692 * default stringfield option handler, so registering options that point to stringfields requires 00693 * this macro to be called instead of the FLDSET macro. 00694 * \param type The type with the fields (e.g. "struct my_struct") 00695 * \param varargs The fields in the struct whose offsets are needed as arguments 00696 */ 00697 #define STRFLDSET(type, ...) FLDSET(type, __VA_ARGS__, __field_mgr_pool, __field_mgr) 00698 00699 /*! \def CHARFLDSET(type, field) 00700 * \brief A helper macro to pass the appropriate arguments to aco_option_register for OPT_CHAR_ARRAY_T 00701 * \note This will pass the offset of the field and its length as arguments 00702 * \param type The type with the char array field (e.g. "struct my_struct") 00703 * \param field The name of char array field 00704 */ 00705 #define CHARFLDSET(type, field) ARGIFY(offsetof(type, field), sizeof(((type *)0)->field)) 00706 00707 /*! \def POPPED(...) 00708 * \brief A list of arguments without the first argument 00709 * \note Used internally to remove the leading "number of arguments" argument from ARGMAP for 00710 * FLDSET. This is because a call to FLDSET may be followed by additional arguments in 00711 * aco_register_option, so the true number of arguments will possibly be different than what 00712 * ARGMAP returns. 00713 * \params varags A list of arguments 00714 * 00715 * POPPED(a, b, c) -> b, c 00716 */ 00717 #define POPPED(...) POPPED1(__VA_ARGS__) 00718 #define POPPED1(x, ...) __VA_ARGS__ 00719 00720 #if defined(__cplusplus) || defined(c_plusplus) 00721 } 00722 #endif 00723 00724 #endif /* _ASTERISK_CONFIG_OPTIONS_H */