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 87de48cda96654ab63a7ab2e9a6479a9fc406c80..828c7b931593dba7d92ea982db6eec4d356062c6 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)
 {
+       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;
 }
 
@@ -263,7 +292,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       while((c = getopt(argc, argv, "sS")) != -1) {
+       while((c = getopt(argc, argv, "mfsS")) != -1) {
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");
diff --git a/file.c b/file.c
index eb05d25859f773c32ee7ce6b4f4a0f5aea2d0dc7..9f47ef219c876367b796034af412136cdffe4061 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);
 
-       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);
 }
 
@@ -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);
 
-       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);
 }
 
@@ -586,6 +596,8 @@ error:
        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);
diff --git a/list.c b/list.c
index 4ff4f7b342bf340c9c5eb82b8d1a3f2d2a6ee779..8f30db7563bc328989df9de148feb925d8f3ff14 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);
+       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);
-       } 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);
-       else
-               uci_alloc_section(p, value, section);
+       else {
+               s = uci_alloc_section(p, value, section);
+               if (ctx->pctx)
+                       ctx->pctx->section = s;
+       }
 
        return 0;
 }