X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fconf-input.c;h=f8d2eb840ce21eb693df10e68fdb0eb9151e4332;hb=1481eca416a467e9952dbc5e4852afe66eaf1256;hp=3dc5db1d385ef96baae70de29102988d9f8c0649;hpb=b3d3d2847cb34363c5e7a5a6bf8155b710d7afcd;p=libucw.git diff --git a/ucw/conf-input.c b/ucw/conf-input.c index 3dc5db1d..f8d2eb84 100644 --- a/ucw/conf-input.c +++ b/ucw/conf-input.c @@ -20,6 +20,7 @@ #include #include +#include #include /* Text file parser */ @@ -28,20 +29,20 @@ #include -#define GBUF_TYPE uns +#define GBUF_TYPE uint #define GBUF_PREFIX(x) split_##x #include struct cf_parser_state { const char *name_parse_fb; struct fastbuf *parse_fb; - uns line_num; + uint line_num; char *line; split_t word_buf; - uns words; - uns ends_by_brace; // the line is ended by "{" + uint words; + uint ends_by_brace; // the line is ended by "{" bb_t copy_buf; - uns copied; + uint copied; char line_buf[]; }; @@ -63,7 +64,7 @@ get_line(struct cf_parser_state *p, char **msg) static void append(struct cf_parser_state *p, char *start, char *end) { - uns len = end - start; + uint len = end - start; bb_grow(&p->copy_buf, p->copied + len + 1); memcpy(p->copy_buf.ptr + p->copied, start, len); p->copied += len + 1; @@ -71,7 +72,7 @@ append(struct cf_parser_state *p, char *start, char *end) } static char * -get_word(struct cf_parser_state *p, uns is_command_name) +get_word(struct cf_parser_state *p, uint is_command_name) { char *msg; char *line = p->line; @@ -94,10 +95,10 @@ get_word(struct cf_parser_state *p, uns is_command_name) } else if (*line == '"') { line++; - uns start_copy = p->copied; + uint start_copy = p->copied; while (1) { char *start = line; - uns escape = 0; + uint escape = 0; while (*line) { if (*line == '"' && !escape) break; @@ -121,7 +122,7 @@ get_word(struct cf_parser_state *p, uns is_command_name) line++; char *tmp = stk_str_unesc(p->copy_buf.ptr + start_copy); - uns l = strlen(tmp); + uint l = strlen(tmp); bb_grow(&p->copy_buf, start_copy + l + 1); strcpy(p->copy_buf.ptr + start_copy, tmp); p->copied = start_copy + l + 1; @@ -149,7 +150,7 @@ get_word(struct cf_parser_state *p, uns is_command_name) } static char * -get_token(struct cf_parser_state *p, uns is_command_name, char **err) +get_token(struct cf_parser_state *p, uint is_command_name, char **err) { *err = NULL; while (1) { @@ -170,7 +171,7 @@ get_token(struct cf_parser_state *p, uns is_command_name, char **err) msg(L_WARN, "The line %s:%d following a backslash is empty", p->name_parse_fb ? : "", p->line_num); } else { split_grow(&p->word_buf, p->words+1); - uns start = p->copied; + uint start = p->copied; p->word_buf.ptr[p->words++] = p->copied; *err = get_word(p, is_command_name); return *err ? NULL : p->copy_buf.ptr + start; @@ -202,8 +203,18 @@ split_command(struct cf_parser_state *p) /* Parsing multiple files */ +static int +maybe_commit(struct cf_context *cc) +{ + if (cf_commit_all(cc->postpone_commit ? CF_NO_COMMIT : cc->everything_committed ? CF_COMMIT : CF_COMMIT_ALL)) + return 1; + if (!cc->postpone_commit) + cc->everything_committed = 1; + return 0; +} + static char * -parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, uns depth) +parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, uint depth) { struct cf_parser_state *p = cc->parser; if (!p) @@ -214,19 +225,23 @@ parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, un p->line = p->line_buf; *p->line = 0; - char *err; + if (!depth) + cf_init_stack(cc); + + char *err = NULL; while (1) { err = split_command(p); if (err) goto error; if (!p->words) - return NULL; + break; char *name = p->copy_buf.ptr + p->word_buf.ptr[0]; char *pars[p->words-1]; - for (uns i=1; iwords; i++) + for (uint i=1; iwords; i++) pars[i-1] = p->copy_buf.ptr + p->word_buf.ptr[i]; - if (!strcasecmp(name, "include")) + int optional_include = !strcasecmp(name, "optionalinclude"); + if (optional_include || !strcasecmp(name, "include")) { if (p->words != 2) err = "Expecting one filename"; @@ -238,10 +253,12 @@ parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, un goto error; struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14); if (!new_fb) { + if (optional_include && errno == ENOENT) + continue; err = cf_printf("Cannot open file %s: %m", pars[0]); goto error; } - uns ll = p->line_num; + uint ll = p->line_num; err = parse_fastbuf(cc, stk_strdup(pars[0]), new_fb, depth+1); p->line_num = ll; bclose(new_fb); @@ -281,6 +298,17 @@ parse_fastbuf(struct cf_context *cc, const char *name_fb, struct fastbuf *fb, un if (err) goto error; } + + if (!depth) + { + if (cf_done_stack(cc)) + err = "Unterminated block"; + else if (maybe_commit(cc)) + err = "Commit failed"; + } + if (!err) + return NULL; + error: if (name_fb) msg(L_ERROR, "File %s, line %d: %s", name_fb, p->line_num, err); @@ -294,25 +322,23 @@ error: static int load_file(struct cf_context *cc, const char *file) { - cf_init_stack(cc); struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14); if (!fb) { - msg(L_ERROR, "Cannot open %s: %m", file); + msg(L_ERROR, "Cannot open configuration file %s: %m", file); return 1; } char *err_msg = parse_fastbuf(cc, file, fb, 0); bclose(fb); - return !!err_msg || cf_done_stack(cc); + return !!err_msg; } static int load_string(struct cf_context *cc, const char *string) { - cf_init_stack(cc); struct fastbuf fb; fbbuf_init_read(&fb, (byte *)string, strlen(string), 0); char *msg = parse_fastbuf(cc, NULL, &fb, 0); - return !!msg || cf_done_stack(cc); + return !!msg; } /* Safe loading and reloading */ @@ -327,9 +353,9 @@ struct conf_entry { /* We remember a list of actions to apply upon reload */ }; static void -cf_remember_entry(struct cf_context *cc, uns type, const char *arg) +cf_remember_entry(struct cf_context *cc, uint type, const char *arg) { - if (!cc->need_journal) + if (!cc->enable_journal) return; struct conf_entry *ce = cf_malloc(sizeof(*ce)); ce->type = type; @@ -341,9 +367,10 @@ int cf_reload(const char *file) { struct cf_context *cc = cf_get_context(); + ASSERT(cc->enable_journal); cf_journal_swap(); struct cf_journal_item *oldj = cf_journal_new_transaction(1); - uns ec = cc->everything_committed; + uint ec = cc->everything_committed; cc->everything_committed = 0; clist old_entries; @@ -406,3 +433,28 @@ cf_set(const char *string) cf_journal_rollback_transaction(0, oldj); return err; } + +void +cf_revert(void) +{ + cf_journal_swap(); + cf_journal_delete(); +} + +void +cf_open_group(void) +{ + struct cf_context *cc = cf_get_context(); + cc->postpone_commit++; +} + +int +cf_close_group(void) +{ + struct cf_context *cc = cf_get_context(); + ASSERT(cc->postpone_commit); + if (!--cc->postpone_commit) + return maybe_commit(cc); + else + return 0; +}