implement uci get
[project/uci.git] / cli.c
diff --git a/cli.c b/cli.c
index 85fab6f..4e0c5b7 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -94,12 +94,54 @@ static int uci_do_export(int argc, char **argv)
        return 0;
 }
 
+
+
+static int uci_do_get(int argc, char **argv)
+{
+       char *package = NULL;
+       char *section = NULL;
+       char *option = NULL;
+       struct uci_package *p = NULL;
+       struct uci_element *e = NULL;
+       char *value = NULL;
+
+       package = strtok(argv[1], ".");
+       if (!package)
+               return 1;
+
+       section = strtok(NULL, ".");
+       if (section)
+               option = strtok(NULL, ".");
+
+       if (uci_load(ctx, package, &p) != UCI_OK) {
+               uci_perror(ctx, "uci");
+               return 1;
+       }
+       if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
+               return 1;
+       switch(e->type) {
+       case UCI_TYPE_SECTION:
+               value = uci_to_section(e)->type;
+               break;
+       case UCI_TYPE_OPTION:
+               value = uci_to_option(e)->value;
+               break;
+       default:
+               /* should not happen */
+               return 1;
+       }
+       printf("%s\n", value);
+       return 0;
+}
+
 static int uci_cmd(int argc, char **argv)
 {
        if (!strcasecmp(argv[0], "show"))
                return uci_show(argc, argv);
        if (!strcasecmp(argv[0], "export"))
                return uci_do_export(argc, argv);
+       if (!strcasecmp(argv[0], "get"))
+               return uci_do_get(argc, argv);
        return 255;
 }
 
@@ -107,13 +149,13 @@ int main(int argc, char **argv)
 {
        int ret;
 
-       ctx = uci_alloc();
+       ctx = uci_alloc_context();
        if (argc < 2)
                uci_usage(argc, argv);
        ret = uci_cmd(argc - 1, argv + 1);
        if (ret == 255)
                uci_usage(argc, argv);
-       uci_free(ctx);
+       uci_free_context(ctx);
 
        return ret;
 }