blob: da1061da9599aaa17b9c45897ed652470f492275 [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
Michal Vaskofc2cd072021-02-24 13:17:17 +010014#define _GNU_SOURCE
15
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include <ctype.h>
18#include <errno.h>
19#include <stdarg.h>
20#include <stdbool.h>
Radek Krejci47fab892020-11-05 17:02:41 +010021#include <stdint.h>
David Sedlák3b4db242018-10-19 16:11:01 +020022#include <stdio.h>
23#include <stdlib.h>
David Sedlák872c7b42018-10-26 13:15:20 +020024#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020025
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "common.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010027#include "compat.h"
David Sedlákf824ad52018-10-14 23:58:15 +020028#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020031#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "log.h"
Michal Vasko63f3d842020-07-08 10:10:14 +020033#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020034#include "parser_schema.h"
Michal Vasko69730152020-10-09 16:30:07 +020035#include "path.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "set.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020037#include "tree.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010038#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020040#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020041#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "xml.h"
David Sedlák00250342019-06-21 14:19:39 +020043
Radek Krejci77114102021-03-10 15:21:57 +010044struct lys_glob_unres;
45
David Sedlák2b214ac2019-06-06 16:11:03 +020046/**
47 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020048 *
David Sedlák2b214ac2019-06-06 16:11:03 +020049 * @param ns Namespace URI to check.
50 *
51 * @return true if ns equals YIN_NS_URI false otherwise.
52 */
53#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
54
Michal Vaskoafac7822020-10-20 14:22:26 +020055enum yin_argument {
56 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
57 YIN_ARG_NAME, /**< argument name */
58 YIN_ARG_TARGET_NODE, /**< argument target-node */
59 YIN_ARG_MODULE, /**< argument module */
60 YIN_ARG_VALUE, /**< argument value */
61 YIN_ARG_TEXT, /**< argument text */
62 YIN_ARG_CONDITION, /**< argument condition */
63 YIN_ARG_URI, /**< argument uri */
64 YIN_ARG_DATE, /**< argument data */
65 YIN_ARG_TAG, /**< argument tag */
66 YIN_ARG_NONE /**< empty (special value) */
67};
68
Michal Vasko22df3f02020-08-24 13:29:22 +020069const char * const yin_attr_list[] = {
David Sedlákf6251182019-06-06 10:22:13 +020070 [YIN_ARG_NAME] = "name",
71 [YIN_ARG_TARGET_NODE] = "target-node",
72 [YIN_ARG_MODULE] = "module",
73 [YIN_ARG_VALUE] = "value",
74 [YIN_ARG_TEXT] = "text",
75 [YIN_ARG_CONDITION] = "condition",
76 [YIN_ARG_URI] = "uri",
77 [YIN_ARG_DATE] = "date",
78 [YIN_ARG_TAG] = "tag",
David Sedlák071f7662019-09-12 02:02:51 +020079 [YIN_ARG_NONE] = "none",
David Sedlákf6251182019-06-06 10:22:13 +020080};
81
Michal Vaskoafac7822020-10-20 14:22:26 +020082#define yin_attr2str(STMT) yin_attr_list[STMT]
83
84#define VALID_VALS1 " Only valid value is \"%s\"."
85#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
86#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
87#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
88
89/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
90#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
91 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
92 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
93 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
94
95/* flags to set constraints of subelements */
96#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
97#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
98#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
99#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
100
101#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
102 time to simply check validity of given constraints */
103
104struct yin_subelement {
105 enum ly_stmt type; /**< type of keyword */
106 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
107 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 */
108};
109
110/* Meta information passed to yin_parse_argument function,
111 holds information about where content of argument element will be stored. */
112struct yin_argument_meta {
113 uint16_t *flags; /**< Argument flags */
114 const char **argument; /**< Argument value */
115};
116
117/**
118 * @brief Meta information passed to functions working with tree_schema,
119 * that require additional information about parent node.
120 */
121struct tree_node_meta {
122 struct lysp_node *parent; /**< parent node */
123 struct lysp_node **nodes; /**< linked list of siblings */
124};
125
126/**
127 * @brief Meta information passed to yin_parse_import function.
128 */
129struct import_meta {
130 const char *prefix; /**< module prefix. */
131 struct lysp_import **imports; /**< imports to add to. */
132};
133
134/**
135 * @brief Meta information passed to yin_parse_include function.
136 */
137struct include_meta {
138 const char *name; /**< Module/submodule name. */
139 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
140};
141
142/**
143 * @brief Meta information passed to yin_parse_inout function.
144 */
145struct inout_meta {
146 struct lysp_node *parent; /**< Parent node. */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100147 struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
Michal Vaskoafac7822020-10-20 14:22:26 +0200148};
149
150/**
151 * @brief Meta information passed to yin_parse_minmax function.
152 */
153struct minmax_dev_meta {
154 uint32_t *lim; /**< min/max value to write to. */
155 uint16_t *flags; /**< min/max flags to write to. */
156 struct lysp_ext_instance **exts; /**< extension instances to add to. */
157};
158
159LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
160 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
161
162/**
163 * @brief Match yang keyword from yin data.
164 *
165 * @param[in,out] ctx Yin parser context for logging and to store current state.
166 * @param[in] name Start of keyword name
167 * @param[in] name_len Lenght of keyword name.
168 * @param[in] prefix Start of keyword prefix.
169 * @param[in] prefix_len Lenght of prefix.
170 * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
171 *
172 * @return yang_keyword values.
173 */
Radek Krejcid6b76452019-09-03 17:03:03 +0200174enum ly_stmt
Michal Vaskob36053d2020-03-26 15:49:30 +0100175yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200176 const char *prefix, size_t prefix_len, enum ly_stmt parent)
David Sedlák1bccdfa2019-06-17 15:55:27 +0200177{
David Sedlák8f7a1172019-06-20 14:42:18 +0200178 const char *start = NULL;
Radek Krejcid6b76452019-09-03 17:03:03 +0200179 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200180 const struct lyxml_ns *ns = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200181 struct ly_in *in;
David Sedlák8f7a1172019-06-20 14:42:18 +0200182
Michal Vasko69730152020-10-09 16:30:07 +0200183 if (!name || (name_len == 0)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200184 return LY_STMT_NONE;
David Sedlák1bccdfa2019-06-17 15:55:27 +0200185 }
186
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200187 ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200188 if (ns) {
189 if (!IS_YIN_NS(ns->uri)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200190 return LY_STMT_EXTENSION_INSTANCE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200191 }
192 } else {
193 /* elements without namespace are automatically unknown */
Radek Krejcid6b76452019-09-03 17:03:03 +0200194 return LY_STMT_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200195 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200196
Michal Vasko63f3d842020-07-08 10:10:14 +0200197 LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
198 start = in->current;
Radek Krejcid54412f2020-12-17 20:25:35 +0100199 kw = lysp_match_kw(in, NULL);
Michal Vasko63f3d842020-07-08 10:10:14 +0200200 name = in->current;
201 ly_in_free(in, 0);
David Sedlák8f7a1172019-06-20 14:42:18 +0200202
203 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +0200204 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
Michal Vasko69730152020-10-09 16:30:07 +0200205 if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200206 return LY_STMT_ARG_VALUE;
David Sedlákc1771b12019-07-10 15:55:46 +0200207 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200208 return kw;
209 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +0200210 if (strncmp(start, "text", name_len) == 0) {
Radek Krejcid6b76452019-09-03 17:03:03 +0200211 return LY_STMT_ARG_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200212 } else {
Radek Krejcid6b76452019-09-03 17:03:03 +0200213 return LY_STMT_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200214 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200215 }
216}
217
Michal Vaskoafac7822020-10-20 14:22:26 +0200218/**
219 * @brief Match argument name.
220 *
221 * @param[in] name String representing name.
222 * @param[in] len Lenght of the name.
223 *
224 * @return yin_argument values.
225 */
David Sedlákc5b20842019-08-13 10:18:31 +0200226enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +0200227yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200228{
David Sedlákc5b20842019-08-13 10:18:31 +0200229 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200230 size_t already_read = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200231
David Sedlák7ff55a92019-06-17 11:11:41 +0200232 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200233
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200234#define READ_INC(LEN) already_read += LEN
235#define ARG_SET(STMT) arg=STMT
236#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
David Sedlákc10e7902018-12-17 02:17:59 +0100237
David Sedlák1c8b2702019-02-22 11:03:02 +0100238 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100239 case 'c':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200240 READ_INC(1);
241 if (ARG_CHECK("ondition", 8)) {
242 ARG_SET(YIN_ARG_CONDITION);
243 }
David Sedlák3b4db242018-10-19 16:11:01 +0200244 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200245
David Sedlák94de2aa2019-02-15 12:42:11 +0100246 case 'd':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200247 READ_INC(1);
248 if (ARG_CHECK("ate", 3)) {
249 ARG_SET(YIN_ARG_DATE);
250 }
David Sedlák3b4db242018-10-19 16:11:01 +0200251 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200252
David Sedlák94de2aa2019-02-15 12:42:11 +0100253 case 'm':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200254 READ_INC(1);
255 if (ARG_CHECK("odule", 5)) {
256 ARG_SET(YIN_ARG_MODULE);
257 }
David Sedlák872c7b42018-10-26 13:15:20 +0200258 break;
259
David Sedlák94de2aa2019-02-15 12:42:11 +0100260 case 'n':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200261 READ_INC(1);
262 if (ARG_CHECK("ame", 3)) {
263 ARG_SET(YIN_ARG_NAME);
264 }
David Sedlák872c7b42018-10-26 13:15:20 +0200265 break;
266
David Sedlák94de2aa2019-02-15 12:42:11 +0100267 case 't':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200268 READ_INC(1);
269 if (ARG_CHECK("a", 1)) {
270 if (ARG_CHECK("g", 1)) {
271 ARG_SET(YIN_ARG_TAG);
272 } else if (ARG_CHECK("rget-node", 9)) {
273 ARG_SET(YIN_ARG_TARGET_NODE);
274 }
275 } else if (ARG_CHECK("ext", 3)) {
276 ARG_SET(YIN_ARG_TEXT);
277 }
David Sedlák3b4db242018-10-19 16:11:01 +0200278 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200279
David Sedlák94de2aa2019-02-15 12:42:11 +0100280 case 'u':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200281 READ_INC(1);
282 if (ARG_CHECK("ri", 2)) {
283 ARG_SET(YIN_ARG_URI);
284 }
David Sedlák3b4db242018-10-19 16:11:01 +0200285 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200286
David Sedlák94de2aa2019-02-15 12:42:11 +0100287 case 'v':
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200288 READ_INC(1);
289 if (ARG_CHECK("alue", 4)) {
290 ARG_SET(YIN_ARG_VALUE);
291 }
David Sedlák3b4db242018-10-19 16:11:01 +0200292 break;
293 }
294
David Sedlákc10e7902018-12-17 02:17:59 +0100295 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200296 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200297 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200298 }
299
Radek Krejci8bb8d9d2020-08-21 14:53:27 +0200300#undef ARG_CHECK
301#undef ARG_SET
302#undef READ_INC
David Sedlák18730132019-03-15 15:51:34 +0100303
David Sedlák872c7b42018-10-26 13:15:20 +0200304 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200305}
306
Radek Krejcid6b76452019-09-03 17:03:03 +0200307#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
308 kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
309 kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
310 kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
David Sedlák81497a32019-08-13 16:56:26 +0200311
Radek Krejcid6b76452019-09-03 17:03:03 +0200312#define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT)
David Sedlák81497a32019-08-13 16:56:26 +0200313
David Sedlák26ea1432019-08-14 13:42:23 +0200314/**
315 * @brief Free subelems information allocated on heap.
316 *
317 * @param[in] count Size of subelems array.
318 * @param[in] subelems Subelems array to free.
319 */
David Sedlák81497a32019-08-13 16:56:26 +0200320static void
321subelems_deallocator(size_t count, struct yin_subelement *subelems)
322{
Michal Vaskod989ba02020-08-24 10:59:24 +0200323 for (size_t i = 0; i < count; ++i) {
David Sedlák81497a32019-08-13 16:56:26 +0200324 if (HAS_META(subelems[i].type)) {
325 free(subelems[i].dest);
326 }
327 }
328
329 free(subelems);
330}
331
David Sedlák26ea1432019-08-14 13:42:23 +0200332/**
333 * @brief Allocate subelems information on heap.
334 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200335 * @param[in] ctx YIN parser context, used for logging.
David Sedlák26ea1432019-08-14 13:42:23 +0200336 * @param[in] count Number of subelements.
337 * @param[in] parent Parent node if any.
338 * @param[out] result Allocated subelems array.
339 *
340 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
341 */
David Sedlák81497a32019-08-13 16:56:26 +0200342static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100343subelems_allocator(struct lys_yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
Radek Krejci0f969882020-08-21 16:56:47 +0200344 struct yin_subelement **result, ...)
David Sedlák81497a32019-08-13 16:56:26 +0200345{
346 va_list ap;
347
348 *result = calloc(count, sizeof **result);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200349 LY_CHECK_GOTO(!(*result), mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200350
351 va_start(ap, result);
352 for (size_t i = 0; i < count; ++i) {
353 /* TYPE */
Radek Krejcid6b76452019-09-03 17:03:03 +0200354 (*result)[i].type = va_arg(ap, enum ly_stmt);
David Sedlák81497a32019-08-13 16:56:26 +0200355 /* DEST */
356 if (IS_NODE_ELEM((*result)[i].type)) {
357 struct tree_node_meta *node_meta = NULL;
358 node_meta = calloc(1, sizeof *node_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200359 LY_CHECK_GOTO(!node_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200360 node_meta->parent = parent;
David Sedlákbf8a2b72019-08-14 16:48:10 +0200361 node_meta->nodes = va_arg(ap, void *);
David Sedlák81497a32019-08-13 16:56:26 +0200362 (*result)[i].dest = node_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200363 } else if ((*result)[i].type == LY_STMT_IMPORT) {
David Sedlák81497a32019-08-13 16:56:26 +0200364 struct import_meta *imp_meta = NULL;
365 imp_meta = calloc(1, sizeof *imp_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200366 LY_CHECK_GOTO(!imp_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200367 imp_meta->prefix = va_arg(ap, const char *);
368 imp_meta->imports = va_arg(ap, struct lysp_import **);
369 (*result)[i].dest = imp_meta;
Radek Krejcid6b76452019-09-03 17:03:03 +0200370 } else if ((*result)[i].type == LY_STMT_INCLUDE) {
David Sedlák81497a32019-08-13 16:56:26 +0200371 struct include_meta *inc_meta = NULL;
372 inc_meta = calloc(1, sizeof *inc_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200373 LY_CHECK_GOTO(!inc_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200374 inc_meta->name = va_arg(ap, const char *);
375 inc_meta->includes = va_arg(ap, struct lysp_include **);
376 (*result)[i].dest = inc_meta;
Michal Vasko69730152020-10-09 16:30:07 +0200377 } else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
David Sedlák81497a32019-08-13 16:56:26 +0200378 struct inout_meta *inout_meta = NULL;
379 inout_meta = calloc(1, sizeof *inout_meta);
David Sedlákbf8a2b72019-08-14 16:48:10 +0200380 LY_CHECK_GOTO(!inout_meta, mem_err);
David Sedlák81497a32019-08-13 16:56:26 +0200381 inout_meta->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100382 inout_meta->inout_p = va_arg(ap, struct lysp_node_action_inout *);
David Sedlák81497a32019-08-13 16:56:26 +0200383 (*result)[i].dest = inout_meta;
384 } else {
385 (*result)[i].dest = va_arg(ap, void *);
386 }
387 /* FLAGS */
388 (*result)[i].flags = va_arg(ap, int);
389 }
390 va_end(ap);
391
392 return LY_SUCCESS;
393
David Sedlákbf8a2b72019-08-14 16:48:10 +0200394mem_err:
David Sedlák81497a32019-08-13 16:56:26 +0200395 subelems_deallocator(count, *result);
Michal Vaskob36053d2020-03-26 15:49:30 +0100396 LOGMEM(ctx->xmlctx->ctx);
David Sedlák81497a32019-08-13 16:56:26 +0200397 return LY_EMEM;
398}
399
Michal Vaskoafac7822020-10-20 14:22:26 +0200400/**
401 * @brief Check that val is valid UTF8 character sequence of val_type.
402 * Doesn't check empty string, only character validity.
403 *
404 * @param[in] ctx Yin parser context for logging.
405 * @param[in] val_type Type of the input string to select method of checking character validity.
406 *
407 * @return LY_ERR values.
408 */
David Sedlák8f7a1172019-06-20 14:42:18 +0200409LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100410yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type)
David Sedlák4a650532019-07-10 11:55:18 +0200411{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200412 uint8_t prefix = 0;
413 uint32_t c;
Michal Vaskob36053d2020-03-26 15:49:30 +0100414 size_t utf8_char_len, already_read = 0;
415 const char *val;
416
417 assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
418
419 val = ctx->xmlctx->value;
420 while (already_read < ctx->xmlctx->value_len) {
David Sedlák4a650532019-07-10 11:55:18 +0200421 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
Michal Vasko69730152020-10-09 16:30:07 +0200422 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200423 already_read += utf8_char_len;
Michal Vaskob36053d2020-03-26 15:49:30 +0100424 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200425
426 switch (val_type) {
427 case Y_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200428 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, NULL));
David Sedlák4a650532019-07-10 11:55:18 +0200429 break;
430 case Y_PREF_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200431 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, already_read ? 0 : 1, &prefix));
David Sedlák4a650532019-07-10 11:55:18 +0200432 break;
433 case Y_STR_ARG:
434 case Y_MAYBE_STR_ARG:
435 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
436 break;
437 }
438 }
439
440 return LY_SUCCESS;
441}
442
David Sedlákb4e44562019-07-04 15:42:12 +0200443/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100444 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200445 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200446 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200447 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
448 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200449 * @param[out] arg_val Where value of argument should be stored. Can be NULL iff arg_type is specified as YIN_ARG_NONE.
David Sedlák292763b2019-07-09 11:10:53 +0200450 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200451 * @param[in] current_element Identification of current element, used for logging.
452 *
453 * @return LY_ERR values.
454 */
455static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100456yin_parse_attribute(struct lys_yin_parser_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200457 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200458{
David Sedlákc5b20842019-08-13 10:18:31 +0200459 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200460 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200461
David Sedlák1bccdfa2019-06-17 15:55:27 +0200462 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100463 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200464 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100465 if (!ctx->xmlctx->prefix) {
466 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200467 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100468 /* skip it */
469 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200470 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200471 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
Michal Vasko69730152020-10-09 16:30:07 +0200472 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200473 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100474
475 /* go to value */
476 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
477 LY_CHECK_RET(yin_validate_value(ctx, val_type));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200478 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg_val);
David Sedlák169cc522019-08-15 13:23:45 +0200479 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200480 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100481 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +0200482 ctx->xmlctx->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200483 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200484 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100485 } else {
486 /* skip it */
487 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200488 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100489
490 /* next attribute */
491 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200492 }
493
David Sedlák292763b2019-07-09 11:10:53 +0200494 /* anything else than Y_MAYBE_STR_ARG is mandatory */
Michal Vasko69730152020-10-09 16:30:07 +0200495 if ((val_type != Y_MAYBE_STR_ARG) && !found) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100496 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
Michal Vasko69730152020-10-09 16:30:07 +0200497 yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200498 return LY_EVALID;
499 }
500
501 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200502}
503
David Sedlákd6e56892019-07-01 15:40:24 +0200504/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200505 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200506 *
507 * @param[in] type Type of wanted record.
508 * @param[in] array_size Size of array.
509 * @param[in] array Searched array.
510 *
511 * @return Pointer to desired record on success, NULL if element is not in the array.
512 */
David Sedlákb4e44562019-07-04 15:42:12 +0200513static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200514get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200515{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200516 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200517 if (array[u].type == type) {
518 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200519 }
520 }
David Sedlákd6e56892019-07-01 15:40:24 +0200521 return NULL;
522}
523
David Sedlákbba38e52019-07-09 15:20:01 +0200524/**
525 * @brief Helper function to check mandatory constraint of subelement.
526 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200527 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200528 * @param[in] subelem_info Array of information about subelements.
529 * @param[in] subelem_info_size Size of subelem_info array.
530 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
531 *
532 * @return LY_ERR values.
533 */
534static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100535yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200536 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200537{
David Sedlákb0faad82019-07-04 14:28:59 +0200538 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200539 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200540 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200541 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200542 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200543 return LY_EVALID;
544 }
545 }
546
547 return LY_SUCCESS;
548}
549
David Sedlákbba38e52019-07-09 15:20:01 +0200550/**
551 * @brief Helper function to check "first" constraint of subelement.
552 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200553 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200554 * @param[in] subelem_info Array of information about subelements.
555 * @param[in] subelem_info_size Size of subelem_info array.
556 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200557 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement, used for logging.
David Sedlákbba38e52019-07-09 15:20:01 +0200558 *
559 * @return LY_ERR values.
560 */
561static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100562yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200563 signed char subelem_info_size, enum ly_stmt current_element,
564 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200565{
David Sedlákb0faad82019-07-04 14:28:59 +0200566 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200567 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200568 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200569 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200570 return LY_EVALID;
571 }
572 }
573
574 return LY_SUCCESS;
575}
576
David Sedlákbba38e52019-07-09 15:20:01 +0200577/**
David Sedlákb4e44562019-07-04 15:42:12 +0200578 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
579 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200580 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200581 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200582 * @param[in] kw Type of current element.
583 * @param[out] value Where value of attribute should be stored.
584 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200585 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200586 * @param[in,out] exts Extension instances to add to.
David Sedlákd6e56892019-07-01 15:40:24 +0200587 * @return LY_ERR values.
588 */
David Sedlákb4e44562019-07-04 15:42:12 +0200589static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100590yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
Radek Krejci0f969882020-08-21 16:56:47 +0200591 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200592{
Michal Vaskob36053d2020-03-26 15:49:30 +0100593 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
594 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100595 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200596 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
597 };
David Sedlákb4e44562019-07-04 15:42:12 +0200598
Radek Krejcif13b87b2020-12-01 22:02:17 +0100599 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200600}
601
602/**
David Sedlák6542aed2019-08-14 10:47:43 +0200603 * @brief Parse path element.
604 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200605 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200606 * @param[in] kw Type of current element.
607 * @param[out] type Type structure to store parsed value, flags and extension instances.
David Sedlák6542aed2019-08-14 10:47:43 +0200608 * @return LY_ERR values.
609 */
610static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100611yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200612{
Michal Vasko004d3152020-06-11 19:59:22 +0200613 LY_ERR ret;
614 const char *str_path;
615
616 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
Michal Vasko6b26e742020-07-17 15:02:10 +0200617 ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
Michal Vasko69730152020-10-09 16:30:07 +0200618 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +0200619 lydict_remove(ctx->xmlctx->ctx, str_path);
620 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200621 type->flags |= LYS_SET_PATH;
622
623 return LY_SUCCESS;
624}
625
626/**
David Sedlákd3983112019-07-12 11:20:56 +0200627 * @brief Parse pattern element.
628 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200629 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200630 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200631 * @return LY_ERR values.
632 */
633static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100634yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200635{
636 const char *real_value = NULL;
637 char *saved_value = NULL;
638 struct lysp_restr *restr;
639
Michal Vaskob36053d2020-03-26 15:49:30 +0100640 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
641 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
642 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN));
David Sedlákd3983112019-07-12 11:20:56 +0200643 size_t len = strlen(real_value);
644
645 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100646 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200647 memmove(saved_value + 1, real_value, len);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100648 lydict_remove(ctx->xmlctx->ctx, real_value);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100649 saved_value[0] = LYSP_RESTR_PATTERN_ACK;
David Sedlákd3983112019-07-12 11:20:56 +0200650 saved_value[len + 1] = '\0';
Michal Vasko7f45cf22020-10-01 12:49:44 +0200651 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200652 restr->arg.mod = ctx->parsed_mod;
David Sedlákd3983112019-07-12 11:20:56 +0200653 type->flags |= LYS_SET_PATTERN;
654
Radek Krejcif13b87b2020-12-01 22:02:17 +0100655 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200656 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
657 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
658 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
659 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
660 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
661 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
662 };
Michal Vasko69730152020-10-09 16:30:07 +0200663
Radek Krejcif13b87b2020-12-01 22:02:17 +0100664 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200665}
666
David Sedlákc5b20842019-08-13 10:18:31 +0200667/**
668 * @brief Parse fraction-digits element.
669 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200670 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200671 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200672 * @return LY_ERR values.
673 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200674static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100675yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200676{
677 const char *temp_val = NULL;
678 char *ptr;
679 unsigned long int num;
680
Michal Vaskob36053d2020-03-26 15:49:30 +0100681 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
682 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS));
David Sedlákf75d55e2019-07-12 16:52:50 +0200683
Michal Vasko69730152020-10-09 16:30:07 +0200684 if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200685 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100686 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200687 return LY_EVALID;
688 }
689
690 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100691 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlákf75d55e2019-07-12 16:52:50 +0200692 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200693 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100694 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200695 return LY_EVALID;
696 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100697 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +0200698 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100699 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200700 return LY_EVALID;
701 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100702 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200703 type->fraction_digits = num;
704 type->flags |= LYS_SET_FRDIGITS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100705 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200706 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
707 };
Michal Vasko69730152020-10-09 16:30:07 +0200708
Radek Krejcif13b87b2020-12-01 22:02:17 +0100709 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200710}
711
David Sedlák07869a52019-07-12 14:28:19 +0200712/**
David Sedlák43801c92019-08-05 15:58:54 +0200713 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200714 *
715 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200716 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200717 * @return LY_ERR values.
718 */
David Sedlákca36c422019-07-12 12:47:55 +0200719static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100720yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200721{
David Sedlák07869a52019-07-12 14:28:19 +0200722 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200723
Michal Vaskob36053d2020-03-26 15:49:30 +0100724 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200725 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100726 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
727 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
David Sedlák43801c92019-08-05 15:58:54 +0200728 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
Michal Vaskob36053d2020-03-26 15:49:30 +0100729 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák43801c92019-08-05 15:58:54 +0200730 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200731
Radek Krejcif13b87b2020-12-01 22:02:17 +0100732 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200733 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
734 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
735 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
736 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
737 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
738 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
739 };
Michal Vasko69730152020-10-09 16:30:07 +0200740
Radek Krejcif13b87b2020-12-01 22:02:17 +0100741 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200742}
743
744/**
745 * @brief Parse bit element.
746 *
747 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200748 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200749 * @return LY_ERR values.
750 */
751static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100752yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200753{
754 struct lysp_type_enum *en;
755
Michal Vaskob36053d2020-03-26 15:49:30 +0100756 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200757 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100758 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
759 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
David Sedlák43801c92019-08-05 15:58:54 +0200760 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
761
Radek Krejcif13b87b2020-12-01 22:02:17 +0100762 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200763 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
764 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
765 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
766 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
767 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
768 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
769 };
Michal Vasko69730152020-10-09 16:30:07 +0200770
Radek Krejcif13b87b2020-12-01 22:02:17 +0100771 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200772}
773
David Sedlákd3983112019-07-12 11:20:56 +0200774/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200775 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
776 * more instances, such as base or if-feature.
777 *
778 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5f8191e2019-07-08 16:35:52 +0200779 * @param[in] kw Type of current element.
780 * @param[out] values Parsed values to add to.
781 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200782 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200783 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200784 * @return LY_ERR values.
785 */
786static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100787yin_parse_simple_elements(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char ***values, enum yin_argument arg_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200788 enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200789{
790 const char **value;
Michal Vasko69730152020-10-09 16:30:07 +0200791
Michal Vaskob36053d2020-03-26 15:49:30 +0100792 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200793 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100794 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200795 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
796 };
David Sedlák968ac342019-07-11 15:17:59 +0200797
Michal Vaskob36053d2020-03-26 15:49:30 +0100798 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
799 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200800
Radek Krejcif13b87b2020-12-01 22:02:17 +0100801 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlák5f8191e2019-07-08 16:35:52 +0200802}
803
804/**
David Sedlák6542aed2019-08-14 10:47:43 +0200805 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
806 *
807 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200808 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200809 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
David Sedlák6542aed2019-08-14 10:47:43 +0200810 * @param[in] arg_type Expected type of attribute.
811 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200812 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200813 * @return LY_ERR values.
814 */
815static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100816yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200817 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200818{
819 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100820 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200821 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200822 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100823 LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200824 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200825 }
826
827 return LY_SUCCESS;
828}
829
830/**
831 * @brief Parse base element.
832 *
833 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200834 * @param[in] parent Identification of parent element.
835 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200836 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200837 * @return LY_ERR values.
838 */
839static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100840yin_parse_base(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, void *dest, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200841{
842 struct lysp_type *type = NULL;
843
Radek Krejcid6b76452019-09-03 17:03:03 +0200844 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200845 type = (struct lysp_type *)dest;
Michal Vaskob36053d2020-03-26 15:49:30 +0100846 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
Michal Vasko69730152020-10-09 16:30:07 +0200847 Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200848 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200849 } else if (parent == LY_STMT_IDENTITY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100850 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
Michal Vasko69730152020-10-09 16:30:07 +0200851 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200852 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100853 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200854 return LY_EINT;
855 }
856
857 return LY_SUCCESS;
858}
859
860/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200861 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200862 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200863 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100864 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200865 * @return LY_ERR values.
866 */
867static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100868yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200869{
870 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100871 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200872 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
873 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200874
875 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100876 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
877 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE));
David Sedlákcf5569a2019-07-11 13:31:34 +0200878 if (strcmp(temp_val, "true") == 0) {
879 type->require_instance = 1;
880 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200881 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200882 "require-instance", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100883 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200884 return LY_EVALID;
885 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100886 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200887
Radek Krejcif13b87b2020-12-01 22:02:17 +0100888 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200889}
890
891/**
David Sedlákce77bf52019-07-11 16:59:31 +0200892 * @brief Parse modifier element.
893 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200894 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200895 * @param[in,out] pat Value to write to.
896 * @param[in,out] exts Extension instances to add to.
897 *
898 * @return LY_ERR values.
899 */
900static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100901yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200902{
David Sedlákd3983112019-07-12 11:20:56 +0200903 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200904 const char *temp_val;
905 char *modified_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100906 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200907 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
908 };
David Sedlákce77bf52019-07-11 16:59:31 +0200909
Michal Vaskob36053d2020-03-26 15:49:30 +0100910 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
911 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER));
David Sedlákce77bf52019-07-11 16:59:31 +0200912 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200913 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200914 "modifier", "invert-match");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100915 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200916 return LY_EVALID;
917 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100918 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200919
920 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200921 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100922 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200923 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100924 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200925
926 /* modify the new value */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100927 modified_val[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200928 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
David Sedlákce77bf52019-07-11 16:59:31 +0200929
Radek Krejcif13b87b2020-12-01 22:02:17 +0100930 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200931}
932
933/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200934 * @brief Parse a restriction element (length, range or one instance of must).
935 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200936 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200937 * @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.
David Sedlák6542aed2019-08-14 10:47:43 +0200938 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200939 */
940static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100941yin_parse_restriction(struct lys_yin_parser_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
David Sedlákb7296dd2019-07-11 14:58:38 +0200942{
Radek Krejcid6b76452019-09-03 17:03:03 +0200943 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100944 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200945 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
946 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
947 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
948 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
949 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
950 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200951 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200952 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200953
Michal Vaskob36053d2020-03-26 15:49:30 +0100954 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200955 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200956 restr->arg.mod = ctx->parsed_mod;
Michal Vaskob36053d2020-03-26 15:49:30 +0100957
Radek Krejcif13b87b2020-12-01 22:02:17 +0100958 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr_kw, NULL, &restr->exts);
David Sedlákb7296dd2019-07-11 14:58:38 +0200959}
960
961/**
David Sedlák6542aed2019-08-14 10:47:43 +0200962 * @brief Parse range element.
963 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200964 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200965 * @param[out] type Type structure to store parsed value and flags.
966 *
967 * @return LY_ERR values.
968 */
969static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100970yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200971{
972 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +0100973 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
974 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200975 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +0200976
977 return LY_SUCCESS;
978}
979
980/**
981 * @brief Parse length element.
982 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200983 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200984 * @param[out] type Type structure to store parsed value and flags.
985 *
986 * @return LY_ERR values.
987 */
988static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100989yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200990{
991 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +0100992 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
993 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +0200994 type->flags |= LYS_SET_LENGTH;
995
996 return LY_SUCCESS;
997}
998
999/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001000 * @brief Parse must element.
1001 *
1002 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001003 * @param[in,out] restrs Restrictions to add to.
1004 *
1005 * @return LY_ERR values.
1006 */
1007static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001008yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001009{
1010 struct lysp_restr *restr;
1011
Michal Vaskob36053d2020-03-26 15:49:30 +01001012 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1013 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001014}
1015
1016/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001017 * @brief Parse a node id into an array.
1018 *
1019 * @param[in,out] ctx YIN parser context for logging and to store current state.
1020 * @param[in] kw Type of current element.
1021 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1022 * @param[in,out] exts Extension instances to add to.
1023 *
1024 * @return LY_ERR values.
1025 */
1026static LY_ERR
1027yin_parse_qname(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
1028 struct lysp_ext_instance **exts)
1029{
1030 struct lysp_qname *qname, **qnames;
1031
1032 switch (kw) {
1033 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001034 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1035 qname = (struct lysp_qname *)subinfo->dest;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001036 } else {
1037 qnames = (struct lysp_qname **)subinfo->dest;
1038 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001039 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001040 qname->mod = ctx->parsed_mod;
1041 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
1042 case LY_STMT_IF_FEATURE:
1043 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1044 qnames = (struct lysp_qname **)subinfo->dest;
1045 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1046 qname->mod = ctx->parsed_mod;
1047 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001048 default:
1049 break;
1050 }
1051
1052 LOGINT(ctx->xmlctx->ctx);
1053 return LY_EINT;
1054}
1055
1056/**
David Sedlák5545f5d2019-07-11 11:55:16 +02001057 * @brief Parse position or value element.
1058 *
1059 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001060 * @param[in] kw Type of current element, can be set to LY_STMT_POSITION or LY_STMT_VALUE.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001061 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +02001062 *
1063 * @return LY_ERR values.
1064 */
1065static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001066yin_parse_value_pos(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type_enum *enm)
David Sedlák5545f5d2019-07-11 11:55:16 +02001067{
Radek Krejcid6b76452019-09-03 17:03:03 +02001068 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +02001069 const char *temp_val = NULL;
1070 char *ptr;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001071 long int num = 0;
1072 unsigned long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +02001073
1074 /* set value flag */
1075 enm->flags |= LYS_SET_VALUE;
1076
1077 /* get attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001078 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1079 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
Michal Vasko69730152020-10-09 16:30:07 +02001080 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
Radek Krejci0f969882020-08-21 16:56:47 +02001081 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +02001082 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001083 goto error;
1084 }
1085
1086 /* convert value */
1087 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001088 if (kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001089 num = strtol(temp_val, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001090 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlák1538a842019-08-08 15:38:51 +02001091 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001092 goto error;
1093 }
1094 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001095 unum = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák5545f5d2019-07-11 11:55:16 +02001096 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +02001097 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001098 goto error;
1099 }
1100 }
1101 /* check if whole argument value was converted */
1102 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001103 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +02001104 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +02001105 }
1106 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +02001107 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001108 goto error;
1109 }
1110 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +02001111 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +02001112 enm->value = num;
1113 } else {
1114 enm->value = unum;
1115 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001116 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák5545f5d2019-07-11 11:55:16 +02001117
1118 /* parse subelements */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001119 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001120 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1121 };
Michal Vasko69730152020-10-09 16:30:07 +02001122
Radek Krejcif13b87b2020-12-01 22:02:17 +01001123 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, &enm->exts);
David Sedlák5545f5d2019-07-11 11:55:16 +02001124
David Sedlákbf8a2b72019-08-14 16:48:10 +02001125error:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001126 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vaskob36053d2020-03-26 15:49:30 +01001127 return LY_EVALID;
David Sedlák5545f5d2019-07-11 11:55:16 +02001128}
1129
David Sedlák05404f62019-07-24 14:11:53 +02001130/**
1131 * @brief Parse belongs-to element.
1132 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001133 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001134 * @param[out] submod Structure of submodule that is being parsed.
1135 * @param[in,out] exts Extension instances to add to.
1136 *
1137 * @return LY_ERR values
1138 */
1139static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001140yin_parse_belongs_to(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts)
David Sedlák05404f62019-07-24 14:11:53 +02001141{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001142 const char *belongsto;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001143 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001144 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1145 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1146 };
David Sedlák05404f62019-07-24 14:11:53 +02001147
Michal Vaskob36053d2020-03-26 15:49:30 +01001148 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001149 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001150 if (ctx->parsed_mod->mod->name != belongsto) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001151 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001152 belongsto, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001153 lydict_remove(ctx->xmlctx->ctx, belongsto);
1154 return LY_EVALID;
1155 }
1156 lydict_remove(ctx->xmlctx->ctx, belongsto);
Michal Vaskob36053d2020-03-26 15:49:30 +01001157
Radek Krejcif13b87b2020-12-01 22:02:17 +01001158 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +02001159}
1160
David Sedlák5545f5d2019-07-11 11:55:16 +02001161/**
David Sedlákc1771b12019-07-10 15:55:46 +02001162 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001163 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001164 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001165 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001166 * @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.
David Sedlákb4e44562019-07-04 15:42:12 +02001167 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001168 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001169 *
1170 * @return LY_ERR values.
1171 */
1172static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001173yin_parse_meta(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **value, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +02001174{
Radek Krejcid6b76452019-09-03 17:03:03 +02001175 assert(elem_type == LY_STMT_ORGANIZATION || elem_type == LY_STMT_CONTACT || elem_type == LY_STMT_DESCRIPTION || elem_type == LY_STMT_REFERENCE);
David Sedlákb4e44562019-07-04 15:42:12 +02001176
Radek Krejcif13b87b2020-12-01 22:02:17 +01001177 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001178 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1179 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1180 };
Michal Vasko69730152020-10-09 16:30:07 +02001181
David Sedlákdf2a9732019-08-07 13:23:16 +02001182 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001183 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1184 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
David Sedlákb4e44562019-07-04 15:42:12 +02001185
David Sedlákdf2a9732019-08-07 13:23:16 +02001186 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001187 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001188}
1189
1190/**
David Sedlákc1771b12019-07-10 15:55:46 +02001191 * @brief Parse error-message element.
1192 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001193 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc1771b12019-07-10 15:55:46 +02001194 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001195 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001196 *
1197 * @return LY_ERR values.
1198 */
1199static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001200yin_parse_err_msg(struct lys_yin_parser_ctx *ctx, const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +02001201{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001202 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001203 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1204 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1205 };
David Sedlákc1771b12019-07-10 15:55:46 +02001206
David Sedlákdf2a9732019-08-07 13:23:16 +02001207 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001208 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1209 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE));
David Sedlákdf2a9732019-08-07 13:23:16 +02001210
Radek Krejcif13b87b2020-12-01 22:02:17 +01001211 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001212}
1213
1214/**
David Sedlák6542aed2019-08-14 10:47:43 +02001215 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001216 *
1217 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001218 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001219 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001220 *
1221 * @return LY_ERR values.
1222 */
1223static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001224yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001225{
David Sedlák6542aed2019-08-14 10:47:43 +02001226 struct lysp_type *type = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02001227
Radek Krejcid6b76452019-09-03 17:03:03 +02001228 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001229 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001230 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001231 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001232 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001233 type = (struct lysp_type *)subinfo->dest;
1234 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001235
David Sedlák6542aed2019-08-14 10:47:43 +02001236 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001237 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001238 struct lysp_type *nested_type = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001239 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001240 type->flags |= LYS_SET_TYPE;
1241 type = nested_type;
1242 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001243
Michal Vasko5bbcc242021-02-23 09:29:13 +01001244 type->pmod = ctx->parsed_mod;
1245
Radek Krejcif13b87b2020-12-01 22:02:17 +01001246 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001247 {LY_STMT_BASE, type, 0},
1248 {LY_STMT_BIT, type, 0},
1249 {LY_STMT_ENUM, type, 0},
1250 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1251 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1252 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1253 {LY_STMT_PATTERN, type, 0},
1254 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1255 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1256 {LY_STMT_TYPE, type},
1257 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1258 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001259
1260 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1261 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01001262 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001263}
1264
David Sedlák1af868e2019-07-17 17:03:14 +02001265/**
1266 * @brief Parse max-elements element.
1267 *
1268 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001269 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001270 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001271 * @param[in,out] exts Extension instances to add to.
1272 *
1273 * @return LY_ERR values.
1274 */
1275static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001276yin_parse_maxelements(struct lys_yin_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák1af868e2019-07-17 17:03:14 +02001277{
1278 const char *temp_val = NULL;
1279 char *ptr;
1280 unsigned long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001281 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001282 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1283 };
David Sedlák374d2b32019-07-17 15:06:55 +02001284
David Sedlák1af868e2019-07-17 17:03:14 +02001285 *flags |= LYS_SET_MAX;
Michal Vaskob36053d2020-03-26 15:49:30 +01001286 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1287 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS));
Michal Vasko69730152020-10-09 16:30:07 +02001288 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '0') || ((temp_val[0] != 'u') && !isdigit(temp_val[0]))) {
David Sedlák1538a842019-08-08 15:38:51 +02001289 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001290 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001291 return LY_EVALID;
1292 }
1293
1294 if (strcmp(temp_val, "unbounded")) {
1295 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001296 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák1af868e2019-07-17 17:03:14 +02001297 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001298 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001299 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001300 return LY_EVALID;
1301 }
1302 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001303 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001304 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001305 return LY_EVALID;
1306 }
1307 *max = num;
1308 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001309 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001310 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001311}
David Sedlák374d2b32019-07-17 15:06:55 +02001312
1313/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001314 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001315 *
1316 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001317 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001318 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001319 * @param[in,out] exts Extension instances to add to.
1320 *
1321 * @return LY_ERR values.
1322 */
1323static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001324yin_parse_minelements(struct lys_yin_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák09e18c92019-07-18 11:17:11 +02001325{
1326 const char *temp_val = NULL;
1327 char *ptr;
1328 unsigned long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001329 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001330 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1331 };
David Sedlák09e18c92019-07-18 11:17:11 +02001332
1333 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001334 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1335 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS));
David Sedlák09e18c92019-07-18 11:17:11 +02001336
Michal Vasko69730152020-10-09 16:30:07 +02001337 if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
David Sedlák1538a842019-08-08 15:38:51 +02001338 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001339 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001340 return LY_EVALID;
1341 }
1342
1343 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001344 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák09e18c92019-07-18 11:17:11 +02001345 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001346 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001347 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001348 return LY_EVALID;
1349 }
Michal Vasko69730152020-10-09 16:30:07 +02001350 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001351 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001352 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001353 return LY_EVALID;
1354 }
1355 *min = num;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001356 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001357 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001358}
1359
David Sedláka2dad212019-07-18 12:45:19 +02001360/**
1361 * @brief Parse min-elements or max-elements element.
1362 *
1363 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001364 * @param[in] parent Identification of parent element.
1365 * @param[in] current Identification of current element.
1366 * @param[in] dest Where the parsed value and flags should be stored.
1367 *
1368 * @return LY_ERR values.
1369 */
David Sedlák09e18c92019-07-18 11:17:11 +02001370static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001371yin_parse_minmax(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, enum ly_stmt current, void *dest)
David Sedlák09e18c92019-07-18 11:17:11 +02001372{
Radek Krejcid6b76452019-09-03 17:03:03 +02001373 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1374 assert(parent == LY_STMT_LEAF_LIST || parent == LY_STMT_REFINE || parent == LY_STMT_LIST || parent == LY_STMT_DEVIATE);
David Sedlák09e18c92019-07-18 11:17:11 +02001375 uint32_t *lim;
1376 uint16_t *flags;
1377 struct lysp_ext_instance **exts;
1378
Radek Krejcid6b76452019-09-03 17:03:03 +02001379 if (parent == LY_STMT_LEAF_LIST) {
1380 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001381 flags = &((struct lysp_node_leaflist *)dest)->flags;
1382 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001383 } else if (parent == LY_STMT_REFINE) {
1384 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001385 flags = &((struct lysp_refine *)dest)->flags;
1386 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001387 } else if (parent == LY_STMT_LIST) {
1388 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001389 flags = &((struct lysp_node_list *)dest)->flags;
1390 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001391 } else {
1392 lim = ((struct minmax_dev_meta *)dest)->lim;
1393 flags = ((struct minmax_dev_meta *)dest)->flags;
1394 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001395 }
1396
Radek Krejcid6b76452019-09-03 17:03:03 +02001397 if (current == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001398 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001399 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001400 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001401 }
1402
1403 return LY_SUCCESS;
1404}
1405
1406/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001407 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001408 *
1409 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001410 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001411 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001412 *
1413 * @return LY_ERR values.
1414 */
1415static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001416yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001417{
1418 const char *temp_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001419 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001420 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1421 };
David Sedláka2dad212019-07-18 12:45:19 +02001422
Michal Vaskob36053d2020-03-26 15:49:30 +01001423 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1424 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY));
David Sedláka2dad212019-07-18 12:45:19 +02001425 if (strcmp(temp_val, "system") == 0) {
1426 *flags |= LYS_ORDBY_SYSTEM;
1427 } else if (strcmp(temp_val, "user") == 0) {
1428 *flags |= LYS_ORDBY_USER;
1429 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001430 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001431 "ordered-by", "system", "user");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001432 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001433 return LY_EVALID;
1434 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001435 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001436
Radek Krejcif13b87b2020-12-01 22:02:17 +01001437 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001438}
1439
1440/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001441 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001442 *
1443 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001444 * @param[in] any_kw Identification of current element, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML
David Sedlákad83cf92019-08-13 12:53:53 +02001445 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001446 *
1447 * @return LY_ERR values.
1448 */
1449static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001450yin_parse_any(struct lys_yin_parser_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta)
David Sedlák8a83bbb2019-07-18 14:46:00 +02001451{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001452 struct lysp_node_anydata *any;
1453
David Sedlák8d552d62019-08-06 15:29:05 +02001454 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001455 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001456 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001457 any->parent = node_meta->parent;
1458
David Sedlákbf8a2b72019-08-14 16:48:10 +02001459 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001460 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1461 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
David Sedlák8a83bbb2019-07-18 14:46:00 +02001462
Radek Krejcif13b87b2020-12-01 22:02:17 +01001463 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001464 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1465 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1466 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1467 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1468 {LY_STMT_MUST, &any->musts, 0},
1469 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1470 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1471 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1472 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1473 };
Michal Vasko69730152020-10-09 16:30:07 +02001474
Radek Krejcif13b87b2020-12-01 22:02:17 +01001475 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any_kw, NULL, &any->exts);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001476}
1477
1478/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001479 * @brief parse leaf element.
1480 *
1481 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001482 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001483 *
1484 * @return LY_ERR values.
1485 */
1486static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001487yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001488{
David Sedlák203ca3a2019-07-18 15:26:25 +02001489 struct lysp_node_leaf *leaf;
1490
David Sedlák8d552d62019-08-06 15:29:05 +02001491 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001492 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001493 leaf->nodetype = LYS_LEAF;
1494 leaf->parent = node_meta->parent;
1495
David Sedlák203ca3a2019-07-18 15:26:25 +02001496 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001497 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1498 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF));
David Sedlák203ca3a2019-07-18 15:26:25 +02001499
1500 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001501 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001502 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1503 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1504 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1505 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1506 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1507 {LY_STMT_MUST, &leaf->musts, 0},
1508 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1509 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1510 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1511 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1512 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1513 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1514 };
Michal Vasko69730152020-10-09 16:30:07 +02001515
Radek Krejcif13b87b2020-12-01 22:02:17 +01001516 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001517}
1518
1519/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001520 * @brief Parse leaf-list element.
1521 *
1522 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001523 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001524 *
1525 * @return LY_ERR values.
1526 */
1527static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001528yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001529{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001530 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001531
Michal Vaskob36053d2020-03-26 15:49:30 +01001532 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001533
David Sedlákc3da3ef2019-07-19 12:56:08 +02001534 llist->nodetype = LYS_LEAFLIST;
1535 llist->parent = node_meta->parent;
1536
David Sedlákc3da3ef2019-07-19 12:56:08 +02001537 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001538 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1539 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001540
1541 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001542 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001543 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1544 {LY_STMT_DEFAULT, &llist->dflts, 0},
1545 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1546 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1547 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1548 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1549 {LY_STMT_MUST, &llist->musts, 0},
1550 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1551 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1552 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1553 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1554 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1555 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1556 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1557 };
Michal Vasko69730152020-10-09 16:30:07 +02001558
Radek Krejcif13b87b2020-12-01 22:02:17 +01001559 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF_LIST, NULL, &llist->exts));
David Sedlákc3da3ef2019-07-19 12:56:08 +02001560
David Sedlákbf8a2b72019-08-14 16:48:10 +02001561 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001562 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001563 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001564 return LY_EVALID;
1565 }
Michal Vasko69730152020-10-09 16:30:07 +02001566 if (llist->max && (llist->min > llist->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02001567 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001568 return LY_EVALID;
1569 }
1570
1571 return LY_SUCCESS;
1572}
1573
1574/**
David Sedlák04e17b22019-07-19 15:29:48 +02001575 * @brief Parse typedef element.
1576 *
1577 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001578 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001579 *
1580 * @return LY_ERR values.
1581 */
1582static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001583yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001584{
1585 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001586 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vasko69730152020-10-09 16:30:07 +02001587
Michal Vaskob36053d2020-03-26 15:49:30 +01001588 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001589
1590 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001591 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1592 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF));
David Sedlák04e17b22019-07-19 15:29:48 +02001593
1594 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001595 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001596 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1597 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1598 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1599 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1600 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1601 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1602 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1603 };
Michal Vasko69730152020-10-09 16:30:07 +02001604
Radek Krejcif13b87b2020-12-01 22:02:17 +01001605 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPEDEF, NULL, &tpdf->exts));
David Sedlák04e17b22019-07-19 15:29:48 +02001606
1607 /* store data for collision check */
Michal Vasko69730152020-10-09 16:30:07 +02001608 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT |
1609 LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02001610 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001611 }
1612
1613 return LY_SUCCESS;
1614}
1615
1616/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001617 * @brief Parse refine element.
1618 *
1619 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001620 * @param[in,out] refines Refines to add to.
1621 *
1622 * @return LY_ERR values.
1623 */
1624static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001625yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001626{
1627 struct lysp_refine *rf;
1628
1629 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001630 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001631
1632 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001633 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1634 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1635 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001636
1637 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001638 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001639 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001640 {LY_STMT_DEFAULT, &rf->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001641 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001642 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001643 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1644 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1645 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1646 {LY_STMT_MUST, &rf->musts, 0},
1647 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1648 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1649 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1650 };
Michal Vasko69730152020-10-09 16:30:07 +02001651
Radek Krejcif13b87b2020-12-01 22:02:17 +01001652 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001653}
1654
1655/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001656 * @brief Parse uses element.
1657 *
1658 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001659 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001660 *
1661 * @return LY_ERR values.
1662 */
1663static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001664yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001665{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001666 struct lysp_node_uses *uses;
1667
David Sedlák8d552d62019-08-06 15:29:05 +02001668 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001669 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001670 uses->nodetype = LYS_USES;
1671 uses->parent = node_meta->parent;
1672
David Sedlák0d6de5a2019-07-22 13:25:44 +02001673 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001674 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1675 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001676
1677 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001678 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
Radek Krejcif13b87b2020-12-01 22:02:17 +01001679 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001680 {LY_STMT_AUGMENT, &augments, 0},
1681 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1682 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1683 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1684 {LY_STMT_REFINE, &uses->refines, 0},
1685 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1686 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1687 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1688 };
Michal Vasko69730152020-10-09 16:30:07 +02001689
Radek Krejcif13b87b2020-12-01 22:02:17 +01001690 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_USES, NULL, &uses->exts));
David Sedlák0d6de5a2019-07-22 13:25:44 +02001691
1692 return LY_SUCCESS;
1693}
1694
1695/**
David Sedlákaa854b02019-07-22 14:17:10 +02001696 * @brief Parse revision element.
1697 *
1698 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001699 * @param[in,out] revs Parsed revisions to add to.
1700 *
1701 * @return LY_ERR values.
1702 */
1703static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001704yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001705{
1706 struct lysp_revision *rev;
1707 const char *temp_date = NULL;
1708
1709 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001710 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001711
1712 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001713 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1714 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION));
David Sedlákaa854b02019-07-22 14:17:10 +02001715 /* check value */
1716 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskoe180ed02021-02-05 16:31:20 +01001717 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001718 return LY_EVALID;
1719 }
Radek Krejci36642162021-01-12 16:49:22 +01001720 memcpy(rev->date, temp_date, LY_REV_SIZE);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001721 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001722
1723 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001724 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001725 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1726 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1727 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1728 };
Michal Vasko69730152020-10-09 16:30:07 +02001729
Radek Krejcif13b87b2020-12-01 22:02:17 +01001730 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001731}
1732
David Sedlák5e13dea2019-07-22 16:06:45 +02001733/**
1734 * @brief Parse include element.
1735 *
1736 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001737 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1738 *
1739 * @return LY_ERR values.
1740 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001741static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001742yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001743{
1744 struct lysp_include *inc;
1745
1746 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001747 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001748
1749 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001750 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1751 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE));
David Sedlák0c2bab92019-07-22 15:33:19 +02001752
1753 /* submodules share the namespace with the module names, so there must not be
1754 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001755 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001756 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001757 return LY_EVALID;
1758 }
1759
1760 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001761 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001762 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1763 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1764 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1765 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1766 };
Michal Vasko69730152020-10-09 16:30:07 +02001767
Radek Krejcif13b87b2020-12-01 22:02:17 +01001768 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001769}
1770
David Sedlákaa854b02019-07-22 14:17:10 +02001771/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001772 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001773 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001774 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001775 * @param[in,out] rev Array to store the parsed value in.
1776 * @param[in,out] exts Extension instances to add to.
1777 *
1778 * @return LY_ERR values.
1779 */
1780static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001781yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001782{
1783 const char *temp_rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001784 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001785 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1786 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001787
Michal Vaskob36053d2020-03-26 15:49:30 +01001788 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1789 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
David Sedlákdfbbb442019-08-06 16:33:21 +02001790 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
Michal Vaskoe180ed02021-02-05 16:31:20 +01001791 lydict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001792
1793 strcpy(rev, temp_rev);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001794 lydict_remove(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001795
Radek Krejcif13b87b2020-12-01 22:02:17 +01001796 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001797}
1798
1799/**
1800 * @brief Parse config element.
1801 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001802 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001803 * @param[in,out] flags Flags to add to.
1804 * @param[in,out] exts Extension instances to add to.
1805 *
1806 * @return LY_ERR values.
1807 */
1808static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001809yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001810{
1811 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001812 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001813 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1814 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001815
Michal Vaskob36053d2020-03-26 15:49:30 +01001816 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1817 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG));
David Sedlákdfbbb442019-08-06 16:33:21 +02001818 if (strcmp(temp_val, "true") == 0) {
1819 *flags |= LYS_CONFIG_W;
1820 } else if (strcmp(temp_val, "false") == 0) {
1821 *flags |= LYS_CONFIG_R;
1822 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001823 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
Michal Vasko69730152020-10-09 16:30:07 +02001824 "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001825 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001826 return LY_EVALID;
1827 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001828 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001829
Radek Krejcif13b87b2020-12-01 22:02:17 +01001830 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001831}
1832
1833/**
1834 * @brief Parse yang-version element.
1835 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001836 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001837 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001838 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001839 *
1840 * @return LY_ERR values.
1841 */
1842static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001843yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001844{
1845 const char *temp_version = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001846 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001847 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1848 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001849
Michal Vaskob36053d2020-03-26 15:49:30 +01001850 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1851 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION));
Radek Krejci96e48da2020-09-04 13:18:06 +02001852 if (strcmp(temp_version, "1") == 0) {
David Sedlákdfbbb442019-08-06 16:33:21 +02001853 *version = LYS_VERSION_1_0;
1854 } else if (strcmp(temp_version, "1.1") == 0) {
1855 *version = LYS_VERSION_1_1;
1856 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001857 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001858 "yang-version", "1", "1.1");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001859 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001860 return LY_EVALID;
1861 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001862 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001863
Radek Krejcif13b87b2020-12-01 22:02:17 +01001864 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001865}
1866
1867/**
1868 * @brief Parse import element.
1869 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001870 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001871 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1872 *
1873 * @return LY_ERR values.
1874 */
1875static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001876yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001877{
1878 struct lysp_import *imp;
Michal Vasko69730152020-10-09 16:30:07 +02001879
David Sedlákdfbbb442019-08-06 16:33:21 +02001880 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001881 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001882
Radek Krejcif13b87b2020-12-01 22:02:17 +01001883 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001884 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1885 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1886 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1887 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1888 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1889 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001890
1891 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001892 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1893 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
Radek Krejcif13b87b2020-12-01 22:02:17 +01001894 LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IMPORT, NULL, &imp->exts));
David Sedlákdfbbb442019-08-06 16:33:21 +02001895 /* check prefix validity */
1896 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1897
1898 return LY_SUCCESS;
1899}
1900
1901/**
1902 * @brief Parse mandatory element.
1903 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001904 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001905 * @param[in,out] flags Flags to add to.
1906 * @param[in,out] exts Extension instances to add to.
1907 *
1908 * @return LY_ERR values.
1909 */
1910static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001911yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001912{
1913 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001914 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001915 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1916 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001917
Michal Vaskob36053d2020-03-26 15:49:30 +01001918 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1919 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY));
David Sedlákdfbbb442019-08-06 16:33:21 +02001920 if (strcmp(temp_val, "true") == 0) {
1921 *flags |= LYS_MAND_TRUE;
1922 } else if (strcmp(temp_val, "false") == 0) {
1923 *flags |= LYS_MAND_FALSE;
1924 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001925 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001926 "mandatory", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001927 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001928 return LY_EVALID;
1929 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001930 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001931
Radek Krejcif13b87b2020-12-01 22:02:17 +01001932 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001933}
1934
1935/**
1936 * @brief Parse status element.
1937 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001938 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001939 * @param[in,out] flags Flags to add to.
1940 * @param[in,out] exts Extension instances to add to.
1941 *
1942 * @return LY_ERR values.
1943 */
1944static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001945yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001946{
1947 const char *value = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001948 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001949 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1950 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001951
Michal Vaskob36053d2020-03-26 15:49:30 +01001952 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1953 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS));
David Sedlákdfbbb442019-08-06 16:33:21 +02001954 if (strcmp(value, "current") == 0) {
1955 *flags |= LYS_STATUS_CURR;
1956 } else if (strcmp(value, "deprecated") == 0) {
1957 *flags |= LYS_STATUS_DEPRC;
1958 } else if (strcmp(value, "obsolete") == 0) {
1959 *flags |= LYS_STATUS_OBSLT;
1960 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001961 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001962 "status", "current", "deprecated", "obsolete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001963 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001964 return LY_EVALID;
1965 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001966 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001967
Radek Krejcif13b87b2020-12-01 22:02:17 +01001968 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001969}
1970
1971/**
1972 * @brief Parse when element.
1973 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001974 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001975 * @param[out] when_p When pointer to parse to.
1976 */
1977static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001978yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02001979{
1980 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02001981 LY_ERR ret = LY_SUCCESS;
1982
David Sedlákdfbbb442019-08-06 16:33:21 +02001983 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01001984 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02001985
1986 ret = lyxml_ctx_next(ctx->xmlctx);
1987 LY_CHECK_ERR_RET(ret, free(when), ret);
1988
Michal Vaskob36053d2020-03-26 15:49:30 +01001989 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02001990 LY_CHECK_ERR_RET(ret, free(when), ret);
1991
David Sedlákdfbbb442019-08-06 16:33:21 +02001992 *when_p = when;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001993 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001994 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1995 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1996 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1997 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001998
Radek Krejcif13b87b2020-12-01 22:02:17 +01001999 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002000}
2001
2002/**
2003 * @brief Parse yin-elemenet element.
2004 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002005 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002006 * @param[in,out] data Data to read from, always moved to currently handled position.
2007 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002008 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002009 *
2010 * @return LY_ERR values.
2011 */
2012static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002013yin_parse_yin_element(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002014{
2015 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002016 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002017 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2018 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002019
Michal Vaskob36053d2020-03-26 15:49:30 +01002020 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2021 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT));
David Sedlákdfbbb442019-08-06 16:33:21 +02002022 if (strcmp(temp_val, "true") == 0) {
2023 *flags |= LYS_YINELEM_TRUE;
2024 } else if (strcmp(temp_val, "false") == 0) {
2025 *flags |= LYS_YINELEM_FALSE;
2026 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002027 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002028 "yin-element", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002029 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002030 return LY_EVALID;
2031 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002032 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002033
Radek Krejcif13b87b2020-12-01 22:02:17 +01002034 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002035}
2036
2037/**
2038 * @brief Parse argument element.
2039 *
Michal Vaskob36053d2020-03-26 15:49:30 +01002040 * @param[in,out] xmlctx Xml context.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002041 * @param[in,out] arg_meta Meta information about destionation of parsed data.
2042 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002043 *
2044 * @return LY_ERR values.
2045 */
2046static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002047yin_parse_argument(struct lys_yin_parser_ctx *ctx, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02002048{
Radek Krejcif13b87b2020-12-01 22:02:17 +01002049 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002050 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2051 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2052 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002053
Michal Vaskob36053d2020-03-26 15:49:30 +01002054 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2055 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT));
David Sedlákdfbbb442019-08-06 16:33:21 +02002056
Radek Krejcif13b87b2020-12-01 22:02:17 +01002057 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002058}
2059
2060/**
2061 * @brief Parse the extension statement.
2062 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002063 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002064 * @param[in,out] extensions Extensions to add to.
2065 *
2066 * @return LY_ERR values.
2067 */
2068static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002069yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02002070{
2071 struct lysp_ext *ex;
Michal Vasko69730152020-10-09 16:30:07 +02002072
Michal Vaskob36053d2020-03-26 15:49:30 +01002073 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2074 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2075 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION));
David Sedlákdfbbb442019-08-06 16:33:21 +02002076
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002077 struct yin_argument_meta arg_info = {&ex->flags, &ex->argname};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002078 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002079 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2080 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2081 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2082 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2083 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2084 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002085
Radek Krejcif13b87b2020-12-01 22:02:17 +01002086 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002087}
2088
2089/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002090 * @brief Parse feature element.
2091 *
2092 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02002093 * @param[in,out] features Features to add to.
2094 *
2095 * @return LY_ERR values.
2096 */
2097static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002098yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02002099{
2100 struct lysp_feature *feat;
2101
2102 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01002103 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02002104
2105 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002106 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2107 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE));
David Sedlák5e13dea2019-07-22 16:06:45 +02002108
2109 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002110 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002111 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2112 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2113 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2114 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2115 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2116 };
Michal Vasko69730152020-10-09 16:30:07 +02002117
Radek Krejcif13b87b2020-12-01 22:02:17 +01002118 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02002119}
2120
2121/**
David Sedlák28794f22019-07-22 16:45:00 +02002122 * @brief Parse identity element.
2123 *
2124 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02002125 * @param[in,out] identities Identities to add to.
2126 *
2127 * @return LY_ERR values.
2128 */
2129static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002130yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02002131{
2132 struct lysp_ident *ident;
2133
2134 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01002135 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02002136
2137 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002138 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2139 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY));
David Sedlák28794f22019-07-22 16:45:00 +02002140
2141 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002142 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002143 {LY_STMT_BASE, &ident->bases, 0},
2144 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2145 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2146 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2147 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2148 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2149 };
Michal Vasko69730152020-10-09 16:30:07 +02002150
Radek Krejcif13b87b2020-12-01 22:02:17 +01002151 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02002152}
2153
2154/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002155 * @brief Parse list element.
2156 *
2157 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002158 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002159 *
2160 * @return LY_ERR values.
2161 */
2162static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002163yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002164{
David Sedlákaf536aa2019-07-23 13:42:23 +02002165 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002166 LY_ERR ret = LY_SUCCESS;
2167 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002168 size_t subelems_size;
David Sedlákaf536aa2019-07-23 13:42:23 +02002169
Michal Vaskob36053d2020-03-26 15:49:30 +01002170 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02002171 list->nodetype = LYS_LIST;
2172 list->parent = node_meta->parent;
2173
David Sedlákaf536aa2019-07-23 13:42:23 +02002174 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002175 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2176 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST));
David Sedlákaf536aa2019-07-23 13:42:23 +02002177
2178 /* parse list content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002179 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002180 LY_STMT_ACTION, &list->actions, 0,
2181 LY_STMT_ANYDATA, &list->child, 0,
2182 LY_STMT_ANYXML, &list->child, 0,
2183 LY_STMT_CHOICE, &list->child, 0,
2184 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2185 LY_STMT_CONTAINER, &list->child, 0,
2186 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2187 LY_STMT_GROUPING, &list->groupings, 0,
2188 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2189 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2190 LY_STMT_LEAF, &list->child, 0,
2191 LY_STMT_LEAF_LIST, &list->child, 0,
2192 LY_STMT_LIST, &list->child, 0,
2193 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2194 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2195 LY_STMT_MUST, &list->musts, 0,
2196 LY_STMT_NOTIFICATION, &list->notifs, 0,
2197 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2198 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2199 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2200 LY_STMT_TYPEDEF, &list->typedefs, 0,
2201 LY_STMT_UNIQUE, &list->uniques, 0,
2202 LY_STMT_USES, &list->child, 0,
2203 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2204 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002205 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_LIST, NULL, &list->exts);
2206 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002207 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002208
Michal Vasko69730152020-10-09 16:30:07 +02002209 if (list->max && (list->min > list->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02002210 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002211 return LY_EVALID;
2212 }
2213
2214 return LY_SUCCESS;
2215}
2216
2217/**
David Sedlák031b9e72019-07-23 15:19:37 +02002218 * @brief Parse notification element.
2219 *
2220 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002221 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002222 *
2223 * @return LY_ERR values.
2224 */
2225static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002226yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002227{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002228 struct lysp_node_notif *notif;
2229 struct lysp_node_notif **notifs = (struct lysp_node_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002230 LY_ERR ret = LY_SUCCESS;
2231 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002232 size_t subelems_size;
David Sedlák031b9e72019-07-23 15:19:37 +02002233
2234 /* allocate new notification */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002235 LY_LIST_NEW_RET(ctx->xmlctx->ctx, notifs, notif, next, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002236 notif->nodetype = LYS_NOTIF;
2237 notif->parent = notif_meta->parent;
2238
2239 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002240 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2241 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION));
David Sedlák031b9e72019-07-23 15:19:37 +02002242
2243 /* parse notification content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002244 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002245 LY_STMT_ANYDATA, &notif->child, 0,
2246 LY_STMT_ANYXML, &notif->child, 0,
2247 LY_STMT_CHOICE, &notif->child, 0,
2248 LY_STMT_CONTAINER, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002249 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2250 LY_STMT_GROUPING, &notif->groupings, 0,
2251 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002252 LY_STMT_LEAF, &notif->child, 0,
2253 LY_STMT_LEAF_LIST, &notif->child, 0,
2254 LY_STMT_LIST, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002255 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2256 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2257 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2258 LY_STMT_TYPEDEF, &notif->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002259 LY_STMT_USES, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002260 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02002261
Radek Krejcif13b87b2020-12-01 22:02:17 +01002262 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_NOTIFICATION, NULL, &notif->exts);
2263 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002264 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002265
David Sedlák031b9e72019-07-23 15:19:37 +02002266 return LY_SUCCESS;
2267}
2268
2269/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002270 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002271 *
2272 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002273 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002274 *
2275 * @return LY_ERR values.
2276 */
2277static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002278yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002279{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002280 struct lysp_node_grp *grp;
2281 struct lysp_node_grp **grps = (struct lysp_node_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002282 LY_ERR ret = LY_SUCCESS;
2283 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002284 size_t subelems_size;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002285
2286 /* create new grouping */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002287 LY_LIST_NEW_RET(ctx->xmlctx->ctx, grps, grp, next, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002288 grp->nodetype = LYS_GROUPING;
2289 grp->parent = gr_meta->parent;
2290
2291 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002292 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2293 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING));
David Sedláke3ce9ef2019-07-23 16:34:30 +02002294
2295 /* parse grouping content */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002296 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, &grp->node, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002297 LY_STMT_ACTION, &grp->actions, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002298 LY_STMT_ANYDATA, &grp->child, 0,
2299 LY_STMT_ANYXML, &grp->child, 0,
2300 LY_STMT_CHOICE, &grp->child, 0,
2301 LY_STMT_CONTAINER, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002302 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2303 LY_STMT_GROUPING, &grp->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002304 LY_STMT_LEAF, &grp->child, 0,
2305 LY_STMT_LEAF_LIST, &grp->child, 0,
2306 LY_STMT_LIST, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002307 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2308 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2309 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2310 LY_STMT_TYPEDEF, &grp->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002311 LY_STMT_USES, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002312 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002313 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_GROUPING, NULL, &grp->exts);
2314 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002315
Radek Krejci2a9fc652021-01-22 17:44:34 +01002316 return ret;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002317}
2318
2319/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002320 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002321 *
2322 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002323 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002324 *
2325 * @return LY_ERR values.
2326 */
2327static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002328yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002329{
David Sedlákf111bcb2019-07-23 17:15:51 +02002330 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002331 LY_ERR ret = LY_SUCCESS;
2332 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002333 size_t subelems_size;
David Sedlákf111bcb2019-07-23 17:15:51 +02002334
2335 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002336 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002337 cont->nodetype = LYS_CONTAINER;
2338 cont->parent = node_meta->parent;
2339
David Sedlákf111bcb2019-07-23 17:15:51 +02002340 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002341 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2342 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER));
David Sedlákf111bcb2019-07-23 17:15:51 +02002343
2344 /* parse container content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002345 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002346 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2347 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2348 LY_STMT_ANYXML, &cont->child, 0,
2349 LY_STMT_CHOICE, &cont->child, 0,
2350 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2351 LY_STMT_CONTAINER, &cont->child, 0,
2352 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2353 LY_STMT_GROUPING, &cont->groupings, 0,
2354 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2355 LY_STMT_LEAF, &cont->child, 0,
2356 LY_STMT_LEAF_LIST, &cont->child, 0,
2357 LY_STMT_LIST, &cont->child, 0,
2358 LY_STMT_MUST, &cont->musts, 0,
2359 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2360 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2361 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2362 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2363 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2364 LY_STMT_USES, &cont->child, 0,
2365 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2366 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002367 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CONTAINER, NULL, &cont->exts);
2368 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002369
Radek Krejci2a9fc652021-01-22 17:44:34 +01002370 return ret;
David Sedlákf111bcb2019-07-23 17:15:51 +02002371}
2372
2373/**
David Sedlák5379d392019-07-24 10:42:03 +02002374 * @brief Parse case element.
2375 *
2376 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002377 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002378 *
2379 * @return LY_ERR values.
2380 */
2381static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002382yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002383{
David Sedlák5379d392019-07-24 10:42:03 +02002384 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002385 LY_ERR ret = LY_SUCCESS;
Michal Vasko69730152020-10-09 16:30:07 +02002386 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002387 size_t subelems_size;
David Sedlák5379d392019-07-24 10:42:03 +02002388
2389 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002390 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002391 cas->nodetype = LYS_CASE;
2392 cas->parent = node_meta->parent;
2393
David Sedlák5379d392019-07-24 10:42:03 +02002394 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002395 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2396 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE));
David Sedlák5379d392019-07-24 10:42:03 +02002397
2398 /* parse case content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002399 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002400 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2401 LY_STMT_ANYXML, &cas->child, 0,
2402 LY_STMT_CHOICE, &cas->child, 0,
2403 LY_STMT_CONTAINER, &cas->child, 0,
2404 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2405 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2406 LY_STMT_LEAF, &cas->child, 0,
2407 LY_STMT_LEAF_LIST, &cas->child, 0,
2408 LY_STMT_LIST, &cas->child, 0,
2409 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2410 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2411 LY_STMT_USES, &cas->child, 0,
2412 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2413 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002414 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CASE, NULL, &cas->exts);
2415 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002416
2417 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002418}
2419
2420/**
David Sedlák05404f62019-07-24 14:11:53 +02002421 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002422 *
2423 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002424 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002425 *
2426 * @return LY_ERR values.
2427 */
2428LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002429yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002430{
David Sedlák81497a32019-08-13 16:56:26 +02002431 LY_ERR ret = LY_SUCCESS;
2432 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002433 struct lysp_node_choice *choice;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002434 size_t subelems_size;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002435
2436 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002437 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002438
David Sedlákb7abcfa2019-07-24 12:33:35 +02002439 choice->nodetype = LYS_CHOICE;
2440 choice->parent = node_meta->parent;
2441
David Sedlákb7abcfa2019-07-24 12:33:35 +02002442 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002443 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2444 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE));
David Sedlákb7abcfa2019-07-24 12:33:35 +02002445
2446 /* parse choice content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002447 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002448 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2449 LY_STMT_ANYXML, &choice->child, 0,
2450 LY_STMT_CASE, &choice->child, 0,
2451 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2452 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2453 LY_STMT_CONTAINER, &choice->child, 0,
2454 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2455 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2456 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2457 LY_STMT_LEAF, &choice->child, 0,
2458 LY_STMT_LEAF_LIST, &choice->child, 0,
2459 LY_STMT_LIST, &choice->child, 0,
2460 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2461 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2462 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2463 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2464 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002465 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CHOICE, NULL, &choice->exts);
2466 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002467 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002468}
2469
2470/**
David Sedlák05404f62019-07-24 14:11:53 +02002471 * @brief Parse input or output element.
2472 *
2473 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002474 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002475 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2476 *
2477 * @return LY_ERR values.
2478 */
2479static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002480yin_parse_inout(struct lys_yin_parser_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta)
David Sedlák05404f62019-07-24 14:11:53 +02002481{
David Sedlák81497a32019-08-13 16:56:26 +02002482 LY_ERR ret = LY_SUCCESS;
2483 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002484 size_t subelems_size;
David Sedlák81497a32019-08-13 16:56:26 +02002485
David Sedlák05404f62019-07-24 14:11:53 +02002486 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002487 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), (inout_kw == LY_STMT_INPUT) ? "input" : "output", 0, &inout_meta->inout_p->name));
Radek Krejcid6b76452019-09-03 17:03:03 +02002488 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002489 inout_meta->inout_p->parent = inout_meta->parent;
2490
2491 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002492 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2493 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
David Sedlák05404f62019-07-24 14:11:53 +02002494
2495 /* parser input/output content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002496 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002497 LY_STMT_ANYDATA, &inout_meta->inout_p->child, YIN_SUBELEM_VER2,
2498 LY_STMT_ANYXML, &inout_meta->inout_p->child, 0,
2499 LY_STMT_CHOICE, &inout_meta->inout_p->child, 0,
2500 LY_STMT_CONTAINER, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002501 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002502 LY_STMT_LEAF, &inout_meta->inout_p->child, 0,
2503 LY_STMT_LEAF_LIST, &inout_meta->inout_p->child, 0,
2504 LY_STMT_LIST, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002505 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2506 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002507 LY_STMT_USES, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002508 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002509 ret = yin_parse_content(ctx, subelems, subelems_size, inout_kw, NULL, &inout_meta->inout_p->exts);
2510 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002511 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002512
Radek Krejci01180ac2021-01-27 08:48:22 +01002513 if (!inout_meta->inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002514 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2515 return LY_EVALID;
2516 }
2517
David Sedlák05404f62019-07-24 14:11:53 +02002518 return LY_SUCCESS;
2519}
2520
David Sedlák992fb7c2019-07-24 16:51:01 +02002521/**
2522 * @brief Parse action element.
2523 *
2524 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002525 * @param[in] act_meta Meta information about parent node and actions to add to.
2526 *
2527 * @return LY_ERR values.
2528 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002529static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002530yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002531{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002532 struct lysp_node_action *act, **acts = (struct lysp_node_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002533 LY_ERR ret = LY_SUCCESS;
2534 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002535 size_t subelems_size;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002536 enum ly_stmt kw = act_meta->parent ? LY_STMT_ACTION : LY_STMT_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002537
2538 /* create new action */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002539 LY_LIST_NEW_RET(ctx->xmlctx->ctx, acts, act, next, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002540 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002541 act->parent = act_meta->parent;
2542
2543 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002544 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002545 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, kw));
David Sedlák85d0eca2019-07-24 15:15:21 +02002546
2547 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002548 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002549 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2550 LY_STMT_GROUPING, &act->groupings, 0,
2551 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2552 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2553 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2554 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2555 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2556 LY_STMT_TYPEDEF, &act->typedefs, 0,
2557 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002558 ret = (yin_parse_content(ctx, subelems, subelems_size, kw, NULL, &act->exts));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002559 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002560 LY_CHECK_RET(ret);
2561
Michal Vasko7f45cf22020-10-01 12:49:44 +02002562 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2563 if (!act->input.nodetype) {
2564 act->input.nodetype = LYS_INPUT;
2565 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002566 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002567 }
2568 if (!act->output.nodetype) {
2569 act->output.nodetype = LYS_OUTPUT;
2570 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002571 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002572 }
2573
David Sedlák85d0eca2019-07-24 15:15:21 +02002574 return LY_SUCCESS;
2575}
2576
David Sedlák05404f62019-07-24 14:11:53 +02002577/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002578 * @brief Parse augment element.
2579 *
2580 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002581 * @param[in] aug_meta Meta information about parent node and augments to add to.
2582 *
2583 * @return LY_ERR values.
2584 */
2585static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002586yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002587{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002588 struct lysp_node_augment *aug;
2589 struct lysp_node_augment **augs = (struct lysp_node_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002590 LY_ERR ret = LY_SUCCESS;
2591 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002592 size_t subelems_size;
David Sedlák992fb7c2019-07-24 16:51:01 +02002593
2594 /* create new augment */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002595 LY_LIST_NEW_RET(ctx->xmlctx->ctx, augs, aug, next, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002596 aug->nodetype = LYS_AUGMENT;
2597 aug->parent = aug_meta->parent;
2598
2599 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002600 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2601 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2602 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002603
2604 /* parser augment content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002605 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002606 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2607 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2608 LY_STMT_ANYXML, &aug->child, 0,
2609 LY_STMT_CASE, &aug->child, 0,
2610 LY_STMT_CHOICE, &aug->child, 0,
2611 LY_STMT_CONTAINER, &aug->child, 0,
2612 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002613 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002614 LY_STMT_LEAF, &aug->child, 0,
2615 LY_STMT_LEAF_LIST, &aug->child, 0,
2616 LY_STMT_LIST, &aug->child, 0,
2617 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2618 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2619 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2620 LY_STMT_USES, &aug->child, 0,
2621 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2622 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002623 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_AUGMENT, NULL, &aug->exts);
2624 subelems_deallocator(subelems_size, subelems);
David Sedlák992fb7c2019-07-24 16:51:01 +02002625
Radek Krejci2a9fc652021-01-22 17:44:34 +01002626 return ret;
David Sedlák992fb7c2019-07-24 16:51:01 +02002627}
2628
David Sedlák8b754462019-07-25 16:22:13 +02002629/**
2630 * @brief Parse deviate element.
2631 *
2632 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002633 * @param[in] deviates Deviates to add to.
2634 *
2635 * @return LY_ERR values.
2636 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002637static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002638yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002639{
2640 LY_ERR ret = LY_SUCCESS;
2641 uint8_t dev_mod;
2642 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002643 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002644 struct lysp_deviate_add *d_add = NULL;
2645 struct lysp_deviate_rpl *d_rpl = NULL;
2646 struct lysp_deviate_del *d_del = NULL;
2647
2648 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002649 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2650 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE));
David Sedlák4ffcec82019-07-25 15:10:21 +02002651
2652 if (strcmp(temp_val, "not-supported") == 0) {
2653 dev_mod = LYS_DEV_NOT_SUPPORTED;
2654 } else if (strcmp(temp_val, "add") == 0) {
2655 dev_mod = LYS_DEV_ADD;
2656 } else if (strcmp(temp_val, "replace") == 0) {
2657 dev_mod = LYS_DEV_REPLACE;
2658 } else if (strcmp(temp_val, "delete") == 0) {
2659 dev_mod = LYS_DEV_DELETE;
2660 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002661 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
Michal Vasko69730152020-10-09 16:30:07 +02002662 "not-supported", "add", "replace", "delete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002663 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002664 return LY_EVALID;
2665 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002666 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002667
2668 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2669 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002670 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002671 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002672 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2673 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002674 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002675
2676 } else if (dev_mod == LYS_DEV_ADD) {
2677 d_add = calloc(1, sizeof *d_add);
Michal Vaskob36053d2020-03-26 15:49:30 +01002678 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002679 d = (struct lysp_deviate *)d_add;
2680 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2681 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002682 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002683 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002684 {LY_STMT_DEFAULT, &d_add->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002685 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2686 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2687 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2688 {LY_STMT_MUST, &d_add->musts, 0},
2689 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2690 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2691 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2692 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002693 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002694
2695 } else if (dev_mod == LYS_DEV_REPLACE) {
2696 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002697 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002698 d = (struct lysp_deviate *)d_rpl;
2699 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2700 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002701 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002702 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002703 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
Radek Krejci0f969882020-08-21 16:56:47 +02002704 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2705 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2706 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2707 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2708 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2709 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2710 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002711 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002712
2713 } else {
2714 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002715 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002716 d = (struct lysp_deviate *)d_del;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002717 struct yin_subelement subelems[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002718 {LY_STMT_DEFAULT, &d_del->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002719 {LY_STMT_MUST, &d_del->musts, 0},
2720 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2721 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2722 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2723 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002724 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002725 }
2726 LY_CHECK_GOTO(ret, cleanup);
2727
2728 d->mod = dev_mod;
2729 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002730 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002731
2732 return ret;
2733
2734cleanup:
2735 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002736 return ret;
2737}
2738
David Sedlák992fb7c2019-07-24 16:51:01 +02002739/**
David Sedlák8b754462019-07-25 16:22:13 +02002740 * @brief Parse deviation element.
2741 *
2742 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002743 * @param[in] deviations Deviations to add to.
2744 *
2745 * @return LY_ERR values.
2746 */
2747static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002748yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002749{
2750 struct lysp_deviation *dev;
2751
2752 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002753 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002754
2755 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002756 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2757 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
2758 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
Radek Krejcif13b87b2020-12-01 22:02:17 +01002759 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002760 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2761 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2762 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2763 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2764 };
Michal Vasko69730152020-10-09 16:30:07 +02002765
Radek Krejcif13b87b2020-12-01 22:02:17 +01002766 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002767}
2768
2769/**
David Sedlákc5b20842019-08-13 10:18:31 +02002770 * @brief map keyword to keyword-group.
2771 *
2772 * @param[in] ctx YIN parser context used for logging.
2773 * @param[in] kw Keyword that is child of module or submodule.
2774 * @param[out] group Group of keyword.
2775 *
2776 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2777 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002778static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002779kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002780{
2781 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002782 /* module header */
2783 case LY_STMT_NONE:
2784 case LY_STMT_NAMESPACE:
2785 case LY_STMT_PREFIX:
2786 case LY_STMT_BELONGS_TO:
2787 case LY_STMT_YANG_VERSION:
2788 *group = Y_MOD_MODULE_HEADER;
2789 break;
2790 /* linkage */
2791 case LY_STMT_INCLUDE:
2792 case LY_STMT_IMPORT:
2793 *group = Y_MOD_LINKAGE;
2794 break;
2795 /* meta */
2796 case LY_STMT_ORGANIZATION:
2797 case LY_STMT_CONTACT:
2798 case LY_STMT_DESCRIPTION:
2799 case LY_STMT_REFERENCE:
2800 *group = Y_MOD_META;
2801 break;
2802 /* revision */
2803 case LY_STMT_REVISION:
2804 *group = Y_MOD_REVISION;
2805 break;
2806 /* body */
2807 case LY_STMT_ANYDATA:
2808 case LY_STMT_ANYXML:
2809 case LY_STMT_AUGMENT:
2810 case LY_STMT_CHOICE:
2811 case LY_STMT_CONTAINER:
2812 case LY_STMT_DEVIATION:
2813 case LY_STMT_EXTENSION:
2814 case LY_STMT_FEATURE:
2815 case LY_STMT_GROUPING:
2816 case LY_STMT_IDENTITY:
2817 case LY_STMT_LEAF:
2818 case LY_STMT_LEAF_LIST:
2819 case LY_STMT_LIST:
2820 case LY_STMT_NOTIFICATION:
2821 case LY_STMT_RPC:
2822 case LY_STMT_TYPEDEF:
2823 case LY_STMT_USES:
2824 case LY_STMT_EXTENSION_INSTANCE:
2825 *group = Y_MOD_BODY;
2826 break;
2827 default:
2828 LOGINT(ctx->xmlctx->ctx);
2829 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02002830 }
2831
2832 return LY_SUCCESS;
2833}
2834
David Sedlákc5b20842019-08-13 10:18:31 +02002835/**
2836 * @brief Check if relative order of two keywords is valid.
2837 *
2838 * @param[in] ctx YIN parser context used for logging.
2839 * @param[in] kw Current keyword.
2840 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002841 * @param[in] parrent Identification of parrent element, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
David Sedlákc5b20842019-08-13 10:18:31 +02002842 * because relative order is required only in module and submodule sub-elements, used for logging.
2843 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002844 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002845 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002846static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002847yin_check_relative_order(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum ly_stmt next_kw, enum ly_stmt parrent)
David Sedláke6cd89e2019-08-07 12:46:02 +02002848{
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002850 enum yang_module_stmt gr, next_gr;
2851
2852 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2853 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2854
2855 if (gr > next_gr) {
2856 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2857 return LY_EVALID;
2858 }
2859
2860 return LY_SUCCESS;
2861}
2862
David Sedlákc5dd0942019-09-13 10:23:59 +02002863/**
Michal Vaskoafac7822020-10-20 14:22:26 +02002864 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
2865 *
2866 * @param[in,out] ctx Yin parser context for logging and to store current state.
2867 * @param[in] elem_type Type of element that is currently being parsed.
2868 * @param[out] arg Value to write to.
2869 *
2870 * @return LY_ERR values.
2871 */
2872static LY_ERR
2873yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
2874{
2875 enum ly_stmt child;
2876
2877 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2878
2879 switch (elem_type) {
2880 case LY_STMT_ACTION:
2881 case LY_STMT_ANYDATA:
2882 case LY_STMT_ANYXML:
2883 case LY_STMT_ARGUMENT:
2884 case LY_STMT_BASE:
2885 case LY_STMT_BIT:
2886 case LY_STMT_CASE:
2887 case LY_STMT_CHOICE:
2888 case LY_STMT_CONTAINER:
2889 case LY_STMT_ENUM:
2890 case LY_STMT_EXTENSION:
2891 case LY_STMT_FEATURE:
2892 case LY_STMT_GROUPING:
2893 case LY_STMT_IDENTITY:
2894 case LY_STMT_IF_FEATURE:
2895 case LY_STMT_LEAF:
2896 case LY_STMT_LEAF_LIST:
2897 case LY_STMT_LIST:
2898 case LY_STMT_MODULE:
2899 case LY_STMT_NOTIFICATION:
2900 case LY_STMT_RPC:
2901 case LY_STMT_SUBMODULE:
2902 case LY_STMT_TYPE:
2903 case LY_STMT_TYPEDEF:
2904 case LY_STMT_UNITS:
2905 case LY_STMT_USES:
2906 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
2907 break;
2908 case LY_STMT_AUGMENT:
2909 case LY_STMT_DEVIATION:
2910 case LY_STMT_REFINE:
2911 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
2912 break;
2913 case LY_STMT_CONFIG:
2914 case LY_STMT_DEFAULT:
2915 case LY_STMT_DEVIATE:
2916 case LY_STMT_ERROR_APP_TAG:
2917 case LY_STMT_FRACTION_DIGITS:
2918 case LY_STMT_KEY:
2919 case LY_STMT_LENGTH:
2920 case LY_STMT_MANDATORY:
2921 case LY_STMT_MAX_ELEMENTS:
2922 case LY_STMT_MIN_ELEMENTS:
2923 case LY_STMT_MODIFIER:
2924 case LY_STMT_ORDERED_BY:
2925 case LY_STMT_PATH:
2926 case LY_STMT_PATTERN:
2927 case LY_STMT_POSITION:
2928 case LY_STMT_PREFIX:
2929 case LY_STMT_PRESENCE:
2930 case LY_STMT_RANGE:
2931 case LY_STMT_REQUIRE_INSTANCE:
2932 case LY_STMT_STATUS:
2933 case LY_STMT_VALUE:
2934 case LY_STMT_YANG_VERSION:
2935 case LY_STMT_YIN_ELEMENT:
2936 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
2937 break;
2938 case LY_STMT_IMPORT:
2939 case LY_STMT_INCLUDE:
2940 case LY_STMT_BELONGS_TO:
2941 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
2942 break;
2943 case LY_STMT_INPUT:
2944 case LY_STMT_OUTPUT:
2945 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2946 break;
2947 case LY_STMT_MUST:
2948 case LY_STMT_WHEN:
2949 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
2950 break;
2951 case LY_STMT_NAMESPACE:
2952 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
2953 break;
2954 case LY_STMT_REVISION:
2955 case LY_STMT_REVISION_DATE:
2956 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
2957 break;
2958 case LY_STMT_UNIQUE:
2959 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
2960 break;
2961 /* argument is mapped to yin element */
2962 case LY_STMT_CONTACT:
2963 case LY_STMT_DESCRIPTION:
2964 case LY_STMT_ORGANIZATION:
2965 case LY_STMT_REFERENCE:
2966 case LY_STMT_ERROR_MESSAGE:
2967 /* there shouldn't be any attribute, argument is supposed to be first subelement */
2968 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2969
2970 /* no content */
2971 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
2972 if (ctx->xmlctx->ws_only) {
2973 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2974 }
2975 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
2976 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
2977 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
2978 return LY_EVALID;
2979 }
2980
2981 /* parse child element */
2982 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
2983 if (((elem_type == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
2984 ((elem_type != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
2985 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
2986 ly_stmt2str(elem_type));
2987 return LY_EVALID;
2988 }
2989 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2990
2991 /* no attributes expected? TODO */
2992 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
2993 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2994 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2995 }
2996
2997 /* load and save content */
2998 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
2999 LY_CHECK_RET(!*arg, LY_EMEM);
3000
3001 /* load closing tag of subelement */
3002 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3003
3004 /* if only subelement was parsed as argument, load also closing tag TODO what? */
3005 /*if (ctx->xmlctx->status == LYXML_ELEMENT) {
3006 LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
3007 }*/
3008 break;
3009 default:
3010 LOGINT(ctx->xmlctx->ctx);
3011 return LY_EINT;
3012 }
3013
3014 return LY_SUCCESS;
3015}
3016
3017/**
3018 * @brief Parse yin element into generic structure.
3019 *
3020 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
3021 * @param[in] parent Identification of parent element.
3022 * @param[out] element Where the element structure should be stored.
3023 *
3024 * @return LY_ERR values.
3025 */
3026LY_ERR
3027yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
3028{
3029 LY_ERR ret = LY_SUCCESS;
3030 struct lysp_stmt *last = NULL, *new = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003031 char *id;
Michal Vaskoafac7822020-10-20 14:22:26 +02003032
3033 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3034
3035 /* allocate new structure for element */
3036 *element = calloc(1, sizeof(**element));
3037 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003038
3039 /* store identifier */
3040 if (ctx->xmlctx->prefix_len) {
3041 if (asprintf(&id, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3042 ctx->xmlctx->name) == -1) {
3043 LOGMEM(ctx->xmlctx->ctx);
3044 ret = LY_EMEM;
3045 goto cleanup;
3046 }
3047 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->xmlctx->ctx, id, &(*element)->stmt), cleanup);
3048
3049 /* store prefix data for the statement */
3050 LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->stmt, strlen((*element)->stmt), LY_PREF_XML,
3051 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3052 } else {
3053 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &(*element)->stmt), cleanup);
3054 }
Michal Vaskoafac7822020-10-20 14:22:26 +02003055
3056 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3057 ctx->xmlctx->prefix_len, parent);
3058
3059 last = (*element)->child;
3060 if ((*element)->kw == LY_STMT_NONE) {
3061 /* unrecognized element */
3062 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3063 ly_stmt2str(parent));
3064 ret = LY_EVALID;
3065 goto cleanup;
3066 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3067 /* element is known yang keyword, which means argument can be parsed correctly. */
3068 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3069 LY_CHECK_GOTO(ret, cleanup);
3070 } else {
3071 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3072
3073 /* load attributes in generic way, save all attributes in linked list */
3074 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3075 new = calloc(1, sizeof(*last));
3076 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3077 if (!(*element)->child) {
3078 /* save first */
3079 (*element)->child = new;
3080 } else {
3081 last->next = new;
3082 }
3083 last = new;
3084
3085 last->flags |= LYS_YIN_ATTR;
3086 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3087 last->kw = LY_STMT_NONE;
3088 /* attributes with prefix are ignored */
3089 if (!ctx->xmlctx->prefix) {
3090 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3091
3092 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3093 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3094 } else {
3095 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3096 }
3097 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3098 }
3099 }
3100
3101 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3102 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3103 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3104 /* parse subelements */
3105 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3106 LY_CHECK_GOTO(ret, cleanup);
3107 if (!(*element)->child) {
3108 /* save first */
3109 (*element)->child = new;
3110 } else {
3111 last->next = new;
3112 }
3113 last = new;
3114
3115 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3116 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3117 }
3118 } else {
3119 /* save element content */
3120 if (ctx->xmlctx->value_len) {
3121 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3122 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003123
3124 /* store prefix data for the argument as well */
3125 LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->arg, strlen((*element)->arg), LY_PREF_XML,
3126 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
Michal Vaskoafac7822020-10-20 14:22:26 +02003127 }
3128
3129 /* read closing tag */
3130 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3131 }
3132
3133cleanup:
3134 return ret;
3135}
3136
3137/**
3138 * @brief Parse instance of extension.
3139 *
3140 * @param[in,out] ctx Yin parser context for logging and to store current state.
Radek Krejcifc596f92021-02-26 22:40:26 +01003141 * @param[in] subelem The statement this extension instance is a subelement of.
Michal Vaskoafac7822020-10-20 14:22:26 +02003142 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
3143 * @param[in,out] exts Extension instance to add to.
3144 *
3145 * @return LY_ERR values.
3146 */
3147LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01003148yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, enum ly_stmt subelem, LY_ARRAY_COUNT_TYPE subelem_index,
Michal Vaskoafac7822020-10-20 14:22:26 +02003149 struct lysp_ext_instance **exts)
3150{
3151 struct lysp_ext_instance *e;
3152 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003153 char *ext_name;
Michal Vaskoafac7822020-10-20 14:22:26 +02003154
3155 assert(ctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci3aac9a72020-12-01 12:24:26 +01003156 assert(exts);
Michal Vaskoafac7822020-10-20 14:22:26 +02003157
3158 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3159
Michal Vaskofc2cd072021-02-24 13:17:17 +01003160 if (!ctx->xmlctx->prefix_len) {
3161 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.",
3162 (int)ctx->xmlctx->name_len, ctx->xmlctx->name);
3163 return LY_EVALID;
3164 }
3165
3166 /* store prefixed name */
3167 if (asprintf(&ext_name, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3168 ctx->xmlctx->name) == -1) {
3169 LOGMEM(ctx->xmlctx->ctx);
3170 return LY_EMEM;
3171 }
3172 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, ext_name, &e->name));
3173
3174 /* store prefix data for the name */
3175 LY_CHECK_RET(ly_store_prefix_data(ctx->xmlctx->ctx, e->name, strlen(e->name), LY_PREF_XML, &ctx->xmlctx->ns,
3176 &e->format, &e->prefix_data));
3177
Radek Krejciab430862021-03-02 20:13:40 +01003178 e->parent_stmt = subelem;
3179 e->parent_stmt_index = subelem_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003180
Michal Vaskoafac7822020-10-20 14:22:26 +02003181 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3182
3183 /* store attributes as subelements */
3184 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3185 if (!ctx->xmlctx->prefix) {
3186 new_subelem = calloc(1, sizeof(*new_subelem));
3187 if (!e->child) {
3188 e->child = new_subelem;
3189 } else {
3190 last_subelem->next = new_subelem;
3191 }
3192 last_subelem = new_subelem;
3193
3194 last_subelem->flags |= LYS_YIN_ATTR;
3195 LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3196 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3197 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3198
Michal Vaskofc2cd072021-02-24 13:17:17 +01003199 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic,
3200 last_subelem->arg);
Michal Vaskoafac7822020-10-20 14:22:26 +02003201 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3202 } else {
3203 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3204 }
3205
3206 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3207 }
3208
3209 /* parse subelements */
3210 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3211 if (ctx->xmlctx->ws_only) {
3212 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3213 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3214 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3215 if (!e->child) {
3216 e->child = new_subelem;
3217 } else {
3218 last_subelem->next = new_subelem;
3219 }
3220 last_subelem = new_subelem;
3221
3222 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3223 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3224 }
3225 } else if (ctx->xmlctx->value_len) {
3226 /* save text content */
3227 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
3228 LY_CHECK_RET(!e->argument, LY_EMEM);
3229
Michal Vaskofc2cd072021-02-24 13:17:17 +01003230 /* store prefix data for the argument as well */
3231 LY_CHECK_RET(ly_store_prefix_data(ctx->xmlctx->ctx, e->argument, strlen(e->argument), LY_PREF_XML,
Radek Krejci07a55962021-03-02 20:16:43 +01003232 &ctx->xmlctx->ns, &e->format, &e->prefix_data));
Michal Vaskofc2cd072021-02-24 13:17:17 +01003233
Michal Vaskoafac7822020-10-20 14:22:26 +02003234 /* parser next */
3235 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3236 }
3237
3238 return LY_SUCCESS;
3239}
3240
3241/**
3242 * @brief Generic function for content parsing
3243 *
3244 * @param[in,out] ctx Yin parser context for logging and to store current state.
3245 * @param[in] subelem_info array of valid subelement types and meta information
3246 * @param[in] subelem_info_size Size of subelem_info array.
3247 * @param[in] current_element Type of current element.
3248 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3249 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
3250 *
3251 * @return LY_ERR values.
3252 */
David Sedlákd6e56892019-07-01 15:40:24 +02003253LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003254yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Radek Krejci0f969882020-08-21 16:56:47 +02003255 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02003256{
3257 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01003258 enum LYXML_PARSER_STATUS next_status;
Radek Krejcid6b76452019-09-03 17:03:03 +02003259 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02003260 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02003261
Michal Vaskob36053d2020-03-26 15:49:30 +01003262 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02003263
Michal Vaskob36053d2020-03-26 15:49:30 +01003264 if (ctx->xmlctx->ws_only) {
3265 /* check whether there are any children */
3266 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3267 } else {
3268 /* we want to parse the value */
3269 next_status = LYXML_ELEM_CLOSE;
3270 }
3271
3272 if (next_status == LYXML_ELEMENT) {
3273 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3274
3275 /* current element has subelements as content */
3276 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3277 /* match keyword */
3278 last_kw = kw;
3279 kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003280 ctx->xmlctx->prefix_len, current_element);
Michal Vaskob36053d2020-03-26 15:49:30 +01003281
3282 /* check if this element can be child of current element */
3283 subelem = get_record(kw, subelem_info_size, subelem_info);
3284 if (!subelem) {
Michal Vasko69730152020-10-09 16:30:07 +02003285 if ((current_element == LY_STMT_DEVIATE) && isdevsub(kw)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003286 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
3287 } else {
3288 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +02003289 ctx->xmlctx->name, ly_stmt2str(current_element));
Michal Vaskob36053d2020-03-26 15:49:30 +01003290 }
3291 ret = LY_EVALID;
3292 goto cleanup;
3293 }
3294
3295 /* relative order is required only in module and submodule sub-elements */
Michal Vasko69730152020-10-09 16:30:07 +02003296 if ((current_element == LY_STMT_MODULE) || (current_element == LY_STMT_SUBMODULE)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003297 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
3298 LY_CHECK_GOTO(ret, cleanup);
3299 }
3300
3301 /* flag check */
3302 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3303 /* subelement uniquenes */
3304 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
3305 return LY_EVALID;
3306 }
3307 if (subelem->flags & YIN_SUBELEM_FIRST) {
3308 /* subelement is supposed to be defined as first subelement */
3309 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
3310 LY_CHECK_GOTO(ret, cleanup);
3311 }
3312 if (subelem->flags & YIN_SUBELEM_VER2) {
3313 /* subelement is supported only in version 1.1 or higher */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003314 if (ctx->parsed_mod->version < LYS_VERSION_1_1) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003315 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003316 ret = LY_EVALID;
3317 goto cleanup;
3318 }
David Sedlákd6e56892019-07-01 15:40:24 +02003319 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003320 /* note that element was parsed for easy uniqueness check in next iterations */
3321 subelem->flags |= YIN_SUBELEM_PARSED;
3322
3323 switch (kw) {
3324 /* call responsible function */
3325 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003326 ret = yin_parse_extension_instance(ctx, current_element,
Michal Vasko69730152020-10-09 16:30:07 +02003327 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003328 break;
3329 case LY_STMT_ACTION:
3330 case LY_STMT_RPC:
3331 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3332 break;
3333 case LY_STMT_ANYDATA:
3334 case LY_STMT_ANYXML:
3335 ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
3336 break;
3337 case LY_STMT_ARGUMENT:
3338 ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
3339 break;
3340 case LY_STMT_AUGMENT:
3341 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3342 break;
3343 case LY_STMT_BASE:
3344 ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
3345 break;
3346 case LY_STMT_BELONGS_TO:
3347 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3348 break;
3349 case LY_STMT_BIT:
3350 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3351 break;
3352 case LY_STMT_CASE:
3353 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3354 break;
3355 case LY_STMT_CHOICE:
3356 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3357 break;
3358 case LY_STMT_CONFIG:
3359 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3360 break;
3361 case LY_STMT_CONTACT:
3362 case LY_STMT_DESCRIPTION:
3363 case LY_STMT_ORGANIZATION:
3364 case LY_STMT_REFERENCE:
3365 ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
3366 break;
3367 case LY_STMT_CONTAINER:
3368 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3369 break;
3370 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003371 ret = yin_parse_qname(ctx, kw, subelem, exts);
3372 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003373 case LY_STMT_ERROR_APP_TAG:
3374 case LY_STMT_KEY:
3375 case LY_STMT_PRESENCE:
3376 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3377 break;
3378 case LY_STMT_DEVIATE:
3379 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3380 break;
3381 case LY_STMT_DEVIATION:
3382 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3383 break;
3384 case LY_STMT_ENUM:
3385 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3386 break;
3387 case LY_STMT_ERROR_MESSAGE:
3388 ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
3389 break;
3390 case LY_STMT_EXTENSION:
3391 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3392 break;
3393 case LY_STMT_FEATURE:
3394 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3395 break;
3396 case LY_STMT_FRACTION_DIGITS:
3397 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3398 break;
3399 case LY_STMT_GROUPING:
3400 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3401 break;
3402 case LY_STMT_IDENTITY:
3403 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3404 break;
3405 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003406 ret = yin_parse_qname(ctx, kw, subelem, exts);
3407 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003408 case LY_STMT_UNITS:
3409 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
3410 break;
3411 case LY_STMT_IMPORT:
3412 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3413 break;
3414 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01003415 if ((current_element == LY_STMT_SUBMODULE) && (ctx->parsed_mod->version == LYS_VERSION_1_1)) {
3416 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
3417 ((struct lysp_submodule *)(ctx->parsed_mod))->name);
3418 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003419 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3420 break;
3421 case LY_STMT_INPUT:
3422 case LY_STMT_OUTPUT:
3423 ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
3424 break;
3425 case LY_STMT_LEAF:
3426 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3427 break;
3428 case LY_STMT_LEAF_LIST:
3429 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3430 break;
3431 case LY_STMT_LENGTH:
3432 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3433 break;
3434 case LY_STMT_LIST:
3435 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3436 break;
3437 case LY_STMT_MANDATORY:
3438 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3439 break;
3440 case LY_STMT_MAX_ELEMENTS:
3441 case LY_STMT_MIN_ELEMENTS:
3442 ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
3443 break;
3444 case LY_STMT_MODIFIER:
3445 ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
3446 break;
3447 case LY_STMT_MUST:
3448 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3449 break;
3450 case LY_STMT_NAMESPACE:
3451 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
3452 break;
3453 case LY_STMT_NOTIFICATION:
3454 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3455 break;
3456 case LY_STMT_ORDERED_BY:
3457 ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
3458 break;
3459 case LY_STMT_PATH:
3460 ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
3461 break;
3462 case LY_STMT_PATTERN:
3463 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3464 break;
3465 case LY_STMT_VALUE:
3466 case LY_STMT_POSITION:
3467 ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
3468 break;
3469 case LY_STMT_PREFIX:
3470 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
3471 break;
3472 case LY_STMT_RANGE:
3473 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3474 break;
3475 case LY_STMT_REFINE:
3476 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3477 break;
3478 case LY_STMT_REQUIRE_INSTANCE:
3479 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3480 break;
3481 case LY_STMT_REVISION:
3482 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3483 break;
3484 case LY_STMT_REVISION_DATE:
3485 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3486 break;
3487 case LY_STMT_STATUS:
3488 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3489 break;
3490 case LY_STMT_TYPE:
3491 ret = yin_parse_type(ctx, current_element, subelem);
3492 break;
3493 case LY_STMT_TYPEDEF:
3494 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3495 break;
3496 case LY_STMT_UNIQUE:
3497 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
3498 break;
3499 case LY_STMT_USES:
3500 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3501 break;
3502 case LY_STMT_WHEN:
3503 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3504 break;
3505 case LY_STMT_YANG_VERSION:
3506 ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
3507 break;
3508 case LY_STMT_YIN_ELEMENT:
3509 ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
3510 break;
3511 case LY_STMT_ARG_TEXT:
3512 case LY_STMT_ARG_VALUE:
3513 /* TODO what to do with content/attributes? */
3514 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3515 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3516 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3517 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3518 }
3519 ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
3520 break;
3521 default:
3522 LOGINT(ctx->xmlctx->ctx);
3523 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003524 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003525 LY_CHECK_GOTO(ret, cleanup);
3526 subelem = NULL;
3527
3528 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003529 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003530 } else {
3531 LY_CHECK_RET(ret);
3532 /* elements with text or none content */
3533 /* save text content, if text_content isn't set, it's just ignored */
3534 /* no resources are allocated in this branch, no need to use cleanup label */
3535 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3536 if (text_content) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003537 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *text_content);
Michal Vaskob36053d2020-03-26 15:49:30 +01003538 LY_CHECK_RET(!*text_content, LY_EMEM);
3539 }
3540
3541 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003542 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003543
David Sedlák8b754462019-07-25 16:22:13 +02003544 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3545 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003546
3547cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003548 return ret;
3549}
3550
David Sedlákaa98bba2019-09-12 11:52:14 +02003551/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003552 * @brief Parse module element.
David Sedlákaa98bba2019-09-12 11:52:14 +02003553 *
3554 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vaskoafac7822020-10-20 14:22:26 +02003555 * @param[out] mod Parsed module structure.
David Sedlákaa98bba2019-09-12 11:52:14 +02003556 *
3557 * @return LY_ERR values.
3558 */
David Sedlák1e2cdd02019-06-27 14:17:43 +02003559LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003560yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003561{
David Sedlák81497a32019-08-13 16:56:26 +02003562 LY_ERR ret = LY_SUCCESS;
3563 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003564 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003565 size_t subelems_size;
David Sedlák3b4db242018-10-19 16:11:01 +02003566
Michal Vaskoc3781c32020-10-06 14:04:08 +02003567 mod->is_submod = 0;
3568
Michal Vaskob36053d2020-03-26 15:49:30 +01003569 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3570 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01003571 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003572 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3573 LY_STMT_ANYXML, &mod->data, 0,
3574 LY_STMT_AUGMENT, &mod->augments, 0,
3575 LY_STMT_CHOICE, &mod->data, 0,
3576 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3577 LY_STMT_CONTAINER, &mod->data, 0,
3578 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3579 LY_STMT_DEVIATION, &mod->deviations, 0,
3580 LY_STMT_EXTENSION, &mod->extensions, 0,
3581 LY_STMT_FEATURE, &mod->features, 0,
3582 LY_STMT_GROUPING, &mod->groupings, 0,
3583 LY_STMT_IDENTITY, &mod->identities, 0,
3584 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3585 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3586 LY_STMT_LEAF, &mod->data, 0,
3587 LY_STMT_LEAF_LIST, &mod->data, 0,
3588 LY_STMT_LIST, &mod->data, 0,
3589 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3590 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3591 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3592 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3593 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3594 LY_STMT_REVISION, &mod->revs, 0,
3595 LY_STMT_RPC, &mod->rpcs, 0,
3596 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3597 LY_STMT_USES, &mod->data, 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003598 LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
Michal Vasko69730152020-10-09 16:30:07 +02003599 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003600
Radek Krejcif13b87b2020-12-01 22:02:17 +01003601 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_MODULE, NULL, &mod->exts);
3602 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003603 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003604
David Sedláked809f12019-09-13 10:15:11 +02003605 /* submodules share the namespace with the module names, so there must not be
3606 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003607 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003608 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01003609 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003610 return LY_EVALID;
3611 }
3612
3613 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003614}
3615
Michal Vaskoafac7822020-10-20 14:22:26 +02003616/**
3617 * @brief Parse submodule element.
3618 *
3619 * @param[in,out] ctx Yin parser context for logging and to store current state.
3620 * @param[in] mod_attrs Attributes of submodule element.
3621 * @param[out] submod Parsed submodule structure.
3622 *
3623 * @return LY_ERR values.
3624 */
David Sedlák3b4db242018-10-19 16:11:01 +02003625LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003626yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003627{
David Sedlák81497a32019-08-13 16:56:26 +02003628 LY_ERR ret = LY_SUCCESS;
3629 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003630 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003631 size_t subelems_size;
David Sedlák298ff6d2019-07-26 14:29:03 +02003632
Michal Vaskoc3781c32020-10-06 14:04:08 +02003633 submod->is_submod = 1;
3634
Michal Vaskob36053d2020-03-26 15:49:30 +01003635 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3636 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE));
Radek Krejcif13b87b2020-12-01 22:02:17 +01003637 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003638 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3639 LY_STMT_ANYXML, &submod->data, 0,
3640 LY_STMT_AUGMENT, &submod->augments, 0,
3641 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3642 LY_STMT_CHOICE, &submod->data, 0,
3643 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3644 LY_STMT_CONTAINER, &submod->data, 0,
3645 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3646 LY_STMT_DEVIATION, &submod->deviations, 0,
3647 LY_STMT_EXTENSION, &submod->extensions, 0,
3648 LY_STMT_FEATURE, &submod->features, 0,
3649 LY_STMT_GROUPING, &submod->groupings, 0,
3650 LY_STMT_IDENTITY, &submod->identities, 0,
3651 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3652 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3653 LY_STMT_LEAF, &submod->data, 0,
3654 LY_STMT_LEAF_LIST, &submod->data, 0,
3655 LY_STMT_LIST, &submod->data, 0,
3656 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3657 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3658 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3659 LY_STMT_REVISION, &submod->revs, 0,
3660 LY_STMT_RPC, &submod->rpcs, 0,
3661 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3662 LY_STMT_USES, &submod->data, 0,
3663 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3664 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003665
Radek Krejcif13b87b2020-12-01 22:02:17 +01003666 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_SUBMODULE, NULL, &submod->exts);
3667 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003668 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003669
David Sedláked809f12019-09-13 10:15:11 +02003670 /* submodules share the namespace with the module names, so there must not be
3671 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003672 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003673 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01003674 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
David Sedláked809f12019-09-13 10:15:11 +02003675 return LY_EVALID;
3676 }
3677
3678 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003679}
3680
3681LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003682yin_parse_submodule(struct lys_yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx,
Radek Krejci0f969882020-08-21 16:56:47 +02003683 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003684{
Radek Krejcid6b76452019-09-03 17:03:03 +02003685 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003686 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003687 struct lysp_submodule *mod_p = NULL;
3688
3689 /* create context */
3690 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003691 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003692 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003693 (*yin_ctx)->unres = main_ctx->unres;
Michal Vasko63f3d842020-07-08 10:10:14 +02003694 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003695
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003696 mod_p = calloc(1, sizeof *mod_p);
3697 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3698 mod_p->mod = main_ctx->parsed_mod->mod;
3699 mod_p->parsing = 1;
3700 (*yin_ctx)->parsed_mod = (struct lysp_module *)mod_p;
3701
David Sedlák1b623122019-08-05 15:27:49 +02003702 /* map the typedefs and groupings list from main context to the submodule's context */
3703 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3704 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3705
David Sedlák8985a142019-07-31 16:43:06 +02003706 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003707 kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003708 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003709 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003710 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3711 ret = LY_EINVAL;
3712 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003713 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003714 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003715 ret = LY_EVALID;
3716 goto cleanup;
3717 }
3718
Michal Vaskob36053d2020-03-26 15:49:30 +01003719 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003720 LY_CHECK_GOTO(ret, cleanup);
3721
David Sedlákbf8a2b72019-08-14 16:48:10 +02003722 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003723 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003724 if (in->current[0] == '\n') {
3725 LY_IN_NEW_LINE(in);
3726 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003727 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003728 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003729 if (in->current[0]) {
3730 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003731 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003732 ret = LY_EVALID;
3733 goto cleanup;
3734 }
3735
David Sedlák8985a142019-07-31 16:43:06 +02003736 mod_p->parsing = 0;
3737 *submod = mod_p;
3738
3739cleanup:
3740 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003741 lysp_module_free((struct lysp_module *)mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003742 yin_parser_ctx_free(*yin_ctx);
3743 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003744 }
David Sedlák8985a142019-07-31 16:43:06 +02003745 return ret;
3746}
3747
3748LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01003749yin_parse_module(struct lys_yin_parser_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod, struct lys_glob_unres *unres)
David Sedlák3b4db242018-10-19 16:11:01 +02003750{
David Sedláke4889912018-11-02 09:52:40 +01003751 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003752 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003753 struct lysp_module *mod_p = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003754
David Sedlák8985a142019-07-31 16:43:06 +02003755 /* create context */
3756 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003757 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003758 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003759 (*yin_ctx)->unres = unres;
Michal Vasko63f3d842020-07-08 10:10:14 +02003760 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
David Sedlákda8ffa32019-07-08 14:17:10 +02003761
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003762 mod_p = calloc(1, sizeof *mod_p);
3763 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3764 mod_p->mod = mod;
3765 mod_p->parsing = 1;
3766 (*yin_ctx)->parsed_mod = mod_p;
3767
David Sedlák8985a142019-07-31 16:43:06 +02003768 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003769 kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003770 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003771 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003772 LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
David Sedlák3017da42019-02-15 09:48:04 +01003773 ret = LY_EINVAL;
3774 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003775 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003776 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003777 ret = LY_EVALID;
3778 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003779 }
3780
David Sedlák00250342019-06-21 14:19:39 +02003781 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003782 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003783 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003784
David Sedlákbf8a2b72019-08-14 16:48:10 +02003785 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003786 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003787 if (in->current[0] == '\n') {
3788 LY_IN_NEW_LINE(in);
3789 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003790 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003791 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003792 if (in->current[0]) {
3793 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003794 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003795 ret = LY_EVALID;
3796 goto cleanup;
3797 }
3798
David Sedlák3017da42019-02-15 09:48:04 +01003799 mod_p->parsing = 0;
3800 mod->parsed = mod_p;
3801
3802cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003803 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003804 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003805 yin_parser_ctx_free(*yin_ctx);
3806 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003807 }
David Sedlák2e411422018-12-17 02:35:39 +01003808 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003809}