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 if (!bgets(parse_fb, line_buf, MAX_LINE))
58 append(byte *start, byte *end)
60 uns len = end - start;
61 bb_grow(©_buf, copied + len + 1);
62 memcpy(copy_buf.ptr + copied, start, len);
64 copy_buf.ptr[copied-1] = 0;
68 get_word(uns is_command_name)
74 while (*line && *line != '\'')
79 copy_buf.ptr[copied-1] = '\n';
81 return "Unterminated apostrophe word at the end";
85 } else if (*line == '"') {
87 uns start_copy = copied;
92 if (*line == '"' && !escape)
94 else if (*line == '\\')
104 copy_buf.ptr[copied-1] = '\n';
105 else // merge two lines
108 return "Unterminated quoted word at the end";
112 byte *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
114 bb_grow(©_buf, start_copy + l + 1);
115 strcpy(copy_buf.ptr + start_copy, tmp);
116 copied = start_copy + l + 1;
119 // promised that *line is non-null and non-blank
121 while (*line && !Cblank(*line)
122 && *line != '{' && *line != '}' && *line != ';'
123 && (*line != '=' || !is_command_name))
125 if (*line == '=') { // nice for setting from a command-line
127 return "Assignment without a variable";
130 if (line == start) // already the first char is control
134 while (Cblank(*line))
140 get_token(uns is_command_name, byte **msg)
144 if (!*line || *line == '#') {
145 if (!is_command_name || !get_line())
147 } else if (*line == ';') {
149 if (!is_command_name || *msg)
151 } else if (*line == '\\' && !line[1]) {
153 *msg = "Last line ends by a backslash";
156 if (!*line || *line == '#')
157 log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
159 split_grow(&word_buf, words+1);
161 word_buf.ptr[words++] = copied;
162 *msg = get_word(is_command_name);
163 return *msg ? NULL : copy_buf.ptr + start;
171 words = copied = ends_by_brace = 0;
172 byte *msg, *start_word;
173 if (!(start_word = get_token(1, &msg)))
175 if (*start_word == '{') // only one opening brace
176 return "Unexpected opening brace";
177 while (*line != '}') // stays for the next time
179 if (!(start_word = get_token(0, &msg)))
181 if (*start_word == '{') {
182 words--; // discard the brace
190 /* Parsing multiple files */
193 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
196 name_parse_fb = name_fb;
203 msg = split_command();
208 byte *name = copy_buf.ptr + word_buf.ptr[0];
210 for (uns i=1; i<words; i++)
211 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
212 if (!strcasecmp(name, "include"))
215 msg = "Expecting one filename";
217 msg = "Too many nested files";
218 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
219 msg = "The input command must be the last one on a line";
222 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
224 msg = cf_printf("Cannot open file %s: %m", pars[0]);
228 msg = parse_fastbuf(stk_strdup(pars[0]), new_fb, depth+1);
236 enum cf_operation op;
237 byte *c = strchr(name, ':');
239 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
242 switch (Clocase(*c)) {
243 case 's': op = OP_SET; break;
244 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
245 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
246 case 'p': op = OP_PREPEND; break;
247 case 'r': op = OP_REMOVE; break;
248 case 'e': op = OP_EDIT; break;
249 case 'b': op = OP_BEFORE; break;
250 default: op = OP_SET; break;
252 if (strcasecmp(c, cf_op_names[op])) {
253 msg = cf_printf("Unknown operation %s", c);
259 msg = cf_interpret_line(name, op, words-1, pars);
264 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
265 return "included from here";
268 #ifndef DEFAULT_CONFIG
269 #define DEFAULT_CONFIG NULL
271 byte *cf_def_file = DEFAULT_CONFIG;
273 static uns postpone_commit; // only for cf_getopt()
274 static uns everything_committed; // after the 1st load, this flag is set on
279 if (cf_check_stack())
281 if (cf_commit_all(postpone_commit ? CF_NO_COMMIT : everything_committed ? CF_COMMIT : CF_COMMIT_ALL))
283 if (!postpone_commit)
284 everything_committed = 1;
289 load_file(byte *file)
292 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
294 log(L_ERROR, "Cannot open %s: %m", file);
297 byte *msg = parse_fastbuf(file, fb, 0);
299 int err = !!msg || done_stack();
306 load_string(byte *string)
310 fbbuf_init_read(&fb, string, strlen(string), 0);
311 byte *msg = parse_fastbuf("memory string", &fb, 0);
312 return !!msg || done_stack();
315 /* Safe loading and reloading */
318 cf_reload(byte *file)
321 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
322 uns ec = everything_committed;
323 everything_committed = 0;
324 int err = load_file(file);
328 cf_journal_commit_transaction(1, NULL);
332 everything_committed = ec;
333 cf_journal_rollback_transaction(1, oldj);
342 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
343 int err = load_file(file);
345 cf_journal_commit_transaction(1, oldj);
347 cf_journal_rollback_transaction(1, oldj);
354 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
355 int err = load_string(string);
357 cf_journal_commit_transaction(0, oldj);
359 cf_journal_rollback_transaction(0, oldj);
363 /* Command-line parser */
369 if (cf_load(cf_def_file))
370 die("Cannot load default config %s", cf_def_file);
376 if (postpone_commit) {
379 die("Cannot commit after the initialization");
384 cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
386 static int other_options = 0;
388 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
389 if (res == 'S' || res == 'C' || res == 0x64436667)
392 die("The -S and -C options must precede all other arguments");
397 die("Cannot set %s", optarg);
398 } else if (res == 'C') {
401 die("Cannot load config file %s", optarg);
404 else { /* --dumpconfig */
407 struct fastbuf *b = bfdopen(1, 4096);
414 /* unhandled option or end of options */
415 if (res != ':' && res != '?')