blob: c01528eb1455a946c503bd57e1c824ed609b040d [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ákf824ad52018-10-14 23:58:15 +020022
David Sedlákf824ad52018-10-14 23:58:15 +020023#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020025#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020026#include "tree.h"
27#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020028#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020030
David Sedlák2b214ac2019-06-06 16:11:03 +020031/**
32 * @brief check if given string is URI of yin namespace.
33 * @param ns Namespace URI to check.
34 *
35 * @return true if ns equals YIN_NS_URI false otherwise.
36 */
37#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
38
David Sedláke1a30302019-07-10 13:49:38 +020039static LY_ERR
40yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
41 struct lysp_ext_instance **exts);
42
David Sedlákf6251182019-06-06 10:22:13 +020043const char *const yin_attr_list[] = {
44 [YIN_ARG_NAME] = "name",
45 [YIN_ARG_TARGET_NODE] = "target-node",
46 [YIN_ARG_MODULE] = "module",
47 [YIN_ARG_VALUE] = "value",
48 [YIN_ARG_TEXT] = "text",
49 [YIN_ARG_CONDITION] = "condition",
50 [YIN_ARG_URI] = "uri",
51 [YIN_ARG_DATE] = "date",
52 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020053};
54
David Sedlák1bccdfa2019-06-17 15:55:27 +020055enum yang_keyword
David Sedlákc1771b12019-07-10 15:55:46 +020056yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
57 const char *prefix, size_t prefix_len, enum yang_keyword parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020058{
David Sedlák8f7a1172019-06-20 14:42:18 +020059 const char *start = NULL;
60 enum yang_keyword kw = YANG_NONE;
61 const struct lyxml_ns *ns = NULL;
62
63 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020064 return YANG_NONE;
65 }
66
David Sedlákda8ffa32019-07-08 14:17:10 +020067 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020068 if (ns) {
69 if (!IS_YIN_NS(ns->uri)) {
70 return YANG_CUSTOM;
71 }
72 } else {
73 /* elements without namespace are automatically unknown */
74 return YANG_NONE;
75 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020076
David Sedlák8f7a1172019-06-20 14:42:18 +020077 start = name;
78 kw = lysp_match_kw(NULL, &name);
79
80 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020081 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
82 if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) {
83 return YIN_VALUE;
84 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020085 return kw;
86 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020087 if (strncmp(start, "text", name_len) == 0) {
88 return YIN_TEXT;
89 } else if (strncmp(start, "value", name_len) == 0) {
90 return YIN_VALUE;
91 } else {
92 return YANG_NONE;
93 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020094 }
95}
96
David Sedlák872c7b42018-10-26 13:15:20 +020097enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020098yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020099{
David Sedláka7406952019-04-05 10:33:07 +0200100 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200101 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200102 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200103
David Sedlák94de2aa2019-02-15 12:42:11 +0100104#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
105#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100106#define IF_ARG_PREFIX_END }
107
David Sedlák1c8b2702019-02-22 11:03:02 +0100108 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100109 case 'c':
110 already_read += 1;
111 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200112 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200113
David Sedlák94de2aa2019-02-15 12:42:11 +0100114 case 'd':
115 already_read += 1;
116 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200117 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200118
David Sedlák94de2aa2019-02-15 12:42:11 +0100119 case 'm':
120 already_read += 1;
121 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200122 break;
123
David Sedlák94de2aa2019-02-15 12:42:11 +0100124 case 'n':
125 already_read += 1;
126 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200127 break;
128
David Sedlák94de2aa2019-02-15 12:42:11 +0100129 case 't':
130 already_read += 1;
131 IF_ARG_PREFIX("a", 1)
132 IF_ARG("g", 1, YIN_ARG_TAG)
133 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
134 IF_ARG_PREFIX_END
135 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200136 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200137
David Sedlák94de2aa2019-02-15 12:42:11 +0100138 case 'u':
139 already_read += 1;
140 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200141 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200142
David Sedlák94de2aa2019-02-15 12:42:11 +0100143 case 'v':
144 already_read += 1;
145 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200146 break;
147 }
148
David Sedlákc10e7902018-12-17 02:17:59 +0100149 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200150 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200151 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200152 }
153
David Sedlák18730132019-03-15 15:51:34 +0100154#undef IF_ARG
155#undef IF_ARG_PREFIX
156#undef IF_ARG_PREFIX_END
157
David Sedlák872c7b42018-10-26 13:15:20 +0200158 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200159}
160
David Sedlákb4e44562019-07-04 15:42:12 +0200161/**
162 * @brief free argument record, content loaded from lyxml_get_string() can be
163 * dynamically allocated in some cases so it must be also freed.
164 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200165static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
166 (void)ctx; /* unused */
David Sedlák00250342019-06-21 14:19:39 +0200167 if (record->dynamic_content) {
168 free(record->content);
169 }
170}
171
David Sedlák8f7a1172019-06-20 14:42:18 +0200172LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200173yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200174{
175 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200176 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200177 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200178
David Sedlák555c7202019-07-04 12:14:12 +0200179 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200180 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
181 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200182 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200183
David Sedlákda8ffa32019-07-08 14:17:10 +0200184 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
185 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200186 argument_record->name = name.value;
187 argument_record->name_len = name.len;
188 argument_record->prefix = prefix.value;
189 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200190 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200191 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200192 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200193 }
194 }
195
David Sedlák8f7a1172019-06-20 14:42:18 +0200196cleanup:
197 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200198 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200199 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200200 }
201 return ret;
202}
203
David Sedlák4a650532019-07-10 11:55:18 +0200204LY_ERR
205yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
206{
207 int prefix = 0;
208 unsigned int c;
209 size_t utf8_char_len;
210 size_t already_read = 0;
211 while (already_read < len) {
212 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
213 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
214 already_read += utf8_char_len;
215 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
216
217 switch (val_type) {
218 case Y_IDENTIF_ARG:
219 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
220 break;
221 case Y_PREF_IDENTIF_ARG:
222 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
223 break;
224 case Y_STR_ARG:
225 case Y_MAYBE_STR_ARG:
226 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
227 break;
228 }
229 }
230
231 return LY_SUCCESS;
232}
233
David Sedlákb4e44562019-07-04 15:42:12 +0200234/**
235 * @brief Parse yin argument.
236 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200237 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200238 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200239 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200240 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
241 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200242 * @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 +0200243 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200244 * @param[in] current_element Identification of current element, used for logging.
245 *
246 * @return LY_ERR values.
247 */
248static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200249yin_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 +0200250 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200251{
David Sedlák8f7a1172019-06-20 14:42:18 +0200252 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
253 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200254 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200255
David Sedlák1bccdfa2019-06-17 15:55:27 +0200256 /* validation of attributes */
David Sedlákb4e44562019-07-04 15:42:12 +0200257 LY_ARRAY_FOR(*attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200258 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
259 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200260 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200261 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200262 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200263 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200264 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
265 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200266 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200267 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200268 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200269 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200270 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200271 /* string is no longer supposed to be freed when the sized array is freed */
272 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200273 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200274 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200275 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200276 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200277 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200278 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 +0200279 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200280 }
281 }
282 }
283
David Sedlák292763b2019-07-09 11:10:53 +0200284 /* anything else than Y_MAYBE_STR_ARG is mandatory */
285 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200286 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 +0200287 return LY_EVALID;
288 }
289
290 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200291}
292
David Sedlákd6e56892019-07-01 15:40:24 +0200293/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200294 * @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 +0200295 *
296 * @param[in] type Type of wanted record.
297 * @param[in] array_size Size of array.
298 * @param[in] array Searched array.
299 *
300 * @return Pointer to desired record on success, NULL if element is not in the array.
301 */
David Sedlákb4e44562019-07-04 15:42:12 +0200302static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200303get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200304{
David Sedlákb0faad82019-07-04 14:28:59 +0200305 signed char left = 0, right = array_size - 1, middle;
306
307 while (left <= right) {
308 middle = left + (right - left) / 2;
309
310 if (array[middle].type == type) {
311 return &array[middle];
312 }
313
314 if (array[middle].type < type) {
315 left = middle + 1;
316 } else {
317 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200318 }
319 }
320
321 return NULL;
322}
323
David Sedlákbba38e52019-07-09 15:20:01 +0200324/**
325 * @brief Helper function to check mandatory constraint of subelement.
326 *
327 * @param[in,out] ctx Yin parser context for logging and to store current state.
328 * @param[in] subelem_info Array of information about subelements.
329 * @param[in] subelem_info_size Size of subelem_info array.
330 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
331 *
332 * @return LY_ERR values.
333 */
334static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200335yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200336 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200337{
David Sedlákb0faad82019-07-04 14:28:59 +0200338 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200339 /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */
340 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200341 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 +0200342 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200343 return LY_EVALID;
344 }
345 }
346
347 return LY_SUCCESS;
348}
349
David Sedlákbba38e52019-07-09 15:20:01 +0200350/**
351 * @brief Helper function to check "first" constraint of subelement.
352 *
353 * @param[in,out] ctx Yin parser context for logging and to store current state.
354 * @param[in] subelem_info Array of information about subelements.
355 * @param[in] subelem_info_size Size of subelem_info array.
356 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
357 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
358 *
359 * @return LY_ERR values.
360 */
361static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200362yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200363 signed char subelem_info_size, enum yang_keyword current_element,
364 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200365{
David Sedlákb0faad82019-07-04 14:28:59 +0200366 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200367 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200368 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 +0200369 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200370 return LY_EVALID;
371 }
372 }
373
374 return LY_SUCCESS;
375}
376
David Sedlákb0faad82019-07-04 14:28:59 +0200377/* TODO add something like ifdef NDEBUG around this function, this is supposed to be checked only in debug mode */
David Sedlákbba38e52019-07-09 15:20:01 +0200378/**
379 * @brief Helper function to check if array of information about subelements is in ascending order.
380 *
381 * @param[in] subelem_info Array of information about subelements.
382 * @param[in] subelem_info_size Size of subelem_info array.
383 *
384 * @return True iff subelem_info array is in ascending order, False otherwise.
385 */
386static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200387is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
388{
David Sedlák292763b2019-07-09 11:10:53 +0200389 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200390
391 for (signed char i = 0; i < subelem_info_size; ++i) {
392 if (subelem_info[i].type <= current) {
393 return false;
394 }
395 current = subelem_info[i].type;
396 }
397
398 return true;
399}
400
David Sedlákd6e56892019-07-01 15:40:24 +0200401/**
David Sedlákb4e44562019-07-04 15:42:12 +0200402 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
403 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200404 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200405 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200406 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200407 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200408 * @param[in] kw Type of current element.
409 * @param[out] value Where value of attribute should be stored.
410 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200411 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200412 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200413 *
David Sedlákd6e56892019-07-01 15:40:24 +0200414 * @return LY_ERR values.
415 */
David Sedlákb4e44562019-07-04 15:42:12 +0200416static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200417yin_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 +0200418 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 +0200419{
David Sedlák292763b2019-07-09 11:10:53 +0200420 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlákb4e44562019-07-04 15:42:12 +0200421 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
422
David Sedlákda8ffa32019-07-08 14:17:10 +0200423 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200424}
425
426/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200427 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
428 * more instances, such as base or if-feature.
429 *
430 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200431 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200432 * @param[in,out] data Data to read from, always moved to currently handled character.
433 * @param[in] kw Type of current element.
434 * @param[out] values Parsed values to add to.
435 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200436 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200437 * @param[in,out] exts Extension instance to add to.
438 *
439 * @return LY_ERR values.
440 */
441static LY_ERR
442yin_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 +0200443 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 +0200444{
445 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200446 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200447 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
448 struct yin_subelement subelems[1] = {{YANG_CUSTOM, &index, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200449 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200450
451 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
452}
453
454/**
David Sedlákc1771b12019-07-10 15:55:46 +0200455 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200456 * text element as child
457 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200458 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200459 * @param[in,out] data Data to read from.
David Sedlákc1771b12019-07-10 15:55:46 +0200460 * @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 +0200461 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200462 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200463 *
464 * @return LY_ERR values.
465 */
466static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200467yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200468 const char **value, struct lysp_ext_instance **exts)
469{
470 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
471
472 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
473 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
474
David Sedlákda8ffa32019-07-08 14:17:10 +0200475 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200476}
477
478/**
David Sedlákc1771b12019-07-10 15:55:46 +0200479 * @brief Parse error-message element.
480 *
481 * @param[in,out] ctx Yin parser context for logging and to store current state.
482 * @param[in,out] data Data to read from.
483 * @param[out] value Where the content of error-message element should be stored.
484 * @param[in,out] exts Extension instance to add to.
485 *
486 * @return LY_ERR values.
487 */
488static LY_ERR
489yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
490 struct lysp_ext_instance **exts)
491{
492 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
493 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
494
495 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
496}
497
498/**
David Sedlákb4e44562019-07-04 15:42:12 +0200499 * @brief Map keyword type to substatement info.
500 *
501 * @param[in] kw Keyword type.
502 *
503 * @return correct LYEXT_SUBSTMT information.
504 */
505static LYEXT_SUBSTMT
506kw2lyext_substmt(enum yang_keyword kw)
507{
508 switch (kw) {
509 case YANG_ARGUMENT:
510 return LYEXT_SUBSTMT_ARGUMENT;
511 case YANG_BASE:
512 return LYEXT_SUBSTMT_BASE;
513 case YANG_BELONGS_TO:
514 return LYEXT_SUBSTMT_BELONGSTO;
515 case YANG_CONTACT:
516 return LYEXT_SUBSTMT_CONTACT;
517 case YANG_DEFAULT:
518 return LYEXT_SUBSTMT_DEFAULT;
519 case YANG_DESCRIPTION:
520 return LYEXT_SUBSTMT_DESCRIPTION;
521 case YANG_ERROR_APP_TAG:
522 return LYEXT_SUBSTMT_ERRTAG;
523 case YANG_ERROR_MESSAGE:
524 return LYEXT_SUBSTMT_ERRMSG;
525 case YANG_KEY:
526 return LYEXT_SUBSTMT_KEY;
527 case YANG_NAMESPACE:
528 return LYEXT_SUBSTMT_NAMESPACE;
529 case YANG_ORGANIZATION:
530 return LYEXT_SUBSTMT_ORGANIZATION;
531 case YANG_PATH:
532 return LYEXT_SUBSTMT_PATH;
533 case YANG_PREFIX:
534 return LYEXT_SUBSTMT_PREFIX;
535 case YANG_PRESENCE:
536 return LYEXT_SUBSTMT_PRESENCE;
537 case YANG_REFERENCE:
538 return LYEXT_SUBSTMT_REFERENCE;
539 case YANG_REVISION_DATE:
540 return LYEXT_SUBSTMT_REVISIONDATE;
541 case YANG_UNITS:
542 return LYEXT_SUBSTMT_UNITS;
543 case YANG_VALUE:
544 return LYEXT_SUBSTMT_VALUE;
545 case YANG_YANG_VERSION:
546 return LYEXT_SUBSTMT_VERSION;
547 case YANG_MODIFIER:
548 return LYEXT_SUBSTMT_MODIFIER;
549 case YANG_REQUIRE_INSTANCE:
550 return LYEXT_SUBSTMT_REQINSTANCE;
551 case YANG_YIN_ELEMENT:
552 return LYEXT_SUBSTMT_YINELEM;
553 case YANG_CONFIG:
554 return LYEXT_SUBSTMT_CONFIG;
555 case YANG_MANDATORY:
556 return LYEXT_SUBSTMT_MANDATORY;
557 case YANG_ORDERED_BY:
558 return LYEXT_SUBSTMT_ORDEREDBY;
559 case YANG_STATUS:
560 return LYEXT_SUBSTMT_STATUS;
561 case YANG_FRACTION_DIGITS:
562 return LYEXT_SUBSTMT_FRACDIGITS;
563 case YANG_MAX_ELEMENTS:
564 return LYEXT_SUBSTMT_MAX;
565 case YANG_MIN_ELEMENTS:
566 return LYEXT_SUBSTMT_MIN;
567 case YANG_POSITION:
568 return LYEXT_SUBSTMT_POSITION;
569 case YANG_UNIQUE:
570 return LYEXT_SUBSTMT_UNIQUE;
571 case YANG_IF_FEATURE:
572 return LYEXT_SUBSTMT_IFFEATURE;
573 default:
574 return LYEXT_SUBSTMT_SELF;
575 }
576}
577
David Sedlák9c40a922019-07-08 17:04:43 +0200578
579/**
580 * @brief Parse belongs-to element.
581 *
582 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200583 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200584 * @param[in,out] data Data to read from, always moved to currently handled character.
585 * @param[out] submod Structure of submodule that is being parsed.
586 * @param[in,out] exts Extension instances to add to.
587 *
588 * @return LY_ERR values
589 */
590static LY_ERR
591yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
592 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
593{
594 struct yin_subelement subelems[2] = {{YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
595 {YANG_CUSTOM, NULL, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200596 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 +0200597
598 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
599}
600
David Sedlákd6e56892019-07-01 15:40:24 +0200601LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200602yin_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 +0200603 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 +0200604{
605 LY_ERR ret = LY_SUCCESS;
606 struct sized_string prefix, name;
607 char *out;
608 size_t out_len;
609 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200610 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200611 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200612 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200613 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200614 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200615 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200616
David Sedlákda8ffa32019-07-08 14:17:10 +0200617 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
618 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200619 /* current element has subelements as content */
620 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200621 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
622 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200623 LY_CHECK_GOTO(ret, cleanup);
624 if (!name.value) {
625 /* end of current element reached */
626 break;
627 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200628 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200629 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +0200630 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +0200631
632 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200633 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
634 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200635 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 +0200636 ret = LY_EVALID;
637 goto cleanup;
638 }
639
David Sedlák5f8191e2019-07-08 16:35:52 +0200640 /* TODO check relative order */
641
David Sedlák21f87cd2019-07-03 16:53:23 +0200642 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200643 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200644 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 +0200645 return LY_EVALID;
646 }
647 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200648 LY_CHECK_RET(yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec));
David Sedlák21f87cd2019-07-03 16:53:23 +0200649 }
650 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
651
David Sedlákd6e56892019-07-01 15:40:24 +0200652 switch (kw) {
653 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200654 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlákda8ffa32019-07-08 14:17:10 +0200655 ret = yin_parse_extension_instance(ctx, &subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200656 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200657 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200658 break;
659 case YANG_ACTION:
660 break;
661 case YANG_ANYDATA:
662 break;
663 case YANG_ANYXML:
664 break;
665 case YANG_ARGUMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200666 ret = yin_parse_argument_element(ctx, &subelem_attrs, data, (struct yin_argument_meta *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200667 break;
668 case YANG_AUGMENT:
669 break;
670 case YANG_BASE:
David Sedlák292763b2019-07-09 11:10:53 +0200671 if (current_element == YANG_IDENTITY) {
672 type = (struct lysp_type *)subelem_info_rec->dest;
673 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
674 Y_PREF_IDENTIF_ARG, exts);
675 type->flags |= LYS_SET_BASE;
676 } else if (current_element == YANG_TYPE) {
677 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
678 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
679 } else {
680 LOGINT(ctx->xml_ctx.ctx);
681 ret = LY_EINT;
682 }
David Sedlákd6e56892019-07-01 15:40:24 +0200683 break;
684 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +0200685 ret = yin_parse_belongs_to(ctx, subelem_attrs, data, (struct lysp_submodule *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200686 break;
687 case YANG_BIT:
688 break;
689 case YANG_CASE:
690 break;
691 case YANG_CHOICE:
692 break;
693 case YANG_CONFIG:
David Sedláke1a30302019-07-10 13:49:38 +0200694 ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200695 break;
696 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200697 case YANG_DESCRIPTION:
698 case YANG_ORGANIZATION:
699 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200700 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200701 break;
702 case YANG_CONTAINER:
703 break;
704 case YANG_DEFAULT:
705 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200706 case YANG_DEVIATE:
707 break;
708 case YANG_DEVIATION:
709 break;
710 case YANG_ENUM:
711 break;
712 case YANG_ERROR_APP_TAG:
David Sedlák2ce1be62019-07-10 16:15:09 +0200713 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
714 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200715 break;
716 case YANG_ERROR_MESSAGE:
David Sedlákc1771b12019-07-10 15:55:46 +0200717 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200718 break;
719 case YANG_EXTENSION:
David Sedlákda8ffa32019-07-08 14:17:10 +0200720 ret = yin_parse_extension(ctx, &subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200721 break;
722 case YANG_FEATURE:
723 break;
724 case YANG_FRACTION_DIGITS:
725 break;
726 case YANG_GROUPING:
727 break;
728 case YANG_IDENTITY:
729 break;
730 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200731 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200732 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200733 break;
734 case YANG_IMPORT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200735 ret = yin_parse_import(ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200736 break;
737 case YANG_INCLUDE:
738 break;
739 case YANG_INPUT:
740 break;
741 case YANG_KEY:
742 break;
743 case YANG_LEAF:
744 break;
745 case YANG_LEAF_LIST:
746 break;
747 case YANG_LENGTH:
748 break;
749 case YANG_LIST:
750 break;
751 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +0200752 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200753 break;
754 case YANG_MAX_ELEMENTS:
755 break;
756 case YANG_MIN_ELEMENTS:
757 break;
758 case YANG_MODIFIER:
759 break;
760 case YANG_MODULE:
761 break;
762 case YANG_MUST:
763 break;
764 case YANG_NAMESPACE:
David Sedlák2ce1be62019-07-10 16:15:09 +0200765 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
766 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200767 break;
768 case YANG_NOTIFICATION:
769 break;
770 case YANG_ORDERED_BY:
771 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200772 case YANG_OUTPUT:
773 break;
774 case YANG_PATH:
775 break;
776 case YANG_PATTERN:
777 break;
778 case YANG_POSITION:
779 break;
780 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +0200781 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200782 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200783 break;
784 case YANG_PRESENCE:
785 break;
786 case YANG_RANGE:
787 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200788 case YANG_REFINE:
789 break;
790 case YANG_REQUIRE_INSTANCE:
791 break;
792 case YANG_REVISION:
793 break;
794 case YANG_REVISION_DATE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200795 ret = yin_parse_revision_date(ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200796 break;
797 case YANG_RPC:
798 break;
799 case YANG_STATUS:
David Sedlákda8ffa32019-07-08 14:17:10 +0200800 ret = yin_parse_status(ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200801 break;
802 case YANG_SUBMODULE:
803 break;
804 case YANG_TYPE:
805 break;
806 case YANG_TYPEDEF:
807 break;
808 case YANG_UNIQUE:
David Sedláka5b1d382019-07-10 16:31:09 +0200809 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
810 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200811 break;
812 case YANG_UNITS:
David Sedláka5b1d382019-07-10 16:31:09 +0200813 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
814 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200815 break;
816 case YANG_USES:
817 break;
818 case YANG_VALUE:
819 break;
820 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200821 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200822 break;
823 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +0200824 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200825 break;
826 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200827 ret = yin_parse_yin_element_element(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200828 break;
829 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +0200830 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +0200831 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +0200832 break;
833 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200834 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200835 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200836 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200837 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200838 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200839 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200840 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200841 }
842 } else {
843 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200844 /* save text content, if text_content isn't set, it's just ignored */
845 if (text_content) {
846 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200847 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200848 if (!*text_content) {
849 free(out);
850 return LY_EMEM;
851 }
852 } else {
853 if (out_len == 0) {
854 *text_content = NULL;
855 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200856 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200857 }
858 }
859 }
David Sedlákd6e56892019-07-01 15:40:24 +0200860 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +0200861 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 +0200862 }
David Sedlák555c7202019-07-04 12:14:12 +0200863
David Sedlákda8ffa32019-07-08 14:17:10 +0200864 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200865 }
866
867cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +0200868 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200869 return ret;
870}
871
David Sedlák619db942019-07-03 14:47:30 +0200872LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200873yin_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 +0200874 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200875{
David Sedlák3ffbc522019-07-02 17:49:28 +0200876 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200877 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200878
David Sedlák292763b2019-07-09 11:10:53 +0200879 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
David Sedlákda8ffa32019-07-08 14:17:10 +0200880 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
David Sedlákda63c082019-06-04 13:52:23 +0200881
882 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +0200883 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200884
David Sedlákda8ffa32019-07-08 14:17:10 +0200885 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200886}
David Sedlák00250342019-06-21 14:19:39 +0200887
David Sedláke1a30302019-07-10 13:49:38 +0200888/**
889 * @brief Parse config element.
890 *
891 * @param[in] ctx Yin parser context for logging and to store current state.
892 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
893 * @param[in,out] data Data to read from, always moved to currently handled character.
894 * @param[in,out] flags Flags to add to.
895 * @param[in,out] exts Extension instances to add to.
896 *
897 * @return LY_ERR values.
898 */
899static LY_ERR
900yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
901 struct lysp_ext_instance **exts)
902{
903 const char *temp_val = NULL;
904 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
905
906 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
907 if (strcmp(temp_val, "true") == 0) {
908 *flags |= LYS_CONFIG_W;
909 } else if (strcmp(temp_val, "false") == 0) {
910 *flags |= LYS_CONFIG_R;
911 } else {
912 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
913 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
914 return LY_EVALID;
915 }
916 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
917
918 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
919}
920
David Sedlák3ffbc522019-07-02 17:49:28 +0200921LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +0200922yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
923 struct lysp_ext_instance **exts)
924{
925 const char *temp_version = NULL;
926 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
927
928 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
929 if (strcmp(temp_version, "1.0") == 0) {
930 *version = LYS_VERSION_1_0;
931 } else if (strcmp(temp_version, "1.1") == 0) {
932 *version = LYS_VERSION_1_1;
933 } else {
934 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
935 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
936 return LY_EVALID;
937 }
938 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
939 ctx->mod_version = *version;
940
941 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
942}
943
944LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200945yin_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 +0100946{
David Sedlák736fd0d2019-02-15 16:06:31 +0100947 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200948 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +0200949 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200950
David Sedlákb0faad82019-07-04 14:28:59 +0200951 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
952 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +0200953 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
954 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200955 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100956
David Sedlák92147b02019-07-09 14:01:01 +0200957 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +0200958 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 +0200959 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200960 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +0200961 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 +0200962
David Sedlákda8ffa32019-07-08 14:17:10 +0200963 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100964}
965
David Sedlák11900c82019-06-18 16:29:12 +0200966LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +0200967yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
968 struct lysp_ext_instance **exts)
969{
970 const char *temp_val = NULL;
971 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
972
973 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
974 if (strcmp(temp_val, "true") == 0) {
975 *flags |= LYS_MAND_TRUE;
976 } else if (strcmp(temp_val, "false") == 0) {
977 *flags |= LYS_MAND_FALSE;
978 } else {
979 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
980 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
981 return LY_EVALID;
982 }
983 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
984
985 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
986}
987
988LY_ERR
989yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags,
990 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200991{
David Sedlák3ffbc522019-07-02 17:49:28 +0200992 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200993 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200994
David Sedlák292763b2019-07-09 11:10:53 +0200995 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 +0200996 if (strcmp(value, "current") == 0) {
997 *flags |= LYS_STATUS_CURR;
998 } else if (strcmp(value, "deprecated") == 0) {
999 *flags |= LYS_STATUS_DEPRC;
1000 } else if (strcmp(value, "obsolete") == 0) {
1001 *flags |= LYS_STATUS_OBSLT;
1002 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001003 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1004 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001005 return LY_EVALID;
1006 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001007 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001008
David Sedlákda8ffa32019-07-08 14:17:10 +02001009 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001010}
1011
David Sedlák11900c82019-06-18 16:29:12 +02001012LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001013yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1014{
1015 struct lysp_when *when;
1016 when = calloc(1, sizeof *when);
1017 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1018 yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
1019 *when_p = when;
1020 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1021 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1022 {YANG_CUSTOM, NULL, 0}};
1023
1024 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1025}
1026
1027LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001028yin_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 +02001029 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001030{
David Sedlák3ffbc522019-07-02 17:49:28 +02001031 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +02001032 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +02001033
David Sedlák292763b2019-07-09 11:10:53 +02001034 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 +02001035 if (strcmp(temp_val, "true") == 0) {
1036 *flags |= LYS_YINELEM_TRUE;
1037 } else if (strcmp(temp_val, "false") == 0) {
1038 *flags |= LYS_YINELEM_FALSE;
1039 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001040 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1041 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001042 return LY_EVALID;
1043 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001044 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001045
David Sedlákda8ffa32019-07-08 14:17:10 +02001046 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001047}
1048
1049LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001050yin_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 +02001051 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001052{
1053 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001054 char *out;
1055 const char *name, *prefix;
1056 size_t out_len, prefix_len, name_len;
1057 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001058 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001059 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1060 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001061
David Sedlákda8ffa32019-07-08 14:17:10 +02001062 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001063
1064 e->yin = 0;
1065 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001066 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001067 e->insubstmt = subelem;
1068 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001069 e->yin |= LYS_YIN;
1070
David Sedlákb1a78352019-06-28 16:16:29 +02001071 /* store attributes as subelements */
1072 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
1073 if (!iter->prefix) {
1074 new_subelem = calloc(1, sizeof(*new_subelem));
1075 if (!e->child) {
1076 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001077 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001078 last_subelem->next = new_subelem;
1079 }
1080 last_subelem = new_subelem;
1081
1082 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001083 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1084 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001085 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001086 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1087 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001088 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001089 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1090 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001091 }
1092 }
1093 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001094
David Sedlákf250ecf2019-07-01 11:02:05 +02001095 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001096 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1097 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001098 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001099 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1100 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001101 if (!name) {
1102 /* end of extension instance reached */
1103 break;
1104 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001105 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 +02001106 if (!e->child) {
1107 e->child = new_subelem;
1108 } else {
1109 last_subelem->next = new_subelem;
1110 }
1111 last_subelem = new_subelem;
1112 }
David Sedlák555c7202019-07-04 12:14:12 +02001113 } else {
1114 /* save text content */
1115 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001116 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001117 if (!e->argument) {
1118 free(out);
1119 return LY_EMEM;
1120 }
1121 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001122 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001123 LY_CHECK_RET(!e->argument, LY_EMEM);
1124 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001125 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001126 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001127 }
David Sedlákb1a78352019-06-28 16:16:29 +02001128 }
1129
1130 return LY_SUCCESS;
1131}
1132
1133LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001134yin_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 +02001135 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001136{
1137 LY_ERR ret = LY_SUCCESS;
1138 const char *temp_prefix, *temp_name;
1139 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001140 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001141 int dynamic;
1142 struct yin_arg_record *subelem_args = NULL;
1143 struct lysp_stmt *last = NULL, *new = NULL;
1144
1145 /* allocate new structure for element */
1146 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001147 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1148 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001149
1150 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001151 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001152 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001153 /* add new element to linked-list */
1154 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001155 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001156 if (!(*element)->child) {
1157 /* save first */
1158 (*element)->child = new;
1159 } else {
1160 last->next = new;
1161 }
1162 last = new;
1163
1164 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001165 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 +02001166 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001167 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001168 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001169 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1170 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001171 /* attributes with prefix are ignored */
1172 if (!temp_prefix) {
1173 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001174 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001175 if (!last->arg) {
1176 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001177 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001178 ret = LY_EMEM;
1179 goto err;
1180 }
1181 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001182 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1183 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001184 }
1185 }
1186 }
1187
1188 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001189 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001190 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001191 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001192 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001193 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 +02001194 LY_CHECK_GOTO(ret, err);
1195 if (!name) {
1196 /* end of element reached */
1197 break;
1198 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001199 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 +02001200 LY_CHECK_GOTO(ret, err);
1201 last = last->next;
1202 }
1203 } else {
1204 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001205 if (out_len != 0) {
1206 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001207 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001208 if (!(*element)->arg) {
1209 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001210 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001211 ret = LY_EMEM;
1212 goto err;
1213 }
1214 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001215 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1216 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001217 }
David Sedlákb1a78352019-06-28 16:16:29 +02001218 }
1219 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001220 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 +02001221 LY_CHECK_GOTO(ret, err);
1222 }
1223
David Sedlákda8ffa32019-07-08 14:17:10 +02001224 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001225 return LY_SUCCESS;
1226
1227err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001228 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001229 return ret;
1230}
1231
1232LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001233yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001234 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001235{
David Sedlák3ffbc522019-07-02 17:49:28 +02001236 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001237 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001238
David Sedlák292763b2019-07-09 11:10:53 +02001239 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 +02001240
David Sedlákda8ffa32019-07-08 14:17:10 +02001241 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001242}
1243
1244LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001245yin_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 +02001246{
David Sedlák11900c82019-06-18 16:29:12 +02001247 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001248 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001249 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 +02001250
David Sedlák3ffbc522019-07-02 17:49:28 +02001251 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1252 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1253 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1254 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1255 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001256 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001257
David Sedlákda8ffa32019-07-08 14:17:10 +02001258 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001259}
1260
David Sedlák00250342019-06-21 14:19:39 +02001261/**
1262 * @brief Parse module substatements.
1263 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001264 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001265 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001266 * @param[in,out] data Data to read from.
1267 * @param[out] mod Parsed module structure.
1268 *
1269 * @return LY_ERR values.
1270 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001271static LY_ERR
1272yin_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 +02001273{
David Sedlákda8ffa32019-07-08 14:17:10 +02001274 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001275 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1276 {YANG_EXTENSION, &(*mod)->exts, 0},
1277 {YANG_IMPORT, *mod, 0},
1278 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1279 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001280 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1281 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001282 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001283
David Sedlák292763b2019-07-09 11:10:53 +02001284 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 +01001285
David Sedlákda8ffa32019-07-08 14:17:10 +02001286 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001287}
1288
1289LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001290yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001291{
David Sedláke4889912018-11-02 09:52:40 +01001292 LY_ERR ret = LY_SUCCESS;
1293 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001294 struct lysp_module *mod_p = NULL;
1295 const char *prefix, *name;
1296 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001297
David Sedlák8f7a1172019-06-20 14:42:18 +02001298 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001299
David Sedlákda8ffa32019-07-08 14:17:10 +02001300 struct yin_parser_ctx yin_ctx;
1301
1302 /* initialize context */
1303 memset(&yin_ctx, 0, sizeof yin_ctx);
1304 yin_ctx.xml_ctx.ctx = ctx;
1305 yin_ctx.xml_ctx.line = 1;
1306
David Sedláke4889912018-11-02 09:52:40 +01001307
David Sedlák3017da42019-02-15 09:48:04 +01001308 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001309 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001310 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001311 ret = yin_load_attributes(&yin_ctx, &data, &args);
David Sedlák00250342019-06-21 14:19:39 +02001312 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001313 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001314 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001315 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1316 ret = LY_EINVAL;
1317 goto cleanup;
1318 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001319 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 +02001320 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001321 ret = LY_EVALID;
1322 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001323 }
1324
David Sedlák3017da42019-02-15 09:48:04 +01001325 /* allocate module */
1326 mod_p = calloc(1, sizeof *mod_p);
1327 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1328 mod_p->mod = mod;
1329 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001330
David Sedlák00250342019-06-21 14:19:39 +02001331 /* parse module substatements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001332 ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001333 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001334
David Sedlák3017da42019-02-15 09:48:04 +01001335 mod_p->parsing = 0;
1336 mod->parsed = mod_p;
1337
1338cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001339 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001340 lysp_module_free(mod_p);
1341 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001342 FREE_ARRAY(&yin_ctx, args, free_arg_rec);
1343 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001344 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001345}