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 maybe_commit(struct cf_context *cc)
208 if (cf_commit_all(cc->postpone_commit ? CF_NO_COMMIT : cc->everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
210 if (!cc->postpone_commit)
211 cc->everything_committed = 1;
216 parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, uns depth)
218 struct cf_parser_state *p = cc->parser;
220 p = cc->parser = xmalloc_zero(sizeof(*p) + MAX_LINE);
221 p->name_parse_fb = name_fb;
224 p->line = p->line_buf;
233 err = split_command(p);
238 char *name = p->copy_buf.ptr + p->word_buf.ptr[0];
239 char *pars[p->words-1];
240 for (uns i=1; i<p->words; i++)
241 pars[i-1] = p->copy_buf.ptr + p->word_buf.ptr[i];
242 if (!strcasecmp(name, "include"))
245 err = "Expecting one filename";
247 err = "Too many nested files";
248 else if (*p->line && *p->line != '#') // because the contents of line_buf is not re-entrant and will be cleared
249 err = "The include command must be the last one on a line";
252 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
254 err = cf_printf("Cannot open file %s: %m", pars[0]);
257 uns ll = p->line_num;
258 err = parse_fastbuf(cc, stk_strdup(pars[0]), new_fb, depth+1);
266 enum cf_operation op;
267 char *c = strchr(name, ':');
269 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
272 switch (Clocase(*c)) {
273 case 's': op = OP_SET; break;
274 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
275 case 'a': switch (Clocase(c[1])) {
276 case 'p': op = OP_APPEND; break;
277 case 'f': op = OP_AFTER; break;
278 default: op = OP_ALL;
280 case 'p': op = OP_PREPEND; break;
281 case 'r': op = (c[1] && Clocase(c[2]) == 'm') ? OP_REMOVE : OP_RESET; break;
282 case 'e': op = OP_EDIT; break;
283 case 'b': op = OP_BEFORE; break;
284 default: op = OP_SET; break;
286 if (strcasecmp(c, cf_op_names[op])) {
287 err = cf_printf("Unknown operation %s", c);
291 if (p->ends_by_brace)
293 err = cf_interpret_line(cc, name, op, p->words-1, pars);
300 if (cf_done_stack(cc))
301 err = "Unterminated block";
302 else if (maybe_commit(cc))
303 err = "Commit failed";
310 msg(L_ERROR, "File %s, line %d: %s", name_fb, p->line_num, err);
311 else if (p->line_num == 1)
312 msg(L_ERROR, "Manual setting of configuration: %s", err);
314 msg(L_ERROR, "Manual setting of configuration, line %d: %s", p->line_num, err);
315 return "included from here";
319 load_file(struct cf_context *cc, const char *file)
321 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
323 msg(L_ERROR, "Cannot open configuration file %s: %m", file);
326 char *err_msg = parse_fastbuf(cc, file, fb, 0);
332 load_string(struct cf_context *cc, const char *string)
335 fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
336 char *msg = parse_fastbuf(cc, NULL, &fb, 0);
340 /* Safe loading and reloading */
342 struct conf_entry { /* We remember a list of actions to apply upon reload */
352 cf_remember_entry(struct cf_context *cc, uns type, const char *arg)
354 if (!cc->enable_journal)
356 struct conf_entry *ce = cf_malloc(sizeof(*ce));
358 ce->arg = cf_strdup(arg);
359 clist_add_tail(&cc->conf_entries, &ce->n);
363 cf_reload(const char *file)
365 struct cf_context *cc = cf_get_context();
366 ASSERT(cc->enable_journal);
368 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
369 uns ec = cc->everything_committed;
370 cc->everything_committed = 0;
373 clist_move(&old_entries, &cc->conf_entries);
378 err = load_file(cc, file);
380 CLIST_FOR_EACH(struct conf_entry *, ce, old_entries) {
381 if (ce->type == CE_FILE)
382 err |= load_file(cc, ce->arg);
384 err |= load_string(cc, ce->arg);
387 cf_remember_entry(cc, ce->type, ce->arg);
390 err |= cf_close_group();
394 cf_journal_commit_transaction(1, NULL);
396 cc->everything_committed = ec;
397 cf_journal_rollback_transaction(1, oldj);
399 clist_move(&cc->conf_entries, &old_entries);
405 cf_load(const char *file)
407 struct cf_context *cc = cf_get_context();
408 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
409 int err = load_file(cc, file);
411 cf_journal_commit_transaction(1, oldj);
412 cf_remember_entry(cc, CE_FILE, file);
413 cc->config_loaded = 1;
415 cf_journal_rollback_transaction(1, oldj);
420 cf_set(const char *string)
422 struct cf_context *cc = cf_get_context();
423 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
424 int err = load_string(cc, string);
426 cf_journal_commit_transaction(0, oldj);
427 cf_remember_entry(cc, CE_STRING, string);
429 cf_journal_rollback_transaction(0, oldj);
443 struct cf_context *cc = cf_get_context();
444 cc->postpone_commit++;
450 struct cf_context *cc = cf_get_context();
451 ASSERT(cc->postpone_commit);
452 if (!--cc->postpone_commit)
453 return maybe_commit(cc);