1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE */
21 /* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
22 /* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS */
23 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR */
24 /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF */
25 /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS */
26 /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN */
27 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) */
28 /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF */
29 /* THE POSSIBILITY OF SUCH DAMAGE. */
30 /******************************************************************************/
33 * Copyright © 2008 Florent Bruneau
48 static inline config_t *config_new(void)
50 config_t *config = p_new(config_t, 1);
51 config->entry_point = -1;
55 void config_delete(config_t **config)
58 for (int i = 0 ; i < (*config)->filters_len ; ++i) {
59 filter_wipe((*config)->filters + i);
61 p_delete(&(*config)->filters);
65 config_t *config_read(const char *file)
68 //filter_t *filter = NULL;
76 ssize_t key_len, value_len;
78 if (!file_map_open(&map, file, false)) {
82 config = config_new();
85 #define READ_ERROR(Fmt, ...) \
87 syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1, \
88 p - linep + 1, ##__VA_ARGS__); \
91 #define ADD_IN_BUFFER(Buffer, Len, Char) \
93 if ((Len) >= BUFSIZ - 1) { \
94 READ_ERROR("unreasonnable long line"); \
96 (Buffer)[(Len)++] = (Char); \
97 (Buffer)[(Len)] = '\0'; \
99 #define READ_NEXT(OnEOF) \
105 if (++p >= map.end) { \
109 #define READ_BLANK(OnEOF) \
111 bool in_comment = false; \
112 while (in_comment || isspace(*p) || *p == '#') { \
114 in_comment = false; \
115 } else if (*p == '#') { \
121 #define READ_TOKEN(Name, Buffer, Len) \
124 (Buffer)[0] = '\0'; \
125 if (!isalpha(*p)) { \
126 READ_ERROR("invalid %s, unexpected character '%c'", Name, *p); \
129 ADD_IN_BUFFER(Buffer, Len, *p); \
130 READ_NEXT(goto badeof); \
131 } while (isalnum(*p) || *p == '_'); \
133 #define READ_STRING(Name, Buffer, Len, OnEOF) \
136 (Buffer)[0] = '\0'; \
138 bool escaped = false; \
139 while (*p == '"') { \
140 READ_NEXT(goto badeof); \
143 READ_ERROR("string must not contain EOL"); \
144 } else if (escaped) { \
145 ADD_IN_BUFFER(Buffer, Len, *p); \
147 } else if (*p == '\\') { \
149 } else if (*p == '"') { \
150 READ_NEXT(goto badeof); \
153 ADD_IN_BUFFER(Buffer, Len, *p); \
155 READ_NEXT(goto badeof); \
157 READ_BLANK(goto badeof); \
160 READ_ERROR("%s must end with a ';'", Name); \
163 bool escaped = false; \
164 while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
166 if (*p == '\r' || *p == '\n') { \
167 READ_BLANK(goto badeof); \
169 ADD_IN_BUFFER(Buffer, Len, '\\'); \
175 } else if (*p == '\r' || *p == '\n') { \
176 READ_ERROR("%s must not contain EOL", Name); \
178 ADD_IN_BUFFER(Buffer, Len, *p); \
180 READ_NEXT(goto badeof); \
183 ADD_IN_BUFFER(Buffer, Len, '\\'); \
195 value[0] = key[0] = '\0';
196 value_len = key_len = 0;
199 READ_TOKEN("section name", key, key_len);
200 READ_BLANK(goto badeof);
203 READ_NEXT(goto badeof);
204 goto read_param_value;
206 READ_NEXT(goto badeof);
209 READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
213 READ_BLANK(goto badeof);
214 READ_STRING("parameter value", value, value_len, ;);
215 /* TODO: Insert parameter in the configuration.
220 /* TODO: Create a filter with the given name.
222 READ_BLANK(goto badeof);
224 READ_TOKEN("filter parameter name", key, key_len);
225 READ_BLANK(goto badeof);
227 READ_ERROR("invalid character '%c', expected '='", *p);
229 READ_NEXT(goto badeof);
230 READ_BLANK(goto badeof);
231 READ_STRING("filter parameter value", value, value_len, goto badeof);
232 READ_BLANK(goto badeof);
233 /* TODO: Insert parameter in the filter.
237 /* TODO: Check the filter.
245 syslog(LOG_ERR, "Unexpected end of file");
248 config_delete(&config);