X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuci.git;a=blobdiff_plain;f=cli.c;h=845fc2acee988e0ac285ba6d6e8f900d53549cf3;hp=87de48cda96654ab63a7ab2e9a6479a9fc406c80;hb=96d35364c9159c96b164369d0d0c5147b0c73a24;hpb=05cf670dde3610279a07b76277cc29ceb4d7ad00 diff --git a/cli.c b/cli.c index 87de48c..845fc2a 100644 --- a/cli.c +++ b/cli.c @@ -18,8 +18,9 @@ static const char *appname = "uci"; static enum { CLI_FLAG_MERGE = (1 << 0), + CLI_FLAG_QUIET = (1 << 1) } flags; -static FILE *input = stdin; +static FILE *input; static struct uci_context *ctx; enum { @@ -50,7 +51,10 @@ static void uci_usage(int argc, char **argv) "Options:\n" "\t-f use as input instead of stdin\n" "\t-m when importing, merge data into an existing package\n" - "\t-s force strict mode (stop on parser errors)\n" + "\t-n name unnamed sections on export (default)\n" + "\t-N don't name unnamed sections\n" + "\t-q quiet mode (don't print error messages)\n" + "\t-s force strict mode (stop on parser errors, default)\n" "\t-S disable strict mode\n" "\n", argv[0] @@ -58,6 +62,14 @@ static void uci_usage(int argc, char **argv) exit(255); } +static void cli_perror(void) +{ + if (flags & CLI_FLAG_QUIET) + return; + + uci_perror(ctx, appname); +} + static void uci_show_section(struct uci_section *p) { struct uci_element *e; @@ -86,13 +98,15 @@ static int package_cmd(int cmd, char *package) struct uci_package *p = NULL; if (uci_load(ctx, package, &p) != UCI_OK) { - uci_perror(ctx, appname); + cli_perror(); return 1; } + if (!p) + return 0; switch(cmd) { case CMD_COMMIT: - if (uci_commit(ctx, &p) != UCI_OK) - uci_perror(ctx, appname); + if (uci_commit(ctx, &p, false) != UCI_OK) + cli_perror(); break; case CMD_EXPORT: uci_export(ctx, stdout, p, true); @@ -108,6 +122,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) { + cli_perror(); + return 1; + } + return 0; } @@ -123,7 +173,7 @@ static int uci_do_package_cmd(int cmd, int argc, char **argv) return package_cmd(cmd, argv[1]); if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) { - uci_perror(ctx, appname); + cli_perror(); return 1; } @@ -161,9 +211,11 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) return 1; if (uci_load(ctx, package, &p) != UCI_OK) { - uci_perror(ctx, appname); + cli_perror(); return 1; } + if (!p) + return 0; switch(cmd) { case CMD_GET: @@ -204,7 +256,7 @@ static int uci_do_section_cmd(int cmd, int argc, char **argv) ret = uci_save(ctx, p); if (ret != UCI_OK) { - uci_perror(ctx, appname); + cli_perror(); return 1; } @@ -257,13 +309,14 @@ int main(int argc, char **argv) int ret; int c; + input = stdin; ctx = uci_alloc_context(); if (!ctx) { fprintf(stderr, "Out of memory\n"); return 1; } - while((c = getopt(argc, argv, "sS")) != -1) { + while((c = getopt(argc, argv, "mf:sSnNq")) != -1) { switch(c) { case 'f': input = fopen(optarg, "r"); @@ -282,6 +335,15 @@ int main(int argc, char **argv) ctx->flags &= ~UCI_FLAG_STRICT; ctx->flags |= UCI_FLAG_PERROR; break; + case 'n': + ctx->flags |= UCI_FLAG_EXPORT_NAME; + break; + case 'N': + ctx->flags &= ~UCI_FLAG_EXPORT_NAME; + break; + case 'q': + flags |= CLI_FLAG_QUIET; + break; default: uci_usage(argc, argv); break;