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/stkstring.h"
24 /* Text file parser */
26 static const char *name_parse_fb;
27 static struct fastbuf *parse_fb;
31 static char line_buf[MAX_LINE];
32 static char *line = line_buf;
39 #define GBUF_PREFIX(x) split_##x
41 static split_t word_buf;
43 static uns ends_by_brace; // the line is ended by "{"
48 int err = bgets_nodie(parse_fb, line_buf, MAX_LINE);
51 *msg = err < 0 ? "Line too long" : NULL;
61 append(char *start, char *end)
63 uns len = end - start;
64 bb_grow(©_buf, copied + len + 1);
65 memcpy(copy_buf.ptr + copied, start, len);
67 copy_buf.ptr[copied-1] = 0;
71 get_word(uns is_command_name)
78 while (*line && *line != '\'')
83 copy_buf.ptr[copied-1] = '\n';
85 return msg ? : "Unterminated apostrophe word at the end";
89 } else if (*line == '"') {
91 uns start_copy = copied;
96 if (*line == '"' && !escape)
98 else if (*line == '\\')
108 copy_buf.ptr[copied-1] = '\n';
109 else // merge two lines
112 return msg ? : "Unterminated quoted word at the end";
116 char *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
118 bb_grow(©_buf, start_copy + l + 1);
119 strcpy(copy_buf.ptr + start_copy, tmp);
120 copied = start_copy + l + 1;
123 // promised that *line is non-null and non-blank
125 while (*line && !Cblank(*line)
126 && *line != '{' && *line != '}' && *line != ';'
127 && (*line != '=' || !is_command_name))
129 if (*line == '=') { // nice for setting from a command-line
131 return "Assignment without a variable";
134 if (line == start) // already the first char is control
138 while (Cblank(*line))
144 get_token(uns is_command_name, char **err)
148 if (!*line || *line == '#') {
149 if (!is_command_name || !get_line(err))
151 } else if (*line == ';') {
153 if (!is_command_name || *err)
155 } else if (*line == '\\' && !line[1]) {
156 if (!get_line(err)) {
158 *err = "Last line ends by a backslash";
161 if (!*line || *line == '#')
162 msg(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : "", line_num);
164 split_grow(&word_buf, words+1);
166 word_buf.ptr[words++] = copied;
167 *err = get_word(is_command_name);
168 return *err ? NULL : copy_buf.ptr + start;
176 words = copied = ends_by_brace = 0;
177 char *msg, *start_word;
178 if (!(start_word = get_token(1, &msg)))
180 if (*start_word == '{') // only one opening brace
181 return "Unexpected opening brace";
182 while (*line != '}') // stays for the next time
184 if (!(start_word = get_token(0, &msg)))
186 if (*start_word == '{') {
187 words--; // discard the brace
195 /* Parsing multiple files */
198 parse_fastbuf(const char *name_fb, struct fastbuf *fb, uns depth)
201 name_parse_fb = name_fb;
208 err = split_command();
213 char *name = copy_buf.ptr + word_buf.ptr[0];
215 for (uns i=1; i<words; i++)
216 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
217 if (!strcasecmp(name, "include"))
220 err = "Expecting one filename";
222 err = "Too many nested files";
223 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
224 err = "The input command must be the last one on a line";
227 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
229 err = cf_printf("Cannot open file %s: %m", pars[0]);
233 err = parse_fastbuf(stk_strdup(pars[0]), new_fb, depth+1);
241 enum cf_operation op;
242 char *c = strchr(name, ':');
244 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
247 switch (Clocase(*c)) {
248 case 's': op = OP_SET; break;
249 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
250 case 'a': switch (Clocase(c[1])) {
251 case 'p': op = OP_APPEND; break;
252 case 'f': op = OP_AFTER; break;
253 default: op = OP_ALL;
255 case 'p': op = OP_PREPEND; break;
256 case 'r': op = OP_REMOVE; break;
257 case 'e': op = OP_EDIT; break;
258 case 'b': op = OP_BEFORE; break;
259 default: op = OP_SET; break;
261 if (strcasecmp(c, cf_op_names[op])) {
262 err = cf_printf("Unknown operation %s", c);
268 err = cf_interpret_line(name, op, words-1, pars);
274 msg(L_ERROR, "File %s, line %d: %s", name_fb, line_num, err);
275 else if (line_num == 1)
276 msg(L_ERROR, "Manual setting of configuration: %s", err);
278 msg(L_ERROR, "Manual setting of configuration, line %d: %s", line_num, err);
279 return "included from here";
282 #ifndef DEFAULT_CONFIG
283 #define DEFAULT_CONFIG NULL
285 char *cf_def_file = DEFAULT_CONFIG;
287 #ifndef ENV_VAR_CONFIG
288 #define ENV_VAR_CONFIG NULL
290 char *cf_env_file = ENV_VAR_CONFIG;
292 static uns postpone_commit; // only for cf_getopt()
293 static uns everything_committed; // after the 1st load, this flag is set on
298 if (cf_check_stack())
300 if (cf_commit_all(postpone_commit ? CF_NO_COMMIT : everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
302 if (!postpone_commit)
303 everything_committed = 1;
308 load_file(const char *file)
311 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
313 msg(L_ERROR, "Cannot open %s: %m", file);
316 char *err_msg = parse_fastbuf(file, fb, 0);
318 int err = !!err_msg || done_stack();
325 load_string(const char *string)
329 fbbuf_init_read(&fb, (byte *)string, strlen(string), 0);
330 char *msg = parse_fastbuf(NULL, &fb, 0);
331 return !!msg || done_stack();
334 /* Safe loading and reloading */
337 cf_reload(const char *file)
340 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
341 uns ec = everything_committed;
342 everything_committed = 0;
343 int err = load_file(file);
347 cf_journal_commit_transaction(1, NULL);
351 everything_committed = ec;
352 cf_journal_rollback_transaction(1, oldj);
359 cf_load(const char *file)
361 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
362 int err = load_file(file);
364 cf_journal_commit_transaction(1, oldj);
366 cf_journal_rollback_transaction(1, oldj);
371 cf_set(const char *string)
373 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
374 int err = load_string(string);
376 cf_journal_commit_transaction(0, oldj);
378 cf_journal_rollback_transaction(0, oldj);
382 /* Command-line parser */
390 if (cf_env_file && (env = getenv(cf_env_file)))
393 die("Cannot load config file %s", env);
395 else if (cf_load(cf_def_file))
396 die("Cannot load default config %s", cf_def_file);
400 // We need to create an empty pool
401 cf_journal_commit_transaction(1, cf_journal_new_transaction(1));
408 if (postpone_commit) {
411 die("Cannot commit after the initialization");
416 cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
418 static int other_options = 0;
420 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
421 if (res == 'S' || res == 'C' || res == 0x64436667)
424 die("The -S and -C options must precede all other arguments");
429 die("Cannot set %s", optarg);
430 } else if (res == 'C') {
433 die("Cannot load config file %s", optarg);
436 else { /* --dumpconfig */
439 struct fastbuf *b = bfdopen(1, 4096);
446 /* unhandled option or end of options */
447 if (res != ':' && res != '?')