add strict mode flag (enabled by default, can be disabled to ignore lines with parser...
authorFelix Fietkau <nbd@openwrt.org>
Wed, 30 Jan 2008 01:58:57 +0000 (02:58 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Wed, 30 Jan 2008 01:58:57 +0000 (02:58 +0100)
cli.c
file.c
libuci.c
uci.h

diff --git a/cli.c b/cli.c
index 043ba1a..7139f0b 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -12,6 +12,7 @@
  */
 #include <strings.h>
 #include <stdlib.h>
  */
 #include <strings.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include "uci.h"
 
 static const char *appname = "uci";
 #include "uci.h"
 
 static const char *appname = "uci";
@@ -208,13 +209,39 @@ static int uci_cmd(int argc, char **argv)
 int main(int argc, char **argv)
 {
        int ret;
 int main(int argc, char **argv)
 {
        int ret;
+       int c;
 
        ctx = uci_alloc_context();
 
        ctx = uci_alloc_context();
+       if (!ctx) {
+               fprintf(stderr, "Out of memory\n");
+               return 1;
+       }
+
+       while((c = getopt(argc, argv, "sS")) != -1) {
+               switch(c) {
+                       case 's':
+                               ctx->flags |= UCI_FLAG_STRICT;
+                               break;
+                       case 'S':
+                               ctx->flags &= ~UCI_FLAG_STRICT;
+                               ctx->flags |= UCI_FLAG_PERROR;
+                               break;
+                       default:
+                               uci_usage(argc, argv);
+                               break;
+               }
+       }
+       if (optind > 1)
+               argv[optind - 1] = argv[0];
+       argv += optind - 1;
+       argc -= optind - 1;
+
        if (argc < 2)
                uci_usage(argc, argv);
        ret = uci_cmd(argc - 1, argv + 1);
        if (ret == 255)
                uci_usage(argc, argv);
        if (argc < 2)
                uci_usage(argc, argv);
        ret = uci_cmd(argc - 1, argv + 1);
        if (ret == 255)
                uci_usage(argc, argv);
+
        uci_free_context(ctx);
 
        return ret;
        uci_free_context(ctx);
 
        return ret;
diff --git a/file.c b/file.c
index b8aa865..be63247 100644 (file)
--- a/file.c
+++ b/file.c
@@ -502,8 +502,18 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u
 
        while (!feof(pctx->file)) {
                uci_getln(ctx, 0);
 
        while (!feof(pctx->file)) {
                uci_getln(ctx, 0);
+
+               UCI_TRAP_SAVE(ctx, error);
                if (pctx->buf[0])
                        uci_parse_line(ctx, single);
                if (pctx->buf[0])
                        uci_parse_line(ctx, single);
+               UCI_TRAP_RESTORE(ctx);
+               continue;
+error:
+               if (ctx->flags & UCI_FLAG_PERROR)
+                       uci_perror(ctx, NULL);
+               if ((ctx->errno != UCI_ERR_PARSE) ||
+                       (ctx->flags & UCI_FLAG_STRICT))
+                       UCI_THROW(ctx, ctx->errno);
        }
 
        if (package)
        }
 
        if (package)
index 74b3d7b..0dd81fe 100644 (file)
--- a/libuci.c
+++ b/libuci.c
@@ -47,6 +47,7 @@ struct uci_context *uci_alloc_context(void)
        ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
        memset(ctx, 0, sizeof(struct uci_context));
        uci_list_init(&ctx->root);
        ctx = (struct uci_context *) malloc(sizeof(struct uci_context));
        memset(ctx, 0, sizeof(struct uci_context));
        uci_list_init(&ctx->root);
+       ctx->flags = UCI_FLAG_STRICT;
 
        return ctx;
 }
 
        return ctx;
 }
diff --git a/uci.h b/uci.h
index 78a0249..e0b3d30 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -220,6 +220,11 @@ enum uci_type {
        UCI_TYPE_OPTION = 3
 };
 
        UCI_TYPE_OPTION = 3
 };
 
+enum uci_flags {
+       UCI_FLAG_STRICT = (1 << 0), /* strict mode for the parser */
+       UCI_FLAG_PERROR = (1 << 1), /* print error messages to stderr */
+};
+
 struct uci_element
 {
        struct uci_list list;
 struct uci_element
 {
        struct uci_list list;
@@ -235,6 +240,9 @@ struct uci_context
        /* parser context, use for error handling only */
        struct uci_parse_context *pctx;
 
        /* parser context, use for error handling only */
        struct uci_parse_context *pctx;
 
+       /* uci runtime flags */
+       enum uci_flags flags;
+
        /* private: */
        int errno;
        const char *func;
        /* private: */
        int errno;
        const char *func;