blob: bbab210510b6cb3161ed3075382cae0023fb7a44 [file] [log] [blame]
/**
* @file parser_yin.c
* @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
* @brief YIN parser.
*
* Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "context.h"
#include "dict.h"
#include "in.h"
#include "in_internal.h"
#include "log.h"
#include "parser_internal.h"
#include "parser_schema.h"
#include "path.h"
#include "set.h"
#include "tree.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "xml.h"
/**
* @brief check if given string is URI of yin namespace.
*
* @param ns Namespace URI to check.
*
* @return true if ns equals YIN_NS_URI false otherwise.
*/
#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
enum yin_argument {
YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
YIN_ARG_NAME, /**< argument name */
YIN_ARG_TARGET_NODE, /**< argument target-node */
YIN_ARG_MODULE, /**< argument module */
YIN_ARG_VALUE, /**< argument value */
YIN_ARG_TEXT, /**< argument text */
YIN_ARG_CONDITION, /**< argument condition */
YIN_ARG_URI, /**< argument uri */
YIN_ARG_DATE, /**< argument data */
YIN_ARG_TAG, /**< argument tag */
YIN_ARG_NONE /**< empty (special value) */
};
const char * const yin_attr_list[] = {
[YIN_ARG_NAME] = "name",
[YIN_ARG_TARGET_NODE] = "target-node",
[YIN_ARG_MODULE] = "module",
[YIN_ARG_VALUE] = "value",
[YIN_ARG_TEXT] = "text",
[YIN_ARG_CONDITION] = "condition",
[YIN_ARG_URI] = "uri",
[YIN_ARG_DATE] = "date",
[YIN_ARG_TAG] = "tag",
[YIN_ARG_NONE] = "none",
};
#define yin_attr2str(STMT) yin_attr_list[STMT]
#define VALID_VALS1 " Only valid value is \"%s\"."
#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
/* flags to set constraints of subelements */
#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
time to simply check validity of given constraints */
struct yin_subelement {
enum ly_stmt type; /**< type of keyword */
void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
};
/* Meta information passed to yin_parse_argument function,
holds information about where content of argument element will be stored. */
struct yin_argument_meta {
uint16_t *flags; /**< Argument flags */
const char **argument; /**< Argument value */
};
/**
* @brief Meta information passed to functions working with tree_schema,
* that require additional information about parent node.
*/
struct tree_node_meta {
struct lysp_node *parent; /**< parent node */
struct lysp_node **nodes; /**< linked list of siblings */
};
/**
* @brief Meta information passed to yin_parse_import function.
*/
struct import_meta {
const char *prefix; /**< module prefix. */
struct lysp_import **imports; /**< imports to add to. */
};
/**
* @brief Meta information passed to yin_parse_include function.
*/
struct include_meta {
const char *name; /**< Module/submodule name. */
struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
};
/**
* @brief Meta information passed to yin_parse_inout function.
*/
struct inout_meta {
struct lysp_node *parent; /**< Parent node. */
struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
};
/**
* @brief Meta information passed to yin_parse_minmax function.
*/
struct minmax_dev_meta {
uint32_t *lim; /**< min/max value to write to. */
uint16_t *flags; /**< min/max flags to write to. */
struct lysp_ext_instance **exts; /**< extension instances to add to. */
};
LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
/**
* @brief Match yang keyword from yin data.
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
* @param[in] name Start of keyword name
* @param[in] name_len Lenght of keyword name.
* @param[in] prefix Start of keyword prefix.
* @param[in] prefix_len Lenght of prefix.
* @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
*
* @return yang_keyword values.
*/
enum ly_stmt
yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
const char *prefix, size_t prefix_len, enum ly_stmt parent)
{
const char *start = NULL;
enum ly_stmt kw = LY_STMT_NONE;
const struct lyxml_ns *ns = NULL;
struct ly_in *in;
if (!name || (name_len == 0)) {
return LY_STMT_NONE;
}
ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
if (ns) {
if (!IS_YIN_NS(ns->uri)) {
return LY_STMT_EXTENSION_INSTANCE;
}
} else {
/* elements without namespace are automatically unknown */
return LY_STMT_NONE;
}
LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
start = in->current;
kw = lysp_match_kw(NULL, in);
name = in->current;
ly_in_free(in, 0);
if (name - start == (long int)name_len) {
/* this is done because of collision in yang statement value and yang argument mapped to yin element value */
if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
return LY_STMT_ARG_VALUE;
}
return kw;
} else {
if (strncmp(start, "text", name_len) == 0) {
return LY_STMT_ARG_TEXT;
} else {
return LY_STMT_NONE;
}
}
}
/**
* @brief Match argument name.
*
* @param[in] name String representing name.
* @param[in] len Lenght of the name.
*
* @return yin_argument values.
*/
enum yin_argument
yin_match_argument_name(const char *name, size_t len)
{
enum yin_argument arg = YIN_ARG_UNKNOWN;
size_t already_read = 0;
LY_CHECK_RET(len == 0, YIN_ARG_NONE);
#define READ_INC(LEN) already_read += LEN
#define ARG_SET(STMT) arg=STMT
#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
switch (*name) {
case 'c':
READ_INC(1);
if (ARG_CHECK("ondition", 8)) {
ARG_SET(YIN_ARG_CONDITION);
}
break;
case 'd':
READ_INC(1);
if (ARG_CHECK("ate", 3)) {
ARG_SET(YIN_ARG_DATE);
}
break;
case 'm':
READ_INC(1);
if (ARG_CHECK("odule", 5)) {
ARG_SET(YIN_ARG_MODULE);
}
break;
case 'n':
READ_INC(1);
if (ARG_CHECK("ame", 3)) {
ARG_SET(YIN_ARG_NAME);
}
break;
case 't':
READ_INC(1);
if (ARG_CHECK("a", 1)) {
if (ARG_CHECK("g", 1)) {
ARG_SET(YIN_ARG_TAG);
} else if (ARG_CHECK("rget-node", 9)) {
ARG_SET(YIN_ARG_TARGET_NODE);
}
} else if (ARG_CHECK("ext", 3)) {
ARG_SET(YIN_ARG_TEXT);
}
break;
case 'u':
READ_INC(1);
if (ARG_CHECK("ri", 2)) {
ARG_SET(YIN_ARG_URI);
}
break;
case 'v':
READ_INC(1);
if (ARG_CHECK("alue", 4)) {
ARG_SET(YIN_ARG_VALUE);
}
break;
}
/* whole argument must be matched */
if (already_read != len) {
arg = YIN_ARG_UNKNOWN;
}
#undef ARG_CHECK
#undef ARG_SET
#undef READ_INC
return arg;
}
#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
#define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT)
/**
* @brief Free subelems information allocated on heap.
*
* @param[in] count Size of subelems array.
* @param[in] subelems Subelems array to free.
*/
static void
subelems_deallocator(size_t count, struct yin_subelement *subelems)
{
for (size_t i = 0; i < count; ++i) {
if (HAS_META(subelems[i].type)) {
free(subelems[i].dest);
}
}
free(subelems);
}
/**
* @brief Allocate subelems information on heap.
*
* @param[in] ctx YIN parser context, used for logging.
* @param[in] count Number of subelements.
* @param[in] parent Parent node if any.
* @param[out] result Allocated subelems array.
*
* @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
*/
static LY_ERR
subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
struct yin_subelement **result, ...)
{
va_list ap;
*result = calloc(count, sizeof **result);
LY_CHECK_GOTO(!(*result), mem_err);
va_start(ap, result);
for (size_t i = 0; i < count; ++i) {
/* TYPE */
(*result)[i].type = va_arg(ap, enum ly_stmt);
/* DEST */
if (IS_NODE_ELEM((*result)[i].type)) {
struct tree_node_meta *node_meta = NULL;
node_meta = calloc(1, sizeof *node_meta);
LY_CHECK_GOTO(!node_meta, mem_err);
node_meta->parent = parent;
node_meta->nodes = va_arg(ap, void *);
(*result)[i].dest = node_meta;
} else if ((*result)[i].type == LY_STMT_IMPORT) {
struct import_meta *imp_meta = NULL;
imp_meta = calloc(1, sizeof *imp_meta);
LY_CHECK_GOTO(!imp_meta, mem_err);
imp_meta->prefix = va_arg(ap, const char *);
imp_meta->imports = va_arg(ap, struct lysp_import **);
(*result)[i].dest = imp_meta;
} else if ((*result)[i].type == LY_STMT_INCLUDE) {
struct include_meta *inc_meta = NULL;
inc_meta = calloc(1, sizeof *inc_meta);
LY_CHECK_GOTO(!inc_meta, mem_err);
inc_meta->name = va_arg(ap, const char *);
inc_meta->includes = va_arg(ap, struct lysp_include **);
(*result)[i].dest = inc_meta;
} else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
struct inout_meta *inout_meta = NULL;
inout_meta = calloc(1, sizeof *inout_meta);
LY_CHECK_GOTO(!inout_meta, mem_err);
inout_meta->parent = parent;
inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *);
(*result)[i].dest = inout_meta;
} else {
(*result)[i].dest = va_arg(ap, void *);
}
/* FLAGS */
(*result)[i].flags = va_arg(ap, int);
}
va_end(ap);
return LY_SUCCESS;
mem_err:
subelems_deallocator(count, *result);
LOGMEM(ctx->xmlctx->ctx);
return LY_EMEM;
}
/**
* @brief Check that val is valid UTF8 character sequence of val_type.
* Doesn't check empty string, only character validity.
*
* @param[in] ctx Yin parser context for logging.
* @param[in] val_type Type of the input string to select method of checking character validity.
*
* @return LY_ERR values.
*/
LY_ERR
yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type)
{
uint8_t prefix = 0;
uint32_t c;
size_t utf8_char_len, already_read = 0;
const char *val;
assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
val = ctx->xmlctx->value;
while (already_read < ctx->xmlctx->value_len) {
LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
already_read += utf8_char_len;
LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
switch (val_type) {
case Y_IDENTIF_ARG:
LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, NULL));
break;
case Y_PREF_IDENTIF_ARG:
LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, &prefix));
break;
case Y_STR_ARG:
case Y_MAYBE_STR_ARG:
LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
break;
}
}
return LY_SUCCESS;
}
/**
* @brief Parse yin attributes.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
* special argument).
* @param[out] arg_val Where value of argument should be stored. Can be NULL iff arg_type is specified as YIN_ARG_NONE.
* @param[in] val_type Type of expected value of attribute.
* @param[in] current_element Identification of current element, used for logging.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_attribute(struct lys_yin_parser_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type,
enum ly_stmt current_element)
{
enum yin_argument arg = YIN_ARG_UNKNOWN;
bool found = false;
/* validation of attributes */
while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
/* yin arguments represented as attributes have no namespace, which in this case means no prefix */
if (!ctx->xmlctx->prefix) {
arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
if (arg == YIN_ARG_NONE) {
/* skip it */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
} else if (arg == arg_type) {
LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
found = true;
/* go to value */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_validate_value(ctx, val_type));
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg_val);
LY_CHECK_RET(!(*arg_val), LY_EMEM);
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
ctx->xmlctx->name, ly_stmt2str(current_element));
return LY_EVALID;
}
} else {
/* skip it */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
/* next attribute */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
/* anything else than Y_MAYBE_STR_ARG is mandatory */
if ((val_type != Y_MAYBE_STR_ARG) && !found) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
yin_attr2str(arg_type), ly_stmt2str(current_element));
return LY_EVALID;
}
return LY_SUCCESS;
}
/**
* @brief Get record with given type.
*
* @param[in] type Type of wanted record.
* @param[in] array_size Size of array.
* @param[in] array Searched array.
*
* @return Pointer to desired record on success, NULL if element is not in the array.
*/
static struct yin_subelement *
get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
{
for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
if (array[u].type == type) {
return &array[u];
}
}
return NULL;
}
/**
* @brief Helper function to check mandatory constraint of subelement.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] subelem_info Array of information about subelements.
* @param[in] subelem_info_size Size of subelem_info array.
* @param[in] current_element Identification of element that is currently being parsed, used for logging.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
signed char subelem_info_size, enum ly_stmt current_element)
{
for (signed char i = 0; i < subelem_info_size; ++i) {
/* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
return LY_EVALID;
}
}
return LY_SUCCESS;
}
/**
* @brief Helper function to check "first" constraint of subelement.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] subelem_info Array of information about subelements.
* @param[in] subelem_info_size Size of subelem_info array.
* @param[in] current_element Identification of element that is currently being parsed, used for logging.
* @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement, used for logging.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
signed char subelem_info_size, enum ly_stmt current_element,
struct yin_subelement *exp_first)
{
for (signed char i = 0; i < subelem_info_size; ++i) {
if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
return LY_EVALID;
}
}
return LY_SUCCESS;
}
/**
* @brief Parse simple element without any special constraints and argument mapped to yin attribute,
* for example prefix or namespace element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] kw Type of current element.
* @param[out] value Where value of attribute should be stored.
* @param[in] arg_type Expected type of attribute.
* @param[in] arg_val_type Type of expected value of attribute.
* @param[in,out] exts Extension instances to add to.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
{
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
}
/**
* @brief Parse path element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] kw Type of current element.
* @param[out] type Type structure to store parsed value, flags and extension instances.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
{
LY_ERR ret;
const char *str_path;
LY_CHECK_RET(yin_parse_simple_element(ctx, kw, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
lydict_remove(ctx->xmlctx->ctx, str_path);
LY_CHECK_RET(ret);
type->flags |= LYS_SET_PATH;
return LY_SUCCESS;
}
/**
* @brief Parse pattern element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] type Type structure to store parsed value, flags and extension instances.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
const char *real_value = NULL;
char *saved_value = NULL;
struct lysp_restr *restr;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN));
size_t len = strlen(real_value);
saved_value = malloc(len + 2);
LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
memmove(saved_value + 1, real_value, len);
FREE_STRING(ctx->xmlctx->ctx, real_value);
saved_value[0] = LYSP_RESTR_PATTERN_ACK;
saved_value[len + 1] = '\0';
LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
restr->arg.mod = ctx->parsed_mod;
type->flags |= LYS_SET_PATTERN;
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
{LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
{LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_PATTERN, NULL, &restr->exts);
}
/**
* @brief Parse fraction-digits element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] type Type structure to store value, flags and extension instances.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
const char *temp_val = NULL;
char *ptr;
unsigned long int num;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS));
if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
errno = 0;
num = strtoul(temp_val, &ptr, LY_BASE_DEC);
if (*ptr != '\0') {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
type->fraction_digits = num;
type->flags |= LYS_SET_FRDIGITS;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
}
/**
* @brief Parse enum element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] type Type structure to store parsed value, flags and extension instances.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
struct lysp_type_enum *en;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
type->flags |= LYS_SET_ENUM;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &en->iffeatures, 0},
{LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ENUM, NULL, &en->exts);
}
/**
* @brief Parse bit element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] type Type structure to store parsed value, flags and extension instances.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
struct lysp_type_enum *en;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
type->flags |= LYS_SET_BIT;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &en->iffeatures, 0},
{LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BIT, NULL, &en->exts);
}
/**
* @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
* more instances, such as base or if-feature.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] kw Type of current element.
* @param[out] values Parsed values to add to.
* @param[in] arg_type Expected type of attribute.
* @param[in] arg_val_type Type of expected value of attribute.
* @param[in,out] exts Extension instances to add to.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_simple_elements(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char ***values, enum yin_argument arg_type,
enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
{
const char **value;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, &index, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
}
/**
* @brief Parse simple element without any special constraints and argument mapped to yin attribute.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] kw Type of current element.
* @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
* @param[in] arg_type Expected type of attribute.
* @param[in] arg_val_type Type of expected value of attribute.
* @param[in,out] exts Extension instances to add to.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
{
if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
arg_type, arg_val_type, exts));
} else {
LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
arg_type, arg_val_type, exts));
}
return LY_SUCCESS;
}
/**
* @brief Parse base element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] parent Identification of parent element.
* @param[out] dest Where parsed values should be stored.
* @param[in,out] exts Extension instances to add to.
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_base(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, void *dest, struct lysp_ext_instance **exts)
{
struct lysp_type *type = NULL;
if (parent == LY_STMT_TYPE) {
type = (struct lysp_type *)dest;
LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
Y_PREF_IDENTIF_ARG, exts));
type->flags |= LYS_SET_BASE;
} else if (parent == LY_STMT_IDENTITY) {
LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
} else {
LOGINT(ctx->xmlctx->ctx);
return LY_EINT;
}
return LY_SUCCESS;
}
/**
* @brief Parse require-instance element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] type Type structure to store value, flag and extensions.
* @return LY_ERR values.
*/
static LY_ERR
yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
const char *temp_val = NULL;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
type->flags |= LYS_SET_REQINST;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE));
if (strcmp(temp_val, "true") == 0) {
type->require_instance = 1;
} else if (strcmp(temp_val, "false") != 0) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
"require-instance", "true", "false");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
}
/**
* @brief Parse modifier element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] pat Value to write to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
{
assert(**pat == 0x06);
const char *temp_val;
char *modified_val;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER));
if (strcmp(temp_val, "invert-match") != 0) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
"modifier", "invert-match");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
lydict_remove(ctx->xmlctx->ctx, temp_val);
/* allocate new value */
modified_val = malloc(strlen(*pat) + 1);
LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
strcpy(modified_val, *pat);
lydict_remove(ctx->xmlctx->ctx, *pat);
/* modify the new value */
modified_val[0] = LYSP_RESTR_PATTERN_NACK;
LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MODIFIER, NULL, exts);
}
/**
* @brief Parse a restriction element (length, range or one instance of must).
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] restr_kw Identificaton of element that is being parsed, can be set to LY_STMT_MUST, LY_STMT_LENGTH or LY_STMT_RANGE.
* @param[in] restr Value to write to.
*/
static LY_ERR
yin_parse_restriction(struct lys_yin_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
{
assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
{LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
/* argument of must is called condition, but argument of length and range is called value */
enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
restr->arg.mod = ctx->parsed_mod;
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr_kw, NULL, &restr->exts);
}
/**
* @brief Parse range element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] type Type structure to store parsed value and flags.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
type->range = calloc(1, sizeof *type->range);
LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
type->flags |= LYS_SET_RANGE;
return LY_SUCCESS;
}
/**
* @brief Parse length element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] type Type structure to store parsed value and flags.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
{
type->length = calloc(1, sizeof *type->length);
LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
type->flags |= LYS_SET_LENGTH;
return LY_SUCCESS;
}
/**
* @brief Parse must element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] restrs Restrictions to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
{
struct lysp_restr *restr;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
}
/**
* @brief Parse a node id into an array.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] kw Type of current element.
* @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_qname(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
struct lysp_ext_instance **exts)
{
struct lysp_qname *qname, **qnames;
switch (kw) {
case LY_STMT_DEFAULT:
if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
qname = (struct lysp_qname *)subinfo->dest;
} else {
qnames = (struct lysp_qname **)subinfo->dest;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
}
qname->mod = ctx->parsed_mod;
return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
case LY_STMT_IF_FEATURE:
assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
qnames = (struct lysp_qname **)subinfo->dest;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
qname->mod = ctx->parsed_mod;
return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
default:
break;
}
LOGINT(ctx->xmlctx->ctx);
return LY_EINT;
}
/**
* @brief Parse position or value element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] kw Type of current element, can be set to LY_STMT_POSITION or LY_STMT_VALUE.
* @param[out] enm Enum structure to save value, flags and extension instances.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_value_pos(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type_enum *enm)
{
assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
const char *temp_val = NULL;
char *ptr;
long int num = 0;
unsigned long int unum = 0;
/* set value flag */
enm->flags |= LYS_SET_VALUE;
/* get attribute value */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
goto error;
}
/* convert value */
errno = 0;
if (kw == LY_STMT_VALUE) {
num = strtol(temp_val, &ptr, LY_BASE_DEC);
if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
goto error;
}
} else {
unum = strtoul(temp_val, &ptr, LY_BASE_DEC);
if (unum > UINT64_C(4294967295)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
goto error;
}
}
/* check if whole argument value was converted */
if (*ptr != '\0') {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
goto error;
}
if (errno == ERANGE) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw));
goto error;
}
/* save correctly ternary operator can't be used because num and unum have different signes */
if (kw == LY_STMT_VALUE) {
enm->value = num;
} else {
enm->value = unum;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
/* parse subelements */
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, &enm->exts);
error:
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
/**
* @brief Parse belongs-to element.
*
* @param[in] ctx YIN parser context for logging and to store current state.
* @param[out] submod Structure of submodule that is being parsed.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values
*/
static LY_ERR
yin_parse_belongs_to(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts)
{
const char *belongsto;
struct yin_subelement subelems[] = {
{LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
if (ctx->parsed_mod->mod->name != belongsto) {
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
belongsto, ctx->parsed_mod->mod->name);
lydict_remove(ctx->xmlctx->ctx, belongsto);
return LY_EVALID;
}
lydict_remove(ctx->xmlctx->ctx, belongsto);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BELONGS_TO, NULL, exts);
}
/**
* @brief Function to parse meta tags (description, contact, ...) eg. elements with
* text element as child.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] elem_type Type of element can be set to LY_STMT_ORGANIZATION or LY_STMT_CONTACT or LY_STMT_DESCRIPTION or LY_STMT_REFERENCE.
* @param[out] value Where the content of meta element should be stored.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_meta(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts)
{
assert(elem_type == LY_STMT_ORGANIZATION || elem_type == LY_STMT_CONTACT || elem_type == LY_STMT_DESCRIPTION || elem_type == LY_STMT_REFERENCE);
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
{LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
};
/* check attributes */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
/* parse content */
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), elem_type, NULL, exts);
}
/**
* @brief Parse error-message element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] value Where the content of error-message element should be stored.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_err_msg(struct lys_yin_parser_ctx *ctx, const char **value, struct lysp_ext_instance **exts)
{
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
{LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
};
/* check attributes */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE));
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ERROR_MESSAGE, NULL, exts);
}
/**
* @brief Parse type element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] parent Identification of parent element.
* @param[in,out] type Type to write to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
{
struct lysp_type *type = NULL;
if (parent == LY_STMT_DEVIATE) {
*(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
type = *((struct lysp_type **)subinfo->dest);
} else {
type = (struct lysp_type *)subinfo->dest;
}
type->pmod = ctx->parsed_mod;
/* type as child of another type */
if (parent == LY_STMT_TYPE) {
struct lysp_type *nested_type = NULL;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
type->flags |= LYS_SET_TYPE;
type = nested_type;
}
struct yin_subelement subelems[] = {
{LY_STMT_BASE, type, 0},
{LY_STMT_BIT, type, 0},
{LY_STMT_ENUM, type, 0},
{LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
{LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
{LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
{LY_STMT_PATTERN, type, 0},
{LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
{LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
{LY_STMT_TYPE, type},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPE, NULL, &type->exts);
}
/**
* @brief Parse max-elements element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] max Value to write to.
* @param[in] flags Flags to write to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_maxelements(struct lys_yin_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *temp_val = NULL;
char *ptr;
unsigned long int num;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
*flags |= LYS_SET_MAX;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS));
if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '0') || ((temp_val[0] != 'u') && !isdigit(temp_val[0]))) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
if (strcmp(temp_val, "unbounded")) {
errno = 0;
num = strtoul(temp_val, &ptr, LY_BASE_DEC);
if (*ptr != '\0') {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
if ((errno == ERANGE) || (num > UINT32_MAX)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
*max = num;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MAX_ELEMENTS, NULL, exts);
}
/**
* @brief Parse min-elements element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] min Value to write to.
* @param[in] flags Flags to write to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_minelements(struct lys_yin_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *temp_val = NULL;
char *ptr;
unsigned long int num;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
*flags |= LYS_SET_MIN;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS));
if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
errno = 0;
num = strtoul(temp_val, &ptr, LY_BASE_DEC);
if (ptr[0] != 0) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
if ((errno == ERANGE) || (num > UINT32_MAX)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
*min = num;
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MIN_ELEMENTS, NULL, exts);
}
/**
* @brief Parse min-elements or max-elements element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] parent Identification of parent element.
* @param[in] current Identification of current element.
* @param[in] dest Where the parsed value and flags should be stored.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_minmax(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, enum ly_stmt current, void *dest)
{
assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
assert(parent == LY_STMT_LEAF_LIST || parent == LY_STMT_REFINE || parent == LY_STMT_LIST || parent == LY_STMT_DEVIATE);
uint32_t *lim;
uint16_t *flags;
struct lysp_ext_instance **exts;
if (parent == LY_STMT_LEAF_LIST) {
lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
flags = &((struct lysp_node_leaflist *)dest)->flags;
exts = &((struct lysp_node_leaflist *)dest)->exts;
} else if (parent == LY_STMT_REFINE) {
lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
flags = &((struct lysp_refine *)dest)->flags;
exts = &((struct lysp_refine *)dest)->exts;
} else if (parent == LY_STMT_LIST) {
lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
flags = &((struct lysp_node_list *)dest)->flags;
exts = &((struct lysp_node_list *)dest)->exts;
} else {
lim = ((struct minmax_dev_meta *)dest)->lim;
flags = ((struct minmax_dev_meta *)dest)->flags;
exts = ((struct minmax_dev_meta *)dest)->exts;
}
if (current == LY_STMT_MAX_ELEMENTS) {
LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
} else {
LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
}
return LY_SUCCESS;
}
/**
* @brief Parse ordered-by element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] flags Flags to write to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *temp_val;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY));
if (strcmp(temp_val, "system") == 0) {
*flags |= LYS_ORDBY_SYSTEM;
} else if (strcmp(temp_val, "user") == 0) {
*flags |= LYS_ORDBY_USER;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
"ordered-by", "system", "user");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ORDERED_BY, NULL, exts);
}
/**
* @brief Parse any-data or any-xml element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] any_kw Identification of current element, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_any(struct lys_yin_parser_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta)
{
struct lysp_node_anydata *any;
/* create new sibling */
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
any->parent = node_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
struct yin_subelement subelems[] = {
{LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &any->iffeatures, 0},
{LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_MUST, &any->musts, 0},
{LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any_kw, NULL, &any->exts);
}
/**
* @brief parse leaf element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
struct lysp_node_leaf *leaf;
/* create structure new leaf */
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
leaf->nodetype = LYS_LEAF;
leaf->parent = node_meta->parent;
/* parser argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF));
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
{LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
{LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_MUST, &leaf->musts, 0},
{LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
{LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
{LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF, NULL, &leaf->exts);
}
/**
* @brief Parse leaf-list element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
struct lysp_node_leaflist *llist;
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
llist->nodetype = LYS_LEAFLIST;
llist->parent = node_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST));
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_DEFAULT, &llist->dflts, 0},
{LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
{LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
{LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
{LY_STMT_MUST, &llist->musts, 0},
{LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
{LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
{LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF_LIST, NULL, &llist->exts));
/* check invalid combination of subelements */
if ((llist->min) && (llist->dflts)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
return LY_EVALID;
}
if (llist->max && (llist->min > llist->max)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
return LY_EVALID;
}
return LY_SUCCESS;
}
/**
* @brief Parse typedef element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] typedef_meta Meta information about parent node and typedefs to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
{
struct lysp_tpdf *tpdf;
struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF));
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
{LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
{LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPEDEF, NULL, &tpdf->exts));
/* store data for collision check */
if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT |
LYS_OUTPUT | LYS_NOTIF))) {
LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
}
return LY_SUCCESS;
}
/**
* @brief Parse refine element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] refines Refines to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
{
struct lysp_refine *rf;
/* allocate new refine */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
/* parse attribute */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_DEFAULT, &rf->dflts, 0},
{LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
{LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
{LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
{LY_STMT_MUST, &rf->musts, 0},
{LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REFINE, NULL, &rf->exts);
}
/**
* @brief Parse uses element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
struct lysp_node_uses *uses;
/* create new uses */
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
uses->nodetype = LYS_USES;
uses->parent = node_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES));
/* parse content */
struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
struct yin_subelement subelems[] = {
{LY_STMT_AUGMENT, &augments, 0},
{LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
{LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFINE, &uses->refines, 0},
{LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_USES, NULL, &uses->exts));
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
return LY_SUCCESS;
}
/**
* @brief Parse revision element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] revs Parsed revisions to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
{
struct lysp_revision *rev;
const char *temp_date = NULL;
/* allocate new reivison */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION));
/* check value */
if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
FREE_STRING(ctx->xmlctx->ctx, temp_date);
return LY_EVALID;
}
strcpy(rev->date, temp_date);
FREE_STRING(ctx->xmlctx->ctx, temp_date);
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION, NULL, &rev->exts);
}
/**
* @brief Parse include element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
{
struct lysp_include *inc;
/* allocate new include */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE));
/* submodules share the namespace with the module names, so there must not be
* a module of the same name in the context, no need for revision matching */
if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name);
return LY_EVALID;
}
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
{LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
{LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_INCLUDE, NULL, &inc->exts);
}
/**
* @brief Parse revision-date element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] rev Array to store the parsed value in.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
{
const char *temp_rev;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
FREE_STRING(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
strcpy(rev, temp_rev);
FREE_STRING(ctx->xmlctx->ctx, temp_rev);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION_DATE, NULL, exts);
}
/**
* @brief Parse config element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] flags Flags to add to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *temp_val = NULL;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG));
if (strcmp(temp_val, "true") == 0) {
*flags |= LYS_CONFIG_W;
} else if (strcmp(temp_val, "false") == 0) {
*flags |= LYS_CONFIG_R;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
"true", "false");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_CONFIG, NULL, exts);
}
/**
* @brief Parse yang-version element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] version Storage for the parsed information.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
{
const char *temp_version = NULL;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION));
if (strcmp(temp_version, "1") == 0) {
*version = LYS_VERSION_1_0;
} else if (strcmp(temp_version, "1.1") == 0) {
*version = LYS_VERSION_1_1;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
"yang-version", "1", "1.1");
FREE_STRING(ctx->xmlctx->ctx, temp_version);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_version);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YANG_VERSION, NULL, exts);
}
/**
* @brief Parse import element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] imp_meta Meta information about prefix and imports to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
{
struct lysp_import *imp;
/* allocate new element in sized array for import */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
/* parse import attributes */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IMPORT, NULL, &imp->exts));
/* check prefix validity */
LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
return LY_SUCCESS;
}
/**
* @brief Parse mandatory element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] flags Flags to add to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *temp_val = NULL;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY));
if (strcmp(temp_val, "true") == 0) {
*flags |= LYS_MAND_TRUE;
} else if (strcmp(temp_val, "false") == 0) {
*flags |= LYS_MAND_FALSE;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
"mandatory", "true", "false");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MANDATORY, NULL, exts);
}
/**
* @brief Parse status element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] flags Flags to add to.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *value = NULL;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS));
if (strcmp(value, "current") == 0) {
*flags |= LYS_STATUS_CURR;
} else if (strcmp(value, "deprecated") == 0) {
*flags |= LYS_STATUS_DEPRC;
} else if (strcmp(value, "obsolete") == 0) {
*flags |= LYS_STATUS_OBSLT;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
"status", "current", "deprecated", "obsolete");
FREE_STRING(ctx->xmlctx->ctx, value);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, value);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_STATUS, NULL, exts);
}
/**
* @brief Parse when element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[out] when_p When pointer to parse to.
*/
static LY_ERR
yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
{
struct lysp_when *when;
LY_ERR ret = LY_SUCCESS;
when = calloc(1, sizeof *when);
LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
ret = lyxml_ctx_next(ctx->xmlctx);
LY_CHECK_ERR_RET(ret, free(when), ret);
ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
LY_CHECK_ERR_RET(ret, free(when), ret);
*when_p = when;
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_WHEN, NULL, &when->exts);
}
/**
* @brief Parse yin-elemenet element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] data Data to read from, always moved to currently handled position.
* @param[in,out] flags Flags to add to.
* @prama[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_yin_element(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
{
const char *temp_val = NULL;
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT));
if (strcmp(temp_val, "true") == 0) {
*flags |= LYS_YINELEM_TRUE;
} else if (strcmp(temp_val, "false") == 0) {
*flags |= LYS_YINELEM_FALSE;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
"yin-element", "true", "false");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YIN_ELEMENT, NULL, exts);
}
/**
* @brief Parse argument element.
*
* @param[in,out] xmlctx Xml context.
* @param[in,out] arg_meta Meta information about destionation of parsed data.
* @param[in,out] exts Extension instances to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_argument(struct lys_yin_parser_ctx *ctx, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
{
struct yin_subelement subelems[] = {
{LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT));
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ARGUMENT, NULL, exts);
}
/**
* @brief Parse the extension statement.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] extensions Extensions to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
{
struct lysp_ext *ex;
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION));
struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
struct yin_subelement subelems[] = {
{LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
{LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_EXTENSION, NULL, &ex->exts);
}
/**
* @brief Parse feature element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] features Features to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
{
struct lysp_feature *feat;
/* allocate new feature */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE));
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
{LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FEATURE, NULL, &feat->exts);
}
/**
* @brief Parse identity element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] identities Identities to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
{
struct lysp_ident *ident;
/* allocate new identity */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY));
/* parse content */
struct yin_subelement subelems[] = {
{LY_STMT_BASE, &ident->bases, 0},
{LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
{LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IDENTITY, NULL, &ident->exts);
}
/**
* @brief Parse list element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
struct lysp_node_list *list;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
list->nodetype = LYS_LIST;
list->parent = node_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST));
/* parse list content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
LY_STMT_ACTION, &list->actions, 0,
LY_STMT_ANYDATA, &list->child, 0,
LY_STMT_ANYXML, &list->child, 0,
LY_STMT_CHOICE, &list->child, 0,
LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_CONTAINER, &list->child, 0,
LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_GROUPING, &list->groupings, 0,
LY_STMT_IF_FEATURE, &list->iffeatures, 0,
LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
LY_STMT_LEAF, &list->child, 0,
LY_STMT_LEAF_LIST, &list->child, 0,
LY_STMT_LIST, &list->child, 0,
LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
LY_STMT_MUST, &list->musts, 0,
LY_STMT_NOTIFICATION, &list->notifs, 0,
LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_TYPEDEF, &list->typedefs, 0,
LY_STMT_UNIQUE, &list->uniques, 0,
LY_STMT_USES, &list->child, 0,
LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_LIST, NULL, &list->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
/* finalize parent pointers to the reallocated items */
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
if (list->max && (list->min > list->max)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
return LY_EVALID;
}
return LY_SUCCESS;
}
/**
* @brief Parse notification element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] notif_meta Meta information about parent node and notifications to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
{
struct lysp_notif *notif;
struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->nodes;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* allocate new notification */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *notifs, notif, LY_EMEM);
notif->nodetype = LYS_NOTIF;
notif->parent = notif_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION));
/* parse notification content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
LY_STMT_ANYDATA, &notif->data, 0,
LY_STMT_ANYXML, &notif->data, 0,
LY_STMT_CHOICE, &notif->data, 0,
LY_STMT_CONTAINER, &notif->data, 0,
LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_GROUPING, &notif->groupings, 0,
LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
LY_STMT_LEAF, &notif->data, 0,
LY_STMT_LEAF_LIST, &notif->data, 0,
LY_STMT_LIST, &notif->data, 0,
LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_TYPEDEF, &notif->typedefs, 0,
LY_STMT_USES, &notif->data, 0,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_NOTIFICATION, NULL, &notif->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
/* finalize parent pointers to the reallocated items */
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
return LY_SUCCESS;
}
/**
* @brief Parse grouping element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in,out] gr_meta Meta information about parent node and groupings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
{
struct lysp_grp *grp;
struct lysp_grp **grps = (struct lysp_grp **)gr_meta->nodes;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* create new grouping */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *grps, grp, LY_EMEM);
grp->nodetype = LYS_GROUPING;
grp->parent = gr_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING));
/* parse grouping content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)grp, &subelems,
LY_STMT_ACTION, &grp->actions, 0,
LY_STMT_ANYDATA, &grp->data, 0,
LY_STMT_ANYXML, &grp->data, 0,
LY_STMT_CHOICE, &grp->data, 0,
LY_STMT_CONTAINER, &grp->data, 0,
LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_GROUPING, &grp->groupings, 0,
LY_STMT_LEAF, &grp->data, 0,
LY_STMT_LEAF_LIST, &grp->data, 0,
LY_STMT_LIST, &grp->data, 0,
LY_STMT_NOTIFICATION, &grp->notifs, 0,
LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_TYPEDEF, &grp->typedefs, 0,
LY_STMT_USES, &grp->data, 0,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_GROUPING, NULL, &grp->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
/* finalize parent pointers to the reallocated items */
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
return LY_SUCCESS;
}
/**
* @brief Parse container element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
struct lysp_node_container *cont;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* create new container */
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
cont->nodetype = LYS_CONTAINER;
cont->parent = node_meta->parent;
/* parse aegument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER));
/* parse container content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &cont->child, 0,
LY_STMT_CHOICE, &cont->child, 0,
LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_CONTAINER, &cont->child, 0,
LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_GROUPING, &cont->groupings, 0,
LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
LY_STMT_LEAF, &cont->child, 0,
LY_STMT_LEAF_LIST, &cont->child, 0,
LY_STMT_LIST, &cont->child, 0,
LY_STMT_MUST, &cont->musts, 0,
LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_TYPEDEF, &cont->typedefs, 0,
LY_STMT_USES, &cont->child, 0,
LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CONTAINER, NULL, &cont->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
return LY_SUCCESS;
}
/**
* @brief Parse case element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
struct lysp_node_case *cas;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* create new case */
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
cas->nodetype = LYS_CASE;
cas->parent = node_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE));
/* parse case content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &cas->child, 0,
LY_STMT_CHOICE, &cas->child, 0,
LY_STMT_CONTAINER, &cas->child, 0,
LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
LY_STMT_LEAF, &cas->child, 0,
LY_STMT_LEAF_LIST, &cas->child, 0,
LY_STMT_LIST, &cas->child, 0,
LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_USES, &cas->child, 0,
LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CASE, NULL, &cas->exts);
subelems_deallocator(subelems_size, subelems);
return ret;
}
/**
* @brief Parse choice element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] node_meta Meta information about parent node and siblings to add to.
*
* @return LY_ERR values.
*/
LY_ERR
yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
{
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
struct lysp_node_choice *choice;
size_t subelems_size;
/* create new choice */
LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
choice->nodetype = LYS_CHOICE;
choice->parent = node_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE));
/* parse choice content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &choice->child, 0,
LY_STMT_CASE, &choice->child, 0,
LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_CONTAINER, &choice->child, 0,
LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
LY_STMT_LEAF, &choice->child, 0,
LY_STMT_LEAF_LIST, &choice->child, 0,
LY_STMT_LIST, &choice->child, 0,
LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CHOICE, NULL, &choice->exts);
subelems_deallocator(subelems_size, subelems);
return ret;
}
/**
* @brief Parse input or output element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] inout_kw Identification of input/output element.
* @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_inout(struct lys_yin_parser_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta)
{
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* initiate structure */
inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
inout_meta->inout_p->parent = inout_meta->parent;
/* check attributes */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
/* parser input/output content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
LY_STMT_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &inout_meta->inout_p->data, 0,
LY_STMT_CHOICE, &inout_meta->inout_p->data, 0,
LY_STMT_CONTAINER, &inout_meta->inout_p->data, 0,
LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
LY_STMT_LEAF, &inout_meta->inout_p->data, 0,
LY_STMT_LEAF_LIST, &inout_meta->inout_p->data, 0,
LY_STMT_LIST, &inout_meta->inout_p->data, 0,
LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
LY_STMT_USES, &inout_meta->inout_p->data, 0,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, inout_kw, NULL, &inout_meta->inout_p->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
if (!inout_meta->inout_p->data) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
return LY_EVALID;
}
/* finalize parent pointers to the reallocated items */
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings, NULL, NULL, NULL));
return LY_SUCCESS;
}
/**
* @brief Parse action element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] act_meta Meta information about parent node and actions to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
{
struct lysp_action *act, **acts = (struct lysp_action **)act_meta->nodes;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* create new action */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *acts, act, LY_EMEM);
act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
act->parent = act_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, LY_STMT_ACTION));
/* parse content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_GROUPING, &act->groupings, 0,
LY_STMT_IF_FEATURE, &act->iffeatures, 0,
LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_TYPEDEF, &act->typedefs, 0,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = (yin_parse_content(ctx, subelems, subelems_size, LY_STMT_ACTION, NULL, &act->exts));
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
/* always initialize inout, they are technically present (needed for later deviations/refines) */
if (!act->input.nodetype) {
act->input.nodetype = LYS_INPUT;
act->input.parent = (struct lysp_node *)act;
}
if (!act->output.nodetype) {
act->output.nodetype = LYS_OUTPUT;
act->output.parent = (struct lysp_node *)act;
}
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
return LY_SUCCESS;
}
/**
* @brief Parse augment element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] aug_meta Meta information about parent node and augments to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
{
struct lysp_augment *aug;
struct lysp_augment **augs = (struct lysp_augment **)aug_meta->nodes;
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
size_t subelems_size;
/* create new augment */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *augs, aug, LY_EMEM);
aug->nodetype = LYS_AUGMENT;
aug->parent = aug_meta->parent;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
/* parser augment content */
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &aug->child, 0,
LY_STMT_CASE, &aug->child, 0,
LY_STMT_CHOICE, &aug->child, 0,
LY_STMT_CONTAINER, &aug->child, 0,
LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
LY_STMT_LEAF, &aug->child, 0,
LY_STMT_LEAF_LIST, &aug->child, 0,
LY_STMT_LIST, &aug->child, 0,
LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
LY_STMT_USES, &aug->child, 0,
LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_AUGMENT, NULL, &aug->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
return LY_SUCCESS;
}
/**
* @brief Parse deviate element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] deviates Deviates to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
{
LY_ERR ret = LY_SUCCESS;
uint8_t dev_mod;
const char *temp_val;
struct lysp_deviate *d;
struct lysp_deviate_add *d_add = NULL;
struct lysp_deviate_rpl *d_rpl = NULL;
struct lysp_deviate_del *d_del = NULL;
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE));
if (strcmp(temp_val, "not-supported") == 0) {
dev_mod = LYS_DEV_NOT_SUPPORTED;
} else if (strcmp(temp_val, "add") == 0) {
dev_mod = LYS_DEV_ADD;
} else if (strcmp(temp_val, "replace") == 0) {
dev_mod = LYS_DEV_REPLACE;
} else if (strcmp(temp_val, "delete") == 0) {
dev_mod = LYS_DEV_DELETE;
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
"not-supported", "add", "replace", "delete");
FREE_STRING(ctx->xmlctx->ctx, temp_val);
return LY_EVALID;
}
FREE_STRING(ctx->xmlctx->ctx, temp_val);
if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
d = calloc(1, sizeof *d);
LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
struct yin_subelement subelems[] = {
{LY_STMT_EXTENSION_INSTANCE, NULL, 0}
};
ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d->exts);
} else if (dev_mod == LYS_DEV_ADD) {
d_add = calloc(1, sizeof *d_add);
LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
d = (struct lysp_deviate *)d_add;
struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
struct yin_subelement subelems[] = {
{LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_DEFAULT, &d_add->dflts, 0},
{LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
{LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
{LY_STMT_MUST, &d_add->musts, 0},
{LY_STMT_UNIQUE, &d_add->uniques, 0},
{LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_add->exts);
} else if (dev_mod == LYS_DEV_REPLACE) {
d_rpl = calloc(1, sizeof *d_rpl);
LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
d = (struct lysp_deviate *)d_rpl;
struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
struct yin_subelement subelems[] = {
{LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
{LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
{LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
{LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
{LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
{LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_rpl->exts);
} else {
d_del = calloc(1, sizeof *d_del);
LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
d = (struct lysp_deviate *)d_del;
struct yin_subelement subelems[] = {
{LY_STMT_DEFAULT, &d_del->dflts, 0},
{LY_STMT_MUST, &d_del->musts, 0},
{LY_STMT_UNIQUE, &d_del->uniques, 0},
{LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_del->exts);
}
LY_CHECK_GOTO(ret, cleanup);
d->mod = dev_mod;
/* insert into siblings */
LY_LIST_INSERT(deviates, d, next);
return ret;
cleanup:
free(d);
return ret;
}
/**
* @brief Parse deviation element.
*
* @param[in,out] ctx YIN parser context for logging and to store current state.
* @param[in] deviations Deviations to add to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
{
struct lysp_deviation *dev;
/* create new deviation */
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
/* parse argument */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
struct yin_subelement subelems[] = {
{LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
{LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
{LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
{LY_STMT_EXTENSION_INSTANCE, NULL, 0},
};
return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATION, NULL, &dev->exts);
}
/**
* @brief Map keyword type to substatement info.
*
* @param[in] kw Keyword type.
*
* @return correct LYEXT_SUBSTMT information.
*/
static LYEXT_SUBSTMT
kw2lyext_substmt(enum ly_stmt kw)
{
switch (kw) {
case LY_STMT_ARGUMENT:
return LYEXT_SUBSTMT_ARGUMENT;
case LY_STMT_BASE:
return LYEXT_SUBSTMT_BASE;
case LY_STMT_BELONGS_TO:
return LYEXT_SUBSTMT_BELONGSTO;
case LY_STMT_CONTACT:
return LYEXT_SUBSTMT_CONTACT;
case LY_STMT_DEFAULT:
return LYEXT_SUBSTMT_DEFAULT;
case LY_STMT_DESCRIPTION:
return LYEXT_SUBSTMT_DESCRIPTION;
case LY_STMT_ERROR_APP_TAG:
return LYEXT_SUBSTMT_ERRTAG;
case LY_STMT_ERROR_MESSAGE:
return LYEXT_SUBSTMT_ERRMSG;
case LY_STMT_KEY:
return LYEXT_SUBSTMT_KEY;
case LY_STMT_NAMESPACE:
return LYEXT_SUBSTMT_NAMESPACE;
case LY_STMT_ORGANIZATION:
return LYEXT_SUBSTMT_ORGANIZATION;
case LY_STMT_PATH:
return LYEXT_SUBSTMT_PATH;
case LY_STMT_PREFIX:
return LYEXT_SUBSTMT_PREFIX;
case LY_STMT_PRESENCE:
return LYEXT_SUBSTMT_PRESENCE;
case LY_STMT_REFERENCE:
return LYEXT_SUBSTMT_REFERENCE;
case LY_STMT_REVISION_DATE:
return LYEXT_SUBSTMT_REVISIONDATE;
case LY_STMT_UNITS:
return LYEXT_SUBSTMT_UNITS;
case LY_STMT_VALUE:
return LYEXT_SUBSTMT_VALUE;
case LY_STMT_YANG_VERSION:
return LYEXT_SUBSTMT_VERSION;
case LY_STMT_MODIFIER:
return LYEXT_SUBSTMT_MODIFIER;
case LY_STMT_REQUIRE_INSTANCE:
return LYEXT_SUBSTMT_REQINSTANCE;
case LY_STMT_YIN_ELEMENT:
return LYEXT_SUBSTMT_YINELEM;
case LY_STMT_CONFIG:
return LYEXT_SUBSTMT_CONFIG;
case LY_STMT_MANDATORY:
return LYEXT_SUBSTMT_MANDATORY;
case LY_STMT_ORDERED_BY:
return LYEXT_SUBSTMT_ORDEREDBY;
case LY_STMT_STATUS:
return LYEXT_SUBSTMT_STATUS;
case LY_STMT_FRACTION_DIGITS:
return LYEXT_SUBSTMT_FRACDIGITS;
case LY_STMT_MAX_ELEMENTS:
return LYEXT_SUBSTMT_MAX;
case LY_STMT_MIN_ELEMENTS:
return LYEXT_SUBSTMT_MIN;
case LY_STMT_POSITION:
return LYEXT_SUBSTMT_POSITION;
case LY_STMT_UNIQUE:
return LYEXT_SUBSTMT_UNIQUE;
case LY_STMT_IF_FEATURE:
return LYEXT_SUBSTMT_IFFEATURE;
default:
return LYEXT_SUBSTMT_SELF;
}
}
/**
* @brief map keyword to keyword-group.
*
* @param[in] ctx YIN parser context used for logging.
* @param[in] kw Keyword that is child of module or submodule.
* @param[out] group Group of keyword.
*
* @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
*/
static LY_ERR
kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
{
switch (kw) {
/* module header */
case LY_STMT_NONE:
case LY_STMT_NAMESPACE:
case LY_STMT_PREFIX:
case LY_STMT_BELONGS_TO:
case LY_STMT_YANG_VERSION:
*group = Y_MOD_MODULE_HEADER;
break;
/* linkage */
case LY_STMT_INCLUDE:
case LY_STMT_IMPORT:
*group = Y_MOD_LINKAGE;
break;
/* meta */
case LY_STMT_ORGANIZATION:
case LY_STMT_CONTACT:
case LY_STMT_DESCRIPTION:
case LY_STMT_REFERENCE:
*group = Y_MOD_META;
break;
/* revision */
case LY_STMT_REVISION:
*group = Y_MOD_REVISION;
break;
/* body */
case LY_STMT_ANYDATA:
case LY_STMT_ANYXML:
case LY_STMT_AUGMENT:
case LY_STMT_CHOICE:
case LY_STMT_CONTAINER:
case LY_STMT_DEVIATION:
case LY_STMT_EXTENSION:
case LY_STMT_FEATURE:
case LY_STMT_GROUPING:
case LY_STMT_IDENTITY:
case LY_STMT_LEAF:
case LY_STMT_LEAF_LIST:
case LY_STMT_LIST:
case LY_STMT_NOTIFICATION:
case LY_STMT_RPC:
case LY_STMT_TYPEDEF:
case LY_STMT_USES:
case LY_STMT_EXTENSION_INSTANCE:
*group = Y_MOD_BODY;
break;
default:
LOGINT(ctx->xmlctx->ctx);
return LY_EINT;
}
return LY_SUCCESS;
}
/**
* @brief Check if relative order of two keywords is valid.
*
* @param[in] ctx YIN parser context used for logging.
* @param[in] kw Current keyword.
* @param[in] next_kw Next keyword.
* @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
* because relative order is required only in module and submodule sub-elements, used for logging.
*
* @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
*/
static LY_ERR
yin_check_relative_order(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum ly_stmt next_kw, enum ly_stmt parrent)
{
assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
enum yang_module_stmt gr, next_gr;
LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
if (gr > next_gr) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
return LY_EVALID;
}
return LY_SUCCESS;
}
/**
* @brief Get element name prefixed by full URI of xml namespace.
*
* @param[in] ctx YIN parser context used for logging and to get inormation about xml namespaces.
* @param[in] name Name of element.
* @param[in] name_len Length of element name.
* @param[in] prefix Prefix of element.
* @param[in] prefix_len Length of element prefix.
*
* @return Element name prefixed by URI on success, NULL on failure.
*/
static const char *
name2nsname(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
{
const char *ename = NULL;
const struct lyxml_ns *ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
LY_CHECK_ERR_RET(!ns, LOGINT(ctx->xmlctx->ctx), NULL);
if (!strcmp(ns->uri, YIN_NS_URI)) {
/* standard YANG statement in YIN namespace - keep it unprefixed as in case of YANG */
lydict_insert(ctx->xmlctx->ctx, name, name_len, &ename);
return ename;
}
/* some statement in special namespace (extension instance) */
size_t ns_len = strlen(ns->uri);
size_t len = ns_len + name_len + 1; /* +1 because of ':' delimiter between ns and actual name */
char *result;
char *temp;
temp = result = malloc(sizeof(*temp) * (len + 1)); /* +1 for '\0' terminator */
LY_CHECK_ERR_RET(!temp, LOGMEM(ctx->xmlctx->ctx), NULL);
strcpy(result, ns->uri);
result[ns_len] = ':';
temp = &result[ns_len + 1];
strncpy(temp, name, name_len);
result[len] = '\0';
lydict_insert_zc(ctx->xmlctx->ctx, result, &ename);
return ename;
}
/**
* @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
* @param[in] elem_type Type of element that is currently being parsed.
* @param[out] arg Value to write to.
*
* @return LY_ERR values.
*/
static LY_ERR
yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
{
enum ly_stmt child;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
switch (elem_type) {
case LY_STMT_ACTION:
case LY_STMT_ANYDATA:
case LY_STMT_ANYXML:
case LY_STMT_ARGUMENT:
case LY_STMT_BASE:
case LY_STMT_BIT:
case LY_STMT_CASE:
case LY_STMT_CHOICE:
case LY_STMT_CONTAINER:
case LY_STMT_ENUM:
case LY_STMT_EXTENSION:
case LY_STMT_FEATURE:
case LY_STMT_GROUPING:
case LY_STMT_IDENTITY:
case LY_STMT_IF_FEATURE:
case LY_STMT_LEAF:
case LY_STMT_LEAF_LIST:
case LY_STMT_LIST:
case LY_STMT_MODULE:
case LY_STMT_NOTIFICATION:
case LY_STMT_RPC:
case LY_STMT_SUBMODULE:
case LY_STMT_TYPE:
case LY_STMT_TYPEDEF:
case LY_STMT_UNITS:
case LY_STMT_USES:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_AUGMENT:
case LY_STMT_DEVIATION:
case LY_STMT_REFINE:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_CONFIG:
case LY_STMT_DEFAULT:
case LY_STMT_DEVIATE:
case LY_STMT_ERROR_APP_TAG:
case LY_STMT_FRACTION_DIGITS:
case LY_STMT_KEY:
case LY_STMT_LENGTH:
case LY_STMT_MANDATORY:
case LY_STMT_MAX_ELEMENTS:
case LY_STMT_MIN_ELEMENTS:
case LY_STMT_MODIFIER:
case LY_STMT_ORDERED_BY:
case LY_STMT_PATH:
case LY_STMT_PATTERN:
case LY_STMT_POSITION:
case LY_STMT_PREFIX:
case LY_STMT_PRESENCE:
case LY_STMT_RANGE:
case LY_STMT_REQUIRE_INSTANCE:
case LY_STMT_STATUS:
case LY_STMT_VALUE:
case LY_STMT_YANG_VERSION:
case LY_STMT_YIN_ELEMENT:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_IMPORT:
case LY_STMT_INCLUDE:
case LY_STMT_BELONGS_TO:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_INPUT:
case LY_STMT_OUTPUT:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_MUST:
case LY_STMT_WHEN:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_NAMESPACE:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_REVISION:
case LY_STMT_REVISION_DATE:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
break;
case LY_STMT_UNIQUE:
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
break;
/* argument is mapped to yin element */
case LY_STMT_CONTACT:
case LY_STMT_DESCRIPTION:
case LY_STMT_ORGANIZATION:
case LY_STMT_REFERENCE:
case LY_STMT_ERROR_MESSAGE:
/* there shouldn't be any attribute, argument is supposed to be first subelement */
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
/* no content */
assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
if (ctx->xmlctx->ws_only) {
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
return LY_EVALID;
}
/* parse child element */
child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
if (((elem_type == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
((elem_type != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
ly_stmt2str(elem_type));
return LY_EVALID;
}
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
/* no attributes expected? TODO */
while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
/* load and save content */
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
LY_CHECK_RET(!*arg, LY_EMEM);
/* load closing tag of subelement */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
/* if only subelement was parsed as argument, load also closing tag TODO what? */
/*if (ctx->xmlctx->status == LYXML_ELEMENT) {
LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
}*/
break;
default:
LOGINT(ctx->xmlctx->ctx);
return LY_EINT;
}
return LY_SUCCESS;
}
/**
* @brief Parse yin element into generic structure.
*
* @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
* @param[in] parent Identification of parent element.
* @param[out] element Where the element structure should be stored.
*
* @return LY_ERR values.
*/
LY_ERR
yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
{
LY_ERR ret = LY_SUCCESS;
struct lysp_stmt *last = NULL, *new = NULL;
assert(ctx->xmlctx->status == LYXML_ELEMENT);
/* allocate new structure for element */
*element = calloc(1, sizeof(**element));
LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
(*element)->stmt = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len);
LY_CHECK_ERR_GOTO(!(*element)->stmt, ret = LY_EMEM, cleanup);
(*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
ctx->xmlctx->prefix_len, parent);
last = (*element)->child;
if ((*element)->kw == LY_STMT_NONE) {
/* unrecognized element */
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
ly_stmt2str(parent));
ret = LY_EVALID;
goto cleanup;
} else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
/* element is known yang keyword, which means argument can be parsed correctly. */
ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
LY_CHECK_GOTO(ret, cleanup);
} else {
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
/* load attributes in generic way, save all attributes in linked list */
while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
new = calloc(1, sizeof(*last));
LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
if (!(*element)->child) {
/* save first */
(*element)->child = new;
} else {
last->next = new;
}
last = new;
last->flags |= LYS_YIN_ATTR;
LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
last->kw = LY_STMT_NONE;
/* attributes with prefix are ignored */
if (!ctx->xmlctx->prefix) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
} else {
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
}
if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
while (ctx->xmlctx->status == LYXML_ELEMENT) {
/* parse subelements */
ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
LY_CHECK_GOTO(ret, cleanup);
if (!(*element)->child) {
/* save first */
(*element)->child = new;
} else {
last->next = new;
}
last = new;
assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
} else {
/* save element content */
if (ctx->xmlctx->value_len) {
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
}
/* read closing tag */
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
cleanup:
return ret;
}
/**
* @brief Parse instance of extension.
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
* @param[in] subelem Type of the keyword this extension instance is a subelement of.
* @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
* @param[in,out] exts Extension instance to add to.
*
* @return LY_ERR values.
*/
LY_ERR
yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
struct lysp_ext_instance **exts)
{
struct lysp_ext_instance *e;
struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
assert(ctx->xmlctx->status == LYXML_ELEMENT);
assert(exts);
LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
e->yin = 0;
/* store name and insubstmt info */
e->name = name2nsname(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len);
LY_CHECK_RET(!e->name, LY_EMEM);
e->insubstmt = subelem;
e->insubstmt_index = subelem_index;
e->yin |= LYS_YIN;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
/* store attributes as subelements */
while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
if (!ctx->xmlctx->prefix) {
new_subelem = calloc(1, sizeof(*new_subelem));
if (!e->child) {
e->child = new_subelem;
} else {
last_subelem->next = new_subelem;
}
last_subelem = new_subelem;
last_subelem->flags |= LYS_YIN_ATTR;
LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last_subelem->arg);
LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
} else {
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
/* parse subelements */
assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
if (ctx->xmlctx->ws_only) {
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
while (ctx->xmlctx->status == LYXML_ELEMENT) {
LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
if (!e->child) {
e->child = new_subelem;
} else {
last_subelem->next = new_subelem;
}
last_subelem = new_subelem;
assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
} else if (ctx->xmlctx->value_len) {
/* save text content */
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
LY_CHECK_RET(!e->argument, LY_EMEM);
/* parser next */
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
}
return LY_SUCCESS;
}
/**
* @brief Generic function for content parsing
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
* @param[in] subelem_info array of valid subelement types and meta information
* @param[in] subelem_info_size Size of subelem_info array.
* @param[in] current_element Type of current element.
* @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
* @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
*
* @return LY_ERR values.
*/
LY_ERR
yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
{
LY_ERR ret = LY_SUCCESS;
enum LYXML_PARSER_STATUS next_status;
enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
struct yin_subelement *subelem = NULL;
assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
if (ctx->xmlctx->ws_only) {
/* check whether there are any children */
LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
} else {
/* we want to parse the value */
next_status = LYXML_ELEM_CLOSE;
}
if (next_status == LYXML_ELEMENT) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
/* current element has subelements as content */
while (ctx->xmlctx->status == LYXML_ELEMENT) {
/* match keyword */
last_kw = kw;
kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
ctx->xmlctx->prefix_len, current_element);
/* check if this element can be child of current element */
subelem = get_record(kw, subelem_info_size, subelem_info);
if (!subelem) {
if ((current_element == LY_STMT_DEVIATE) && isdevsub(kw)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
} else {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
ctx->xmlctx->name, ly_stmt2str(current_element));
}
ret = LY_EVALID;
goto cleanup;
}
/* relative order is required only in module and submodule sub-elements */
if ((current_element == LY_STMT_MODULE) || (current_element == LY_STMT_SUBMODULE)) {
ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
LY_CHECK_GOTO(ret, cleanup);
}
/* flag check */
if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
/* subelement uniquenes */
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
return LY_EVALID;
}
if (subelem->flags & YIN_SUBELEM_FIRST) {
/* subelement is supposed to be defined as first subelement */
ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
LY_CHECK_GOTO(ret, cleanup);
}
if (subelem->flags & YIN_SUBELEM_VER2) {
/* subelement is supported only in version 1.1 or higher */
if (ctx->parsed_mod->version < LYS_VERSION_1_1) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
ret = LY_EVALID;
goto cleanup;
}
}
/* note that element was parsed for easy uniqueness check in next iterations */
subelem->flags |= YIN_SUBELEM_PARSED;
switch (kw) {
/* call responsible function */
case LY_STMT_EXTENSION_INSTANCE:
ret = yin_parse_extension_instance(ctx, kw2lyext_substmt(current_element),
(subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
break;
case LY_STMT_ACTION:
case LY_STMT_RPC:
ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_ANYDATA:
case LY_STMT_ANYXML:
ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_ARGUMENT:
ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
break;
case LY_STMT_AUGMENT:
ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_BASE:
ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
break;
case LY_STMT_BELONGS_TO:
ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
break;
case LY_STMT_BIT:
ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_CASE:
ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_CHOICE:
ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_CONFIG:
ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
break;
case LY_STMT_CONTACT:
case LY_STMT_DESCRIPTION:
case LY_STMT_ORGANIZATION:
case LY_STMT_REFERENCE:
ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
break;
case LY_STMT_CONTAINER:
ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_DEFAULT:
ret = yin_parse_qname(ctx, kw, subelem, exts);
break;
case LY_STMT_ERROR_APP_TAG:
case LY_STMT_KEY:
case LY_STMT_PRESENCE:
ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
break;
case LY_STMT_DEVIATE:
ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
break;
case LY_STMT_DEVIATION:
ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
break;
case LY_STMT_ENUM:
ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_ERROR_MESSAGE:
ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
break;
case LY_STMT_EXTENSION:
ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
break;
case LY_STMT_FEATURE:
ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
break;
case LY_STMT_FRACTION_DIGITS:
ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_GROUPING:
ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_IDENTITY:
ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
break;
case LY_STMT_IF_FEATURE:
ret = yin_parse_qname(ctx, kw, subelem, exts);
break;
case LY_STMT_UNITS:
ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
break;
case LY_STMT_IMPORT:
ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
break;
case LY_STMT_INCLUDE:
ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
break;
case LY_STMT_INPUT:
case LY_STMT_OUTPUT:
ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
break;
case LY_STMT_LEAF:
ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_LEAF_LIST:
ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_LENGTH:
ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_LIST:
ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_MANDATORY:
ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
break;
case LY_STMT_MAX_ELEMENTS:
case LY_STMT_MIN_ELEMENTS:
ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
break;
case LY_STMT_MODIFIER:
ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
break;
case LY_STMT_MUST:
ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
break;
case LY_STMT_NAMESPACE:
ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
break;
case LY_STMT_NOTIFICATION:
ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_ORDERED_BY:
ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
break;
case LY_STMT_PATH:
ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_PATTERN:
ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_VALUE:
case LY_STMT_POSITION:
ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
break;
case LY_STMT_PREFIX:
ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
break;
case LY_STMT_RANGE:
ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_REFINE:
ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
break;
case LY_STMT_REQUIRE_INSTANCE:
ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
break;
case LY_STMT_REVISION:
ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
break;
case LY_STMT_REVISION_DATE:
ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
break;
case LY_STMT_STATUS:
ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
break;
case LY_STMT_TYPE:
ret = yin_parse_type(ctx, current_element, subelem);
break;
case LY_STMT_TYPEDEF:
ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_UNIQUE:
ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
break;
case LY_STMT_USES:
ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
break;
case LY_STMT_WHEN:
ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
break;
case LY_STMT_YANG_VERSION:
ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
break;
case LY_STMT_YIN_ELEMENT:
ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
break;
case LY_STMT_ARG_TEXT:
case LY_STMT_ARG_VALUE:
/* TODO what to do with content/attributes? */
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
break;
default:
LOGINT(ctx->xmlctx->ctx);
ret = LY_EINT;
}
LY_CHECK_GOTO(ret, cleanup);
subelem = NULL;
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
} else {
LY_CHECK_RET(ret);
/* elements with text or none content */
/* save text content, if text_content isn't set, it's just ignored */
/* no resources are allocated in this branch, no need to use cleanup label */
LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
if (text_content) {
INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *text_content);
LY_CHECK_RET(!*text_content, LY_EMEM);
}
LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
}
/* mandatory subelemnts are checked only after whole element was succesfully parsed */
LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
cleanup:
return ret;
}
/**
* @brief Parse module element.
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
* @param[out] mod Parsed module structure.
*
* @return LY_ERR values.
*/
LY_ERR
yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
{
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
struct lysp_submodule *dup;
size_t subelems_size;
mod->is_submod = 0;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE));
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &mod->data, 0,
LY_STMT_AUGMENT, &mod->augments, 0,
LY_STMT_CHOICE, &mod->data, 0,
LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
LY_STMT_CONTAINER, &mod->data, 0,
LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_DEVIATION, &mod->deviations, 0,
LY_STMT_EXTENSION, &mod->extensions, 0,
LY_STMT_FEATURE, &mod->features, 0,
LY_STMT_GROUPING, &mod->groupings, 0,
LY_STMT_IDENTITY, &mod->identities, 0,
LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
LY_STMT_LEAF, &mod->data, 0,
LY_STMT_LEAF_LIST, &mod->data, 0,
LY_STMT_LIST, &mod->data, 0,
LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
LY_STMT_NOTIFICATION, &mod->notifs, 0,
LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_REVISION, &mod->revs, 0,
LY_STMT_RPC, &mod->rpcs, 0,
LY_STMT_TYPEDEF, &mod->typedefs, 0,
LY_STMT_USES, &mod->data, 0,
LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_MODULE, NULL, &mod->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
/* finalize parent pointers to the reallocated items */
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, mod->groupings, mod->augments, mod->rpcs, mod->notifs));
/* submodules share the namespace with the module names, so there must not be
* a submodule of the same name in the context, no need for revision matching */
dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, mod->mod->name, NULL);
if (dup) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
return LY_EVALID;
}
return LY_SUCCESS;
}
/**
* @brief Parse submodule element.
*
* @param[in,out] ctx Yin parser context for logging and to store current state.
* @param[in] mod_attrs Attributes of submodule element.
* @param[out] submod Parsed submodule structure.
*
* @return LY_ERR values.
*/
LY_ERR
yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
{
LY_ERR ret = LY_SUCCESS;
struct yin_subelement *subelems = NULL;
struct lysp_submodule *dup;
size_t subelems_size;
submod->is_submod = 1;
LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE));
LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
LY_STMT_ANYXML, &submod->data, 0,
LY_STMT_AUGMENT, &submod->augments, 0,
LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
LY_STMT_CHOICE, &submod->data, 0,
LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
LY_STMT_CONTAINER, &submod->data, 0,
LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
LY_STMT_DEVIATION, &submod->deviations, 0,
LY_STMT_EXTENSION, &submod->extensions, 0,
LY_STMT_FEATURE, &submod->features, 0,
LY_STMT_GROUPING, &submod->groupings, 0,
LY_STMT_IDENTITY, &submod->identities, 0,
LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
LY_STMT_LEAF, &submod->data, 0,
LY_STMT_LEAF_LIST, &submod->data, 0,
LY_STMT_LIST, &submod->data, 0,
LY_STMT_NOTIFICATION, &submod->notifs, 0,
LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
LY_STMT_REVISION, &submod->revs, 0,
LY_STMT_RPC, &submod->rpcs, 0,
LY_STMT_TYPEDEF, &submod->typedefs, 0,
LY_STMT_USES, &submod->data, 0,
LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
LY_STMT_EXTENSION_INSTANCE, NULL, 0));
ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_SUBMODULE, NULL, &submod->exts);
subelems_deallocator(subelems_size, subelems);
LY_CHECK_RET(ret);
/* finalize parent pointers to the reallocated items */
LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, submod->groupings, submod->augments, submod->rpcs, submod->notifs));
/* submodules share the namespace with the module names, so there must not be
* a submodule of the same name in the context, no need for revision matching */
dup = ly_ctx_get_submodule(ctx->xmlctx->ctx, NULL, submod->name, NULL);
if (dup && strcmp(dup->mod->name, submod->mod->name)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
return LY_EVALID;
}
return LY_SUCCESS;
}
LY_ERR
yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
struct ly_in *in, struct lysp_submodule **submod)
{
enum ly_stmt kw = LY_STMT_NONE;
LY_ERR ret = LY_SUCCESS;
struct lysp_submodule *mod_p = NULL;
/* create context */
*yin_ctx = calloc(1, sizeof **yin_ctx);
LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
(*yin_ctx)->format = LYS_IN_YIN;
(*yin_ctx)->unres = main_ctx->unres;
LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
mod_p = calloc(1, sizeof *mod_p);
LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
mod_p->mod = main_ctx->parsed_mod->mod;
mod_p->parsing = 1;
(*yin_ctx)->parsed_mod = (struct lysp_module *)mod_p;
/* map the typedefs and groupings list from main context to the submodule's context */
memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
/* check submodule */
kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
(*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
if (kw == LY_STMT_MODULE) {
LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
ret = LY_EINVAL;
goto cleanup;
} else if (kw != LY_STMT_SUBMODULE) {
LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
ret = LY_EVALID;
goto cleanup;
}
ret = yin_parse_submod(*yin_ctx, mod_p);
LY_CHECK_GOTO(ret, cleanup);
/* skip possible trailing whitespaces at end of the input */
while (isspace(in->current[0])) {
ly_in_skip(in, 1);
}
if (in->current[0]) {
LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
strlen(in->current) > 15 ? "..." : "");
ret = LY_EVALID;
goto cleanup;
}
mod_p->parsing = 0;
*submod = mod_p;
cleanup:
if (ret) {
lysp_module_free((struct lysp_module *)mod_p);
yin_parser_ctx_free(*yin_ctx);
*yin_ctx = NULL;
}
return ret;
}
LY_ERR
yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres)
{
LY_ERR ret = LY_SUCCESS;
enum ly_stmt kw = LY_STMT_NONE;
struct lysp_module *mod_p = NULL;
/* create context */
*yin_ctx = calloc(1, sizeof **yin_ctx);
LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
(*yin_ctx)->format = LYS_IN_YIN;
(*yin_ctx)->unres = unres;
LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
mod_p = calloc(1, sizeof *mod_p);
LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
mod_p->mod = mod;
mod_p->parsing = 1;
(*yin_ctx)->parsed_mod = mod_p;
/* check module */
kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
(*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
if (kw == LY_STMT_SUBMODULE) {
LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
ret = LY_EINVAL;
goto cleanup;
} else if (kw != LY_STMT_MODULE) {
LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
ret = LY_EVALID;
goto cleanup;
}
/* parse module substatements */
ret = yin_parse_mod(*yin_ctx, mod_p);
LY_CHECK_GOTO(ret, cleanup);
/* skip possible trailing whitespaces at end of the input */
while (isspace(in->current[0])) {
ly_in_skip(in, 1);
}
if (in->current[0]) {
LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
strlen(in->current) > 15 ? "..." : "");
ret = LY_EVALID;
goto cleanup;
}
mod_p->parsing = 0;
mod->parsed = mod_p;
cleanup:
if (ret != LY_SUCCESS) {
lysp_module_free(mod_p);
yin_parser_ctx_free(*yin_ctx);
*yin_ctx = NULL;
}
return ret;
}