3 * Standalone Configuration File Parser
5 * Copyright (C) 2008 iptelorg GmbH
6 * Written by Jan Janak <jan@iptel.org>
8 * This file is part of SER, a free SIP server.
10 * SER is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the License, or (at your option) any later
15 * SER is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define MAX_TOKEN_LEN 512 /**< Token names cannot be longer than this value */
34 typedef enum cfg_flags {
35 /** Extended tokens can contain also delimiters, in addition to
36 * alpha-numeric characters, this is used on the righ side of assignments
37 * where no quotes are used.
39 CFG_EXTENDED_ALPHA = (1 << 0),
41 /** The parser performs case-insensitive comparisons of token strings by
42 * default. The parser will use case-sensitive comparison instead if this
45 CFG_CASE_SENSITIVE = (1 << 1),
47 /** This is a flag that can be set in the last element of cfg_option
48 * arrays (this is the one with 0 as token name). When this flag is set
49 * then the value or parsing function of the element will be used for
50 * options that do not match any other element in the array.
52 CFG_DEFAULT = (1 << 2),
55 /** When this flag is set then the name of the options is a prefix and all
56 * options that have the same prefix will be matched by this entry.
58 CFG_PREFIX = (1 << 3),
60 /** The result of cfg_parse_str_val will be in a buffer allocated by
61 * pkg_malloc, if the destination varaiable contains a pointer to a buffer
62 * already then it will be freed with pkg_free first.
64 CFG_STR_PKGMEM = (1 << 4),
66 /** The result of cfg_parse_str_val will be in a buffer allocated by
67 * shm_malloc, if the destination variable contains a pointer to a buffer
68 * already then it will be freed with shm_free first.
70 CFG_STR_SHMMEM = (1 << 5),
72 /** The result of cfg_parse_str_val will be in a buffer allocated by
73 * malloc, if the destination variable contains a pointer to a buffer
74 * already then it will be freed with free first.
76 CFG_STR_MALLOC = (1 << 6),
78 /** The result of cfg_parse_str_val will be copied into a pre-allocated
79 * buffer with a fixed size, a pointer to str variable which contains the
80 * buffer and its size is passed to the function in parameter 'param'.
82 CFG_STR_STATIC = (1 << 7),
94 /** Structure representing a lexical token */
95 typedef struct cfg_token {
96 char buf [MAX_TOKEN_LEN];
97 int type; /**< Token type */
98 str val; /**< Token value */
99 struct { /**< Position of first and last character of token in file */
100 int line; /**< The starting/ending line of the token */
101 int col; /**< The starting/ending column of the token */
108 typedef int (*cfg_func_f)(void* param, struct cfg_parser* st,
112 /** Token mapping structure.
113 * This structure is used to map tokens to values or function calls. Arrays of
114 * such structures are typically provided by the caller of the parser.
116 typedef struct cfg_option {
117 char* name; /**< Token name */
119 void* param; /**< Pointer to the destination variable */
120 int val; /**< Value */
121 cfg_func_f f; /**< Parser function to be called */
126 typedef struct cfg_parser {
127 FILE* f; /**< Handle of the currently open file */
128 char* file; /**< Current file name */
129 int line; /**< Current line */
130 int col; /**< Column index */
131 struct cfg_option* options; /**< Array of supported options */
133 cfg_func_f parser; /**< Section parser function */
134 void* param; /**< Parameter value for the parser function */
136 struct cfg_token* cur_opt; /**< Current option */
140 extern struct cfg_option cfg_bool_values[];
142 struct cfg_parser* cfg_parser_init(str* filename);
144 void cfg_section_parser(struct cfg_parser* st, cfg_func_f parser, void* param);
146 void cfg_set_options(struct cfg_parser* st, struct cfg_option* options);
148 int sr_cfg_parse(struct cfg_parser* st);
150 void cfg_parser_close(struct cfg_parser* st);
152 struct cfg_option* cfg_lookup_token(struct cfg_option* options, str* token);
154 /** Interface to the lexical scanner */
155 int cfg_get_token(struct cfg_token* token, struct cfg_parser* st, unsigned int flags);
157 /* Commonly needed parser functions */
159 int cfg_eat_equal(struct cfg_parser* st, unsigned int flags);
161 int cfg_eat_eol(struct cfg_parser* st, unsigned int flags);
163 /* Parse section identifier of form [section]. The function expects parameter
164 * param to be of type (str*). The result string is allocated using pkg_malloc
165 * and is zero terminated. To free the memory use pkg_free(((str*)param)->s)
167 int cfg_parse_section(void* param, struct cfg_parser* st, unsigned int flags);
169 /* Parse string parameter value, either quoted or unquoted */
170 int cfg_parse_str_opt(void* param, struct cfg_parser* st, unsigned int flags);
172 int cfg_parse_str(void* param, struct cfg_parser* st, unsigned int flags);
174 int cfg_parse_enum_opt(void* param, struct cfg_parser* st, unsigned int flags);
176 int cfg_parse_enum(void* param, struct cfg_parser* st, unsigned int flags);
178 /* Parser integer parameter value */
179 int cfg_parse_int_opt(void* param, struct cfg_parser* st, unsigned int flags);
181 int cfg_parse_int(void* param, struct cfg_parser* st, unsigned int flags);
183 /* Parse boolean parameter value */
184 int cfg_parse_bool_opt(void* param, struct cfg_parser* st, unsigned int flags);
186 int cfg_parse_bool(void* param, struct cfg_parser* st, unsigned int flags);
188 #endif /* _CFG_PARSER_H */