yang parser CHANGE move validation of enum name to separate function
so it can be reused in yin parser
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 0622bf4..e6aae12 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -1693,7 +1693,7 @@
{
LY_ERR ret = LY_SUCCESS;
char *buf, *word;
- size_t word_len, u;
+ size_t word_len;
enum yang_keyword kw;
struct lysp_type_enum *enm;
@@ -1702,24 +1702,8 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, data, enum_kw == YANG_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
if (enum_kw == YANG_ENUM) {
- if (!word_len) {
- LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
- free(buf);
- return LY_EVALID;
- } else if (isspace(word[0]) || isspace(word[word_len - 1])) {
- LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
- word_len, word);
- free(buf);
- return LY_EVALID;
- } else {
- for (u = 0; u < word_len; ++u) {
- if (iscntrl(word[u])) {
- LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
- word_len, word, u + 1);
- break;
- }
- }
- }
+ ret = lysp_check_enum_name(ctx, word, word_len);
+ LY_CHECK_ERR_RET(ret, free(buf), ret);
} else { /* YANG_BIT */
}
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index c367f96..2118af7 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -424,6 +424,29 @@
return LY_ENOTFOUND;
}
+LY_ERR
+lysp_check_enum_name(struct lys_parser_ctx *ctx, char *name, size_t name_len)
+{
+ if (!name_len) {
+ LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
+ return LY_EVALID;
+ } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
+ LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
+ name_len, name);
+ return LY_EVALID;
+ } else {
+ for (size_t u = 0; u < name_len; ++u) {
+ if (iscntrl(name[u])) {
+ LOGWRN(ctx->ctx, "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
+ name_len, name, u + 1);
+ break;
+ }
+ }
+ }
+
+ return LY_SUCCESS;
+}
+
/*
* @brief Check name of a new type to avoid name collisions.
*
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index 2d03115..17bae89 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -174,7 +174,7 @@
void lysp_sort_revisions(struct lysp_revision *revs);
/**
- * @brief Find type specified type definition
+ * @brief Find type specified type definition.
*
* @param[in] id Name of the type including possible prefix. Module where the prefix is being searched is start_module.
* @param[in] start_node Context node where the type is being instantiated to be able to search typedefs in parents.
@@ -188,6 +188,17 @@
LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module);
/**
+ * @brief Validate enum name.
+ *
+ * @param[in] ctx yang parser context for logging.
+ * @param[in] name String to check.
+ * @param[in] name_len Length of name.
+ *
+ * @return LY_ERR values
+ */
+LY_ERR lysp_check_enum_name(struct lys_parser_ctx *ctx, char *name, size_t name_len);
+
+/**
* @brief Find and parse module of the given name.
*
* @param[in] ctx libyang context.