overhaul package and section type validation - makes it easier to read and fixes...
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index d5bcd8a..e56992e 100644 (file)
--- a/util.c
+++ b/util.c
@@ -96,6 +96,16 @@ __plugin bool uci_validate_str(const char *str, bool name)
        return true;
 }
 
+static inline bool uci_validate_package(const char *str)
+{
+       return uci_validate_str(str, false);
+}
+
+static inline bool uci_validate_type(const char *str)
+{
+       return uci_validate_str(str, false);
+}
+
 static inline bool uci_validate_name(const char *str)
 {
        return uci_validate_str(str, true);
@@ -120,6 +130,7 @@ static void uci_alloc_parse_context(struct uci_context *ctx)
 int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
 {
        char *last = NULL;
+       char *tmp;
 
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, str);
@@ -153,8 +164,12 @@ int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
                ptr->target = UCI_TYPE_OPTION;
        }
 
+       tmp = strsep(&str, ".");
+       if (tmp)
+               goto error;
+
 lastval:
-       if (ptr->package && !uci_validate_str(ptr->package, false))
+       if (ptr->package && !uci_validate_package(ptr->package))
                goto error;
        if (ptr->section && !uci_validate_name(ptr->section))
                ptr->flags |= UCI_LOOKUP_EXTENDED;
@@ -393,14 +408,14 @@ int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char *
        UCI_ASSERT(ctx, str != NULL);
        UCI_ASSERT(ctx, result != NULL);
 
-       if (ctx->pctx) {
-               if (ctx->pctx->file != stream) {
-                       uci_cleanup(ctx);
-               }
-       } else {
+       if (ctx->pctx && (ctx->pctx->file != stream))
+               uci_cleanup(ctx);
+
+       if (!ctx->pctx)
                uci_alloc_parse_context(ctx);
-               ctx->pctx->file = stream;
-       }
+
+       ctx->pctx->file = stream;
+
        if (!*str) {
                uci_getln(ctx, 0);
                *str = ctx->pctx->buf;