Browse Source

implement config unload

Felix Fietkau 16 years ago
parent
commit
f7df28b4d0
5 changed files with 46 additions and 6 deletions
  1. 7 0
      cli.c
  2. 1 1
      libuci.c
  3. 23 3
      list.c
  4. 7 2
      parse.c
  5. 8 0
      uci.h

+ 7 - 0
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;

+ 1 - 1
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;

+ 23 - 3
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;

+ 7 - 2
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);

+ 8 - 0
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
  *