2 * UCW Library -- Configuration files: parsing input streams
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2012 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include <ucw/conf-internal.h>
14 #include <ucw/clists.h>
15 #include <ucw/mempool.h>
16 #include <ucw/fastbuf.h>
17 #include <ucw/chartype.h>
18 #include <ucw/string.h>
19 #include <ucw/stkstring.h>
25 /* Text file parser */
32 #define GBUF_PREFIX(x) split_##x
35 struct cf_parser_state {
36 const char *name_parse_fb;
37 struct fastbuf *parse_fb;
42 uns ends_by_brace; // the line is ended by "{"
49 get_line(struct cf_parser_state *p, char **msg)
51 int err = bgets_nodie(p->parse_fb, p->line_buf, MAX_LINE);
54 *msg = err < 0 ? "Line too long" : NULL;
57 p->line = p->line_buf;
58 while (Cblank(*p->line))
64 append(struct cf_parser_state *p, char *start, char *end)
66 uns len = end - start;
67 bb_grow(&p->copy_buf, p->copied + len + 1);
68 memcpy(p->copy_buf.ptr + p->copied, start, len);
70 p->copy_buf.ptr[p->copied-1] = 0;
74 get_word(struct cf_parser_state *p, uns is_command_name)
83 while (*line && *line != '\'')
85 append(p, start, line);
88 p->copy_buf.ptr[p->copied-1] = '\n';
89 if (!get_line(p, &msg))
90 return msg ? : "Unterminated apostrophe word at the end";
95 } else if (*line == '"') {
97 uns start_copy = p->copied;
102 if (*line == '"' && !escape)
104 else if (*line == '\\')
110 append(p, start, line);
114 p->copy_buf.ptr[p->copied-1] = '\n';
115 else // merge two lines
117 if (!get_line(p, &msg))
118 return msg ? : "Unterminated quoted word at the end";
123 char *tmp = stk_str_unesc(p->copy_buf.ptr + start_copy);
125 bb_grow(&p->copy_buf, start_copy + l + 1);
126 strcpy(p->copy_buf.ptr + start_copy, tmp);
127 p->copied = start_copy + l + 1;
130 // promised that *line is non-null and non-blank
132 while (*line && !Cblank(*line)
133 && *line != '{' && *line != '}' && *line != ';'
134 && (*line != '=' || !is_command_name))
136 if (*line == '=') { // nice for setting from a command-line
138 return "Assignment without a variable";
141 if (line == start) // already the first char is control
143 append(p, start, line);
145 while (Cblank(*line))
152 get_token(struct cf_parser_state *p, uns is_command_name, char **err)
156 if (!*p->line || *p->line == '#') {
157 if (!is_command_name || !get_line(p, err))
159 } else if (*p->line == ';') {
160 *err = get_word(p, 0);
161 if (!is_command_name || *err)
163 } else if (*p->line == '\\' && !p->line[1]) {
164 if (!get_line(p, err)) {
166 *err = "Last line ends by a backslash";
169 if (!*p->line || *p->line == '#')
170 msg(L_WARN, "The line %s:%d following a backslash is empty", p->name_parse_fb ? : "", p->line_num);
172 split_grow(&p->word_buf, p->words+1);
173 uns start = p->copied;
174 p->word_buf.ptr[p->words++] = p->copied;
175 *err = get_word(p, is_command_name);
176 return *err ? NULL : p->copy_buf.ptr + start;
182 split_command(struct cf_parser_state *p)
184 p->words = p->copied = p->ends_by_brace = 0;
185 char *msg, *start_word;
186 if (!(start_word = get_token(p, 1, &msg)))
188 if (*start_word == '{') // only one opening brace
189 return "Unexpected opening brace";
190 while (*p->line != '}') // stays for the next time
192 if (!(start_word = get_token(p, 0, &msg)))
194 if (*start_word == '{') {
195 p->words--; // discard the brace
196 p->ends_by_brace = 1;
203 /* Parsing multiple files */
206 parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, uns depth)
208 struct cf_parser_state *p = cc->parser;
210 p = cc->parser = xmalloc_zero(sizeof(*p) + MAX_LINE);
211 p->name_parse_fb = name_fb;
214 p->line = p->line_buf;
220 err = split_command(p);
225 char *name = p->copy_buf.ptr + p->word_buf.ptr[0];
226 char *pars[p->words-1];
227 for (uns i=1; i<p->words; i++)
228 pars[i-1] = p->copy_buf.ptr + p->word_buf.ptr[i];
229 if (!strcasecmp(name, "include"))
232 err = "Expecting one filename";
234 err = "Too many nested files";
235 else if (*p->line && *p->line != '#') // because the contents of line_buf is not re-entrant and will be cleared
236 err = "The include command must be the last one on a line";
239 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
241 err = cf_printf("Cannot open file %s: %m", pars[0]);
244 uns ll = p->line_num;
245 err = parse_fastbuf(cc, stk_strdup(pars[0]), new_fb, depth+1);
253 enum cf_operation op;
254 char *c = strchr(name, ':');
256 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
259 switch (Clocase(*c)) {
260 case 's': op = OP_SET; break;
261 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
262 case 'a': switch (Clocase(c[1])) {
263 case 'p': op = OP_APPEND; break;
264 case 'f': op = OP_AFTER; break;
265 default: op = OP_ALL;
267 case 'p': op = OP_PREPEND; break;
268 case 'r': op = (c[1] && Clocase(c[2]) == 'm') ? OP_REMOVE : OP_RESET; break;
269 case 'e': op = OP_EDIT; break;
270 case 'b': op = OP_BEFORE; break;
271 default: op = OP_SET; break;
273 if (strcasecmp(c, cf_op_names[op])) {
274 err = cf_printf("Unknown operation %s", c);
278 if (p->ends_by_brace)
280 err = cf_interpret_line(cc, name, op, p->words-1, pars);
286 msg(L_ERROR, "File %s, line %d: %s", name_fb, p->line_num, err);
287 else if (p->line_num == 1)
288 msg(L_ERROR, "Manual setting of configuration: %s", err);
290 msg(L_ERROR, "Manual setting of configuration, line %d: %s", p->line_num, err);
291 return "included from here";
295 load_file(struct cf_context *cc, const char *file)
298 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
300 msg(L_ERROR, "Cannot open %s: %m", file);
303 char *err_msg = parse_fastbuf(cc, file, fb, 0);
305 return !!err_msg || cf_done_stack(cc);
309 load_string(struct cf_context *cc, const char *string)
313 fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
314 char *msg = parse_fastbuf(cc, NULL, &fb, 0);
315 return !!msg || cf_done_stack(cc);
318 /* Safe loading and reloading */
320 struct conf_entry { /* We remember a list of actions to apply upon reload */
330 cf_remember_entry(struct cf_context *cc, uns type, const char *arg)
332 if (!cc->need_journal)
334 if (!cc->postpone_commit)
336 struct conf_entry *ce = cf_malloc(sizeof(*ce));
338 ce->arg = cf_strdup(arg);
339 clist_add_tail(&cc->conf_entries, &ce->n);
343 cf_reload(const char *file)
345 struct cf_context *cc = cf_get_context();
347 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
348 uns ec = cc->everything_committed;
349 cc->everything_committed = 0;
352 clist_move(&old_entries, &cc->conf_entries);
353 cc->postpone_commit = 1;
357 err = load_file(cc, file);
359 CLIST_FOR_EACH(struct conf_entry *, ce, old_entries) {
360 if (ce->type == CE_FILE)
361 err |= load_file(cc, ce->arg);
363 err |= load_string(cc, ce->arg);
366 cf_remember_entry(cc, ce->type, ce->arg);
369 cc->postpone_commit = 0;
371 err |= cf_done_stack(cc);
375 cf_journal_commit_transaction(1, NULL);
377 cc->everything_committed = ec;
378 cf_journal_rollback_transaction(1, oldj);
380 clist_move(&cc->conf_entries, &old_entries);
386 cf_load(const char *file)
388 struct cf_context *cc = cf_get_context();
389 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
390 int err = load_file(cc, file);
392 cf_journal_commit_transaction(1, oldj);
393 cf_remember_entry(cc, CE_FILE, file);
394 cc->config_loaded = 1;
396 cf_journal_rollback_transaction(1, oldj);
401 cf_set(const char *string)
403 struct cf_context *cc = cf_get_context();
404 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
405 int err = load_string(cc, string);
407 cf_journal_commit_transaction(0, oldj);
408 cf_remember_entry(cc, CE_STRING, string);
410 cf_journal_rollback_transaction(0, oldj);