-parsetest
-*.o
+uci
+*.[oa]
.*.swp
+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]
--- /dev/null
+/*
+ * 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;
+}
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);
* 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)
{
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;
+}
+
type = next_arg(ctx, str, true);
name = next_arg(ctx, str, false);
assert_eol(ctx, str);
-
- DPRINTF("Section<%s>: %s\n", type, name);
}
/*
name = next_arg(ctx, str, true);
value = next_arg(ctx, str, true);
assert_eol(ctx, str);
-
- DPRINTF("\tOption: %s=\"%s\"\n", name, value);
}
/*
+++ /dev/null
-/*
- * 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;
-}
*/
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
* @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 */