2 * UCW Library -- Configuration files: parsing input streams
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2006 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 "lib/getopt.h"
14 #include "lib/conf-internal.h"
15 #include "lib/mempool.h"
16 #include "lib/fastbuf.h"
17 #include "lib/chartype.h"
18 #include "lib/string.h"
19 #include "lib/stkstring.h"
25 /* Text file parser */
27 static const char *name_parse_fb;
28 static struct fastbuf *parse_fb;
32 static char line_buf[MAX_LINE];
33 static char *line = line_buf;
40 #define GBUF_PREFIX(x) split_##x
42 static split_t word_buf;
44 static uns ends_by_brace; // the line is ended by "{"
49 int err = bgets_nodie(parse_fb, line_buf, MAX_LINE);
52 *msg = err < 0 ? "Line too long" : NULL;
62 append(char *start, char *end)
64 uns len = end - start;
65 bb_grow(©_buf, copied + len + 1);
66 memcpy(copy_buf.ptr + copied, start, len);
68 copy_buf.ptr[copied-1] = 0;
72 get_word(uns is_command_name)
79 while (*line && *line != '\'')
84 copy_buf.ptr[copied-1] = '\n';
86 return msg ? : "Unterminated apostrophe word at the end";
90 } else if (*line == '"') {
92 uns start_copy = copied;
97 if (*line == '"' && !escape)
99 else if (*line == '\\')
109 copy_buf.ptr[copied-1] = '\n';
110 else // merge two lines
113 return msg ? : "Unterminated quoted word at the end";
117 char *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
119 bb_grow(©_buf, start_copy + l + 1);
120 strcpy(copy_buf.ptr + start_copy, tmp);
121 copied = start_copy + l + 1;
124 // promised that *line is non-null and non-blank
126 while (*line && !Cblank(*line)
127 && *line != '{' && *line != '}' && *line != ';'
128 && (*line != '=' || !is_command_name))
130 if (*line == '=') { // nice for setting from a command-line
132 return "Assignment without a variable";
135 if (line == start) // already the first char is control
139 while (Cblank(*line))
145 get_token(uns is_command_name, char **err)
149 if (!*line || *line == '#') {
150 if (!is_command_name || !get_line(err))
152 } else if (*line == ';') {
154 if (!is_command_name || *err)
156 } else if (*line == '\\' && !line[1]) {
157 if (!get_line(err)) {
159 *err = "Last line ends by a backslash";
162 if (!*line || *line == '#')
163 msg(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : "", line_num);
165 split_grow(&word_buf, words+1);
167 word_buf.ptr[words++] = copied;
168 *err = get_word(is_command_name);
169 return *err ? NULL : copy_buf.ptr + start;
177 words = copied = ends_by_brace = 0;
178 char *msg, *start_word;
179 if (!(start_word = get_token(1, &msg)))
181 if (*start_word == '{') // only one opening brace
182 return "Unexpected opening brace";
183 while (*line != '}') // stays for the next time
185 if (!(start_word = get_token(0, &msg)))
187 if (*start_word == '{') {
188 words--; // discard the brace
196 /* Parsing multiple files */
199 parse_fastbuf(const char *name_fb, struct fastbuf *fb, uns depth)
202 name_parse_fb = name_fb;
209 err = split_command();
214 char *name = copy_buf.ptr + word_buf.ptr[0];
216 for (uns i=1; i<words; i++)
217 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
218 if (!strcasecmp(name, "include"))
221 err = "Expecting one filename";
223 err = "Too many nested files";
224 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
225 err = "The input command must be the last one on a line";
228 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
230 err = cf_printf("Cannot open file %s: %m", pars[0]);
234 err = parse_fastbuf(stk_strdup(pars[0]), new_fb, depth+1);
242 enum cf_operation op;
243 char *c = strchr(name, ':');
245 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
248 switch (Clocase(*c)) {
249 case 's': op = OP_SET; break;
250 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
251 case 'a': switch (Clocase(c[1])) {
252 case 'p': op = OP_APPEND; break;
253 case 'f': op = OP_AFTER; break;
254 default: op = OP_ALL;
256 case 'p': op = OP_PREPEND; break;
257 case 'r': op = OP_REMOVE; break;
258 case 'e': op = OP_EDIT; break;
259 case 'b': op = OP_BEFORE; break;
260 default: op = OP_SET; break;
262 if (strcasecmp(c, cf_op_names[op])) {
263 err = cf_printf("Unknown operation %s", c);
269 err = cf_interpret_line(name, op, words-1, pars);
275 msg(L_ERROR, "File %s, line %d: %s", name_fb, line_num, err);
276 else if (line_num == 1)
277 msg(L_ERROR, "Manual setting of configuration: %s", err);
279 msg(L_ERROR, "Manual setting of configuration, line %d: %s", line_num, err);
280 return "included from here";
283 #ifndef DEFAULT_CONFIG
284 #define DEFAULT_CONFIG NULL
286 char *cf_def_file = DEFAULT_CONFIG;
288 #ifndef ENV_VAR_CONFIG
289 #define ENV_VAR_CONFIG NULL
291 char *cf_env_file = ENV_VAR_CONFIG;
293 static uns postpone_commit; // only for cf_getopt()
294 static uns everything_committed; // after the 1st load, this flag is set on
299 if (cf_check_stack())
301 if (cf_commit_all(postpone_commit ? CF_NO_COMMIT : everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
303 if (!postpone_commit)
304 everything_committed = 1;
309 load_file(const char *file)
312 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
314 msg(L_ERROR, "Cannot open %s: %m", file);
317 char *err_msg = parse_fastbuf(file, fb, 0);
319 int err = !!err_msg || done_stack();
326 load_string(const char *string)
330 fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
331 char *msg = parse_fastbuf(NULL, &fb, 0);
332 return !!msg || done_stack();
335 /* Safe loading and reloading */
338 cf_reload(const char *file)
341 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
342 uns ec = everything_committed;
343 everything_committed = 0;
344 int err = load_file(file);
348 cf_journal_commit_transaction(1, NULL);
352 everything_committed = ec;
353 cf_journal_rollback_transaction(1, oldj);
360 cf_load(const char *file)
362 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
363 int err = load_file(file);
365 cf_journal_commit_transaction(1, oldj);
367 cf_journal_rollback_transaction(1, oldj);
372 cf_set(const char *string)
374 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
375 int err = load_string(string);
377 cf_journal_commit_transaction(0, oldj);
379 cf_journal_rollback_transaction(0, oldj);
383 /* Command-line parser */
391 if (cf_env_file && (env = getenv(cf_env_file)))
394 die("Cannot load config file %s", env);
396 else if (cf_load(cf_def_file))
397 die("Cannot load default config %s", cf_def_file);
401 // We need to create an empty pool
402 cf_journal_commit_transaction(1, cf_journal_new_transaction(1));
409 if (postpone_commit) {
412 die("Cannot commit after the initialization");
417 cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
419 static int other_options = 0;
421 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
422 if (res == 'S' || res == 'C' || res == 0x64436667)
425 die("The -S and -C options must precede all other arguments");
430 die("Cannot set %s", optarg);
431 } else if (res == 'C') {
434 die("Cannot load config file %s", optarg);
437 else { /* --dumpconfig */
440 struct fastbuf *b = bfdopen(1, 4096);
447 /* unhandled option or end of options */
448 if (res != ':' && res != '?')