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, ...) \
86 syslog(LOG_ERR, "config file %s:%d:%d: " Fmt, file, line + 1, \
87 p - linep + 1, ##__VA_ARGS__)
88 #define ADD_IN_BUFFER(Buffer, Len, Char) \
89 if ((Len) >= BUFSIZ - 1) { \
90 READ_ERROR("unreasonnable long line"); \
93 (Buffer)[(Len)++] = (Char); \
94 (Buffer)[(Len)] = '\0';
96 #define READ_NEXT(OnEOF) \
101 if (++p >= map.end) { \
104 syslog(LOG_ERR, "Read char '%c' at %d", *p, __LINE__);
105 #define READ_BLANK(OnEOF) \
107 bool in_comment = false; \
108 while (in_comment || isspace(*p) || *p == '#') { \
110 in_comment = false; \
111 } else if (*p == '#') { \
117 #define READ_TOKEN(Name, Buffer, Len) \
120 (Buffer)[0] = '\0'; \
121 if (!isalpha(*p)) { \
122 READ_ERROR("invalid %s, unexpected character '%c'", Name, *p); \
126 ADD_IN_BUFFER(Buffer, Len, *p); \
127 READ_NEXT(goto badeof) \
128 } while (isalnum(*p) || *p == '_'); \
130 #define READ_STRING(Name, Buffer, Len, OnEOF) \
133 (Buffer)[0] = '\0'; \
135 bool escaped = false; \
136 while (*p == '"') { \
137 READ_NEXT(goto badeof); \
140 READ_ERROR("string must not contain EOL"); \
142 } else if (escaped) { \
143 ADD_IN_BUFFER(Buffer, Len, *p); \
145 } else if (*p == '\\') { \
147 } else if (*p == '"') { \
148 READ_NEXT(goto badeof); \
151 ADD_IN_BUFFER(Buffer, Len, *p); \
153 READ_NEXT(goto badeof); \
155 READ_BLANK(goto badeof); \
158 READ_ERROR("%s must end with a ';'", Name); \
162 bool escaped = false; \
163 while (*p != ';' && isascii(*p) && (isprint(*p) || isspace(*p))) { \
165 if (*p == '\r' || *p == '\n') { \
166 READ_BLANK(goto badeof); \
168 ADD_IN_BUFFER(Buffer, Len, '\\'); \
174 } else if (*p == '\r' || *p == '\n') { \
175 READ_ERROR("%s must not contain EOL", Name); \
177 ADD_IN_BUFFER(Buffer, Len, *p); \
179 READ_NEXT(goto badeof); \
182 ADD_IN_BUFFER(Buffer, Len, '\\'); \
186 syslog(LOG_ERR, "string read: %s", Buffer); \
191 syslog(LOG_ERR, "read_section");
196 value[0] = key[0] = '\0';
197 value_len = key_len = 0;
200 READ_TOKEN("section name", key, key_len);
201 READ_BLANK(goto badeof);
204 READ_NEXT(goto badeof)
205 goto read_param_value;
207 READ_NEXT(goto badeof)
210 READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
215 syslog(LOG_ERR, "read_param_value: key=%s", key);
216 READ_BLANK(goto badeof);
217 READ_STRING("parameter value", value, value_len, ;);
218 /* TODO: Insert parameter in the configuration.
223 syslog(LOG_ERR, "read_filter: key=%s", key);
224 /* TODO: Create a filter with the given name.
226 READ_BLANK(goto badeof);
228 READ_TOKEN("filter parameter name", key, key_len);
229 syslog(LOG_ERR, "read parameter: key=%s", key);
230 READ_BLANK(goto badeof);
232 READ_ERROR("invalid character '%c', expected '='", *p);
235 READ_NEXT(goto badeof);
236 READ_BLANK(goto badeof);
237 READ_STRING("filter parameter value", value, value_len, goto badeof);
238 READ_BLANK(goto badeof);
239 /* TODO: Insert parameter in the filter.
243 /* TODO: Check the filter.
251 syslog(LOG_ERR, "Unexpected end of file");
254 config_delete(&config);