forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg_parser.h
200 lines (152 loc) · 6.49 KB
/
cfg_parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* $Id$
* Standalone Configuration File Parser
*
* Copyright (C) 2008 iptelorg GmbH
* Written by Jan Janak <[email protected]>
*
* This file is part of SER, a free SIP server.
*
* SER is free software; you can redistribute it and/or modify it under the
* 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.
*
* SER is distributed in the hope that it will be useful, but WITHOUT ANY
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
* \file
* \brief SIP-router core :: Standalone Configuration File Parser
*
* \ingroup core
* Module: \ref core
*
* See \ref ConfigEngine
*/
#ifndef _CFG_PARSER_H
#define _CFG_PARSER_H
#include "str.h"
#include <stdio.h>
#define MAX_TOKEN_LEN 512 /*!< Token names cannot be longer than this value */
/*! \brief Configuration flags */
typedef enum cfg_flags {
/*! \brief Extended tokens can contain also delimiters, in addition to
* alpha-numeric characters, this is used on the righ side of assignments
* where no quotes are used.
*/
CFG_EXTENDED_ALPHA = (1 << 0),
/*! \brief The parser performs case-insensitive comparisons of token strings by
* default. The parser will use case-sensitive comparison instead if this
* flag is set.
*/
CFG_CASE_SENSITIVE = (1 << 1),
/*! \brief This is a flag that can be set in the last element of cfg_option
* 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),
/*! \brief When this flag is set then the name of the options is a prefix and all
* options that have the same prefix will be matched by this entry.
*/
CFG_PREFIX = (1 << 3),
/*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
* 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),
/*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
* 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),
/*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
* 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),
/*! \brief The result of cfg_parse_str_val will be copied into a pre-allocated
* 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
};
/*! \brief Structure representing a lexical token */
typedef struct cfg_token {
char buf [MAX_TOKEN_LEN];
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 */
} start, end;
} cfg_token_t;
struct cfg_parser;
typedef int (*cfg_func_f)(void* param, struct cfg_parser* st,
unsigned int flags);
/*! \brief Token mapping structure.
*
* 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 {
char* name; /*!< Token name */
unsigned int flags;
void* param; /*!< Pointer to the destination variable */
int val; /*!< Value */
cfg_func_f f; /*!< Parser function to be called */
} cfg_option_t;
/*! \brief Parser state */
typedef struct cfg_parser {
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 */
struct {
cfg_func_f parser; /*!< Section parser function */
void* param; /*!< Parameter value for the parser function */
} section;
struct cfg_token* cur_opt; /*!< Current option */
} cfg_parser_t;
extern struct cfg_option cfg_bool_values[];
struct cfg_parser* cfg_parser_init(str* basedir, str* filename);
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);
int sr_cfg_parse(struct cfg_parser* st);
void cfg_parser_close(struct cfg_parser* st);
struct cfg_option* cfg_lookup_token(struct cfg_option* options, str* token);
/*! ! \brief Interface to the lexical scanner */
int cfg_get_token(struct cfg_token* token, struct cfg_parser* st, unsigned int flags);
/* Commonly needed parser functions */
int cfg_eat_equal(struct cfg_parser* st, unsigned int flags);
int cfg_eat_eol(struct cfg_parser* st, unsigned int flags);
/*! \brief Parse section identifier of form [section]. The function expects parameter
* 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);
/*! \brief Parse string parameter value, either quoted or unquoted */
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);
int cfg_parse_enum(void* param, struct cfg_parser* st, unsigned int flags);
/*! \brief Parser integer parameter value */
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);
/*! \brief Parse boolean parameter value */
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);
#endif /* _CFG_PARSER_H */