From e66c20610e5a4039bfa8536149a9c651f5028544 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 18 Dec 2014 15:19:47 +0100 Subject: [PATCH] allow "-" in package names Signed-off-by: Felix Fietkau --- file.c | 18 +++++++++--------- test/references/import.data | 2 +- test/tests.d/000_import | 2 +- uci_internal.h | 8 ++++---- util.c | 18 +++++++++++------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/file.c b/file.c index 5be2e66..be75e54 100644 --- a/file.c +++ b/file.c @@ -237,7 +237,7 @@ done: /* * extract the next argument from the command line */ -static int next_arg(struct uci_context *ctx, bool required, bool name) +static int next_arg(struct uci_context *ctx, bool required, bool name, bool package) { struct uci_parse_context *pctx = ctx->pctx; int val, ptr; @@ -256,7 +256,7 @@ static int next_arg(struct uci_context *ctx, bool required, bool name) goto done; } - if (name && !uci_validate_name(pctx_str(pctx, val))) + if (name && !uci_validate_str(pctx_str(pctx, val), name, package)) uci_parse_error(ctx, "invalid character in name field"); done: @@ -282,7 +282,7 @@ int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char * uci_getln(ctx, 0); /*FIXME do we need to skip empty lines? */ - ofs_result = next_arg(ctx, false, false); + ofs_result = next_arg(ctx, false, false, false); *result = pctx_str(ctx->pctx, ofs_result); *str = pctx_cur_str(ctx->pctx); @@ -336,7 +336,7 @@ static void assert_eol(struct uci_context *ctx) int ofs_tmp; skip_whitespace(ctx); - ofs_tmp = next_arg(ctx, false, false); + ofs_tmp = next_arg(ctx, false, false, false); tmp = pctx_str(ctx->pctx, ofs_tmp); if (*tmp && (ctx->flags & UCI_FLAG_STRICT)) uci_parse_error(ctx, "too many arguments"); @@ -389,7 +389,7 @@ static void uci_parse_package(struct uci_context *ctx, bool single) /* command string null-terminated by strtok */ pctx->pos += strlen(pctx_cur_str(pctx)) + 1; - ofs_name = next_arg(ctx, true, true); + ofs_name = next_arg(ctx, true, true, true); name = pctx_str(pctx, ofs_name); assert_eol(ctx); if (single) @@ -422,12 +422,12 @@ static void uci_parse_config(struct uci_context *ctx) /* command string null-terminated by strtok */ pctx->pos += strlen(pctx_cur_str(pctx)) + 1; - ofs_type = next_arg(ctx, true, false); + ofs_type = next_arg(ctx, true, false, false); type = pctx_str(pctx, ofs_type); if (!uci_validate_type(type)) uci_parse_error(ctx, "invalid character in type field"); - ofs_name = next_arg(ctx, false, true); + ofs_name = next_arg(ctx, false, true, false); type = pctx_str(pctx, ofs_type); name = pctx_str(pctx, ofs_name); assert_eol(ctx); @@ -467,8 +467,8 @@ static void uci_parse_option(struct uci_context *ctx, bool list) /* command string null-terminated by strtok */ pctx->pos += strlen(pctx_cur_str(pctx)) + 1; - ofs_name = next_arg(ctx, true, true); - ofs_value = next_arg(ctx, false, false); + ofs_name = next_arg(ctx, true, true, false); + ofs_value = next_arg(ctx, false, false, false); name = pctx_str(pctx, ofs_name); value = pctx_str(pctx, ofs_value); assert_eol(ctx); diff --git a/test/references/import.data b/test/references/import.data index a7a21ef..7d549f9 100644 --- a/test/references/import.data +++ b/test/references/import.data @@ -1,4 +1,4 @@ -package 'import' +package 'import-test' config 'type' 'section' option 'opt' 'val' diff --git a/test/tests.d/000_import b/test/tests.d/000_import index a8a250c..45a1448 100644 --- a/test/tests.d/000_import +++ b/test/tests.d/000_import @@ -1,5 +1,5 @@ test_import () { ${UCI} import < ${REF_DIR}/import.data - assertSameFile ${REF_DIR}/import.result ${CONFIG_DIR}/import + assertSameFile ${REF_DIR}/import.result ${CONFIG_DIR}/import-test } diff --git a/uci_internal.h b/uci_internal.h index 89863f1..db8cc30 100644 --- a/uci_internal.h +++ b/uci_internal.h @@ -47,7 +47,7 @@ extern const char *uci_savedir; __private void *uci_malloc(struct uci_context *ctx, size_t size); __private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size); __private char *uci_strdup(struct uci_context *ctx, const char *str); -__private bool uci_validate_str(const char *str, bool name); +__private bool uci_validate_str(const char *str, bool name, bool package); __private void uci_add_delta(struct uci_context *ctx, struct uci_list *list, int cmd, const char *section, const char *option, const char *value); __private void uci_free_delta(struct uci_delta *h); __private struct uci_package *uci_alloc_package(struct uci_context *ctx, const char *name); @@ -71,17 +71,17 @@ __private int uci_load_delta(struct uci_context *ctx, struct uci_package *p, boo static inline bool uci_validate_package(const char *str) { - return uci_validate_str(str, false); + return uci_validate_str(str, false, true); } static inline bool uci_validate_type(const char *str) { - return uci_validate_str(str, false); + return uci_validate_str(str, false, false); } static inline bool uci_validate_name(const char *str) { - return uci_validate_str(str, true); + return uci_validate_str(str, true, false); } /* initialize a list head/item */ diff --git a/util.c b/util.c index f16a378..12aec9b 100644 --- a/util.c +++ b/util.c @@ -69,18 +69,22 @@ __private char *uci_strdup(struct uci_context *ctx, const char *str) * for names, only alphanum and _ is allowed (shell compatibility) * for types, we allow more characters */ -__private bool uci_validate_str(const char *str, bool name) +__private bool uci_validate_str(const char *str, bool name, bool package) { if (!*str) return false; - while (*str) { + for (; *str; str++) { unsigned char c = *str; - if (!isalnum(c) && c != '_') { - if (name || (c < 33) || (c > 126)) - return false; - } - str++; + + if (isalnum(c) || c == '_') + continue; + + if (c == '-' && package) + continue; + + if (name || (c < 33) || (c > 126)) + return false; } return true; } -- 2.25.1