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 byte *name_parse_fb;
27 static struct fastbuf *parse_fb;
31 static byte line_buf[MAX_LINE];
32 static byte *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(byte *start, byte *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 ? : (byte*) "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 ? : (byte*) "Unterminated quoted word at the end";
116 byte *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, byte **msg)
148 if (!*line || *line == '#') {
149 if (!is_command_name || !get_line(msg))
151 } else if (*line == ';') {
153 if (!is_command_name || *msg)
155 } else if (*line == '\\' && !line[1]) {
156 if (!get_line(msg)) {
158 *msg = "Last line ends by a backslash";
161 if (!*line || *line == '#')
162 log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb ? : (byte*) "", line_num);
164 split_grow(&word_buf, words+1);
166 word_buf.ptr[words++] = copied;
167 *msg = get_word(is_command_name);
168 return *msg ? NULL : copy_buf.ptr + start;
176 words = copied = ends_by_brace = 0;
177 byte *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(byte *name_fb, struct fastbuf *fb, uns depth)
201 name_parse_fb = name_fb;
208 msg = split_command();
213 byte *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 msg = "Expecting one filename";
222 msg = "Too many nested files";
223 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
224 msg = "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 msg = cf_printf("Cannot open file %s: %m", pars[0]);
233 msg = parse_fastbuf(stk_strdup(pars[0]), new_fb, depth+1);
241 enum cf_operation op;
242 byte *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 msg = cf_printf("Unknown operation %s", c);
268 msg = cf_interpret_line(name, op, words-1, pars);
274 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
275 else if (line_num == 1)
276 log(L_ERROR, "Manual setting of configuration: %s", msg);
278 log(L_ERROR, "Manual setting of configuration, line %d: %s", line_num, msg);
279 return "included from here";
282 #ifndef DEFAULT_CONFIG
283 #define DEFAULT_CONFIG NULL
285 byte *cf_def_file = DEFAULT_CONFIG;
287 static uns postpone_commit; // only for cf_getopt()
288 static uns everything_committed; // after the 1st load, this flag is set on
293 if (cf_check_stack())
295 if (cf_commit_all(postpone_commit ? CF_NO_COMMIT : everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
297 if (!postpone_commit)
298 everything_committed = 1;
303 load_file(byte *file)
306 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
308 log(L_ERROR, "Cannot open %s: %m", file);
311 byte *msg = parse_fastbuf(file, fb, 0);
313 int err = !!msg || done_stack();
320 load_string(byte *string)
324 fbbuf_init_read(&fb, string, strlen(string), 0);
325 byte *msg = parse_fastbuf(NULL, &fb, 0);
326 return !!msg || done_stack();
329 /* Safe loading and reloading */
332 cf_reload(byte *file)
335 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
336 uns ec = everything_committed;
337 everything_committed = 0;
338 int err = load_file(file);
342 cf_journal_commit_transaction(1, NULL);
346 everything_committed = ec;
347 cf_journal_rollback_transaction(1, oldj);
356 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
357 int err = load_file(file);
359 cf_journal_commit_transaction(1, oldj);
361 cf_journal_rollback_transaction(1, oldj);
368 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
369 int err = load_string(string);
371 cf_journal_commit_transaction(0, oldj);
373 cf_journal_rollback_transaction(0, oldj);
377 /* Command-line parser */
383 if (cf_load(cf_def_file))
384 die("Cannot load default config %s", cf_def_file);
390 if (postpone_commit) {
393 die("Cannot commit after the initialization");
398 cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
400 static int other_options = 0;
402 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
403 if (res == 'S' || res == 'C' || res == 0x64436667)
406 die("The -S and -C options must precede all other arguments");
411 die("Cannot set %s", optarg);
412 } else if (res == 'C') {
415 die("Cannot load config file %s", optarg);
418 else { /* --dumpconfig */
421 struct fastbuf *b = bfdopen(1, 4096);
428 /* unhandled option or end of options */
429 if (res != ':' && res != '?')