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 1;
}
- while((c = getopt(argc, argv, "sS")) != -1) {
+ while((c = getopt(argc, argv, "mfsS")) != -1) {
switch(c) {
case 'f':
input = fopen(optarg, "r");
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);
}
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_fixup_section(ctx, ctx->pctx->section);
if (package)
*package = pctx->package;
+ if (pctx->merge)
+ pctx->package = NULL;
pctx->name = NULL;
uci_switch_config(ctx);
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;
}
/*
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;
}