blob: c98102c719f4459fb2396432fbf594b570db06af [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
424 switch (val_type) {
425 case Y_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200426 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 +0200427 break;
428 case Y_PREF_IDENTIF_ARG:
Radek Krejci1deb5be2020-08-26 16:43:36 +0200429 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 +0200430 break;
431 case Y_STR_ARG:
432 case Y_MAYBE_STR_ARG:
433 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
434 break;
435 }
Radek Iša2eac74c2021-03-15 15:53:08 +0100436
437 already_read += utf8_char_len;
438 LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
David Sedlák4a650532019-07-10 11:55:18 +0200439 }
440
Radek Iša42cc0b72021-03-15 16:51:31 +0100441 if ((already_read == 0) &&
442 ((val_type == Y_PREF_IDENTIF_ARG) || (val_type == Y_IDENTIF_ARG))) {
443 /* Empty identifier or prefix*/
444 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Empty identifier is not allowed.");
445 return LY_EVALID;
446 }
447
David Sedlák4a650532019-07-10 11:55:18 +0200448 return LY_SUCCESS;
449}
450
David Sedlákb4e44562019-07-04 15:42:12 +0200451/**
Michal Vaskob36053d2020-03-26 15:49:30 +0100452 * @brief Parse yin attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200453 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200454 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák4a650532019-07-10 11:55:18 +0200455 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
456 * special argument).
David Sedlákbf8a2b72019-08-14 16:48:10 +0200457 * @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 +0200458 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200459 * @param[in] current_element Identification of current element, used for logging.
460 *
461 * @return LY_ERR values.
462 */
463static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100464yin_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 +0200465 enum ly_stmt current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200466{
David Sedlákc5b20842019-08-13 10:18:31 +0200467 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák619db942019-07-03 14:47:30 +0200468 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200469
David Sedlák1bccdfa2019-06-17 15:55:27 +0200470 /* validation of attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +0100471 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
David Sedlák00250342019-06-21 14:19:39 +0200472 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
Michal Vaskob36053d2020-03-26 15:49:30 +0100473 if (!ctx->xmlctx->prefix) {
474 arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200475 if (arg == YIN_ARG_NONE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100476 /* skip it */
477 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedlák7ff55a92019-06-17 11:11:41 +0200478 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200479 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
Michal Vasko69730152020-10-09 16:30:07 +0200480 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200481 found = true;
Michal Vaskob36053d2020-03-26 15:49:30 +0100482
483 /* go to value */
484 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
485 LY_CHECK_RET(yin_validate_value(ctx, val_type));
Radek Krejci011e4aa2020-09-04 15:22:31 +0200486 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 +0200487 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák2b214ac2019-06-06 16:11:03 +0200488 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100489 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +0200490 ctx->xmlctx->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200491 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200492 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100493 } else {
494 /* skip it */
495 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200496 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100497
498 /* next attribute */
499 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
David Sedláka7406952019-04-05 10:33:07 +0200500 }
501
David Sedlák292763b2019-07-09 11:10:53 +0200502 /* anything else than Y_MAYBE_STR_ARG is mandatory */
Michal Vasko69730152020-10-09 16:30:07 +0200503 if ((val_type != Y_MAYBE_STR_ARG) && !found) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100504 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
Michal Vasko69730152020-10-09 16:30:07 +0200505 yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200506 return LY_EVALID;
507 }
508
509 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200510}
511
David Sedlákd6e56892019-07-01 15:40:24 +0200512/**
Radek Krejci12b1c152019-09-05 16:20:48 +0200513 * @brief Get record with given type.
David Sedlákd6e56892019-07-01 15:40:24 +0200514 *
515 * @param[in] type Type of wanted record.
516 * @param[in] array_size Size of array.
517 * @param[in] array Searched array.
518 *
519 * @return Pointer to desired record on success, NULL if element is not in the array.
520 */
David Sedlákb4e44562019-07-04 15:42:12 +0200521static struct yin_subelement *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200522get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200523{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200524 for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
Radek Krejci12b1c152019-09-05 16:20:48 +0200525 if (array[u].type == type) {
526 return &array[u];
David Sedlákd6e56892019-07-01 15:40:24 +0200527 }
528 }
David Sedlákd6e56892019-07-01 15:40:24 +0200529 return NULL;
530}
531
David Sedlákbba38e52019-07-09 15:20:01 +0200532/**
533 * @brief Helper function to check mandatory constraint of subelement.
534 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200535 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200536 * @param[in] subelem_info Array of information about subelements.
537 * @param[in] subelem_info_size Size of subelem_info array.
538 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
539 *
540 * @return LY_ERR values.
541 */
542static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100543yin_check_subelem_mandatory_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200544 signed char subelem_info_size, enum ly_stmt current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200545{
David Sedlákb0faad82019-07-04 14:28:59 +0200546 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200547 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
Radek Krejci12b1c152019-09-05 16:20:48 +0200548 if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200549 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200550 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200551 return LY_EVALID;
552 }
553 }
554
555 return LY_SUCCESS;
556}
557
David Sedlákbba38e52019-07-09 15:20:01 +0200558/**
559 * @brief Helper function to check "first" constraint of subelement.
560 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200561 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200562 * @param[in] subelem_info Array of information about subelements.
563 * @param[in] subelem_info_size Size of subelem_info array.
564 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200565 * @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 +0200566 *
567 * @return LY_ERR values.
568 */
569static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100570yin_check_subelem_first_constraint(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
Radek Krejci0f969882020-08-21 16:56:47 +0200571 signed char subelem_info_size, enum ly_stmt current_element,
572 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200573{
David Sedlákb0faad82019-07-04 14:28:59 +0200574 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200575 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200576 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
Michal Vasko69730152020-10-09 16:30:07 +0200577 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200578 return LY_EVALID;
579 }
580 }
581
582 return LY_SUCCESS;
583}
584
David Sedlákbba38e52019-07-09 15:20:01 +0200585/**
David Sedlákb4e44562019-07-04 15:42:12 +0200586 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
587 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200588 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200589 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200590 * @param[in] kw Type of current element.
591 * @param[out] value Where value of attribute should be stored.
592 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200593 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200594 * @param[in,out] exts Extension instances to add to.
David Sedlákd6e56892019-07-01 15:40:24 +0200595 * @return LY_ERR values.
596 */
David Sedlákb4e44562019-07-04 15:42:12 +0200597static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100598yin_parse_simple_element(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, const char **value,
Radek Krejci0f969882020-08-21 16:56:47 +0200599 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200600{
Michal Vaskob36053d2020-03-26 15:49:30 +0100601 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
602 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
Radek Krejcif13b87b2020-12-01 22:02:17 +0100603 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200604 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
605 };
David Sedlákb4e44562019-07-04 15:42:12 +0200606
Radek Krejcif13b87b2020-12-01 22:02:17 +0100607 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200608}
609
610/**
David Sedlák6542aed2019-08-14 10:47:43 +0200611 * @brief Parse path element.
612 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200613 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200614 * @param[in] kw Type of current element.
615 * @param[out] type Type structure to store parsed value, flags and extension instances.
David Sedlák6542aed2019-08-14 10:47:43 +0200616 * @return LY_ERR values.
617 */
618static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100619yin_parse_path(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200620{
Michal Vasko004d3152020-06-11 19:59:22 +0200621 LY_ERR ret;
622 const char *str_path;
623
624 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 +0200625 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 +0200626 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
Michal Vasko004d3152020-06-11 19:59:22 +0200627 lydict_remove(ctx->xmlctx->ctx, str_path);
628 LY_CHECK_RET(ret);
David Sedlák6542aed2019-08-14 10:47:43 +0200629 type->flags |= LYS_SET_PATH;
630
631 return LY_SUCCESS;
632}
633
634/**
David Sedlákd3983112019-07-12 11:20:56 +0200635 * @brief Parse pattern element.
636 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200637 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200638 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlákd3983112019-07-12 11:20:56 +0200639 * @return LY_ERR values.
640 */
641static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100642yin_parse_pattern(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákd3983112019-07-12 11:20:56 +0200643{
644 const char *real_value = NULL;
645 char *saved_value = NULL;
646 struct lysp_restr *restr;
647
Michal Vaskob36053d2020-03-26 15:49:30 +0100648 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
649 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
650 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 +0200651 size_t len = strlen(real_value);
652
653 saved_value = malloc(len + 2);
Michal Vaskob36053d2020-03-26 15:49:30 +0100654 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200655 memmove(saved_value + 1, real_value, len);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100656 lydict_remove(ctx->xmlctx->ctx, real_value);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100657 saved_value[0] = LYSP_RESTR_PATTERN_ACK;
David Sedlákd3983112019-07-12 11:20:56 +0200658 saved_value[len + 1] = '\0';
Michal Vasko7f45cf22020-10-01 12:49:44 +0200659 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200660 restr->arg.mod = ctx->parsed_mod;
David Sedlákd3983112019-07-12 11:20:56 +0200661 type->flags |= LYS_SET_PATTERN;
662
Radek Krejcif13b87b2020-12-01 22:02:17 +0100663 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200664 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
665 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
666 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
667 {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
668 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
669 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
670 };
Michal Vasko69730152020-10-09 16:30:07 +0200671
Radek Krejcif13b87b2020-12-01 22:02:17 +0100672 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_PATTERN, NULL, &restr->exts);
David Sedlákd3983112019-07-12 11:20:56 +0200673}
674
David Sedlákc5b20842019-08-13 10:18:31 +0200675/**
676 * @brief Parse fraction-digits element.
677 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200678 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc5b20842019-08-13 10:18:31 +0200679 * @param[in,out] type Type structure to store value, flags and extension instances.
David Sedlákc5b20842019-08-13 10:18:31 +0200680 * @return LY_ERR values.
681 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200682static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100683yin_parse_fracdigits(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákf75d55e2019-07-12 16:52:50 +0200684{
685 const char *temp_val = NULL;
686 char *ptr;
687 unsigned long int num;
688
Michal Vaskob36053d2020-03-26 15:49:30 +0100689 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
690 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 +0200691
Michal Vasko69730152020-10-09 16:30:07 +0200692 if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[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 }
697
698 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100699 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlákf75d55e2019-07-12 16:52:50 +0200700 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200701 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100702 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200703 return LY_EVALID;
704 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100705 if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +0200706 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100707 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200708 return LY_EVALID;
709 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100710 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200711 type->fraction_digits = num;
712 type->flags |= LYS_SET_FRDIGITS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100713 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200714 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
715 };
Michal Vasko69730152020-10-09 16:30:07 +0200716
Radek Krejcif13b87b2020-12-01 22:02:17 +0100717 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FRACTION_DIGITS, NULL, &type->exts);
David Sedlákf75d55e2019-07-12 16:52:50 +0200718}
719
David Sedlák07869a52019-07-12 14:28:19 +0200720/**
David Sedlák43801c92019-08-05 15:58:54 +0200721 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200722 *
723 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200724 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200725 * @return LY_ERR values.
726 */
David Sedlákca36c422019-07-12 12:47:55 +0200727static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100728yin_parse_enum(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200729{
David Sedlák07869a52019-07-12 14:28:19 +0200730 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200731
Michal Vaskob36053d2020-03-26 15:49:30 +0100732 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200733 type->flags |= LYS_SET_ENUM;
Michal Vaskob36053d2020-03-26 15:49:30 +0100734 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
735 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 +0200736 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
Michal Vaskob36053d2020-03-26 15:49:30 +0100737 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák43801c92019-08-05 15:58:54 +0200738 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200739
Radek Krejcif13b87b2020-12-01 22:02:17 +0100740 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200741 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
742 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
743 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
744 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
745 {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
746 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
747 };
Michal Vasko69730152020-10-09 16:30:07 +0200748
Radek Krejcif13b87b2020-12-01 22:02:17 +0100749 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ENUM, NULL, &en->exts);
David Sedlák43801c92019-08-05 15:58:54 +0200750}
751
752/**
753 * @brief Parse bit element.
754 *
755 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200756 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200757 * @return LY_ERR values.
758 */
759static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100760yin_parse_bit(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák43801c92019-08-05 15:58:54 +0200761{
762 struct lysp_type_enum *en;
763
Michal Vaskob36053d2020-03-26 15:49:30 +0100764 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
David Sedlák43801c92019-08-05 15:58:54 +0200765 type->flags |= LYS_SET_BIT;
Michal Vaskob36053d2020-03-26 15:49:30 +0100766 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
767 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
Radek Iša8b640882021-03-12 17:59:34 +0100768 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->bits, name, "bit", en->name);
David Sedlák43801c92019-08-05 15:58:54 +0200769
Radek Krejcif13b87b2020-12-01 22:02:17 +0100770 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200771 {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
772 {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
773 {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
774 {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
775 {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
776 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
777 };
Michal Vasko69730152020-10-09 16:30:07 +0200778
Radek Krejcif13b87b2020-12-01 22:02:17 +0100779 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200780}
781
David Sedlákd3983112019-07-12 11:20:56 +0200782/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200783 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
784 * more instances, such as base or if-feature.
785 *
786 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5f8191e2019-07-08 16:35:52 +0200787 * @param[in] kw Type of current element.
788 * @param[out] values Parsed values to add to.
789 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200790 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200791 * @param[in,out] exts Extension instances to add to.
David Sedlák5f8191e2019-07-08 16:35:52 +0200792 * @return LY_ERR values.
793 */
794static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100795yin_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 +0200796 enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200797{
798 const char **value;
Michal Vasko69730152020-10-09 16:30:07 +0200799
Michal Vaskob36053d2020-03-26 15:49:30 +0100800 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200801 LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values) - 1;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100802 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200803 {LY_STMT_EXTENSION_INSTANCE, &index, 0}
804 };
David Sedlák968ac342019-07-11 15:17:59 +0200805
Michal Vaskob36053d2020-03-26 15:49:30 +0100806 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
807 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200808
Radek Krejcif13b87b2020-12-01 22:02:17 +0100809 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, exts);
David Sedlák5f8191e2019-07-08 16:35:52 +0200810}
811
812/**
David Sedlák6542aed2019-08-14 10:47:43 +0200813 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
814 *
815 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200816 * @param[in] kw Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200817 * @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 +0200818 * @param[in] arg_type Expected type of attribute.
819 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200820 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200821 * @return LY_ERR values.
822 */
823static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100824yin_parse_simple_elem(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
Radek Krejci0f969882020-08-21 16:56:47 +0200825 enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák6542aed2019-08-14 10:47:43 +0200826{
827 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100828 LY_CHECK_RET(yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200829 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200830 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100831 LY_CHECK_RET(yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest,
Michal Vasko69730152020-10-09 16:30:07 +0200832 arg_type, arg_val_type, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200833 }
834
835 return LY_SUCCESS;
836}
837
838/**
839 * @brief Parse base element.
840 *
841 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200842 * @param[in] parent Identification of parent element.
843 * @param[out] dest Where parsed values should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200844 * @param[in,out] exts Extension instances to add to.
David Sedlák6542aed2019-08-14 10:47:43 +0200845 * @return LY_ERR values.
846 */
847static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100848yin_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 +0200849{
850 struct lysp_type *type = NULL;
851
Radek Krejcid6b76452019-09-03 17:03:03 +0200852 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +0200853 type = (struct lysp_type *)dest;
Michal Vaskob36053d2020-03-26 15:49:30 +0100854 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME,
Michal Vasko69730152020-10-09 16:30:07 +0200855 Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200856 type->flags |= LYS_SET_BASE;
Radek Krejcid6b76452019-09-03 17:03:03 +0200857 } else if (parent == LY_STMT_IDENTITY) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100858 LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest,
Michal Vasko69730152020-10-09 16:30:07 +0200859 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
David Sedlák6542aed2019-08-14 10:47:43 +0200860 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100861 LOGINT(ctx->xmlctx->ctx);
David Sedlák6542aed2019-08-14 10:47:43 +0200862 return LY_EINT;
863 }
864
865 return LY_SUCCESS;
866}
867
868/**
David Sedlákbf8a2b72019-08-14 16:48:10 +0200869 * @brief Parse require-instance element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200870 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200871 * @param[in,out] ctx YIN parser context for logging and to store current state.
Michal Vaskob36053d2020-03-26 15:49:30 +0100872 * @param[out] type Type structure to store value, flag and extensions.
David Sedlákcf5569a2019-07-11 13:31:34 +0200873 * @return LY_ERR values.
874 */
875static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100876yin_pasrse_reqinstance(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlákcf5569a2019-07-11 13:31:34 +0200877{
878 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100879 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200880 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
881 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200882
883 type->flags |= LYS_SET_REQINST;
Michal Vaskob36053d2020-03-26 15:49:30 +0100884 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
885 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 +0200886 if (strcmp(temp_val, "true") == 0) {
887 type->require_instance = 1;
888 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200889 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200890 "require-instance", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100891 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200892 return LY_EVALID;
893 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100894 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákcf5569a2019-07-11 13:31:34 +0200895
Radek Krejcif13b87b2020-12-01 22:02:17 +0100896 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts);
David Sedlákcf5569a2019-07-11 13:31:34 +0200897}
898
899/**
David Sedlákce77bf52019-07-11 16:59:31 +0200900 * @brief Parse modifier element.
901 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200902 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákce77bf52019-07-11 16:59:31 +0200903 * @param[in,out] pat Value to write to.
904 * @param[in,out] exts Extension instances to add to.
905 *
906 * @return LY_ERR values.
907 */
908static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100909yin_parse_modifier(struct lys_yin_parser_ctx *ctx, const char **pat, struct lysp_ext_instance **exts)
David Sedlákce77bf52019-07-11 16:59:31 +0200910{
David Sedlákd3983112019-07-12 11:20:56 +0200911 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200912 const char *temp_val;
913 char *modified_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100914 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200915 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
916 };
David Sedlákce77bf52019-07-11 16:59:31 +0200917
Michal Vaskob36053d2020-03-26 15:49:30 +0100918 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
919 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 +0200920 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200921 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +0200922 "modifier", "invert-match");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100923 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200924 return LY_EVALID;
925 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100926 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200927
928 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200929 modified_val = malloc(strlen(*pat) + 1);
Michal Vaskob36053d2020-03-26 15:49:30 +0100930 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200931 strcpy(modified_val, *pat);
Michal Vaskob36053d2020-03-26 15:49:30 +0100932 lydict_remove(ctx->xmlctx->ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200933
934 /* modify the new value */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100935 modified_val[0] = LYSP_RESTR_PATTERN_NACK;
Radek Krejci011e4aa2020-09-04 15:22:31 +0200936 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
David Sedlákce77bf52019-07-11 16:59:31 +0200937
Radek Krejcif13b87b2020-12-01 22:02:17 +0100938 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MODIFIER, NULL, exts);
David Sedlákce77bf52019-07-11 16:59:31 +0200939}
940
941/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200942 * @brief Parse a restriction element (length, range or one instance of must).
943 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200944 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +0200945 * @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 +0200946 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200947 */
948static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100949yin_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 +0200950{
Radek Krejcid6b76452019-09-03 17:03:03 +0200951 assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100952 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +0200953 {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
954 {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
955 {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
956 {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
957 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
958 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200959 /* argument of must is called condition, but argument of length and range is called value */
Radek Krejcid6b76452019-09-03 17:03:03 +0200960 enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200961
Michal Vaskob36053d2020-03-26 15:49:30 +0100962 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vasko7f45cf22020-10-01 12:49:44 +0200963 LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200964 restr->arg.mod = ctx->parsed_mod;
Michal Vaskob36053d2020-03-26 15:49:30 +0100965
Radek Krejcif13b87b2020-12-01 22:02:17 +0100966 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr_kw, NULL, &restr->exts);
David Sedlákb7296dd2019-07-11 14:58:38 +0200967}
968
969/**
David Sedlák6542aed2019-08-14 10:47:43 +0200970 * @brief Parse range element.
971 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200972 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200973 * @param[out] type Type structure to store parsed value and flags.
974 *
975 * @return LY_ERR values.
976 */
977static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100978yin_parse_range(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200979{
980 type->range = calloc(1, sizeof *type->range);
Michal Vaskob36053d2020-03-26 15:49:30 +0100981 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
982 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200983 type->flags |= LYS_SET_RANGE;
David Sedlák6542aed2019-08-14 10:47:43 +0200984
985 return LY_SUCCESS;
986}
987
988/**
989 * @brief Parse length element.
990 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200991 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +0200992 * @param[out] type Type structure to store parsed value and flags.
993 *
994 * @return LY_ERR values.
995 */
996static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100997yin_parse_length(struct lys_yin_parser_ctx *ctx, struct lysp_type *type)
David Sedlák6542aed2019-08-14 10:47:43 +0200998{
999 type->length = calloc(1, sizeof *type->length);
Michal Vaskob36053d2020-03-26 15:49:30 +01001000 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1001 LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
David Sedlák6542aed2019-08-14 10:47:43 +02001002 type->flags |= LYS_SET_LENGTH;
1003
1004 return LY_SUCCESS;
1005}
1006
1007/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001008 * @brief Parse must element.
1009 *
1010 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001011 * @param[in,out] restrs Restrictions to add to.
1012 *
1013 * @return LY_ERR values.
1014 */
1015static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001016yin_parse_must(struct lys_yin_parser_ctx *ctx, struct lysp_restr **restrs)
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001017{
1018 struct lysp_restr *restr;
1019
Michal Vaskob36053d2020-03-26 15:49:30 +01001020 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1021 return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001022}
1023
1024/**
Michal Vasko7f45cf22020-10-01 12:49:44 +02001025 * @brief Parse a node id into an array.
1026 *
1027 * @param[in,out] ctx YIN parser context for logging and to store current state.
1028 * @param[in] kw Type of current element.
1029 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1030 * @param[in,out] exts Extension instances to add to.
1031 *
1032 * @return LY_ERR values.
1033 */
1034static LY_ERR
1035yin_parse_qname(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, struct yin_subelement *subinfo,
1036 struct lysp_ext_instance **exts)
1037{
1038 struct lysp_qname *qname, **qnames;
1039
1040 switch (kw) {
1041 case LY_STMT_DEFAULT:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001042 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1043 qname = (struct lysp_qname *)subinfo->dest;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001044 } else {
1045 qnames = (struct lysp_qname **)subinfo->dest;
1046 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001047 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001048 qname->mod = ctx->parsed_mod;
1049 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
1050 case LY_STMT_IF_FEATURE:
1051 assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1052 qnames = (struct lysp_qname **)subinfo->dest;
1053 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1054 qname->mod = ctx->parsed_mod;
1055 return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001056 default:
1057 break;
1058 }
1059
1060 LOGINT(ctx->xmlctx->ctx);
1061 return LY_EINT;
1062}
1063
1064/**
David Sedlák5545f5d2019-07-11 11:55:16 +02001065 * @brief Parse position or value element.
1066 *
1067 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001068 * @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 +02001069 * @param[out] enm Enum structure to save value, flags and extension instances.
David Sedlák5545f5d2019-07-11 11:55:16 +02001070 *
1071 * @return LY_ERR values.
1072 */
1073static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001074yin_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 +02001075{
Radek Krejcid6b76452019-09-03 17:03:03 +02001076 assert(kw == LY_STMT_POSITION || kw == LY_STMT_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +02001077 const char *temp_val = NULL;
1078 char *ptr;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001079 long int num = 0;
1080 unsigned long int unum = 0;
David Sedlák5545f5d2019-07-11 11:55:16 +02001081
1082 /* set value flag */
1083 enm->flags |= LYS_SET_VALUE;
1084
1085 /* get attribute value */
Michal Vaskob36053d2020-03-26 15:49:30 +01001086 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1087 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
Michal Vasko69730152020-10-09 16:30:07 +02001088 if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
Radek Krejci0f969882020-08-21 16:56:47 +02001089 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +02001090 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 +02001091 goto error;
1092 }
1093
1094 /* convert value */
1095 errno = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02001096 if (kw == LY_STMT_VALUE) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001097 num = strtol(temp_val, &ptr, LY_BASE_DEC);
Michal Vasko69730152020-10-09 16:30:07 +02001098 if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
David Sedlák1538a842019-08-08 15:38:51 +02001099 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 +02001100 goto error;
1101 }
1102 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001103 unum = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák5545f5d2019-07-11 11:55:16 +02001104 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +02001105 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 +02001106 goto error;
1107 }
1108 }
1109 /* check if whole argument value was converted */
1110 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001111 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 +02001112 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +02001113 }
1114 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +02001115 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 +02001116 goto error;
1117 }
1118 /* save correctly ternary operator can't be used because num and unum have different signes */
Radek Krejcid6b76452019-09-03 17:03:03 +02001119 if (kw == LY_STMT_VALUE) {
David Sedlák5545f5d2019-07-11 11:55:16 +02001120 enm->value = num;
1121 } else {
1122 enm->value = unum;
1123 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001124 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák5545f5d2019-07-11 11:55:16 +02001125
1126 /* parse subelements */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001127 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001128 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1129 };
Michal Vasko69730152020-10-09 16:30:07 +02001130
Radek Krejcif13b87b2020-12-01 22:02:17 +01001131 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), kw, NULL, &enm->exts);
David Sedlák5545f5d2019-07-11 11:55:16 +02001132
David Sedlákbf8a2b72019-08-14 16:48:10 +02001133error:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001134 lydict_remove(ctx->xmlctx->ctx, temp_val);
Michal Vaskob36053d2020-03-26 15:49:30 +01001135 return LY_EVALID;
David Sedlák5545f5d2019-07-11 11:55:16 +02001136}
1137
David Sedlák05404f62019-07-24 14:11:53 +02001138/**
1139 * @brief Parse belongs-to element.
1140 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001141 * @param[in] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001142 * @param[out] submod Structure of submodule that is being parsed.
1143 * @param[in,out] exts Extension instances to add to.
1144 *
1145 * @return LY_ERR values
1146 */
1147static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001148yin_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 +02001149{
Michal Vaskoc3781c32020-10-06 14:04:08 +02001150 const char *belongsto;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001151 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001152 {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1153 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1154 };
David Sedlák05404f62019-07-24 14:11:53 +02001155
Michal Vaskob36053d2020-03-26 15:49:30 +01001156 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001157 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 +02001158 if (ctx->parsed_mod->mod->name != belongsto) {
Michal Vaskoc3781c32020-10-06 14:04:08 +02001159 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 +02001160 belongsto, ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02001161 lydict_remove(ctx->xmlctx->ctx, belongsto);
1162 return LY_EVALID;
1163 }
1164 lydict_remove(ctx->xmlctx->ctx, belongsto);
Michal Vaskob36053d2020-03-26 15:49:30 +01001165
Radek Krejcif13b87b2020-12-01 22:02:17 +01001166 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_BELONGS_TO, NULL, exts);
David Sedlák05404f62019-07-24 14:11:53 +02001167}
1168
David Sedlák5545f5d2019-07-11 11:55:16 +02001169/**
David Sedlákc1771b12019-07-10 15:55:46 +02001170 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákbf8a2b72019-08-14 16:48:10 +02001171 * text element as child.
David Sedlákb4e44562019-07-04 15:42:12 +02001172 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001173 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001174 * @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 +02001175 * @param[out] value Where the content of meta element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001176 * @param[in,out] exts Extension instances to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001177 *
1178 * @return LY_ERR values.
1179 */
1180static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001181yin_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 +02001182{
Radek Krejcid6b76452019-09-03 17:03:03 +02001183 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 +02001184
Radek Krejcif13b87b2020-12-01 22:02:17 +01001185 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001186 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1187 {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1188 };
Michal Vasko69730152020-10-09 16:30:07 +02001189
David Sedlákdf2a9732019-08-07 13:23:16 +02001190 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001191 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1192 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 +02001193
David Sedlákdf2a9732019-08-07 13:23:16 +02001194 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001195 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001196}
1197
1198/**
David Sedlákc1771b12019-07-10 15:55:46 +02001199 * @brief Parse error-message element.
1200 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001201 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákc1771b12019-07-10 15:55:46 +02001202 * @param[out] value Where the content of error-message element should be stored.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001203 * @param[in,out] exts Extension instances to add to.
David Sedlákc1771b12019-07-10 15:55:46 +02001204 *
1205 * @return LY_ERR values.
1206 */
1207static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001208yin_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 +02001209{
Radek Krejcif13b87b2020-12-01 22:02:17 +01001210 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001211 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1212 {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1213 };
David Sedlákc1771b12019-07-10 15:55:46 +02001214
David Sedlákdf2a9732019-08-07 13:23:16 +02001215 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001216 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1217 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 +02001218
Radek Krejcif13b87b2020-12-01 22:02:17 +01001219 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ERROR_MESSAGE, NULL, exts);
David Sedlákc1771b12019-07-10 15:55:46 +02001220}
1221
1222/**
David Sedlák6542aed2019-08-14 10:47:43 +02001223 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001224 *
1225 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák6542aed2019-08-14 10:47:43 +02001226 * @param[in] parent Identification of parent element.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001227 * @param[in,out] type Type to write to.
David Sedlák374d2b32019-07-17 15:06:55 +02001228 *
1229 * @return LY_ERR values.
1230 */
1231static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001232yin_parse_type(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001233{
David Sedlák6542aed2019-08-14 10:47:43 +02001234 struct lysp_type *type = NULL;
Michal Vasko69730152020-10-09 16:30:07 +02001235
Radek Krejcid6b76452019-09-03 17:03:03 +02001236 if (parent == LY_STMT_DEVIATE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001237 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
Michal Vaskob36053d2020-03-26 15:49:30 +01001238 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001239 type = *((struct lysp_type **)subinfo->dest);
Michal Vaskod989ba02020-08-24 10:59:24 +02001240 } else {
David Sedlák6542aed2019-08-14 10:47:43 +02001241 type = (struct lysp_type *)subinfo->dest;
1242 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001243
David Sedlák6542aed2019-08-14 10:47:43 +02001244 /* type as child of another type */
Radek Krejcid6b76452019-09-03 17:03:03 +02001245 if (parent == LY_STMT_TYPE) {
David Sedlák6542aed2019-08-14 10:47:43 +02001246 struct lysp_type *nested_type = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001247 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
David Sedlák6542aed2019-08-14 10:47:43 +02001248 type->flags |= LYS_SET_TYPE;
1249 type = nested_type;
1250 }
Michal Vaskoe9c050f2020-10-06 14:01:23 +02001251
Michal Vasko5bbcc242021-02-23 09:29:13 +01001252 type->pmod = ctx->parsed_mod;
1253
Radek Krejcif13b87b2020-12-01 22:02:17 +01001254 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001255 {LY_STMT_BASE, type, 0},
1256 {LY_STMT_BIT, type, 0},
1257 {LY_STMT_ENUM, type, 0},
1258 {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1259 {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1260 {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1261 {LY_STMT_PATTERN, type, 0},
1262 {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1263 {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1264 {LY_STMT_TYPE, type},
1265 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1266 };
Michal Vaskob36053d2020-03-26 15:49:30 +01001267
1268 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1269 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 +01001270 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_TYPE, NULL, &type->exts);
David Sedlák374d2b32019-07-17 15:06:55 +02001271}
1272
David Sedlák1af868e2019-07-17 17:03:14 +02001273/**
1274 * @brief Parse max-elements element.
1275 *
1276 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák1af868e2019-07-17 17:03:14 +02001277 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001278 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001279 * @param[in,out] exts Extension instances to add to.
1280 *
1281 * @return LY_ERR values.
1282 */
1283static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001284yin_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 +02001285{
1286 const char *temp_val = NULL;
1287 char *ptr;
1288 unsigned long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001289 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001290 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1291 };
David Sedlák374d2b32019-07-17 15:06:55 +02001292
David Sedlák1af868e2019-07-17 17:03:14 +02001293 *flags |= LYS_SET_MAX;
Michal Vaskob36053d2020-03-26 15:49:30 +01001294 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1295 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 +02001296 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 +02001297 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001298 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001299 return LY_EVALID;
1300 }
1301
1302 if (strcmp(temp_val, "unbounded")) {
1303 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001304 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák1af868e2019-07-17 17:03:14 +02001305 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001306 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001307 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001308 return LY_EVALID;
1309 }
1310 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001311 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001312 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák1af868e2019-07-17 17:03:14 +02001313 return LY_EVALID;
1314 }
1315 *max = num;
1316 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001317 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001318 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MAX_ELEMENTS, NULL, exts);
David Sedlák1af868e2019-07-17 17:03:14 +02001319}
David Sedlák374d2b32019-07-17 15:06:55 +02001320
1321/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001322 * @brief Parse min-elements element.
David Sedlák09e18c92019-07-18 11:17:11 +02001323 *
1324 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák09e18c92019-07-18 11:17:11 +02001325 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001326 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001327 * @param[in,out] exts Extension instances to add to.
1328 *
1329 * @return LY_ERR values.
1330 */
1331static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001332yin_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 +02001333{
1334 const char *temp_val = NULL;
1335 char *ptr;
1336 unsigned long int num;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001337 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001338 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1339 };
David Sedlák09e18c92019-07-18 11:17:11 +02001340
1341 *flags |= LYS_SET_MIN;
Michal Vaskob36053d2020-03-26 15:49:30 +01001342 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1343 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 +02001344
Michal Vasko69730152020-10-09 16:30:07 +02001345 if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\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 }
1350
1351 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001352 num = strtoul(temp_val, &ptr, LY_BASE_DEC);
David Sedlák09e18c92019-07-18 11:17:11 +02001353 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001354 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001355 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001356 return LY_EVALID;
1357 }
Michal Vasko69730152020-10-09 16:30:07 +02001358 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001359 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001360 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák09e18c92019-07-18 11:17:11 +02001361 return LY_EVALID;
1362 }
1363 *min = num;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001364 lydict_remove(ctx->xmlctx->ctx, temp_val);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001365 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001366}
1367
David Sedláka2dad212019-07-18 12:45:19 +02001368/**
1369 * @brief Parse min-elements or max-elements element.
1370 *
1371 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001372 * @param[in] parent Identification of parent element.
1373 * @param[in] current Identification of current element.
1374 * @param[in] dest Where the parsed value and flags should be stored.
1375 *
1376 * @return LY_ERR values.
1377 */
David Sedlák09e18c92019-07-18 11:17:11 +02001378static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001379yin_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 +02001380{
Radek Krejcid6b76452019-09-03 17:03:03 +02001381 assert(current == LY_STMT_MAX_ELEMENTS || current == LY_STMT_MIN_ELEMENTS);
1382 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 +02001383 uint32_t *lim;
1384 uint16_t *flags;
1385 struct lysp_ext_instance **exts;
1386
Radek Krejcid6b76452019-09-03 17:03:03 +02001387 if (parent == LY_STMT_LEAF_LIST) {
1388 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 +02001389 flags = &((struct lysp_node_leaflist *)dest)->flags;
1390 exts = &((struct lysp_node_leaflist *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001391 } else if (parent == LY_STMT_REFINE) {
1392 lim = (current == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
David Sedlák09e18c92019-07-18 11:17:11 +02001393 flags = &((struct lysp_refine *)dest)->flags;
1394 exts = &((struct lysp_refine *)dest)->exts;
Radek Krejcid6b76452019-09-03 17:03:03 +02001395 } else if (parent == LY_STMT_LIST) {
1396 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 +02001397 flags = &((struct lysp_node_list *)dest)->flags;
1398 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001399 } else {
1400 lim = ((struct minmax_dev_meta *)dest)->lim;
1401 flags = ((struct minmax_dev_meta *)dest)->flags;
1402 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001403 }
1404
Radek Krejcid6b76452019-09-03 17:03:03 +02001405 if (current == LY_STMT_MAX_ELEMENTS) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001406 LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001407 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001408 LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
David Sedlák09e18c92019-07-18 11:17:11 +02001409 }
1410
1411 return LY_SUCCESS;
1412}
1413
1414/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001415 * @brief Parse ordered-by element.
David Sedláka2dad212019-07-18 12:45:19 +02001416 *
1417 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedláka2dad212019-07-18 12:45:19 +02001418 * @param[out] flags Flags to write to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001419 * @param[in,out] exts Extension instances to add to.
David Sedláka2dad212019-07-18 12:45:19 +02001420 *
1421 * @return LY_ERR values.
1422 */
1423static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001424yin_parse_orderedby(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedláka2dad212019-07-18 12:45:19 +02001425{
1426 const char *temp_val;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001427 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001428 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1429 };
David Sedláka2dad212019-07-18 12:45:19 +02001430
Michal Vaskob36053d2020-03-26 15:49:30 +01001431 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1432 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 +02001433 if (strcmp(temp_val, "system") == 0) {
1434 *flags |= LYS_ORDBY_SYSTEM;
1435 } else if (strcmp(temp_val, "user") == 0) {
1436 *flags |= LYS_ORDBY_USER;
1437 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001438 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001439 "ordered-by", "system", "user");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001440 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001441 return LY_EVALID;
1442 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001443 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001444
Radek Krejcif13b87b2020-12-01 22:02:17 +01001445 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ORDERED_BY, NULL, exts);
David Sedláka2dad212019-07-18 12:45:19 +02001446}
1447
1448/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001449 * @brief Parse any-data or any-xml element.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001450 *
1451 * @param[in,out] ctx YIN parser context for logging and to store current state.
Radek Krejcid6b76452019-09-03 17:03:03 +02001452 * @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 +02001453 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001454 *
1455 * @return LY_ERR values.
1456 */
1457static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001458yin_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 +02001459{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001460 struct lysp_node_anydata *any;
1461
David Sedlák8d552d62019-08-06 15:29:05 +02001462 /* create new sibling */
Michal Vaskob36053d2020-03-26 15:49:30 +01001463 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
Radek Krejcid6b76452019-09-03 17:03:03 +02001464 any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001465 any->parent = node_meta->parent;
1466
David Sedlákbf8a2b72019-08-14 16:48:10 +02001467 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001468 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1469 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 +02001470
Radek Krejcif13b87b2020-12-01 22:02:17 +01001471 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001472 {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1473 {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1474 {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1475 {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1476 {LY_STMT_MUST, &any->musts, 0},
1477 {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1478 {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1479 {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1480 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1481 };
Michal Vasko69730152020-10-09 16:30:07 +02001482
Radek Krejcif13b87b2020-12-01 22:02:17 +01001483 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any_kw, NULL, &any->exts);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001484}
1485
1486/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001487 * @brief parse leaf element.
1488 *
1489 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001490 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001491 *
1492 * @return LY_ERR values.
1493 */
1494static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001495yin_parse_leaf(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák203ca3a2019-07-18 15:26:25 +02001496{
David Sedlák203ca3a2019-07-18 15:26:25 +02001497 struct lysp_node_leaf *leaf;
1498
David Sedlák8d552d62019-08-06 15:29:05 +02001499 /* create structure new leaf */
Michal Vaskob36053d2020-03-26 15:49:30 +01001500 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
David Sedlák203ca3a2019-07-18 15:26:25 +02001501 leaf->nodetype = LYS_LEAF;
1502 leaf->parent = node_meta->parent;
1503
David Sedlák203ca3a2019-07-18 15:26:25 +02001504 /* parser argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001505 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1506 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 +02001507
1508 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001509 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001510 {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1511 {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1512 {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1513 {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1514 {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1515 {LY_STMT_MUST, &leaf->musts, 0},
1516 {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1517 {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1518 {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1519 {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1520 {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1521 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1522 };
Michal Vasko69730152020-10-09 16:30:07 +02001523
Radek Krejcif13b87b2020-12-01 22:02:17 +01001524 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_LEAF, NULL, &leaf->exts);
David Sedlák203ca3a2019-07-18 15:26:25 +02001525}
1526
1527/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001528 * @brief Parse leaf-list element.
1529 *
1530 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001531 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001532 *
1533 * @return LY_ERR values.
1534 */
1535static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001536yin_parse_leaflist(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákc3da3ef2019-07-19 12:56:08 +02001537{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001538 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001539
Michal Vaskob36053d2020-03-26 15:49:30 +01001540 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02001541
David Sedlákc3da3ef2019-07-19 12:56:08 +02001542 llist->nodetype = LYS_LEAFLIST;
1543 llist->parent = node_meta->parent;
1544
David Sedlákc3da3ef2019-07-19 12:56:08 +02001545 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001546 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1547 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 +02001548
1549 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001550 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001551 {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1552 {LY_STMT_DEFAULT, &llist->dflts, 0},
1553 {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1554 {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1555 {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1556 {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1557 {LY_STMT_MUST, &llist->musts, 0},
1558 {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1559 {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1560 {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1561 {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1562 {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1563 {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1564 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1565 };
Michal Vasko69730152020-10-09 16:30:07 +02001566
Radek Krejcif13b87b2020-12-01 22:02:17 +01001567 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 +02001568
David Sedlákbf8a2b72019-08-14 16:48:10 +02001569 /* check invalid combination of subelements */
David Sedlákc3da3ef2019-07-19 12:56:08 +02001570 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001571 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001572 return LY_EVALID;
1573 }
Michal Vasko69730152020-10-09 16:30:07 +02001574 if (llist->max && (llist->min > llist->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02001575 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001576 return LY_EVALID;
1577 }
1578
1579 return LY_SUCCESS;
1580}
1581
1582/**
David Sedlák04e17b22019-07-19 15:29:48 +02001583 * @brief Parse typedef element.
1584 *
1585 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02001586 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001587 *
1588 * @return LY_ERR values.
1589 */
1590static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001591yin_parse_typedef(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001592{
1593 struct lysp_tpdf *tpdf;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001594 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
Michal Vasko69730152020-10-09 16:30:07 +02001595
Michal Vaskob36053d2020-03-26 15:49:30 +01001596 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001597
1598 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001599 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1600 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 +02001601
1602 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001603 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001604 {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1605 {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1606 {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1607 {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1608 {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1609 {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1610 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1611 };
Michal Vasko69730152020-10-09 16:30:07 +02001612
Radek Krejcif13b87b2020-12-01 22:02:17 +01001613 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 +02001614
1615 /* store data for collision check */
Michal Vasko69730152020-10-09 16:30:07 +02001616 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_RPC | LYS_ACTION | LYS_INPUT |
1617 LYS_OUTPUT | LYS_NOTIF))) {
Radek Krejciba03a5a2020-08-27 14:40:41 +02001618 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
David Sedlák04e17b22019-07-19 15:29:48 +02001619 }
1620
1621 return LY_SUCCESS;
1622}
1623
1624/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001625 * @brief Parse refine element.
1626 *
1627 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákd2d676a2019-07-22 11:28:19 +02001628 * @param[in,out] refines Refines to add to.
1629 *
1630 * @return LY_ERR values.
1631 */
1632static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001633yin_parse_refine(struct lys_yin_parser_ctx *ctx, struct lysp_refine **refines)
David Sedlákd2d676a2019-07-22 11:28:19 +02001634{
1635 struct lysp_refine *rf;
1636
1637 /* allocate new refine */
Michal Vaskob36053d2020-03-26 15:49:30 +01001638 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
David Sedlákd2d676a2019-07-22 11:28:19 +02001639
1640 /* parse attribute */
Michal Vaskob36053d2020-03-26 15:49:30 +01001641 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1642 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1643 CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
David Sedlákd2d676a2019-07-22 11:28:19 +02001644
1645 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001646 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001647 {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001648 {LY_STMT_DEFAULT, &rf->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001649 {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001650 {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02001651 {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1652 {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1653 {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1654 {LY_STMT_MUST, &rf->musts, 0},
1655 {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1656 {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1657 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1658 };
Michal Vasko69730152020-10-09 16:30:07 +02001659
Radek Krejcif13b87b2020-12-01 22:02:17 +01001660 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REFINE, NULL, &rf->exts);
David Sedlákd2d676a2019-07-22 11:28:19 +02001661}
1662
1663/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001664 * @brief Parse uses element.
1665 *
1666 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02001667 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001668 *
1669 * @return LY_ERR values.
1670 */
1671static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001672yin_parse_uses(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák0d6de5a2019-07-22 13:25:44 +02001673{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001674 struct lysp_node_uses *uses;
1675
David Sedlák8d552d62019-08-06 15:29:05 +02001676 /* create new uses */
Michal Vaskob36053d2020-03-26 15:49:30 +01001677 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001678 uses->nodetype = LYS_USES;
1679 uses->parent = node_meta->parent;
1680
David Sedlák0d6de5a2019-07-22 13:25:44 +02001681 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001682 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1683 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 +02001684
1685 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001686 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
Radek Krejcif13b87b2020-12-01 22:02:17 +01001687 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001688 {LY_STMT_AUGMENT, &augments, 0},
1689 {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1690 {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1691 {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1692 {LY_STMT_REFINE, &uses->refines, 0},
1693 {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1694 {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1695 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1696 };
Michal Vasko69730152020-10-09 16:30:07 +02001697
Radek Krejcif13b87b2020-12-01 22:02:17 +01001698 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 +02001699
1700 return LY_SUCCESS;
1701}
1702
1703/**
David Sedlákaa854b02019-07-22 14:17:10 +02001704 * @brief Parse revision element.
1705 *
1706 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákaa854b02019-07-22 14:17:10 +02001707 * @param[in,out] revs Parsed revisions to add to.
1708 *
1709 * @return LY_ERR values.
1710 */
1711static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001712yin_parse_revision(struct lys_yin_parser_ctx *ctx, struct lysp_revision **revs)
David Sedlákaa854b02019-07-22 14:17:10 +02001713{
1714 struct lysp_revision *rev;
1715 const char *temp_date = NULL;
1716
1717 /* allocate new reivison */
Michal Vaskob36053d2020-03-26 15:49:30 +01001718 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
David Sedlákaa854b02019-07-22 14:17:10 +02001719
1720 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001721 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1722 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 +02001723 /* check value */
1724 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
Michal Vaskoe180ed02021-02-05 16:31:20 +01001725 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001726 return LY_EVALID;
1727 }
Radek Krejci36642162021-01-12 16:49:22 +01001728 memcpy(rev->date, temp_date, LY_REV_SIZE);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001729 lydict_remove(ctx->xmlctx->ctx, temp_date);
David Sedlákaa854b02019-07-22 14:17:10 +02001730
1731 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001732 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001733 {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1734 {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1735 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1736 };
Michal Vasko69730152020-10-09 16:30:07 +02001737
Radek Krejcif13b87b2020-12-01 22:02:17 +01001738 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION, NULL, &rev->exts);
David Sedlákaa854b02019-07-22 14:17:10 +02001739}
1740
David Sedlák5e13dea2019-07-22 16:06:45 +02001741/**
1742 * @brief Parse include element.
1743 *
1744 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02001745 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1746 *
1747 * @return LY_ERR values.
1748 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001749static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001750yin_parse_include(struct lys_yin_parser_ctx *ctx, struct include_meta *inc_meta)
David Sedlák0c2bab92019-07-22 15:33:19 +02001751{
1752 struct lysp_include *inc;
1753
1754 /* allocate new include */
Michal Vaskob36053d2020-03-26 15:49:30 +01001755 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
David Sedlák0c2bab92019-07-22 15:33:19 +02001756
1757 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01001758 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1759 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 +02001760
1761 /* submodules share the namespace with the module names, so there must not be
1762 * a module of the same name in the context, no need for revision matching */
Michal Vaskob36053d2020-03-26 15:49:30 +01001763 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01001764 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001765 return LY_EVALID;
1766 }
1767
1768 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001769 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001770 {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1771 {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1772 {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1773 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1774 };
Michal Vasko69730152020-10-09 16:30:07 +02001775
Radek Krejcif13b87b2020-12-01 22:02:17 +01001776 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_INCLUDE, NULL, &inc->exts);
David Sedlák0c2bab92019-07-22 15:33:19 +02001777}
1778
David Sedlákaa854b02019-07-22 14:17:10 +02001779/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02001780 * @brief Parse revision-date element.
David Sedlákdfbbb442019-08-06 16:33:21 +02001781 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001782 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001783 * @param[in,out] rev Array to store the parsed value in.
1784 * @param[in,out] exts Extension instances to add to.
1785 *
1786 * @return LY_ERR values.
1787 */
1788static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001789yin_parse_revision_date(struct lys_yin_parser_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001790{
1791 const char *temp_rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001792 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001793 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1794 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001795
Michal Vaskob36053d2020-03-26 15:49:30 +01001796 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1797 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 +02001798 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 +01001799 lydict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
David Sedlákdfbbb442019-08-06 16:33:21 +02001800
1801 strcpy(rev, temp_rev);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001802 lydict_remove(ctx->xmlctx->ctx, temp_rev);
David Sedlákdfbbb442019-08-06 16:33:21 +02001803
Radek Krejcif13b87b2020-12-01 22:02:17 +01001804 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_REVISION_DATE, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001805}
1806
1807/**
1808 * @brief Parse config element.
1809 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001810 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001811 * @param[in,out] flags Flags to add to.
1812 * @param[in,out] exts Extension instances to add to.
1813 *
1814 * @return LY_ERR values.
1815 */
1816static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001817yin_parse_config(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001818{
1819 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001820 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001821 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1822 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001823
Michal Vaskob36053d2020-03-26 15:49:30 +01001824 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1825 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 +02001826 if (strcmp(temp_val, "true") == 0) {
1827 *flags |= LYS_CONFIG_W;
1828 } else if (strcmp(temp_val, "false") == 0) {
1829 *flags |= LYS_CONFIG_R;
1830 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001831 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
Michal Vasko69730152020-10-09 16:30:07 +02001832 "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001833 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001834 return LY_EVALID;
1835 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001836 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001837
Radek Krejcif13b87b2020-12-01 22:02:17 +01001838 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_CONFIG, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001839}
1840
1841/**
1842 * @brief Parse yang-version element.
1843 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001844 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001845 * @param[out] version Storage for the parsed information.
David Sedlákbf8a2b72019-08-14 16:48:10 +02001846 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02001847 *
1848 * @return LY_ERR values.
1849 */
1850static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001851yin_parse_yangversion(struct lys_yin_parser_ctx *ctx, uint8_t *version, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001852{
1853 const char *temp_version = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001854 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001855 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1856 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001857
Michal Vaskob36053d2020-03-26 15:49:30 +01001858 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1859 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 +02001860 if (strcmp(temp_version, "1") == 0) {
David Sedlákdfbbb442019-08-06 16:33:21 +02001861 *version = LYS_VERSION_1_0;
1862 } else if (strcmp(temp_version, "1.1") == 0) {
1863 *version = LYS_VERSION_1_1;
1864 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001865 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001866 "yang-version", "1", "1.1");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001867 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001868 return LY_EVALID;
1869 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001870 lydict_remove(ctx->xmlctx->ctx, temp_version);
David Sedlákdfbbb442019-08-06 16:33:21 +02001871
Radek Krejcif13b87b2020-12-01 22:02:17 +01001872 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YANG_VERSION, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001873}
1874
1875/**
1876 * @brief Parse import element.
1877 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001878 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001879 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1880 *
1881 * @return LY_ERR values.
1882 */
1883static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001884yin_parse_import(struct lys_yin_parser_ctx *ctx, struct import_meta *imp_meta)
David Sedlákdfbbb442019-08-06 16:33:21 +02001885{
1886 struct lysp_import *imp;
Michal Vasko69730152020-10-09 16:30:07 +02001887
David Sedlákdfbbb442019-08-06 16:33:21 +02001888 /* allocate new element in sized array for import */
Michal Vaskob36053d2020-03-26 15:49:30 +01001889 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
David Sedlákdfbbb442019-08-06 16:33:21 +02001890
Radek Krejcif13b87b2020-12-01 22:02:17 +01001891 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001892 {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1893 {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1894 {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1895 {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1896 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1897 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001898
1899 /* parse import attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01001900 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1901 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 +01001902 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 +02001903 /* check prefix validity */
1904 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1905
1906 return LY_SUCCESS;
1907}
1908
1909/**
1910 * @brief Parse mandatory element.
1911 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001912 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001913 * @param[in,out] flags Flags to add to.
1914 * @param[in,out] exts Extension instances to add to.
1915 *
1916 * @return LY_ERR values.
1917 */
1918static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001919yin_parse_mandatory(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001920{
1921 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001922 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001923 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1924 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001925
Michal Vaskob36053d2020-03-26 15:49:30 +01001926 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1927 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 +02001928 if (strcmp(temp_val, "true") == 0) {
1929 *flags |= LYS_MAND_TRUE;
1930 } else if (strcmp(temp_val, "false") == 0) {
1931 *flags |= LYS_MAND_FALSE;
1932 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001933 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001934 "mandatory", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001935 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001936 return LY_EVALID;
1937 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001938 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02001939
Radek Krejcif13b87b2020-12-01 22:02:17 +01001940 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_MANDATORY, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001941}
1942
1943/**
1944 * @brief Parse status element.
1945 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001946 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001947 * @param[in,out] flags Flags to add to.
1948 * @param[in,out] exts Extension instances to add to.
1949 *
1950 * @return LY_ERR values.
1951 */
1952static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001953yin_parse_status(struct lys_yin_parser_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlákdfbbb442019-08-06 16:33:21 +02001954{
1955 const char *value = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001956 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02001957 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1958 };
David Sedlákdfbbb442019-08-06 16:33:21 +02001959
Michal Vaskob36053d2020-03-26 15:49:30 +01001960 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1961 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 +02001962 if (strcmp(value, "current") == 0) {
1963 *flags |= LYS_STATUS_CURR;
1964 } else if (strcmp(value, "deprecated") == 0) {
1965 *flags |= LYS_STATUS_DEPRC;
1966 } else if (strcmp(value, "obsolete") == 0) {
1967 *flags |= LYS_STATUS_OBSLT;
1968 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001969 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
Michal Vasko69730152020-10-09 16:30:07 +02001970 "status", "current", "deprecated", "obsolete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001971 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001972 return LY_EVALID;
1973 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001974 lydict_remove(ctx->xmlctx->ctx, value);
David Sedlákdfbbb442019-08-06 16:33:21 +02001975
Radek Krejcif13b87b2020-12-01 22:02:17 +01001976 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_STATUS, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02001977}
1978
1979/**
1980 * @brief Parse when element.
1981 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02001982 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02001983 * @param[out] when_p When pointer to parse to.
1984 */
1985static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01001986yin_parse_when(struct lys_yin_parser_ctx *ctx, struct lysp_when **when_p)
David Sedlákdfbbb442019-08-06 16:33:21 +02001987{
1988 struct lysp_when *when;
David Sedláka56e0012019-08-15 13:21:25 +02001989 LY_ERR ret = LY_SUCCESS;
1990
David Sedlákdfbbb442019-08-06 16:33:21 +02001991 when = calloc(1, sizeof *when);
Michal Vaskob36053d2020-03-26 15:49:30 +01001992 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif6923e82020-07-02 16:36:53 +02001993
1994 ret = lyxml_ctx_next(ctx->xmlctx);
1995 LY_CHECK_ERR_RET(ret, free(when), ret);
1996
Michal Vaskob36053d2020-03-26 15:49:30 +01001997 ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
David Sedláka56e0012019-08-15 13:21:25 +02001998 LY_CHECK_ERR_RET(ret, free(when), ret);
1999
David Sedlákdfbbb442019-08-06 16:33:21 +02002000 *when_p = when;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002001 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002002 {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
2003 {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
2004 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2005 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002006
Radek Krejcif13b87b2020-12-01 22:02:17 +01002007 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_WHEN, NULL, &when->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002008}
2009
2010/**
2011 * @brief Parse yin-elemenet element.
2012 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002013 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002014 * @param[in,out] data Data to read from, always moved to currently handled position.
2015 * @param[in,out] flags Flags to add to.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002016 * @prama[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002017 *
2018 * @return LY_ERR values.
2019 */
2020static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002021yin_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 +02002022{
2023 const char *temp_val = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002024 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002025 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2026 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002027
Michal Vaskob36053d2020-03-26 15:49:30 +01002028 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2029 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 +02002030 if (strcmp(temp_val, "true") == 0) {
2031 *flags |= LYS_YINELEM_TRUE;
2032 } else if (strcmp(temp_val, "false") == 0) {
2033 *flags |= LYS_YINELEM_FALSE;
2034 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002035 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
Michal Vasko69730152020-10-09 16:30:07 +02002036 "yin-element", "true", "false");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002037 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002038 return LY_EVALID;
2039 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002040 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlákdfbbb442019-08-06 16:33:21 +02002041
Radek Krejcif13b87b2020-12-01 22:02:17 +01002042 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_YIN_ELEMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002043}
2044
2045/**
2046 * @brief Parse argument element.
2047 *
Michal Vaskob36053d2020-03-26 15:49:30 +01002048 * @param[in,out] xmlctx Xml context.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002049 * @param[in,out] arg_meta Meta information about destionation of parsed data.
2050 * @param[in,out] exts Extension instances to add to.
David Sedlákdfbbb442019-08-06 16:33:21 +02002051 *
2052 * @return LY_ERR values.
2053 */
2054static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002055yin_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 +02002056{
Radek Krejcif13b87b2020-12-01 22:02:17 +01002057 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002058 {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2059 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2060 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002061
Michal Vaskob36053d2020-03-26 15:49:30 +01002062 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2063 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 +02002064
Radek Krejcif13b87b2020-12-01 22:02:17 +01002065 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_ARGUMENT, NULL, exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002066}
2067
2068/**
2069 * @brief Parse the extension statement.
2070 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002071 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákdfbbb442019-08-06 16:33:21 +02002072 * @param[in,out] extensions Extensions to add to.
2073 *
2074 * @return LY_ERR values.
2075 */
2076static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002077yin_parse_extension(struct lys_yin_parser_ctx *ctx, struct lysp_ext **extensions)
David Sedlákdfbbb442019-08-06 16:33:21 +02002078{
2079 struct lysp_ext *ex;
Michal Vasko69730152020-10-09 16:30:07 +02002080
Michal Vaskob36053d2020-03-26 15:49:30 +01002081 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2082 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2083 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 +02002084
Radek Krejci9f87b0c2021-03-05 14:45:26 +01002085 struct yin_argument_meta arg_info = {&ex->flags, &ex->argname};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002086 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002087 {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2088 {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2089 {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2090 {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2091 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2092 };
David Sedlákdfbbb442019-08-06 16:33:21 +02002093
Radek Krejcif13b87b2020-12-01 22:02:17 +01002094 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_EXTENSION, NULL, &ex->exts);
David Sedlákdfbbb442019-08-06 16:33:21 +02002095}
2096
2097/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002098 * @brief Parse feature element.
2099 *
2100 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák5e13dea2019-07-22 16:06:45 +02002101 * @param[in,out] features Features to add to.
2102 *
2103 * @return LY_ERR values.
2104 */
2105static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002106yin_parse_feature(struct lys_yin_parser_ctx *ctx, struct lysp_feature **features)
David Sedlák5e13dea2019-07-22 16:06:45 +02002107{
2108 struct lysp_feature *feat;
2109
2110 /* allocate new feature */
Michal Vaskob36053d2020-03-26 15:49:30 +01002111 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
David Sedlák5e13dea2019-07-22 16:06:45 +02002112
2113 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002114 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2115 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 +02002116
2117 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002118 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002119 {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2120 {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2121 {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2122 {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2123 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2124 };
Michal Vasko69730152020-10-09 16:30:07 +02002125
Radek Krejcif13b87b2020-12-01 22:02:17 +01002126 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_FEATURE, NULL, &feat->exts);
David Sedlák5e13dea2019-07-22 16:06:45 +02002127}
2128
2129/**
David Sedlák28794f22019-07-22 16:45:00 +02002130 * @brief Parse identity element.
2131 *
2132 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák28794f22019-07-22 16:45:00 +02002133 * @param[in,out] identities Identities to add to.
2134 *
2135 * @return LY_ERR values.
2136 */
2137static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002138yin_parse_identity(struct lys_yin_parser_ctx *ctx, struct lysp_ident **identities)
David Sedlák28794f22019-07-22 16:45:00 +02002139{
2140 struct lysp_ident *ident;
2141
2142 /* allocate new identity */
Michal Vaskob36053d2020-03-26 15:49:30 +01002143 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
David Sedlák28794f22019-07-22 16:45:00 +02002144
2145 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002146 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2147 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 +02002148
2149 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002150 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002151 {LY_STMT_BASE, &ident->bases, 0},
2152 {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2153 {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2154 {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2155 {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2156 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2157 };
Michal Vasko69730152020-10-09 16:30:07 +02002158
Radek Krejcif13b87b2020-12-01 22:02:17 +01002159 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_IDENTITY, NULL, &ident->exts);
David Sedlák28794f22019-07-22 16:45:00 +02002160}
2161
2162/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002163 * @brief Parse list element.
2164 *
2165 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002166 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002167 *
2168 * @return LY_ERR values.
2169 */
2170static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002171yin_parse_list(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002172{
David Sedlákaf536aa2019-07-23 13:42:23 +02002173 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002174 LY_ERR ret = LY_SUCCESS;
2175 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002176 size_t subelems_size;
David Sedlákaf536aa2019-07-23 13:42:23 +02002177
Michal Vaskob36053d2020-03-26 15:49:30 +01002178 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
David Sedlákaf536aa2019-07-23 13:42:23 +02002179 list->nodetype = LYS_LIST;
2180 list->parent = node_meta->parent;
2181
David Sedlákaf536aa2019-07-23 13:42:23 +02002182 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002183 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2184 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 +02002185
2186 /* parse list content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002187 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002188 LY_STMT_ACTION, &list->actions, 0,
2189 LY_STMT_ANYDATA, &list->child, 0,
2190 LY_STMT_ANYXML, &list->child, 0,
2191 LY_STMT_CHOICE, &list->child, 0,
2192 LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2193 LY_STMT_CONTAINER, &list->child, 0,
2194 LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2195 LY_STMT_GROUPING, &list->groupings, 0,
2196 LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2197 LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2198 LY_STMT_LEAF, &list->child, 0,
2199 LY_STMT_LEAF_LIST, &list->child, 0,
2200 LY_STMT_LIST, &list->child, 0,
2201 LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2202 LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2203 LY_STMT_MUST, &list->musts, 0,
2204 LY_STMT_NOTIFICATION, &list->notifs, 0,
2205 LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2206 LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2207 LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2208 LY_STMT_TYPEDEF, &list->typedefs, 0,
2209 LY_STMT_UNIQUE, &list->uniques, 0,
2210 LY_STMT_USES, &list->child, 0,
2211 LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2212 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002213 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_LIST, NULL, &list->exts);
2214 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002215 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002216
Michal Vasko69730152020-10-09 16:30:07 +02002217 if (list->max && (list->min > list->max)) {
David Sedlák1538a842019-08-08 15:38:51 +02002218 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002219 return LY_EVALID;
2220 }
2221
2222 return LY_SUCCESS;
2223}
2224
2225/**
David Sedlák031b9e72019-07-23 15:19:37 +02002226 * @brief Parse notification element.
2227 *
2228 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák05404f62019-07-24 14:11:53 +02002229 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002230 *
2231 * @return LY_ERR values.
2232 */
2233static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002234yin_parse_notification(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002235{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002236 struct lysp_node_notif *notif;
2237 struct lysp_node_notif **notifs = (struct lysp_node_notif **)notif_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002238 LY_ERR ret = LY_SUCCESS;
2239 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002240 size_t subelems_size;
David Sedlák031b9e72019-07-23 15:19:37 +02002241
2242 /* allocate new notification */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002243 LY_LIST_NEW_RET(ctx->xmlctx->ctx, notifs, notif, next, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002244 notif->nodetype = LYS_NOTIF;
2245 notif->parent = notif_meta->parent;
2246
2247 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002248 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2249 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 +02002250
2251 /* parse notification content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002252 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002253 LY_STMT_ANYDATA, &notif->child, 0,
2254 LY_STMT_ANYXML, &notif->child, 0,
2255 LY_STMT_CHOICE, &notif->child, 0,
2256 LY_STMT_CONTAINER, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002257 LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2258 LY_STMT_GROUPING, &notif->groupings, 0,
2259 LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002260 LY_STMT_LEAF, &notif->child, 0,
2261 LY_STMT_LEAF_LIST, &notif->child, 0,
2262 LY_STMT_LIST, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002263 LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2264 LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2265 LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2266 LY_STMT_TYPEDEF, &notif->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002267 LY_STMT_USES, &notif->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002268 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02002269
Radek Krejcif13b87b2020-12-01 22:02:17 +01002270 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_NOTIFICATION, NULL, &notif->exts);
2271 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002272 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002273
David Sedlák031b9e72019-07-23 15:19:37 +02002274 return LY_SUCCESS;
2275}
2276
2277/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002278 * @brief Parse grouping element.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002279 *
2280 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002281 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002282 *
2283 * @return LY_ERR values.
2284 */
2285static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002286yin_parse_grouping(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002287{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002288 struct lysp_node_grp *grp;
2289 struct lysp_node_grp **grps = (struct lysp_node_grp **)gr_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002290 LY_ERR ret = LY_SUCCESS;
2291 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002292 size_t subelems_size;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002293
2294 /* create new grouping */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002295 LY_LIST_NEW_RET(ctx->xmlctx->ctx, grps, grp, next, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002296 grp->nodetype = LYS_GROUPING;
2297 grp->parent = gr_meta->parent;
2298
2299 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002300 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2301 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 +02002302
2303 /* parse grouping content */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002304 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, &grp->node, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002305 LY_STMT_ACTION, &grp->actions, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002306 LY_STMT_ANYDATA, &grp->child, 0,
2307 LY_STMT_ANYXML, &grp->child, 0,
2308 LY_STMT_CHOICE, &grp->child, 0,
2309 LY_STMT_CONTAINER, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002310 LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2311 LY_STMT_GROUPING, &grp->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002312 LY_STMT_LEAF, &grp->child, 0,
2313 LY_STMT_LEAF_LIST, &grp->child, 0,
2314 LY_STMT_LIST, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002315 LY_STMT_NOTIFICATION, &grp->notifs, 0,
2316 LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2317 LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2318 LY_STMT_TYPEDEF, &grp->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002319 LY_STMT_USES, &grp->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002320 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002321 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_GROUPING, NULL, &grp->exts);
2322 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002323
Radek Krejci2a9fc652021-01-22 17:44:34 +01002324 return ret;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002325}
2326
2327/**
David Sedlákbf8a2b72019-08-14 16:48:10 +02002328 * @brief Parse container element.
David Sedlákf111bcb2019-07-23 17:15:51 +02002329 *
2330 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002331 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002332 *
2333 * @return LY_ERR values.
2334 */
2335static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002336yin_parse_container(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákf111bcb2019-07-23 17:15:51 +02002337{
David Sedlákf111bcb2019-07-23 17:15:51 +02002338 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002339 LY_ERR ret = LY_SUCCESS;
2340 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002341 size_t subelems_size;
David Sedlákf111bcb2019-07-23 17:15:51 +02002342
2343 /* create new container */
Michal Vaskob36053d2020-03-26 15:49:30 +01002344 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
David Sedlákf111bcb2019-07-23 17:15:51 +02002345 cont->nodetype = LYS_CONTAINER;
2346 cont->parent = node_meta->parent;
2347
David Sedlákf111bcb2019-07-23 17:15:51 +02002348 /* parse aegument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002349 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2350 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 +02002351
2352 /* parse container content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002353 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002354 LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2355 LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2356 LY_STMT_ANYXML, &cont->child, 0,
2357 LY_STMT_CHOICE, &cont->child, 0,
2358 LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2359 LY_STMT_CONTAINER, &cont->child, 0,
2360 LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2361 LY_STMT_GROUPING, &cont->groupings, 0,
2362 LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2363 LY_STMT_LEAF, &cont->child, 0,
2364 LY_STMT_LEAF_LIST, &cont->child, 0,
2365 LY_STMT_LIST, &cont->child, 0,
2366 LY_STMT_MUST, &cont->musts, 0,
2367 LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2368 LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2369 LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2370 LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2371 LY_STMT_TYPEDEF, &cont->typedefs, 0,
2372 LY_STMT_USES, &cont->child, 0,
2373 LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2374 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002375 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CONTAINER, NULL, &cont->exts);
2376 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002377
Radek Krejci2a9fc652021-01-22 17:44:34 +01002378 return ret;
David Sedlákf111bcb2019-07-23 17:15:51 +02002379}
2380
2381/**
David Sedlák5379d392019-07-24 10:42:03 +02002382 * @brief Parse case element.
2383 *
2384 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002385 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002386 *
2387 * @return LY_ERR values.
2388 */
2389static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002390yin_parse_case(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlák5379d392019-07-24 10:42:03 +02002391{
David Sedlák5379d392019-07-24 10:42:03 +02002392 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002393 LY_ERR ret = LY_SUCCESS;
Michal Vasko69730152020-10-09 16:30:07 +02002394 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002395 size_t subelems_size;
David Sedlák5379d392019-07-24 10:42:03 +02002396
2397 /* create new case */
Michal Vaskob36053d2020-03-26 15:49:30 +01002398 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
David Sedlák5379d392019-07-24 10:42:03 +02002399 cas->nodetype = LYS_CASE;
2400 cas->parent = node_meta->parent;
2401
David Sedlák5379d392019-07-24 10:42:03 +02002402 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002403 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2404 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 +02002405
2406 /* parse case content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002407 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002408 LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2409 LY_STMT_ANYXML, &cas->child, 0,
2410 LY_STMT_CHOICE, &cas->child, 0,
2411 LY_STMT_CONTAINER, &cas->child, 0,
2412 LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2413 LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2414 LY_STMT_LEAF, &cas->child, 0,
2415 LY_STMT_LEAF_LIST, &cas->child, 0,
2416 LY_STMT_LIST, &cas->child, 0,
2417 LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2418 LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2419 LY_STMT_USES, &cas->child, 0,
2420 LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2421 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002422 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CASE, NULL, &cas->exts);
2423 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002424
2425 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002426}
2427
2428/**
David Sedlák05404f62019-07-24 14:11:53 +02002429 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002430 *
2431 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákad83cf92019-08-13 12:53:53 +02002432 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002433 *
2434 * @return LY_ERR values.
2435 */
2436LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002437yin_parse_choice(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *node_meta)
David Sedlákb7abcfa2019-07-24 12:33:35 +02002438{
David Sedlák81497a32019-08-13 16:56:26 +02002439 LY_ERR ret = LY_SUCCESS;
2440 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002441 struct lysp_node_choice *choice;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002442 size_t subelems_size;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002443
2444 /* create new choice */
Michal Vaskob36053d2020-03-26 15:49:30 +01002445 LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
David Sedlák8d552d62019-08-06 15:29:05 +02002446
David Sedlákb7abcfa2019-07-24 12:33:35 +02002447 choice->nodetype = LYS_CHOICE;
2448 choice->parent = node_meta->parent;
2449
David Sedlákb7abcfa2019-07-24 12:33:35 +02002450 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002451 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2452 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 +02002453
2454 /* parse choice content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002455 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002456 LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2457 LY_STMT_ANYXML, &choice->child, 0,
2458 LY_STMT_CASE, &choice->child, 0,
2459 LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2460 LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2461 LY_STMT_CONTAINER, &choice->child, 0,
2462 LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2463 LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2464 LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2465 LY_STMT_LEAF, &choice->child, 0,
2466 LY_STMT_LEAF_LIST, &choice->child, 0,
2467 LY_STMT_LIST, &choice->child, 0,
2468 LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2469 LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2470 LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2471 LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2472 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002473 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_CHOICE, NULL, &choice->exts);
2474 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002475 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002476}
2477
2478/**
David Sedlák05404f62019-07-24 14:11:53 +02002479 * @brief Parse input or output element.
2480 *
2481 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +02002482 * @param[in] inout_kw Identification of input/output element.
David Sedlák05404f62019-07-24 14:11:53 +02002483 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2484 *
2485 * @return LY_ERR values.
2486 */
2487static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002488yin_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 +02002489{
David Sedlák81497a32019-08-13 16:56:26 +02002490 LY_ERR ret = LY_SUCCESS;
2491 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002492 size_t subelems_size;
David Sedlák81497a32019-08-13 16:56:26 +02002493
David Sedlák05404f62019-07-24 14:11:53 +02002494 /* initiate structure */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002495 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 +02002496 inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
David Sedlák05404f62019-07-24 14:11:53 +02002497 inout_meta->inout_p->parent = inout_meta->parent;
2498
2499 /* check attributes */
Michal Vaskob36053d2020-03-26 15:49:30 +01002500 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2501 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 +02002502
2503 /* parser input/output content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002504 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
Radek Krejci01180ac2021-01-27 08:48:22 +01002505 LY_STMT_ANYDATA, &inout_meta->inout_p->child, YIN_SUBELEM_VER2,
2506 LY_STMT_ANYXML, &inout_meta->inout_p->child, 0,
2507 LY_STMT_CHOICE, &inout_meta->inout_p->child, 0,
2508 LY_STMT_CONTAINER, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002509 LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002510 LY_STMT_LEAF, &inout_meta->inout_p->child, 0,
2511 LY_STMT_LEAF_LIST, &inout_meta->inout_p->child, 0,
2512 LY_STMT_LIST, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002513 LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2514 LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
Radek Krejci01180ac2021-01-27 08:48:22 +01002515 LY_STMT_USES, &inout_meta->inout_p->child, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002516 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002517 ret = yin_parse_content(ctx, subelems, subelems_size, inout_kw, NULL, &inout_meta->inout_p->exts);
2518 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002519 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002520
Radek Krejci01180ac2021-01-27 08:48:22 +01002521 if (!inout_meta->inout_p->child) {
Michal Vaskob83af8a2020-01-06 09:49:22 +01002522 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", ly_stmt2str(inout_kw));
2523 return LY_EVALID;
2524 }
2525
David Sedlák05404f62019-07-24 14:11:53 +02002526 return LY_SUCCESS;
2527}
2528
David Sedlák992fb7c2019-07-24 16:51:01 +02002529/**
2530 * @brief Parse action element.
2531 *
2532 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002533 * @param[in] act_meta Meta information about parent node and actions to add to.
2534 *
2535 * @return LY_ERR values.
2536 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002537static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002538yin_parse_action(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002539{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002540 struct lysp_node_action *act, **acts = (struct lysp_node_action **)act_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002541 LY_ERR ret = LY_SUCCESS;
2542 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002543 size_t subelems_size;
Radek Krejci39b7fc22021-02-26 23:29:18 +01002544 enum ly_stmt kw = act_meta->parent ? LY_STMT_ACTION : LY_STMT_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002545
2546 /* create new action */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002547 LY_LIST_NEW_RET(ctx->xmlctx->ctx, acts, act, next, LY_EMEM);
Michal Vasko1bf09392020-03-27 12:38:10 +01002548 act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
David Sedlák85d0eca2019-07-24 15:15:21 +02002549 act->parent = act_meta->parent;
2550
2551 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002552 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002553 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, kw));
David Sedlák85d0eca2019-07-24 15:15:21 +02002554
2555 /* parse content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002556 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002557 LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2558 LY_STMT_GROUPING, &act->groupings, 0,
2559 LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2560 LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2561 LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2562 LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2563 LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2564 LY_STMT_TYPEDEF, &act->typedefs, 0,
2565 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejci39b7fc22021-02-26 23:29:18 +01002566 ret = (yin_parse_content(ctx, subelems, subelems_size, kw, NULL, &act->exts));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002567 subelems_deallocator(subelems_size, subelems);
David Sedlák81497a32019-08-13 16:56:26 +02002568 LY_CHECK_RET(ret);
2569
Michal Vasko7f45cf22020-10-01 12:49:44 +02002570 /* always initialize inout, they are technically present (needed for later deviations/refines) */
2571 if (!act->input.nodetype) {
2572 act->input.nodetype = LYS_INPUT;
2573 act->input.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002574 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002575 }
2576 if (!act->output.nodetype) {
2577 act->output.nodetype = LYS_OUTPUT;
2578 act->output.parent = (struct lysp_node *)act;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002579 LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
Michal Vasko7f45cf22020-10-01 12:49:44 +02002580 }
2581
David Sedlák85d0eca2019-07-24 15:15:21 +02002582 return LY_SUCCESS;
2583}
2584
David Sedlák05404f62019-07-24 14:11:53 +02002585/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002586 * @brief Parse augment element.
2587 *
2588 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák992fb7c2019-07-24 16:51:01 +02002589 * @param[in] aug_meta Meta information about parent node and augments to add to.
2590 *
2591 * @return LY_ERR values.
2592 */
2593static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002594yin_parse_augment(struct lys_yin_parser_ctx *ctx, struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002595{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002596 struct lysp_node_augment *aug;
2597 struct lysp_node_augment **augs = (struct lysp_node_augment **)aug_meta->nodes;
David Sedlák81497a32019-08-13 16:56:26 +02002598 LY_ERR ret = LY_SUCCESS;
2599 struct yin_subelement *subelems = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002600 size_t subelems_size;
David Sedlák992fb7c2019-07-24 16:51:01 +02002601
2602 /* create new augment */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002603 LY_LIST_NEW_RET(ctx->xmlctx->ctx, augs, aug, next, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002604 aug->nodetype = LYS_AUGMENT;
2605 aug->parent = aug_meta->parent;
2606
2607 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002608 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2609 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2610 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
David Sedlák992fb7c2019-07-24 16:51:01 +02002611
2612 /* parser augment content */
Radek Krejcif13b87b2020-12-01 22:02:17 +01002613 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02002614 LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2615 LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2616 LY_STMT_ANYXML, &aug->child, 0,
2617 LY_STMT_CASE, &aug->child, 0,
2618 LY_STMT_CHOICE, &aug->child, 0,
2619 LY_STMT_CONTAINER, &aug->child, 0,
2620 LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002621 LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
Michal Vasko69730152020-10-09 16:30:07 +02002622 LY_STMT_LEAF, &aug->child, 0,
2623 LY_STMT_LEAF_LIST, &aug->child, 0,
2624 LY_STMT_LIST, &aug->child, 0,
2625 LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2626 LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2627 LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2628 LY_STMT_USES, &aug->child, 0,
2629 LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2630 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
Radek Krejcif13b87b2020-12-01 22:02:17 +01002631 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_AUGMENT, NULL, &aug->exts);
2632 subelems_deallocator(subelems_size, subelems);
David Sedlák992fb7c2019-07-24 16:51:01 +02002633
Radek Krejci2a9fc652021-01-22 17:44:34 +01002634 return ret;
David Sedlák992fb7c2019-07-24 16:51:01 +02002635}
2636
David Sedlák8b754462019-07-25 16:22:13 +02002637/**
2638 * @brief Parse deviate element.
2639 *
2640 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002641 * @param[in] deviates Deviates to add to.
2642 *
2643 * @return LY_ERR values.
2644 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002645static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002646yin_parse_deviate(struct lys_yin_parser_ctx *ctx, struct lysp_deviate **deviates)
David Sedlák4ffcec82019-07-25 15:10:21 +02002647{
2648 LY_ERR ret = LY_SUCCESS;
2649 uint8_t dev_mod;
2650 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002651 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002652 struct lysp_deviate_add *d_add = NULL;
2653 struct lysp_deviate_rpl *d_rpl = NULL;
2654 struct lysp_deviate_del *d_del = NULL;
2655
2656 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002657 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2658 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 +02002659
2660 if (strcmp(temp_val, "not-supported") == 0) {
2661 dev_mod = LYS_DEV_NOT_SUPPORTED;
2662 } else if (strcmp(temp_val, "add") == 0) {
2663 dev_mod = LYS_DEV_ADD;
2664 } else if (strcmp(temp_val, "replace") == 0) {
2665 dev_mod = LYS_DEV_REPLACE;
2666 } else if (strcmp(temp_val, "delete") == 0) {
2667 dev_mod = LYS_DEV_DELETE;
2668 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002669 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
Michal Vasko69730152020-10-09 16:30:07 +02002670 "not-supported", "add", "replace", "delete");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002671 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002672 return LY_EVALID;
2673 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01002674 lydict_remove(ctx->xmlctx->ctx, temp_val);
David Sedlák4ffcec82019-07-25 15:10:21 +02002675
2676 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2677 d = calloc(1, sizeof *d);
Michal Vaskob36053d2020-03-26 15:49:30 +01002678 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
Radek Krejcif13b87b2020-12-01 22:02:17 +01002679 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002680 {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2681 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002682 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002683
2684 } else if (dev_mod == LYS_DEV_ADD) {
2685 d_add = calloc(1, sizeof *d_add);
Michal Vaskob36053d2020-03-26 15:49:30 +01002686 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002687 d = (struct lysp_deviate *)d_add;
2688 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2689 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002690 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002691 {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002692 {LY_STMT_DEFAULT, &d_add->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002693 {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2694 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2695 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2696 {LY_STMT_MUST, &d_add->musts, 0},
2697 {LY_STMT_UNIQUE, &d_add->uniques, 0},
2698 {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2699 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2700 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002701 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_add->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002702
2703 } else if (dev_mod == LYS_DEV_REPLACE) {
2704 d_rpl = calloc(1, sizeof *d_rpl);
Michal Vaskob36053d2020-03-26 15:49:30 +01002705 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002706 d = (struct lysp_deviate *)d_rpl;
2707 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2708 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
Radek Krejcif13b87b2020-12-01 22:02:17 +01002709 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002710 {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002711 {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
Radek Krejci0f969882020-08-21 16:56:47 +02002712 {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2713 {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2714 {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2715 {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2716 {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2717 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2718 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002719 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_rpl->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002720
2721 } else {
2722 d_del = calloc(1, sizeof *d_del);
Michal Vaskob36053d2020-03-26 15:49:30 +01002723 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
David Sedlák4ffcec82019-07-25 15:10:21 +02002724 d = (struct lysp_deviate *)d_del;
Radek Krejcif13b87b2020-12-01 22:02:17 +01002725 struct yin_subelement subelems[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002726 {LY_STMT_DEFAULT, &d_del->dflts, 0},
Radek Krejci0f969882020-08-21 16:56:47 +02002727 {LY_STMT_MUST, &d_del->musts, 0},
2728 {LY_STMT_UNIQUE, &d_del->uniques, 0},
2729 {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2730 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2731 };
Radek Krejcif13b87b2020-12-01 22:02:17 +01002732 ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATE, NULL, &d_del->exts);
David Sedlák4ffcec82019-07-25 15:10:21 +02002733 }
2734 LY_CHECK_GOTO(ret, cleanup);
2735
2736 d->mod = dev_mod;
2737 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002738 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002739
2740 return ret;
2741
2742cleanup:
2743 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002744 return ret;
2745}
2746
David Sedlák992fb7c2019-07-24 16:51:01 +02002747/**
David Sedlák8b754462019-07-25 16:22:13 +02002748 * @brief Parse deviation element.
2749 *
2750 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlák8b754462019-07-25 16:22:13 +02002751 * @param[in] deviations Deviations to add to.
2752 *
2753 * @return LY_ERR values.
2754 */
2755static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002756yin_parse_deviation(struct lys_yin_parser_ctx *ctx, struct lysp_deviation **deviations)
David Sedlák8b754462019-07-25 16:22:13 +02002757{
2758 struct lysp_deviation *dev;
2759
2760 /* create new deviation */
Michal Vaskob36053d2020-03-26 15:49:30 +01002761 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
David Sedlák8b754462019-07-25 16:22:13 +02002762
2763 /* parse argument */
Michal Vaskob36053d2020-03-26 15:49:30 +01002764 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2765 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION));
2766 CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
Radek Krejcif13b87b2020-12-01 22:02:17 +01002767 struct yin_subelement subelems[] = {
Radek Krejci0f969882020-08-21 16:56:47 +02002768 {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2769 {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2770 {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2771 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2772 };
Michal Vasko69730152020-10-09 16:30:07 +02002773
Radek Krejcif13b87b2020-12-01 22:02:17 +01002774 return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), LY_STMT_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002775}
2776
2777/**
David Sedlákc5b20842019-08-13 10:18:31 +02002778 * @brief map keyword to keyword-group.
2779 *
2780 * @param[in] ctx YIN parser context used for logging.
2781 * @param[in] kw Keyword that is child of module or submodule.
2782 * @param[out] group Group of keyword.
2783 *
2784 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2785 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002786static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002787kw2kw_group(struct lys_yin_parser_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
David Sedláke6cd89e2019-08-07 12:46:02 +02002788{
2789 switch (kw) {
Radek Krejci0f969882020-08-21 16:56:47 +02002790 /* module header */
2791 case LY_STMT_NONE:
2792 case LY_STMT_NAMESPACE:
2793 case LY_STMT_PREFIX:
2794 case LY_STMT_BELONGS_TO:
2795 case LY_STMT_YANG_VERSION:
2796 *group = Y_MOD_MODULE_HEADER;
2797 break;
2798 /* linkage */
2799 case LY_STMT_INCLUDE:
2800 case LY_STMT_IMPORT:
2801 *group = Y_MOD_LINKAGE;
2802 break;
2803 /* meta */
2804 case LY_STMT_ORGANIZATION:
2805 case LY_STMT_CONTACT:
2806 case LY_STMT_DESCRIPTION:
2807 case LY_STMT_REFERENCE:
2808 *group = Y_MOD_META;
2809 break;
2810 /* revision */
2811 case LY_STMT_REVISION:
2812 *group = Y_MOD_REVISION;
2813 break;
2814 /* body */
2815 case LY_STMT_ANYDATA:
2816 case LY_STMT_ANYXML:
2817 case LY_STMT_AUGMENT:
2818 case LY_STMT_CHOICE:
2819 case LY_STMT_CONTAINER:
2820 case LY_STMT_DEVIATION:
2821 case LY_STMT_EXTENSION:
2822 case LY_STMT_FEATURE:
2823 case LY_STMT_GROUPING:
2824 case LY_STMT_IDENTITY:
2825 case LY_STMT_LEAF:
2826 case LY_STMT_LEAF_LIST:
2827 case LY_STMT_LIST:
2828 case LY_STMT_NOTIFICATION:
2829 case LY_STMT_RPC:
2830 case LY_STMT_TYPEDEF:
2831 case LY_STMT_USES:
2832 case LY_STMT_EXTENSION_INSTANCE:
2833 *group = Y_MOD_BODY;
2834 break;
2835 default:
2836 LOGINT(ctx->xmlctx->ctx);
2837 return LY_EINT;
David Sedláke6cd89e2019-08-07 12:46:02 +02002838 }
2839
2840 return LY_SUCCESS;
2841}
2842
David Sedlákc5b20842019-08-13 10:18:31 +02002843/**
2844 * @brief Check if relative order of two keywords is valid.
2845 *
2846 * @param[in] ctx YIN parser context used for logging.
2847 * @param[in] kw Current keyword.
2848 * @param[in] next_kw Next keyword.
Radek Krejcid6b76452019-09-03 17:03:03 +02002849 * @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 +02002850 * because relative order is required only in module and submodule sub-elements, used for logging.
2851 *
David Sedlákbf8a2b72019-08-14 16:48:10 +02002852 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
David Sedlákc5b20842019-08-13 10:18:31 +02002853 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002854static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01002855yin_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 +02002856{
Radek Krejcid6b76452019-09-03 17:03:03 +02002857 assert(parrent == LY_STMT_MODULE || parrent == LY_STMT_SUBMODULE);
David Sedláke6cd89e2019-08-07 12:46:02 +02002858 enum yang_module_stmt gr, next_gr;
2859
2860 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2861 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2862
2863 if (gr > next_gr) {
2864 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2865 return LY_EVALID;
2866 }
2867
2868 return LY_SUCCESS;
2869}
2870
David Sedlákc5dd0942019-09-13 10:23:59 +02002871/**
Michal Vaskoafac7822020-10-20 14:22:26 +02002872 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
2873 *
2874 * @param[in,out] ctx Yin parser context for logging and to store current state.
2875 * @param[in] elem_type Type of element that is currently being parsed.
2876 * @param[out] arg Value to write to.
2877 *
2878 * @return LY_ERR values.
2879 */
2880static LY_ERR
2881yin_parse_extension_instance_arg(struct lys_yin_parser_ctx *ctx, enum ly_stmt elem_type, const char **arg)
2882{
2883 enum ly_stmt child;
2884
2885 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2886
2887 switch (elem_type) {
2888 case LY_STMT_ACTION:
2889 case LY_STMT_ANYDATA:
2890 case LY_STMT_ANYXML:
2891 case LY_STMT_ARGUMENT:
2892 case LY_STMT_BASE:
2893 case LY_STMT_BIT:
2894 case LY_STMT_CASE:
2895 case LY_STMT_CHOICE:
2896 case LY_STMT_CONTAINER:
2897 case LY_STMT_ENUM:
2898 case LY_STMT_EXTENSION:
2899 case LY_STMT_FEATURE:
2900 case LY_STMT_GROUPING:
2901 case LY_STMT_IDENTITY:
2902 case LY_STMT_IF_FEATURE:
2903 case LY_STMT_LEAF:
2904 case LY_STMT_LEAF_LIST:
2905 case LY_STMT_LIST:
2906 case LY_STMT_MODULE:
2907 case LY_STMT_NOTIFICATION:
2908 case LY_STMT_RPC:
2909 case LY_STMT_SUBMODULE:
2910 case LY_STMT_TYPE:
2911 case LY_STMT_TYPEDEF:
2912 case LY_STMT_UNITS:
2913 case LY_STMT_USES:
2914 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, elem_type));
2915 break;
2916 case LY_STMT_AUGMENT:
2917 case LY_STMT_DEVIATION:
2918 case LY_STMT_REFINE:
2919 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, elem_type));
2920 break;
2921 case LY_STMT_CONFIG:
2922 case LY_STMT_DEFAULT:
2923 case LY_STMT_DEVIATE:
2924 case LY_STMT_ERROR_APP_TAG:
2925 case LY_STMT_FRACTION_DIGITS:
2926 case LY_STMT_KEY:
2927 case LY_STMT_LENGTH:
2928 case LY_STMT_MANDATORY:
2929 case LY_STMT_MAX_ELEMENTS:
2930 case LY_STMT_MIN_ELEMENTS:
2931 case LY_STMT_MODIFIER:
2932 case LY_STMT_ORDERED_BY:
2933 case LY_STMT_PATH:
2934 case LY_STMT_PATTERN:
2935 case LY_STMT_POSITION:
2936 case LY_STMT_PREFIX:
2937 case LY_STMT_PRESENCE:
2938 case LY_STMT_RANGE:
2939 case LY_STMT_REQUIRE_INSTANCE:
2940 case LY_STMT_STATUS:
2941 case LY_STMT_VALUE:
2942 case LY_STMT_YANG_VERSION:
2943 case LY_STMT_YIN_ELEMENT:
2944 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, elem_type));
2945 break;
2946 case LY_STMT_IMPORT:
2947 case LY_STMT_INCLUDE:
2948 case LY_STMT_BELONGS_TO:
2949 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, elem_type));
2950 break;
2951 case LY_STMT_INPUT:
2952 case LY_STMT_OUTPUT:
2953 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2954 break;
2955 case LY_STMT_MUST:
2956 case LY_STMT_WHEN:
2957 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, elem_type));
2958 break;
2959 case LY_STMT_NAMESPACE:
2960 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, elem_type));
2961 break;
2962 case LY_STMT_REVISION:
2963 case LY_STMT_REVISION_DATE:
2964 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, elem_type));
2965 break;
2966 case LY_STMT_UNIQUE:
2967 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, elem_type));
2968 break;
2969 /* argument is mapped to yin element */
2970 case LY_STMT_CONTACT:
2971 case LY_STMT_DESCRIPTION:
2972 case LY_STMT_ORGANIZATION:
2973 case LY_STMT_REFERENCE:
2974 case LY_STMT_ERROR_MESSAGE:
2975 /* there shouldn't be any attribute, argument is supposed to be first subelement */
2976 LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, elem_type));
2977
2978 /* no content */
2979 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
2980 if (ctx->xmlctx->ws_only) {
2981 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2982 }
2983 if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
2984 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
2985 elem_type == LY_STMT_ERROR_MESSAGE ? "value" : "text", ly_stmt2str(elem_type));
2986 return LY_EVALID;
2987 }
2988
2989 /* parse child element */
2990 child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix, ctx->xmlctx->prefix_len, elem_type);
2991 if (((elem_type == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
2992 ((elem_type != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
2993 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
2994 ly_stmt2str(elem_type));
2995 return LY_EVALID;
2996 }
2997 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2998
2999 /* no attributes expected? TODO */
3000 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3001 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3002 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3003 }
3004
3005 /* load and save content */
3006 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
3007 LY_CHECK_RET(!*arg, LY_EMEM);
3008
3009 /* load closing tag of subelement */
3010 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3011
3012 /* if only subelement was parsed as argument, load also closing tag TODO what? */
3013 /*if (ctx->xmlctx->status == LYXML_ELEMENT) {
3014 LY_CHECK_RET(lyxml_get_element(&ctx->xmlctx, data, &prefix, &prefix_len, &name, &name_len));
3015 }*/
3016 break;
3017 default:
3018 LOGINT(ctx->xmlctx->ctx);
3019 return LY_EINT;
3020 }
3021
3022 return LY_SUCCESS;
3023}
3024
3025/**
3026 * @brief Parse yin element into generic structure.
3027 *
3028 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
3029 * @param[in] parent Identification of parent element.
3030 * @param[out] element Where the element structure should be stored.
3031 *
3032 * @return LY_ERR values.
3033 */
3034LY_ERR
3035yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element)
3036{
3037 LY_ERR ret = LY_SUCCESS;
3038 struct lysp_stmt *last = NULL, *new = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003039 char *id;
Michal Vaskoafac7822020-10-20 14:22:26 +02003040
3041 assert(ctx->xmlctx->status == LYXML_ELEMENT);
3042
3043 /* allocate new structure for element */
3044 *element = calloc(1, sizeof(**element));
3045 LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003046
3047 /* store identifier */
3048 if (ctx->xmlctx->prefix_len) {
3049 if (asprintf(&id, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3050 ctx->xmlctx->name) == -1) {
3051 LOGMEM(ctx->xmlctx->ctx);
3052 ret = LY_EMEM;
3053 goto cleanup;
3054 }
3055 LY_CHECK_GOTO(ret = lydict_insert_zc(ctx->xmlctx->ctx, id, &(*element)->stmt), cleanup);
3056
3057 /* store prefix data for the statement */
3058 LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->stmt, strlen((*element)->stmt), LY_PREF_XML,
3059 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3060 } else {
3061 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &(*element)->stmt), cleanup);
3062 }
Michal Vaskoafac7822020-10-20 14:22:26 +02003063
3064 (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3065 ctx->xmlctx->prefix_len, parent);
3066
3067 last = (*element)->child;
3068 if ((*element)->kw == LY_STMT_NONE) {
3069 /* unrecognized element */
3070 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len, ctx->xmlctx->name,
3071 ly_stmt2str(parent));
3072 ret = LY_EVALID;
3073 goto cleanup;
3074 } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3075 /* element is known yang keyword, which means argument can be parsed correctly. */
3076 ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3077 LY_CHECK_GOTO(ret, cleanup);
3078 } else {
3079 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3080
3081 /* load attributes in generic way, save all attributes in linked list */
3082 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3083 new = calloc(1, sizeof(*last));
3084 LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3085 if (!(*element)->child) {
3086 /* save first */
3087 (*element)->child = new;
3088 } else {
3089 last->next = new;
3090 }
3091 last = new;
3092
3093 last->flags |= LYS_YIN_ATTR;
3094 LY_CHECK_GOTO(ret = lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3095 last->kw = LY_STMT_NONE;
3096 /* attributes with prefix are ignored */
3097 if (!ctx->xmlctx->prefix) {
3098 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3099
3100 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3101 LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3102 } else {
3103 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3104 }
3105 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3106 }
3107 }
3108
3109 if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3110 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3111 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3112 /* parse subelements */
3113 ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3114 LY_CHECK_GOTO(ret, cleanup);
3115 if (!(*element)->child) {
3116 /* save first */
3117 (*element)->child = new;
3118 } else {
3119 last->next = new;
3120 }
3121 last = new;
3122
3123 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3124 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3125 }
3126 } else {
3127 /* save element content */
3128 if (ctx->xmlctx->value_len) {
3129 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3130 LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01003131
3132 /* store prefix data for the argument as well */
3133 LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->arg, strlen((*element)->arg), LY_PREF_XML,
3134 &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
Michal Vaskoafac7822020-10-20 14:22:26 +02003135 }
3136
3137 /* read closing tag */
3138 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3139 }
3140
3141cleanup:
3142 return ret;
3143}
3144
3145/**
3146 * @brief Parse instance of extension.
3147 *
3148 * @param[in,out] ctx Yin parser context for logging and to store current state.
Radek Krejcifc596f92021-02-26 22:40:26 +01003149 * @param[in] subelem The statement this extension instance is a subelement of.
Michal Vaskoafac7822020-10-20 14:22:26 +02003150 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
3151 * @param[in,out] exts Extension instance to add to.
3152 *
3153 * @return LY_ERR values.
3154 */
3155LY_ERR
Radek Krejcifc596f92021-02-26 22:40:26 +01003156yin_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 +02003157 struct lysp_ext_instance **exts)
3158{
3159 struct lysp_ext_instance *e;
3160 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003161 char *ext_name;
Michal Vaskoafac7822020-10-20 14:22:26 +02003162
3163 assert(ctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci3aac9a72020-12-01 12:24:26 +01003164 assert(exts);
Michal Vaskoafac7822020-10-20 14:22:26 +02003165
3166 LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3167
Michal Vaskofc2cd072021-02-24 13:17:17 +01003168 if (!ctx->xmlctx->prefix_len) {
3169 LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.",
3170 (int)ctx->xmlctx->name_len, ctx->xmlctx->name);
3171 return LY_EVALID;
3172 }
3173
3174 /* store prefixed name */
3175 if (asprintf(&ext_name, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3176 ctx->xmlctx->name) == -1) {
3177 LOGMEM(ctx->xmlctx->ctx);
3178 return LY_EMEM;
3179 }
3180 LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, ext_name, &e->name));
3181
3182 /* store prefix data for the name */
3183 LY_CHECK_RET(ly_store_prefix_data(ctx->xmlctx->ctx, e->name, strlen(e->name), LY_PREF_XML, &ctx->xmlctx->ns,
3184 &e->format, &e->prefix_data));
3185
Radek Krejciab430862021-03-02 20:13:40 +01003186 e->parent_stmt = subelem;
3187 e->parent_stmt_index = subelem_index;
Michal Vaskofc2cd072021-02-24 13:17:17 +01003188
Michal Vaskoafac7822020-10-20 14:22:26 +02003189 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3190
3191 /* store attributes as subelements */
3192 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3193 if (!ctx->xmlctx->prefix) {
3194 new_subelem = calloc(1, sizeof(*new_subelem));
3195 if (!e->child) {
3196 e->child = new_subelem;
3197 } else {
3198 last_subelem->next = new_subelem;
3199 }
3200 last_subelem = new_subelem;
3201
3202 last_subelem->flags |= LYS_YIN_ATTR;
3203 LY_CHECK_RET(lydict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3204 LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3205 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3206
Michal Vaskofc2cd072021-02-24 13:17:17 +01003207 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic,
3208 last_subelem->arg);
Michal Vaskoafac7822020-10-20 14:22:26 +02003209 LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3210 } else {
3211 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3212 }
3213
3214 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3215 }
3216
3217 /* parse subelements */
3218 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3219 if (ctx->xmlctx->ws_only) {
3220 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3221 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3222 LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3223 if (!e->child) {
3224 e->child = new_subelem;
3225 } else {
3226 last_subelem->next = new_subelem;
3227 }
3228 last_subelem = new_subelem;
3229
3230 assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3231 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3232 }
3233 } else if (ctx->xmlctx->value_len) {
3234 /* save text content */
3235 INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, e->argument);
3236 LY_CHECK_RET(!e->argument, LY_EMEM);
3237
Michal Vaskofc2cd072021-02-24 13:17:17 +01003238 /* store prefix data for the argument as well */
3239 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 +01003240 &ctx->xmlctx->ns, &e->format, &e->prefix_data));
Michal Vaskofc2cd072021-02-24 13:17:17 +01003241
Michal Vaskoafac7822020-10-20 14:22:26 +02003242 /* parser next */
3243 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3244 }
3245
3246 return LY_SUCCESS;
3247}
3248
3249/**
3250 * @brief Generic function for content parsing
3251 *
3252 * @param[in,out] ctx Yin parser context for logging and to store current state.
3253 * @param[in] subelem_info array of valid subelement types and meta information
3254 * @param[in] subelem_info_size Size of subelem_info array.
3255 * @param[in] current_element Type of current element.
3256 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3257 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
3258 *
3259 * @return LY_ERR values.
3260 */
David Sedlákd6e56892019-07-01 15:40:24 +02003261LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003262yin_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 +02003263 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02003264{
3265 LY_ERR ret = LY_SUCCESS;
Michal Vaskob36053d2020-03-26 15:49:30 +01003266 enum LYXML_PARSER_STATUS next_status;
Radek Krejcid6b76452019-09-03 17:03:03 +02003267 enum ly_stmt kw = LY_STMT_NONE, last_kw = LY_STMT_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02003268 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02003269
Michal Vaskob36053d2020-03-26 15:49:30 +01003270 assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
David Sedlákd6e56892019-07-01 15:40:24 +02003271
Michal Vaskob36053d2020-03-26 15:49:30 +01003272 if (ctx->xmlctx->ws_only) {
3273 /* check whether there are any children */
3274 LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3275 } else {
3276 /* we want to parse the value */
3277 next_status = LYXML_ELEM_CLOSE;
3278 }
3279
3280 if (next_status == LYXML_ELEMENT) {
3281 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3282
3283 /* current element has subelements as content */
3284 while (ctx->xmlctx->status == LYXML_ELEMENT) {
3285 /* match keyword */
3286 last_kw = kw;
3287 kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
Michal Vasko69730152020-10-09 16:30:07 +02003288 ctx->xmlctx->prefix_len, current_element);
Michal Vaskob36053d2020-03-26 15:49:30 +01003289
3290 /* check if this element can be child of current element */
3291 subelem = get_record(kw, subelem_info_size, subelem_info);
3292 if (!subelem) {
Michal Vasko69730152020-10-09 16:30:07 +02003293 if ((current_element == LY_STMT_DEVIATE) && isdevsub(kw)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003294 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
3295 } else {
3296 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, ctx->xmlctx->name_len,
Michal Vasko69730152020-10-09 16:30:07 +02003297 ctx->xmlctx->name, ly_stmt2str(current_element));
Michal Vaskob36053d2020-03-26 15:49:30 +01003298 }
3299 ret = LY_EVALID;
3300 goto cleanup;
3301 }
3302
3303 /* relative order is required only in module and submodule sub-elements */
Michal Vasko69730152020-10-09 16:30:07 +02003304 if ((current_element == LY_STMT_MODULE) || (current_element == LY_STMT_SUBMODULE)) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003305 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
3306 LY_CHECK_GOTO(ret, cleanup);
3307 }
3308
3309 /* flag check */
3310 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3311 /* subelement uniquenes */
3312 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
3313 return LY_EVALID;
3314 }
3315 if (subelem->flags & YIN_SUBELEM_FIRST) {
3316 /* subelement is supposed to be defined as first subelement */
3317 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
3318 LY_CHECK_GOTO(ret, cleanup);
3319 }
3320 if (subelem->flags & YIN_SUBELEM_VER2) {
3321 /* subelement is supported only in version 1.1 or higher */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003322 if (ctx->parsed_mod->version < LYS_VERSION_1_1) {
Michal Vaskob36053d2020-03-26 15:49:30 +01003323 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 +02003324 ret = LY_EVALID;
3325 goto cleanup;
3326 }
David Sedlákd6e56892019-07-01 15:40:24 +02003327 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003328 /* note that element was parsed for easy uniqueness check in next iterations */
3329 subelem->flags |= YIN_SUBELEM_PARSED;
3330
3331 switch (kw) {
3332 /* call responsible function */
3333 case LY_STMT_EXTENSION_INSTANCE:
Radek Krejci39b7fc22021-02-26 23:29:18 +01003334 ret = yin_parse_extension_instance(ctx, current_element,
Michal Vasko69730152020-10-09 16:30:07 +02003335 (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01003336 break;
3337 case LY_STMT_ACTION:
3338 case LY_STMT_RPC:
3339 ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3340 break;
3341 case LY_STMT_ANYDATA:
3342 case LY_STMT_ANYXML:
3343 ret = yin_parse_any(ctx, kw, (struct tree_node_meta *)subelem->dest);
3344 break;
3345 case LY_STMT_ARGUMENT:
3346 ret = yin_parse_argument(ctx, (struct yin_argument_meta *)subelem->dest, exts);
3347 break;
3348 case LY_STMT_AUGMENT:
3349 ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3350 break;
3351 case LY_STMT_BASE:
3352 ret = yin_parse_base(ctx, current_element, subelem->dest, exts);
3353 break;
3354 case LY_STMT_BELONGS_TO:
3355 ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3356 break;
3357 case LY_STMT_BIT:
3358 ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3359 break;
3360 case LY_STMT_CASE:
3361 ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3362 break;
3363 case LY_STMT_CHOICE:
3364 ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3365 break;
3366 case LY_STMT_CONFIG:
3367 ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3368 break;
3369 case LY_STMT_CONTACT:
3370 case LY_STMT_DESCRIPTION:
3371 case LY_STMT_ORGANIZATION:
3372 case LY_STMT_REFERENCE:
3373 ret = yin_parse_meta(ctx, kw, (const char **)subelem->dest, exts);
3374 break;
3375 case LY_STMT_CONTAINER:
3376 ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3377 break;
3378 case LY_STMT_DEFAULT:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003379 ret = yin_parse_qname(ctx, kw, subelem, exts);
3380 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003381 case LY_STMT_ERROR_APP_TAG:
3382 case LY_STMT_KEY:
3383 case LY_STMT_PRESENCE:
3384 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3385 break;
3386 case LY_STMT_DEVIATE:
3387 ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3388 break;
3389 case LY_STMT_DEVIATION:
3390 ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3391 break;
3392 case LY_STMT_ENUM:
3393 ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3394 break;
3395 case LY_STMT_ERROR_MESSAGE:
3396 ret = yin_parse_err_msg(ctx, (const char **)subelem->dest, exts);
3397 break;
3398 case LY_STMT_EXTENSION:
3399 ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3400 break;
3401 case LY_STMT_FEATURE:
3402 ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3403 break;
3404 case LY_STMT_FRACTION_DIGITS:
3405 ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3406 break;
3407 case LY_STMT_GROUPING:
3408 ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3409 break;
3410 case LY_STMT_IDENTITY:
3411 ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3412 break;
3413 case LY_STMT_IF_FEATURE:
Michal Vasko7f45cf22020-10-01 12:49:44 +02003414 ret = yin_parse_qname(ctx, kw, subelem, exts);
3415 break;
Michal Vaskob36053d2020-03-26 15:49:30 +01003416 case LY_STMT_UNITS:
3417 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
3418 break;
3419 case LY_STMT_IMPORT:
3420 ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3421 break;
3422 case LY_STMT_INCLUDE:
Radek Krejcieeee95c2021-01-19 10:57:22 +01003423 if ((current_element == LY_STMT_SUBMODULE) && (ctx->parsed_mod->version == LYS_VERSION_1_1)) {
3424 LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in submodules (%s) are not necessary.",
3425 ((struct lysp_submodule *)(ctx->parsed_mod))->name);
3426 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003427 ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3428 break;
3429 case LY_STMT_INPUT:
3430 case LY_STMT_OUTPUT:
3431 ret = yin_parse_inout(ctx, kw, (struct inout_meta *)subelem->dest);
3432 break;
3433 case LY_STMT_LEAF:
3434 ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3435 break;
3436 case LY_STMT_LEAF_LIST:
3437 ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3438 break;
3439 case LY_STMT_LENGTH:
3440 ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3441 break;
3442 case LY_STMT_LIST:
3443 ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3444 break;
3445 case LY_STMT_MANDATORY:
3446 ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3447 break;
3448 case LY_STMT_MAX_ELEMENTS:
3449 case LY_STMT_MIN_ELEMENTS:
3450 ret = yin_parse_minmax(ctx, current_element, kw, subelem->dest);
3451 break;
3452 case LY_STMT_MODIFIER:
3453 ret = yin_parse_modifier(ctx, (const char **)subelem->dest, exts);
3454 break;
3455 case LY_STMT_MUST:
3456 ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3457 break;
3458 case LY_STMT_NAMESPACE:
3459 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
3460 break;
3461 case LY_STMT_NOTIFICATION:
3462 ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3463 break;
3464 case LY_STMT_ORDERED_BY:
3465 ret = yin_parse_orderedby(ctx, (uint16_t *)subelem->dest, exts);
3466 break;
3467 case LY_STMT_PATH:
3468 ret = yin_parse_path(ctx, kw, (struct lysp_type *)subelem->dest);
3469 break;
3470 case LY_STMT_PATTERN:
3471 ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3472 break;
3473 case LY_STMT_VALUE:
3474 case LY_STMT_POSITION:
3475 ret = yin_parse_value_pos(ctx, kw, (struct lysp_type_enum *)subelem->dest);
3476 break;
3477 case LY_STMT_PREFIX:
3478 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
3479 break;
3480 case LY_STMT_RANGE:
3481 ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3482 break;
3483 case LY_STMT_REFINE:
3484 ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3485 break;
3486 case LY_STMT_REQUIRE_INSTANCE:
3487 ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3488 break;
3489 case LY_STMT_REVISION:
3490 ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3491 break;
3492 case LY_STMT_REVISION_DATE:
3493 ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3494 break;
3495 case LY_STMT_STATUS:
3496 ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3497 break;
3498 case LY_STMT_TYPE:
3499 ret = yin_parse_type(ctx, current_element, subelem);
3500 break;
3501 case LY_STMT_TYPEDEF:
3502 ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3503 break;
3504 case LY_STMT_UNIQUE:
3505 ret = yin_parse_simple_elem(ctx, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
3506 break;
3507 case LY_STMT_USES:
3508 ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3509 break;
3510 case LY_STMT_WHEN:
3511 ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3512 break;
3513 case LY_STMT_YANG_VERSION:
3514 ret = yin_parse_yangversion(ctx, (uint8_t *)subelem->dest, exts);
3515 break;
3516 case LY_STMT_YIN_ELEMENT:
3517 ret = yin_parse_yin_element(ctx, (uint16_t *)subelem->dest, exts);
3518 break;
3519 case LY_STMT_ARG_TEXT:
3520 case LY_STMT_ARG_VALUE:
3521 /* TODO what to do with content/attributes? */
3522 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3523 while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3524 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3525 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3526 }
3527 ret = yin_parse_content(ctx, NULL, 0, kw, (const char **)subelem->dest, NULL);
3528 break;
3529 default:
3530 LOGINT(ctx->xmlctx->ctx);
3531 ret = LY_EINT;
David Sedlák3ffbc522019-07-02 17:49:28 +02003532 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003533 LY_CHECK_GOTO(ret, cleanup);
3534 subelem = NULL;
3535
3536 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003537 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003538 } else {
3539 LY_CHECK_RET(ret);
3540 /* elements with text or none content */
3541 /* save text content, if text_content isn't set, it's just ignored */
3542 /* no resources are allocated in this branch, no need to use cleanup label */
3543 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3544 if (text_content) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02003545 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 +01003546 LY_CHECK_RET(!*text_content, LY_EMEM);
3547 }
3548
3549 LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +02003550 }
Michal Vaskob36053d2020-03-26 15:49:30 +01003551
David Sedlák8b754462019-07-25 16:22:13 +02003552 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3553 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003554
3555cleanup:
David Sedlákd6e56892019-07-01 15:40:24 +02003556 return ret;
3557}
3558
David Sedlákaa98bba2019-09-12 11:52:14 +02003559/**
Michal Vaskoafac7822020-10-20 14:22:26 +02003560 * @brief Parse module element.
David Sedlákaa98bba2019-09-12 11:52:14 +02003561 *
3562 * @param[in,out] ctx Yin parser context for logging and to store current state.
Michal Vaskoafac7822020-10-20 14:22:26 +02003563 * @param[out] mod Parsed module structure.
David Sedlákaa98bba2019-09-12 11:52:14 +02003564 *
3565 * @return LY_ERR values.
3566 */
David Sedlák1e2cdd02019-06-27 14:17:43 +02003567LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003568yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003569{
David Sedlák81497a32019-08-13 16:56:26 +02003570 LY_ERR ret = LY_SUCCESS;
3571 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003572 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003573 size_t subelems_size;
David Sedlák3b4db242018-10-19 16:11:01 +02003574
Michal Vaskoc3781c32020-10-06 14:04:08 +02003575 mod->is_submod = 0;
3576
Michal Vaskob36053d2020-03-26 15:49:30 +01003577 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3578 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 +01003579 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003580 LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3581 LY_STMT_ANYXML, &mod->data, 0,
3582 LY_STMT_AUGMENT, &mod->augments, 0,
3583 LY_STMT_CHOICE, &mod->data, 0,
3584 LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3585 LY_STMT_CONTAINER, &mod->data, 0,
3586 LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3587 LY_STMT_DEVIATION, &mod->deviations, 0,
3588 LY_STMT_EXTENSION, &mod->extensions, 0,
3589 LY_STMT_FEATURE, &mod->features, 0,
3590 LY_STMT_GROUPING, &mod->groupings, 0,
3591 LY_STMT_IDENTITY, &mod->identities, 0,
3592 LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3593 LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3594 LY_STMT_LEAF, &mod->data, 0,
3595 LY_STMT_LEAF_LIST, &mod->data, 0,
3596 LY_STMT_LIST, &mod->data, 0,
3597 LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3598 LY_STMT_NOTIFICATION, &mod->notifs, 0,
3599 LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3600 LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3601 LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3602 LY_STMT_REVISION, &mod->revs, 0,
3603 LY_STMT_RPC, &mod->rpcs, 0,
3604 LY_STMT_TYPEDEF, &mod->typedefs, 0,
3605 LY_STMT_USES, &mod->data, 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003606 LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
Michal Vasko69730152020-10-09 16:30:07 +02003607 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003608
Radek Krejcif13b87b2020-12-01 22:02:17 +01003609 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_MODULE, NULL, &mod->exts);
3610 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003611 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003612
David Sedláked809f12019-09-13 10:15:11 +02003613 /* submodules share the namespace with the module names, so there must not be
3614 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003615 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003616 if (dup) {
Radek Krejci854e1552020-12-21 15:05:23 +01003617 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
David Sedláked809f12019-09-13 10:15:11 +02003618 return LY_EVALID;
3619 }
3620
3621 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +02003622}
3623
Michal Vaskoafac7822020-10-20 14:22:26 +02003624/**
3625 * @brief Parse submodule element.
3626 *
3627 * @param[in,out] ctx Yin parser context for logging and to store current state.
3628 * @param[in] mod_attrs Attributes of submodule element.
3629 * @param[out] submod Parsed submodule structure.
3630 *
3631 * @return LY_ERR values.
3632 */
David Sedlák3b4db242018-10-19 16:11:01 +02003633LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +01003634yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod)
David Sedlák298ff6d2019-07-26 14:29:03 +02003635{
David Sedlák81497a32019-08-13 16:56:26 +02003636 LY_ERR ret = LY_SUCCESS;
3637 struct yin_subelement *subelems = NULL;
Michal Vasko8dc31992021-02-22 10:30:47 +01003638 const struct lysp_submodule *dup;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003639 size_t subelems_size;
David Sedlák298ff6d2019-07-26 14:29:03 +02003640
Michal Vaskoc3781c32020-10-06 14:04:08 +02003641 submod->is_submod = 1;
3642
Michal Vaskob36053d2020-03-26 15:49:30 +01003643 LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3644 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 +01003645 LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
Michal Vasko69730152020-10-09 16:30:07 +02003646 LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3647 LY_STMT_ANYXML, &submod->data, 0,
3648 LY_STMT_AUGMENT, &submod->augments, 0,
3649 LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3650 LY_STMT_CHOICE, &submod->data, 0,
3651 LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3652 LY_STMT_CONTAINER, &submod->data, 0,
3653 LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3654 LY_STMT_DEVIATION, &submod->deviations, 0,
3655 LY_STMT_EXTENSION, &submod->extensions, 0,
3656 LY_STMT_FEATURE, &submod->features, 0,
3657 LY_STMT_GROUPING, &submod->groupings, 0,
3658 LY_STMT_IDENTITY, &submod->identities, 0,
3659 LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3660 LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3661 LY_STMT_LEAF, &submod->data, 0,
3662 LY_STMT_LEAF_LIST, &submod->data, 0,
3663 LY_STMT_LIST, &submod->data, 0,
3664 LY_STMT_NOTIFICATION, &submod->notifs, 0,
3665 LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3666 LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3667 LY_STMT_REVISION, &submod->revs, 0,
3668 LY_STMT_RPC, &submod->rpcs, 0,
3669 LY_STMT_TYPEDEF, &submod->typedefs, 0,
3670 LY_STMT_USES, &submod->data, 0,
3671 LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3672 LY_STMT_EXTENSION_INSTANCE, NULL, 0));
David Sedlák81497a32019-08-13 16:56:26 +02003673
Radek Krejcif13b87b2020-12-01 22:02:17 +01003674 ret = yin_parse_content(ctx, subelems, subelems_size, LY_STMT_SUBMODULE, NULL, &submod->exts);
3675 subelems_deallocator(subelems_size, subelems);
David Sedláked809f12019-09-13 10:15:11 +02003676 LY_CHECK_RET(ret);
David Sedlák81497a32019-08-13 16:56:26 +02003677
David Sedláked809f12019-09-13 10:15:11 +02003678 /* submodules share the namespace with the module names, so there must not be
3679 * a submodule of the same name in the context, no need for revision matching */
Michal Vasko8dc31992021-02-22 10:30:47 +01003680 dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, submod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003681 if (dup && strcmp(dup->mod->name, submod->mod->name)) {
Radek Krejci854e1552020-12-21 15:05:23 +01003682 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
David Sedláked809f12019-09-13 10:15:11 +02003683 return LY_EVALID;
3684 }
3685
3686 return LY_SUCCESS;
David Sedlák298ff6d2019-07-26 14:29:03 +02003687}
3688
3689LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +02003690yin_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 +02003691 struct ly_in *in, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003692{
Radek Krejcid6b76452019-09-03 17:03:03 +02003693 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák8985a142019-07-31 16:43:06 +02003694 LY_ERR ret = LY_SUCCESS;
David Sedlák8985a142019-07-31 16:43:06 +02003695 struct lysp_submodule *mod_p = NULL;
3696
3697 /* create context */
3698 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003699 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003700 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003701 (*yin_ctx)->unres = main_ctx->unres;
Michal Vasko63f3d842020-07-08 10:10:14 +02003702 LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
David Sedlák8985a142019-07-31 16:43:06 +02003703
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003704 mod_p = calloc(1, sizeof *mod_p);
3705 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3706 mod_p->mod = main_ctx->parsed_mod->mod;
3707 mod_p->parsing = 1;
3708 (*yin_ctx)->parsed_mod = (struct lysp_module *)mod_p;
3709
David Sedlák1b623122019-08-05 15:27:49 +02003710 /* map the typedefs and groupings list from main context to the submodule's context */
3711 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3712 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3713
David Sedlák8985a142019-07-31 16:43:06 +02003714 /* check submodule */
Michal Vaskob36053d2020-03-26 15:49:30 +01003715 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 +02003716 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003717 if (kw == LY_STMT_MODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003718 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3719 ret = LY_EINVAL;
3720 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003721 } else if (kw != LY_STMT_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003722 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003723 ret = LY_EVALID;
3724 goto cleanup;
3725 }
3726
Michal Vaskob36053d2020-03-26 15:49:30 +01003727 ret = yin_parse_submod(*yin_ctx, mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003728 LY_CHECK_GOTO(ret, cleanup);
3729
David Sedlákbf8a2b72019-08-14 16:48:10 +02003730 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003731 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003732 if (in->current[0] == '\n') {
3733 LY_IN_NEW_LINE(in);
3734 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003735 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003736 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003737 if (in->current[0]) {
3738 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003739 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003740 ret = LY_EVALID;
3741 goto cleanup;
3742 }
3743
David Sedlák8985a142019-07-31 16:43:06 +02003744 mod_p->parsing = 0;
3745 *submod = mod_p;
3746
3747cleanup:
3748 if (ret) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003749 lysp_module_free((struct lysp_module *)mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003750 yin_parser_ctx_free(*yin_ctx);
3751 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003752 }
David Sedlák8985a142019-07-31 16:43:06 +02003753 return ret;
3754}
3755
3756LY_ERR
Michal Vasko405cc9e2020-12-01 12:01:27 +01003757yin_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 +02003758{
David Sedláke4889912018-11-02 09:52:40 +01003759 LY_ERR ret = LY_SUCCESS;
Radek Krejcid6b76452019-09-03 17:03:03 +02003760 enum ly_stmt kw = LY_STMT_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003761 struct lysp_module *mod_p = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003762
David Sedlák8985a142019-07-31 16:43:06 +02003763 /* create context */
3764 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003765 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
Michal Vaskob36053d2020-03-26 15:49:30 +01003766 (*yin_ctx)->format = LYS_IN_YIN;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003767 (*yin_ctx)->unres = unres;
Michal Vasko63f3d842020-07-08 10:10:14 +02003768 LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
David Sedlákda8ffa32019-07-08 14:17:10 +02003769
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003770 mod_p = calloc(1, sizeof *mod_p);
3771 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3772 mod_p->mod = mod;
3773 mod_p->parsing = 1;
3774 (*yin_ctx)->parsed_mod = mod_p;
3775
David Sedlák8985a142019-07-31 16:43:06 +02003776 /* check module */
Michal Vaskob36053d2020-03-26 15:49:30 +01003777 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 +02003778 (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
Radek Krejcid6b76452019-09-03 17:03:03 +02003779 if (kw == LY_STMT_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003780 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 +01003781 ret = LY_EINVAL;
3782 goto cleanup;
Radek Krejcid6b76452019-09-03 17:03:03 +02003783 } else if (kw != LY_STMT_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003784 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003785 ret = LY_EVALID;
3786 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003787 }
3788
David Sedlák00250342019-06-21 14:19:39 +02003789 /* parse module substatements */
Michal Vaskob36053d2020-03-26 15:49:30 +01003790 ret = yin_parse_mod(*yin_ctx, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003791 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003792
David Sedlákbf8a2b72019-08-14 16:48:10 +02003793 /* skip possible trailing whitespaces at end of the input */
Michal Vasko63f3d842020-07-08 10:10:14 +02003794 while (isspace(in->current[0])) {
Radek Krejcidd713ce2021-01-04 23:12:12 +01003795 if (in->current[0] == '\n') {
3796 LY_IN_NEW_LINE(in);
3797 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003798 ly_in_skip(in, 1);
David Sedlák6d781b62019-08-02 15:22:52 +02003799 }
Michal Vasko63f3d842020-07-08 10:10:14 +02003800 if (in->current[0]) {
3801 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
Michal Vasko69730152020-10-09 16:30:07 +02003802 strlen(in->current) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003803 ret = LY_EVALID;
3804 goto cleanup;
3805 }
3806
David Sedlák3017da42019-02-15 09:48:04 +01003807 mod_p->parsing = 0;
3808 mod->parsed = mod_p;
3809
3810cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003811 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003812 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003813 yin_parser_ctx_free(*yin_ctx);
3814 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003815 }
David Sedlák2e411422018-12-17 02:35:39 +01003816 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003817}