add support for merged importing
authorFelix Fietkau <nbd@openwrt.org>
Sat, 2 Feb 2008 22:46:58 +0000 (23:46 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sat, 2 Feb 2008 22:46:58 +0000 (23:46 +0100)
cli.c
file.c
list.c

diff --git a/cli.c b/cli.c
index 87de48c..828c7b9 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -108,6 +108,35 @@ static int package_cmd(int cmd, char *package)
 
 static int uci_do_import(int argc, char **argv)
 {
 
 static int uci_do_import(int argc, char **argv)
 {
+       struct uci_package *package = NULL;
+       char **configs = NULL;
+       char *name = NULL;
+       int ret = UCI_OK;
+       char **p;
+
+       if (argc > 2)
+               return 255;
+
+       if (argc == 2)
+               name = argv[1];
+       else if (flags & CLI_FLAG_MERGE)
+               /* need a package to merge */
+               return 255;
+
+       if (flags & CLI_FLAG_MERGE) {
+               if (uci_load(ctx, name, &package) != UCI_OK)
+                       package = NULL;
+       }
+       ret = uci_import(ctx, input, name, &package, (name != NULL));
+       if ((ret == UCI_OK) && (flags & CLI_FLAG_MERGE)) {
+               ret = uci_save(ctx, package);
+       }
+
+       if (ret != UCI_OK) {
+               uci_perror(ctx, appname);
+               return 1;
+       }
+
        return 0;
 }
 
        return 0;
 }
 
@@ -263,7 +292,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
                return 1;
        }
 
-       while((c = getopt(argc, argv, "sS")) != -1) {
+       while((c = getopt(argc, argv, "mfsS")) != -1) {
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");
diff --git a/file.c b/file.c
index eb05d25..9f47ef2 100644 (file)
--- a/file.c
+++ b/file.c
@@ -392,9 +392,14 @@ static void uci_parse_config(struct uci_context *ctx, char **str)
        name = next_arg(ctx, str, false, true);
        assert_eol(ctx, str);
 
        name = next_arg(ctx, str, false, true);
        assert_eol(ctx, str);
 
-       if (pctx->merge)
-               UCI_INTERNAL(uci_set, ctx, pctx->package, name, NULL, type);
-       else
+       if (pctx->merge) {
+               UCI_TRAP_SAVE(ctx, error);
+               uci_set(ctx, pctx->package, name, NULL, type);
+               UCI_TRAP_RESTORE(ctx);
+               return;
+error:
+               UCI_THROW(ctx, ctx->errno);
+       } else
                pctx->section = uci_alloc_section(pctx->package, type, name);
 }
 
                pctx->section = uci_alloc_section(pctx->package, type, name);
 }
 
@@ -417,9 +422,14 @@ static void uci_parse_option(struct uci_context *ctx, char **str)
        value = next_arg(ctx, str, true, false);
        assert_eol(ctx, str);
 
        value = next_arg(ctx, str, true, false);
        assert_eol(ctx, str);
 
-       if (pctx->merge)
-               UCI_INTERNAL(uci_set, ctx, pctx->package, pctx->section->e.name, name, value);
-       else
+       if (pctx->merge) {
+               UCI_TRAP_SAVE(ctx, error);
+               uci_set(ctx, pctx->package, pctx->section->e.name, name, value);
+               UCI_TRAP_RESTORE(ctx);
+               return;
+error:
+               UCI_THROW(ctx, ctx->errno);
+       } else
                uci_alloc_option(pctx->section, name, value);
 }
 
                uci_alloc_option(pctx->section, name, value);
 }
 
@@ -586,6 +596,8 @@ error:
        uci_fixup_section(ctx, ctx->pctx->section);
        if (package)
                *package = pctx->package;
        uci_fixup_section(ctx, ctx->pctx->section);
        if (package)
                *package = pctx->package;
+       if (pctx->merge)
+               pctx->package = NULL;
 
        pctx->name = NULL;
        uci_switch_config(ctx);
 
        pctx->name = NULL;
        uci_switch_config(ctx);
diff --git a/list.c b/list.c
index 4ff4f7b..8f30db7 100644 (file)
--- a/list.c
+++ b/list.c
@@ -450,13 +450,14 @@ int uci_set(struct uci_context *ctx, struct uci_package *p, char *section, char
                goto notfound;
 
        s = uci_to_section(e);
                goto notfound;
 
        s = uci_to_section(e);
+       if (ctx->pctx)
+               ctx->pctx->section = s;
+
        if (option) {
                e = uci_lookup_list(ctx, &s->options, option);
                if (!e)
                        goto notfound;
                o = uci_to_option(e);
        if (option) {
                e = uci_lookup_list(ctx, &s->options, option);
                if (!e)
                        goto notfound;
                o = uci_to_option(e);
-       } else if (internal && ctx->pctx) {
-               ctx->pctx->section = s;
        }
 
        /* 
        }
 
        /* 
@@ -489,8 +490,11 @@ notfound:
                uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
        if (s)
                uci_alloc_option(s, option, value);
                uci_add_history(ctx, p, UCI_CMD_ADD, section, option, value);
        if (s)
                uci_alloc_option(s, option, value);
-       else
-               uci_alloc_section(p, value, section);
+       else {
+               s = uci_alloc_section(p, value, section);
+               if (ctx->pctx)
+                       ctx->pctx->section = s;
+       }
 
        return 0;
 }
 
        return 0;
 }