blob: 4dc25e0aacc5ddd4b930ed71886518f526b16bdb [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ák00250342019-06-21 14:19:39 +0200169 if (record->dynamic_content) {
170 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
789/**
David Sedlákc1771b12019-07-10 15:55:46 +0200790 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200791 * text element as child
792 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200793 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200794 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200795 * @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 +0200796 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200797 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200798 *
799 * @return LY_ERR values.
800 */
801static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200802yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200803 const char **value, struct lysp_ext_instance **exts)
804{
805 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
806
David Sedlák968ac342019-07-11 15:17:59 +0200807 struct yin_subelement subelems[2] = {
808 {YANG_CUSTOM, NULL, 0},
809 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
810 };
David Sedlákb4e44562019-07-04 15:42:12 +0200811
David Sedlákda8ffa32019-07-08 14:17:10 +0200812 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200813}
814
815/**
David Sedlákc1771b12019-07-10 15:55:46 +0200816 * @brief Parse error-message element.
817 *
818 * @param[in,out] ctx Yin parser context for logging and to store current state.
819 * @param[in,out] data Data to read from.
820 * @param[out] value Where the content of error-message element should be stored.
821 * @param[in,out] exts Extension instance to add to.
822 *
823 * @return LY_ERR values.
824 */
825static LY_ERR
826yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
827 struct lysp_ext_instance **exts)
828{
David Sedlák968ac342019-07-11 15:17:59 +0200829 struct yin_subelement subelems[2] = {
830 {YANG_CUSTOM, NULL, 0},
831 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
832 };
David Sedlákc1771b12019-07-10 15:55:46 +0200833
834 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
835}
836
837/**
David Sedlák374d2b32019-07-17 15:06:55 +0200838 * @brief parse type element.
839 *
840 * @brief Parse position or value element.
841 *
842 * @param[in,out] ctx YIN parser context for logging and to store current state.
843 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
844 * @param[in,out] data Data to read from, always moved to currently handled character.
845 * @param[in,out] type Type to wrote to.
846 * @param[in,out] exts Extension instance to add to.
847 *
848 * @return LY_ERR values.
849 */
850static LY_ERR
851yin_parse_type(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type)
852{
853 struct yin_subelement subelems[11] = {
854 {YANG_BASE, type, 0},
855 {YANG_BIT, type, 0},
856 {YANG_ENUM, type, 0},
857 {YANG_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
858 {YANG_LENGTH, type, YIN_SUBELEM_UNIQUE},
859 {YANG_PATH, type, YIN_SUBELEM_UNIQUE},
860 {YANG_PATTERN, type, 0},
861 {YANG_RANGE, type, YIN_SUBELEM_UNIQUE},
862 {YANG_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
863 {YANG_TYPE, type},
864 {YANG_CUSTOM, NULL, 0},
865 };
866 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, YANG_TYPE));
867 return yin_parse_content(ctx, subelems, 11, data, YANG_TYPE, NULL, &type->exts);
868}
869
David Sedlák1af868e2019-07-17 17:03:14 +0200870/**
871 * @brief Parse max-elements element.
872 *
873 * @param[in,out] ctx YIN parser context for logging and to store current state.
874 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
875 * @param[in,out] data Data to read from, always moved to currently handled character.
876 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +0200877 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +0200878 * @param[in,out] exts Extension instances to add to.
879 *
880 * @return LY_ERR values.
881 */
882static LY_ERR
883yin_parse_maxelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *max,
884 uint16_t *flags, struct lysp_ext_instance **exts)
885{
886 const char *temp_val = NULL;
887 char *ptr;
888 unsigned long int num;
889 struct yin_subelement subelems[1] = {
890 {YANG_CUSTOM, NULL, 0},
891 };
David Sedlák374d2b32019-07-17 15:06:55 +0200892
David Sedlák1af868e2019-07-17 17:03:14 +0200893 *flags |= LYS_SET_MAX;
894 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MAX_ELEMENTS));
895 if (!temp_val || temp_val[0] == '\0' || temp_val[0] == '0' || (temp_val[0] != 'u' && !isdigit(temp_val[0]))) {
896 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "max-elements");
897 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
898 return LY_EVALID;
899 }
900
901 if (strcmp(temp_val, "unbounded")) {
902 errno = 0;
903 num = strtoul(temp_val, &ptr, 10);
904 if (*ptr != '\0') {
905 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "max-elements");
906 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
907 return LY_EVALID;
908 }
909 if ((errno == ERANGE) || (num > UINT32_MAX)) {
910 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, "max-elements");
911 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
912 return LY_EVALID;
913 }
914 *max = num;
915 }
916 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
917 return yin_parse_content(ctx, subelems, 1, data, YANG_MAX_ELEMENTS, NULL, exts);
918}
David Sedlák374d2b32019-07-17 15:06:55 +0200919
920/**
David Sedlák09e18c92019-07-18 11:17:11 +0200921 * @brief Parse max-elements element.
922 *
923 * @param[in,out] ctx YIN parser context for logging and to store current state.
924 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
925 * @param[in,out] data Data to read from, always moved to currently handled character.
926 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +0200927 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +0200928 * @param[in,out] exts Extension instances to add to.
929 *
930 * @return LY_ERR values.
931 */
932static LY_ERR
933yin_parse_minelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *min,
934 uint16_t *flags, struct lysp_ext_instance **exts)
935{
936 const char *temp_val = NULL;
937 char *ptr;
938 unsigned long int num;
939 struct yin_subelement subelems[1] = {
940 {YANG_CUSTOM, NULL, 0},
941 };
942
943 *flags |= LYS_SET_MIN;
David Sedláka2dad212019-07-18 12:45:19 +0200944 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 +0200945
946 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) {
947 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "min-elements");
948 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
949 return LY_EVALID;
950 }
951
952 errno = 0;
953 num = strtoul(temp_val, &ptr, 10);
954 if (ptr[0] != 0) {
955 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "min-elements");
956 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
957 return LY_EVALID;
958 }
959 if (errno == ERANGE || num > UINT32_MAX) {
960 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, "min-elements");
961 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
962 return LY_EVALID;
963 }
964 *min = num;
965 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +0200966 return yin_parse_content(ctx, subelems, 1, data, YANG_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +0200967}
968
David Sedláka2dad212019-07-18 12:45:19 +0200969/**
970 * @brief Parse min-elements or max-elements element.
971 *
972 * @param[in,out] ctx YIN parser context for logging and to store current state.
973 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
974 * @param[in,out] data Data to read from, always moved to currently handled character.
975 * @param[in] parent Identification of parent element.
976 * @param[in] current Identification of current element.
977 * @param[in] dest Where the parsed value and flags should be stored.
978 *
979 * @return LY_ERR values.
980 */
David Sedlák09e18c92019-07-18 11:17:11 +0200981static LY_ERR
982yin_parse_minmax(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
983 enum yang_keyword parent, enum yang_keyword current, void *dest)
984{
985 assert(current == YANG_MAX_ELEMENTS || current == YANG_MIN_ELEMENTS);
986 assert(parent == YANG_LEAF_LIST || parent == YANG_REFINE || parent == YANG_LIST);
987 uint32_t *lim;
988 uint16_t *flags;
989 struct lysp_ext_instance **exts;
990
991 if (parent == YANG_LEAF_LIST) {
992 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
993 flags = &((struct lysp_node_leaflist *)dest)->flags;
994 exts = &((struct lysp_node_leaflist *)dest)->exts;
995 } else if (parent == YANG_REFINE) {
996 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
997 flags = &((struct lysp_refine *)dest)->flags;
998 exts = &((struct lysp_refine *)dest)->exts;
999 } else {
1000 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
1001 flags = &((struct lysp_node_list *)dest)->flags;
1002 exts = &((struct lysp_node_list *)dest)->exts;
1003 }
1004
1005 if (current == YANG_MAX_ELEMENTS) {
1006 LY_CHECK_RET(yin_parse_maxelements(ctx, attrs, data, lim, flags, exts));
1007 } else {
1008 LY_CHECK_RET(yin_parse_minelements(ctx, attrs, data, lim, flags, exts));
1009 }
1010
1011 return LY_SUCCESS;
1012}
1013
1014/**
David Sedláka2dad212019-07-18 12:45:19 +02001015 * @brief Parser ordered-by element.
1016 *
1017 * @param[in,out] ctx YIN parser context for logging and to store current state.
1018 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1019 * @param[in,out] data Data to read from, always moved to currently handled character.
1020 * @param[out] flags Flags to write to.
1021 * @param[in,out] exts Extension instance to add to.
1022 *
1023 * @return LY_ERR values.
1024 */
1025static LY_ERR
1026yin_parse_orderedby(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1027 uint16_t *flags, struct lysp_ext_instance **exts)
1028{
1029 const char *temp_val;
1030 struct yin_subelement subelems[1] = {
1031 {YANG_CUSTOM, NULL, 0},
1032 };
1033
1034 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_ORDERED_BY));
1035 if (strcmp(temp_val, "system") == 0) {
1036 *flags |= LYS_ORDBY_SYSTEM;
1037 } else if (strcmp(temp_val, "user") == 0) {
1038 *flags |= LYS_ORDBY_USER;
1039 } else {
1040 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "ordered-by");
1041 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1042 return LY_EVALID;
1043 }
1044 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1045
1046 return yin_parse_content(ctx, subelems, 1, data, YANG_ORDERED_BY, NULL, exts);
1047}
1048
1049/**
David Sedlákb4e44562019-07-04 15:42:12 +02001050 * @brief Map keyword type to substatement info.
1051 *
1052 * @param[in] kw Keyword type.
1053 *
1054 * @return correct LYEXT_SUBSTMT information.
1055 */
1056static LYEXT_SUBSTMT
1057kw2lyext_substmt(enum yang_keyword kw)
1058{
1059 switch (kw) {
1060 case YANG_ARGUMENT:
1061 return LYEXT_SUBSTMT_ARGUMENT;
1062 case YANG_BASE:
1063 return LYEXT_SUBSTMT_BASE;
1064 case YANG_BELONGS_TO:
1065 return LYEXT_SUBSTMT_BELONGSTO;
1066 case YANG_CONTACT:
1067 return LYEXT_SUBSTMT_CONTACT;
1068 case YANG_DEFAULT:
1069 return LYEXT_SUBSTMT_DEFAULT;
1070 case YANG_DESCRIPTION:
1071 return LYEXT_SUBSTMT_DESCRIPTION;
1072 case YANG_ERROR_APP_TAG:
1073 return LYEXT_SUBSTMT_ERRTAG;
1074 case YANG_ERROR_MESSAGE:
1075 return LYEXT_SUBSTMT_ERRMSG;
1076 case YANG_KEY:
1077 return LYEXT_SUBSTMT_KEY;
1078 case YANG_NAMESPACE:
1079 return LYEXT_SUBSTMT_NAMESPACE;
1080 case YANG_ORGANIZATION:
1081 return LYEXT_SUBSTMT_ORGANIZATION;
1082 case YANG_PATH:
1083 return LYEXT_SUBSTMT_PATH;
1084 case YANG_PREFIX:
1085 return LYEXT_SUBSTMT_PREFIX;
1086 case YANG_PRESENCE:
1087 return LYEXT_SUBSTMT_PRESENCE;
1088 case YANG_REFERENCE:
1089 return LYEXT_SUBSTMT_REFERENCE;
1090 case YANG_REVISION_DATE:
1091 return LYEXT_SUBSTMT_REVISIONDATE;
1092 case YANG_UNITS:
1093 return LYEXT_SUBSTMT_UNITS;
1094 case YANG_VALUE:
1095 return LYEXT_SUBSTMT_VALUE;
1096 case YANG_YANG_VERSION:
1097 return LYEXT_SUBSTMT_VERSION;
1098 case YANG_MODIFIER:
1099 return LYEXT_SUBSTMT_MODIFIER;
1100 case YANG_REQUIRE_INSTANCE:
1101 return LYEXT_SUBSTMT_REQINSTANCE;
1102 case YANG_YIN_ELEMENT:
1103 return LYEXT_SUBSTMT_YINELEM;
1104 case YANG_CONFIG:
1105 return LYEXT_SUBSTMT_CONFIG;
1106 case YANG_MANDATORY:
1107 return LYEXT_SUBSTMT_MANDATORY;
1108 case YANG_ORDERED_BY:
1109 return LYEXT_SUBSTMT_ORDEREDBY;
1110 case YANG_STATUS:
1111 return LYEXT_SUBSTMT_STATUS;
1112 case YANG_FRACTION_DIGITS:
1113 return LYEXT_SUBSTMT_FRACDIGITS;
1114 case YANG_MAX_ELEMENTS:
1115 return LYEXT_SUBSTMT_MAX;
1116 case YANG_MIN_ELEMENTS:
1117 return LYEXT_SUBSTMT_MIN;
1118 case YANG_POSITION:
1119 return LYEXT_SUBSTMT_POSITION;
1120 case YANG_UNIQUE:
1121 return LYEXT_SUBSTMT_UNIQUE;
1122 case YANG_IF_FEATURE:
1123 return LYEXT_SUBSTMT_IFFEATURE;
1124 default:
1125 return LYEXT_SUBSTMT_SELF;
1126 }
1127}
1128
David Sedlák9c40a922019-07-08 17:04:43 +02001129/**
1130 * @brief Parse belongs-to element.
1131 *
1132 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +02001133 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +02001134 * @param[in,out] data Data to read from, always moved to currently handled character.
1135 * @param[out] submod Structure of submodule that is being parsed.
1136 * @param[in,out] exts Extension instances to add to.
1137 *
1138 * @return LY_ERR values
1139 */
1140static LY_ERR
1141yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1142 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
1143{
David Sedlák968ac342019-07-11 15:17:59 +02001144 struct yin_subelement subelems[2] = {
1145 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1146 {YANG_CUSTOM, NULL, 0}
1147 };
David Sedlák1f90d252019-07-10 17:09:32 +02001148 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, YANG_BELONGS_TO));
David Sedlák9c40a922019-07-08 17:04:43 +02001149
1150 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
1151}
1152
David Sedlákd6e56892019-07-01 15:40:24 +02001153LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001154yin_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 +02001155 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 +02001156{
1157 LY_ERR ret = LY_SUCCESS;
1158 struct sized_string prefix, name;
David Sedlák8e7bda82019-07-16 17:57:50 +02001159 char *out = NULL;
1160 size_t out_len = 0;
1161 int dynamic = 0;
David Sedlák1af868e2019-07-17 17:03:14 +02001162 struct yin_arg_record *attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02001163 enum yang_keyword kw = YANG_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02001164 struct yin_subelement *subelem = NULL;
David Sedlák374d2b32019-07-17 15:06:55 +02001165 struct lysp_type *type, *nested_type;
David Sedlák09e18c92019-07-18 11:17:11 +02001166
David Sedlákb0faad82019-07-04 14:28:59 +02001167 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +02001168
David Sedlákda8ffa32019-07-08 14:17:10 +02001169 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1170 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +02001171 /* current element has subelements as content */
1172 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001173 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1174 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +02001175 LY_CHECK_GOTO(ret, cleanup);
1176 if (!name.value) {
1177 /* end of current element reached */
1178 break;
1179 }
David Sedlák1af868e2019-07-17 17:03:14 +02001180 ret = yin_load_attributes(ctx, data, &attrs);
David Sedlákd6e56892019-07-01 15:40:24 +02001181 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001182 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +02001183
1184 /* check if this element can be child of current element */
David Sedlák1af868e2019-07-17 17:03:14 +02001185 subelem = get_record(kw, subelem_info_size, subelem_info);
1186 if (!subelem) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001187 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 +02001188 ret = LY_EVALID;
1189 goto cleanup;
1190 }
1191
David Sedlák5f8191e2019-07-08 16:35:52 +02001192 /* TODO check relative order */
1193
David Sedlák21f87cd2019-07-03 16:53:23 +02001194 /* if element is unique and already defined log error */
David Sedlák1af868e2019-07-17 17:03:14 +02001195 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001196 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 +02001197 return LY_EVALID;
1198 }
David Sedlák1af868e2019-07-17 17:03:14 +02001199 if (subelem->flags & YIN_SUBELEM_FIRST) {
1200 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
David Sedlák66d7c842019-07-11 15:06:04 +02001201 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +02001202 }
David Sedlák1af868e2019-07-17 17:03:14 +02001203 subelem->flags |= YIN_SUBELEM_PARSED;
David Sedlák21f87cd2019-07-03 16:53:23 +02001204
David Sedlákd6e56892019-07-01 15:40:24 +02001205 switch (kw) {
1206 case YANG_CUSTOM:
David Sedlák1af868e2019-07-17 17:03:14 +02001207 ret = yin_parse_extension_instance(ctx, attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +02001208 namelen2fulllen(name.len, prefix.len),
David Sedlák1af868e2019-07-17 17:03:14 +02001209 kw2lyext_substmt(current_element),
1210 (subelem->dest) ? *((uint32_t*)subelem->dest) : 0, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001211 break;
1212 case YANG_ACTION:
1213 break;
1214 case YANG_ANYDATA:
1215 break;
1216 case YANG_ANYXML:
1217 break;
1218 case YANG_ARGUMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02001219 ret = yin_parse_argument_element(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001220 break;
1221 case YANG_AUGMENT:
1222 break;
1223 case YANG_BASE:
David Sedláka62750b2019-07-16 11:21:31 +02001224 if (current_element == YANG_TYPE) {
David Sedlák1af868e2019-07-17 17:03:14 +02001225 type = (struct lysp_type *)subelem->dest;
1226 ret = yin_parse_simple_elements(ctx, attrs, data, kw, &type->bases, YIN_ARG_NAME,
David Sedlák292763b2019-07-09 11:10:53 +02001227 Y_PREF_IDENTIF_ARG, exts);
1228 type->flags |= LYS_SET_BASE;
David Sedláka62750b2019-07-16 11:21:31 +02001229 } else if (current_element == YANG_IDENTITY) {
David Sedlák1af868e2019-07-17 17:03:14 +02001230 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
David Sedlák292763b2019-07-09 11:10:53 +02001231 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
1232 } else {
1233 LOGINT(ctx->xml_ctx.ctx);
1234 ret = LY_EINT;
1235 }
David Sedlákd6e56892019-07-01 15:40:24 +02001236 break;
1237 case YANG_BELONGS_TO:
David Sedlák1af868e2019-07-17 17:03:14 +02001238 ret = yin_parse_belongs_to(ctx, attrs, data, (struct lysp_submodule *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001239 break;
1240 case YANG_BIT:
David Sedlák07869a52019-07-12 14:28:19 +02001241 case YANG_ENUM:
David Sedlák1af868e2019-07-17 17:03:14 +02001242 ret = yin_parse_enum_bit(ctx, attrs, data, kw, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001243 break;
1244 case YANG_CASE:
1245 break;
1246 case YANG_CHOICE:
1247 break;
1248 case YANG_CONFIG:
David Sedlák1af868e2019-07-17 17:03:14 +02001249 ret = yin_parse_config(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001250 break;
1251 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +02001252 case YANG_DESCRIPTION:
1253 case YANG_ORGANIZATION:
1254 case YANG_REFERENCE:
David Sedlák1af868e2019-07-17 17:03:14 +02001255 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001256 break;
1257 case YANG_CONTAINER:
1258 break;
1259 case YANG_DEFAULT:
David Sedlák1af868e2019-07-17 17:03:14 +02001260 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedláke7084ce2019-07-10 16:44:15 +02001261 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001262 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001263 case YANG_DEVIATE:
1264 break;
1265 case YANG_DEVIATION:
1266 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001267 case YANG_ERROR_APP_TAG:
David Sedlák1af868e2019-07-17 17:03:14 +02001268 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedlák2ce1be62019-07-10 16:15:09 +02001269 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001270 break;
1271 case YANG_ERROR_MESSAGE:
David Sedlák1af868e2019-07-17 17:03:14 +02001272 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001273 break;
1274 case YANG_EXTENSION:
David Sedlák1af868e2019-07-17 17:03:14 +02001275 ret = yin_parse_extension(ctx, attrs, data, (struct lysp_ext **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001276 break;
1277 case YANG_FEATURE:
1278 break;
1279 case YANG_FRACTION_DIGITS:
David Sedlák1af868e2019-07-17 17:03:14 +02001280 ret = yin_parse_fracdigits(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001281 break;
1282 case YANG_GROUPING:
1283 break;
1284 case YANG_IDENTITY:
1285 break;
1286 case YANG_IF_FEATURE:
David Sedlák1af868e2019-07-17 17:03:14 +02001287 ret = yin_parse_simple_elements(ctx, attrs, data, kw,
1288 (const char ***)subelem->dest, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001289 break;
1290 case YANG_IMPORT:
David Sedlák1af868e2019-07-17 17:03:14 +02001291 ret = yin_parse_import(ctx, attrs, data, (struct lysp_module *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001292 break;
1293 case YANG_INCLUDE:
1294 break;
1295 case YANG_INPUT:
1296 break;
1297 case YANG_KEY:
1298 break;
1299 case YANG_LEAF:
1300 break;
1301 case YANG_LEAF_LIST:
1302 break;
1303 case YANG_LENGTH:
David Sedlák1af868e2019-07-17 17:03:14 +02001304 type = (struct lysp_type *)subelem->dest;
David Sedlák438ae432019-07-11 15:36:54 +02001305 type->length = calloc(1, sizeof *type->length);
1306 LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02001307 ret = yin_parse_restriction(ctx, attrs, data, kw, type->length);
David Sedlák438ae432019-07-11 15:36:54 +02001308 type->flags |= LYS_SET_LENGTH;
David Sedlákd6e56892019-07-01 15:40:24 +02001309 break;
1310 case YANG_LIST:
1311 break;
1312 case YANG_MANDATORY:
David Sedlák1af868e2019-07-17 17:03:14 +02001313 ret = yin_parse_mandatory(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001314 break;
1315 case YANG_MAX_ELEMENTS:
David Sedlákd6e56892019-07-01 15:40:24 +02001316 case YANG_MIN_ELEMENTS:
David Sedlák09e18c92019-07-18 11:17:11 +02001317 ret = yin_parse_minmax(ctx, attrs, data, current_element, kw, subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001318 break;
1319 case YANG_MODIFIER:
David Sedlák1af868e2019-07-17 17:03:14 +02001320 ret = yin_parse_modifier(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001321 break;
1322 case YANG_MODULE:
1323 break;
1324 case YANG_MUST:
David Sedlák1af868e2019-07-17 17:03:14 +02001325 ret = yin_parse_must(ctx, attrs, data, (struct lysp_restr **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001326 break;
1327 case YANG_NAMESPACE:
David Sedlák1af868e2019-07-17 17:03:14 +02001328 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedlák2ce1be62019-07-10 16:15:09 +02001329 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001330 break;
1331 case YANG_NOTIFICATION:
1332 break;
1333 case YANG_ORDERED_BY:
David Sedláka2dad212019-07-18 12:45:19 +02001334 ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001335 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001336 case YANG_OUTPUT:
1337 break;
1338 case YANG_PATH:
David Sedlák1af868e2019-07-17 17:03:14 +02001339 type = (struct lysp_type *)subelem->dest;
1340 ret = yin_parse_simple_element(ctx, attrs, data, kw, &type->path,
David Sedlák58979872019-07-12 11:42:43 +02001341 YIN_ARG_VALUE, Y_STR_ARG, exts);
1342 type->flags |= LYS_SET_PATH;
David Sedlákd6e56892019-07-01 15:40:24 +02001343 break;
1344 case YANG_PATTERN:
David Sedlák1af868e2019-07-17 17:03:14 +02001345 ret = yin_parse_pattern(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001346 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02001347 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02001348 case YANG_POSITION:
David Sedlák1af868e2019-07-17 17:03:14 +02001349 ret = yin_parse_value_pos_element(ctx, attrs, data, kw,
1350 (struct lysp_type_enum *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001351 break;
1352 case YANG_PREFIX:
David Sedlák1af868e2019-07-17 17:03:14 +02001353 ret = yin_parse_simple_element(ctx, attrs, data, kw,
1354 (const char **)subelem->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001355 break;
1356 case YANG_PRESENCE:
1357 break;
1358 case YANG_RANGE:
David Sedlák1af868e2019-07-17 17:03:14 +02001359 type = (struct lysp_type *)subelem->dest;
David Sedlákb7296dd2019-07-11 14:58:38 +02001360 type->range = calloc(1, sizeof *type->range);
David Sedlák66d7c842019-07-11 15:06:04 +02001361 LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02001362 ret = yin_parse_restriction(ctx, attrs, data, kw, type->range);
David Sedlák438ae432019-07-11 15:36:54 +02001363 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +02001364 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001365 case YANG_REFINE:
1366 break;
1367 case YANG_REQUIRE_INSTANCE:
David Sedlák1af868e2019-07-17 17:03:14 +02001368 ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001369 break;
1370 case YANG_REVISION:
1371 break;
1372 case YANG_REVISION_DATE:
David Sedlák1af868e2019-07-17 17:03:14 +02001373 ret = yin_parse_revision_date(ctx, attrs, data, (char *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001374 break;
1375 case YANG_RPC:
1376 break;
1377 case YANG_STATUS:
David Sedlák1af868e2019-07-17 17:03:14 +02001378 ret = yin_parse_status(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001379 break;
1380 case YANG_SUBMODULE:
1381 break;
1382 case YANG_TYPE:
David Sedlák374d2b32019-07-17 15:06:55 +02001383 /* type as child of another type */
David Sedlák1af868e2019-07-17 17:03:14 +02001384 type = (struct lysp_type *)subelem->dest;
David Sedlák374d2b32019-07-17 15:06:55 +02001385 if (current_element == YANG_TYPE) {
1386 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, type->types, nested_type, ret, cleanup);
1387 type = nested_type;
1388 }
David Sedlák1af868e2019-07-17 17:03:14 +02001389 ret = yin_parse_type(ctx, attrs, data, type);
David Sedlákd6e56892019-07-01 15:40:24 +02001390 break;
1391 case YANG_TYPEDEF:
1392 break;
1393 case YANG_UNIQUE:
David Sedlák1af868e2019-07-17 17:03:14 +02001394 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
David Sedláka5b1d382019-07-10 16:31:09 +02001395 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001396 break;
1397 case YANG_UNITS:
David Sedlák1af868e2019-07-17 17:03:14 +02001398 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedláka5b1d382019-07-10 16:31:09 +02001399 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001400 break;
1401 case YANG_USES:
1402 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001403 case YANG_WHEN:
David Sedlák1af868e2019-07-17 17:03:14 +02001404 ret = yin_parse_when(ctx, attrs, data, (struct lysp_when **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001405 break;
1406 case YANG_YANG_VERSION:
David Sedlák1af868e2019-07-17 17:03:14 +02001407 ret = yin_parse_yangversion(ctx, attrs, data, (uint8_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001408 break;
1409 case YANG_YIN_ELEMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02001410 ret = yin_parse_yin_element_element(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001411 break;
1412 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02001413 case YIN_VALUE:
David Sedlák1af868e2019-07-17 17:03:14 +02001414 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02001415 break;
1416 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02001417 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02001418 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02001419 }
David Sedlák3ffbc522019-07-02 17:49:28 +02001420 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02001421 FREE_ARRAY(ctx, attrs, free_arg_rec);
1422 attrs = NULL;
1423 subelem = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02001424 }
1425 } else {
1426 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02001427 /* save text content, if text_content isn't set, it's just ignored */
David Sedlák3b4df842019-07-17 11:39:46 +02001428 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG, out, out_len));
David Sedlák3ffbc522019-07-02 17:49:28 +02001429 if (text_content) {
1430 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001431 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02001432 if (!*text_content) {
1433 free(out);
1434 return LY_EMEM;
1435 }
1436 } else {
1437 if (out_len == 0) {
David Sedlák99295322019-07-17 11:34:18 +02001438 *text_content = lydict_insert(ctx->xml_ctx.ctx, "", 0);
David Sedlák3ffbc522019-07-02 17:49:28 +02001439 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001440 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02001441 }
1442 }
1443 }
David Sedlákd6e56892019-07-01 15:40:24 +02001444 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001445 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 +02001446 }
David Sedlák555c7202019-07-04 12:14:12 +02001447
David Sedlákda8ffa32019-07-08 14:17:10 +02001448 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02001449 }
1450
1451cleanup:
David Sedlák1af868e2019-07-17 17:03:14 +02001452 FREE_ARRAY(ctx, attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001453 return ret;
1454}
1455
David Sedlák619db942019-07-03 14:47:30 +02001456LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001457yin_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 +02001458 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +02001459{
David Sedlák3ffbc522019-07-02 17:49:28 +02001460 const char *temp_rev;
David Sedlák968ac342019-07-11 15:17:59 +02001461 struct yin_subelement subelems[1] = {
1462 {YANG_CUSTOM, NULL, 0}
1463 };
David Sedlák81e04022019-04-05 15:05:46 +02001464
David Sedlák292763b2019-07-09 11:10:53 +02001465 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 +02001466 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
1467 FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID);
David Sedlákda63c082019-06-04 13:52:23 +02001468
1469 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +02001470 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +02001471
David Sedlákda8ffa32019-07-08 14:17:10 +02001472 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001473}
David Sedlák00250342019-06-21 14:19:39 +02001474
David Sedláke1a30302019-07-10 13:49:38 +02001475/**
1476 * @brief Parse config element.
1477 *
1478 * @param[in] ctx Yin parser context for logging and to store current state.
1479 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1480 * @param[in,out] data Data to read from, always moved to currently handled character.
1481 * @param[in,out] flags Flags to add to.
1482 * @param[in,out] exts Extension instances to add to.
1483 *
1484 * @return LY_ERR values.
1485 */
1486static LY_ERR
1487yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1488 struct lysp_ext_instance **exts)
1489{
1490 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001491 struct yin_subelement subelems[1] = {
1492 {YANG_CUSTOM, NULL, 0}
1493 };
David Sedláke1a30302019-07-10 13:49:38 +02001494
David Sedlák1f90d252019-07-10 17:09:32 +02001495 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 +02001496 if (strcmp(temp_val, "true") == 0) {
1497 *flags |= LYS_CONFIG_W;
1498 } else if (strcmp(temp_val, "false") == 0) {
1499 *flags |= LYS_CONFIG_R;
1500 } else {
1501 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
1502 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1503 return LY_EVALID;
1504 }
1505 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1506
1507 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1508}
1509
David Sedlák3ffbc522019-07-02 17:49:28 +02001510LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +02001511yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1512 struct lysp_ext_instance **exts)
1513{
1514 const char *temp_version = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001515 struct yin_subelement subelems[1] = {
1516 {YANG_CUSTOM, NULL, 0}
1517 };
David Sedlák92147b02019-07-09 14:01:01 +02001518
David Sedlák1f90d252019-07-10 17:09:32 +02001519 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 +02001520 if (strcmp(temp_version, "1.0") == 0) {
1521 *version = LYS_VERSION_1_0;
1522 } else if (strcmp(temp_version, "1.1") == 0) {
1523 *version = LYS_VERSION_1_1;
1524 } else {
1525 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
1526 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1527 return LY_EVALID;
1528 }
1529 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1530 ctx->mod_version = *version;
1531
1532 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1533}
1534
1535LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001536yin_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 +01001537{
David Sedlák736fd0d2019-02-15 16:06:31 +01001538 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +02001539 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +02001540 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02001541
David Sedlák968ac342019-07-11 15:17:59 +02001542 struct yin_subelement subelems[5] = {
1543 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1544 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1545 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1546 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1547 {YANG_CUSTOM, NULL, 0}
1548 };
David Sedlák736fd0d2019-02-15 16:06:31 +01001549
David Sedlák92147b02019-07-09 14:01:01 +02001550 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +02001551 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 +02001552 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +02001553 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +02001554 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 +02001555
David Sedlákda8ffa32019-07-08 14:17:10 +02001556 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +01001557}
1558
David Sedlák11900c82019-06-18 16:29:12 +02001559LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +02001560yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1561 struct lysp_ext_instance **exts)
1562{
1563 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001564 struct yin_subelement subelems[1] = {
1565 {YANG_CUSTOM, NULL, 0}
1566 };
David Sedlák1fdb2522019-07-09 16:22:57 +02001567
David Sedlák1f90d252019-07-10 17:09:32 +02001568 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 +02001569 if (strcmp(temp_val, "true") == 0) {
1570 *flags |= LYS_MAND_TRUE;
1571 } else if (strcmp(temp_val, "false") == 0) {
1572 *flags |= LYS_MAND_FALSE;
1573 } else {
1574 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
1575 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1576 return LY_EVALID;
1577 }
1578 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1579
1580 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1581}
1582
1583LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001584yin_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 +02001585 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +02001586{
David Sedlák3ffbc522019-07-02 17:49:28 +02001587 const char *value = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001588 struct yin_subelement subelems[1] = {
1589 {YANG_CUSTOM, NULL, 0}
1590 };
David Sedlák3ffbc522019-07-02 17:49:28 +02001591
David Sedlák292763b2019-07-09 11:10:53 +02001592 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 +02001593 if (strcmp(value, "current") == 0) {
1594 *flags |= LYS_STATUS_CURR;
1595 } else if (strcmp(value, "deprecated") == 0) {
1596 *flags |= LYS_STATUS_DEPRC;
1597 } else if (strcmp(value, "obsolete") == 0) {
1598 *flags |= LYS_STATUS_OBSLT;
1599 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001600 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1601 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001602 return LY_EVALID;
1603 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001604 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001605
David Sedlákda8ffa32019-07-08 14:17:10 +02001606 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001607}
1608
David Sedlák11900c82019-06-18 16:29:12 +02001609LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001610yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1611{
1612 struct lysp_when *when;
1613 when = calloc(1, sizeof *when);
1614 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1f90d252019-07-10 17:09:32 +02001615 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
David Sedlák32eee7b2019-07-09 12:38:44 +02001616 *when_p = when;
David Sedlák968ac342019-07-11 15:17:59 +02001617 struct yin_subelement subelems[3] = {
1618 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1619 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1620 {YANG_CUSTOM, NULL, 0}
1621 };
David Sedlák32eee7b2019-07-09 12:38:44 +02001622
1623 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1624}
1625
1626LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001627yin_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 +02001628 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001629{
David Sedlák3ffbc522019-07-02 17:49:28 +02001630 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001631 struct yin_subelement subelems[1] = {
1632 {YANG_CUSTOM, NULL, 0}
1633 };
David Sedlák2721d3d2019-06-21 15:37:41 +02001634
David Sedlák1f90d252019-07-10 17:09:32 +02001635 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 +02001636 if (strcmp(temp_val, "true") == 0) {
1637 *flags |= LYS_YINELEM_TRUE;
1638 } else if (strcmp(temp_val, "false") == 0) {
1639 *flags |= LYS_YINELEM_FALSE;
1640 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001641 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1642 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001643 return LY_EVALID;
1644 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001645 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001646
David Sedlákda8ffa32019-07-08 14:17:10 +02001647 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001648}
1649
1650LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001651yin_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 +02001652 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001653{
1654 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001655 char *out;
1656 const char *name, *prefix;
1657 size_t out_len, prefix_len, name_len;
1658 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001659 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001660 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1661 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001662
David Sedlákda8ffa32019-07-08 14:17:10 +02001663 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001664
1665 e->yin = 0;
1666 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001667 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001668 e->insubstmt = subelem;
1669 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001670 e->yin |= LYS_YIN;
1671
David Sedlákb1a78352019-06-28 16:16:29 +02001672 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02001673 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02001674 if (!iter->prefix) {
1675 new_subelem = calloc(1, sizeof(*new_subelem));
1676 if (!e->child) {
1677 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001678 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001679 last_subelem->next = new_subelem;
1680 }
1681 last_subelem = new_subelem;
1682
1683 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001684 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1685 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001686 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001687 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1688 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001689 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001690 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1691 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001692 }
1693 }
1694 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001695
David Sedlákf250ecf2019-07-01 11:02:05 +02001696 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001697 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1698 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001699 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001700 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1701 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001702 if (!name) {
1703 /* end of extension instance reached */
1704 break;
1705 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001706 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 +02001707 if (!e->child) {
1708 e->child = new_subelem;
1709 } else {
1710 last_subelem->next = new_subelem;
1711 }
1712 last_subelem = new_subelem;
1713 }
David Sedlák555c7202019-07-04 12:14:12 +02001714 } else {
1715 /* save text content */
1716 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001717 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001718 if (!e->argument) {
1719 free(out);
1720 return LY_EMEM;
1721 }
1722 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001723 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001724 LY_CHECK_RET(!e->argument, LY_EMEM);
1725 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001726 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001727 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001728 }
David Sedlákb1a78352019-06-28 16:16:29 +02001729 }
1730
1731 return LY_SUCCESS;
1732}
1733
1734LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001735yin_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 +02001736 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001737{
1738 LY_ERR ret = LY_SUCCESS;
1739 const char *temp_prefix, *temp_name;
1740 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001741 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001742 int dynamic;
1743 struct yin_arg_record *subelem_args = NULL;
1744 struct lysp_stmt *last = NULL, *new = NULL;
1745
1746 /* allocate new structure for element */
1747 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001748 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1749 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001750
1751 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001752 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001753 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001754 /* add new element to linked-list */
1755 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001756 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001757 if (!(*element)->child) {
1758 /* save first */
1759 (*element)->child = new;
1760 } else {
1761 last->next = new;
1762 }
1763 last = new;
1764
1765 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001766 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 +02001767 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001768 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001769 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001770 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1771 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001772 /* attributes with prefix are ignored */
1773 if (!temp_prefix) {
1774 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001775 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001776 if (!last->arg) {
1777 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001778 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001779 ret = LY_EMEM;
1780 goto err;
1781 }
1782 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001783 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1784 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001785 }
1786 }
1787 }
1788
1789 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001790 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001791 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001792 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001793 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001794 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 +02001795 LY_CHECK_GOTO(ret, err);
1796 if (!name) {
1797 /* end of element reached */
1798 break;
1799 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001800 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 +02001801 LY_CHECK_GOTO(ret, err);
1802 last = last->next;
1803 }
1804 } else {
1805 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001806 if (out_len != 0) {
1807 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001808 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001809 if (!(*element)->arg) {
1810 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001811 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001812 ret = LY_EMEM;
1813 goto err;
1814 }
1815 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001816 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1817 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001818 }
David Sedlákb1a78352019-06-28 16:16:29 +02001819 }
1820 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001821 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 +02001822 LY_CHECK_GOTO(ret, err);
1823 }
1824
David Sedlákda8ffa32019-07-08 14:17:10 +02001825 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001826 return LY_SUCCESS;
1827
1828err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001829 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001830 return ret;
1831}
1832
1833LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001834yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001835 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001836{
David Sedlák968ac342019-07-11 15:17:59 +02001837 struct yin_subelement subelems[2] = {
1838 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1839 {YANG_CUSTOM, NULL, 0}
1840 };
David Sedlák9494eb22019-06-21 16:06:53 +02001841
David Sedlák292763b2019-07-09 11:10:53 +02001842 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 +02001843
David Sedlákda8ffa32019-07-08 14:17:10 +02001844 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001845}
1846
1847LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001848yin_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 +02001849{
David Sedlák11900c82019-06-18 16:29:12 +02001850 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001851 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001852 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 +02001853
David Sedlák3ffbc522019-07-02 17:49:28 +02001854 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
David Sedlák968ac342019-07-11 15:17:59 +02001855 struct yin_subelement subelems[5] = {
1856 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1857 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1858 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1859 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1860 {YANG_CUSTOM, NULL, 0}
1861 };
David Sedlák11900c82019-06-18 16:29:12 +02001862
David Sedlákda8ffa32019-07-08 14:17:10 +02001863 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001864}
1865
David Sedlák00250342019-06-21 14:19:39 +02001866/**
1867 * @brief Parse module substatements.
1868 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001869 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001870 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001871 * @param[in,out] data Data to read from.
1872 * @param[out] mod Parsed module structure.
1873 *
1874 * @return LY_ERR values.
1875 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001876static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001877yin_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 +02001878{
David Sedlák968ac342019-07-11 15:17:59 +02001879 struct yin_subelement subelems[9] = {
1880 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
1881 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1882 {YANG_EXTENSION, &(*mod)->exts, 0},
1883 {YANG_IMPORT, *mod, 0},
1884 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1885 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
1886 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1887 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
1888 {YANG_CUSTOM, NULL, 0}
1889 };
David Sedlák3b4db242018-10-19 16:11:01 +02001890
David Sedlák292763b2019-07-09 11:10:53 +02001891 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 +01001892
David Sedlákda8ffa32019-07-08 14:17:10 +02001893 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001894}
1895
1896LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001897yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001898{
David Sedláke4889912018-11-02 09:52:40 +01001899 LY_ERR ret = LY_SUCCESS;
1900 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001901 struct lysp_module *mod_p = NULL;
1902 const char *prefix, *name;
1903 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001904
David Sedlák1f90d252019-07-10 17:09:32 +02001905 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001906
David Sedlákda8ffa32019-07-08 14:17:10 +02001907 struct yin_parser_ctx yin_ctx;
1908
1909 /* initialize context */
1910 memset(&yin_ctx, 0, sizeof yin_ctx);
1911 yin_ctx.xml_ctx.ctx = ctx;
1912 yin_ctx.xml_ctx.line = 1;
1913
David Sedláke4889912018-11-02 09:52:40 +01001914
David Sedlák3017da42019-02-15 09:48:04 +01001915 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001916 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001917 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1f90d252019-07-10 17:09:32 +02001918 ret = yin_load_attributes(&yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02001919 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001920 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001921 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001922 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1923 ret = LY_EINVAL;
1924 goto cleanup;
1925 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001926 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 +02001927 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001928 ret = LY_EVALID;
1929 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001930 }
1931
David Sedlák3017da42019-02-15 09:48:04 +01001932 /* allocate module */
1933 mod_p = calloc(1, sizeof *mod_p);
1934 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1935 mod_p->mod = mod;
1936 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001937
David Sedlák00250342019-06-21 14:19:39 +02001938 /* parse module substatements */
David Sedlák1f90d252019-07-10 17:09:32 +02001939 ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001940 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001941
David Sedlák3017da42019-02-15 09:48:04 +01001942 mod_p->parsing = 0;
1943 mod->parsed = mod_p;
1944
1945cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001946 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001947 lysp_module_free(mod_p);
1948 }
David Sedlák1f90d252019-07-10 17:09:32 +02001949 FREE_ARRAY(&yin_ctx, attrs, free_arg_rec);
David Sedlákda8ffa32019-07-08 14:17:10 +02001950 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001951 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001952}