2 * UCW Library -- Configuration files: parsing input streams
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2009 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/getopt.h"
14 #include "ucw/conf-internal.h"
15 #include "ucw/clists.h"
16 #include "ucw/mempool.h"
17 #include "ucw/fastbuf.h"
18 #include "ucw/chartype.h"
19 #include "ucw/string.h"
20 #include "ucw/stkstring.h"
26 /* Text file parser */
28 static const char *name_parse_fb;
29 static struct fastbuf *parse_fb;
33 static char line_buf[MAX_LINE];
34 static char *line = line_buf;
41 #define GBUF_PREFIX(x) split_##x
43 static split_t word_buf;
45 static uns ends_by_brace; // the line is ended by "{"
50 int err = bgets_nodie(parse_fb, line_buf, MAX_LINE);
53 *msg = err < 0 ? "Line too long" : NULL;
63 append(char *start, char *end)
65 uns len = end - start;
66 bb_grow(©_buf, copied + len + 1);
67 memcpy(copy_buf.ptr + copied, start, len);
69 copy_buf.ptr[copied-1] = 0;
73 get_word(uns is_command_name)
80 while (*line && *line != '\'')
85 copy_buf.ptr[copied-1] = '\n';
87 return msg ? : "Unterminated apostrophe word at the end";
91 } else if (*line == '"') {
93 uns start_copy = copied;
98 if (*line == '"' && !escape)
100 else if (*line == '\\')
110 copy_buf.ptr[copied-1] = '\n';
111 else // merge two lines
114 return msg ? : "Unterminated quoted word at the end";
118 char *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
120 bb_grow(©_buf, start_copy + l + 1);
121 strcpy(copy_buf.ptr + start_copy, tmp);
122 copied = start_copy + l + 1;
125 // promised that *line is non-null and non-blank
127 while (*line && !Cblank(*line)
128 && *line != '{' && *line != '}' && *line != ';'
129 && (*line != '=' || !is_command_name))
131 if (*line == '=') { // nice for setting from a command-line
133 return "Assignment without a variable";
136 if (line == start) // already the first char is control
140 while (Cblank(*line))
146 get_token(uns is_command_name, char **err)
150 if (!*line || *line == '#') {
151 if (!is_command_name || !get_line(err))
153 } else if (*line == ';') {
155 if (!is_command_name || *err)
157 } else if (*line == '\\' && !line[1]) {
158 if (!get_line(err)) {
160 *err = "Last line ends by a backslash";
163 if (!*line || *line == '#')
164 msg(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : "", line_num);
166 split_grow(&word_buf, words+1);
168 word_buf.ptr[words++] = copied;
169 *err = get_word(is_command_name);
170 return *err ? NULL : copy_buf.ptr + start;
178 words = copied = ends_by_brace = 0;
179 char *msg, *start_word;
180 if (!(start_word = get_token(1, &msg)))
182 if (*start_word == '{') // only one opening brace
183 return "Unexpected opening brace";
184 while (*line != '}') // stays for the next time
186 if (!(start_word = get_token(0, &msg)))
188 if (*start_word == '{') {
189 words--; // discard the brace
197 /* Parsing multiple files */
200 parse_fastbuf(const char *name_fb, struct fastbuf *fb, uns depth)
203 name_parse_fb = name_fb;
210 err = split_command();
215 char *name = copy_buf.ptr + word_buf.ptr[0];
217 for (uns i=1; i<words; i++)
218 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
219 if (!strcasecmp(name, "include"))
222 err = "Expecting one filename";
224 err = "Too many nested files";
225 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
226 err = "The input command must be the last one on a line";
229 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
231 err = cf_printf("Cannot open file %s: %m", pars[0]);
235 err = parse_fastbuf(stk_strdup(pars[0]), new_fb, depth+1);
243 enum cf_operation op;
244 char *c = strchr(name, ':');
246 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
249 switch (Clocase(*c)) {
250 case 's': op = OP_SET; break;
251 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
252 case 'a': switch (Clocase(c[1])) {
253 case 'p': op = OP_APPEND; break;
254 case 'f': op = OP_AFTER; break;
255 default: op = OP_ALL;
257 case 'p': op = OP_PREPEND; break;
258 case 'r': op = OP_REMOVE; break;
259 case 'e': op = OP_EDIT; break;
260 case 'b': op = OP_BEFORE; break;
261 default: op = OP_SET; break;
263 if (strcasecmp(c, cf_op_names[op])) {
264 err = cf_printf("Unknown operation %s", c);
270 err = cf_interpret_line(name, op, words-1, pars);
276 msg(L_ERROR, "File %s, line %d: %s", name_fb, line_num, err);
277 else if (line_num == 1)
278 msg(L_ERROR, "Manual setting of configuration: %s", err);
280 msg(L_ERROR, "Manual setting of configuration, line %d: %s", line_num, err);
281 return "included from here";
284 #ifndef DEFAULT_CONFIG
285 #define DEFAULT_CONFIG NULL
287 char *cf_def_file = DEFAULT_CONFIG;
289 #ifndef ENV_VAR_CONFIG
290 #define ENV_VAR_CONFIG NULL
292 char *cf_env_file = ENV_VAR_CONFIG;
294 static uns postpone_commit; // only for cf_getopt()
295 static uns everything_committed; // after the 1st load, this flag is set on
300 if (cf_check_stack())
302 if (cf_commit_all(postpone_commit ? CF_NO_COMMIT : everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
304 if (!postpone_commit)
305 everything_committed = 1;
310 load_file(const char *file)
313 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
315 msg(L_ERROR, "Cannot open %s: %m", file);
318 char *err_msg = parse_fastbuf(file, fb, 0);
320 return !!err_msg || done_stack();
324 load_string(const char *string)
328 fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
329 char *msg = parse_fastbuf(NULL, &fb, 0);
330 return !!msg || done_stack();
333 /* Safe loading and reloading */
335 struct conf_entry { /* We remember a list of actions to apply upon reload */
344 static clist conf_entries;
347 cf_remember_entry(uns type, const char *arg)
349 if (!cf_need_journal)
351 if (!postpone_commit)
353 struct conf_entry *ce = cf_malloc(sizeof(*ce));
355 ce->arg = cf_strdup(arg);
356 clist_add_tail(&conf_entries, &ce->n);
360 cf_reload(const char *file)
363 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
364 uns ec = everything_committed;
365 everything_committed = 0;
367 if (!conf_entries.head.next)
368 clist_init(&conf_entries);
370 clist_move(&old_entries, &conf_entries);
375 err = load_file(file);
377 CLIST_FOR_EACH(struct conf_entry *, ce, old_entries) {
378 if (ce->type == CE_FILE)
379 err |= load_file(ce->arg);
381 err |= load_string(ce->arg);
384 cf_remember_entry(ce->type, ce->arg);
393 cf_journal_commit_transaction(1, NULL);
395 everything_committed = ec;
396 cf_journal_rollback_transaction(1, oldj);
398 clist_move(&conf_entries, &old_entries);
404 cf_load(const char *file)
406 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
407 int err = load_file(file);
409 cf_journal_commit_transaction(1, oldj);
410 cf_remember_entry(CE_FILE, file);
413 cf_journal_rollback_transaction(1, oldj);
418 cf_set(const char *string)
420 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
421 int err = load_string(string);
423 cf_journal_commit_transaction(0, oldj);
424 cf_remember_entry(CE_STRING, string);
426 cf_journal_rollback_transaction(0, oldj);
430 /* Command-line parser */
438 if (cf_env_file && (env = getenv(cf_env_file)))
441 die("Cannot load config file %s", env);
443 else if (cf_load(cf_def_file))
444 die("Cannot load default config %s", cf_def_file);
448 // We need to create an empty pool
449 cf_journal_commit_transaction(1, cf_journal_new_transaction(1));
456 if (postpone_commit) {
459 die("Cannot commit after the initialization");
464 cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
466 clist_init(&conf_entries);
469 static int other_options = 0;
471 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
472 if (res == 'S' || res == 'C' || res == 0x64436667)
475 die("The -S and -C options must precede all other arguments");
479 die("Cannot set %s", optarg);
480 } else if (res == 'C') {
482 die("Cannot load config file %s", optarg);
485 else { /* --dumpconfig */
488 struct fastbuf *b = bfdopen(1, 4096);
495 /* unhandled option or end of options */
496 if (res != ':' && res != '?')