blob: f7e703c801284a151ebf747b91c00675dcb8a1b4 [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ákd4909d92019-07-09 16:33:10 +0200809 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
810 (const char ***)subelem_info_rec->dest, YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200811 break;
812 case YANG_UNITS:
813 break;
814 case YANG_USES:
815 break;
816 case YANG_VALUE:
817 break;
818 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200819 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200820 break;
821 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +0200822 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200823 break;
824 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200825 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 +0200826 break;
827 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +0200828 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +0200829 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +0200830 break;
831 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200832 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200833 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200834 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200835 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200836 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200837 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200838 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200839 }
840 } else {
841 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200842 /* save text content, if text_content isn't set, it's just ignored */
843 if (text_content) {
844 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200845 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200846 if (!*text_content) {
847 free(out);
848 return LY_EMEM;
849 }
850 } else {
851 if (out_len == 0) {
852 *text_content = NULL;
853 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200854 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200855 }
856 }
857 }
David Sedlákd6e56892019-07-01 15:40:24 +0200858 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +0200859 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 +0200860 }
David Sedlák555c7202019-07-04 12:14:12 +0200861
David Sedlákda8ffa32019-07-08 14:17:10 +0200862 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200863 }
864
865cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +0200866 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200867 return ret;
868}
869
David Sedlák619db942019-07-03 14:47:30 +0200870LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200871yin_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 +0200872 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200873{
David Sedlák3ffbc522019-07-02 17:49:28 +0200874 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200875 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200876
David Sedlák292763b2019-07-09 11:10:53 +0200877 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 +0200878 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 +0200879
880 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +0200881 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200882
David Sedlákda8ffa32019-07-08 14:17:10 +0200883 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200884}
David Sedlák00250342019-06-21 14:19:39 +0200885
David Sedláke1a30302019-07-10 13:49:38 +0200886/**
887 * @brief Parse config element.
888 *
889 * @param[in] ctx Yin parser context for logging and to store current state.
890 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
891 * @param[in,out] data Data to read from, always moved to currently handled character.
892 * @param[in,out] flags Flags to add to.
893 * @param[in,out] exts Extension instances to add to.
894 *
895 * @return LY_ERR values.
896 */
897static LY_ERR
898yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
899 struct lysp_ext_instance **exts)
900{
901 const char *temp_val = NULL;
902 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
903
904 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
905 if (strcmp(temp_val, "true") == 0) {
906 *flags |= LYS_CONFIG_W;
907 } else if (strcmp(temp_val, "false") == 0) {
908 *flags |= LYS_CONFIG_R;
909 } else {
910 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
911 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
912 return LY_EVALID;
913 }
914 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
915
916 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
917}
918
David Sedlák3ffbc522019-07-02 17:49:28 +0200919LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +0200920yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
921 struct lysp_ext_instance **exts)
922{
923 const char *temp_version = NULL;
924 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
925
926 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
927 if (strcmp(temp_version, "1.0") == 0) {
928 *version = LYS_VERSION_1_0;
929 } else if (strcmp(temp_version, "1.1") == 0) {
930 *version = LYS_VERSION_1_1;
931 } else {
932 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
933 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
934 return LY_EVALID;
935 }
936 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
937 ctx->mod_version = *version;
938
939 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
940}
941
942LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200943yin_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 +0100944{
David Sedlák736fd0d2019-02-15 16:06:31 +0100945 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200946 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +0200947 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200948
David Sedlákb0faad82019-07-04 14:28:59 +0200949 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
950 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +0200951 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
952 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200953 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100954
David Sedlák92147b02019-07-09 14:01:01 +0200955 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +0200956 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 +0200957 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200958 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +0200959 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 +0200960
David Sedlákda8ffa32019-07-08 14:17:10 +0200961 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100962}
963
David Sedlák11900c82019-06-18 16:29:12 +0200964LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +0200965yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
966 struct lysp_ext_instance **exts)
967{
968 const char *temp_val = NULL;
969 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
970
971 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
972 if (strcmp(temp_val, "true") == 0) {
973 *flags |= LYS_MAND_TRUE;
974 } else if (strcmp(temp_val, "false") == 0) {
975 *flags |= LYS_MAND_FALSE;
976 } else {
977 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
978 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
979 return LY_EVALID;
980 }
981 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
982
983 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
984}
985
986LY_ERR
987yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags,
988 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200989{
David Sedlák3ffbc522019-07-02 17:49:28 +0200990 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200991 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200992
David Sedlák292763b2019-07-09 11:10:53 +0200993 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 +0200994 if (strcmp(value, "current") == 0) {
995 *flags |= LYS_STATUS_CURR;
996 } else if (strcmp(value, "deprecated") == 0) {
997 *flags |= LYS_STATUS_DEPRC;
998 } else if (strcmp(value, "obsolete") == 0) {
999 *flags |= LYS_STATUS_OBSLT;
1000 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001001 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1002 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001003 return LY_EVALID;
1004 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001005 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001006
David Sedlákda8ffa32019-07-08 14:17:10 +02001007 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001008}
1009
David Sedlák11900c82019-06-18 16:29:12 +02001010LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001011yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1012{
1013 struct lysp_when *when;
1014 when = calloc(1, sizeof *when);
1015 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1016 yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
1017 *when_p = when;
1018 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1019 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1020 {YANG_CUSTOM, NULL, 0}};
1021
1022 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1023}
1024
1025LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001026yin_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 +02001027 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001028{
David Sedlák3ffbc522019-07-02 17:49:28 +02001029 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +02001030 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +02001031
David Sedlák292763b2019-07-09 11:10:53 +02001032 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 +02001033 if (strcmp(temp_val, "true") == 0) {
1034 *flags |= LYS_YINELEM_TRUE;
1035 } else if (strcmp(temp_val, "false") == 0) {
1036 *flags |= LYS_YINELEM_FALSE;
1037 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001038 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1039 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001040 return LY_EVALID;
1041 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001042 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001043
David Sedlákda8ffa32019-07-08 14:17:10 +02001044 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001045}
1046
1047LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001048yin_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 +02001049 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001050{
1051 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001052 char *out;
1053 const char *name, *prefix;
1054 size_t out_len, prefix_len, name_len;
1055 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001056 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001057 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1058 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001059
David Sedlákda8ffa32019-07-08 14:17:10 +02001060 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001061
1062 e->yin = 0;
1063 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001064 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001065 e->insubstmt = subelem;
1066 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001067 e->yin |= LYS_YIN;
1068
David Sedlákb1a78352019-06-28 16:16:29 +02001069 /* store attributes as subelements */
1070 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
1071 if (!iter->prefix) {
1072 new_subelem = calloc(1, sizeof(*new_subelem));
1073 if (!e->child) {
1074 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001075 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001076 last_subelem->next = new_subelem;
1077 }
1078 last_subelem = new_subelem;
1079
1080 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001081 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1082 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001083 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001084 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1085 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001086 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001087 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1088 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001089 }
1090 }
1091 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001092
David Sedlákf250ecf2019-07-01 11:02:05 +02001093 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001094 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1095 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001096 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001097 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1098 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001099 if (!name) {
1100 /* end of extension instance reached */
1101 break;
1102 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001103 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 +02001104 if (!e->child) {
1105 e->child = new_subelem;
1106 } else {
1107 last_subelem->next = new_subelem;
1108 }
1109 last_subelem = new_subelem;
1110 }
David Sedlák555c7202019-07-04 12:14:12 +02001111 } else {
1112 /* save text content */
1113 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001114 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001115 if (!e->argument) {
1116 free(out);
1117 return LY_EMEM;
1118 }
1119 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001120 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001121 LY_CHECK_RET(!e->argument, LY_EMEM);
1122 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001123 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001124 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001125 }
David Sedlákb1a78352019-06-28 16:16:29 +02001126 }
1127
1128 return LY_SUCCESS;
1129}
1130
1131LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001132yin_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 +02001133 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001134{
1135 LY_ERR ret = LY_SUCCESS;
1136 const char *temp_prefix, *temp_name;
1137 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001138 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001139 int dynamic;
1140 struct yin_arg_record *subelem_args = NULL;
1141 struct lysp_stmt *last = NULL, *new = NULL;
1142
1143 /* allocate new structure for element */
1144 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001145 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1146 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001147
1148 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001149 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001150 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001151 /* add new element to linked-list */
1152 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001153 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001154 if (!(*element)->child) {
1155 /* save first */
1156 (*element)->child = new;
1157 } else {
1158 last->next = new;
1159 }
1160 last = new;
1161
1162 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001163 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 +02001164 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001165 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001166 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001167 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1168 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001169 /* attributes with prefix are ignored */
1170 if (!temp_prefix) {
1171 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001172 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001173 if (!last->arg) {
1174 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001175 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001176 ret = LY_EMEM;
1177 goto err;
1178 }
1179 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001180 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1181 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001182 }
1183 }
1184 }
1185
1186 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001187 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001188 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001189 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001190 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001191 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 +02001192 LY_CHECK_GOTO(ret, err);
1193 if (!name) {
1194 /* end of element reached */
1195 break;
1196 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001197 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 +02001198 LY_CHECK_GOTO(ret, err);
1199 last = last->next;
1200 }
1201 } else {
1202 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001203 if (out_len != 0) {
1204 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001205 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001206 if (!(*element)->arg) {
1207 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001208 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001209 ret = LY_EMEM;
1210 goto err;
1211 }
1212 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001213 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1214 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001215 }
David Sedlákb1a78352019-06-28 16:16:29 +02001216 }
1217 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001218 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 +02001219 LY_CHECK_GOTO(ret, err);
1220 }
1221
David Sedlákda8ffa32019-07-08 14:17:10 +02001222 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001223 return LY_SUCCESS;
1224
1225err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001226 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001227 return ret;
1228}
1229
1230LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001231yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001232 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001233{
David Sedlák3ffbc522019-07-02 17:49:28 +02001234 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001235 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001236
David Sedlák292763b2019-07-09 11:10:53 +02001237 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 +02001238
David Sedlákda8ffa32019-07-08 14:17:10 +02001239 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001240}
1241
1242LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001243yin_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 +02001244{
David Sedlák11900c82019-06-18 16:29:12 +02001245 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001246 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001247 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 +02001248
David Sedlák3ffbc522019-07-02 17:49:28 +02001249 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1250 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1251 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1252 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1253 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001254 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001255
David Sedlákda8ffa32019-07-08 14:17:10 +02001256 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001257}
1258
David Sedlák00250342019-06-21 14:19:39 +02001259/**
1260 * @brief Parse module substatements.
1261 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001262 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001263 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001264 * @param[in,out] data Data to read from.
1265 * @param[out] mod Parsed module structure.
1266 *
1267 * @return LY_ERR values.
1268 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001269static LY_ERR
1270yin_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 +02001271{
David Sedlákda8ffa32019-07-08 14:17:10 +02001272 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001273 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1274 {YANG_EXTENSION, &(*mod)->exts, 0},
1275 {YANG_IMPORT, *mod, 0},
1276 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1277 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001278 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1279 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001280 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001281
David Sedlák292763b2019-07-09 11:10:53 +02001282 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 +01001283
David Sedlákda8ffa32019-07-08 14:17:10 +02001284 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001285}
1286
1287LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001288yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001289{
David Sedláke4889912018-11-02 09:52:40 +01001290 LY_ERR ret = LY_SUCCESS;
1291 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001292 struct lysp_module *mod_p = NULL;
1293 const char *prefix, *name;
1294 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001295
David Sedlák8f7a1172019-06-20 14:42:18 +02001296 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001297
David Sedlákda8ffa32019-07-08 14:17:10 +02001298 struct yin_parser_ctx yin_ctx;
1299
1300 /* initialize context */
1301 memset(&yin_ctx, 0, sizeof yin_ctx);
1302 yin_ctx.xml_ctx.ctx = ctx;
1303 yin_ctx.xml_ctx.line = 1;
1304
David Sedláke4889912018-11-02 09:52:40 +01001305
David Sedlák3017da42019-02-15 09:48:04 +01001306 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001307 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001308 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001309 ret = yin_load_attributes(&yin_ctx, &data, &args);
David Sedlák00250342019-06-21 14:19:39 +02001310 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001311 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001312 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001313 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1314 ret = LY_EINVAL;
1315 goto cleanup;
1316 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001317 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 +02001318 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001319 ret = LY_EVALID;
1320 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001321 }
1322
David Sedlák3017da42019-02-15 09:48:04 +01001323 /* allocate module */
1324 mod_p = calloc(1, sizeof *mod_p);
1325 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1326 mod_p->mod = mod;
1327 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001328
David Sedlák00250342019-06-21 14:19:39 +02001329 /* parse module substatements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001330 ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001331 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001332
David Sedlák3017da42019-02-15 09:48:04 +01001333 mod_p->parsing = 0;
1334 mod->parsed = mod_p;
1335
1336cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001337 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001338 lysp_module_free(mod_p);
1339 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001340 FREE_ARRAY(&yin_ctx, args, free_arg_rec);
1341 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001342 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001343}