]> pilppa.org Git - uci.git/commitdiff
better config file handling
authorFelix Fietkau <nbd@openwrt.org>
Sun, 20 Jan 2008 15:10:32 +0000 (16:10 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 20 Jan 2008 15:10:32 +0000 (16:10 +0100)
Makefile
libuci.c
parse.c
test.c
uci.h [moved from libuci.h with 93% similarity]

index bf194d141faa42773220901e0641c4d06cfc3d84..b33f6fad7b93c74871572cc83d9f4e31c9370921 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,8 +5,8 @@ all: parsetest
 parsetest: libuci.o test.o
        $(CC) $(CFLAGS) -o $@ $^
 
-libuci.o: libuci.c parse.c libuci.h list.c err.h
-test.o: test.c libuci.h
+libuci.o: libuci.c parse.c uci.h list.c err.h
+test.o: test.c uci.h
 
 clean:
        rm -f parsetest *.o
index 4dfffffc77efa00a400a2eb944b3a48fe66194ae..262ca698c5f68753ecd68a4e6a156e1e50e295ec 100644 (file)
--- a/libuci.c
+++ b/libuci.c
@@ -21,7 +21,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include "libuci.h"
+#include "uci.h"
 
 #define DEBUG
 #include "err.h"
@@ -31,6 +31,7 @@ static const char *uci_errstr[] = {
        [UCI_ERR_MEM] =      "Out of memory",
        [UCI_ERR_INVAL] =    "Invalid argument",
        [UCI_ERR_NOTFOUND] = "Entry not found",
+       [UCI_ERR_IO] =       "I/O error",
        [UCI_ERR_PARSE] =    "Parse error",
        [UCI_ERR_UNKNOWN] =  "Unknown error",
 };
diff --git a/parse.c b/parse.c
index 9dd7126f3325a01a361174f492590ff07725a0af..5fc46618208fb3c3bbee2da5400b53cb8ce56861 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -16,6 +16,7 @@
  * This file contains the code for parsing uci config files
  */
 
+#include <sys/stat.h>
 #include <ctype.h>
 
 #define LINEBUF        128
@@ -267,9 +268,11 @@ static void uci_parse_line(struct uci_context *ctx)
        }
 }
 
-int uci_parse(struct uci_context *ctx, const char *name)
+int uci_load(struct uci_context *ctx, const char *name)
 {
        struct uci_parse_context *pctx;
+       struct stat statbuf;
+       char *filename;
 
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, name != NULL);
@@ -280,11 +283,26 @@ int uci_parse(struct uci_context *ctx, const char *name)
        pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
        ctx->pctx = pctx;
 
-       /* TODO: use /etc/config/ */
-       pctx->file = fopen(name, "r");
-       if (!pctx->file)
+       switch (name[0]) {
+       case '.':
+       case '/':
+               /* absolute/relative path outside of /etc/config */
+               filename = (char *) name;
+               break;
+       default:
+               filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
+               sprintf(filename, UCI_CONFDIR "/%s", name);
+               break;
+       }
+
+       if ((stat(filename, &statbuf) < 0) ||
+               ((statbuf.st_mode &  S_IFMT) != S_IFREG))
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
 
+       pctx->file = fopen(filename, "r");
+       if (!pctx->file)
+               UCI_THROW(ctx, UCI_ERR_IO);
+
        pctx->cfg = uci_alloc_file(ctx, name);
 
        while (!feof(pctx->file)) {
diff --git a/test.c b/test.c
index 53b2a6a41c625f5cf4680bb575838b3a1d2ffd40..caf773b6f6056a80677b0fcad19300350ece99bd 100644 (file)
--- a/test.c
+++ b/test.c
@@ -10,7 +10,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */
-#include "libuci.h"
+#include "uci.h"
 
 int main(int argc, char **argv)
 {
@@ -21,7 +21,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if (uci_parse(ctx, argv[1])) {
+       if (uci_load(ctx, argv[1])) {
                uci_perror(ctx, "uci_parse");
                return 1;
        }
diff --git a/libuci.h b/uci.h
similarity index 93%
rename from libuci.h
rename to uci.h
index de96f45811bb955ebd6e39a959366fc1549f9edb..4706255db9dc66da1789bc66b37d03511a1c9243 100644 (file)
--- a/libuci.h
+++ b/uci.h
 #include <setjmp.h>
 #include <stdio.h>
 
+#define UCI_CONFDIR "/etc/config"
+
 enum
 {
        UCI_OK = 0,
        UCI_ERR_MEM,
        UCI_ERR_INVAL,
        UCI_ERR_NOTFOUND,
+       UCI_ERR_IO,
        UCI_ERR_PARSE,
        UCI_ERR_UNKNOWN,
        UCI_ERR_LAST
@@ -54,12 +57,12 @@ extern struct uci_context *uci_alloc(void);
 extern void uci_perror(struct uci_context *ctx, const char *str);
 
 /**
- * uci_parse: Parse an uci config file and store it in the uci context
+ * uci_load: Parse an uci config file and store it in the uci context
  *
  * @ctx: uci context
  * @name: name of the config file (relative to the config directory)
  */
-int uci_parse(struct uci_context *ctx, const char *name);
+int uci_load(struct uci_context *ctx, const char *name);
 
 /**
  * uci_cleanup: Clean up after an error