add support for listing config files
authorFelix Fietkau <nbd@openwrt.org>
Sun, 20 Jan 2008 21:16:59 +0000 (22:16 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 20 Jan 2008 21:16:59 +0000 (22:16 +0100)
.gitignore
Makefile
cli.c [new file with mode: 0644]
libuci.c
list.c
parse.c
test.c [deleted file]
uci.h

index 5d893ce..fabbfaa 100644 (file)
@@ -1,3 +1,3 @@
-parsetest
-*.o
+uci
+*.[oa]
 .*.swp
index 645ec65..ec382ed 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,21 @@
+COPTS=-g -O2
+CFLAGS=$(COPTS) -Wall -pedantic -std=gnu99 -Wno-unused -Werror
+
+AR=ar
 CC=gcc
-CFLAGS=-g -O2 -Wall -pedantic -std=gnu99 -Wno-unused -Werror
+RANLIB=ranlib
+
+all: uci
 
-all: parsetest
-parsetest: libuci.o test.o
+cli.o: cli.c uci.h
+uci: cli.o libuci.a
        $(CC) $(CFLAGS) -o $@ $^
 
 libuci.o: libuci.c parse.c uci.h list.c err.h
-test.o: test.c uci.h
+libuci.a: libuci.o
+       rm -f $@
+       $(AR) rc $@ $^
+       $(RANLIB) $@
 
 clean:
-       rm -f parsetest *.o
+       rm -f uci *.[oa]
diff --git a/cli.c b/cli.c
new file mode 100644 (file)
index 0000000..d83e257
--- /dev/null
+++ b/cli.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <strings.h>
+#include <stdlib.h>
+#include "uci.h"
+
+static struct uci_context *ctx;
+
+static void uci_usage(int argc, char **argv)
+{
+       fprintf(stderr,
+               "Usage: %s [options] <command> [arguments]\n\n"
+               "Commands:\n"
+               "\tshow [<config>[.<section>[.<option>]]]\n"
+               "\n",
+               argv[0]
+       );
+       exit(255);
+}
+
+static int uci_show(int argc, char **argv)
+{
+       char **configs = uci_list_configs(ctx);
+       char **p;
+
+       if (!configs)
+               return 0;
+
+       for (p = configs; *p; p++) {
+               fprintf(stderr, "# config: %s\n", *p);
+       }
+
+       return 0;
+}
+
+static int uci_cmd(int argc, char **argv)
+{
+       if (!strcasecmp(argv[0], "show"))
+               uci_show(argc, argv);
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       int ret;
+
+       ctx = uci_alloc();
+       if (argc < 2)
+               uci_usage(argc, argv);
+       ret = uci_cmd(argc - 1, argv + 1);
+       uci_free(ctx);
+
+       return ret;
+}
index cb0bc36..1902673 100644 (file)
--- a/libuci.c
+++ b/libuci.c
@@ -94,6 +94,18 @@ struct uci_context *uci_alloc(void)
        return ctx;
 }
 
+void uci_free(struct uci_context *ctx)
+{
+       struct uci_config *cfg;
+
+       uci_cleanup(ctx);
+       uci_foreach_entry(config, &ctx->root, cfg) {
+               uci_drop_file(cfg);
+       }
+       free(ctx);
+       return;
+}
+
 int uci_cleanup(struct uci_context *ctx)
 {
        UCI_HANDLE_ERR(ctx);
diff --git a/list.c b/list.c
index addfc73..f4ab8ec 100644 (file)
--- a/list.c
+++ b/list.c
@@ -12,6 +12,8 @@
  * GNU General Public License for more details.
  */
 
+#include <glob.h>
+
 /* initialize a list head/item */
 static inline void uci_list_init(struct uci_list *ptr)
 {
@@ -156,3 +158,31 @@ error:
        return NULL;
 }
 
+char **uci_list_configs(struct uci_context *ctx)
+{
+       char **configs;
+       glob_t globbuf;
+       int size, i;
+       char *buf;
+
+       if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
+               return NULL;
+
+       size = sizeof(char *) * (globbuf.gl_pathc + 1);
+       for(i = 0; i < globbuf.gl_pathc; i++)
+               size += strlen(globbuf.gl_pathv[i]) + 1;
+
+       configs = malloc(size);
+       if (!configs)
+               return NULL;
+
+       memset(configs, 0, size);
+       buf = (char *) &configs[globbuf.gl_pathc + 1];
+       for(i = 0; i < globbuf.gl_pathc; i++) {
+               configs[i] = buf;
+               strcpy(buf, globbuf.gl_pathv[i]);
+               buf += strlen(buf) + 1;
+       }
+       return configs;
+}
+
diff --git a/parse.c b/parse.c
index 31756d9..c8f090c 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -253,8 +253,6 @@ static void uci_parse_config(struct uci_context *ctx, char **str)
        type = next_arg(ctx, str, true);
        name = next_arg(ctx, str, false);
        assert_eol(ctx, str);
-
-       DPRINTF("Section<%s>: %s\n", type, name);
 }
 
 /*
@@ -270,8 +268,6 @@ static void uci_parse_option(struct uci_context *ctx, char **str)
        name = next_arg(ctx, str, true);
        value = next_arg(ctx, str, true);
        assert_eol(ctx, str);
-
-       DPRINTF("\tOption: %s=\"%s\"\n", name, value);
 }
 
 /*
diff --git a/test.c b/test.c
deleted file mode 100644 (file)
index caf773b..0000000
--- a/test.c
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
-#include "uci.h"
-
-int main(int argc, char **argv)
-{
-       struct uci_context *ctx = uci_alloc();
-
-       if (!ctx) {
-               fprintf(stderr, "Failed to allocate uci context");
-               return 1;
-       }
-
-       if (uci_load(ctx, argv[1])) {
-               uci_perror(ctx, "uci_parse");
-               return 1;
-       }
-
-       return 0;
-}
diff --git a/uci.h b/uci.h
index d18095d..26cc3b7 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -50,6 +50,11 @@ struct uci_parse_context;
 extern struct uci_context *uci_alloc(void);
 
 /**
+ * uci_free: Free the uci context including all of its data
+ */
+extern void uci_free(struct uci_context *ctx);
+
+/**
  * uci_perror: Print the last uci error that occured
  * @ctx: uci context
  * @str: string to print before the error message
@@ -62,15 +67,21 @@ extern void uci_perror(struct uci_context *ctx, const char *str);
  * @ctx: uci context
  * @name: name of the config file (relative to the config directory)
  */
-int uci_load(struct uci_context *ctx, const char *name);
+extern int uci_load(struct uci_context *ctx, const char *name);
 
 /**
  * uci_cleanup: Clean up after an error
  *
  * @ctx: uci context
  */
-int uci_cleanup(struct uci_context *ctx);
+extern int uci_cleanup(struct uci_context *ctx);
 
+/**
+ * uci_list_configs: List available uci config files
+ *
+ * @ctx: uci context
+ */
+extern char **uci_list_configs(struct uci_context *ctx);
 
 /* UCI data structures */