clean up history handling
[project/uci.git] / file.c
diff --git a/file.c b/file.c
index 412ce13..e6722b2 100644 (file)
--- a/file.c
+++ b/file.c
@@ -238,7 +238,7 @@ done:
 /*
  * extract the next argument from the command line
  */
-static char *next_arg(struct uci_context *ctx, char **str, bool required)
+static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
 {
        char *val;
        char *ptr;
@@ -248,6 +248,8 @@ static char *next_arg(struct uci_context *ctx, char **str, bool required)
        parse_str(ctx, str, &ptr);
        if (required && !*val)
                uci_parse_error(ctx, *str, "insufficient arguments");
+       if (name && !uci_validate_name(val))
+               uci_parse_error(ctx, val, "invalid character in field");
 
        return val;
 }
@@ -260,7 +262,7 @@ static void assert_eol(struct uci_context *ctx, char **str)
 {
        char *tmp;
 
-       tmp = next_arg(ctx, str, false);
+       tmp = next_arg(ctx, str, false, false);
        if (tmp && *tmp)
                uci_parse_error(ctx, *str, "too many arguments");
 }
@@ -293,14 +295,9 @@ static void uci_switch_config(struct uci_context *ctx)
         * if an older config under the same name exists, unload it
         * ignore errors here, e.g. if the config was not found
         */
-       UCI_TRAP_SAVE(ctx, ignore);
        e = uci_lookup_list(ctx, &ctx->root, name);
        if (e)
-               uci_unload(ctx, uci_to_package(e));
-       UCI_TRAP_RESTORE(ctx);
-ignore:
-       ctx->errno = 0;
-
+               UCI_THROW(ctx, UCI_ERR_DUPLICATE);
        pctx->package = uci_alloc_package(ctx, name);
 }
 
@@ -314,7 +311,7 @@ static void uci_parse_package(struct uci_context *ctx, char **str, bool single)
        /* command string null-terminated by strtok */
        *str += strlen(*str) + 1;
 
-       name = next_arg(ctx, str, true);
+       name = next_arg(ctx, str, true, true);
        assert_eol(ctx, str);
        if (single)
                return;
@@ -341,8 +338,8 @@ static void uci_parse_config(struct uci_context *ctx, char **str)
        /* command string null-terminated by strtok */
        *str += strlen(*str) + 1;
 
-       type = next_arg(ctx, str, true);
-       name = next_arg(ctx, str, false);
+       type = next_arg(ctx, str, true, true);
+       name = next_arg(ctx, str, false, true);
        assert_eol(ctx, str);
        ctx->pctx->section = uci_alloc_section(ctx->pctx->package, type, name);
 }
@@ -361,8 +358,8 @@ static void uci_parse_option(struct uci_context *ctx, char **str)
        /* command string null-terminated by strtok */
        *str += strlen(*str) + 1;
 
-       name = next_arg(ctx, str, true);
-       value = next_arg(ctx, str, true);
+       name = next_arg(ctx, str, true, true);
+       value = next_arg(ctx, str, true, false);
        assert_eol(ctx, str);
        uci_alloc_option(ctx->pctx->section, name, value);
 }
@@ -475,15 +472,14 @@ int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *packag
        UCI_HANDLE_ERR(ctx);
        UCI_ASSERT(ctx, stream != NULL);
 
-       if (package) {
+       if (package)
                uci_export_package(package, stream, header);
-               goto done;
+       else {
+               uci_foreach_element(&ctx->root, e) {
+                       uci_export_package(uci_to_package(e), stream, header);
+               }
        }
 
-       uci_foreach_element(&ctx->root, e) {
-               uci_export_package(uci_to_package(e), stream, header);
-       }
-done:
        return 0;
 }