X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=file.c;h=963bc419a46969beff07621206d7d41051084364;hb=HEAD;hp=33ba93c84e5d402d407f6dde3a19ca76462da6fc;hpb=6ce771b1095c220f849e70cdcbf23bd09f7fe7d5;p=uci.git diff --git a/file.c b/file.c index 33ba93c..963bc41 100644 --- a/file.c +++ b/file.c @@ -200,7 +200,7 @@ static void parse_str(struct uci_context *ctx, char **str, char **target) } while (**str && !isspace(**str)); done: - /* + /* * if the string was unquoted and we've stopped at a whitespace * character, skip to the next one, because the whitespace will * be overwritten by a null byte here @@ -266,7 +266,7 @@ int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char * } static int -uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e, bool complete) +uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e) { UCI_ASSERT(ctx, ptr != NULL); UCI_ASSERT(ctx, e != NULL); @@ -296,8 +296,6 @@ fill_package: ptr->package = ptr->p->e.name; ptr->flags |= UCI_LOOKUP_DONE; - if (complete) - ptr->flags |= UCI_LOOKUP_COMPLETE; return 0; } @@ -318,7 +316,7 @@ static void assert_eol(struct uci_context *ctx, char **str) uci_parse_error(ctx, *str, "too many arguments"); } -/* +/* * switch to a different config, either triggered by uci_load, or by a * 'package <...>' statement in the import file */ @@ -343,7 +341,7 @@ static void uci_switch_config(struct uci_context *ctx) if (!name) return; - /* + /* * if an older config under the same name exists, unload it * ignore errors here, e.g. if the config was not found */ @@ -404,7 +402,7 @@ static void uci_parse_config(struct uci_context *ctx, char **str) ctx->internal = !pctx->merge; UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section); } else { - uci_fill_ptr(ctx, &ptr, &pctx->package->e, false); + uci_fill_ptr(ctx, &ptr, &pctx->package->e); e = uci_lookup_list(&pctx->package->sections, name); if (e) ptr.s = uci_to_section(e); @@ -438,7 +436,7 @@ static void uci_parse_option(struct uci_context *ctx, char **str, bool list) value = next_arg(ctx, str, false, false); assert_eol(ctx, str); - uci_fill_ptr(ctx, &ptr, &pctx->section->e, false); + uci_fill_ptr(ctx, &ptr, &pctx->section->e); e = uci_lookup_list(&pctx->section->options, name); if (e) ptr.o = uci_to_option(e); @@ -612,51 +610,53 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u struct uci_parse_context *pctx; UCI_HANDLE_ERR(ctx); + pctx = NULL; /* make sure no memory from previous parse attempts is leaked */ uci_cleanup(ctx); uci_alloc_parse_context(ctx); pctx = ctx->pctx; - pctx->file = stream; - if (*package && single) { - pctx->package = *package; - pctx->merge = true; - } - - /* - * If 'name' was supplied, assume that the supplied stream does not contain - * the appropriate 'package ' string to specify the config name - * NB: the config file can still override the package name - */ - if (name) { - UCI_ASSERT(ctx, uci_validate_package(name)); - pctx->name = name; - } + if (pctx != NULL) { + pctx->file = stream; + if (*package && single) { + pctx->package = *package; + pctx->merge = true; + } + /* + * If 'name' was supplied, assume that the supplied stream does not contain + * the appropriate 'package ' string to specify the config name + * NB: the config file can still override the package name + */ + if (name) { + UCI_ASSERT(ctx, uci_validate_package(name)); + pctx->name = name; + } - while (!feof(pctx->file)) { - uci_getln(ctx, 0); - UCI_TRAP_SAVE(ctx, error); - if (pctx->buf[0]) - uci_parse_line(ctx, single); - UCI_TRAP_RESTORE(ctx); - continue; + while (!feof(pctx->file)) { + uci_getln(ctx, 0); + UCI_TRAP_SAVE(ctx, error); + if (pctx->buf[0]) + uci_parse_line(ctx, single); + UCI_TRAP_RESTORE(ctx); + continue; error: - if (ctx->flags & UCI_FLAG_PERROR) - uci_perror(ctx, NULL); - if ((ctx->err != UCI_ERR_PARSE) || - (ctx->flags & UCI_FLAG_STRICT)) - UCI_THROW(ctx, ctx->err); - } + if (ctx->flags & UCI_FLAG_PERROR) + uci_perror(ctx, NULL); + if ((ctx->err != UCI_ERR_PARSE) || + (ctx->flags & UCI_FLAG_STRICT)) + UCI_THROW(ctx, ctx->err); + } - uci_fixup_section(ctx, ctx->pctx->section); - if (!pctx->package && name) - uci_switch_config(ctx); - if (package) - *package = pctx->package; - if (pctx->merge) - pctx->package = NULL; + uci_fixup_section(ctx, ctx->pctx->section); + if (!pctx->package && name) + uci_switch_config(ctx); + if (package) + *package = pctx->package; + if (pctx->merge) + pctx->package = NULL; - pctx->name = NULL; + pctx->name = NULL; + } uci_switch_config(ctx); /* no error happened, we can get rid of the parser context now */ @@ -694,34 +694,34 @@ void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool /* open the config file for writing now, so that it is locked */ f = uci_open_stream(ctx, p->path, SEEK_SET, true, true); - /* flush unsaved changes and reload from history file */ + /* flush unsaved changes and reload from delta file */ UCI_TRAP_SAVE(ctx, done); - if (p->has_history) { + if (p->has_delta) { if (!overwrite) { name = uci_strdup(ctx, p->e.name); path = uci_strdup(ctx, p->path); - /* dump our own changes to the history file */ - if (!uci_list_empty(&p->history)) + /* dump our own changes to the delta file */ + if (!uci_list_empty(&p->delta)) UCI_INTERNAL(uci_save, ctx, p); - /* - * other processes might have modified the config - * as well. dump and reload + /* + * other processes might have modified the config + * as well. dump and reload */ uci_free_package(&p); uci_cleanup(ctx); UCI_INTERNAL(uci_import, ctx, f, name, &p, true); p->path = path; - p->has_history = true; + p->has_delta = true; *package = p; /* freed together with the uci_package */ path = NULL; } - /* flush history */ - if (!uci_load_history(ctx, p, true)) + /* flush delta */ + if (!uci_load_delta(ctx, p, true)) goto done; } @@ -743,7 +743,7 @@ done: } -/* +/* * This function returns the filename by returning the string * after the last '/' character. By checking for a non-'\0' * character afterwards, directories are ignored (glob marks @@ -810,7 +810,7 @@ static char **uci_list_config_files(struct uci_context *ctx) static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name) { struct uci_package *package = NULL; - char *filename; + char *filename = NULL; bool confdir; FILE *file = NULL; @@ -841,14 +841,16 @@ static struct uci_package *uci_file_load(struct uci_context *ctx, const char *na if (package) { package->path = filename; - package->has_history = confdir; - uci_load_history(ctx, package, false); + package->has_delta = confdir; + uci_load_delta(ctx, package, false); } done: uci_close_stream(file); - if (ctx->err) + if (ctx->err) { + free(filename); UCI_THROW(ctx, ctx->err); + } return package; }