Sat Jul 12 2014 17:18:32

Asterisk developer's documentation


config.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, 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 File Parser
00021  */
00022 
00023 #ifndef _ASTERISK_CONFIG_H
00024 #define _ASTERISK_CONFIG_H
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 #include "asterisk/utils.h"
00031 #include "asterisk/inline_api.h"
00032 
00033 struct ast_config;
00034 
00035 struct ast_category;
00036 
00037 /*! Options for ast_config_load()
00038  */
00039 enum {
00040    /*! Load the configuration, including comments */
00041    CONFIG_FLAG_WITHCOMMENTS  = (1 << 0),
00042    /*! On a reload, give us a -1 if the file hasn't changed. */
00043    CONFIG_FLAG_FILEUNCHANGED = (1 << 1),
00044    /*! Don't attempt to cache mtime on this config file. */
00045    CONFIG_FLAG_NOCACHE       = (1 << 2),
00046    /*! Don't attempt to load from realtime (typically called from a realtime driver dependency) */
00047    CONFIG_FLAG_NOREALTIME    = (1 << 3),
00048 };
00049 
00050 #define  CONFIG_STATUS_FILEMISSING  (void *)0
00051 #define  CONFIG_STATUS_FILEUNCHANGED   (void *)-1
00052 #define  CONFIG_STATUS_FILEINVALID  (void *)-2
00053 
00054 /*!
00055  * \brief Types used in ast_realtime_require_field
00056  */
00057 typedef enum {
00058    RQ_INTEGER1,
00059    RQ_UINTEGER1,
00060    RQ_INTEGER2,
00061    RQ_UINTEGER2,
00062    RQ_INTEGER3,
00063    RQ_UINTEGER3,
00064    RQ_INTEGER4,
00065    RQ_UINTEGER4,
00066    RQ_INTEGER8,
00067    RQ_UINTEGER8,
00068    RQ_CHAR,
00069    RQ_FLOAT,
00070    RQ_DATE,
00071    RQ_DATETIME,
00072 } require_type;
00073 
00074 /*! \brief Structure for variables, used for configurations and for channel variables */
00075 struct ast_variable {
00076    /*! Variable name.  Stored in stuff[] at struct end. */
00077    const char *name;
00078    /*! Variable value.  Stored in stuff[] at struct end. */
00079    const char *value;
00080 
00081    /*! Next node in the list. */
00082    struct ast_variable *next;
00083 
00084    /*! Filename where variable found.  Stored in stuff[] at struct end. */
00085    const char *file;
00086 
00087    int lineno;
00088    int object;    /*!< 0 for variable, 1 for object */
00089    int blanklines;   /*!< Number of blanklines following entry */
00090    struct ast_comment *precomments;
00091    struct ast_comment *sameline;
00092    struct ast_comment *trailing; /*!< the last object in the list will get assigned any trailing comments when EOF is hit */
00093    /*!
00094     * \brief Contents of file, name, and value in that order stuffed here.
00095     * \note File must be stuffed before name because of ast_include_rename().
00096     */
00097    char stuff[0];
00098 };
00099 
00100 typedef struct ast_config *config_load_func(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked);
00101 typedef struct ast_variable *realtime_var_get(const char *database, const char *table, va_list ap);
00102 typedef struct ast_config *realtime_multi_get(const char *database, const char *table, va_list ap);
00103 typedef int realtime_update(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
00104 typedef int realtime_update2(const char *database, const char *table, va_list ap);
00105 typedef int realtime_store(const char *database, const char *table, va_list ap);
00106 typedef int realtime_destroy(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
00107 
00108 /*!
00109  * \brief Function pointer called to ensure database schema is properly configured for realtime use
00110  * \since 1.6.1
00111  */
00112 typedef int realtime_require(const char *database, const char *table, va_list ap);
00113 
00114 /*!
00115  * \brief Function pointer called to clear the database cache and free resources used for such
00116  * \since 1.6.1
00117  */
00118 typedef int realtime_unload(const char *database, const char *table);
00119 
00120 /*! \brief Configuration engine structure, used to define realtime drivers */
00121 struct ast_config_engine {
00122    char *name;
00123    config_load_func *load_func;
00124    realtime_var_get *realtime_func;
00125    realtime_multi_get *realtime_multi_func;
00126    realtime_update *update_func;
00127    realtime_update2 *update2_func;
00128    realtime_store *store_func;
00129    realtime_destroy *destroy_func;
00130    realtime_require *require_func;
00131    realtime_unload *unload_func;
00132    struct ast_config_engine *next;
00133 };
00134 
00135 /*!
00136  * \brief Load a config file
00137  *
00138  * \param filename path of file to open.  If no preceding '/' character,
00139  * path is considered relative to AST_CONFIG_DIR
00140  * \param who_asked The module which is making this request.
00141  * \param flags Optional flags:
00142  * CONFIG_FLAG_WITHCOMMENTS - load the file with comments intact;
00143  * CONFIG_FLAG_FILEUNCHANGED - check the file mtime and return CONFIG_STATUS_FILEUNCHANGED if the mtime is the same; or
00144  * CONFIG_FLAG_NOCACHE - don't cache file mtime (main purpose of this option is to save memory on temporary files).
00145  *
00146  * \details
00147  * Create a config structure from a given configuration file.
00148  *
00149  * \return an ast_config data structure on success
00150  * \retval NULL on error
00151  */
00152 struct ast_config *ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags);
00153 
00154 /*!
00155  * \brief Load a config file
00156  *
00157  * \param filename path of file to open.  If no preceding '/' character,
00158  * path is considered relative to AST_CONFIG_DIR
00159  * \param flags Optional flags:
00160  * CONFIG_FLAG_WITHCOMMENTS - load the file with comments intact;
00161  * CONFIG_FLAG_FILEUNCHANGED - check the file mtime and return CONFIG_STATUS_FILEUNCHANGED if the mtime is the same; or
00162  * CONFIG_FLAG_NOCACHE - don't cache file mtime (main purpose of this option is to save memory on temporary files).
00163  *
00164  * \details
00165  * Create a config structure from a given configuration file.
00166  *
00167  * \return an ast_config data structure on success
00168  * \retval NULL on error
00169  */
00170 #define ast_config_load(filename, flags)  ast_config_load2(filename, AST_MODULE, flags)
00171 
00172 /*!
00173  * \brief Destroys a config
00174  *
00175  * \param config pointer to config data structure
00176  *
00177  * \details
00178  * Free memory associated with a given config
00179  */
00180 void ast_config_destroy(struct ast_config *config);
00181 
00182 /*!
00183  * \brief returns the root ast_variable of a config
00184  *
00185  * \param config pointer to an ast_config data structure
00186  * \param cat name of the category for which you want the root
00187  *
00188  * \return the category specified
00189  */
00190 struct ast_variable *ast_category_root(struct ast_config *config, char *cat);
00191 
00192 /*!
00193  * \brief Sorts categories in a config in the order of a numerical value contained within them.
00194  *
00195  * \param config The config structure you wish to sort
00196  * \param variable Which numerical value you wish to sort by
00197  * \param descending If true, we sort highest to lowest instead of lowest to highest
00198  *
00199  * \details
00200  * This function will assume a value of 0 for any non-numerical strings and NULL fields.
00201  */
00202 void ast_config_sort_categories(struct ast_config *config, int descending,
00203                         int (*comparator)(struct ast_category *p, struct ast_category *q));
00204 
00205 /*!
00206  * \brief Goes through categories
00207  *
00208  * \param config Which config structure you wish to "browse"
00209  * \param prev A pointer to a previous category.
00210  *
00211  * \details
00212  * This function is kind of non-intuitive in it's use.
00213  * To begin, one passes NULL as the second argument.
00214  * It will return a pointer to the string of the first category in the file.
00215  * From here on after, one must then pass the previous usage's return value
00216  * as the second pointer, and it will return a pointer to the category name
00217  * afterwards.
00218  *
00219  * \retval a category on success
00220  * \retval NULL on failure/no-more-categories
00221  */
00222 char *ast_category_browse(struct ast_config *config, const char *prev);
00223 
00224 /*!
00225  * \brief Goes through variables
00226  *
00227  * \details
00228  * Somewhat similar in intent as the ast_category_browse.
00229  * List variables of config file category
00230  *
00231  * \retval ast_variable list on success
00232  * \retval NULL on failure
00233  */
00234 struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category);
00235 
00236 /*!
00237  * \brief given a pointer to a category, return the root variable.
00238  *
00239  * \details
00240  * This is equivalent to ast_variable_browse(), but more efficient if we
00241  * already have the struct ast_category * (e.g. from ast_category_get())
00242  */
00243 struct ast_variable *ast_category_first(struct ast_category *cat);
00244 
00245 /*!
00246  * \brief Gets a variable
00247  *
00248  * \param config which (opened) config to use
00249  * \param category category under which the variable lies
00250  * \param variable which variable you wish to get the data for
00251  *
00252  * \details
00253  * Goes through a given config file in the given category and searches for the given variable
00254  *
00255  * \retval The variable value on success
00256  * \retval NULL if unable to find it.
00257  */
00258 const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable);
00259 
00260 /*!
00261  * \brief Retrieve a category if it exists
00262  *
00263  * \param config which config to use
00264  * \param category_name name of the category you're looking for
00265  *
00266  * \details
00267  * This will search through the categories within a given config file for a match.
00268  *
00269  * \retval pointer to category if found
00270  * \retval NULL if not.
00271  */
00272 struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name);
00273 
00274 /*!
00275  * \brief Check for category duplicates
00276  *
00277  * \param config which config to use
00278  * \param category_name name of the category you're looking for
00279  *
00280  * \details
00281  * This will search through the categories within a given config file for a match.
00282  *
00283  * \return non-zero if found
00284  */
00285 int ast_category_exist(const struct ast_config *config, const char *category_name);
00286 
00287 /*!
00288  * \brief Retrieve realtime configuration
00289  *
00290  * \param family which family/config to lookup
00291  *
00292  * \details
00293  * This will use builtin configuration backends to look up a particular
00294  * entity in realtime and return a variable list of its parameters.
00295  *
00296  * \note
00297  * Unlike the variables in ast_config, the resulting list of variables
00298  * MUST be freed with ast_variables_destroy() as there is no container.
00299  *
00300  * \note
00301  * The difference between these two calls is that ast_load_realtime excludes
00302  * fields whose values are NULL, while ast_load_realtime_all loads all columns.
00303  *
00304  * \note
00305  * You should use the constant SENTINEL to terminate arguments, in
00306  * order to preserve cross-platform compatibility.
00307  */
00308 struct ast_variable *ast_load_realtime(const char *family, ...) attribute_sentinel;
00309 struct ast_variable *ast_load_realtime_all(const char *family, ...) attribute_sentinel;
00310 
00311 /*!
00312  * \brief Release any resources cached for a realtime family
00313  * \since 1.6.1
00314  *
00315  * \param family which family/config to destroy
00316  *
00317  * \details
00318  * Various backends may cache attributes about a realtime data storage
00319  * facility; on reload, a front end resource may request to purge that cache.
00320  *
00321  * \retval 0 If any cache was purged
00322  * \retval -1 If no cache was found
00323  */
00324 int ast_unload_realtime(const char *family);
00325 
00326 /*!
00327  * \brief Inform realtime what fields that may be stored
00328  * \since 1.6.1
00329  *
00330  * \param family which family/config is referenced
00331  *
00332  * \details
00333  * This will inform builtin configuration backends that particular fields
00334  * may be updated during the use of that configuration section.  This is
00335  * mainly to be used during startup routines, to ensure that various fields
00336  * exist in the backend.  The backends may take various actions, such as
00337  * creating new fields in the data store or warning the administrator that
00338  * new fields may need to be created, in order to ensure proper function.
00339  *
00340  * The arguments are specified in groups of 3:  column name, column type,
00341  * and column size.  The column types are specified as integer constants,
00342  * defined by the enum require_type.  Note that the size is specified as
00343  * the number of equivalent character fields that a field may take up, even
00344  * if a field is otherwise specified as an integer type.  This is due to
00345  * the fact that some fields have historically been specified as character
00346  * types, even if they contained integer values.
00347  *
00348  * A family should always specify its fields to the minimum necessary
00349  * requirements to fulfill all possible values (within reason; for example,
00350  * a timeout value may reasonably be specified as an INTEGER2, with size 5.
00351  * Even though values above 32767 seconds are possible, they are unlikely
00352  * to be useful, and we should not complain about that size).
00353  *
00354  * \retval 0 Required fields met specified standards
00355  * \retval -1 One or more fields was missing or insufficient
00356  *
00357  * \note You should use the constant SENTINEL to terminate arguments, in
00358  * order to preserve cross-platform compatibility.
00359  */
00360 int ast_realtime_require_field(const char *family, ...) attribute_sentinel;
00361 
00362 /*!
00363  * \brief Retrieve realtime configuration
00364  *
00365  * \param family which family/config to lookup
00366  *
00367  * \details
00368  * This will use builtin configuration backends to look up a particular
00369  * entity in realtime and return a variable list of its parameters. Unlike
00370  * the ast_load_realtime, this function can return more than one entry and
00371  * is thus stored inside a traditional ast_config structure rather than
00372  * just returning a linked list of variables.
00373  *
00374  * \return An ast_config with one or more results
00375  * \retval NULL Error or no results returned
00376  *
00377  * \note You should use the constant SENTINEL to terminate arguments, in
00378  * order to preserve cross-platform compatibility.
00379  */
00380 struct ast_config *ast_load_realtime_multientry(const char *family, ...) attribute_sentinel;
00381 
00382 /*!
00383  * \brief Update realtime configuration
00384  *
00385  * \param family which family/config to be updated
00386  * \param keyfield which field to use as the key
00387  * \param lookup which value to look for in the key field to match the entry.
00388  *
00389  * \details
00390  * This function is used to update a parameter in realtime configuration space.
00391  *
00392  * \return Number of rows affected, or -1 on error.
00393  *
00394  * \note You should use the constant SENTINEL to terminate arguments, in
00395  * order to preserve cross-platform compatibility.
00396  */
00397 int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...) attribute_sentinel;
00398 
00399 /*!
00400  * \brief Update realtime configuration
00401  *
00402  * \param family which family/config to be updated
00403  *
00404  * \details
00405  * This function is used to update a parameter in realtime configuration space.
00406  * It includes the ability to lookup a row based upon multiple key criteria.
00407  * As a result, this function includes two sentinel values, one to terminate
00408  * lookup values and the other to terminate the listing of fields to update.
00409  *
00410  * \return Number of rows affected, or -1 on error.
00411  *
00412  * \note You should use the constant SENTINEL to terminate arguments, in
00413  * order to preserve cross-platform compatibility.
00414  */
00415 int ast_update2_realtime(const char *family, ...) attribute_sentinel;
00416 
00417 /*!
00418  * \brief Create realtime configuration
00419  *
00420  * \param family which family/config to be created
00421  *
00422  * \details
00423  * This function is used to create a parameter in realtime configuration space.
00424  *
00425  * \return Number of rows affected, or -1 on error.
00426  *
00427  * \note
00428  * On the MySQL engine only, for reasons of backwards compatibility, the return
00429  * value is the insert ID.  This value is nonportable and may be changed in a
00430  * future version to match the other engines.
00431  *
00432  * \note You should use the constant SENTINEL to terminate arguments, in
00433  * order to preserve cross-platform compatibility.
00434  */
00435 int ast_store_realtime(const char *family, ...) attribute_sentinel;
00436 
00437 /*!
00438  * \brief Destroy realtime configuration
00439  *
00440  * \param family which family/config to be destroyed
00441  * \param keyfield which field to use as the key
00442  * \param lookup which value to look for in the key field to match the entry.
00443  *
00444  * \details
00445  * This function is used to destroy an entry in realtime configuration space.
00446  * Additional params are used as keys.
00447  *
00448  * \return Number of rows affected, or -1 on error.
00449  *
00450  * \note You should use the constant SENTINEL to terminate arguments, in
00451  * order to preserve cross-platform compatibility.
00452  */
00453 int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) attribute_sentinel;
00454 
00455 /*!
00456  * \brief Check if realtime engine is configured for family
00457  * \param family which family/config to be checked
00458  * \return 1 if family is configured in realtime and engine exists
00459  */
00460 int ast_check_realtime(const char *family);
00461 
00462 /*! \brief Check if there's any realtime engines loaded */
00463 int ast_realtime_enabled(void);
00464 
00465 /*!
00466  * \brief Duplicate variable list
00467  * \param var the linked list of variables to clone
00468  * \return A duplicated list which you'll need to free with
00469  * ast_variables_destroy or NULL when out of memory.
00470  *
00471  * \note Do not depend on this to copy more than just name, value and filename
00472  * (the arguments to ast_variables_new).
00473  */
00474 struct ast_variable *ast_variables_dup(struct ast_variable *var);
00475 
00476 /*!
00477  * \brief Reverse a variable list
00478  * \param var the linked list of variables to reverse
00479  * \return The head of the reversed variable list
00480  *
00481  * \note The variable list var is not preserved in this function and should
00482  * not be used after reversing it.
00483  */
00484 struct ast_variable *ast_variables_reverse(struct ast_variable *var);
00485 
00486 /*!
00487  * \brief Free variable list
00488  * \param var the linked list of variables to free
00489  *
00490  * \details
00491  * This function frees a list of variables.
00492  */
00493 void ast_variables_destroy(struct ast_variable *var);
00494 
00495 /*!
00496  * \brief Register config engine
00497  * \retval 1 Always
00498  */
00499 int ast_config_engine_register(struct ast_config_engine *newconfig);
00500 
00501 /*!
00502  * \brief Deregister config engine
00503  * \retval 0 Always
00504  */
00505 int ast_config_engine_deregister(struct ast_config_engine *del);
00506 
00507 /*!
00508  * \brief Determine if a mapping exists for a given family
00509  *
00510  * \param family which family you are looking to see if a mapping exists for
00511  * \retval 1 if it is mapped
00512  * \retval 0 if it is not
00513  */
00514 int ast_realtime_is_mapping_defined(const char *family);
00515 
00516 /*!
00517  * \brief Exposed initialization method for core process
00518  *
00519  * \details
00520  * This method is intended for use only with the core initialization and is
00521  * not designed to be called from any user applications.
00522  */
00523 int register_config_cli(void);
00524 
00525 /*!
00526  * \brief Exposed re-initialization method for core process
00527  *
00528  * \details
00529  * This method is intended for use only with the core re-initialization and is
00530  * not designed to be called from any user applications.
00531  */
00532 int read_config_maps(void);
00533 
00534 /*! \brief Create a new base configuration structure */
00535 struct ast_config *ast_config_new(void);
00536 
00537 /*!
00538  * \brief Retrieve the current category name being built.
00539  *
00540  * \details
00541  * API for backend configuration engines while building a configuration set.
00542  */
00543 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg);
00544 
00545 /*!
00546  * \brief Set the category within the configuration as being current.
00547  *
00548  * \details
00549  * API for backend configuration engines while building a configuration set.
00550  */
00551 void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat);
00552 
00553 /*!
00554  * \brief Retrieve a configuration variable within the configuration set.
00555  *
00556  * \details
00557  * Retrieves the named variable \p var within category \p cat of configuration
00558  * set \p cfg.  If not found, attempts to retrieve the named variable \p var
00559  * from within category \em general.
00560  *
00561  * \return Value of \p var, or NULL if not found.
00562  */
00563 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var);
00564 
00565 /*! \brief Create a category structure */
00566 struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno);
00567 void ast_category_append(struct ast_config *config, struct ast_category *cat);
00568 
00569 /*!
00570  * \brief Inserts new category
00571  * 
00572  * \param config which config to use
00573  * \param cat newly created category to insert
00574  * \param match which category to insert above
00575  *
00576  * \details
00577  * This function is used to insert a new category above another category
00578  * matching the match parameter.
00579  */
00580 void ast_category_insert(struct ast_config *config, struct ast_category *cat, const char *match);
00581 int ast_category_delete(struct ast_config *cfg, const char *category);
00582 
00583 /*!
00584  * \brief Removes and destroys all variables within a category
00585  * \retval 0 if the category was found and emptied
00586  * \retval -1 if the category was not found
00587  */
00588 int ast_category_empty(struct ast_config *cfg, const char *category);
00589 void ast_category_destroy(struct ast_category *cat);
00590 struct ast_variable *ast_category_detach_variables(struct ast_category *cat);
00591 void ast_category_rename(struct ast_category *cat, const char *name);
00592 
00593 #ifdef MALLOC_DEBUG
00594 struct ast_variable *_ast_variable_new(const char *name, const char *value, const char *filename, const char *file, const char *function, int lineno);
00595 #define ast_variable_new(a, b, c) _ast_variable_new(a, b, c, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00596 #else
00597 struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename);
00598 #endif
00599 struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size);
00600 struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file);
00601 void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file);
00602 void ast_variable_append(struct ast_category *category, struct ast_variable *variable);
00603 void ast_variable_insert(struct ast_category *category, struct ast_variable *variable, const char *line);
00604 int ast_variable_delete(struct ast_category *category, const char *variable, const char *match, const char *line);
00605 
00606 /*!
00607  * \brief Update variable value within a config
00608  *
00609  * \param category Category element within the config
00610  * \param variable Name of the variable to change
00611  * \param value New value of the variable
00612  * \param match If set, previous value of the variable (if NULL or zero-length, no matching will be done)
00613  * \param object Boolean of whether to make the new variable an object
00614  *
00615  * \return 0 on success or -1 on failure.
00616  */
00617 int ast_variable_update(struct ast_category *category, const char *variable,
00618                   const char *value, const char *match, unsigned int object);
00619 
00620 int ast_config_text_file_save(const char *filename, const struct ast_config *cfg, const char *generator);
00621 int config_text_file_save(const char *filename, const struct ast_config *cfg, const char *generator) __attribute__((deprecated));
00622 
00623 struct ast_config *ast_config_internal_load(const char *configfile, struct ast_config *cfg, struct ast_flags flags, const char *suggested_incl_file, const char *who_asked);
00624 /*!
00625  * \brief
00626  * Copies the contents of one ast_config into another
00627  *
00628  * \note
00629  * This creates a config on the heap. The caller of this must
00630  * be prepared to free the memory returned.
00631  *
00632  * \param orig the config to copy
00633  * \return The new config on success, NULL on failure.
00634  */
00635 struct ast_config *ast_config_copy(const struct ast_config *orig);
00636 
00637 /*!
00638  * \brief
00639  * Flags that affect the behaviour of config hooks.
00640  */
00641 enum config_hook_flags {
00642    butt,
00643 };
00644 
00645 /*
00646  * \brief Callback when configuration is updated
00647  *
00648  * \param cfg A copy of the configuration that is being changed.
00649  *            This MUST be freed by the callback before returning.
00650  */
00651 typedef int (*config_hook_cb)(struct ast_config *cfg);
00652 
00653 /*!
00654  * \brief
00655  * Register a config hook for a particular file and module
00656  *
00657  * \param name The name of the hook you are registering.
00658  * \param filename The file whose config you wish to hook into.
00659  * \param module The module that is reloading the config. This
00660  *               can be useful if multiple modules may possibly
00661  *               reload the same file, but you are only interested
00662  *               when a specific module reloads the file
00663  * \param flags Flags that affect the way hooks work.
00664  * \param hook The callback to be called when config is loaded.
00665  * return 0 Success
00666  * return -1 Unsuccess, also known as UTTER AND COMPLETE FAILURE
00667  */
00668 int ast_config_hook_register(const char *name,
00669       const char *filename,
00670       const char *module,
00671       enum config_hook_flags flags,
00672       config_hook_cb hook);
00673 
00674 /*!
00675  * \brief
00676  * Unregister a config hook
00677  *
00678  * \param name The name of the hook to unregister
00679  */
00680 void ast_config_hook_unregister(const char *name);
00681 
00682 /*!
00683  * \brief Support code to parse config file arguments
00684  *
00685  * \details
00686  * The function ast_parse_arg() provides a generic interface to parse
00687  * strings (e.g. numbers, network addresses and so on) in a flexible
00688  * way, e.g. by doing proper error and bound checks, provide default
00689  * values, and so on.
00690  * The function (described later) takes a string as an argument,
00691  * a set of flags to specify the result format and checks to perform,
00692  * a pointer to the result, and optionally some additional arguments.
00693  *
00694  * \return It returns 0 on success, != 0 otherwise.
00695  */
00696 enum ast_parse_flags {
00697    /* low 4 bits of flags are used for the operand type */
00698    PARSE_TYPE  =  0x000f,
00699    /* numeric types, with optional default value and bound checks.
00700     * Additional arguments are passed by value.
00701     */
00702    PARSE_INT32 =  0x0001,
00703    PARSE_UINT32   =  0x0002,
00704    PARSE_DOUBLE   =  0x0003,
00705 #if 0 /* not supported yet */
00706    PARSE_INT16 =  0x0004,
00707    PARSE_UINT16   =  0x0005,
00708 #endif
00709 
00710    /* Returns a struct ast_sockaddr, with optional default value
00711     * (passed by reference) and port handling (accept, ignore,
00712     * require, forbid). The format is 'ipaddress[:port]'. IPv6 address
00713     * literals need square brackets around them if a port is specified.
00714     */
00715    PARSE_ADDR  =  0x000e,
00716 
00717    /* Returns a struct sockaddr_in, with optional default value
00718     * (passed by reference) and port handling (accept, ignore,
00719     * require, forbid). The format is 'host.name[:port]'
00720     */
00721    PARSE_INADDR   =  0x000f,
00722 
00723    /* Other data types can be added as needed */
00724 
00725    /* If PARSE_DEFAULT is set, next argument is a default value
00726     * which is returned in case of error. The argument is passed
00727     * by value in case of numeric types, by reference in other cases.
00728     */
00729    PARSE_DEFAULT  =  0x0010,  /* assign default on error */
00730 
00731    /* Request a range check, applicable to numbers. Two additional
00732     * arguments are passed by value, specifying the low-high end of
00733     * the range (inclusive). An error is returned if the value
00734     * is outside or inside the range, respectively.
00735     */
00736    PARSE_IN_RANGE =       0x0020, /* accept values inside a range */
00737    PARSE_OUT_RANGE =      0x0040, /* accept values outside a range */
00738    PARSE_RANGE_DEFAULTS = 0x0080, /* default to range min/max on range error */
00739 
00740    /* Port handling, for ast_sockaddr. accept/ignore/require/forbid
00741     * port number after the hostname or address.
00742     */
00743    PARSE_PORT_MASK = 0x0300, /* 0x000: accept port if present */
00744    PARSE_PORT_IGNORE =  0x0100, /* 0x100: ignore port if present */
00745    PARSE_PORT_REQUIRE = 0x0200, /* 0x200: require port number */
00746    PARSE_PORT_FORBID =  0x0300, /* 0x100: forbid port number */
00747 };
00748 
00749 /*!
00750  * \brief The argument parsing routine.
00751  *
00752  * \param arg the string to parse. It is not modified.
00753  * \param flags combination of ast_parse_flags to specify the
00754  * return type and additional checks.
00755  * \param result pointer to the result. NULL is valid here, and can
00756  * be used to perform only the validity checks.
00757  * \param ... extra arguments are required according to flags.
00758  *
00759  * \retval 0 in case of success, != 0 otherwise.
00760  * \retval result returns the parsed value in case of success,
00761  * the default value in case of error, or it is left unchanged
00762  * in case of error and no default specified. Note that in certain
00763  * cases (e.g. sockaddr_in, with multi-field return values) some
00764  * of the fields in result may be changed even if an error occurs.
00765  *
00766  * \details
00767  * Examples of use:
00768  *     ast_parse_arg("223", PARSE_INT32|PARSE_IN_RANGE, &a, -1000, 1000);
00769  * returns 0, a = 223
00770  *     ast_parse_arg("22345", PARSE_INT32|PARSE_IN_RANGE|PARSE_DEFAULT, &a, 9999, 10, 100);
00771  * returns 1, a = 9999
00772  *     ast_parse_arg("22345ssf", PARSE_UINT32|PARSE_IN_RANGE, &b, 10, 100);
00773  * returns 1, b unchanged
00774  *    ast_parse_arg("12", PARSE_UINT32|PARSE_IN_RANGE|PARSE_RANGE_DEFAULTS, &a, 1, 10);
00775  * returns 1, a = 10
00776  *    ast_parse_arg("www.foo.biz:44", PARSE_INADDR, &sa);
00777  * returns 0, sa contains address and port
00778  *    ast_parse_arg("www.foo.biz", PARSE_INADDR|PARSE_PORT_REQUIRE, &sa);
00779  * returns 1 because port is missing, sa contains address
00780  */
00781 int ast_parse_arg(const char *arg, enum ast_parse_flags flags,
00782         void *result, ...);
00783 
00784 /*
00785  * Parsing config file options in C is slightly annoying because we cannot use
00786  * string in a switch() statement, yet we need a similar behaviour, with many
00787  * branches and a break on a matching one.
00788  * The following somehow simplifies the job: we create a block using
00789  * the CV_START and CV_END macros, and then within the block we can run
00790  * actions such as "if (condition) { body; break; }"
00791  * Additional macros are present to run simple functions (e.g. ast_copy_string)
00792  * or to pass arguments to ast_parse_arg()
00793  *
00794  * As an example:
00795 
00796    CV_START(v->name, v->value);  // start the block
00797    CV_STR("foo", x_foo);      // static string
00798    CV_DSTR("bar", y_bar);     // malloc'ed string
00799    CV_F("bar", ...);    // call a generic function
00800    CV_END;           // end the block
00801  */
00802 
00803 /*! \brief the macro to open a block for variable parsing */
00804 #define CV_START(__in_var, __in_val)      \
00805    do {              \
00806       const char *__var = __in_var; \
00807       const char *__val = __in_val;
00808 
00809 /*! \brief close a variable parsing block */
00810 #define  CV_END         } while (0)
00811 
00812 /*! \brief call a generic function if the name matches. */
00813 #define  CV_F(__pattern, __body) if (!strcasecmp((__var), __pattern)) { __body; break; }
00814 
00815 /*!
00816  * \brief helper macros to assign the value to a BOOL, UINT, static string and
00817  * dynamic string
00818  */
00819 #define  CV_BOOL(__x, __dst)  CV_F(__x, (__dst) = ast_true(__val) )
00820 #define CV_UINT(__x, __dst)   CV_F(__x, (__dst) = strtoul(__val, NULL, 0) )
00821 #define CV_STR(__x, __dst) CV_F(__x, ast_copy_string(__dst, __val, sizeof(__dst)))
00822 #define CV_DSTR(__x, __dst)   CV_F(__x, ast_free(__dst); __dst = ast_strdup(__val))
00823 #define CV_STRFIELD(__x, __obj, __field) CV_F(__x, ast_string_field_set(__obj, __field, __val))
00824 
00825 /*! \brief Check if require type is an integer type */
00826 AST_INLINE_API(
00827 int ast_rq_is_int(require_type type),
00828 {
00829    switch (type) {
00830    case RQ_INTEGER1:
00831    case RQ_UINTEGER1:
00832    case RQ_INTEGER2:
00833    case RQ_UINTEGER2:
00834    case RQ_INTEGER3:
00835    case RQ_UINTEGER3:
00836    case RQ_INTEGER4:
00837    case RQ_UINTEGER4:
00838    case RQ_INTEGER8:
00839    case RQ_UINTEGER8:
00840       return 1;
00841    default:
00842       return 0;
00843    }
00844 }
00845 )
00846 
00847 /*!
00848  * \brief Remove standard encoding from realtime values, which ensures
00849  * that a semicolon embedded within a single value is not treated upon
00850  * retrieval as multiple values.
00851  * \param chunk Data to be decoded
00852  * \return The decoded data, in the original buffer
00853  * \since 1.8
00854  * \warn This function modifies the original buffer
00855  */
00856 char *ast_realtime_decode_chunk(char *chunk);
00857 
00858 /*!
00859  * \brief Encodes a chunk of data for realtime
00860  * \param dest Destination buffer
00861  * \param maxlen Length passed through to ast_str_* functions
00862  * \param chunk Source data to be encoded
00863  * \return Buffer within dest
00864  * \since 1.8
00865  */
00866 char *ast_realtime_encode_chunk(struct ast_str **dest, ssize_t maxlen, const char *chunk);
00867 
00868 #if defined(__cplusplus) || defined(c_plusplus)
00869 }
00870 #endif
00871 
00872 #endif /* _ASTERISK_CONFIG_H */