proper commit support, better debugging
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 7139f0b..1c31083 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -33,6 +33,10 @@ static void uci_usage(int argc, char **argv)
                "\tshow     [<config>[.<section>[.<option>]]]\n"
                "\tget      <config>.<section>[.<option>]\n"
                "\tset      <config>.<section>[.<option>]=<value>\n"
+               "\n"
+               "Options:\n"
+               "\t-s       force strict mode (stop on parser errors)\n"
+               "\t-S       disable strict mode\n"
                "\n",
                argv[0]
        );
@@ -66,16 +70,17 @@ static int uci_show(int argc, char **argv)
 {
        char *section = (argc > 2 ? argv[2] : NULL);
        struct uci_package *package;
-       char **configs;
+       char **configs = NULL;
        char **p;
 
-       configs = uci_list_configs(ctx);
-       if (!configs)
-               return 0;
+       if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
+               uci_perror(ctx, appname);
+               return 1;
+       }
 
        if (argc >= 2) {
                if (uci_load(ctx, argv[1], &package) != UCI_OK) {
-                       uci_perror(ctx, NULL);
+                       uci_perror(ctx, appname);
                        return 1;
                }
                uci_show_package(package, section);
@@ -86,7 +91,7 @@ static int uci_show(int argc, char **argv)
        for (p = configs; *p; p++) {
                if ((argc < 2) || !strcmp(argv[1], *p)) {
                        if (uci_load(ctx, *p, &package) != UCI_OK) {
-                               uci_perror(ctx, NULL);
+                               uci_perror(ctx, appname);
                                return 1;
                        }
                        uci_show_package(package, section);
@@ -99,11 +104,13 @@ static int uci_show(int argc, char **argv)
 
 static int uci_do_export(int argc, char **argv)
 {
-       char **configs = uci_list_configs(ctx);
+       char **configs = NULL;
        char **p;
 
-       if (!configs)
-               return 0;
+       if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
+               uci_perror(ctx, appname);
+               return 1;
+       }
 
        for (p = configs; *p; p++) {
                if ((argc < 2) || !strcmp(argv[1], *p)) {
@@ -120,6 +127,35 @@ static int uci_do_export(int argc, char **argv)
        return 0;
 }
 
+static int uci_do_commit(int argc, char **argv)
+{
+       char **configs = NULL;
+       char **p;
+
+       if ((uci_list_configs(ctx, &configs) != UCI_OK) || !configs) {
+               uci_perror(ctx, appname);
+               return 1;
+       }
+
+       for (p = configs; *p; p++) {
+               if ((argc < 2) || !strcmp(argv[1], *p)) {
+                       struct uci_package *package = NULL;
+                       int ret;
+
+                       if (uci_load(ctx, *p, &package) != UCI_OK) {
+                               uci_perror(ctx, appname);
+                               continue;
+                       }
+                       if (uci_commit(ctx, &package) != UCI_OK)
+                               uci_perror(ctx, appname);
+
+                       uci_unload(ctx, package);
+               }
+       }
+       return 0;
+}
+
+
 static int uci_do_cmd(int cmd, int argc, char **argv)
 {
        char *package = NULL;
@@ -140,11 +176,11 @@ static int uci_do_cmd(int cmd, int argc, char **argv)
                return 1;
        }
 
-       if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
-               return 1;
-
        switch(cmd) {
        case CMD_GET:
+               if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
+                       return 1;
+
                switch(e->type) {
                case UCI_TYPE_SECTION:
                        value = uci_to_section(e)->type;
@@ -194,6 +230,8 @@ static int uci_cmd(int argc, char **argv)
                return uci_show(argc, argv);
        if (!strcasecmp(argv[0], "export"))
                return uci_do_export(argc, argv);
+       if (!strcasecmp(argv[0], "commit"))
+               return uci_do_commit(argc, argv);
 
        if (!strcasecmp(argv[0], "get"))
                cmd = CMD_GET;