blob: faf9fb58d9a7eeaf7c801b822f36677cacfc83eb [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",
49 [YIN_ARG_XMLNS] = "xmlns",
50};
51
David Sedlák1bccdfa2019-06-17 15:55:27 +020052enum yang_keyword
David Sedlák8f7a1172019-06-20 14:42:18 +020053yin_match_keyword(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
David Sedlák1bccdfa2019-06-17 15:55:27 +020054{
David Sedlák8f7a1172019-06-20 14:42:18 +020055 const char *start = NULL;
56 enum yang_keyword kw = YANG_NONE;
57 const struct lyxml_ns *ns = NULL;
58
59 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020060 return YANG_NONE;
61 }
62
David Sedlák8f7a1172019-06-20 14:42:18 +020063 ns = lyxml_ns_get(xml_ctx, prefix, prefix_len);
64 if (ns) {
65 if (!IS_YIN_NS(ns->uri)) {
66 return YANG_CUSTOM;
67 }
68 } else {
69 /* elements without namespace are automatically unknown */
70 return YANG_NONE;
71 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020072
David Sedlák8f7a1172019-06-20 14:42:18 +020073
74 start = name;
75 kw = lysp_match_kw(NULL, &name);
76
77 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020078 return kw;
79 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020080 if (strncmp(start, "text", name_len) == 0) {
81 return YIN_TEXT;
82 } else if (strncmp(start, "value", name_len) == 0) {
83 return YIN_VALUE;
84 } else {
85 return YANG_NONE;
86 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020087 }
88}
89
David Sedlák872c7b42018-10-26 13:15:20 +020090enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020091yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020092{
David Sedláka7406952019-04-05 10:33:07 +020093 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020094 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020095 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020096
David Sedlák94de2aa2019-02-15 12:42:11 +010097#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
98#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010099#define IF_ARG_PREFIX_END }
100
David Sedlák1c8b2702019-02-22 11:03:02 +0100101 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +0100102 case 'x':
103 already_read += 1;
104 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
105 break;
David Sedlák94de2aa2019-02-15 12:42:11 +0100106 case 'c':
107 already_read += 1;
108 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
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 'd':
112 already_read += 1;
113 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200114 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200115
David Sedlák94de2aa2019-02-15 12:42:11 +0100116 case 'm':
117 already_read += 1;
118 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200119 break;
120
David Sedlák94de2aa2019-02-15 12:42:11 +0100121 case 'n':
122 already_read += 1;
123 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200124 break;
125
David Sedlák94de2aa2019-02-15 12:42:11 +0100126 case 't':
127 already_read += 1;
128 IF_ARG_PREFIX("a", 1)
129 IF_ARG("g", 1, YIN_ARG_TAG)
130 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
131 IF_ARG_PREFIX_END
132 else IF_ARG("ext", 3, YIN_ARG_TEXT)
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 'u':
136 already_read += 1;
137 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200138 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200139
David Sedlák94de2aa2019-02-15 12:42:11 +0100140 case 'v':
141 already_read += 1;
142 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200143 break;
144 }
145
David Sedlákc10e7902018-12-17 02:17:59 +0100146 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200147 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200148 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200149 }
150
David Sedlák18730132019-03-15 15:51:34 +0100151#undef IF_ARG
152#undef IF_ARG_PREFIX
153#undef IF_ARG_PREFIX_END
154
David Sedlák872c7b42018-10-26 13:15:20 +0200155 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200156}
157
David Sedlák00250342019-06-21 14:19:39 +0200158static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) {
159 (void)xml_ctx; /* unused */
160 if (record->dynamic_content) {
161 free(record->content);
162 }
163}
164
David Sedlák8f7a1172019-06-20 14:42:18 +0200165LY_ERR
166yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args)
David Sedláka7406952019-04-05 10:33:07 +0200167{
168 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200169 struct yin_arg_record *argument_record = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200170
David Sedlák7ff55a92019-06-17 11:11:41 +0200171 /* load all attributes first */
David Sedlák57715b12019-06-17 13:05:22 +0200172 while (xml_ctx->status == LYXML_ATTRIBUTE) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200173 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *args, argument_record, ret, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200174 ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len,
175 &argument_record->name, &argument_record->name_len);
David Sedlák00250342019-06-21 14:19:39 +0200176 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200177
David Sedlák7ff55a92019-06-17 11:11:41 +0200178 if (xml_ctx->status == LYXML_ATTR_CONTENT) {
David Sedlák57715b12019-06-17 13:05:22 +0200179 argument_record->content = NULL;
180 argument_record->content_len = 0;
181 argument_record->dynamic_content = 0;
182 ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len,
183 &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ák00250342019-06-21 14:19:39 +0200190 FREE_ARRAY(xml_ctx, *args, free_arg_rec);
191 *args = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200192 }
193 return ret;
194}
195
196LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200197yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type,
198 const char **arg_val, uint8_t flags, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200199{
David Sedlák8f7a1172019-06-20 14:42:18 +0200200 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
201 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200202 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200203
David Sedlák1bccdfa2019-06-17 15:55:27 +0200204 /* validation of attributes */
David Sedlák8f7a1172019-06-20 14:42:18 +0200205 LY_ARRAY_FOR(*args, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200206 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
207 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200208 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200209 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200210 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200211 } else if (arg == arg_type) {
David Sedlák619db942019-07-03 14:47:30 +0200212 found = true;
David Sedlák57715b12019-06-17 13:05:22 +0200213 if (iter->dynamic_content) {
214 *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200215 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200216 /* string is no longer supposed to be freed when the sized array is freed */
217 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200218 } else {
219 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200220 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200221 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200222 } else {
David Sedlák21f87cd2019-07-03 16:53:23 +0200223 LOGVAL_PARSER(xml_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 +0200224 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200225 }
226 }
227 }
228
David Sedlák619db942019-07-03 14:47:30 +0200229 if (flags & YIN_ARG_MANDATORY && !found) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200230 LOGVAL_PARSER(xml_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 +0200231 return LY_EVALID;
232 }
233
234 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200235}
236
David Sedlákd6e56892019-07-01 15:40:24 +0200237/**
238 * @brief get record with given type.
239 *
240 * @param[in] type Type of wanted record.
241 * @param[in] array_size Size of array.
242 * @param[in] array Searched array.
243 *
244 * @return Pointer to desired record on success, NULL if element is not in the array.
245 */
246struct yin_subelement *
247get_record(enum yang_keyword type, size_t array_size, struct yin_subelement *array)
248{
249 for (size_t i = 0; i < array_size; ++i) {
250 if (array[i].type == type) {
251 return &array[i];
252 }
253 }
254
255 return NULL;
256}
257
David Sedlák21f87cd2019-07-03 16:53:23 +0200258LY_ERR
259yin_check_subelem_mandatory_constraint(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info,
260 size_t subelem_info_size, enum yang_keyword current_element)
261{
262 for (size_t i = 0; i < subelem_info_size; ++i) {
263 /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */
264 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
265 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Missing mandatory subelement %s of %s element", ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
266 return LY_EVALID;
267 }
268 }
269
270 return LY_SUCCESS;
271}
272
273LY_ERR
274yin_check_subelem_first_constraint(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info,
275 size_t subelem_info_size, enum yang_keyword current_element, struct yin_subelement *exp_first)
276{
277 for (size_t i = 0; i < subelem_info_size; ++i) {
278 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
279 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Subelement %s of %s element must be defined as first subelement.", exp_first->type, current_element);
280 return LY_EVALID;
281 }
282 }
283
284 return LY_SUCCESS;
285}
286
David Sedlákd6e56892019-07-01 15:40:24 +0200287/**
David Sedlák3ffbc522019-07-02 17:49:28 +0200288 * @brief Generic function for content parsing
David Sedlákd6e56892019-07-01 15:40:24 +0200289 *
290 * @param[in,out] xml_ctx Xml context.
David Sedlák3ffbc522019-07-02 17:49:28 +0200291 * @param[in] subelem_info array of valid subelement types and meta information,
292 * array must be ordered by subelem_info->type in ascending order.
David Sedlákd6e56892019-07-01 15:40:24 +0200293 * @param[in] subelem_info_size Size of subelem_info array.
David Sedlákd6e56892019-07-01 15:40:24 +0200294 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák3ffbc522019-07-02 17:49:28 +0200295 * @param[in] current_element Type of current element.
296 * @param[out] Where the text content of element should be stored. Text content is ignored if set to NULL.
297 * @param[in] exts Extension instance to add to. Can be null if element cannot have extension as subelement.
David Sedlákd6e56892019-07-01 15:40:24 +0200298 * @return LY_ERR values.
299 */
300LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200301yin_parse_content(struct lyxml_context *xml_ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
302 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 +0200303{
304 LY_ERR ret = LY_SUCCESS;
305 struct sized_string prefix, name;
306 char *out;
307 size_t out_len;
308 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200309 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200310 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200311 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200312
313 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
314 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
315 /* current element has subelements as content */
316 if (ret == LY_EINVAL) {
317 while (xml_ctx->status == LYXML_ELEMENT) {
318 ret = lyxml_get_element(xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
319 LY_CHECK_GOTO(ret, cleanup);
320 if (!name.value) {
321 /* end of current element reached */
322 break;
323 }
324 ret = yin_load_attributes(xml_ctx, data, &subelem_attrs);
325 LY_CHECK_GOTO(ret, cleanup);
326 kw = yin_match_keyword(xml_ctx, name.value, name.len, prefix.value, prefix.len);
327
328 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200329 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
330 if (!subelem_info_rec) {
David Sedlákd6e56892019-07-01 15:40:24 +0200331 LOGVAL_PARSER(xml_ctx, LY_VCODE_UNEXP_SUBELEM, name.len, name.value, ly_stmt2str(current_element));
332 ret = LY_EVALID;
333 goto cleanup;
334 }
335
336 /* TODO macro to check order */
David Sedlák21f87cd2019-07-03 16:53:23 +0200337 /* if element is unique and already defined log error */
338 if (subelem_info_rec->flags & YIN_SUBELEM_UNIQUE & YIN_SUBELEM_PARSED) {
339 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Redefinition of %s element in %s element.", kw, current_element);
340 return LY_EVALID;
341 }
342 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
343 yin_check_subelem_first_constraint(xml_ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec);
344 }
345 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
346
David Sedlákd6e56892019-07-01 15:40:24 +0200347 switch (kw) {
348 case YANG_CUSTOM:
David Sedlák619db942019-07-03 14:47:30 +0200349 /* TODO write function to calculate index instead of hardcoded 0 */
David Sedlák3ffbc522019-07-02 17:49:28 +0200350 ret = yin_parse_extension_instance(xml_ctx, &subelem_attrs, data, name2fullname(name.value, prefix.len),
351 namelen2fulllen(name.len, prefix.len),
David Sedlák619db942019-07-03 14:47:30 +0200352 kw2lyext_substmt(current_element), 0, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200353 LY_CHECK_GOTO(ret, cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +0200354 break;
355 case YANG_ACTION:
356 break;
357 case YANG_ANYDATA:
358 break;
359 case YANG_ANYXML:
360 break;
361 case YANG_ARGUMENT:
David Sedlák3ffbc522019-07-02 17:49:28 +0200362 ret = yin_parse_argument_element(xml_ctx, &subelem_attrs, data, (struct yin_argument_meta *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200363 break;
364 case YANG_AUGMENT:
365 break;
366 case YANG_BASE:
367 break;
368 case YANG_BELONGS_TO:
369 break;
370 case YANG_BIT:
371 break;
372 case YANG_CASE:
373 break;
374 case YANG_CHOICE:
375 break;
376 case YANG_CONFIG:
377 break;
378 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200379 case YANG_DESCRIPTION:
380 case YANG_ORGANIZATION:
381 case YANG_REFERENCE:
382 ret = yin_parse_meta_element(xml_ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200383 break;
384 case YANG_CONTAINER:
385 break;
386 case YANG_DEFAULT:
387 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200388 case YANG_DEVIATE:
389 break;
390 case YANG_DEVIATION:
391 break;
392 case YANG_ENUM:
393 break;
394 case YANG_ERROR_APP_TAG:
395 break;
396 case YANG_ERROR_MESSAGE:
397 break;
398 case YANG_EXTENSION:
399 break;
400 case YANG_FEATURE:
401 break;
402 case YANG_FRACTION_DIGITS:
403 break;
404 case YANG_GROUPING:
405 break;
406 case YANG_IDENTITY:
407 break;
408 case YANG_IF_FEATURE:
409 break;
410 case YANG_IMPORT:
David Sedlák619db942019-07-03 14:47:30 +0200411 ret = yin_parse_import(xml_ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200412 break;
413 case YANG_INCLUDE:
414 break;
415 case YANG_INPUT:
416 break;
417 case YANG_KEY:
418 break;
419 case YANG_LEAF:
420 break;
421 case YANG_LEAF_LIST:
422 break;
423 case YANG_LENGTH:
424 break;
425 case YANG_LIST:
426 break;
427 case YANG_MANDATORY:
428 break;
429 case YANG_MAX_ELEMENTS:
430 break;
431 case YANG_MIN_ELEMENTS:
432 break;
433 case YANG_MODIFIER:
434 break;
435 case YANG_MODULE:
436 break;
437 case YANG_MUST:
438 break;
439 case YANG_NAMESPACE:
David Sedlák619db942019-07-03 14:47:30 +0200440 ret = yin_parse_simple_element(xml_ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
441 YIN_ARG_URI, YIN_ARG_MANDATORY, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200442 break;
443 case YANG_NOTIFICATION:
444 break;
445 case YANG_ORDERED_BY:
446 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200447 case YANG_OUTPUT:
448 break;
449 case YANG_PATH:
450 break;
451 case YANG_PATTERN:
452 break;
453 case YANG_POSITION:
454 break;
455 case YANG_PREFIX:
David Sedlák3ffbc522019-07-02 17:49:28 +0200456 ret = yin_parse_prefix(xml_ctx, subelem_attrs, (const char **)subelem_info_rec->dest, data, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200457 break;
458 case YANG_PRESENCE:
459 break;
460 case YANG_RANGE:
461 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200462 case YANG_REFINE:
463 break;
464 case YANG_REQUIRE_INSTANCE:
465 break;
466 case YANG_REVISION:
467 break;
468 case YANG_REVISION_DATE:
David Sedlák3ffbc522019-07-02 17:49:28 +0200469 ret = yin_parse_revision_date(xml_ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200470 break;
471 case YANG_RPC:
472 break;
473 case YANG_STATUS:
David Sedlák3ffbc522019-07-02 17:49:28 +0200474 ret = yin_parse_status(xml_ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200475 break;
476 case YANG_SUBMODULE:
477 break;
478 case YANG_TYPE:
479 break;
480 case YANG_TYPEDEF:
481 break;
482 case YANG_UNIQUE:
483 break;
484 case YANG_UNITS:
485 break;
486 case YANG_USES:
487 break;
488 case YANG_VALUE:
489 break;
490 case YANG_WHEN:
491 break;
492 case YANG_YANG_VERSION:
493 break;
494 case YANG_YIN_ELEMENT:
David Sedlák3ffbc522019-07-02 17:49:28 +0200495 ret = yin_parse_yin_element_element(xml_ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
496 break;
497 case YIN_TEXT:
David Sedlák72f449d2019-07-04 09:03:59 +0200498 ret = yin_parse_content(xml_ctx, NULL, 0, data, YIN_TEXT, (const char **)subelem_info_rec->dest, NULL);
David Sedlák3ffbc522019-07-02 17:49:28 +0200499 break;
500 case YIN_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +0200501 break;
502 default:
David Sedlák21f87cd2019-07-03 16:53:23 +0200503 LOGINT(xml_ctx->ctx);
504 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200505 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200506 LY_CHECK_GOTO(ret, cleanup);
David Sedlákd6e56892019-07-01 15:40:24 +0200507 FREE_ARRAY(xml_ctx, subelem_attrs, free_arg_rec);
508 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200509 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200510 }
David Sedlák21f87cd2019-07-03 16:53:23 +0200511 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(xml_ctx, subelem_info, subelem_info_size, current_element));
512
David Sedlákd6e56892019-07-01 15:40:24 +0200513 } else {
514 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200515 /* save text content, if text_content isn't set, it's just ignored */
516 if (text_content) {
517 if (dynamic) {
518 *text_content = lydict_insert_zc(xml_ctx->ctx, out);
519 if (!*text_content) {
520 free(out);
521 return LY_EMEM;
522 }
523 } else {
524 if (out_len == 0) {
525 *text_content = NULL;
526 } else {
527 *text_content = lydict_insert(xml_ctx->ctx, out, out_len);
528 }
529 }
530 }
David Sedlákd6e56892019-07-01 15:40:24 +0200531 /* load closing element */
532 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
533 }
534 }
535
536cleanup:
537 FREE_ARRAY(xml_ctx, subelem_attrs, free_arg_rec);
538 return ret;
539}
540
David Sedlák619db942019-07-03 14:47:30 +0200541LY_ERR
542yin_parse_simple_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
543 const char **value, enum YIN_ARGUMENT arg_type, uint8_t argument_flags, struct lysp_ext_instance **exts)
544{
545 LY_CHECK_RET(yin_parse_attribute(xml_ctx, &attrs, arg_type, value, argument_flags, kw));
546 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
547
548 return yin_parse_content(xml_ctx, subelems, 1, data, kw, NULL, exts);
549}
550
David Sedlák619db942019-07-03 14:47:30 +0200551LYEXT_SUBSTMT
552kw2lyext_substmt(enum yang_keyword kw)
David Sedlák3ffbc522019-07-02 17:49:28 +0200553{
554 switch (kw) {
David Sedlák619db942019-07-03 14:47:30 +0200555 case YANG_ARGUMENT:
556 return LYEXT_SUBSTMT_ARGUMENT;
557 case YANG_BASE:
558 return LYEXT_SUBSTMT_BASE;
559 case YANG_BELONGS_TO:
560 return LYEXT_SUBSTMT_BELONGSTO;
David Sedlák3ffbc522019-07-02 17:49:28 +0200561 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200562 return LYEXT_SUBSTMT_CONTACT;
563 case YANG_DEFAULT:
564 return LYEXT_SUBSTMT_DEFAULT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200565 case YANG_DESCRIPTION:
David Sedlák619db942019-07-03 14:47:30 +0200566 return LYEXT_SUBSTMT_DESCRIPTION;
567 case YANG_ERROR_APP_TAG:
568 return LYEXT_SUBSTMT_ERRTAG;
569 case YANG_ERROR_MESSAGE:
570 return LYEXT_SUBSTMT_ERRMSG;
571 case YANG_KEY:
572 return LYEXT_SUBSTMT_KEY;
573 case YANG_NAMESPACE:
574 return LYEXT_SUBSTMT_NAMESPACE;
575 case YANG_ORGANIZATION:
576 return LYEXT_SUBSTMT_ORGANIZATION;
577 case YANG_PATH:
578 return LYEXT_SUBSTMT_PATH;
579 case YANG_PREFIX:
580 return LYEXT_SUBSTMT_PREFIX;
581 case YANG_PRESENCE:
582 return LYEXT_SUBSTMT_PRESENCE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200583 case YANG_REFERENCE:
David Sedlák619db942019-07-03 14:47:30 +0200584 return LYEXT_SUBSTMT_REFERENCE;
585 case YANG_REVISION_DATE:
586 return LYEXT_SUBSTMT_REVISIONDATE;
587 case YANG_UNITS:
588 return LYEXT_SUBSTMT_UNITS;
589 case YANG_VALUE:
590 return LYEXT_SUBSTMT_VALUE;
591 case YANG_YANG_VERSION:
592 return LYEXT_SUBSTMT_VERSION;
593 case YANG_MODIFIER:
594 return LYEXT_SUBSTMT_MODIFIER;
595 case YANG_REQUIRE_INSTANCE:
596 return LYEXT_SUBSTMT_REQINSTANCE;
597 case YANG_YIN_ELEMENT:
598 return LYEXT_SUBSTMT_YINELEM;
599 case YANG_CONFIG:
600 return LYEXT_SUBSTMT_CONFIG;
601 case YANG_MANDATORY:
602 return LYEXT_SUBSTMT_MANDATORY;
603 case YANG_ORDERED_BY:
604 return LYEXT_SUBSTMT_ORDEREDBY;
605 case YANG_STATUS:
606 return LYEXT_SUBSTMT_STATUS;
607 case YANG_FRACTION_DIGITS:
608 return LYEXT_SUBSTMT_FRACDIGITS;
609 case YANG_MAX_ELEMENTS:
610 return LYEXT_SUBSTMT_MAX;
611 case YANG_MIN_ELEMENTS:
612 return LYEXT_SUBSTMT_MIN;
613 case YANG_POSITION:
614 return LYEXT_SUBSTMT_POSITION;
615 case YANG_UNIQUE:
616 return LYEXT_SUBSTMT_UNIQUE;
617 case YANG_IF_FEATURE:
618 return LYEXT_SUBSTMT_IFFEATURE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200619 default:
David Sedlák619db942019-07-03 14:47:30 +0200620 return LYEXT_SUBSTMT_SELF;
David Sedlák3ffbc522019-07-02 17:49:28 +0200621 }
622}
623
624/**
David Sedlák00250342019-06-21 14:19:39 +0200625 * @brief function to parse meta tags eg. elements with text element as child
626 *
627 * @param[in] xml_ctx Xml context.
David Sedlák3ffbc522019-07-02 17:49:28 +0200628 * @param[in] args Sized array of arguments of current element.
David Sedlák00250342019-06-21 14:19:39 +0200629 * @param[in,out] data Data to read from.
630 * @param[out] value Where the content of meta tag should be stored.
631 *
632 * @return LY_ERR values.
633 */
David Sedlák15a92662019-06-18 11:55:15 +0200634LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200635yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, enum yang_keyword elem_type,
636 const char **value, struct lysp_ext_instance **exts)
David Sedlák15a92662019-06-18 11:55:15 +0200637{
David Sedlák3ffbc522019-07-02 17:49:28 +0200638 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
David Sedlák21f87cd2019-07-03 16:53:23 +0200639
640 struct yin_subelement subelems[2] = {{YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST},
David Sedlák619db942019-07-03 14:47:30 +0200641 {YANG_CUSTOM, NULL, 0}};
David Sedlák15a92662019-06-18 11:55:15 +0200642
David Sedlák3ffbc522019-07-02 17:49:28 +0200643 return yin_parse_content(xml_ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlák0daba9a2019-03-22 14:07:49 +0100644}
645
David Sedlák3ffbc522019-07-02 17:49:28 +0200646LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200647yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, char *rev,
648 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200649{
650 LY_ERR ret = LY_SUCCESS;
David Sedlák3ffbc522019-07-02 17:49:28 +0200651 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200652 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200653
David Sedlák619db942019-07-03 14:47:30 +0200654 LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev, YIN_ARG_MANDATORY, YANG_REVISION_DATE));
David Sedlák81e04022019-04-05 15:05:46 +0200655 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200656 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
657
658 strcpy(rev, temp_rev);
659 lydict_remove(xml_ctx->ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200660
David Sedlák3ffbc522019-07-02 17:49:28 +0200661 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
662}
David Sedlák00250342019-06-21 14:19:39 +0200663
David Sedlák3ffbc522019-07-02 17:49:28 +0200664LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200665yin_parse_prefix(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **prefix, const char **data,
666 struct lysp_ext_instance **exts)
David Sedlák3ffbc522019-07-02 17:49:28 +0200667{
David Sedlák619db942019-07-03 14:47:30 +0200668 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
669 LY_CHECK_RET(yin_parse_attribute(xml_ctx, &attrs, YIN_ARG_VALUE, prefix, YIN_ARG_MANDATORY, YANG_PREFIX));
David Sedlák00250342019-06-21 14:19:39 +0200670
David Sedlák3ffbc522019-07-02 17:49:28 +0200671 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák81e04022019-04-05 15:05:46 +0200672}
David Sedlákcd0c9512019-03-29 13:23:06 +0100673
David Sedlákda63c082019-06-04 13:52:23 +0200674LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200675yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **import_args, const char **data, struct lysp_module *mod)
David Sedlák736fd0d2019-02-15 16:06:31 +0100676{
David Sedlák736fd0d2019-02-15 16:06:31 +0100677 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200678 /* allocate new element in sized array for import */
David Sedlák619db942019-07-03 14:47:30 +0200679 LY_ARRAY_NEW_RET(xml_ctx->ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200680
David Sedlák3ffbc522019-07-02 17:49:28 +0200681 struct yin_subelement subelems[5] = {{YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
682 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
683 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
684 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200685 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100686
David Sedláka7406952019-04-05 10:33:07 +0200687 /* parse import attributes */
David Sedlák619db942019-07-03 14:47:30 +0200688 LY_CHECK_RET(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name, YIN_ARG_MANDATORY, YANG_IMPORT));
David Sedlák3ffbc522019-07-02 17:49:28 +0200689 LY_CHECK_RET(yin_parse_content(xml_ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200690 /* check prefix validity */
691 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, mod->imports, mod->mod->prefix, &imp->prefix), LY_EVALID);
David Sedlák00250342019-06-21 14:19:39 +0200692
David Sedlák3ffbc522019-07-02 17:49:28 +0200693 return yin_parse_content(xml_ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100694}
695
David Sedlák11900c82019-06-18 16:29:12 +0200696LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200697yin_parse_status(struct lyxml_context *xml_ctx, struct yin_arg_record **status_args, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200698{
David Sedlák3ffbc522019-07-02 17:49:28 +0200699 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200700 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200701
David Sedlák11900c82019-06-18 16:29:12 +0200702 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200703 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200704 return LY_EVALID;
705 }
706
David Sedlák619db942019-07-03 14:47:30 +0200707 LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value, YIN_ARG_MANDATORY, YANG_STATUS));
David Sedlák11900c82019-06-18 16:29:12 +0200708 if (strcmp(value, "current") == 0) {
709 *flags |= LYS_STATUS_CURR;
710 } else if (strcmp(value, "deprecated") == 0) {
711 *flags |= LYS_STATUS_DEPRC;
712 } else if (strcmp(value, "obsolete") == 0) {
713 *flags |= LYS_STATUS_OBSLT;
714 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200715 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200716 lydict_remove(xml_ctx->ctx, value);
717 return LY_EVALID;
718 }
719 lydict_remove(xml_ctx->ctx, value);
720
David Sedlák3ffbc522019-07-02 17:49:28 +0200721 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +0200722}
723
David Sedlák11900c82019-06-18 16:29:12 +0200724LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200725yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data,
726 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +0200727{
David Sedlák3ffbc522019-07-02 17:49:28 +0200728 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200729 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +0200730
David Sedlák619db942019-07-03 14:47:30 +0200731 LY_CHECK_RET(yin_parse_attribute(xml_ctx, &attrs, YIN_ARG_VALUE, &temp_val, YIN_ARG_MANDATORY, YANG_YIN_ELEMENT));
David Sedlák2721d3d2019-06-21 15:37:41 +0200732 if (strcmp(temp_val, "true") == 0) {
733 *flags |= LYS_YINELEM_TRUE;
734 } else if (strcmp(temp_val, "false") == 0) {
735 *flags |= LYS_YINELEM_FALSE;
736 } else {
737 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
738 lydict_remove(xml_ctx->ctx, temp_val);
739 return LY_EVALID;
740 }
741 lydict_remove(xml_ctx->ctx, temp_val);
742
David Sedlák3ffbc522019-07-02 17:49:28 +0200743 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +0200744}
745
746LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200747yin_parse_extension_instance(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, const char *ext_name,
David Sedlák619db942019-07-03 14:47:30 +0200748 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +0200749{
750 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +0200751 char *out;
752 const char *name, *prefix;
753 size_t out_len, prefix_len, name_len;
754 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200755 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +0200756 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
757 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200758
759 LY_ARRAY_NEW_RET(xml_ctx->ctx, *exts, e, LY_EMEM);
760
761 e->yin = 0;
762 /* store name and insubstmt info */
763 e->name = lydict_insert(xml_ctx->ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +0200764 e->insubstmt = subelem;
765 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200766 e->yin |= LYS_YIN;
767
David Sedlákb1a78352019-06-28 16:16:29 +0200768 /* store attributes as subelements */
769 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
770 if (!iter->prefix) {
771 new_subelem = calloc(1, sizeof(*new_subelem));
772 if (!e->child) {
773 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200774 } else {
David Sedlákb1a78352019-06-28 16:16:29 +0200775 last_subelem->next = new_subelem;
776 }
777 last_subelem = new_subelem;
778
779 last_subelem->flags |= LYS_YIN_ATTR;
780 last_subelem->stmt = lydict_insert(xml_ctx->ctx, iter->name, iter->name_len);
781 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM);
782 if (iter->dynamic_content) {
783 last_subelem->arg = lydict_insert_zc(xml_ctx->ctx, iter->content);
784 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM);
785 } else {
786 last_subelem->arg = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
787 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200788 }
789 }
790 }
David Sedlák1e2cdd02019-06-27 14:17:43 +0200791
David Sedlákf250ecf2019-07-01 11:02:05 +0200792 /* parse subelements */
David Sedlákb1a78352019-06-28 16:16:29 +0200793 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
794 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
795 if (ret == LY_EINVAL) {
796 while (xml_ctx->status == LYXML_ELEMENT) {
797 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
798 if (!name) {
799 /* end of extension instance reached */
800 break;
801 }
David Sedlákf250ecf2019-07-01 11:02:05 +0200802 LY_CHECK_RET(yin_parse_element_generic(xml_ctx, name, name_len, prefix, prefix_len, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +0200803 if (!e->child) {
804 e->child = new_subelem;
805 } else {
806 last_subelem->next = new_subelem;
807 }
808 last_subelem = new_subelem;
809 }
810 }
811 } else {
812 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
813 LY_CHECK_RET(name, LY_EINVAL);
814 }
815
816 return LY_SUCCESS;
817}
818
819LY_ERR
David Sedlákf250ecf2019-07-01 11:02:05 +0200820yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix,
821 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +0200822{
823 LY_ERR ret = LY_SUCCESS;
824 const char *temp_prefix, *temp_name;
825 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200826 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +0200827 int dynamic;
828 struct yin_arg_record *subelem_args = NULL;
829 struct lysp_stmt *last = NULL, *new = NULL;
830
831 /* allocate new structure for element */
832 *element = calloc(1, sizeof(**element));
833 (*element)->stmt = lydict_insert(xml_ctx->ctx, name, name_len);
834 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM);
835
836 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +0200837 /* load attributes */
David Sedlákb1a78352019-06-28 16:16:29 +0200838 while(xml_ctx->status == LYXML_ATTRIBUTE) {
839 /* add new element to linked-list */
840 new = calloc(1, sizeof(*last));
841 LY_CHECK_ERR_GOTO(ret, LOGMEM(xml_ctx->ctx), err);
842 if (!(*element)->child) {
843 /* save first */
844 (*element)->child = new;
845 } else {
846 last->next = new;
847 }
848 last = new;
849
850 last->flags |= LYS_YIN_ATTR;
851 ret = lyxml_get_attribute(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
852 LY_CHECK_GOTO(ret, err);
853 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
854 LY_CHECK_GOTO(ret, err);
855 last->stmt = lydict_insert(xml_ctx->ctx, temp_name, temp_name_len);
856 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
857 /* attributes with prefix are ignored */
858 if (!temp_prefix) {
859 if (dynamic) {
860 last->arg = lydict_insert_zc(xml_ctx->ctx, out);
861 if (!last->arg) {
862 free(out);
863 LOGMEM(xml_ctx->ctx);
864 ret = LY_EMEM;
865 goto err;
866 }
867 } else {
868 last->arg = lydict_insert(xml_ctx->ctx, out, out_len);
869 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
870 }
871 }
872 }
873
874 /* parse content of element */
875 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
876 if (ret == LY_EINVAL) {
877 while (xml_ctx->status == LYXML_ELEMENT) {
878 /* parse subelements */
David Sedlákf250ecf2019-07-01 11:02:05 +0200879 ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +0200880 LY_CHECK_GOTO(ret, err);
881 if (!name) {
882 /* end of element reached */
883 break;
884 }
David Sedlákf250ecf2019-07-01 11:02:05 +0200885 ret = yin_parse_element_generic(xml_ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, data, &last->next);
David Sedlákb1a78352019-06-28 16:16:29 +0200886 LY_CHECK_GOTO(ret, err);
887 last = last->next;
888 }
889 } else {
890 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +0200891 if (out_len != 0) {
892 if (dynamic) {
893 (*element)->arg = lydict_insert_zc(xml_ctx->ctx, out);
894 if (!(*element)->arg) {
895 free(out);
896 LOGMEM(xml_ctx->ctx);
897 ret = LY_EMEM;
898 goto err;
899 }
900 } else {
901 (*element)->arg = lydict_insert(xml_ctx->ctx, out, out_len);
902 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +0200903 }
David Sedlákb1a78352019-06-28 16:16:29 +0200904 }
905 /* read closing tag */
906 ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
907 LY_CHECK_GOTO(ret, err);
908 }
909
910 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
911 return LY_SUCCESS;
912
913err:
914 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200915 return ret;
916}
917
David Sedlák3ffbc522019-07-02 17:49:28 +0200918/**
919 * @brief Parse argument element.
920 *
921 * @param[in,out] xml_ctx Xml context.
922 * @param[in] attrs Attributes of this element.
923 * @param[in,out] data Data to read from, always moved to currently handled character.
924 * @param[in,out] arg_meta Meta information about destionation af prased data.
925 * @param[in,out] exts Extension instance to add to.
926 *
927 * @return LY_ERR values.
928 */
David Sedlák1e2cdd02019-06-27 14:17:43 +0200929LY_ERR
David Sedlák9494eb22019-06-21 16:06:53 +0200930yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +0200931 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +0200932{
David Sedlák3ffbc522019-07-02 17:49:28 +0200933 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200934 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +0200935
David Sedlák619db942019-07-03 14:47:30 +0200936 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, arg_meta->argument, YIN_ARG_MANDATORY, YANG_ARGUMENT));
David Sedlák9494eb22019-06-21 16:06:53 +0200937
David Sedlák3ffbc522019-07-02 17:49:28 +0200938 return yin_parse_content(xml_ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +0200939}
940
941LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200942yin_parse_extension(struct lyxml_context *xml_ctx, struct yin_arg_record **extension_args, const char **data, struct lysp_ext **extensions)
David Sedlák11900c82019-06-18 16:29:12 +0200943{
David Sedlák11900c82019-06-18 16:29:12 +0200944 struct lysp_ext *ex;
David Sedlák3ffbc522019-07-02 17:49:28 +0200945 LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM);
David Sedlák619db942019-07-03 14:47:30 +0200946 LY_CHECK_RET(yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name, YIN_ARG_MANDATORY, YANG_EXTENSION));
947
David Sedlák3ffbc522019-07-02 17:49:28 +0200948 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
949 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
950 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
951 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
952 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200953 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +0200954
David Sedlák3ffbc522019-07-02 17:49:28 +0200955 return yin_parse_content(xml_ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +0200956}
957
David Sedlák00250342019-06-21 14:19:39 +0200958/**
959 * @brief Parse module substatements.
960 *
961 * @param[in] xml_ctx Xml context.
962 * @param[in,out] data Data to read from.
963 * @param[out] mod Parsed module structure.
964 *
965 * @return LY_ERR values.
966 */
967LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200968yin_parse_mod(struct lyxml_context *xml_ctx, struct yin_arg_record **mod_args, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200969{
David Sedlák619db942019-07-03 14:47:30 +0200970 struct yin_subelement subelems[8] = {{YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
971 {YANG_IMPORT, *mod, 0},
972 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
973 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
974 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
975 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
976 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
977 /* TODO add YANG_EXTENSION */
978 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +0200979
David Sedlákf3b24f62018-11-02 10:40:47 +0100980
David Sedlák619db942019-07-03 14:47:30 +0200981 LY_CHECK_RET(yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name, YIN_ARG_MANDATORY, YANG_MODULE));
David Sedlák4a4c0722018-11-26 17:03:10 +0100982
David Sedlák619db942019-07-03 14:47:30 +0200983 return yin_parse_content(xml_ctx, subelems, 8, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +0200984}
985
986LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100987yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200988{
David Sedláke4889912018-11-02 09:52:40 +0100989 LY_ERR ret = LY_SUCCESS;
990 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200991 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200992 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100993 struct lysp_module *mod_p = NULL;
994 const char *prefix, *name;
995 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200996 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200997
David Sedlák3017da42019-02-15 09:48:04 +0100998 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200999 memset(&parser_ctx, 0, sizeof parser_ctx);
1000 xml_ctx->ctx = ctx;
1001 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +01001002
David Sedlák3017da42019-02-15 09:48:04 +01001003 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +02001004 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001005 LY_CHECK_GOTO(ret, cleanup);
1006 ret = yin_load_attributes(xml_ctx, &data, &args);
1007 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +02001008 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +01001009 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001010 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1011 ret = LY_EINVAL;
1012 goto cleanup;
1013 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02001014 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +02001015 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001016 ret = LY_EVALID;
1017 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001018 }
1019
David Sedlák3017da42019-02-15 09:48:04 +01001020 /* allocate module */
1021 mod_p = calloc(1, sizeof *mod_p);
1022 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1023 mod_p->mod = mod;
1024 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001025
David Sedlák00250342019-06-21 14:19:39 +02001026 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +02001027 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001028 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001029
David Sedlák3017da42019-02-15 09:48:04 +01001030 mod_p->parsing = 0;
1031 mod->parsed = mod_p;
1032
1033cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001034 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001035 lysp_module_free(mod_p);
1036 }
David Sedlák00250342019-06-21 14:19:39 +02001037 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +02001038 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001039 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001040}