minor comments
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 828c7b9..7870728 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -19,7 +19,7 @@ static const char *appname = "uci";
 static enum {
        CLI_FLAG_MERGE = (1 << 0),
 } flags;
-static FILE *input = stdin;
+static FILE *input;
 
 static struct uci_context *ctx;
 enum {
@@ -50,8 +50,10 @@ static void uci_usage(int argc, char **argv)
                "Options:\n"
                "\t-f <file>  use <file> 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-s         force strict mode (stop on parser errors, default)\n"
                "\t-S         disable strict mode\n"
+               "\t-n         name unnamed sections on export (default)\n"
+               "\t-N         don't name unnamed sections\n"
                "\n",
                argv[0]
        );
@@ -91,7 +93,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:
@@ -109,10 +111,8 @@ 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;
@@ -128,8 +128,17 @@ static int uci_do_import(int argc, char **argv)
                        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) {
+               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) {
@@ -286,13 +295,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, "mfsS")) != -1) {
+       while((c = getopt(argc, argv, "mf:sSnN")) != -1) {
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");
@@ -311,6 +321,12 @@ 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;
                        default:
                                uci_usage(argc, argv);
                                break;