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;
75 ssize_t key_len, value_len;
77 if (!file_map_open(&map, file, false)) {
81 config = config_new();
84 #define READ_ERROR(Fmt, ...) \
85 syslog(LOG_ERR, "config file %s:%d: " Fmt, file, line, ##__VA_ARGS__)
86 #define ADD_IN_BUFFER(Buffer, Len, Char) \
87 if ((Len) >= BUFSIZ - 1) { \
88 READ_ERROR("unreasonnable long line"); \
91 (Buffer)[(Len)++] = (Char); \
92 (Buffer)[(Len)] = '\0';
94 #define READ_NEXT(OnEOF) \
95 if (++p >= map.end) { \
98 #define READ_BLANK(OnEOF) \
99 while (isblank(*p)) { \
105 #define READ_TOKEN(Name, Buffer, Len) \
108 (Buffer)[0] = '\0'; \
109 if (!isalpha(*p)) { \
110 READ_ERROR("invalid %s, unexpected character '%c'", Name, *p); \
114 ADD_IN_BUFFER(Buffer, Len, *p); \
115 if ((Len) >= BUFSIZ - 1) { \
116 READ_ERROR("unreasonnable long token"); \
119 (Buffer)[(Len)++] = *p; \
120 READ_NEXT(goto badeof) \
121 } while (isalnum(*p) || *p == '_'); \
123 #define READ_STRING(Name, Buffer, Len, OnEOF) \
126 (Buffer)[0] = '\0'; \
128 bool escaped = false; \
129 while (*p == '"') { \
130 READ_NEXT(goto badeof); \
133 READ_ERROR("string must not contain EOL"); \
135 } else if (escaped) { \
136 ADD_IN_BUFFER(Buffer, Len, *p); \
138 } else if (*p == '\\') { \
140 } else if (*p == '"') { \
141 READ_NEXT(goto badeof); \
144 ADD_IN_BUFFER(Buffer, Len, *p); \
146 READ_NEXT(goto badeof); \
148 READ_BLANK(goto badeof); \
151 READ_ERROR("%s must end with a ';'", Name); \
155 bool escaped = false; \
156 READ_NEXT(goto badeof); \
157 while (*p != ';' && isascii(*p) && isprint(*p)) { \
159 if (*p == '\r' || *p == '\n') { \
160 READ_BLANK(goto badeof); \
162 ADD_IN_BUFFER(Buffer, Len, '\\'); \
168 } else if (*p == '\r' || *p == '\n') { \
169 READ_ERROR("%s must not contain EOL", Name); \
171 ADD_IN_BUFFER(Buffer, Len, *p); \
173 READ_NEXT(goto badeof); \
176 ADD_IN_BUFFER(Buffer, Len, '\\'); \
188 value[0] = key[0] = '\0';
189 value_len = key_len = 0;
192 READ_TOKEN("section name", key, key_len);
193 READ_BLANK(goto badeof);
196 READ_NEXT(goto badeof)
197 goto read_param_value;
199 READ_NEXT(goto badeof)
202 READ_ERROR("invalid character '%c', expected '=' or '{'", *p);
207 READ_BLANK(goto badeof);
208 READ_STRING("parameter value", value, value_len, ;);
209 /* TODO: Insert parameter in the configuration.
214 /* TODO: Create a filter with the given name.
216 READ_BLANK(goto badeof);
218 READ_TOKEN("filter parameter name", key, key_len);
219 READ_BLANK(goto badeof);
220 READ_STRING("filter parameter value", value, value_len, goto badeof);
221 /* TODO: Insert parameter in the filter.
224 /* TODO: Check the filter.
232 syslog(LOG_ERR, "Unexpected end of file");
235 config_delete(&config);