Browse Source

allow "-" in package names

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
Felix Fietkau 9 years ago
parent
commit
e66c20610e
5 changed files with 26 additions and 22 deletions
  1. 9 9
      file.c
  2. 1 1
      test/references/import.data
  3. 1 1
      test/tests.d/000_import
  4. 4 4
      uci_internal.h
  5. 11 7
      util.c

+ 9 - 9
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);

+ 1 - 1
test/references/import.data

@@ -1,4 +1,4 @@
-package 'import'
+package 'import-test'
 
 config 'type' 'section'
 	option 'opt' 'val'

+ 1 - 1
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
 }

+ 4 - 4
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 */

+ 11 - 7
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;
 }