allow the user of the library to override the confdir/searchdir
[project/uci.git] / libuci.c
index 0dd81fe..b86c976 100644 (file)
--- a/libuci.c
+++ b/libuci.c
@@ -16,6 +16,7 @@
  * This file contains some common code for the uci library
  */
 
+#define _GNU_SOURCE
 #include <sys/types.h>
 #include <stdbool.h>
 #include <string.h>
@@ -24,6 +25,9 @@
 #include "uci.h"
 #include "err.h"
 
+static const char *uci_confdir = UCI_CONFDIR;
+static const char *uci_savedir = UCI_SAVEDIR;
+
 static const char *uci_errstr[] = {
        [UCI_OK] =            "Success",
        [UCI_ERR_MEM] =       "Out of memory",
@@ -49,6 +53,9 @@ struct uci_context *uci_alloc_context(void)
        uci_list_init(&ctx->root);
        ctx->flags = UCI_FLAG_STRICT;
 
+       ctx->confdir = (char *) uci_confdir;
+       ctx->savedir = (char *) uci_savedir;
+
        return ctx;
 }
 
@@ -56,10 +63,16 @@ void uci_free_context(struct uci_context *ctx)
 {
        struct uci_element *e, *tmp;
 
+       if (ctx->confdir != uci_confdir)
+               free(ctx->confdir);
+       if (ctx->savedir != uci_savedir)
+               free(ctx->savedir);
+
        UCI_TRAP_SAVE(ctx, ignore);
        uci_cleanup(ctx);
        uci_foreach_element_safe(&ctx->root, tmp, e) {
-               uci_free_package(uci_to_package(e));
+               struct uci_package *p = uci_to_package(e);
+               uci_free_package(&p);
        }
        free(ctx);
        UCI_TRAP_RESTORE(ctx);
@@ -68,6 +81,24 @@ ignore:
        return;
 }
 
+int uci_set_confdir(struct uci_context *ctx, char *dir)
+{
+       dir = uci_strdup(ctx, dir);
+       if (ctx->confdir != uci_confdir)
+               free(ctx->confdir);
+       ctx->confdir = dir;
+       return 0;
+}
+
+int uci_set_savedir(struct uci_context *ctx, char *dir)
+{
+       dir = uci_strdup(ctx, dir);
+       if (ctx->savedir != uci_savedir)
+               free(ctx->savedir);
+       ctx->savedir = dir;
+       return 0;
+}
+
 int uci_cleanup(struct uci_context *ctx)
 {
        UCI_HANDLE_ERR(ctx);