92060a0e |
/*
* Standalone Configuration File Parser
*
* Copyright (C) 2008 iptelorg GmbH
* Written by Jan Janak <jan@iptel.org>
*
|
02ca141b |
* This file is part of Kamailio, a free SIP server.
|
92060a0e |
*
|
02ca141b |
* Kamailio is free software; you can redistribute it and/or modify it under the
|
92060a0e |
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
|
02ca141b |
* Kamailio is distributed in the hope that it will be useful, but WITHOUT ANY
|
92060a0e |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
|
9e1ff448 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
92060a0e |
*/
|
3ca1a53f |
|
5a03489e |
/*!
* \file
|
6806e46a |
* \brief Kamailio core :: Standalone Configuration File Parser
|
02ca141b |
* Written by Jan Janak <jan@iptel.org>
|
5a03489e |
*
* \ingroup core
* Module: \ref core
*
* See \ref ConfigEngine
*/
|
92060a0e |
#ifndef _CFG_PARSER_H
#define _CFG_PARSER_H
#include "str.h"
#include <stdio.h>
|
5a03489e |
#define MAX_TOKEN_LEN 512 /*!< Token names cannot be longer than this value */
|
92060a0e |
|
5a03489e |
/*! \brief Configuration flags */
|
92060a0e |
typedef enum cfg_flags {
|
5a03489e |
/*! \brief Extended tokens can contain also delimiters, in addition to
|
92060a0e |
* alpha-numeric characters, this is used on the righ side of assignments
* where no quotes are used.
*/
CFG_EXTENDED_ALPHA = (1 << 0),
|
5a03489e |
/*! \brief The parser performs case-insensitive comparisons of token strings by
|
92060a0e |
* default. The parser will use case-sensitive comparison instead if this
* flag is set.
*/
CFG_CASE_SENSITIVE = (1 << 1),
|
5a03489e |
/*! \brief This is a flag that can be set in the last element of cfg_option
|
92060a0e |
* arrays (this is the one with 0 as token name). When this flag is set
* then the value or parsing function of the element will be used for
* options that do not match any other element in the array.
*/
CFG_DEFAULT = (1 << 2),
|
5a03489e |
/*! \brief When this flag is set then the name of the options is a prefix and all
|
92060a0e |
* options that have the same prefix will be matched by this entry.
*/
CFG_PREFIX = (1 << 3),
|
5a03489e |
/*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
|
92060a0e |
* pkg_malloc, if the destination varaiable contains a pointer to a buffer
* already then it will be freed with pkg_free first.
*/
CFG_STR_PKGMEM = (1 << 4),
|
5a03489e |
/*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
|
92060a0e |
* shm_malloc, if the destination variable contains a pointer to a buffer
* already then it will be freed with shm_free first.
*/
CFG_STR_SHMMEM = (1 << 5),
|
5a03489e |
/*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
|
92060a0e |
* malloc, if the destination variable contains a pointer to a buffer
* already then it will be freed with free first.
*/
CFG_STR_MALLOC = (1 << 6),
|
5a03489e |
/*! \brief The result of cfg_parse_str_val will be copied into a pre-allocated
|
92060a0e |
* buffer with a fixed size, a pointer to str variable which contains the
* buffer and its size is passed to the function in parameter 'param'.
*/
CFG_STR_STATIC = (1 << 7),
} cfg_flags_t;
enum cfg_token_type {
CFG_TOKEN_EOF = -1,
CFG_TOKEN_ALPHA = -2,
CFG_TOKEN_STRING = -3
};
|
5a03489e |
/*! \brief Structure representing a lexical token */
|
92060a0e |
typedef struct cfg_token {
char buf [MAX_TOKEN_LEN];
|
5a03489e |
int type; /*!< Token type */
str val; /*!< Token value */
struct { /*!< Position of first and last character of token in file */
int line; /*!< The starting/ending line of the token */
int col; /*!< The starting/ending column of the token */
|
92060a0e |
} start, end;
} cfg_token_t;
struct cfg_parser;
typedef int (*cfg_func_f)(void* param, struct cfg_parser* st,
unsigned int flags);
|
5a03489e |
/*! \brief Token mapping structure.
*
|
92060a0e |
* This structure is used to map tokens to values or function calls. Arrays of
* such structures are typically provided by the caller of the parser.
*/
typedef struct cfg_option {
|
5a03489e |
char* name; /*!< Token name */
|
92060a0e |
unsigned int flags;
|
5a03489e |
void* param; /*!< Pointer to the destination variable */
int val; /*!< Value */
cfg_func_f f; /*!< Parser function to be called */
|
92060a0e |
} cfg_option_t;
|
5a03489e |
/*! \brief Parser state */
|
92060a0e |
typedef struct cfg_parser {
|
5a03489e |
FILE* f; /*!< Handle of the currently open file */
char* file; /*!< Current file name */
int line; /*!< Current line */
int col; /*!< Column index */
struct cfg_option* options; /*!< Array of supported options */
|
92060a0e |
struct {
|
5a03489e |
cfg_func_f parser; /*!< Section parser function */
void* param; /*!< Parameter value for the parser function */
|
92060a0e |
} section;
|
5a03489e |
struct cfg_token* cur_opt; /*!< Current option */
|
92060a0e |
} cfg_parser_t;
extern struct cfg_option cfg_bool_values[];
|
13991319 |
struct cfg_parser* cfg_parser_init(str* basedir, str* filename);
|
92060a0e |
void cfg_section_parser(struct cfg_parser* st, cfg_func_f parser, void* param);
void cfg_set_options(struct cfg_parser* st, struct cfg_option* options);
|
50ca02e5 |
int sr_cfg_parse(struct cfg_parser* st);
|
92060a0e |
void cfg_parser_close(struct cfg_parser* st);
struct cfg_option* cfg_lookup_token(struct cfg_option* options, str* token);
|
ee47cfb6 |
/*! ! \brief Interface to the lexical scanner */
|
92060a0e |
int cfg_get_token(struct cfg_token* token, struct cfg_parser* st, unsigned int flags);
/* Commonly needed parser functions */
|
7ed7f2ae |
int cfg_eat_equal(struct cfg_parser* st, unsigned int flags);
int cfg_eat_eol(struct cfg_parser* st, unsigned int flags);
|
92060a0e |
|
5a03489e |
/*! \brief Parse section identifier of form [section]. The function expects parameter
|
92060a0e |
* param to be of type (str*). The result string is allocated using pkg_malloc
* and is zero terminated. To free the memory use pkg_free(((str*)param)->s)
*/
int cfg_parse_section(void* param, struct cfg_parser* st, unsigned int flags);
|
5a03489e |
/*! \brief Parse string parameter value, either quoted or unquoted */
|
7ed7f2ae |
int cfg_parse_str_opt(void* param, struct cfg_parser* st, unsigned int flags);
int cfg_parse_str(void* param, struct cfg_parser* st, unsigned int flags);
int cfg_parse_enum_opt(void* param, struct cfg_parser* st, unsigned int flags);
|
92060a0e |
|
7ed7f2ae |
int cfg_parse_enum(void* param, struct cfg_parser* st, unsigned int flags);
|
92060a0e |
|
5a03489e |
/*! \brief Parser integer parameter value */
|
7ed7f2ae |
int cfg_parse_int_opt(void* param, struct cfg_parser* st, unsigned int flags);
int cfg_parse_int(void* param, struct cfg_parser* st, unsigned int flags);
|
92060a0e |
|
5a03489e |
/*! \brief Parse boolean parameter value */
|
7ed7f2ae |
int cfg_parse_bool_opt(void* param, struct cfg_parser* st, unsigned int flags);
int cfg_parse_bool(void* param, struct cfg_parser* st, unsigned int flags);
|
92060a0e |
#endif /* _CFG_PARSER_H */
|