add uci_lookup function
authorFelix Fietkau <nbd@openwrt.org>
Thu, 24 Jan 2008 17:20:11 +0000 (18:20 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Thu, 24 Jan 2008 17:20:11 +0000 (18:20 +0100)
list.c
uci.h

diff --git a/list.c b/list.c
index ea1c1ac..72d2eff 100644 (file)
--- a/list.c
+++ b/list.c
@@ -157,6 +157,44 @@ uci_free_package(struct uci_package *p)
        uci_free_element(&p->e);
 }
 
+static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, char *name)
+{
+       struct uci_element *e;
+
+       uci_foreach_element(list, e) {
+               if (!strcmp(e->name, name))
+                       return e;
+       }
+       UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+}
+
+int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option)
+{
+       struct uci_element *e;
+       struct uci_package *p;
+       struct uci_section *s;
+       struct uci_option *o;
+
+       UCI_HANDLE_ERR(ctx);
+       UCI_ASSERT(ctx, res != NULL);
+       UCI_ASSERT(ctx, package != NULL);
+
+       e = uci_lookup_list(ctx, &ctx->root, package);
+       if (!section)
+               goto found;
+
+       p = uci_to_package(e);
+       e = uci_lookup_list(ctx, &p->sections, section);
+       if (!option)
+               goto found;
+
+       s = uci_to_section(e);
+       e = uci_lookup_list(ctx, &s->options, option);
+
+found:
+       *res = e;
+       return 0;
+}
 
 int uci_unload(struct uci_context *ctx, const char *name)
 {
diff --git a/uci.h b/uci.h
index b9b5cb3..ce3e120 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -52,6 +52,7 @@ struct uci_list
        void *prev;
 };
 
+struct uci_element;
 struct uci_package;
 struct uci_section;
 struct uci_option;
@@ -120,6 +121,20 @@ extern int uci_unload(struct uci_context *ctx, const char *name);
 extern int uci_cleanup(struct uci_context *ctx);
 
 /**
+ * uci_lookup: Look up an uci element
+ *
+ * @ctx: uci context
+ * @res: where to store the result
+ * @package: config package
+ * @section: config section (optional)
+ * @option: option to search for (optional)
+ *
+ * If section is omitted, then a pointer to the config package is returned
+ * If option is omitted, then a pointer to the config section is returned
+ */
+extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option);
+
+/**
  * uci_list_configs: List available uci config files
  *
  * @ctx: uci context