blob: dfbba278aa424c1dcfb1d83ed505d4460313ab46 [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ákf6251182019-06-06 10:22:13 +020039const char *const yin_attr_list[] = {
40 [YIN_ARG_NAME] = "name",
41 [YIN_ARG_TARGET_NODE] = "target-node",
42 [YIN_ARG_MODULE] = "module",
43 [YIN_ARG_VALUE] = "value",
44 [YIN_ARG_TEXT] = "text",
45 [YIN_ARG_CONDITION] = "condition",
46 [YIN_ARG_URI] = "uri",
47 [YIN_ARG_DATE] = "date",
48 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020049};
50
David Sedlák1bccdfa2019-06-17 15:55:27 +020051enum yang_keyword
David Sedlákda8ffa32019-07-08 14:17:10 +020052yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
David Sedlák1bccdfa2019-06-17 15:55:27 +020053{
David Sedlák8f7a1172019-06-20 14:42:18 +020054 const char *start = NULL;
55 enum yang_keyword kw = YANG_NONE;
56 const struct lyxml_ns *ns = NULL;
57
58 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020059 return YANG_NONE;
60 }
61
David Sedlákda8ffa32019-07-08 14:17:10 +020062 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020063 if (ns) {
64 if (!IS_YIN_NS(ns->uri)) {
65 return YANG_CUSTOM;
66 }
67 } else {
68 /* elements without namespace are automatically unknown */
69 return YANG_NONE;
70 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020071
David Sedlák8f7a1172019-06-20 14:42:18 +020072
73 start = name;
74 kw = lysp_match_kw(NULL, &name);
75
76 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020077 return kw;
78 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020079 if (strncmp(start, "text", name_len) == 0) {
80 return YIN_TEXT;
81 } else if (strncmp(start, "value", name_len) == 0) {
82 return YIN_VALUE;
83 } else {
84 return YANG_NONE;
85 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020086 }
87}
88
David Sedlák872c7b42018-10-26 13:15:20 +020089enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020090yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020091{
David Sedláka7406952019-04-05 10:33:07 +020092 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020093 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020094 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020095
David Sedlák94de2aa2019-02-15 12:42:11 +010096#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
97#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010098#define IF_ARG_PREFIX_END }
99
David Sedlák1c8b2702019-02-22 11:03:02 +0100100 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100101 case 'c':
102 already_read += 1;
103 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200104 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200105
David Sedlák94de2aa2019-02-15 12:42:11 +0100106 case 'd':
107 already_read += 1;
108 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200109 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200110
David Sedlák94de2aa2019-02-15 12:42:11 +0100111 case 'm':
112 already_read += 1;
113 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200114 break;
115
David Sedlák94de2aa2019-02-15 12:42:11 +0100116 case 'n':
117 already_read += 1;
118 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200119 break;
120
David Sedlák94de2aa2019-02-15 12:42:11 +0100121 case 't':
122 already_read += 1;
123 IF_ARG_PREFIX("a", 1)
124 IF_ARG("g", 1, YIN_ARG_TAG)
125 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
126 IF_ARG_PREFIX_END
127 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200128 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200129
David Sedlák94de2aa2019-02-15 12:42:11 +0100130 case 'u':
131 already_read += 1;
132 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200133 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200134
David Sedlák94de2aa2019-02-15 12:42:11 +0100135 case 'v':
136 already_read += 1;
137 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200138 break;
139 }
140
David Sedlákc10e7902018-12-17 02:17:59 +0100141 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200142 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200143 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200144 }
145
David Sedlák18730132019-03-15 15:51:34 +0100146#undef IF_ARG
147#undef IF_ARG_PREFIX
148#undef IF_ARG_PREFIX_END
149
David Sedlák872c7b42018-10-26 13:15:20 +0200150 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200151}
152
David Sedlákb4e44562019-07-04 15:42:12 +0200153/**
154 * @brief free argument record, content loaded from lyxml_get_string() can be
155 * dynamically allocated in some cases so it must be also freed.
156 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200157static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
158 (void)ctx; /* unused */
David Sedlák00250342019-06-21 14:19:39 +0200159 if (record->dynamic_content) {
160 free(record->content);
161 }
162}
163
David Sedlák8f7a1172019-06-20 14:42:18 +0200164LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200165yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200166{
167 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200168 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200169 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200170
David Sedlák555c7202019-07-04 12:14:12 +0200171 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200172 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
173 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200174 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200175
David Sedlákda8ffa32019-07-08 14:17:10 +0200176 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
177 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200178 argument_record->name = name.value;
179 argument_record->name_len = name.len;
180 argument_record->prefix = prefix.value;
181 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200182 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200183 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200184 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200185 }
186 }
187
David Sedlák8f7a1172019-06-20 14:42:18 +0200188cleanup:
189 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200190 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200191 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200192 }
193 return ret;
194}
195
David Sedlák4a650532019-07-10 11:55:18 +0200196LY_ERR
197yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
198{
199 int prefix = 0;
200 unsigned int c;
201 size_t utf8_char_len;
202 size_t already_read = 0;
203 while (already_read < len) {
204 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
205 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
206 already_read += utf8_char_len;
207 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
208
209 switch (val_type) {
210 case Y_IDENTIF_ARG:
211 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
212 break;
213 case Y_PREF_IDENTIF_ARG:
214 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
215 break;
216 case Y_STR_ARG:
217 case Y_MAYBE_STR_ARG:
218 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
219 break;
220 }
221 }
222
223 return LY_SUCCESS;
224}
225
David Sedlákb4e44562019-07-04 15:42:12 +0200226/**
227 * @brief Parse yin argument.
228 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200229 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200230 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200231 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200232 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
233 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200234 * @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 +0200235 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200236 * @param[in] current_element Identification of current element, used for logging.
237 *
238 * @return LY_ERR values.
239 */
240static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200241yin_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 +0200242 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200243{
David Sedlák8f7a1172019-06-20 14:42:18 +0200244 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
245 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200246 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200247
David Sedlák1bccdfa2019-06-17 15:55:27 +0200248 /* validation of attributes */
David Sedlákb4e44562019-07-04 15:42:12 +0200249 LY_ARRAY_FOR(*attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200250 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
251 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200252 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200253 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200254 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200255 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200256 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
257 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200258 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200259 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200260 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200261 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200262 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200263 /* string is no longer supposed to be freed when the sized array is freed */
264 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200265 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200266 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200267 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200268 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200269 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200270 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 +0200271 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200272 }
273 }
274 }
275
David Sedlák292763b2019-07-09 11:10:53 +0200276 /* anything else than Y_MAYBE_STR_ARG is mandatory */
277 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200278 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 +0200279 return LY_EVALID;
280 }
281
282 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200283}
284
David Sedlákd6e56892019-07-01 15:40:24 +0200285/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200286 * @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 +0200287 *
288 * @param[in] type Type of wanted record.
289 * @param[in] array_size Size of array.
290 * @param[in] array Searched array.
291 *
292 * @return Pointer to desired record on success, NULL if element is not in the array.
293 */
David Sedlákb4e44562019-07-04 15:42:12 +0200294static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200295get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200296{
David Sedlákb0faad82019-07-04 14:28:59 +0200297 signed char left = 0, right = array_size - 1, middle;
298
299 while (left <= right) {
300 middle = left + (right - left) / 2;
301
302 if (array[middle].type == type) {
303 return &array[middle];
304 }
305
306 if (array[middle].type < type) {
307 left = middle + 1;
308 } else {
309 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200310 }
311 }
312
313 return NULL;
314}
315
David Sedlákbba38e52019-07-09 15:20:01 +0200316/**
317 * @brief Helper function to check mandatory constraint of subelement.
318 *
319 * @param[in,out] ctx Yin parser context for logging and to store current state.
320 * @param[in] subelem_info Array of information about subelements.
321 * @param[in] subelem_info_size Size of subelem_info array.
322 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
323 *
324 * @return LY_ERR values.
325 */
326static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200327yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200328 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200329{
David Sedlákb0faad82019-07-04 14:28:59 +0200330 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200331 /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */
332 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200333 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 +0200334 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200335 return LY_EVALID;
336 }
337 }
338
339 return LY_SUCCESS;
340}
341
David Sedlákbba38e52019-07-09 15:20:01 +0200342/**
343 * @brief Helper function to check "first" constraint of subelement.
344 *
345 * @param[in,out] ctx Yin parser context for logging and to store current state.
346 * @param[in] subelem_info Array of information about subelements.
347 * @param[in] subelem_info_size Size of subelem_info array.
348 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
349 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
350 *
351 * @return LY_ERR values.
352 */
353static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200354yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200355 signed char subelem_info_size, enum yang_keyword current_element, struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200356{
David Sedlákb0faad82019-07-04 14:28:59 +0200357 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200358 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200359 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 +0200360 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200361 return LY_EVALID;
362 }
363 }
364
365 return LY_SUCCESS;
366}
367
David Sedlákb0faad82019-07-04 14:28:59 +0200368/* 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 +0200369/**
370 * @brief Helper function to check if array of information about subelements is in ascending order.
371 *
372 * @param[in] subelem_info Array of information about subelements.
373 * @param[in] subelem_info_size Size of subelem_info array.
374 *
375 * @return True iff subelem_info array is in ascending order, False otherwise.
376 */
377static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200378is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
379{
David Sedlák292763b2019-07-09 11:10:53 +0200380 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200381
382 for (signed char i = 0; i < subelem_info_size; ++i) {
383 if (subelem_info[i].type <= current) {
384 return false;
385 }
386 current = subelem_info[i].type;
387 }
388
389 return true;
390}
391
David Sedlákd6e56892019-07-01 15:40:24 +0200392/**
David Sedlákb4e44562019-07-04 15:42:12 +0200393 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
394 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200395 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200396 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200397 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200398 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200399 * @param[in] kw Type of current element.
400 * @param[out] value Where value of attribute should be stored.
401 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200402 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200403 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200404 *
David Sedlákd6e56892019-07-01 15:40:24 +0200405 * @return LY_ERR values.
406 */
David Sedlákb4e44562019-07-04 15:42:12 +0200407static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200408yin_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 +0200409 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 +0200410{
David Sedlák292763b2019-07-09 11:10:53 +0200411 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlákb4e44562019-07-04 15:42:12 +0200412 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
413
David Sedlákda8ffa32019-07-08 14:17:10 +0200414 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200415}
416
417/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200418 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
419 * more instances, such as base or if-feature.
420 *
421 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200422 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200423 * @param[in,out] data Data to read from, always moved to currently handled character.
424 * @param[in] kw Type of current element.
425 * @param[out] values Parsed values to add to.
426 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200427 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200428 * @param[in,out] exts Extension instance to add to.
429 *
430 * @return LY_ERR values.
431 */
432static LY_ERR
433yin_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 +0200434 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 +0200435{
436 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200437 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200438 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
439 struct yin_subelement subelems[1] = {{YANG_CUSTOM, &index, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200440 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200441
442 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
443}
444
445/**
David Sedlákb4e44562019-07-04 15:42:12 +0200446 * @brief function to parse meta tags (description, contact, ...) eg. elements with
447 * text element as child
448 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200449 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200450 * @param[in,out] data Data to read from.
David Sedlákbba38e52019-07-09 15:20:01 +0200451 * @param[in] Type of element can be se to YANG_ORGANIZATION or YANG_CONTACT or YANG_DESCRIPTION or YANG_REFERENCE.
David Sedlákb4e44562019-07-04 15:42:12 +0200452 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200453 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200454 *
455 * @return LY_ERR values.
456 */
457static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200458yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200459 const char **value, struct lysp_ext_instance **exts)
460{
461 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
462
463 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
464 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
465
David Sedlákda8ffa32019-07-08 14:17:10 +0200466 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200467}
468
469/**
470 * @brief Map keyword type to substatement info.
471 *
472 * @param[in] kw Keyword type.
473 *
474 * @return correct LYEXT_SUBSTMT information.
475 */
476static LYEXT_SUBSTMT
477kw2lyext_substmt(enum yang_keyword kw)
478{
479 switch (kw) {
480 case YANG_ARGUMENT:
481 return LYEXT_SUBSTMT_ARGUMENT;
482 case YANG_BASE:
483 return LYEXT_SUBSTMT_BASE;
484 case YANG_BELONGS_TO:
485 return LYEXT_SUBSTMT_BELONGSTO;
486 case YANG_CONTACT:
487 return LYEXT_SUBSTMT_CONTACT;
488 case YANG_DEFAULT:
489 return LYEXT_SUBSTMT_DEFAULT;
490 case YANG_DESCRIPTION:
491 return LYEXT_SUBSTMT_DESCRIPTION;
492 case YANG_ERROR_APP_TAG:
493 return LYEXT_SUBSTMT_ERRTAG;
494 case YANG_ERROR_MESSAGE:
495 return LYEXT_SUBSTMT_ERRMSG;
496 case YANG_KEY:
497 return LYEXT_SUBSTMT_KEY;
498 case YANG_NAMESPACE:
499 return LYEXT_SUBSTMT_NAMESPACE;
500 case YANG_ORGANIZATION:
501 return LYEXT_SUBSTMT_ORGANIZATION;
502 case YANG_PATH:
503 return LYEXT_SUBSTMT_PATH;
504 case YANG_PREFIX:
505 return LYEXT_SUBSTMT_PREFIX;
506 case YANG_PRESENCE:
507 return LYEXT_SUBSTMT_PRESENCE;
508 case YANG_REFERENCE:
509 return LYEXT_SUBSTMT_REFERENCE;
510 case YANG_REVISION_DATE:
511 return LYEXT_SUBSTMT_REVISIONDATE;
512 case YANG_UNITS:
513 return LYEXT_SUBSTMT_UNITS;
514 case YANG_VALUE:
515 return LYEXT_SUBSTMT_VALUE;
516 case YANG_YANG_VERSION:
517 return LYEXT_SUBSTMT_VERSION;
518 case YANG_MODIFIER:
519 return LYEXT_SUBSTMT_MODIFIER;
520 case YANG_REQUIRE_INSTANCE:
521 return LYEXT_SUBSTMT_REQINSTANCE;
522 case YANG_YIN_ELEMENT:
523 return LYEXT_SUBSTMT_YINELEM;
524 case YANG_CONFIG:
525 return LYEXT_SUBSTMT_CONFIG;
526 case YANG_MANDATORY:
527 return LYEXT_SUBSTMT_MANDATORY;
528 case YANG_ORDERED_BY:
529 return LYEXT_SUBSTMT_ORDEREDBY;
530 case YANG_STATUS:
531 return LYEXT_SUBSTMT_STATUS;
532 case YANG_FRACTION_DIGITS:
533 return LYEXT_SUBSTMT_FRACDIGITS;
534 case YANG_MAX_ELEMENTS:
535 return LYEXT_SUBSTMT_MAX;
536 case YANG_MIN_ELEMENTS:
537 return LYEXT_SUBSTMT_MIN;
538 case YANG_POSITION:
539 return LYEXT_SUBSTMT_POSITION;
540 case YANG_UNIQUE:
541 return LYEXT_SUBSTMT_UNIQUE;
542 case YANG_IF_FEATURE:
543 return LYEXT_SUBSTMT_IFFEATURE;
544 default:
545 return LYEXT_SUBSTMT_SELF;
546 }
547}
548
David Sedlák9c40a922019-07-08 17:04:43 +0200549
550/**
551 * @brief Parse belongs-to element.
552 *
553 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200554 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200555 * @param[in,out] data Data to read from, always moved to currently handled character.
556 * @param[out] submod Structure of submodule that is being parsed.
557 * @param[in,out] exts Extension instances to add to.
558 *
559 * @return LY_ERR values
560 */
561static LY_ERR
562yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
563 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
564{
565 struct yin_subelement subelems[2] = {{YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
566 {YANG_CUSTOM, NULL, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200567 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 +0200568
569 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
570}
571
David Sedlákd6e56892019-07-01 15:40:24 +0200572LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200573yin_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 +0200574 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 +0200575{
576 LY_ERR ret = LY_SUCCESS;
577 struct sized_string prefix, name;
578 char *out;
579 size_t out_len;
580 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200581 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200582 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200583 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200584 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200585 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200586 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200587
David Sedlákda8ffa32019-07-08 14:17:10 +0200588 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
589 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200590 /* current element has subelements as content */
591 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200592 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
593 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200594 LY_CHECK_GOTO(ret, cleanup);
595 if (!name.value) {
596 /* end of current element reached */
597 break;
598 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200599 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200600 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200601 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200602
603 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200604 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
605 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200606 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 +0200607 ret = LY_EVALID;
608 goto cleanup;
609 }
610
David Sedlák5f8191e2019-07-08 16:35:52 +0200611 /* TODO check relative order */
612
David Sedlák21f87cd2019-07-03 16:53:23 +0200613 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200614 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200615 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 +0200616 return LY_EVALID;
617 }
618 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200619 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 +0200620 }
621 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
622
David Sedlákd6e56892019-07-01 15:40:24 +0200623 switch (kw) {
624 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200625 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlákda8ffa32019-07-08 14:17:10 +0200626 ret = yin_parse_extension_instance(ctx, &subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200627 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200628 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200629 break;
630 case YANG_ACTION:
631 break;
632 case YANG_ANYDATA:
633 break;
634 case YANG_ANYXML:
635 break;
636 case YANG_ARGUMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200637 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 +0200638 break;
639 case YANG_AUGMENT:
640 break;
641 case YANG_BASE:
David Sedlák292763b2019-07-09 11:10:53 +0200642 if (current_element == YANG_IDENTITY) {
643 type = (struct lysp_type *)subelem_info_rec->dest;
644 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
645 Y_PREF_IDENTIF_ARG, exts);
646 type->flags |= LYS_SET_BASE;
647 } else if (current_element == YANG_TYPE) {
648 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
649 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
650 } else {
651 LOGINT(ctx->xml_ctx.ctx);
652 ret = LY_EINT;
653 }
David Sedlákd6e56892019-07-01 15:40:24 +0200654 break;
655 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +0200656 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 +0200657 break;
658 case YANG_BIT:
659 break;
660 case YANG_CASE:
661 break;
662 case YANG_CHOICE:
663 break;
664 case YANG_CONFIG:
665 break;
666 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200667 case YANG_DESCRIPTION:
668 case YANG_ORGANIZATION:
669 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200670 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200671 break;
672 case YANG_CONTAINER:
673 break;
674 case YANG_DEFAULT:
675 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200676 case YANG_DEVIATE:
677 break;
678 case YANG_DEVIATION:
679 break;
680 case YANG_ENUM:
681 break;
682 case YANG_ERROR_APP_TAG:
683 break;
684 case YANG_ERROR_MESSAGE:
685 break;
686 case YANG_EXTENSION:
David Sedlákda8ffa32019-07-08 14:17:10 +0200687 ret = yin_parse_extension(ctx, &subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200688 break;
689 case YANG_FEATURE:
690 break;
691 case YANG_FRACTION_DIGITS:
692 break;
693 case YANG_GROUPING:
694 break;
695 case YANG_IDENTITY:
696 break;
697 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200698 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200699 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200700 break;
701 case YANG_IMPORT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200702 ret = yin_parse_import(ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200703 break;
704 case YANG_INCLUDE:
705 break;
706 case YANG_INPUT:
707 break;
708 case YANG_KEY:
709 break;
710 case YANG_LEAF:
711 break;
712 case YANG_LEAF_LIST:
713 break;
714 case YANG_LENGTH:
715 break;
716 case YANG_LIST:
717 break;
718 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +0200719 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200720 break;
721 case YANG_MAX_ELEMENTS:
722 break;
723 case YANG_MIN_ELEMENTS:
724 break;
725 case YANG_MODIFIER:
726 break;
727 case YANG_MODULE:
728 break;
729 case YANG_MUST:
730 break;
731 case YANG_NAMESPACE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200732 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200733 (const char **)subelem_info_rec->dest, YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200734 break;
735 case YANG_NOTIFICATION:
736 break;
737 case YANG_ORDERED_BY:
738 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200739 case YANG_OUTPUT:
740 break;
741 case YANG_PATH:
742 break;
743 case YANG_PATTERN:
744 break;
745 case YANG_POSITION:
746 break;
747 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +0200748 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200749 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200750 break;
751 case YANG_PRESENCE:
752 break;
753 case YANG_RANGE:
754 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200755 case YANG_REFINE:
756 break;
757 case YANG_REQUIRE_INSTANCE:
758 break;
759 case YANG_REVISION:
760 break;
761 case YANG_REVISION_DATE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200762 ret = yin_parse_revision_date(ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200763 break;
764 case YANG_RPC:
765 break;
766 case YANG_STATUS:
David Sedlákda8ffa32019-07-08 14:17:10 +0200767 ret = yin_parse_status(ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200768 break;
769 case YANG_SUBMODULE:
770 break;
771 case YANG_TYPE:
772 break;
773 case YANG_TYPEDEF:
774 break;
775 case YANG_UNIQUE:
David Sedlákd4909d92019-07-09 16:33:10 +0200776 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
777 (const char ***)subelem_info_rec->dest, YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200778 break;
779 case YANG_UNITS:
780 break;
781 case YANG_USES:
782 break;
783 case YANG_VALUE:
784 break;
785 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200786 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200787 break;
788 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +0200789 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200790 break;
791 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200792 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 +0200793 break;
794 case YIN_TEXT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200795 ret = yin_parse_content(ctx, NULL, 0, data, YIN_TEXT, (const char **)subelem_info_rec->dest, NULL);
David Sedlák3ffbc522019-07-02 17:49:28 +0200796 break;
797 case YIN_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +0200798 break;
799 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200800 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200801 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200802 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200803 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200804 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200805 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200806 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200807 }
808 } else {
809 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200810 /* save text content, if text_content isn't set, it's just ignored */
811 if (text_content) {
812 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200813 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200814 if (!*text_content) {
815 free(out);
816 return LY_EMEM;
817 }
818 } else {
819 if (out_len == 0) {
820 *text_content = NULL;
821 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200822 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200823 }
824 }
825 }
David Sedlákd6e56892019-07-01 15:40:24 +0200826 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +0200827 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 +0200828 }
David Sedlák555c7202019-07-04 12:14:12 +0200829
David Sedlákda8ffa32019-07-08 14:17:10 +0200830 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200831 }
832
833cleanup:
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 return ret;
836}
837
David Sedlák619db942019-07-03 14:47:30 +0200838LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200839yin_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 +0200840 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200841{
David Sedlák3ffbc522019-07-02 17:49:28 +0200842 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200843 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200844
David Sedlák292763b2019-07-09 11:10:53 +0200845 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 +0200846 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 +0200847
848 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +0200849 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200850
David Sedlákda8ffa32019-07-08 14:17:10 +0200851 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200852}
David Sedlák00250342019-06-21 14:19:39 +0200853
David Sedlák3ffbc522019-07-02 17:49:28 +0200854LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +0200855yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
856 struct lysp_ext_instance **exts)
857{
858 const char *temp_version = NULL;
859 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
860
861 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
862 if (strcmp(temp_version, "1.0") == 0) {
863 *version = LYS_VERSION_1_0;
864 } else if (strcmp(temp_version, "1.1") == 0) {
865 *version = LYS_VERSION_1_1;
866 } else {
867 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
868 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
869 return LY_EVALID;
870 }
871 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
872 ctx->mod_version = *version;
873
874 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
875}
876
877LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200878yin_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 +0100879{
David Sedlák736fd0d2019-02-15 16:06:31 +0100880 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200881 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +0200882 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200883
David Sedlákb0faad82019-07-04 14:28:59 +0200884 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
885 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +0200886 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
887 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200888 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100889
David Sedlák92147b02019-07-09 14:01:01 +0200890 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +0200891 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 +0200892 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200893 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +0200894 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 +0200895
David Sedlákda8ffa32019-07-08 14:17:10 +0200896 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100897}
898
David Sedlák11900c82019-06-18 16:29:12 +0200899LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +0200900yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
901 struct lysp_ext_instance **exts)
902{
903 const char *temp_val = NULL;
904 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
905
906 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
907 if (strcmp(temp_val, "true") == 0) {
908 *flags |= LYS_MAND_TRUE;
909 } else if (strcmp(temp_val, "false") == 0) {
910 *flags |= LYS_MAND_FALSE;
911 } else {
912 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
913 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
914 return LY_EVALID;
915 }
916 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
917
918 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
919}
920
921LY_ERR
922yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags,
923 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200924{
David Sedlák3ffbc522019-07-02 17:49:28 +0200925 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200926 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200927
David Sedlák292763b2019-07-09 11:10:53 +0200928 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 +0200929 if (strcmp(value, "current") == 0) {
930 *flags |= LYS_STATUS_CURR;
931 } else if (strcmp(value, "deprecated") == 0) {
932 *flags |= LYS_STATUS_DEPRC;
933 } else if (strcmp(value, "obsolete") == 0) {
934 *flags |= LYS_STATUS_OBSLT;
935 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200936 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
937 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +0200938 return LY_EVALID;
939 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200940 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +0200941
David Sedlákda8ffa32019-07-08 14:17:10 +0200942 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +0200943}
944
David Sedlák11900c82019-06-18 16:29:12 +0200945LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +0200946yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
947{
948 struct lysp_when *when;
949 when = calloc(1, sizeof *when);
950 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
951 yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
952 *when_p = when;
953 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
954 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
955 {YANG_CUSTOM, NULL, 0}};
956
957 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
958}
959
960LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200961yin_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 +0200962 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +0200963{
David Sedlák3ffbc522019-07-02 17:49:28 +0200964 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200965 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +0200966
David Sedlák292763b2019-07-09 11:10:53 +0200967 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 +0200968 if (strcmp(temp_val, "true") == 0) {
969 *flags |= LYS_YINELEM_TRUE;
970 } else if (strcmp(temp_val, "false") == 0) {
971 *flags |= LYS_YINELEM_FALSE;
972 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200973 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
974 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +0200975 return LY_EVALID;
976 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200977 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +0200978
David Sedlákda8ffa32019-07-08 14:17:10 +0200979 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +0200980}
981
982LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200983yin_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 +0200984 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +0200985{
986 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +0200987 char *out;
988 const char *name, *prefix;
989 size_t out_len, prefix_len, name_len;
990 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200991 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +0200992 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
993 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200994
David Sedlákda8ffa32019-07-08 14:17:10 +0200995 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200996
997 e->yin = 0;
998 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +0200999 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001000 e->insubstmt = subelem;
1001 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001002 e->yin |= LYS_YIN;
1003
David Sedlákb1a78352019-06-28 16:16:29 +02001004 /* store attributes as subelements */
1005 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
1006 if (!iter->prefix) {
1007 new_subelem = calloc(1, sizeof(*new_subelem));
1008 if (!e->child) {
1009 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001010 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001011 last_subelem->next = new_subelem;
1012 }
1013 last_subelem = new_subelem;
1014
1015 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001016 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1017 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001018 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001019 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1020 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001021 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001022 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1023 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001024 }
1025 }
1026 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001027
David Sedlákf250ecf2019-07-01 11:02:05 +02001028 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001029 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1030 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001031 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001032 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1033 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001034 if (!name) {
1035 /* end of extension instance reached */
1036 break;
1037 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001038 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 +02001039 if (!e->child) {
1040 e->child = new_subelem;
1041 } else {
1042 last_subelem->next = new_subelem;
1043 }
1044 last_subelem = new_subelem;
1045 }
David Sedlák555c7202019-07-04 12:14:12 +02001046 } else {
1047 /* save text content */
1048 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001049 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001050 if (!e->argument) {
1051 free(out);
1052 return LY_EMEM;
1053 }
1054 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001055 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001056 LY_CHECK_RET(!e->argument, LY_EMEM);
1057 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001058 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001059 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001060 }
David Sedlákb1a78352019-06-28 16:16:29 +02001061 }
1062
1063 return LY_SUCCESS;
1064}
1065
1066LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001067yin_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 +02001068 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001069{
1070 LY_ERR ret = LY_SUCCESS;
1071 const char *temp_prefix, *temp_name;
1072 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001073 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001074 int dynamic;
1075 struct yin_arg_record *subelem_args = NULL;
1076 struct lysp_stmt *last = NULL, *new = NULL;
1077
1078 /* allocate new structure for element */
1079 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001080 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1081 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001082
1083 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001084 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001085 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001086 /* add new element to linked-list */
1087 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001088 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001089 if (!(*element)->child) {
1090 /* save first */
1091 (*element)->child = new;
1092 } else {
1093 last->next = new;
1094 }
1095 last = new;
1096
1097 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001098 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 +02001099 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001100 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001101 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001102 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1103 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001104 /* attributes with prefix are ignored */
1105 if (!temp_prefix) {
1106 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001107 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001108 if (!last->arg) {
1109 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001110 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001111 ret = LY_EMEM;
1112 goto err;
1113 }
1114 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001115 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1116 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001117 }
1118 }
1119 }
1120
1121 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001122 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001123 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001124 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001125 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001126 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 +02001127 LY_CHECK_GOTO(ret, err);
1128 if (!name) {
1129 /* end of element reached */
1130 break;
1131 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001132 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 +02001133 LY_CHECK_GOTO(ret, err);
1134 last = last->next;
1135 }
1136 } else {
1137 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001138 if (out_len != 0) {
1139 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001140 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001141 if (!(*element)->arg) {
1142 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001143 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001144 ret = LY_EMEM;
1145 goto err;
1146 }
1147 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001148 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1149 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001150 }
David Sedlákb1a78352019-06-28 16:16:29 +02001151 }
1152 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001153 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 +02001154 LY_CHECK_GOTO(ret, err);
1155 }
1156
David Sedlákda8ffa32019-07-08 14:17:10 +02001157 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001158 return LY_SUCCESS;
1159
1160err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001161 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001162 return ret;
1163}
1164
1165LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001166yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001167 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001168{
David Sedlák3ffbc522019-07-02 17:49:28 +02001169 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001170 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001171
David Sedlák292763b2019-07-09 11:10:53 +02001172 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 +02001173
David Sedlákda8ffa32019-07-08 14:17:10 +02001174 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001175}
1176
1177LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001178yin_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 +02001179{
David Sedlák11900c82019-06-18 16:29:12 +02001180 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001181 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001182 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 +02001183
David Sedlák3ffbc522019-07-02 17:49:28 +02001184 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1185 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1186 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1187 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1188 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001189 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001190
David Sedlákda8ffa32019-07-08 14:17:10 +02001191 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001192}
1193
David Sedlák00250342019-06-21 14:19:39 +02001194/**
1195 * @brief Parse module substatements.
1196 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001197 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001198 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001199 * @param[in,out] data Data to read from.
1200 * @param[out] mod Parsed module structure.
1201 *
1202 * @return LY_ERR values.
1203 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001204static LY_ERR
1205yin_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 +02001206{
David Sedlákda8ffa32019-07-08 14:17:10 +02001207 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001208 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1209 {YANG_EXTENSION, &(*mod)->exts, 0},
1210 {YANG_IMPORT, *mod, 0},
1211 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1212 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001213 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1214 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001215 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001216
David Sedlák292763b2019-07-09 11:10:53 +02001217 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 +01001218
David Sedlákda8ffa32019-07-08 14:17:10 +02001219 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001220}
1221
1222LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001223yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001224{
David Sedláke4889912018-11-02 09:52:40 +01001225 LY_ERR ret = LY_SUCCESS;
1226 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001227 struct lysp_module *mod_p = NULL;
1228 const char *prefix, *name;
1229 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001230
David Sedlák8f7a1172019-06-20 14:42:18 +02001231 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001232
David Sedlákda8ffa32019-07-08 14:17:10 +02001233 struct yin_parser_ctx yin_ctx;
1234
1235 /* initialize context */
1236 memset(&yin_ctx, 0, sizeof yin_ctx);
1237 yin_ctx.xml_ctx.ctx = ctx;
1238 yin_ctx.xml_ctx.line = 1;
1239
David Sedláke4889912018-11-02 09:52:40 +01001240
David Sedlák3017da42019-02-15 09:48:04 +01001241 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001242 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001243 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001244 ret = yin_load_attributes(&yin_ctx, &data, &args);
David Sedlák00250342019-06-21 14:19:39 +02001245 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001246 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +01001247 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001248 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1249 ret = LY_EINVAL;
1250 goto cleanup;
1251 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001252 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 +02001253 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001254 ret = LY_EVALID;
1255 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001256 }
1257
David Sedlák3017da42019-02-15 09:48:04 +01001258 /* allocate module */
1259 mod_p = calloc(1, sizeof *mod_p);
1260 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1261 mod_p->mod = mod;
1262 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001263
David Sedlák00250342019-06-21 14:19:39 +02001264 /* parse module substatements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001265 ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001266 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001267
David Sedlák3017da42019-02-15 09:48:04 +01001268 mod_p->parsing = 0;
1269 mod->parsed = mod_p;
1270
1271cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001272 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001273 lysp_module_free(mod_p);
1274 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001275 FREE_ARRAY(&yin_ctx, args, free_arg_rec);
1276 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001277 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001278}