fix accidental lowercasing of license headers
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 1a478bb..c3e92e3 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -1,4 +1,5 @@
 /*
+ * cli - Command Line Interface for the Unified Configuration Interface
  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  *
  * This program is free software; you can redistribute it and/or modify
 
 static const char *appname = "uci";
 static enum {
-       CLI_FLAG_MERGE = (1 << 0),
+       CLI_FLAG_MERGE =    (1 << 0),
+       CLI_FLAG_QUIET =    (1 << 1),
+       CLI_FLAG_NOCOMMIT = (1 << 2),
 } flags;
-static FILE *input = stdin;
+static FILE *input;
 
 static struct uci_context *ctx;
 enum {
@@ -50,7 +53,12 @@ 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-n         name unnamed sections on export (default)\n"
+               "\t-N         don't name unnamed sections\n"
+               "\t-p <path>  add a search path for config change files\n"
+               "\t-P <path>  add a search path for config change files and use as default\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 +66,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 +102,17 @@ 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 (flags & CLI_FLAG_NOCOMMIT)
+                       return 0;
                if (uci_commit(ctx, &p, false) != UCI_OK)
-                       uci_perror(ctx, appname);
+                       cli_perror();
                break;
        case CMD_EXPORT:
                uci_export(ctx, stdout, p, true);
@@ -109,10 +129,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;
@@ -142,7 +160,7 @@ static int uci_do_import(int argc, char **argv)
        }
 
        if (ret != UCI_OK) {
-               uci_perror(ctx, appname);
+               cli_perror();
                return 1;
        }
 
@@ -161,7 +179,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;
        }
 
@@ -199,9 +217,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:
@@ -242,7 +262,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;
        }
 
@@ -295,13 +315,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, "f:mnNp:P:sSq")) != -1) {
                switch(c) {
                        case 'f':
                                input = fopen(optarg, "r");
@@ -320,6 +341,23 @@ 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 'p':
+                               uci_add_history_path(ctx, optarg);
+                               break;
+                       case 'P':
+                               uci_add_history_path(ctx, ctx->savedir);
+                               uci_set_savedir(ctx, optarg);
+                               flags |= CLI_FLAG_NOCOMMIT;
+                               break;
+                       case 'q':
+                               flags |= CLI_FLAG_QUIET;
+                               break;
                        default:
                                uci_usage(argc, argv);
                                break;