blob: b366f864a33b166e39eb45ea42ed3b5d32f5b9ba [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 */
David Sedlákecf5eb82019-06-03 14:12:44 +020014#include "common.h"
15
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
David Sedlák3b4db242018-10-19 16:11:01 +020017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020020#include <string.h>
David Sedlákd6e56892019-07-01 15:40:24 +020021#include <stdbool.h>
David Sedlák5545f5d2019-07-11 11:55:16 +020022#include <errno.h>
David Sedlákf75d55e2019-07-12 16:52:50 +020023#include <ctype.h>
David Sedlákf824ad52018-10-14 23:58:15 +020024
David Sedlákf824ad52018-10-14 23:58:15 +020025#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020026#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020027#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020028#include "tree.h"
29#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020030#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020031#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020032
David Sedlák2b214ac2019-06-06 16:11:03 +020033/**
34 * @brief check if given string is URI of yin namespace.
35 * @param ns Namespace URI to check.
36 *
37 * @return true if ns equals YIN_NS_URI false otherwise.
38 */
39#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
40
David Sedláke1a30302019-07-10 13:49:38 +020041static LY_ERR
42yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
43 struct lysp_ext_instance **exts);
44
David Sedlákf6251182019-06-06 10:22:13 +020045const char *const yin_attr_list[] = {
46 [YIN_ARG_NAME] = "name",
47 [YIN_ARG_TARGET_NODE] = "target-node",
48 [YIN_ARG_MODULE] = "module",
49 [YIN_ARG_VALUE] = "value",
50 [YIN_ARG_TEXT] = "text",
51 [YIN_ARG_CONDITION] = "condition",
52 [YIN_ARG_URI] = "uri",
53 [YIN_ARG_DATE] = "date",
54 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020055};
56
David Sedlák1bccdfa2019-06-17 15:55:27 +020057enum yang_keyword
David Sedlákc1771b12019-07-10 15:55:46 +020058yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
59 const char *prefix, size_t prefix_len, enum yang_keyword parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020060{
David Sedlák8f7a1172019-06-20 14:42:18 +020061 const char *start = NULL;
62 enum yang_keyword kw = YANG_NONE;
63 const struct lyxml_ns *ns = NULL;
64
65 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020066 return YANG_NONE;
67 }
68
David Sedlákda8ffa32019-07-08 14:17:10 +020069 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020070 if (ns) {
71 if (!IS_YIN_NS(ns->uri)) {
72 return YANG_CUSTOM;
73 }
74 } else {
75 /* elements without namespace are automatically unknown */
76 return YANG_NONE;
77 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020078
David Sedlák8f7a1172019-06-20 14:42:18 +020079 start = name;
80 kw = lysp_match_kw(NULL, &name);
81
82 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020083 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
84 if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) {
85 return YIN_VALUE;
86 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020087 return kw;
88 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020089 if (strncmp(start, "text", name_len) == 0) {
90 return YIN_TEXT;
91 } else if (strncmp(start, "value", name_len) == 0) {
92 return YIN_VALUE;
93 } else {
94 return YANG_NONE;
95 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020096 }
97}
98
David Sedlák872c7b42018-10-26 13:15:20 +020099enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +0200100yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200101{
David Sedláka7406952019-04-05 10:33:07 +0200102 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200103 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200104 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200105
David Sedlák94de2aa2019-02-15 12:42:11 +0100106#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
107#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100108#define IF_ARG_PREFIX_END }
109
David Sedlák1c8b2702019-02-22 11:03:02 +0100110 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100111 case 'c':
112 already_read += 1;
113 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200114 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200115
David Sedlák94de2aa2019-02-15 12:42:11 +0100116 case 'd':
117 already_read += 1;
118 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200119 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200120
David Sedlák94de2aa2019-02-15 12:42:11 +0100121 case 'm':
122 already_read += 1;
123 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200124 break;
125
David Sedlák94de2aa2019-02-15 12:42:11 +0100126 case 'n':
127 already_read += 1;
128 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200129 break;
130
David Sedlák94de2aa2019-02-15 12:42:11 +0100131 case 't':
132 already_read += 1;
133 IF_ARG_PREFIX("a", 1)
134 IF_ARG("g", 1, YIN_ARG_TAG)
135 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
136 IF_ARG_PREFIX_END
137 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200138 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200139
David Sedlák94de2aa2019-02-15 12:42:11 +0100140 case 'u':
141 already_read += 1;
142 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200143 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200144
David Sedlák94de2aa2019-02-15 12:42:11 +0100145 case 'v':
146 already_read += 1;
147 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200148 break;
149 }
150
David Sedlákc10e7902018-12-17 02:17:59 +0100151 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200152 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200153 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200154 }
155
David Sedlák18730132019-03-15 15:51:34 +0100156#undef IF_ARG
157#undef IF_ARG_PREFIX
158#undef IF_ARG_PREFIX_END
159
David Sedlák872c7b42018-10-26 13:15:20 +0200160 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200161}
162
David Sedlákb4e44562019-07-04 15:42:12 +0200163/**
164 * @brief free argument record, content loaded from lyxml_get_string() can be
165 * dynamically allocated in some cases so it must be also freed.
166 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200167static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
168 (void)ctx; /* unused */
David Sedlákd2d676a2019-07-22 11:28:19 +0200169 if (record && record->dynamic_content) {
David Sedlák00250342019-06-21 14:19:39 +0200170 free(record->content);
171 }
172}
173
David Sedlák8f7a1172019-06-20 14:42:18 +0200174LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200175yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200176{
177 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200178 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200179 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200180
David Sedlák555c7202019-07-04 12:14:12 +0200181 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200182 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
183 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200184 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200185
David Sedlákda8ffa32019-07-08 14:17:10 +0200186 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
187 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200188 argument_record->name = name.value;
189 argument_record->name_len = name.len;
190 argument_record->prefix = prefix.value;
191 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200192 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200193 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200194 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200195 }
196 }
197
David Sedlák8f7a1172019-06-20 14:42:18 +0200198cleanup:
199 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200200 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200201 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200202 }
203 return ret;
204}
205
David Sedlák4a650532019-07-10 11:55:18 +0200206LY_ERR
207yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
208{
209 int prefix = 0;
210 unsigned int c;
211 size_t utf8_char_len;
212 size_t already_read = 0;
213 while (already_read < len) {
214 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
215 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
216 already_read += utf8_char_len;
217 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
218
219 switch (val_type) {
220 case Y_IDENTIF_ARG:
221 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
222 break;
223 case Y_PREF_IDENTIF_ARG:
224 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
225 break;
226 case Y_STR_ARG:
227 case Y_MAYBE_STR_ARG:
228 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
229 break;
230 }
231 }
232
233 return LY_SUCCESS;
234}
235
David Sedlákb4e44562019-07-04 15:42:12 +0200236/**
237 * @brief Parse yin argument.
238 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200239 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200240 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200241 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200242 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
243 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200244 * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE.
David Sedlák292763b2019-07-09 11:10:53 +0200245 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200246 * @param[in] current_element Identification of current element, used for logging.
247 *
248 * @return LY_ERR values.
249 */
250static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +0200251yin_parse_attribute(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, enum YIN_ARGUMENT arg_type,
David Sedlák292763b2019-07-09 11:10:53 +0200252 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200253{
David Sedlák8f7a1172019-06-20 14:42:18 +0200254 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
255 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200256 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200257
David Sedlák1bccdfa2019-06-17 15:55:27 +0200258 /* validation of attributes */
David Sedlák1f90d252019-07-10 17:09:32 +0200259 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200260 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
261 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200262 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200263 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200264 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200265 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200266 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
267 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200268 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200269 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200270 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200271 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200272 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200273 /* string is no longer supposed to be freed when the sized array is freed */
274 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200275 } else {
David Sedlák99295322019-07-17 11:34:18 +0200276 if (iter->content_len == 0) {
277 *arg_val = lydict_insert(ctx->xml_ctx.ctx, "", 0);
278 } else {
279 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
280 LY_CHECK_RET(!(*arg_val), LY_EMEM);
281 }
David Sedlák57715b12019-06-17 13:05:22 +0200282 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200283 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200284 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\" of %s element.", iter->name_len, iter->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200285 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200286 }
287 }
288 }
289
David Sedlák292763b2019-07-09 11:10:53 +0200290 /* anything else than Y_MAYBE_STR_ARG is mandatory */
291 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200292 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.", yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200293 return LY_EVALID;
294 }
295
296 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200297}
298
David Sedlákd6e56892019-07-01 15:40:24 +0200299/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200300 * @brief Get record with given type. Array must be sorted in ascending order by array[n].type.
David Sedlákd6e56892019-07-01 15:40:24 +0200301 *
302 * @param[in] type Type of wanted record.
303 * @param[in] array_size Size of array.
304 * @param[in] array Searched array.
305 *
306 * @return Pointer to desired record on success, NULL if element is not in the array.
307 */
David Sedlákb4e44562019-07-04 15:42:12 +0200308static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200309get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200310{
David Sedlákb0faad82019-07-04 14:28:59 +0200311 signed char left = 0, right = array_size - 1, middle;
312
313 while (left <= right) {
314 middle = left + (right - left) / 2;
315
316 if (array[middle].type == type) {
317 return &array[middle];
318 }
319
320 if (array[middle].type < type) {
321 left = middle + 1;
322 } else {
323 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200324 }
325 }
326
327 return NULL;
328}
329
David Sedlákbba38e52019-07-09 15:20:01 +0200330/**
331 * @brief Helper function to check mandatory constraint of subelement.
332 *
333 * @param[in,out] ctx Yin parser context for logging and to store current state.
334 * @param[in] subelem_info Array of information about subelements.
335 * @param[in] subelem_info_size Size of subelem_info array.
336 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
337 *
338 * @return LY_ERR values.
339 */
340static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200341yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200342 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200343{
David Sedlákb0faad82019-07-04 14:28:59 +0200344 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200345 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
David Sedlák21f87cd2019-07-03 16:53:23 +0200346 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200347 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory subelement %s of %s element.",
David Sedlák555c7202019-07-04 12:14:12 +0200348 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200349 return LY_EVALID;
350 }
351 }
352
353 return LY_SUCCESS;
354}
355
David Sedlákbba38e52019-07-09 15:20:01 +0200356/**
357 * @brief Helper function to check "first" constraint of subelement.
358 *
359 * @param[in,out] ctx Yin parser context for logging and to store current state.
360 * @param[in] subelem_info Array of information about subelements.
361 * @param[in] subelem_info_size Size of subelem_info array.
362 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
363 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
364 *
365 * @return LY_ERR values.
366 */
367static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200368yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200369 signed char subelem_info_size, enum yang_keyword current_element,
370 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200371{
David Sedlákb0faad82019-07-04 14:28:59 +0200372 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200373 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200374 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Subelement %s of %s element must be defined as first subelement.",
David Sedlák555c7202019-07-04 12:14:12 +0200375 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200376 return LY_EVALID;
377 }
378 }
379
380 return LY_SUCCESS;
381}
382
David Sedlákbba38e52019-07-09 15:20:01 +0200383/**
384 * @brief Helper function to check if array of information about subelements is in ascending order.
385 *
386 * @param[in] subelem_info Array of information about subelements.
387 * @param[in] subelem_info_size Size of subelem_info array.
388 *
389 * @return True iff subelem_info array is in ascending order, False otherwise.
390 */
David Sedlák5545f5d2019-07-11 11:55:16 +0200391#ifndef NDEBUG
David Sedlákbba38e52019-07-09 15:20:01 +0200392static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200393is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
394{
David Sedlák292763b2019-07-09 11:10:53 +0200395 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200396
397 for (signed char i = 0; i < subelem_info_size; ++i) {
398 if (subelem_info[i].type <= current) {
399 return false;
400 }
401 current = subelem_info[i].type;
402 }
403
404 return true;
405}
David Sedlák5545f5d2019-07-11 11:55:16 +0200406#endif
David Sedlákb0faad82019-07-04 14:28:59 +0200407
David Sedlákd6e56892019-07-01 15:40:24 +0200408/**
David Sedlákb4e44562019-07-04 15:42:12 +0200409 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
410 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200411 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200412 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200413 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200414 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200415 * @param[in] kw Type of current element.
416 * @param[out] value Where value of attribute should be stored.
417 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200418 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200419 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200420 *
David Sedlákd6e56892019-07-01 15:40:24 +0200421 * @return LY_ERR values.
422 */
David Sedlákb4e44562019-07-04 15:42:12 +0200423static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200424yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlák292763b2019-07-09 11:10:53 +0200425 const char **value, enum YIN_ARGUMENT arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200426{
David Sedlák1f90d252019-07-10 17:09:32 +0200427 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200428 struct yin_subelement subelems[1] = {
429 {YANG_CUSTOM, NULL, 0}
430 };
David Sedlákb4e44562019-07-04 15:42:12 +0200431
David Sedlákda8ffa32019-07-08 14:17:10 +0200432 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200433}
434
435/**
David Sedlákd3983112019-07-12 11:20:56 +0200436 * @brief Parse pattern element.
437 *
438 * @param[in,out] ctx Yin parser context for logging and to store current state.
439 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
440 * @param[in,out] data Data to read from, always moved to currently handled character.
441 * @param[in,out] patterns Restrictions to add to.
442 *
443 * @return LY_ERR values.
444 */
445static LY_ERR
446yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
447 struct lysp_type *type)
448{
449 const char *real_value = NULL;
450 char *saved_value = NULL;
451 struct lysp_restr *restr;
452
453 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
454 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
455 size_t len = strlen(real_value);
456
457 saved_value = malloc(len + 2);
458 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
459 memmove(saved_value + 1, real_value, len);
460 FREE_STRING(ctx->xml_ctx.ctx, real_value);
461 saved_value[0] = 0x06;
462 saved_value[len + 1] = '\0';
463 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
464 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
465 type->flags |= LYS_SET_PATTERN;
466
467 struct yin_subelement subelems[6] = {
468 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
469 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
470 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
471 {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
472 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
473 {YANG_CUSTOM, NULL, 0}
474 };
475 return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
476}
477
David Sedlákf75d55e2019-07-12 16:52:50 +0200478static LY_ERR
479yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
480 struct lysp_type *type)
481{
482 const char *temp_val = NULL;
483 char *ptr;
484 unsigned long int num;
485
486 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_FRACTION_DIGITS));
487
488 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
489 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
490 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
491 return LY_EVALID;
492 }
493
494 errno = 0;
495 num = strtoul(temp_val, &ptr, 10);
496 if (*ptr != '\0') {
497 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
498 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
499 return LY_EVALID;
500 }
501 if ((errno == ERANGE) || (num > 18)) {
502 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
503 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
504 return LY_EVALID;
505 }
David Sedlák2ab5d8e2019-07-16 11:19:41 +0200506 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200507 type->fraction_digits = num;
508 type->flags |= LYS_SET_FRDIGITS;
509 struct yin_subelement subelems[1] = {
510 {YANG_CUSTOM, &index, 0}
511 };
512 return yin_parse_content(ctx, subelems, 1, data, YANG_FRACTION_DIGITS, NULL, &type->exts);
513}
514
David Sedlák07869a52019-07-12 14:28:19 +0200515/**
516 * @brief Parse enum or bit element.
517 *
518 * @param[in,out] ctx YIN parser context for logging and to store current state.
519 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
520 * @param[in,out] data Data to read from, always moved to currently handled character.
521 * @param[in] enum_kw Identification of actual keyword, can be set to YANG_BIT or YANG_ENUM.
522 * @param[in,out] enums Enums or bits to add to.
523 *
524 * @return LY_ERR values.
525 */
David Sedlákca36c422019-07-12 12:47:55 +0200526static LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200527yin_parse_enum_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
528 enum yang_keyword enum_kw, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200529{
David Sedlák07869a52019-07-12 14:28:19 +0200530 assert(enum_kw == YANG_BIT || enum_kw == YANG_ENUM);
531 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200532 struct lysp_type_enum **enums;
533
534 if (enum_kw == YANG_BIT) {
535 enums = &type->bits;
536 } else {
537 enums = &type->enums;
538 }
539
540 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *enums, en, LY_EMEM);
David Sedlák07869a52019-07-12 14:28:19 +0200541 type->flags |= (enum_kw == YANG_ENUM) ? LYS_SET_ENUM : LYS_SET_BIT;
David Sedlák1e696782019-07-17 15:06:07 +0200542 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, enum_kw));
David Sedlák07869a52019-07-12 14:28:19 +0200543 if (enum_kw == YANG_ENUM) {
544 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
David Sedlákb9b892c2019-07-12 14:44:02 +0200545 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák07869a52019-07-12 14:28:19 +0200546 }
David Sedlák1e696782019-07-17 15:06:07 +0200547 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, *enums, name, ly_stmt2str(enum_kw), en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200548
549 struct yin_subelement subelems[6] = {
David Sedlák07869a52019-07-12 14:28:19 +0200550 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
551 {YANG_IF_FEATURE, &en->iffeatures, 0},
David Sedlák07869a52019-07-12 14:28:19 +0200552 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
553 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200554 {(enum_kw == YANG_ENUM) ? YANG_VALUE : YANG_POSITION, en, YIN_SUBELEM_UNIQUE},
David Sedlákca36c422019-07-12 12:47:55 +0200555 {YANG_CUSTOM, NULL, 0}
556 };
David Sedlák07869a52019-07-12 14:28:19 +0200557 return yin_parse_content(ctx, subelems, 6, data, enum_kw, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200558}
559
David Sedlákd3983112019-07-12 11:20:56 +0200560/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200561 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
562 * more instances, such as base or if-feature.
563 *
564 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200565 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200566 * @param[in,out] data Data to read from, always moved to currently handled character.
567 * @param[in] kw Type of current element.
568 * @param[out] values Parsed values to add to.
569 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200570 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200571 * @param[in,out] exts Extension instance to add to.
572 *
573 * @return LY_ERR values.
574 */
575static LY_ERR
576yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlák292763b2019-07-09 11:10:53 +0200577 const char ***values, enum YIN_ARGUMENT arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200578{
579 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200580 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200581 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200582 struct yin_subelement subelems[1] = {
583 {YANG_CUSTOM, &index, 0}
584 };
585
David Sedlák1f90d252019-07-10 17:09:32 +0200586 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200587
588 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
589}
590
591/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200592 * @brief Parse require instance element.
593 *
594 * @param[in,out] ctx Yin parser context for logging and to store current state.
595 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
596 * @param[in,out] data Data to read from, always moved to currently handled character.
597 * @prama[out] type Type structure to store value, flag and extensions.
598 *
599 * @return LY_ERR values.
600 */
601static LY_ERR
602yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
603 const char **data, struct lysp_type *type)
604{
605 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200606 struct yin_subelement subelems[1] = {
607 {YANG_CUSTOM, NULL, 0}
608 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200609
610 type->flags |= LYS_SET_REQINST;
611 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
612 if (strcmp(temp_val, "true") == 0) {
613 type->require_instance = 1;
614 } else if (strcmp(temp_val, "false") != 0) {
615 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "require-instance");
616 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
617 return LY_EVALID;
618 }
619 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
620
621 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
622}
623
624/**
David Sedlákce77bf52019-07-11 16:59:31 +0200625 * @brief Parse modifier element.
626 *
627 * @param[in,out] ctx Yin parser context for logging and to store current state.
628 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
629 * @param[in,out] data Data to read from, always moved to currently handled character.
630 * @param[in,out] pat Value to write to.
631 * @param[in,out] exts Extension instances to add to.
632 *
633 * @return LY_ERR values.
634 */
635static LY_ERR
636yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
637 const char **pat, struct lysp_ext_instance **exts)
638{
David Sedlákd3983112019-07-12 11:20:56 +0200639 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200640 const char *temp_val;
641 char *modified_val;
642 struct yin_subelement subelems[1] = {
643 {YANG_CUSTOM, NULL, 0}
644 };
645
646 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER));
647 if (strcmp(temp_val, "invert-match") != 0) {
648 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "modifier");
649 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
650 return LY_EVALID;
651 }
David Sedlákd3983112019-07-12 11:20:56 +0200652 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200653
654 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200655 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200656 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200657 strcpy(modified_val, *pat);
658 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200659
660 /* modify the new value */
661 modified_val[0] = 0x15;
662 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
663
664 return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts);
665}
666
667/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200668 * @brief Parse a restriction element (length, range or one instance of must).
669 *
670 * @param[in,out] ctx Yin parser context for logging and to store current state.
671 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
672 * @param[in,out] data Data to read from, always moved to currently handled character.
673 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
674 * @param[in]
675 */
676static LY_ERR
677yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
678 enum yang_keyword restr_kw, struct lysp_restr *restr)
679{
680 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
681 struct yin_subelement subelems[5] = {
David Sedlák968ac342019-07-11 15:17:59 +0200682 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
683 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
684 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
685 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
686 {YANG_CUSTOM, NULL, 0}
687 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200688 /* argument of must is called condition, but argument of length and range is called value */
689 enum YIN_ARGUMENT arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
690 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
691
692 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
693}
694
695/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200696 * @brief Parse must element.
697 *
698 * @param[in,out] ctx YIN parser context for logging and to store current state.
699 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
700 * @param[in,out] data Data to read from, always moved to currently handled character.
701 * @param[in,out] restrs Restrictions to add to.
702 *
703 * @return LY_ERR values.
704 */
705static LY_ERR
706yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
707{
708 struct lysp_restr *restr;
709
710 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
711 return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr);
712}
713
714/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200715 * @brief Parse position or value element.
716 *
717 * @param[in,out] ctx YIN parser context for logging and to store current state.
718 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
719 * @param[in,out] data Data to read from, always moved to currently handled character.
720 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
721 * @param[out] enm Enum structure to save value, flags and extensions.
722 *
723 * @return LY_ERR values.
724 */
725static LY_ERR
726yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
727 enum yang_keyword kw, struct lysp_type_enum *enm)
728{
729 assert(kw == YANG_POSITION || kw == YANG_VALUE);
730 const char *temp_val = NULL;
731 char *ptr;
732 long int num;
733 unsigned long int unum;
734
735 /* set value flag */
736 enm->flags |= LYS_SET_VALUE;
737
738 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200739 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlákae5378f2019-07-17 11:37:59 +0200740 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') ||
741 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == YANG_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200742 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
743 goto error;
744 }
745
746 /* convert value */
747 errno = 0;
748 if (kw == YANG_VALUE) {
749 num = strtol(temp_val, &ptr, 10);
750 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
751 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
752 goto error;
753 }
754 } else {
755 unum = strtoul(temp_val, &ptr, 10);
756 if (unum > UINT64_C(4294967295)) {
757 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
758 goto error;
759 }
760 }
761 /* check if whole argument value was converted */
762 if (*ptr != '\0') {
763 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +0200764 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +0200765 }
766 if (errno == ERANGE) {
767 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw));
768 goto error;
769 }
770 /* save correctly ternary operator can't be used because num and unum have different signes */
771 if (kw == YANG_VALUE) {
772 enm->value = num;
773 } else {
774 enm->value = unum;
775 }
776 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
777
778 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200779 struct yin_subelement subelems[1] = {
780 {YANG_CUSTOM, NULL, 0}
781 };
David Sedlák5545f5d2019-07-11 11:55:16 +0200782 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
783
784 error:
785 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
786 return LY_EVALID;
787}
788
David Sedlák05404f62019-07-24 14:11:53 +0200789
790/**
791 * @brief Parse belongs-to element.
792 *
793 * @param[in] ctx Yin parser context for logging and to store current state.
794 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
795 * @param[in,out] data Data to read from, always moved to currently handled character.
796 * @param[out] submod Structure of submodule that is being parsed.
797 * @param[in,out] exts Extension instances to add to.
798 *
799 * @return LY_ERR values
800 */
801static LY_ERR
802yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
803 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
804{
805 struct yin_subelement subelems[2] = {
806 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
807 {YANG_CUSTOM, NULL, 0}
808 };
809 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, YANG_BELONGS_TO));
810
811 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
812}
813
David Sedlák5545f5d2019-07-11 11:55:16 +0200814/**
David Sedlákc1771b12019-07-10 15:55:46 +0200815 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200816 * text element as child
817 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200818 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200819 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200820 * @param[in] Type of element can be set to YANG_ORGANIZATION or YANG_CONTACT or YANG_DESCRIPTION or YANG_REFERENCE.
David Sedlákb4e44562019-07-04 15:42:12 +0200821 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200822 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200823 *
824 * @return LY_ERR values.
825 */
826static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200827yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200828 const char **value, struct lysp_ext_instance **exts)
829{
830 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
831
David Sedlák968ac342019-07-11 15:17:59 +0200832 struct yin_subelement subelems[2] = {
833 {YANG_CUSTOM, NULL, 0},
834 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
835 };
David Sedlákb4e44562019-07-04 15:42:12 +0200836
David Sedlákda8ffa32019-07-08 14:17:10 +0200837 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200838}
839
840/**
David Sedlákc1771b12019-07-10 15:55:46 +0200841 * @brief Parse error-message element.
842 *
843 * @param[in,out] ctx Yin parser context for logging and to store current state.
844 * @param[in,out] data Data to read from.
845 * @param[out] value Where the content of error-message element should be stored.
846 * @param[in,out] exts Extension instance to add to.
847 *
848 * @return LY_ERR values.
849 */
850static LY_ERR
851yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
852 struct lysp_ext_instance **exts)
853{
David Sedlák968ac342019-07-11 15:17:59 +0200854 struct yin_subelement subelems[2] = {
855 {YANG_CUSTOM, NULL, 0},
856 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
857 };
David Sedlákc1771b12019-07-10 15:55:46 +0200858
859 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
860}
861
862/**
David Sedlák374d2b32019-07-17 15:06:55 +0200863 * @brief parse type element.
864 *
865 * @brief Parse position or value element.
866 *
867 * @param[in,out] ctx YIN parser context for logging and to store current state.
868 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
869 * @param[in,out] data Data to read from, always moved to currently handled character.
870 * @param[in,out] type Type to wrote to.
871 * @param[in,out] exts Extension instance to add to.
872 *
873 * @return LY_ERR values.
874 */
875static LY_ERR
876yin_parse_type(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type)
877{
878 struct yin_subelement subelems[11] = {
879 {YANG_BASE, type, 0},
880 {YANG_BIT, type, 0},
881 {YANG_ENUM, type, 0},
882 {YANG_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
883 {YANG_LENGTH, type, YIN_SUBELEM_UNIQUE},
884 {YANG_PATH, type, YIN_SUBELEM_UNIQUE},
885 {YANG_PATTERN, type, 0},
886 {YANG_RANGE, type, YIN_SUBELEM_UNIQUE},
887 {YANG_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
888 {YANG_TYPE, type},
889 {YANG_CUSTOM, NULL, 0},
890 };
891 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, YANG_TYPE));
892 return yin_parse_content(ctx, subelems, 11, data, YANG_TYPE, NULL, &type->exts);
893}
894
David Sedlák1af868e2019-07-17 17:03:14 +0200895/**
896 * @brief Parse max-elements element.
897 *
898 * @param[in,out] ctx YIN parser context for logging and to store current state.
899 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
900 * @param[in,out] data Data to read from, always moved to currently handled character.
901 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +0200902 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +0200903 * @param[in,out] exts Extension instances to add to.
904 *
905 * @return LY_ERR values.
906 */
907static LY_ERR
908yin_parse_maxelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *max,
909 uint16_t *flags, struct lysp_ext_instance **exts)
910{
911 const char *temp_val = NULL;
912 char *ptr;
913 unsigned long int num;
914 struct yin_subelement subelems[1] = {
915 {YANG_CUSTOM, NULL, 0},
916 };
David Sedlák374d2b32019-07-17 15:06:55 +0200917
David Sedlák1af868e2019-07-17 17:03:14 +0200918 *flags |= LYS_SET_MAX;
919 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MAX_ELEMENTS));
920 if (!temp_val || temp_val[0] == '\0' || temp_val[0] == '0' || (temp_val[0] != 'u' && !isdigit(temp_val[0]))) {
921 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "max-elements");
922 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
923 return LY_EVALID;
924 }
925
926 if (strcmp(temp_val, "unbounded")) {
927 errno = 0;
928 num = strtoul(temp_val, &ptr, 10);
929 if (*ptr != '\0') {
930 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "max-elements");
931 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
932 return LY_EVALID;
933 }
934 if ((errno == ERANGE) || (num > UINT32_MAX)) {
935 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, "max-elements");
936 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
937 return LY_EVALID;
938 }
939 *max = num;
940 }
941 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
942 return yin_parse_content(ctx, subelems, 1, data, YANG_MAX_ELEMENTS, NULL, exts);
943}
David Sedlák374d2b32019-07-17 15:06:55 +0200944
945/**
David Sedlák09e18c92019-07-18 11:17:11 +0200946 * @brief Parse max-elements element.
947 *
948 * @param[in,out] ctx YIN parser context for logging and to store current state.
949 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
950 * @param[in,out] data Data to read from, always moved to currently handled character.
951 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +0200952 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +0200953 * @param[in,out] exts Extension instances to add to.
954 *
955 * @return LY_ERR values.
956 */
957static LY_ERR
958yin_parse_minelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *min,
959 uint16_t *flags, struct lysp_ext_instance **exts)
960{
961 const char *temp_val = NULL;
962 char *ptr;
963 unsigned long int num;
964 struct yin_subelement subelems[1] = {
965 {YANG_CUSTOM, NULL, 0},
966 };
967
968 *flags |= LYS_SET_MIN;
David Sedláka2dad212019-07-18 12:45:19 +0200969 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MIN_ELEMENTS));
David Sedlák09e18c92019-07-18 11:17:11 +0200970
971 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) {
972 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "min-elements");
973 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
974 return LY_EVALID;
975 }
976
977 errno = 0;
978 num = strtoul(temp_val, &ptr, 10);
979 if (ptr[0] != 0) {
980 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "min-elements");
981 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
982 return LY_EVALID;
983 }
984 if (errno == ERANGE || num > UINT32_MAX) {
985 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, "min-elements");
986 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
987 return LY_EVALID;
988 }
989 *min = num;
990 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +0200991 return yin_parse_content(ctx, subelems, 1, data, YANG_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +0200992}
993
David Sedláka2dad212019-07-18 12:45:19 +0200994/**
995 * @brief Parse min-elements or max-elements element.
996 *
997 * @param[in,out] ctx YIN parser context for logging and to store current state.
998 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
999 * @param[in,out] data Data to read from, always moved to currently handled character.
1000 * @param[in] parent Identification of parent element.
1001 * @param[in] current Identification of current element.
1002 * @param[in] dest Where the parsed value and flags should be stored.
1003 *
1004 * @return LY_ERR values.
1005 */
David Sedlák09e18c92019-07-18 11:17:11 +02001006static LY_ERR
1007yin_parse_minmax(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1008 enum yang_keyword parent, enum yang_keyword current, void *dest)
1009{
1010 assert(current == YANG_MAX_ELEMENTS || current == YANG_MIN_ELEMENTS);
1011 assert(parent == YANG_LEAF_LIST || parent == YANG_REFINE || parent == YANG_LIST);
1012 uint32_t *lim;
1013 uint16_t *flags;
1014 struct lysp_ext_instance **exts;
1015
1016 if (parent == YANG_LEAF_LIST) {
1017 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
1018 flags = &((struct lysp_node_leaflist *)dest)->flags;
1019 exts = &((struct lysp_node_leaflist *)dest)->exts;
1020 } else if (parent == YANG_REFINE) {
1021 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
1022 flags = &((struct lysp_refine *)dest)->flags;
1023 exts = &((struct lysp_refine *)dest)->exts;
1024 } else {
1025 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
1026 flags = &((struct lysp_node_list *)dest)->flags;
1027 exts = &((struct lysp_node_list *)dest)->exts;
1028 }
1029
1030 if (current == YANG_MAX_ELEMENTS) {
1031 LY_CHECK_RET(yin_parse_maxelements(ctx, attrs, data, lim, flags, exts));
1032 } else {
1033 LY_CHECK_RET(yin_parse_minelements(ctx, attrs, data, lim, flags, exts));
1034 }
1035
1036 return LY_SUCCESS;
1037}
1038
1039/**
David Sedláka2dad212019-07-18 12:45:19 +02001040 * @brief Parser ordered-by element.
1041 *
1042 * @param[in,out] ctx YIN parser context for logging and to store current state.
1043 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1044 * @param[in,out] data Data to read from, always moved to currently handled character.
1045 * @param[out] flags Flags to write to.
1046 * @param[in,out] exts Extension instance to add to.
1047 *
1048 * @return LY_ERR values.
1049 */
1050static LY_ERR
1051yin_parse_orderedby(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1052 uint16_t *flags, struct lysp_ext_instance **exts)
1053{
1054 const char *temp_val;
1055 struct yin_subelement subelems[1] = {
1056 {YANG_CUSTOM, NULL, 0},
1057 };
1058
1059 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_ORDERED_BY));
1060 if (strcmp(temp_val, "system") == 0) {
1061 *flags |= LYS_ORDBY_SYSTEM;
1062 } else if (strcmp(temp_val, "user") == 0) {
1063 *flags |= LYS_ORDBY_USER;
1064 } else {
1065 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "ordered-by");
1066 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1067 return LY_EVALID;
1068 }
1069 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1070
1071 return yin_parse_content(ctx, subelems, 1, data, YANG_ORDERED_BY, NULL, exts);
1072}
1073
1074/**
David Sedlák8a83bbb2019-07-18 14:46:00 +02001075 * @brief parse any-data or any-xml element.
1076 *
1077 * @param[in,out] ctx YIN parser context for logging and to store current state.
1078 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1079 * @param[in,out] data Data to read from, always moved to currently handled character.
1080 * @param[in] any_kw Identification of current element, can be set to YANG_ANY_DATA or YANG_ANY_XML
David Sedlák05404f62019-07-24 14:11:53 +02001081 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001082 *
1083 * @return LY_ERR values.
1084 */
1085static LY_ERR
1086yin_parse_any(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1087 enum yang_keyword any_kw, struct tree_node_meta *node_meta)
1088{
1089 struct lysp_node *iter;
1090 struct lysp_node_anydata *any;
1091
1092 /* create structure */
1093 any = calloc(1, sizeof *any);
1094 LY_CHECK_ERR_RET(!any, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1095 any->nodetype = (any_kw == YANG_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
1096 any->parent = node_meta->parent;
1097
1098 /* insert into siblings */
1099 if (!*(node_meta->siblings)) {
1100 *(node_meta->siblings) = (struct lysp_node *)any;
1101 } else {
1102 for (iter = *(node_meta->siblings); iter->next; iter = iter->next);
1103 iter->next = (struct lysp_node *)any;
1104 }
1105
1106 /* parser argument */
David Sedlák203ca3a2019-07-18 15:26:25 +02001107 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
David Sedlák8a83bbb2019-07-18 14:46:00 +02001108
1109 struct yin_subelement subelems[9] = {
1110 {YANG_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1111 {YANG_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1112 {YANG_IF_FEATURE, &any->iffeatures, 0},
1113 {YANG_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1114 {YANG_MUST, &any->musts, 0},
1115 {YANG_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1116 {YANG_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1117 {YANG_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1118 {YANG_CUSTOM, NULL, 0},
1119 };
1120 return yin_parse_content(ctx, subelems, 9, data, any_kw, NULL, &any->exts);
1121}
1122
1123/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001124 * @brief parse leaf element.
1125 *
1126 * @param[in,out] ctx YIN parser context for logging and to store current state.
1127 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1128 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001129 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák203ca3a2019-07-18 15:26:25 +02001130 *
1131 * @return LY_ERR values.
1132 */
1133static LY_ERR
1134yin_parse_leaf(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1135 struct tree_node_meta *node_meta)
1136{
1137 struct lysp_node *iter;
1138 struct lysp_node_leaf *leaf;
1139
1140 /* create structure */
1141 leaf = calloc(1, sizeof *leaf);
1142 LY_CHECK_ERR_RET(!leaf, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1143 leaf->nodetype = LYS_LEAF;
1144 leaf->parent = node_meta->parent;
1145
1146 /* insert into siblings */
1147 if (!*(node_meta->siblings)) {
1148 *node_meta->siblings = (struct lysp_node *)leaf;
1149 } else {
1150 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1151 iter->next = (struct lysp_node *)leaf;
1152 }
1153
1154 /* parser argument */
1155 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, YANG_LEAF));
1156
1157 /* parse content */
1158 struct yin_subelement subelems[12] = {
1159 {YANG_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1160 {YANG_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1161 {YANG_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1162 {YANG_IF_FEATURE, &leaf->iffeatures, 0},
1163 {YANG_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1164 {YANG_MUST, &leaf->musts, 0},
1165 {YANG_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1166 {YANG_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1167 {YANG_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1168 {YANG_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1169 {YANG_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1170 {YANG_CUSTOM, NULL, 0},
David Sedlák05404f62019-07-24 14:11:53 +02001171 };
David Sedlák203ca3a2019-07-18 15:26:25 +02001172 return yin_parse_content(ctx, subelems, 12, data, YANG_LEAF, NULL, &leaf->exts);
1173}
1174
1175/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001176 * @brief Parse leaf-list element.
1177 *
1178 * @param[in,out] ctx YIN parser context for logging and to store current state.
1179 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1180 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001181 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001182 *
1183 * @return LY_ERR values.
1184 */
1185static LY_ERR
1186yin_parse_leaflist(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1187 struct tree_node_meta *node_meta)
1188{
1189 struct lysp_node *iter;
1190 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001191
1192 llist = calloc(1, sizeof *llist);
1193 LY_CHECK_ERR_RET(!llist, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1194 llist->nodetype = LYS_LEAFLIST;
1195 llist->parent = node_meta->parent;
1196
1197 /* insert into siblings */
1198 if (!*(node_meta->siblings)) {
1199 *node_meta->siblings = (struct lysp_node *)llist;
1200 } else {
1201 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1202 iter->next = (struct lysp_node *)llist;
1203 }
1204
1205 /* parse argument */
1206 yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, YANG_LEAF_LIST);
1207
1208 /* parse content */
1209 struct yin_subelement subelems[14] = {
1210 {YANG_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1211 {YANG_DEFAULT, &llist->dflts, 0},
1212 {YANG_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1213 {YANG_IF_FEATURE, &llist->iffeatures, 0},
1214 {YANG_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1215 {YANG_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1216 {YANG_MUST, &llist->musts, 0},
1217 {YANG_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1218 {YANG_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1219 {YANG_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1220 {YANG_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1221 {YANG_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1222 {YANG_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1223 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001224 };
David Sedlákc3da3ef2019-07-19 12:56:08 +02001225 LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, data, YANG_LEAF_LIST, NULL, &llist->exts));
1226
1227 /* invalid combination of subelements */
1228 if ((llist->min) && (llist->dflts)) {
1229 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB, "min-elements", "default", "leaf-list");
1230 return LY_EVALID;
1231 }
1232 if (llist->max && llist->min > llist->max) {
1233 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SEMANTICS,
1234 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
1235 llist->min, llist->max);
1236 return LY_EVALID;
1237 }
1238
1239 return LY_SUCCESS;
1240}
1241
1242/**
David Sedlák04e17b22019-07-19 15:29:48 +02001243 * @brief Parse typedef element.
1244 *
1245 * @param[in,out] ctx YIN parser context for logging and to store current state.
1246 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1247 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001248 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001249 *
1250 * @return LY_ERR values.
1251 */
1252static LY_ERR
1253yin_parse_typedef(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1254 struct typedef_meta *typedef_meta)
1255{
1256 struct lysp_tpdf *tpdf;
1257 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *typedef_meta->typedefs, tpdf, LY_EMEM);
1258
1259 /* parse argument */
1260 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, YANG_TYPEDEF));
1261
1262 /* parse content */
1263 struct yin_subelement subelems[7] = {
1264 {YANG_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1265 {YANG_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1266 {YANG_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1267 {YANG_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1268 {YANG_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1269 {YANG_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1270 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001271 };
David Sedlák04e17b22019-07-19 15:29:48 +02001272 LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, data, YANG_TYPEDEF, NULL, &tpdf->exts));
1273
1274 /* store data for collision check */
1275 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
1276 ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0);
1277 }
1278
1279 return LY_SUCCESS;
1280}
1281
1282/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001283 * @brief Parse refine element.
1284 *
1285 * @param[in,out] ctx YIN parser context for logging and to store current state.
1286 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1287 * @param[in,out] data Data to read from, always moved to currently handled character.
1288 * @param[in,out] refines Refines to add to.
1289 *
1290 * @return LY_ERR values.
1291 */
1292static LY_ERR
1293yin_parse_refine(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1294 struct lysp_refine **refines)
1295{
1296 struct lysp_refine *rf;
1297
1298 /* allocate new refine */
1299 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *refines, rf, LY_EMEM);
1300
1301 /* parse attribute */
1302 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, YANG_REFINE));
1303 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(rf->nodeid), "refine");
1304
1305 /* parse content */
1306 struct yin_subelement subelems[11] = {
1307 {YANG_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
1308 {YANG_DEFAULT, &rf->dflts, 0},
1309 {YANG_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
1310 {YANG_IF_FEATURE, &rf->iffeatures, 0},
1311 {YANG_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1312 {YANG_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1313 {YANG_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1314 {YANG_MUST, &rf->musts, 0},
1315 {YANG_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1316 {YANG_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1317 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001318 };
David Sedlákd2d676a2019-07-22 11:28:19 +02001319 return yin_parse_content(ctx, subelems, 11, data, YANG_REFINE, NULL, &rf->exts);
1320}
1321
1322/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001323 * @brief Parse uses element.
1324 *
1325 * @param[in,out] ctx YIN parser context for logging and to store current state.
1326 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1327 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001328 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001329 *
1330 * @return LY_ERR values.
1331 */
1332static LY_ERR
1333yin_parse_uses(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1334 struct tree_node_meta *node_meta)
1335{
1336 struct lysp_node *iter;
1337 struct lysp_node_uses *uses;
1338
1339 /* create structure */
1340 uses = calloc(1, sizeof *uses);
1341 LY_CHECK_ERR_RET(!uses, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1342 uses->nodetype = LYS_USES;
1343 uses->parent = node_meta->parent;
1344
1345 /* insert into siblings */
1346 if (!*(node_meta->siblings)) {
1347 *node_meta->siblings = (struct lysp_node *)uses;
1348 } else {
1349 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1350 iter->next = (struct lysp_node *)uses;
1351 }
1352
1353 /* parse argument */
1354 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, YANG_USES));
1355
1356 /* parse content */
1357 struct augment_meta augments = {(struct lysp_node *)uses, &uses->augments};
1358 struct yin_subelement subelems[8] = {
1359 {YANG_AUGMENT, &augments, 0},
1360 {YANG_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1361 {YANG_IF_FEATURE, &uses->iffeatures, 0},
1362 {YANG_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1363 {YANG_REFINE, &uses->refines, 0},
1364 {YANG_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1365 {YANG_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1366 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001367 };
David Sedlák0d6de5a2019-07-22 13:25:44 +02001368 LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, data, YANG_USES, NULL, &uses->exts));
1369 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
1370
1371 return LY_SUCCESS;
1372}
1373
1374/**
David Sedlákaa854b02019-07-22 14:17:10 +02001375 * @brief Parse revision element.
1376 *
1377 * @param[in,out] ctx YIN parser context for logging and to store current state.
1378 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1379 * @param[in,out] data Data to read from, always moved to currently handled character.
1380 * @param[in,out] revs Parsed revisions to add to.
1381 *
1382 * @return LY_ERR values.
1383 */
1384static LY_ERR
1385yin_parse_revision(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1386 struct lysp_revision **revs)
1387{
1388 struct lysp_revision *rev;
1389 const char *temp_date = NULL;
1390
1391 /* allocate new reivison */
1392 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *revs, rev, LY_EMEM);
1393
1394 /* parse argument */
1395 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_date, Y_STR_ARG, YANG_REVISION));
1396 /* check value */
1397 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
1398 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1399 return LY_EVALID;
1400 }
1401 strcpy(rev->date, temp_date);
1402 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1403
1404 /* parse content */
1405 struct yin_subelement subelems[3] = {
1406 {YANG_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1407 {YANG_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1408 {YANG_CUSTOM, NULL, 0},
1409 };
1410 return yin_parse_content(ctx, subelems, 3, data, YANG_REVISION, NULL, &rev->exts);
1411}
1412
David Sedlák5e13dea2019-07-22 16:06:45 +02001413/**
1414 * @brief Parse include element.
1415 *
1416 * @param[in,out] ctx YIN parser context for logging and to store current state.
1417 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1418 * @param[in,out] data Data to read from, always moved to currently handled character.
1419 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1420 *
1421 * @return LY_ERR values.
1422 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001423static LY_ERR
1424yin_parse_include(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1425 struct include_meta *inc_meta)
1426{
1427 struct lysp_include *inc;
1428
1429 /* allocate new include */
1430 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *inc_meta->includes, inc, LY_EMEM);
1431
1432 /* parse argument */
1433 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, YANG_INCLUDE));
1434
1435 /* submodules share the namespace with the module names, so there must not be
1436 * a module of the same name in the context, no need for revision matching */
1437 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xml_ctx.ctx, inc->name)) {
1438 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YANG,
1439 "Name collision between module and submodule of name \"%s\".", inc->name);
1440 return LY_EVALID;
1441 }
1442
1443 /* parse content */
1444 struct yin_subelement subelems[4] = {
1445 {YANG_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1446 {YANG_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1447 {YANG_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1448 {YANG_CUSTOM, NULL, 0},
1449 };
1450 return yin_parse_content(ctx, subelems, 4, data, YANG_INCLUDE, NULL, &inc->exts);
1451}
1452
David Sedlákaa854b02019-07-22 14:17:10 +02001453/**
David Sedlák5e13dea2019-07-22 16:06:45 +02001454 * @brief Parse feature element.
1455 *
1456 * @param[in,out] ctx YIN parser context for logging and to store current state.
1457 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1458 * @param[in,out] data Data to read from, always moved to currently handled character.
1459 * @param[in,out] features Features to add to.
1460 *
1461 * @return LY_ERR values.
1462 */
1463static LY_ERR
1464yin_parse_feature(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1465 struct lysp_feature **features)
1466{
1467 struct lysp_feature *feat;
1468
1469 /* allocate new feature */
1470 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *features, feat, LY_EMEM);
1471
1472 /* parse argument */
1473 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, YANG_FEATURE));
1474
1475 /* parse content */
1476 struct yin_subelement subelems[5] = {
1477 {YANG_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
1478 {YANG_IF_FEATURE, &feat->iffeatures, 0},
1479 {YANG_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
1480 {YANG_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
1481 {YANG_CUSTOM, NULL, 0},
1482 };
1483 return yin_parse_content(ctx, subelems, 5, data, YANG_FEATURE, NULL, &feat->exts);
1484}
1485
1486/**
David Sedlák28794f22019-07-22 16:45:00 +02001487 * @brief Parse identity element.
1488 *
1489 * @param[in,out] ctx YIN parser context for logging and to store current state.
1490 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1491 * @param[in,out] data Data to read from, always moved to currently handled character.
1492 * @param[in,out] identities Identities to add to.
1493 *
1494 * @return LY_ERR values.
1495 */
1496static LY_ERR
1497yin_parse_identity(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1498 struct lysp_ident **identities)
1499{
1500 struct lysp_ident *ident;
1501
1502 /* allocate new identity */
1503 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *identities, ident, LY_EMEM);
1504
1505 /* parse argument */
1506 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, YANG_IDENTITY));
1507
1508 /* parse content */
1509 struct yin_subelement subelems[6] = {
1510 {YANG_BASE, &ident->bases, 0},
1511 {YANG_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
1512 {YANG_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
1513 {YANG_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
1514 {YANG_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
1515 {YANG_CUSTOM, NULL, 0},
1516 };
1517 return yin_parse_content(ctx, subelems, 6, data, YANG_IDENTITY, NULL, &ident->exts);
1518}
1519
1520/**
David Sedlákaf536aa2019-07-23 13:42:23 +02001521 * @brief Parse list element.
1522 *
1523 * @param[in,out] ctx YIN parser context for logging and to store current state.
1524 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1525 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001526 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákaf536aa2019-07-23 13:42:23 +02001527 *
1528 * @return LY_ERR values.
1529 */
1530static LY_ERR
David Sedlákf111bcb2019-07-23 17:15:51 +02001531yin_parse_list(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1532 struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02001533{
1534 struct lysp_node *iter;
1535 struct lysp_node_list *list;
1536
1537 /* create structure */
1538 list = calloc(1, sizeof *list);
1539 LY_CHECK_ERR_RET(!list, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1540 list->nodetype = LYS_LIST;
1541 list->parent = node_meta->parent;
1542
1543 /* insert into siblings */
1544 if (!*(node_meta->siblings)) {
1545 *node_meta->siblings = (struct lysp_node *)list;
1546 } else {
1547 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1548 iter->next = (struct lysp_node *)list;
1549 }
1550
1551 /* parse argument */
1552 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, YANG_LIST));
1553
1554 /* parse list content */
David Sedlák85d0eca2019-07-24 15:15:21 +02001555 struct action_meta act_meta = {(struct lysp_node *)list, &list->actions};
David Sedlákaf536aa2019-07-23 13:42:23 +02001556 struct tree_node_meta new_node_meta = {(struct lysp_node *)list, &list->child};
1557 struct typedef_meta typedef_meta = {(struct lysp_node *)list, &list->typedefs};
David Sedlák031b9e72019-07-23 15:19:37 +02001558 struct notif_meta notif_meta = {(struct lysp_node *)list, &list->notifs};
David Sedláke3ce9ef2019-07-23 16:34:30 +02001559 struct grouping_meta gr_meta = {(struct lysp_node *)list, &list->groupings};
David Sedlákaf536aa2019-07-23 13:42:23 +02001560 struct yin_subelement subelems[25] = {
David Sedlák85d0eca2019-07-24 15:15:21 +02001561 {YANG_ACTION, &act_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001562 {YANG_ANYDATA, &new_node_meta, 0},
1563 {YANG_ANYXML, &new_node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001564 {YANG_CHOICE, &new_node_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001565 {YANG_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE},
David Sedlákf111bcb2019-07-23 17:15:51 +02001566 {YANG_CONTAINER, &new_node_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001567 {YANG_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001568 {YANG_GROUPING, &gr_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001569 {YANG_IF_FEATURE, &list->iffeatures, 0},
1570 {YANG_KEY, &list->key, YIN_SUBELEM_UNIQUE},
1571 {YANG_LEAF, &new_node_meta, 0},
1572 {YANG_LEAF_LIST, &new_node_meta, 0},
1573 {YANG_LIST, &new_node_meta, 0},
1574 {YANG_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE},
1575 {YANG_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE},
1576 {YANG_MUST, &list->musts, 0},
David Sedlák031b9e72019-07-23 15:19:37 +02001577 {YANG_NOTIFICATION, &notif_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001578 {YANG_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE},
1579 {YANG_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE},
1580 {YANG_STATUS, &list->flags, YIN_SUBELEM_UNIQUE},
1581 {YANG_TYPEDEF, &typedef_meta, 0},
1582 {YANG_UNIQUE, &list->uniques, 0},
1583 {YANG_USES, &new_node_meta, 0},
1584 {YANG_WHEN, &list->when, YIN_SUBELEM_UNIQUE},
1585 {YANG_CUSTOM, NULL, 0},
1586 };
1587 LY_CHECK_RET(yin_parse_content(ctx, subelems, 25, data, YANG_LIST, NULL, &list->exts));
1588
1589 /* finalize parent pointers to the reallocated items */
1590 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
1591
1592 if (list->max && list->min > list->max) {
1593 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SEMANTICS,
1594 "Invalid combination of min-elements and max-elements: min value %u is bigger than the max value %u.",
1595 list->min, list->max);
1596 return LY_EVALID;
1597 }
1598
1599 return LY_SUCCESS;
1600}
1601
1602/**
David Sedlák031b9e72019-07-23 15:19:37 +02001603 * @brief Parse notification element.
1604 *
1605 * @param[in,out] ctx YIN parser context for logging and to store current state.
1606 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1607 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001608 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02001609 *
1610 * @return LY_ERR values.
1611 */
1612static LY_ERR
1613yin_parse_notification(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1614 struct notif_meta *notif_meta)
1615{
1616 struct lysp_notif *notif;
1617
1618 /* allocate new notification */
1619 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *notif_meta->notifs, notif, LY_EMEM);
1620 notif->nodetype = LYS_NOTIF;
1621 notif->parent = notif_meta->parent;
1622
1623 /* parse argument */
1624 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, YANG_NOTIFICATION));
1625
1626 /* parse notification content */
1627 struct tree_node_meta node_meta = {(struct lysp_node *)notif, &notif->data};
1628 struct typedef_meta typedef_meta = {(struct lysp_node *)notif, &notif->typedefs};
David Sedláke3ce9ef2019-07-23 16:34:30 +02001629 struct grouping_meta gr_meta = {(struct lysp_node *)notif, &notif->groupings};
David Sedlák031b9e72019-07-23 15:19:37 +02001630 struct yin_subelement subelems[16] = {
1631 {YANG_ANYDATA, &node_meta, 0},
1632 {YANG_ANYXML, &node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001633 {YANG_CHOICE, &node_meta, 0},
David Sedlákf111bcb2019-07-23 17:15:51 +02001634 {YANG_CONTAINER, &node_meta, 0},
David Sedlák031b9e72019-07-23 15:19:37 +02001635 {YANG_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001636 {YANG_GROUPING, &gr_meta, 0},
David Sedlák031b9e72019-07-23 15:19:37 +02001637 {YANG_IF_FEATURE, &notif->iffeatures, 0},
1638 {YANG_LEAF, &node_meta, 0},
1639 {YANG_LEAF_LIST, &node_meta, 0},
1640 {YANG_LIST, &node_meta, 0},
1641 {YANG_MUST, &notif->musts, YIN_SUBELEM_VER2},
1642 {YANG_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE},
1643 {YANG_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE},
1644 {YANG_TYPEDEF, &typedef_meta, 0},
1645 {YANG_USES, &node_meta, 0},
1646 {YANG_CUSTOM, NULL, 0},
1647 };
1648 LY_CHECK_RET(yin_parse_content(ctx, subelems, 16, data, YANG_NOTIFICATION, NULL, &notif->exts));
1649
1650 /* finalize parent pointers to the reallocated items */
1651 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
1652
1653 return LY_SUCCESS;
1654}
1655
1656/**
David Sedláke3ce9ef2019-07-23 16:34:30 +02001657 * @brief Parse notification element.
1658 *
1659 * @param[in,out] ctx YIN parser context for logging and to store current state.
1660 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1661 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001662 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02001663 *
1664 * @return LY_ERR values.
1665 */
1666static LY_ERR
1667yin_parse_grouping(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1668 struct grouping_meta *gr_meta)
1669{
1670 struct lysp_grp *grp;
1671
1672 /* create new grouping */
1673 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *gr_meta->groupings, grp, LY_EMEM);
1674 grp->nodetype = LYS_GROUPING;
1675 grp->parent = gr_meta->parent;
1676
1677 /* parse argument */
1678 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, YANG_GROUPING));
1679
1680 /* parse grouping content */
David Sedlák85d0eca2019-07-24 15:15:21 +02001681 struct action_meta act_meta = {(struct lysp_node *)grp, &grp->actions};
David Sedláke3ce9ef2019-07-23 16:34:30 +02001682 struct tree_node_meta node_meta = {(struct lysp_node *)grp, &grp->data};
1683 struct typedef_meta typedef_meta = {(struct lysp_node *)grp, &grp->typedefs};
1684 struct grouping_meta sub_grouping = {(struct lysp_node *)grp, &grp->groupings};
1685 struct notif_meta notif_meta = {(struct lysp_node *)grp, &grp->notifs};
1686 struct yin_subelement subelems[16] = {
David Sedlák85d0eca2019-07-24 15:15:21 +02001687 {YANG_ACTION, &act_meta, 0},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001688 {YANG_ANYDATA, &node_meta, 0},
1689 {YANG_ANYXML, &node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001690 {YANG_CHOICE, &node_meta, 0},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001691 {YANG_CONTAINER, &node_meta, 0},
1692 {YANG_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE},
1693 {YANG_GROUPING, &sub_grouping, 0},
1694 {YANG_LEAF, &node_meta, 0},
1695 {YANG_LEAF_LIST, &node_meta, 0},
1696 {YANG_LIST, &node_meta, 0},
1697 {YANG_NOTIFICATION, &notif_meta, 0},
1698 {YANG_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE},
1699 {YANG_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE},
1700 {YANG_TYPEDEF, &typedef_meta, 0},
1701 {YANG_USES, &node_meta, 0},
1702 {YANG_CUSTOM, NULL, 0},
1703 };
1704 LY_CHECK_RET(yin_parse_content(ctx, subelems, 16, data, YANG_GROUPING, NULL, &grp->exts));
1705 /* finalize parent pointers to the reallocated items */
1706 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
1707
1708 return LY_SUCCESS;
1709}
1710
1711/**
David Sedlákf111bcb2019-07-23 17:15:51 +02001712 * @brief Parse list element.
1713 *
1714 * @param[in,out] ctx YIN parser context for logging and to store current state.
1715 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1716 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001717 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákf111bcb2019-07-23 17:15:51 +02001718 *
1719 * @return LY_ERR values.
1720 */
1721static LY_ERR
1722yin_parse_container(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1723 struct tree_node_meta *node_meta)
1724{
1725 struct lysp_node *iter;
1726 struct lysp_node_container *cont;
1727
1728 /* create new container */
1729 cont = calloc(1, sizeof *cont);
1730 LY_CHECK_ERR_RET(!cont, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1731 cont->nodetype = LYS_CONTAINER;
1732 cont->parent = node_meta->parent;
1733
1734 /* insert into siblings */
1735 if (!*(node_meta->siblings)) {
1736 *node_meta->siblings = (struct lysp_node *)cont;
1737 } else {
1738 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1739 iter->next = (struct lysp_node *)cont;
1740 }
1741
1742 /* parse aegument */
1743 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, YANG_CONTAINER));
1744
1745 /* parse container content */
David Sedlák85d0eca2019-07-24 15:15:21 +02001746 struct action_meta act_meta = {(struct lysp_node *)cont, &cont->actions};
David Sedlákf111bcb2019-07-23 17:15:51 +02001747 struct tree_node_meta new_node_meta = {(struct lysp_node *)cont, &cont->child};
1748 struct grouping_meta grp_meta = {(struct lysp_node *)cont, &cont->groupings};
1749 struct typedef_meta typedef_meta = {(struct lysp_node *)cont, &cont->typedefs};
1750 struct notif_meta notif_meta = {(struct lysp_node *)cont, &cont->notifs};
1751 struct yin_subelement subelems[21] = {
1752 /* TODO action */
David Sedlák85d0eca2019-07-24 15:15:21 +02001753 {YANG_ACTION, &act_meta, YIN_SUBELEM_VER2},
David Sedlákf111bcb2019-07-23 17:15:51 +02001754 {YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
1755 {YANG_ANYXML, &new_node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001756 {YANG_CHOICE, &new_node_meta, 0},
David Sedlákf111bcb2019-07-23 17:15:51 +02001757 {YANG_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE},
1758 {YANG_CONTAINER, &new_node_meta, 0},
1759 {YANG_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE},
1760 {YANG_GROUPING, &grp_meta, 0},
1761 {YANG_IF_FEATURE, &cont->iffeatures, 0},
1762 {YANG_LEAF, &new_node_meta, 0},
1763 {YANG_LEAF_LIST, &new_node_meta, 0},
1764 {YANG_LIST, &new_node_meta, 0},
1765 {YANG_MUST, &cont->musts, 0},
1766 {YANG_NOTIFICATION, &notif_meta, YIN_SUBELEM_VER2},
1767 {YANG_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE},
1768 {YANG_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE},
1769 {YANG_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE},
1770 {YANG_TYPEDEF, &typedef_meta, 0},
1771 {YANG_USES, &new_node_meta, 0},
1772 {YANG_WHEN, &cont->when, YIN_SUBELEM_UNIQUE},
1773 {YANG_CUSTOM, NULL, 0},
1774 };
1775 LY_CHECK_RET(yin_parse_content(ctx, subelems, 21, data, YANG_CONTAINER, NULL, &cont->exts));
1776 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
1777
1778 return LY_SUCCESS;
1779}
1780
1781/**
David Sedlák5379d392019-07-24 10:42:03 +02001782 * @brief Parse case element.
1783 *
1784 * @param[in,out] ctx YIN parser context for logging and to store current state.
1785 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1786 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001787 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák5379d392019-07-24 10:42:03 +02001788 *
1789 * @return LY_ERR values.
1790 */
1791static LY_ERR
1792yin_parse_case(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1793 struct tree_node_meta *node_meta)
1794{
1795 struct lysp_node *iter;
1796 struct lysp_node_case *cas;
1797
1798 /* create new case */
1799 cas = calloc(1, sizeof *cas);
1800 LY_CHECK_ERR_RET(!cas, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1801 cas->nodetype = LYS_CASE;
1802 cas->parent = node_meta->parent;
1803
1804 /* insert into siblings */
1805 if (!*(node_meta->siblings)) {
1806 *node_meta->siblings = (struct lysp_node *)cas;
1807 } else {
1808 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1809 iter->next = (struct lysp_node *)cas;
1810 }
1811
1812 /* parse argument */
1813 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, YANG_CASE));
1814
1815 /* parse case content */
1816 struct tree_node_meta new_node_meta = {(struct lysp_node *)cas, &cas->child};
1817 struct yin_subelement subelems[14] = {
1818 {YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
1819 {YANG_ANYXML, &new_node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001820 {YANG_CHOICE, &new_node_meta, 0},
David Sedlák5379d392019-07-24 10:42:03 +02001821 {YANG_CONTAINER, &new_node_meta, 0},
1822 {YANG_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE},
1823 {YANG_IF_FEATURE, &cas->iffeatures, 0},
1824 {YANG_LEAF, &new_node_meta, 0},
1825 {YANG_LEAF_LIST, &new_node_meta, 0},
1826 {YANG_LIST, &new_node_meta, 0},
1827 {YANG_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE},
1828 {YANG_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE},
1829 {YANG_USES, &new_node_meta, 0},
1830 {YANG_WHEN, &cas->when, YIN_SUBELEM_UNIQUE},
1831 {YANG_CUSTOM, NULL, 0},
1832 };
1833 return yin_parse_content(ctx, subelems, 14, data, YANG_CASE, NULL, &cas->exts);
1834}
1835
1836/**
David Sedlák05404f62019-07-24 14:11:53 +02001837 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02001838 *
1839 * @param[in,out] ctx YIN parser context for logging and to store current state.
1840 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1841 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001842 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákb7abcfa2019-07-24 12:33:35 +02001843 *
1844 * @return LY_ERR values.
1845 */
1846LY_ERR
1847yin_parse_choice(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1848 struct tree_node_meta *node_meta)
1849{
1850 struct lysp_node *iter;
1851 struct lysp_node_choice *choice;
1852
1853 /* create new choice */
1854 choice = calloc(1, sizeof *choice);
1855 LY_CHECK_ERR_RET(!choice, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1856 choice->nodetype = LYS_CHOICE;
1857 choice->parent = node_meta->parent;
1858
1859 /* insert into siblings */
1860 if (!*(node_meta->siblings)) {
1861 *node_meta->siblings = (struct lysp_node *)choice;
1862 } else {
1863 for (iter = *node_meta->siblings; iter->next; iter = iter->next);
1864 iter->next = (struct lysp_node *)choice;
1865 }
1866
1867 /* parse argument */
1868 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, YANG_CHOICE));
1869
1870 /* parse choice content */
1871 struct tree_node_meta new_node_meta = {(struct lysp_node *)choice, &choice->child};
1872 struct yin_subelement subelems[17] = {
1873 {YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
1874 {YANG_ANYXML, &new_node_meta, 0},
1875 {YANG_CASE, &new_node_meta, 0},
1876 {YANG_CHOICE, &new_node_meta, YIN_SUBELEM_VER2},
1877 {YANG_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE},
1878 {YANG_CONTAINER, &new_node_meta, 0},
1879 {YANG_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE},
1880 {YANG_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE},
1881 {YANG_IF_FEATURE, &choice->iffeatures, 0},
1882 {YANG_LEAF, &new_node_meta, 0},
1883 {YANG_LEAF_LIST, &new_node_meta, 0},
1884 {YANG_LIST, &new_node_meta, 0},
1885 {YANG_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE},
1886 {YANG_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE},
1887 {YANG_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE},
1888 {YANG_WHEN, &choice->when, YIN_SUBELEM_UNIQUE},
1889 {YANG_CUSTOM, NULL, 0},
1890 };
1891 return yin_parse_content(ctx, subelems, 17, data, YANG_CHOICE, NULL, &choice->exts);
1892}
1893
1894/**
David Sedlák05404f62019-07-24 14:11:53 +02001895 * @brief Parse input or output element.
1896 *
1897 * @param[in,out] ctx YIN parser context for logging and to store current state.
1898 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1899 * @param[in,out] data Data to read from, always moved to currently handled character.
1900 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
1901 *
1902 * @return LY_ERR values.
1903 */
1904static LY_ERR
1905yin_parse_inout(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword inout_kw,
1906 struct inout_meta *inout_meta)
1907{
1908 /* initiate structure */
1909 inout_meta->inout_p->nodetype = (inout_kw == YANG_INPUT) ? LYS_INPUT : LYS_OUTPUT;
1910 inout_meta->inout_p->parent = inout_meta->parent;
1911
1912 /* check attributes */
1913 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
1914
1915 /* parser input/output content */
1916 struct tree_node_meta node_meta = {(struct lysp_node *)inout_meta->inout_p, &inout_meta->inout_p->data};
1917 struct grouping_meta grp_meta = {(struct lysp_node *)inout_meta->inout_p, &inout_meta->inout_p->groupings};
1918 struct typedef_meta typedef_meta = {(struct lysp_node *)inout_meta->inout_p, &inout_meta->inout_p->typedefs};
1919 struct yin_subelement subelems[12] = {
1920 {YANG_ANYDATA, &node_meta, YIN_SUBELEM_VER2},
1921 {YANG_ANYXML, &node_meta, 0},
1922 {YANG_CHOICE, &node_meta, 0},
1923 {YANG_CONTAINER, &node_meta, 0},
1924 {YANG_GROUPING, &grp_meta, 0},
1925 {YANG_LEAF, &node_meta, 0},
1926 {YANG_LEAF_LIST, &node_meta, 0},
1927 {YANG_LIST, &node_meta, 0},
1928 {YANG_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2},
1929 {YANG_TYPEDEF, &typedef_meta, 0},
1930 {YANG_USES, &node_meta, 0},
1931 {YANG_CUSTOM, NULL, 0},
1932 };
1933 LY_CHECK_RET(yin_parse_content(ctx, subelems, 12, data, inout_kw, NULL, &inout_meta->inout_p->exts));
1934
1935 /* finalize parent pointers to the reallocated items */
1936 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings,
1937 NULL, NULL, NULL));
1938
1939 return LY_SUCCESS;
1940}
1941
David Sedlák85d0eca2019-07-24 15:15:21 +02001942static LY_ERR
1943yin_parse_action(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1944 struct action_meta *act_meta)
1945{
1946 struct lysp_action *act;
1947
1948 /* create new action */
1949 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *act_meta->actions, act, LY_EMEM);
1950 act->nodetype = LYS_ACTION;
1951 act->parent = act_meta->parent;
1952
1953 /* parse argument */
1954 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, YANG_ACTION));
1955
1956 /* parse content */
1957 struct grouping_meta grp_meta = {(struct lysp_node *)act, &act->groupings};
1958 struct typedef_meta typedef_meta = {(struct lysp_node *)act, &act->typedefs};
1959 struct inout_meta input = {(struct lysp_node *)act, &act->input};
1960 struct inout_meta output = {(struct lysp_node *)act, &act->output};
1961 struct yin_subelement subelems[9] = {
1962 {YANG_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE},
1963 {YANG_GROUPING, &grp_meta, 0},
1964 {YANG_IF_FEATURE, &act->iffeatures, 0},
1965 {YANG_INPUT, &input, YIN_SUBELEM_UNIQUE},
1966 {YANG_OUTPUT, &output, YIN_SUBELEM_UNIQUE},
1967 {YANG_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE},
1968 {YANG_STATUS, &act->flags, YIN_SUBELEM_UNIQUE},
1969 {YANG_TYPEDEF, &typedef_meta, 0},
1970 {YANG_CUSTOM, NULL, 0},
1971 };
1972 LY_CHECK_RET(yin_parse_content(ctx, subelems, 9, data, YANG_ACTION, NULL, &act->exts));
1973 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
1974
1975 return LY_SUCCESS;
1976}
1977
David Sedlák05404f62019-07-24 14:11:53 +02001978/**
David Sedlákb4e44562019-07-04 15:42:12 +02001979 * @brief Map keyword type to substatement info.
1980 *
1981 * @param[in] kw Keyword type.
1982 *
1983 * @return correct LYEXT_SUBSTMT information.
1984 */
1985static LYEXT_SUBSTMT
1986kw2lyext_substmt(enum yang_keyword kw)
1987{
1988 switch (kw) {
1989 case YANG_ARGUMENT:
1990 return LYEXT_SUBSTMT_ARGUMENT;
1991 case YANG_BASE:
1992 return LYEXT_SUBSTMT_BASE;
1993 case YANG_BELONGS_TO:
1994 return LYEXT_SUBSTMT_BELONGSTO;
1995 case YANG_CONTACT:
1996 return LYEXT_SUBSTMT_CONTACT;
1997 case YANG_DEFAULT:
1998 return LYEXT_SUBSTMT_DEFAULT;
1999 case YANG_DESCRIPTION:
2000 return LYEXT_SUBSTMT_DESCRIPTION;
2001 case YANG_ERROR_APP_TAG:
2002 return LYEXT_SUBSTMT_ERRTAG;
2003 case YANG_ERROR_MESSAGE:
2004 return LYEXT_SUBSTMT_ERRMSG;
2005 case YANG_KEY:
2006 return LYEXT_SUBSTMT_KEY;
2007 case YANG_NAMESPACE:
2008 return LYEXT_SUBSTMT_NAMESPACE;
2009 case YANG_ORGANIZATION:
2010 return LYEXT_SUBSTMT_ORGANIZATION;
2011 case YANG_PATH:
2012 return LYEXT_SUBSTMT_PATH;
2013 case YANG_PREFIX:
2014 return LYEXT_SUBSTMT_PREFIX;
2015 case YANG_PRESENCE:
2016 return LYEXT_SUBSTMT_PRESENCE;
2017 case YANG_REFERENCE:
2018 return LYEXT_SUBSTMT_REFERENCE;
2019 case YANG_REVISION_DATE:
2020 return LYEXT_SUBSTMT_REVISIONDATE;
2021 case YANG_UNITS:
2022 return LYEXT_SUBSTMT_UNITS;
2023 case YANG_VALUE:
2024 return LYEXT_SUBSTMT_VALUE;
2025 case YANG_YANG_VERSION:
2026 return LYEXT_SUBSTMT_VERSION;
2027 case YANG_MODIFIER:
2028 return LYEXT_SUBSTMT_MODIFIER;
2029 case YANG_REQUIRE_INSTANCE:
2030 return LYEXT_SUBSTMT_REQINSTANCE;
2031 case YANG_YIN_ELEMENT:
2032 return LYEXT_SUBSTMT_YINELEM;
2033 case YANG_CONFIG:
2034 return LYEXT_SUBSTMT_CONFIG;
2035 case YANG_MANDATORY:
2036 return LYEXT_SUBSTMT_MANDATORY;
2037 case YANG_ORDERED_BY:
2038 return LYEXT_SUBSTMT_ORDEREDBY;
2039 case YANG_STATUS:
2040 return LYEXT_SUBSTMT_STATUS;
2041 case YANG_FRACTION_DIGITS:
2042 return LYEXT_SUBSTMT_FRACDIGITS;
2043 case YANG_MAX_ELEMENTS:
2044 return LYEXT_SUBSTMT_MAX;
2045 case YANG_MIN_ELEMENTS:
2046 return LYEXT_SUBSTMT_MIN;
2047 case YANG_POSITION:
2048 return LYEXT_SUBSTMT_POSITION;
2049 case YANG_UNIQUE:
2050 return LYEXT_SUBSTMT_UNIQUE;
2051 case YANG_IF_FEATURE:
2052 return LYEXT_SUBSTMT_IFFEATURE;
2053 default:
2054 return LYEXT_SUBSTMT_SELF;
2055 }
2056}
2057
David Sedlákd6e56892019-07-01 15:40:24 +02002058LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02002059yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, signed char subelem_info_size,
David Sedlák3ffbc522019-07-02 17:49:28 +02002060 const char **data, enum yang_keyword current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +02002061{
2062 LY_ERR ret = LY_SUCCESS;
2063 struct sized_string prefix, name;
David Sedlák8e7bda82019-07-16 17:57:50 +02002064 char *out = NULL;
2065 size_t out_len = 0;
2066 int dynamic = 0;
David Sedlák1af868e2019-07-17 17:03:14 +02002067 struct yin_arg_record *attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02002068 enum yang_keyword kw = YANG_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02002069 struct yin_subelement *subelem = NULL;
David Sedlák374d2b32019-07-17 15:06:55 +02002070 struct lysp_type *type, *nested_type;
David Sedlák09e18c92019-07-18 11:17:11 +02002071
David Sedlákb0faad82019-07-04 14:28:59 +02002072 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +02002073
David Sedlákda8ffa32019-07-08 14:17:10 +02002074 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
2075 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +02002076 /* current element has subelements as content */
2077 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002078 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
2079 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +02002080 LY_CHECK_GOTO(ret, cleanup);
2081 if (!name.value) {
2082 /* end of current element reached */
2083 break;
2084 }
David Sedlák1af868e2019-07-17 17:03:14 +02002085 ret = yin_load_attributes(ctx, data, &attrs);
David Sedlákd6e56892019-07-01 15:40:24 +02002086 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02002087 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +02002088
2089 /* check if this element can be child of current element */
David Sedlák1af868e2019-07-17 17:03:14 +02002090 subelem = get_record(kw, subelem_info_size, subelem_info);
2091 if (!subelem) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002092 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name.len, name.value, ly_stmt2str(current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02002093 ret = LY_EVALID;
2094 goto cleanup;
2095 }
2096
David Sedlák5f8191e2019-07-08 16:35:52 +02002097 /* TODO check relative order */
2098
David Sedlák0c2bab92019-07-22 15:33:19 +02002099 /* check flags */
David Sedlák21f87cd2019-07-03 16:53:23 +02002100 /* if element is unique and already defined log error */
David Sedlák1af868e2019-07-17 17:03:14 +02002101 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002102 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Redefinition of %s element in %s element.", ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +02002103 return LY_EVALID;
2104 }
David Sedlák1af868e2019-07-17 17:03:14 +02002105 if (subelem->flags & YIN_SUBELEM_FIRST) {
2106 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
David Sedlák66d7c842019-07-11 15:06:04 +02002107 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +02002108 }
David Sedlák0c2bab92019-07-22 15:33:19 +02002109 if (subelem->flags & YIN_SUBELEM_VER2) {
2110 if (ctx->mod_version < 2) {
2111 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák9bb1c042019-07-22 16:45:37 +02002112 ret = LY_EVALID;
2113 goto cleanup;
David Sedlák0c2bab92019-07-22 15:33:19 +02002114 }
2115 }
David Sedlák1af868e2019-07-17 17:03:14 +02002116 subelem->flags |= YIN_SUBELEM_PARSED;
David Sedlák21f87cd2019-07-03 16:53:23 +02002117
David Sedlákd6e56892019-07-01 15:40:24 +02002118 switch (kw) {
2119 case YANG_CUSTOM:
David Sedlák1af868e2019-07-17 17:03:14 +02002120 ret = yin_parse_extension_instance(ctx, attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +02002121 namelen2fulllen(name.len, prefix.len),
David Sedlák1af868e2019-07-17 17:03:14 +02002122 kw2lyext_substmt(current_element),
2123 (subelem->dest) ? *((uint32_t*)subelem->dest) : 0, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002124 break;
2125 case YANG_ACTION:
David Sedlák85d0eca2019-07-24 15:15:21 +02002126 ret = yin_parse_action(ctx, attrs, data, (struct action_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002127 break;
2128 case YANG_ANYDATA:
David Sedlákd6e56892019-07-01 15:40:24 +02002129 case YANG_ANYXML:
David Sedlák8a83bbb2019-07-18 14:46:00 +02002130 ret = yin_parse_any(ctx, attrs, data, kw, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002131 break;
2132 case YANG_ARGUMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02002133 ret = yin_parse_argument_element(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002134 break;
2135 case YANG_AUGMENT:
2136 break;
2137 case YANG_BASE:
David Sedláka62750b2019-07-16 11:21:31 +02002138 if (current_element == YANG_TYPE) {
David Sedlák1af868e2019-07-17 17:03:14 +02002139 type = (struct lysp_type *)subelem->dest;
2140 ret = yin_parse_simple_elements(ctx, attrs, data, kw, &type->bases, YIN_ARG_NAME,
David Sedlák292763b2019-07-09 11:10:53 +02002141 Y_PREF_IDENTIF_ARG, exts);
2142 type->flags |= LYS_SET_BASE;
David Sedláka62750b2019-07-16 11:21:31 +02002143 } else if (current_element == YANG_IDENTITY) {
David Sedlák1af868e2019-07-17 17:03:14 +02002144 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
David Sedlák292763b2019-07-09 11:10:53 +02002145 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
2146 } else {
2147 LOGINT(ctx->xml_ctx.ctx);
2148 ret = LY_EINT;
2149 }
David Sedlákd6e56892019-07-01 15:40:24 +02002150 break;
2151 case YANG_BELONGS_TO:
David Sedlák1af868e2019-07-17 17:03:14 +02002152 ret = yin_parse_belongs_to(ctx, attrs, data, (struct lysp_submodule *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002153 break;
2154 case YANG_BIT:
David Sedlák07869a52019-07-12 14:28:19 +02002155 case YANG_ENUM:
David Sedlák1af868e2019-07-17 17:03:14 +02002156 ret = yin_parse_enum_bit(ctx, attrs, data, kw, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002157 break;
2158 case YANG_CASE:
David Sedlák5379d392019-07-24 10:42:03 +02002159 ret = yin_parse_case(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002160 break;
2161 case YANG_CHOICE:
David Sedlákb7abcfa2019-07-24 12:33:35 +02002162 ret = yin_parse_choice(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002163 break;
2164 case YANG_CONFIG:
David Sedlák1af868e2019-07-17 17:03:14 +02002165 ret = yin_parse_config(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002166 break;
2167 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +02002168 case YANG_DESCRIPTION:
2169 case YANG_ORGANIZATION:
2170 case YANG_REFERENCE:
David Sedlák1af868e2019-07-17 17:03:14 +02002171 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002172 break;
2173 case YANG_CONTAINER:
David Sedlákf111bcb2019-07-23 17:15:51 +02002174 ret = yin_parse_container(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002175 break;
2176 case YANG_DEFAULT:
David Sedlákc3da3ef2019-07-19 12:56:08 +02002177 if (subelem->flags & YIN_SUBELEM_UNIQUE) {
2178 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
2179 YIN_ARG_VALUE, Y_STR_ARG, exts);
2180 } else {
2181 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
2182 YIN_ARG_VALUE, Y_STR_ARG, exts);
2183 }
David Sedlákd6e56892019-07-01 15:40:24 +02002184 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002185 case YANG_DEVIATE:
2186 break;
2187 case YANG_DEVIATION:
2188 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002189 case YANG_ERROR_APP_TAG:
David Sedlák1af868e2019-07-17 17:03:14 +02002190 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedlák2ce1be62019-07-10 16:15:09 +02002191 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002192 break;
2193 case YANG_ERROR_MESSAGE:
David Sedlák1af868e2019-07-17 17:03:14 +02002194 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002195 break;
2196 case YANG_EXTENSION:
David Sedlák1af868e2019-07-17 17:03:14 +02002197 ret = yin_parse_extension(ctx, attrs, data, (struct lysp_ext **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002198 break;
2199 case YANG_FEATURE:
David Sedlák5e13dea2019-07-22 16:06:45 +02002200 ret = yin_parse_feature(ctx, attrs, data, (struct lysp_feature **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002201 break;
2202 case YANG_FRACTION_DIGITS:
David Sedlák1af868e2019-07-17 17:03:14 +02002203 ret = yin_parse_fracdigits(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002204 break;
2205 case YANG_GROUPING:
David Sedláke3ce9ef2019-07-23 16:34:30 +02002206 ret = yin_parse_grouping(ctx, attrs, data, (struct grouping_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002207 break;
2208 case YANG_IDENTITY:
David Sedlák28794f22019-07-22 16:45:00 +02002209 ret = yin_parse_identity(ctx, attrs, data, (struct lysp_ident **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002210 break;
2211 case YANG_IF_FEATURE:
David Sedlák1af868e2019-07-17 17:03:14 +02002212 ret = yin_parse_simple_elements(ctx, attrs, data, kw,
2213 (const char ***)subelem->dest, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002214 break;
2215 case YANG_IMPORT:
David Sedlák1af868e2019-07-17 17:03:14 +02002216 ret = yin_parse_import(ctx, attrs, data, (struct lysp_module *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002217 break;
2218 case YANG_INCLUDE:
David Sedlák0c2bab92019-07-22 15:33:19 +02002219 ret = yin_parse_include(ctx, attrs, data, (struct include_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002220 break;
2221 case YANG_INPUT:
David Sedlák05404f62019-07-24 14:11:53 +02002222 case YANG_OUTPUT:
2223 ret = yin_parse_inout(ctx, attrs, data, kw, (struct inout_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002224 break;
2225 case YANG_KEY:
David Sedlák12470a82019-07-19 13:44:36 +02002226 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest, YIN_ARG_VALUE,
2227 Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002228 break;
2229 case YANG_LEAF:
David Sedlák203ca3a2019-07-18 15:26:25 +02002230 ret = yin_parse_leaf(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002231 break;
2232 case YANG_LEAF_LIST:
David Sedlákc3da3ef2019-07-19 12:56:08 +02002233 ret = yin_parse_leaflist(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002234 break;
2235 case YANG_LENGTH:
David Sedlák1af868e2019-07-17 17:03:14 +02002236 type = (struct lysp_type *)subelem->dest;
David Sedlák438ae432019-07-11 15:36:54 +02002237 type->length = calloc(1, sizeof *type->length);
2238 LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02002239 ret = yin_parse_restriction(ctx, attrs, data, kw, type->length);
David Sedlák438ae432019-07-11 15:36:54 +02002240 type->flags |= LYS_SET_LENGTH;
David Sedlákd6e56892019-07-01 15:40:24 +02002241 break;
2242 case YANG_LIST:
David Sedlákaf536aa2019-07-23 13:42:23 +02002243 ret = yin_parse_list(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002244 break;
2245 case YANG_MANDATORY:
David Sedlák1af868e2019-07-17 17:03:14 +02002246 ret = yin_parse_mandatory(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002247 break;
2248 case YANG_MAX_ELEMENTS:
David Sedlákd6e56892019-07-01 15:40:24 +02002249 case YANG_MIN_ELEMENTS:
David Sedlák09e18c92019-07-18 11:17:11 +02002250 ret = yin_parse_minmax(ctx, attrs, data, current_element, kw, subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002251 break;
2252 case YANG_MODIFIER:
David Sedlák1af868e2019-07-17 17:03:14 +02002253 ret = yin_parse_modifier(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002254 break;
2255 case YANG_MODULE:
2256 break;
2257 case YANG_MUST:
David Sedlák1af868e2019-07-17 17:03:14 +02002258 ret = yin_parse_must(ctx, attrs, data, (struct lysp_restr **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002259 break;
2260 case YANG_NAMESPACE:
David Sedlák1af868e2019-07-17 17:03:14 +02002261 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedlák2ce1be62019-07-10 16:15:09 +02002262 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002263 break;
2264 case YANG_NOTIFICATION:
David Sedlák031b9e72019-07-23 15:19:37 +02002265 ret = yin_parse_notification(ctx, attrs, data, (struct notif_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002266 break;
2267 case YANG_ORDERED_BY:
David Sedláka2dad212019-07-18 12:45:19 +02002268 ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002269 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002270 case YANG_PATH:
David Sedlák1af868e2019-07-17 17:03:14 +02002271 type = (struct lysp_type *)subelem->dest;
2272 ret = yin_parse_simple_element(ctx, attrs, data, kw, &type->path,
David Sedlák58979872019-07-12 11:42:43 +02002273 YIN_ARG_VALUE, Y_STR_ARG, exts);
2274 type->flags |= LYS_SET_PATH;
David Sedlákd6e56892019-07-01 15:40:24 +02002275 break;
2276 case YANG_PATTERN:
David Sedlák1af868e2019-07-17 17:03:14 +02002277 ret = yin_parse_pattern(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002278 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02002279 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02002280 case YANG_POSITION:
David Sedlák1af868e2019-07-17 17:03:14 +02002281 ret = yin_parse_value_pos_element(ctx, attrs, data, kw,
2282 (struct lysp_type_enum *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002283 break;
2284 case YANG_PREFIX:
David Sedlák1af868e2019-07-17 17:03:14 +02002285 ret = yin_parse_simple_element(ctx, attrs, data, kw,
2286 (const char **)subelem->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002287 break;
2288 case YANG_PRESENCE:
David Sedlákcb39f642019-07-19 13:19:55 +02002289 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest, YIN_ARG_VALUE,
2290 Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002291 break;
2292 case YANG_RANGE:
David Sedlák1af868e2019-07-17 17:03:14 +02002293 type = (struct lysp_type *)subelem->dest;
David Sedlákb7296dd2019-07-11 14:58:38 +02002294 type->range = calloc(1, sizeof *type->range);
David Sedlák66d7c842019-07-11 15:06:04 +02002295 LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02002296 ret = yin_parse_restriction(ctx, attrs, data, kw, type->range);
David Sedlák438ae432019-07-11 15:36:54 +02002297 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +02002298 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002299 case YANG_REFINE:
David Sedlákd2d676a2019-07-22 11:28:19 +02002300 ret = yin_parse_refine(ctx, attrs, data, (struct lysp_refine **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002301 break;
2302 case YANG_REQUIRE_INSTANCE:
David Sedlák1af868e2019-07-17 17:03:14 +02002303 ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002304 break;
2305 case YANG_REVISION:
David Sedlákaa854b02019-07-22 14:17:10 +02002306 ret = yin_parse_revision(ctx, attrs, data, (struct lysp_revision **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002307 break;
2308 case YANG_REVISION_DATE:
David Sedlák1af868e2019-07-17 17:03:14 +02002309 ret = yin_parse_revision_date(ctx, attrs, data, (char *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002310 break;
2311 case YANG_RPC:
2312 break;
2313 case YANG_STATUS:
David Sedlák1af868e2019-07-17 17:03:14 +02002314 ret = yin_parse_status(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002315 break;
2316 case YANG_SUBMODULE:
2317 break;
2318 case YANG_TYPE:
David Sedlák374d2b32019-07-17 15:06:55 +02002319 /* type as child of another type */
David Sedlák1af868e2019-07-17 17:03:14 +02002320 type = (struct lysp_type *)subelem->dest;
David Sedlák374d2b32019-07-17 15:06:55 +02002321 if (current_element == YANG_TYPE) {
2322 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, type->types, nested_type, ret, cleanup);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002323 type->flags |= LYS_SET_TYPE;
David Sedlák374d2b32019-07-17 15:06:55 +02002324 type = nested_type;
2325 }
David Sedlák1af868e2019-07-17 17:03:14 +02002326 ret = yin_parse_type(ctx, attrs, data, type);
David Sedlákd6e56892019-07-01 15:40:24 +02002327 break;
2328 case YANG_TYPEDEF:
David Sedlák04e17b22019-07-19 15:29:48 +02002329 ret = yin_parse_typedef(ctx, attrs, data, (struct typedef_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002330 break;
2331 case YANG_UNIQUE:
David Sedlák1af868e2019-07-17 17:03:14 +02002332 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
David Sedláka5b1d382019-07-10 16:31:09 +02002333 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002334 break;
2335 case YANG_UNITS:
David Sedlák1af868e2019-07-17 17:03:14 +02002336 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedláka5b1d382019-07-10 16:31:09 +02002337 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002338 break;
2339 case YANG_USES:
David Sedlák0d6de5a2019-07-22 13:25:44 +02002340 ret = yin_parse_uses(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002341 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002342 case YANG_WHEN:
David Sedlák1af868e2019-07-17 17:03:14 +02002343 ret = yin_parse_when(ctx, attrs, data, (struct lysp_when **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002344 break;
2345 case YANG_YANG_VERSION:
David Sedlák1af868e2019-07-17 17:03:14 +02002346 ret = yin_parse_yangversion(ctx, attrs, data, (uint8_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002347 break;
2348 case YANG_YIN_ELEMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02002349 ret = yin_parse_yin_element_element(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02002350 break;
2351 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02002352 case YIN_VALUE:
David Sedlák1af868e2019-07-17 17:03:14 +02002353 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02002354 break;
2355 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02002356 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02002357 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02002358 }
David Sedlák3ffbc522019-07-02 17:49:28 +02002359 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02002360 FREE_ARRAY(ctx, attrs, free_arg_rec);
2361 attrs = NULL;
2362 subelem = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02002363 }
2364 } else {
2365 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02002366 /* save text content, if text_content isn't set, it's just ignored */
David Sedlák3b4df842019-07-17 11:39:46 +02002367 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG, out, out_len));
David Sedlák3ffbc522019-07-02 17:49:28 +02002368 if (text_content) {
2369 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002370 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02002371 if (!*text_content) {
2372 free(out);
2373 return LY_EMEM;
2374 }
2375 } else {
2376 if (out_len == 0) {
David Sedlák99295322019-07-17 11:34:18 +02002377 *text_content = lydict_insert(ctx->xml_ctx.ctx, "", 0);
David Sedlák3ffbc522019-07-02 17:49:28 +02002378 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002379 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02002380 }
2381 }
2382 }
David Sedlákd6e56892019-07-01 15:40:24 +02002383 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +02002384 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
David Sedlákd6e56892019-07-01 15:40:24 +02002385 }
David Sedlák555c7202019-07-04 12:14:12 +02002386
David Sedlákda8ffa32019-07-08 14:17:10 +02002387 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02002388 }
2389
2390cleanup:
David Sedlák1af868e2019-07-17 17:03:14 +02002391 FREE_ARRAY(ctx, attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02002392 return ret;
2393}
2394
David Sedlák619db942019-07-03 14:47:30 +02002395LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002396yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, char *rev,
David Sedlák619db942019-07-03 14:47:30 +02002397 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +02002398{
David Sedlák3ffbc522019-07-02 17:49:28 +02002399 const char *temp_rev;
David Sedlák968ac342019-07-11 15:17:59 +02002400 struct yin_subelement subelems[1] = {
2401 {YANG_CUSTOM, NULL, 0}
2402 };
David Sedlák81e04022019-04-05 15:05:46 +02002403
David Sedlák292763b2019-07-09 11:10:53 +02002404 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
David Sedláka304bc62019-07-17 10:17:58 +02002405 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
2406 FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID);
David Sedlákda63c082019-06-04 13:52:23 +02002407
2408 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +02002409 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +02002410
David Sedlákda8ffa32019-07-08 14:17:10 +02002411 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02002412}
David Sedlák00250342019-06-21 14:19:39 +02002413
David Sedláke1a30302019-07-10 13:49:38 +02002414/**
2415 * @brief Parse config element.
2416 *
2417 * @param[in] ctx Yin parser context for logging and to store current state.
2418 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
2419 * @param[in,out] data Data to read from, always moved to currently handled character.
2420 * @param[in,out] flags Flags to add to.
2421 * @param[in,out] exts Extension instances to add to.
2422 *
2423 * @return LY_ERR values.
2424 */
2425static LY_ERR
2426yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
2427 struct lysp_ext_instance **exts)
2428{
2429 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02002430 struct yin_subelement subelems[1] = {
2431 {YANG_CUSTOM, NULL, 0}
2432 };
David Sedláke1a30302019-07-10 13:49:38 +02002433
David Sedlák1f90d252019-07-10 17:09:32 +02002434 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
David Sedláke1a30302019-07-10 13:49:38 +02002435 if (strcmp(temp_val, "true") == 0) {
2436 *flags |= LYS_CONFIG_W;
2437 } else if (strcmp(temp_val, "false") == 0) {
2438 *flags |= LYS_CONFIG_R;
2439 } else {
2440 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
2441 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2442 return LY_EVALID;
2443 }
2444 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2445
2446 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
2447}
2448
David Sedlák3ffbc522019-07-02 17:49:28 +02002449LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +02002450yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
2451 struct lysp_ext_instance **exts)
2452{
2453 const char *temp_version = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02002454 struct yin_subelement subelems[1] = {
2455 {YANG_CUSTOM, NULL, 0}
2456 };
David Sedlák92147b02019-07-09 14:01:01 +02002457
David Sedlák1f90d252019-07-10 17:09:32 +02002458 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
David Sedlák92147b02019-07-09 14:01:01 +02002459 if (strcmp(temp_version, "1.0") == 0) {
2460 *version = LYS_VERSION_1_0;
2461 } else if (strcmp(temp_version, "1.1") == 0) {
2462 *version = LYS_VERSION_1_1;
2463 } else {
2464 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
2465 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
2466 return LY_EVALID;
2467 }
2468 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
2469 ctx->mod_version = *version;
2470
2471 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
2472}
2473
2474LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002475yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_module *mod)
David Sedlák736fd0d2019-02-15 16:06:31 +01002476{
David Sedlák736fd0d2019-02-15 16:06:31 +01002477 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +02002478 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +02002479 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02002480
David Sedlák968ac342019-07-11 15:17:59 +02002481 struct yin_subelement subelems[5] = {
2482 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
2483 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
2484 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
2485 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
2486 {YANG_CUSTOM, NULL, 0}
2487 };
David Sedlák736fd0d2019-02-15 16:06:31 +01002488
David Sedlák92147b02019-07-09 14:01:01 +02002489 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +02002490 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, YANG_IMPORT));
David Sedlákda8ffa32019-07-08 14:17:10 +02002491 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +02002492 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +02002493 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, mod->imports, mod->mod->prefix, &imp->prefix), LY_EVALID);
David Sedlák00250342019-06-21 14:19:39 +02002494
David Sedlákda8ffa32019-07-08 14:17:10 +02002495 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +01002496}
2497
David Sedlák11900c82019-06-18 16:29:12 +02002498LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +02002499yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
2500 struct lysp_ext_instance **exts)
2501{
2502 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02002503 struct yin_subelement subelems[1] = {
2504 {YANG_CUSTOM, NULL, 0}
2505 };
David Sedlák1fdb2522019-07-09 16:22:57 +02002506
David Sedlák1f90d252019-07-10 17:09:32 +02002507 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
David Sedlák1fdb2522019-07-09 16:22:57 +02002508 if (strcmp(temp_val, "true") == 0) {
2509 *flags |= LYS_MAND_TRUE;
2510 } else if (strcmp(temp_val, "false") == 0) {
2511 *flags |= LYS_MAND_FALSE;
2512 } else {
2513 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
2514 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2515 return LY_EVALID;
2516 }
2517 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2518
2519 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
2520}
2521
2522LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002523yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
David Sedlák1fdb2522019-07-09 16:22:57 +02002524 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +02002525{
David Sedlák3ffbc522019-07-02 17:49:28 +02002526 const char *value = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02002527 struct yin_subelement subelems[1] = {
2528 {YANG_CUSTOM, NULL, 0}
2529 };
David Sedlák3ffbc522019-07-02 17:49:28 +02002530
David Sedlák292763b2019-07-09 11:10:53 +02002531 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, YANG_STATUS));
David Sedlák11900c82019-06-18 16:29:12 +02002532 if (strcmp(value, "current") == 0) {
2533 *flags |= LYS_STATUS_CURR;
2534 } else if (strcmp(value, "deprecated") == 0) {
2535 *flags |= LYS_STATUS_DEPRC;
2536 } else if (strcmp(value, "obsolete") == 0) {
2537 *flags |= LYS_STATUS_OBSLT;
2538 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002539 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
2540 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02002541 return LY_EVALID;
2542 }
David Sedlákda8ffa32019-07-08 14:17:10 +02002543 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02002544
David Sedlákda8ffa32019-07-08 14:17:10 +02002545 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02002546}
2547
David Sedlák11900c82019-06-18 16:29:12 +02002548LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02002549yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
2550{
2551 struct lysp_when *when;
2552 when = calloc(1, sizeof *when);
2553 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1f90d252019-07-10 17:09:32 +02002554 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
David Sedlák32eee7b2019-07-09 12:38:44 +02002555 *when_p = when;
David Sedlák968ac342019-07-11 15:17:59 +02002556 struct yin_subelement subelems[3] = {
2557 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
2558 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
2559 {YANG_CUSTOM, NULL, 0}
2560 };
David Sedlák32eee7b2019-07-09 12:38:44 +02002561
2562 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
2563}
2564
2565LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02002566yin_parse_yin_element_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02002567 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02002568{
David Sedlák3ffbc522019-07-02 17:49:28 +02002569 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02002570 struct yin_subelement subelems[1] = {
2571 {YANG_CUSTOM, NULL, 0}
2572 };
David Sedlák2721d3d2019-06-21 15:37:41 +02002573
David Sedlák1f90d252019-07-10 17:09:32 +02002574 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_YIN_ELEMENT));
David Sedlák2721d3d2019-06-21 15:37:41 +02002575 if (strcmp(temp_val, "true") == 0) {
2576 *flags |= LYS_YINELEM_TRUE;
2577 } else if (strcmp(temp_val, "false") == 0) {
2578 *flags |= LYS_YINELEM_FALSE;
2579 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002580 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
2581 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02002582 return LY_EVALID;
2583 }
David Sedlákda8ffa32019-07-08 14:17:10 +02002584 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02002585
David Sedlákda8ffa32019-07-08 14:17:10 +02002586 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02002587}
2588
2589LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002590yin_parse_extension_instance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, const char *ext_name,
David Sedlák619db942019-07-03 14:47:30 +02002591 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02002592{
2593 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02002594 char *out;
2595 const char *name, *prefix;
2596 size_t out_len, prefix_len, name_len;
2597 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02002598 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02002599 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
2600 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02002601
David Sedlákda8ffa32019-07-08 14:17:10 +02002602 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02002603
2604 e->yin = 0;
2605 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02002606 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02002607 e->insubstmt = subelem;
2608 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02002609 e->yin |= LYS_YIN;
2610
David Sedlákb1a78352019-06-28 16:16:29 +02002611 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02002612 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02002613 if (!iter->prefix) {
2614 new_subelem = calloc(1, sizeof(*new_subelem));
2615 if (!e->child) {
2616 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02002617 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02002618 last_subelem->next = new_subelem;
2619 }
2620 last_subelem = new_subelem;
2621
2622 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02002623 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
2624 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02002625 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002626 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
2627 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02002628 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002629 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
2630 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02002631 }
2632 }
2633 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02002634
David Sedlákf250ecf2019-07-01 11:02:05 +02002635 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02002636 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
2637 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02002638 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002639 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
2640 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02002641 if (!name) {
2642 /* end of extension instance reached */
2643 break;
2644 }
David Sedlákda8ffa32019-07-08 14:17:10 +02002645 LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, prefix, prefix_len, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +02002646 if (!e->child) {
2647 e->child = new_subelem;
2648 } else {
2649 last_subelem->next = new_subelem;
2650 }
2651 last_subelem = new_subelem;
2652 }
David Sedlák555c7202019-07-04 12:14:12 +02002653 } else {
2654 /* save text content */
2655 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002656 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02002657 if (!e->argument) {
2658 free(out);
2659 return LY_EMEM;
2660 }
2661 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002662 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02002663 LY_CHECK_RET(!e->argument, LY_EMEM);
2664 }
David Sedlákda8ffa32019-07-08 14:17:10 +02002665 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02002666 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02002667 }
David Sedlákb1a78352019-06-28 16:16:29 +02002668 }
2669
2670 return LY_SUCCESS;
2671}
2672
2673LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02002674yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix,
David Sedlákf250ecf2019-07-01 11:02:05 +02002675 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02002676{
2677 LY_ERR ret = LY_SUCCESS;
2678 const char *temp_prefix, *temp_name;
2679 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02002680 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02002681 int dynamic;
2682 struct yin_arg_record *subelem_args = NULL;
2683 struct lysp_stmt *last = NULL, *new = NULL;
2684
2685 /* allocate new structure for element */
2686 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02002687 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
2688 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02002689
2690 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02002691 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02002692 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02002693 /* add new element to linked-list */
2694 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02002695 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02002696 if (!(*element)->child) {
2697 /* save first */
2698 (*element)->child = new;
2699 } else {
2700 last->next = new;
2701 }
2702 last = new;
2703
2704 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02002705 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +02002706 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02002707 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02002708 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02002709 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
2710 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02002711 /* attributes with prefix are ignored */
2712 if (!temp_prefix) {
2713 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002714 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02002715 if (!last->arg) {
2716 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02002717 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02002718 ret = LY_EMEM;
2719 goto err;
2720 }
2721 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002722 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
2723 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02002724 }
2725 }
2726 }
2727
2728 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02002729 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02002730 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002731 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02002732 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02002733 ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +02002734 LY_CHECK_GOTO(ret, err);
2735 if (!name) {
2736 /* end of element reached */
2737 break;
2738 }
David Sedlákda8ffa32019-07-08 14:17:10 +02002739 ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, data, &last->next);
David Sedlákb1a78352019-06-28 16:16:29 +02002740 LY_CHECK_GOTO(ret, err);
2741 last = last->next;
2742 }
2743 } else {
2744 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02002745 if (out_len != 0) {
2746 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002747 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02002748 if (!(*element)->arg) {
2749 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02002750 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02002751 ret = LY_EMEM;
2752 goto err;
2753 }
2754 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002755 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
2756 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02002757 }
David Sedlákb1a78352019-06-28 16:16:29 +02002758 }
2759 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02002760 ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +02002761 LY_CHECK_GOTO(ret, err);
2762 }
2763
David Sedlákda8ffa32019-07-08 14:17:10 +02002764 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02002765 return LY_SUCCESS;
2766
2767err:
David Sedlákda8ffa32019-07-08 14:17:10 +02002768 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02002769 return ret;
2770}
2771
2772LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002773yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02002774 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02002775{
David Sedlák968ac342019-07-11 15:17:59 +02002776 struct yin_subelement subelems[2] = {
2777 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2778 {YANG_CUSTOM, NULL, 0}
2779 };
David Sedlák9494eb22019-06-21 16:06:53 +02002780
David Sedlák292763b2019-07-09 11:10:53 +02002781 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, YANG_ARGUMENT));
David Sedlák9494eb22019-06-21 16:06:53 +02002782
David Sedlákda8ffa32019-07-08 14:17:10 +02002783 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02002784}
2785
2786LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002787yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_ext **extensions)
David Sedlák11900c82019-06-18 16:29:12 +02002788{
David Sedlák11900c82019-06-18 16:29:12 +02002789 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02002790 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02002791 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, YANG_EXTENSION));
David Sedlák619db942019-07-03 14:47:30 +02002792
David Sedlák3ffbc522019-07-02 17:49:28 +02002793 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
David Sedlák968ac342019-07-11 15:17:59 +02002794 struct yin_subelement subelems[5] = {
2795 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2796 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2797 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2798 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2799 {YANG_CUSTOM, NULL, 0}
2800 };
David Sedlák11900c82019-06-18 16:29:12 +02002801
David Sedlákda8ffa32019-07-08 14:17:10 +02002802 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02002803}
2804
David Sedlák00250342019-06-21 14:19:39 +02002805/**
2806 * @brief Parse module substatements.
2807 *
David Sedlákda8ffa32019-07-08 14:17:10 +02002808 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02002809 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02002810 * @param[in,out] data Data to read from.
2811 * @param[out] mod Parsed module structure.
2812 *
2813 * @return LY_ERR values.
2814 */
David Sedlákda8ffa32019-07-08 14:17:10 +02002815static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002816yin_parse_mod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +02002817{
David Sedlák968ac342019-07-11 15:17:59 +02002818 struct yin_subelement subelems[9] = {
2819 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
2820 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
2821 {YANG_EXTENSION, &(*mod)->exts, 0},
2822 {YANG_IMPORT, *mod, 0},
2823 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
2824 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
2825 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
2826 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
2827 {YANG_CUSTOM, NULL, 0}
2828 };
David Sedlák3b4db242018-10-19 16:11:01 +02002829
David Sedlák292763b2019-07-09 11:10:53 +02002830 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &(*mod)->mod->name, Y_IDENTIF_ARG, YANG_MODULE));
David Sedlák4a4c0722018-11-26 17:03:10 +01002831
David Sedlákda8ffa32019-07-08 14:17:10 +02002832 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02002833}
2834
2835LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01002836yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02002837{
David Sedláke4889912018-11-02 09:52:40 +01002838 LY_ERR ret = LY_SUCCESS;
2839 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01002840 struct lysp_module *mod_p = NULL;
2841 const char *prefix, *name;
2842 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02002843
David Sedlák1f90d252019-07-10 17:09:32 +02002844 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02002845
David Sedlákda8ffa32019-07-08 14:17:10 +02002846 struct yin_parser_ctx yin_ctx;
2847
2848 /* initialize context */
2849 memset(&yin_ctx, 0, sizeof yin_ctx);
2850 yin_ctx.xml_ctx.ctx = ctx;
2851 yin_ctx.xml_ctx.line = 1;
2852
David Sedláke4889912018-11-02 09:52:40 +01002853
David Sedlák3017da42019-02-15 09:48:04 +01002854 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02002855 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02002856 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1f90d252019-07-10 17:09:32 +02002857 ret = yin_load_attributes(&yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02002858 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02002859 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01002860 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01002861 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
2862 ret = LY_EINVAL;
2863 goto cleanup;
2864 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002865 LOGVAL_PARSER((struct lys_parser_ctx *)&yin_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +02002866 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01002867 ret = LY_EVALID;
2868 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01002869 }
2870
David Sedlák3017da42019-02-15 09:48:04 +01002871 /* allocate module */
2872 mod_p = calloc(1, sizeof *mod_p);
2873 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
2874 mod_p->mod = mod;
2875 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01002876
David Sedlák00250342019-06-21 14:19:39 +02002877 /* parse module substatements */
David Sedlák1f90d252019-07-10 17:09:32 +02002878 ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01002879 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01002880
David Sedlák3017da42019-02-15 09:48:04 +01002881 mod_p->parsing = 0;
2882 mod->parsed = mod_p;
2883
2884cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02002885 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01002886 lysp_module_free(mod_p);
2887 }
David Sedlák1f90d252019-07-10 17:09:32 +02002888 FREE_ARRAY(&yin_ctx, attrs, free_arg_rec);
David Sedlákda8ffa32019-07-08 14:17:10 +02002889 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01002890 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02002891}