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>
26 /* Text file parser */
33 #define GBUF_PREFIX(x) split_##x
36 struct cf_parser_state {
37 const char *name_parse_fb;
38 struct fastbuf *parse_fb;
43 uns ends_by_brace; // the line is ended by "{"
50 get_line(struct cf_parser_state *p, char **msg)
52 int err = bgets_nodie(p->parse_fb, p->line_buf, MAX_LINE);
55 *msg = err < 0 ? "Line too long" : NULL;
58 p->line = p->line_buf;
59 while (Cblank(*p->line))
65 append(struct cf_parser_state *p, char *start, char *end)
67 uns len = end - start;
68 bb_grow(&p->copy_buf, p->copied + len + 1);
69 memcpy(p->copy_buf.ptr + p->copied, start, len);
71 p->copy_buf.ptr[p->copied-1] = 0;
75 get_word(struct cf_parser_state *p, uns is_command_name)
84 while (*line && *line != '\'')
86 append(p, start, line);
89 p->copy_buf.ptr[p->copied-1] = '\n';
90 if (!get_line(p, &msg))
91 return msg ? : "Unterminated apostrophe word at the end";
96 } else if (*line == '"') {
98 uns start_copy = p->copied;
103 if (*line == '"' && !escape)
105 else if (*line == '\\')
111 append(p, start, line);
115 p->copy_buf.ptr[p->copied-1] = '\n';
116 else // merge two lines
118 if (!get_line(p, &msg))
119 return msg ? : "Unterminated quoted word at the end";
124 char *tmp = stk_str_unesc(p->copy_buf.ptr + start_copy);
126 bb_grow(&p->copy_buf, start_copy + l + 1);
127 strcpy(p->copy_buf.ptr + start_copy, tmp);
128 p->copied = start_copy + l + 1;
131 // promised that *line is non-null and non-blank
133 while (*line && !Cblank(*line)
134 && *line != '{' && *line != '}' && *line != ';'
135 && (*line != '=' || !is_command_name))
137 if (*line == '=') { // nice for setting from a command-line
139 return "Assignment without a variable";
142 if (line == start) // already the first char is control
144 append(p, start, line);
146 while (Cblank(*line))
153 get_token(struct cf_parser_state *p, uns is_command_name, char **err)
157 if (!*p->line || *p->line == '#') {
158 if (!is_command_name || !get_line(p, err))
160 } else if (*p->line == ';') {
161 *err = get_word(p, 0);
162 if (!is_command_name || *err)
164 } else if (*p->line == '\\' && !p->line[1]) {
165 if (!get_line(p, err)) {
167 *err = "Last line ends by a backslash";
170 if (!*p->line || *p->line == '#')
171 msg(L_WARN, "The line %s:%d following a backslash is empty", p->name_parse_fb ? : "", p->line_num);
173 split_grow(&p->word_buf, p->words+1);
174 uns start = p->copied;
175 p->word_buf.ptr[p->words++] = p->copied;
176 *err = get_word(p, is_command_name);
177 return *err ? NULL : p->copy_buf.ptr + start;
183 split_command(struct cf_parser_state *p)
185 p->words = p->copied = p->ends_by_brace = 0;
186 char *msg, *start_word;
187 if (!(start_word = get_token(p, 1, &msg)))
189 if (*start_word == '{') // only one opening brace
190 return "Unexpected opening brace";
191 while (*p->line != '}') // stays for the next time
193 if (!(start_word = get_token(p, 0, &msg)))
195 if (*start_word == '{') {
196 p->words--; // discard the brace
197 p->ends_by_brace = 1;
204 /* Parsing multiple files */
207 maybe_commit(struct cf_context *cc)
209 if (cf_commit_all(cc->postpone_commit ? CF_NO_COMMIT : cc->everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
211 if (!cc->postpone_commit)
212 cc->everything_committed = 1;
217 parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, uns depth)
219 struct cf_parser_state *p = cc->parser;
221 p = cc->parser = xmalloc_zero(sizeof(*p) + MAX_LINE);
222 p->name_parse_fb = name_fb;
225 p->line = p->line_buf;
234 err = split_command(p);
239 char *name = p->copy_buf.ptr + p->word_buf.ptr[0];
240 char *pars[p->words-1];
241 for (uns i=1; i<p->words; i++)
242 pars[i-1] = p->copy_buf.ptr + p->word_buf.ptr[i];
243 int optional_include = !strcasecmp(name, "optionalinclude");
244 if (optional_include || !strcasecmp(name, "include"))
247 err = "Expecting one filename";
249 err = "Too many nested files";
250 else if (*p->line && *p->line != '#') // because the contents of line_buf is not re-entrant and will be cleared
251 err = "The include command must be the last one on a line";
254 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
256 if (optional_include && errno == ENOENT)
258 err = cf_printf("Cannot open file %s: %m", pars[0]);
261 uns ll = p->line_num;
262 err = parse_fastbuf(cc, stk_strdup(pars[0]), new_fb, depth+1);
270 enum cf_operation op;
271 char *c = strchr(name, ':');
273 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
276 switch (Clocase(*c)) {
277 case 's': op = OP_SET; break;
278 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
279 case 'a': switch (Clocase(c[1])) {
280 case 'p': op = OP_APPEND; break;
281 case 'f': op = OP_AFTER; break;
282 default: op = OP_ALL;
284 case 'p': op = OP_PREPEND; break;
285 case 'r': op = (c[1] && Clocase(c[2]) == 'm') ? OP_REMOVE : OP_RESET; break;
286 case 'e': op = OP_EDIT; break;
287 case 'b': op = OP_BEFORE; break;
288 default: op = OP_SET; break;
290 if (strcasecmp(c, cf_op_names[op])) {
291 err = cf_printf("Unknown operation %s", c);
295 if (p->ends_by_brace)
297 err = cf_interpret_line(cc, name, op, p->words-1, pars);
304 if (cf_done_stack(cc))
305 err = "Unterminated block";
306 else if (maybe_commit(cc))
307 err = "Commit failed";
314 msg(L_ERROR, "File %s, line %d: %s", name_fb, p->line_num, err);
315 else if (p->line_num == 1)
316 msg(L_ERROR, "Manual setting of configuration: %s", err);
318 msg(L_ERROR, "Manual setting of configuration, line %d: %s", p->line_num, err);
319 return "included from here";
323 load_file(struct cf_context *cc, const char *file)
325 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
327 msg(L_ERROR, "Cannot open configuration file %s: %m", file);
330 char *err_msg = parse_fastbuf(cc, file, fb, 0);
336 load_string(struct cf_context *cc, const char *string)
339 fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
340 char *msg = parse_fastbuf(cc, NULL, &fb, 0);
344 /* Safe loading and reloading */
346 struct conf_entry { /* We remember a list of actions to apply upon reload */
356 cf_remember_entry(struct cf_context *cc, uns type, const char *arg)
358 if (!cc->enable_journal)
360 struct conf_entry *ce = cf_malloc(sizeof(*ce));
362 ce->arg = cf_strdup(arg);
363 clist_add_tail(&cc->conf_entries, &ce->n);
367 cf_reload(const char *file)
369 struct cf_context *cc = cf_get_context();
370 ASSERT(cc->enable_journal);
372 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
373 uns ec = cc->everything_committed;
374 cc->everything_committed = 0;
377 clist_move(&old_entries, &cc->conf_entries);
382 err = load_file(cc, file);
384 CLIST_FOR_EACH(struct conf_entry *, ce, old_entries) {
385 if (ce->type == CE_FILE)
386 err |= load_file(cc, ce->arg);
388 err |= load_string(cc, ce->arg);
391 cf_remember_entry(cc, ce->type, ce->arg);
394 err |= cf_close_group();
398 cf_journal_commit_transaction(1, NULL);
400 cc->everything_committed = ec;
401 cf_journal_rollback_transaction(1, oldj);
403 clist_move(&cc->conf_entries, &old_entries);
409 cf_load(const char *file)
411 struct cf_context *cc = cf_get_context();
412 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
413 int err = load_file(cc, file);
415 cf_journal_commit_transaction(1, oldj);
416 cf_remember_entry(cc, CE_FILE, file);
417 cc->config_loaded = 1;
419 cf_journal_rollback_transaction(1, oldj);
424 cf_set(const char *string)
426 struct cf_context *cc = cf_get_context();
427 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
428 int err = load_string(cc, string);
430 cf_journal_commit_transaction(0, oldj);
431 cf_remember_entry(cc, CE_STRING, string);
433 cf_journal_rollback_transaction(0, oldj);
447 struct cf_context *cc = cf_get_context();
448 cc->postpone_commit++;
454 struct cf_context *cc = cf_get_context();
455 ASSERT(cc->postpone_commit);
456 if (!--cc->postpone_commit)
457 return maybe_commit(cc);