From: Felix Fietkau Date: Sun, 20 Jan 2008 22:03:00 +0000 (+0100) Subject: implement config unload X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=f7df28b4d0155c780fa46a09d2dbcf7825faebae;p=uci.git implement config unload --- diff --git a/cli.c b/cli.c index d83e257..7dece62 100644 --- a/cli.c +++ b/cli.c @@ -28,6 +28,12 @@ static void uci_usage(int argc, char **argv) exit(255); } +static void uci_show_file(const char *name) +{ + uci_load(ctx, name); + uci_unload(ctx, name); +} + static int uci_show(int argc, char **argv) { char **configs = uci_list_configs(ctx); @@ -38,6 +44,7 @@ static int uci_show(int argc, char **argv) for (p = configs; *p; p++) { fprintf(stderr, "# config: %s\n", *p); + uci_show_file(*p); } return 0; diff --git a/libuci.c b/libuci.c index 1902673..ada75c0 100644 --- a/libuci.c +++ b/libuci.c @@ -100,7 +100,7 @@ void uci_free(struct uci_context *ctx) uci_cleanup(ctx); uci_foreach_entry(config, &ctx->root, cfg) { - uci_drop_file(cfg); + uci_drop_config(cfg); } free(ctx); return; diff --git a/list.c b/list.c index f4ab8ec..8d73f11 100644 --- a/list.c +++ b/list.c @@ -121,7 +121,7 @@ error: return NULL; } -static void uci_drop_file(struct uci_config *cfg) +static void uci_drop_config(struct uci_config *cfg) { struct uci_section *s; @@ -139,7 +139,7 @@ static void uci_drop_file(struct uci_config *cfg) } -static struct uci_config *uci_alloc_file(struct uci_context *ctx, const char *name) +static struct uci_config *uci_alloc_config(struct uci_context *ctx, const char *name) { struct uci_config *cfg = NULL; @@ -153,11 +153,31 @@ static struct uci_config *uci_alloc_file(struct uci_context *ctx, const char *na return cfg; error: - uci_drop_file(cfg); + uci_drop_config(cfg); UCI_THROW(ctx, ctx->errno); return NULL; } +int uci_unload(struct uci_context *ctx, const char *name) +{ + struct uci_config *cfg; + + UCI_HANDLE_ERR(ctx); + UCI_ASSERT(ctx, name != NULL); + + uci_foreach_entry(config, &ctx->root, cfg) { + if (!strcmp(cfg->name, name)) + goto found; + } + UCI_THROW(ctx, UCI_ERR_NOTFOUND); + +found: + uci_list_del(&cfg->list); + uci_drop_config(cfg); + + return 0; +} + char **uci_list_configs(struct uci_context *ctx) { char **configs; diff --git a/parse.c b/parse.c index 5158028..4394bf6 100644 --- a/parse.c +++ b/parse.c @@ -80,7 +80,7 @@ static void uci_parse_cleanup(struct uci_context *ctx) ctx->pctx = NULL; if (pctx->cfg) { uci_list_del(&pctx->cfg->list); - uci_drop_file(pctx->cfg); + uci_drop_config(pctx->cfg); } if (pctx->buf) free(pctx->buf); @@ -314,6 +314,11 @@ int uci_load(struct uci_context *ctx, const char *name) UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, name != NULL); + UCI_TRAP_SAVE(ctx, ignore); + uci_unload(ctx, name); + UCI_TRAP_RESTORE(ctx); + +ignore: /* make sure no memory from previous parse attempts is leaked */ uci_parse_cleanup(ctx); @@ -345,7 +350,7 @@ int uci_load(struct uci_context *ctx, const char *name) if (!pctx->file) UCI_THROW(ctx, UCI_ERR_IO); - pctx->cfg = uci_alloc_file(ctx, name); + pctx->cfg = uci_alloc_config(ctx, name); while (!feof(pctx->file)) { uci_getln(ctx); diff --git a/uci.h b/uci.h index 26cc3b7..8784c43 100644 --- a/uci.h +++ b/uci.h @@ -69,6 +69,14 @@ extern void uci_perror(struct uci_context *ctx, const char *str); */ extern int uci_load(struct uci_context *ctx, const char *name); +/** + * uci_unload: Unload a config file from the uci context + * + * @ctx: uci context + * @name: name of the config file + */ +extern int uci_unload(struct uci_context *ctx, const char *name); + /** * uci_cleanup: Clean up after an error *