X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=cli.c;h=0c68d0836c6149deae7db4be43aa349ac2a67074;hp=0d663e1397e7cde07e480a938482a3c247d532d3;hb=1085fcb801f6e716b41d4c80942c7c802b5270b3;hpb=c57bfa88f8e6850edec6bd4da5881a0b4456c1e0 diff --git a/cli.c b/cli.c index 0d663e1..0c68d08 100644 --- a/cli.c +++ b/cli.c @@ -91,7 +91,7 @@ static int package_cmd(int cmd, char *package) } switch(cmd) { case CMD_COMMIT: - if (uci_commit(ctx, &p) != UCI_OK) + if (uci_commit(ctx, &p, false) != UCI_OK) uci_perror(ctx, appname); break; case CMD_EXPORT: @@ -108,6 +108,42 @@ static int package_cmd(int cmd, char *package) static int uci_do_import(int argc, char **argv) { + struct uci_package *package = NULL; + char *name = NULL; + int ret = UCI_OK; + + 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) { + if (flags & CLI_FLAG_MERGE) { + ret = uci_save(ctx, package); + } else { + struct uci_element *e; + /* loop through all config sections and overwrite existing data */ + uci_foreach_element(&ctx->root, e) { + struct uci_package *p = uci_to_package(e); + ret = uci_commit(ctx, &p, true); + } + } + } + + if (ret != UCI_OK) { + uci_perror(ctx, appname); + return 1; + } + return 0; } @@ -137,13 +173,14 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv) static int uci_do_section_cmd(int cmd, int argc, char **argv) { + struct uci_package *p = NULL; + struct uci_element *e = NULL; char *package = NULL; char *section = NULL; char *option = NULL; char *value = NULL; char **ptr = NULL; - struct uci_package *p = NULL; - struct uci_element *e = NULL; + int ret = UCI_OK; if (argc != 2) return 255; @@ -184,22 +221,13 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) printf("%s\n", value); break; case CMD_RENAME: - if (uci_rename(ctx, p, section, option, value) != UCI_OK) { - uci_perror(ctx, appname); - return 1; - } + ret = uci_rename(ctx, p, section, option, value); break; case CMD_SET: - if (uci_set(ctx, p, section, option, value) != UCI_OK) { - uci_perror(ctx, appname); - return 1; - } + ret = uci_set(ctx, p, section, option, value); break; case CMD_DEL: - if (uci_delete(ctx, p, section, option) != UCI_OK) { - uci_perror(ctx, appname); - return 1; - } + ret = uci_delete(ctx, p, section, option); break; } @@ -208,7 +236,10 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) return 0; /* save changes, but don't commit them yet */ - if (uci_save(ctx, p) != UCI_OK) { + if (ret == UCI_OK) + ret = uci_save(ctx, p); + + if (ret != UCI_OK) { uci_perror(ctx, appname); return 1; } @@ -268,7 +299,7 @@ int main(int argc, char **argv) return 1; } - while((c = getopt(argc, argv, "sS")) != -1) { + while((c = getopt(argc, argv, "mf:sS")) != -1) { switch(c) { case 'f': input = fopen(optarg, "r");