From b7c461d025b6db1f9c3e0ce137863d9f912cdbfa Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 30 Jan 2008 02:58:57 +0100 Subject: [PATCH] add strict mode flag (enabled by default, can be disabled to ignore lines with parser errors) --- cli.c | 27 +++++++++++++++++++++++++++ file.c | 10 ++++++++++ libuci.c | 1 + uci.h | 8 ++++++++ 4 files changed, 46 insertions(+) diff --git a/cli.c b/cli.c index 043ba1a..7139f0b 100644 --- a/cli.c +++ b/cli.c @@ -12,6 +12,7 @@ */ #include #include +#include #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 c; 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); + uci_free_context(ctx); return ret; diff --git a/file.c b/file.c index b8aa865..be63247 100644 --- 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); + + UCI_TRAP_SAVE(ctx, error); 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) diff --git a/libuci.c b/libuci.c index 74b3d7b..0dd81fe 100644 --- 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->flags = UCI_FLAG_STRICT; return ctx; } diff --git a/uci.h b/uci.h index 78a0249..e0b3d30 100644 --- a/uci.h +++ b/uci.h @@ -220,6 +220,11 @@ enum uci_type { 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; @@ -235,6 +240,9 @@ struct uci_context /* 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; -- 2.25.1