cleanup, move parse_tuple to libuci, add some input validation
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 9530198..410d7e8 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -65,14 +65,24 @@ static int uci_show(int argc, char **argv)
        if (!configs)
                return 0;
 
+       if (argc >= 2) {
+               if (uci_load(ctx, argv[1], &package) != UCI_OK) {
+                       uci_perror(ctx, NULL);
+                       return 1;
+               }
+               uci_show_package(package, section);
+               uci_unload(ctx, package);
+               return 0;
+       }
+
        for (p = configs; *p; p++) {
                if ((argc < 2) || !strcmp(argv[1], *p)) {
                        if (uci_load(ctx, *p, &package) != UCI_OK) {
-                               uci_perror(ctx, "uci_load");
-                               return 255;
+                               uci_perror(ctx, NULL);
+                               return 1;
                        }
                        uci_show_package(package, section);
-                       uci_unload(ctx, *p);
+                       uci_unload(ctx, package);
                }
        }
 
@@ -95,44 +105,13 @@ static int uci_do_export(int argc, char **argv)
                        ret = uci_load(ctx, *p, &package);
                        if (ret)
                                continue;
-                       uci_export(ctx, stdout, package);
-                       uci_unload(ctx, *p);
+                       uci_export(ctx, stdout, package, true);
+                       uci_unload(ctx, package);
                }
        }
        return 0;
 }
 
-static void parse_tuple(char *str, char **package, char **section, char **option, char **value)
-{
-       char *last = NULL;
-
-       *package = strtok(str, ".");
-       if (!*package)
-               goto done;
-
-       last = *package;
-       *section = strtok(NULL, ".");
-       if (!*section)
-               goto done;
-
-       last = *section;
-       *option = strtok(NULL, ".");
-       if (!*option)
-               goto done;
-
-       last = *option;
-done:
-       if (!value)
-               return;
-
-       last = strtok(last, "=");
-       if (!last)
-               return;
-
-       *value = last + strlen(last) + 1;
-}
-
-
 static int uci_do_get(int argc, char **argv)
 {
        char *package = NULL;
@@ -145,8 +124,7 @@ static int uci_do_get(int argc, char **argv)
        if (argc != 2)
                return 255;
 
-       parse_tuple(argv[1], &package, &section, &option, NULL);
-       if (!package)
+       if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, NULL) != UCI_OK)
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
@@ -154,7 +132,7 @@ static int uci_do_get(int argc, char **argv)
                return 1;
        }
 
-       if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
+       if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
                return 1;
 
        switch(e->type) {
@@ -186,8 +164,7 @@ static int uci_do_set(int argc, char **argv)
        if (argc != 2)
                return 255;
 
-       parse_tuple(argv[1], &package, &section, &option, &value);
-       if (!package)
+       if (uci_parse_tuple(ctx, argv[1], &package, &section, &option, &value) != UCI_OK)
                return 1;
 
        if (uci_load(ctx, package, &p) != UCI_OK) {
@@ -199,7 +176,7 @@ static int uci_do_set(int argc, char **argv)
                uci_perror(ctx, "uci");
                return 1;
        }
-       uci_show_package(p, NULL);
+       uci_commit(ctx, p);
        return 0;
 }