blob: d5f2577fed8611db0e26b36e36da8dca648840f4 [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:
713 break;
714 case YANG_ERROR_MESSAGE:
David Sedlákc1771b12019-07-10 15:55:46 +0200715 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200716 break;
717 case YANG_EXTENSION:
David Sedlákda8ffa32019-07-08 14:17:10 +0200718 ret = yin_parse_extension(ctx, &subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200719 break;
720 case YANG_FEATURE:
721 break;
722 case YANG_FRACTION_DIGITS:
723 break;
724 case YANG_GROUPING:
725 break;
726 case YANG_IDENTITY:
727 break;
728 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200729 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200730 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200731 break;
732 case YANG_IMPORT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200733 ret = yin_parse_import(ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200734 break;
735 case YANG_INCLUDE:
736 break;
737 case YANG_INPUT:
738 break;
739 case YANG_KEY:
740 break;
741 case YANG_LEAF:
742 break;
743 case YANG_LEAF_LIST:
744 break;
745 case YANG_LENGTH:
746 break;
747 case YANG_LIST:
748 break;
749 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +0200750 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200751 break;
752 case YANG_MAX_ELEMENTS:
753 break;
754 case YANG_MIN_ELEMENTS:
755 break;
756 case YANG_MODIFIER:
757 break;
758 case YANG_MODULE:
759 break;
760 case YANG_MUST:
761 break;
762 case YANG_NAMESPACE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200763 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200764 (const char **)subelem_info_rec->dest, YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200765 break;
766 case YANG_NOTIFICATION:
767 break;
768 case YANG_ORDERED_BY:
769 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200770 case YANG_OUTPUT:
771 break;
772 case YANG_PATH:
773 break;
774 case YANG_PATTERN:
775 break;
776 case YANG_POSITION:
777 break;
778 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +0200779 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200780 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200781 break;
782 case YANG_PRESENCE:
783 break;
784 case YANG_RANGE:
785 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200786 case YANG_REFINE:
787 break;
788 case YANG_REQUIRE_INSTANCE:
789 break;
790 case YANG_REVISION:
791 break;
792 case YANG_REVISION_DATE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200793 ret = yin_parse_revision_date(ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200794 break;
795 case YANG_RPC:
796 break;
797 case YANG_STATUS:
David Sedlákda8ffa32019-07-08 14:17:10 +0200798 ret = yin_parse_status(ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200799 break;
800 case YANG_SUBMODULE:
801 break;
802 case YANG_TYPE:
803 break;
804 case YANG_TYPEDEF:
805 break;
806 case YANG_UNIQUE:
David Sedlákd4909d92019-07-09 16:33:10 +0200807 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
808 (const char ***)subelem_info_rec->dest, YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200809 break;
810 case YANG_UNITS:
811 break;
812 case YANG_USES:
813 break;
814 case YANG_VALUE:
815 break;
816 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200817 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200818 break;
819 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +0200820 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200821 break;
822 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200823 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 +0200824 break;
825 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +0200826 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +0200827 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +0200828 break;
829 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200830 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200831 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200832 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200833 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200834 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200835 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200836 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200837 }
838 } else {
839 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200840 /* save text content, if text_content isn't set, it's just ignored */
841 if (text_content) {
842 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200843 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200844 if (!*text_content) {
845 free(out);
846 return LY_EMEM;
847 }
848 } else {
849 if (out_len == 0) {
850 *text_content = NULL;
851 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200852 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200853 }
854 }
855 }
David Sedlákd6e56892019-07-01 15:40:24 +0200856 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +0200857 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 +0200858 }
David Sedlák555c7202019-07-04 12:14:12 +0200859
David Sedlákda8ffa32019-07-08 14:17:10 +0200860 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200861 }
862
863cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +0200864 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200865 return ret;
866}
867
David Sedlák619db942019-07-03 14:47:30 +0200868LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200869yin_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 +0200870 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200871{
David Sedlák3ffbc522019-07-02 17:49:28 +0200872 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200873 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200874
David Sedlák292763b2019-07-09 11:10:53 +0200875 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 +0200876 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 +0200877
878 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +0200879 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200880
David Sedlákda8ffa32019-07-08 14:17:10 +0200881 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200882}
David Sedlák00250342019-06-21 14:19:39 +0200883
David Sedláke1a30302019-07-10 13:49:38 +0200884/**
885 * @brief Parse config element.
886 *
887 * @param[in] ctx Yin parser context for logging and to store current state.
888 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
889 * @param[in,out] data Data to read from, always moved to currently handled character.
890 * @param[in,out] flags Flags to add to.
891 * @param[in,out] exts Extension instances to add to.
892 *
893 * @return LY_ERR values.
894 */
895static LY_ERR
896yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
897 struct lysp_ext_instance **exts)
898{
899 const char *temp_val = NULL;
900 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
901
902 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
903 if (strcmp(temp_val, "true") == 0) {
904 *flags |= LYS_CONFIG_W;
905 } else if (strcmp(temp_val, "false") == 0) {
906 *flags |= LYS_CONFIG_R;
907 } else {
908 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
909 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
910 return LY_EVALID;
911 }
912 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
913
914 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
915}
916
David Sedlák3ffbc522019-07-02 17:49:28 +0200917LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +0200918yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
919 struct lysp_ext_instance **exts)
920{
921 const char *temp_version = NULL;
922 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
923
924 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
925 if (strcmp(temp_version, "1.0") == 0) {
926 *version = LYS_VERSION_1_0;
927 } else if (strcmp(temp_version, "1.1") == 0) {
928 *version = LYS_VERSION_1_1;
929 } else {
930 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
931 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
932 return LY_EVALID;
933 }
934 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
935 ctx->mod_version = *version;
936
937 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
938}
939
940LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200941yin_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 +0100942{
David Sedlák736fd0d2019-02-15 16:06:31 +0100943 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200944 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +0200945 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200946
David Sedlákb0faad82019-07-04 14:28:59 +0200947 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
948 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +0200949 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
950 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200951 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100952
David Sedlák92147b02019-07-09 14:01:01 +0200953 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +0200954 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 +0200955 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200956 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +0200957 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 +0200958
David Sedlákda8ffa32019-07-08 14:17:10 +0200959 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100960}
961
David Sedlák11900c82019-06-18 16:29:12 +0200962LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +0200963yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
964 struct lysp_ext_instance **exts)
965{
966 const char *temp_val = NULL;
967 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
968
969 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
970 if (strcmp(temp_val, "true") == 0) {
971 *flags |= LYS_MAND_TRUE;
972 } else if (strcmp(temp_val, "false") == 0) {
973 *flags |= LYS_MAND_FALSE;
974 } else {
975 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
976 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
977 return LY_EVALID;
978 }
979 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
980
981 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
982}
983
984LY_ERR
985yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags,
986 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200987{
David Sedlák3ffbc522019-07-02 17:49:28 +0200988 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200989 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200990
David Sedlák292763b2019-07-09 11:10:53 +0200991 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 +0200992 if (strcmp(value, "current") == 0) {
993 *flags |= LYS_STATUS_CURR;
994 } else if (strcmp(value, "deprecated") == 0) {
995 *flags |= LYS_STATUS_DEPRC;
996 } else if (strcmp(value, "obsolete") == 0) {
997 *flags |= LYS_STATUS_OBSLT;
998 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200999 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1000 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001001 return LY_EVALID;
1002 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001003 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001004
David Sedlákda8ffa32019-07-08 14:17:10 +02001005 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001006}
1007
David Sedlák11900c82019-06-18 16:29:12 +02001008LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001009yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1010{
1011 struct lysp_when *when;
1012 when = calloc(1, sizeof *when);
1013 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1014 yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
1015 *when_p = when;
1016 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1017 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1018 {YANG_CUSTOM, NULL, 0}};
1019
1020 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1021}
1022
1023LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001024yin_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 +02001025 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001026{
David Sedlák3ffbc522019-07-02 17:49:28 +02001027 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +02001028 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +02001029
David Sedlák292763b2019-07-09 11:10:53 +02001030 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 +02001031 if (strcmp(temp_val, "true") == 0) {
1032 *flags |= LYS_YINELEM_TRUE;
1033 } else if (strcmp(temp_val, "false") == 0) {
1034 *flags |= LYS_YINELEM_FALSE;
1035 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001036 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1037 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001038 return LY_EVALID;
1039 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001040 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001041
David Sedlákda8ffa32019-07-08 14:17:10 +02001042 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001043}
1044
1045LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001046yin_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 +02001047 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001048{
1049 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001050 char *out;
1051 const char *name, *prefix;
1052 size_t out_len, prefix_len, name_len;
1053 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001054 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001055 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1056 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001057
David Sedlákda8ffa32019-07-08 14:17:10 +02001058 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001059
1060 e->yin = 0;
1061 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001062 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001063 e->insubstmt = subelem;
1064 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001065 e->yin |= LYS_YIN;
1066
David Sedlákb1a78352019-06-28 16:16:29 +02001067 /* store attributes as subelements */
1068 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
1069 if (!iter->prefix) {
1070 new_subelem = calloc(1, sizeof(*new_subelem));
1071 if (!e->child) {
1072 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001073 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001074 last_subelem->next = new_subelem;
1075 }
1076 last_subelem = new_subelem;
1077
1078 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001079 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1080 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001081 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001082 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1083 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001084 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001085 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1086 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001087 }
1088 }
1089 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001090
David Sedlákf250ecf2019-07-01 11:02:05 +02001091 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001092 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1093 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001094 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001095 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1096 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001097 if (!name) {
1098 /* end of extension instance reached */
1099 break;
1100 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001101 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 +02001102 if (!e->child) {
1103 e->child = new_subelem;
1104 } else {
1105 last_subelem->next = new_subelem;
1106 }
1107 last_subelem = new_subelem;
1108 }
David Sedlák555c7202019-07-04 12:14:12 +02001109 } else {
1110 /* save text content */
1111 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001112 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001113 if (!e->argument) {
1114 free(out);
1115 return LY_EMEM;
1116 }
1117 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001118 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001119 LY_CHECK_RET(!e->argument, LY_EMEM);
1120 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001121 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001122 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001123 }
David Sedlákb1a78352019-06-28 16:16:29 +02001124 }
1125
1126 return LY_SUCCESS;
1127}
1128
1129LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001130yin_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 +02001131 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001132{
1133 LY_ERR ret = LY_SUCCESS;
1134 const char *temp_prefix, *temp_name;
1135 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001136 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001137 int dynamic;
1138 struct yin_arg_record *subelem_args = NULL;
1139 struct lysp_stmt *last = NULL, *new = NULL;
1140
1141 /* allocate new structure for element */
1142 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001143 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1144 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001145
1146 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001147 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001148 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001149 /* add new element to linked-list */
1150 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001151 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001152 if (!(*element)->child) {
1153 /* save first */
1154 (*element)->child = new;
1155 } else {
1156 last->next = new;
1157 }
1158 last = new;
1159
1160 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001161 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 +02001162 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001163 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001164 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001165 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1166 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001167 /* attributes with prefix are ignored */
1168 if (!temp_prefix) {
1169 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001170 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001171 if (!last->arg) {
1172 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001173 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001174 ret = LY_EMEM;
1175 goto err;
1176 }
1177 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001178 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1179 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001180 }
1181 }
1182 }
1183
1184 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001185 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001186 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001187 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001188 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001189 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 +02001190 LY_CHECK_GOTO(ret, err);
1191 if (!name) {
1192 /* end of element reached */
1193 break;
1194 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001195 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 +02001196 LY_CHECK_GOTO(ret, err);
1197 last = last->next;
1198 }
1199 } else {
1200 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001201 if (out_len != 0) {
1202 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001203 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001204 if (!(*element)->arg) {
1205 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001206 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001207 ret = LY_EMEM;
1208 goto err;
1209 }
1210 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001211 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1212 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001213 }
David Sedlákb1a78352019-06-28 16:16:29 +02001214 }
1215 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001216 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 +02001217 LY_CHECK_GOTO(ret, err);
1218 }
1219
David Sedlákda8ffa32019-07-08 14:17:10 +02001220 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001221 return LY_SUCCESS;
1222
1223err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001224 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001225 return ret;
1226}
1227
1228LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001229yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001230 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001231{
David Sedlák3ffbc522019-07-02 17:49:28 +02001232 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001233 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001234
David Sedlák292763b2019-07-09 11:10:53 +02001235 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 +02001236
David Sedlákda8ffa32019-07-08 14:17:10 +02001237 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001238}
1239
1240LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001241yin_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 +02001242{
David Sedlák11900c82019-06-18 16:29:12 +02001243 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001244 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001245 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 +02001246
David Sedlák3ffbc522019-07-02 17:49:28 +02001247 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1248 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1249 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1250 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1251 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001252 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001253
David Sedlákda8ffa32019-07-08 14:17:10 +02001254 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001255}
1256
David Sedlák00250342019-06-21 14:19:39 +02001257/**
1258 * @brief Parse module substatements.
1259 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001260 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001261 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001262 * @param[in,out] data Data to read from.
1263 * @param[out] mod Parsed module structure.
1264 *
1265 * @return LY_ERR values.
1266 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001267static LY_ERR
1268yin_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 +02001269{
David Sedlákda8ffa32019-07-08 14:17:10 +02001270 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001271 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1272 {YANG_EXTENSION, &(*mod)->exts, 0},
1273 {YANG_IMPORT, *mod, 0},
1274 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1275 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001276 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1277 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001278 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001279
David Sedlák292763b2019-07-09 11:10:53 +02001280 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 +01001281
David Sedlákda8ffa32019-07-08 14:17:10 +02001282 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001283}
1284
1285LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001286yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001287{
David Sedláke4889912018-11-02 09:52:40 +01001288 LY_ERR ret = LY_SUCCESS;
1289 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001290 struct lysp_module *mod_p = NULL;
1291 const char *prefix, *name;
1292 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001293
David Sedlák8f7a1172019-06-20 14:42:18 +02001294 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001295
David Sedlákda8ffa32019-07-08 14:17:10 +02001296 struct yin_parser_ctx yin_ctx;
1297
1298 /* initialize context */
1299 memset(&yin_ctx, 0, sizeof yin_ctx);
1300 yin_ctx.xml_ctx.ctx = ctx;
1301 yin_ctx.xml_ctx.line = 1;
1302
David Sedláke4889912018-11-02 09:52:40 +01001303
David Sedlák3017da42019-02-15 09:48:04 +01001304 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001305 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001306 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001307 ret = yin_load_attributes(&yin_ctx, &data, &args);
David Sedlák00250342019-06-21 14:19:39 +02001308 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001309 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001310 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001311 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1312 ret = LY_EINVAL;
1313 goto cleanup;
1314 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001315 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 +02001316 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001317 ret = LY_EVALID;
1318 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001319 }
1320
David Sedlák3017da42019-02-15 09:48:04 +01001321 /* allocate module */
1322 mod_p = calloc(1, sizeof *mod_p);
1323 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1324 mod_p->mod = mod;
1325 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001326
David Sedlák00250342019-06-21 14:19:39 +02001327 /* parse module substatements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001328 ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001329 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001330
David Sedlák3017da42019-02-15 09:48:04 +01001331 mod_p->parsing = 0;
1332 mod->parsed = mod_p;
1333
1334cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001335 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001336 lysp_module_free(mod_p);
1337 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001338 FREE_ARRAY(&yin_ctx, args, free_arg_rec);
1339 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001340 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001341}