blob: a805f516941da5a8241b4f55b5dacdcb20e7eb34 [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:
498 ret = yin_parse_text_element(xml_ctx, data, (const char **)subelem_info_rec->dest);
499 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ák0daba9a2019-03-22 14:07:49 +0100551LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200552yin_parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100553{
David Sedlák619db942019-07-03 14:47:30 +0200554 return yin_parse_content(xml_ctx, NULL, 0, data, YIN_TEXT, value, NULL);
David Sedlák15a92662019-06-18 11:55:15 +0200555}
556
David Sedlák619db942019-07-03 14:47:30 +0200557LYEXT_SUBSTMT
558kw2lyext_substmt(enum yang_keyword kw)
David Sedlák3ffbc522019-07-02 17:49:28 +0200559{
560 switch (kw) {
David Sedlák619db942019-07-03 14:47:30 +0200561 case YANG_ARGUMENT:
562 return LYEXT_SUBSTMT_ARGUMENT;
563 case YANG_BASE:
564 return LYEXT_SUBSTMT_BASE;
565 case YANG_BELONGS_TO:
566 return LYEXT_SUBSTMT_BELONGSTO;
David Sedlák3ffbc522019-07-02 17:49:28 +0200567 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200568 return LYEXT_SUBSTMT_CONTACT;
569 case YANG_DEFAULT:
570 return LYEXT_SUBSTMT_DEFAULT;
David Sedlák3ffbc522019-07-02 17:49:28 +0200571 case YANG_DESCRIPTION:
David Sedlák619db942019-07-03 14:47:30 +0200572 return LYEXT_SUBSTMT_DESCRIPTION;
573 case YANG_ERROR_APP_TAG:
574 return LYEXT_SUBSTMT_ERRTAG;
575 case YANG_ERROR_MESSAGE:
576 return LYEXT_SUBSTMT_ERRMSG;
577 case YANG_KEY:
578 return LYEXT_SUBSTMT_KEY;
579 case YANG_NAMESPACE:
580 return LYEXT_SUBSTMT_NAMESPACE;
581 case YANG_ORGANIZATION:
582 return LYEXT_SUBSTMT_ORGANIZATION;
583 case YANG_PATH:
584 return LYEXT_SUBSTMT_PATH;
585 case YANG_PREFIX:
586 return LYEXT_SUBSTMT_PREFIX;
587 case YANG_PRESENCE:
588 return LYEXT_SUBSTMT_PRESENCE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200589 case YANG_REFERENCE:
David Sedlák619db942019-07-03 14:47:30 +0200590 return LYEXT_SUBSTMT_REFERENCE;
591 case YANG_REVISION_DATE:
592 return LYEXT_SUBSTMT_REVISIONDATE;
593 case YANG_UNITS:
594 return LYEXT_SUBSTMT_UNITS;
595 case YANG_VALUE:
596 return LYEXT_SUBSTMT_VALUE;
597 case YANG_YANG_VERSION:
598 return LYEXT_SUBSTMT_VERSION;
599 case YANG_MODIFIER:
600 return LYEXT_SUBSTMT_MODIFIER;
601 case YANG_REQUIRE_INSTANCE:
602 return LYEXT_SUBSTMT_REQINSTANCE;
603 case YANG_YIN_ELEMENT:
604 return LYEXT_SUBSTMT_YINELEM;
605 case YANG_CONFIG:
606 return LYEXT_SUBSTMT_CONFIG;
607 case YANG_MANDATORY:
608 return LYEXT_SUBSTMT_MANDATORY;
609 case YANG_ORDERED_BY:
610 return LYEXT_SUBSTMT_ORDEREDBY;
611 case YANG_STATUS:
612 return LYEXT_SUBSTMT_STATUS;
613 case YANG_FRACTION_DIGITS:
614 return LYEXT_SUBSTMT_FRACDIGITS;
615 case YANG_MAX_ELEMENTS:
616 return LYEXT_SUBSTMT_MAX;
617 case YANG_MIN_ELEMENTS:
618 return LYEXT_SUBSTMT_MIN;
619 case YANG_POSITION:
620 return LYEXT_SUBSTMT_POSITION;
621 case YANG_UNIQUE:
622 return LYEXT_SUBSTMT_UNIQUE;
623 case YANG_IF_FEATURE:
624 return LYEXT_SUBSTMT_IFFEATURE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200625 default:
David Sedlák619db942019-07-03 14:47:30 +0200626 return LYEXT_SUBSTMT_SELF;
David Sedlák3ffbc522019-07-02 17:49:28 +0200627 }
628}
629
630/**
David Sedlák00250342019-06-21 14:19:39 +0200631 * @brief function to parse meta tags eg. elements with text element as child
632 *
633 * @param[in] xml_ctx Xml context.
David Sedlák3ffbc522019-07-02 17:49:28 +0200634 * @param[in] args Sized array of arguments of current element.
David Sedlák00250342019-06-21 14:19:39 +0200635 * @param[in,out] data Data to read from.
636 * @param[out] value Where the content of meta tag should be stored.
637 *
638 * @return LY_ERR values.
639 */
David Sedlák15a92662019-06-18 11:55:15 +0200640LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200641yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, enum yang_keyword elem_type,
642 const char **value, struct lysp_ext_instance **exts)
David Sedlák15a92662019-06-18 11:55:15 +0200643{
David Sedlák3ffbc522019-07-02 17:49:28 +0200644 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 +0200645
646 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 +0200647 {YANG_CUSTOM, NULL, 0}};
David Sedlák15a92662019-06-18 11:55:15 +0200648
David Sedlák3ffbc522019-07-02 17:49:28 +0200649 return yin_parse_content(xml_ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlák0daba9a2019-03-22 14:07:49 +0100650}
651
David Sedlák3ffbc522019-07-02 17:49:28 +0200652LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200653yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, char *rev,
654 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200655{
656 LY_ERR ret = LY_SUCCESS;
David Sedlák3ffbc522019-07-02 17:49:28 +0200657 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200658 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200659
David Sedlák619db942019-07-03 14:47:30 +0200660 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 +0200661 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200662 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
663
664 strcpy(rev, temp_rev);
665 lydict_remove(xml_ctx->ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200666
David Sedlák3ffbc522019-07-02 17:49:28 +0200667 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
668}
David Sedlák00250342019-06-21 14:19:39 +0200669
David Sedlák3ffbc522019-07-02 17:49:28 +0200670LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200671yin_parse_prefix(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **prefix, const char **data,
672 struct lysp_ext_instance **exts)
David Sedlák3ffbc522019-07-02 17:49:28 +0200673{
David Sedlák619db942019-07-03 14:47:30 +0200674 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
675 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 +0200676
David Sedlák3ffbc522019-07-02 17:49:28 +0200677 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák81e04022019-04-05 15:05:46 +0200678}
David Sedlákcd0c9512019-03-29 13:23:06 +0100679
David Sedlákda63c082019-06-04 13:52:23 +0200680LY_ERR
David Sedlák619db942019-07-03 14:47:30 +0200681yin_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 +0100682{
David Sedlák736fd0d2019-02-15 16:06:31 +0100683 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200684 /* allocate new element in sized array for import */
David Sedlák619db942019-07-03 14:47:30 +0200685 LY_ARRAY_NEW_RET(xml_ctx->ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200686
David Sedlák3ffbc522019-07-02 17:49:28 +0200687 struct yin_subelement subelems[5] = {{YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
688 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
689 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
690 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200691 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100692
David Sedláka7406952019-04-05 10:33:07 +0200693 /* parse import attributes */
David Sedlák619db942019-07-03 14:47:30 +0200694 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 +0200695 LY_CHECK_RET(yin_parse_content(xml_ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200696 /* check prefix validity */
697 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 +0200698
David Sedlák3ffbc522019-07-02 17:49:28 +0200699 return yin_parse_content(xml_ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100700}
701
David Sedlák11900c82019-06-18 16:29:12 +0200702LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200703yin_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 +0200704{
David Sedlák3ffbc522019-07-02 17:49:28 +0200705 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200706 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200707
David Sedlák11900c82019-06-18 16:29:12 +0200708 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200709 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200710 return LY_EVALID;
711 }
712
David Sedlák619db942019-07-03 14:47:30 +0200713 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 +0200714 if (strcmp(value, "current") == 0) {
715 *flags |= LYS_STATUS_CURR;
716 } else if (strcmp(value, "deprecated") == 0) {
717 *flags |= LYS_STATUS_DEPRC;
718 } else if (strcmp(value, "obsolete") == 0) {
719 *flags |= LYS_STATUS_OBSLT;
720 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200721 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200722 lydict_remove(xml_ctx->ctx, value);
723 return LY_EVALID;
724 }
725 lydict_remove(xml_ctx->ctx, value);
726
David Sedlák3ffbc522019-07-02 17:49:28 +0200727 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +0200728}
729
David Sedlák11900c82019-06-18 16:29:12 +0200730LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200731yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record *attrs, const char **data,
732 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +0200733{
David Sedlák3ffbc522019-07-02 17:49:28 +0200734 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200735 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +0200736
David Sedlák619db942019-07-03 14:47:30 +0200737 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 +0200738 if (strcmp(temp_val, "true") == 0) {
739 *flags |= LYS_YINELEM_TRUE;
740 } else if (strcmp(temp_val, "false") == 0) {
741 *flags |= LYS_YINELEM_FALSE;
742 } else {
743 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
744 lydict_remove(xml_ctx->ctx, temp_val);
745 return LY_EVALID;
746 }
747 lydict_remove(xml_ctx->ctx, temp_val);
748
David Sedlák3ffbc522019-07-02 17:49:28 +0200749 return yin_parse_content(xml_ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +0200750}
751
752LY_ERR
David Sedlák3ffbc522019-07-02 17:49:28 +0200753yin_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 +0200754 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +0200755{
756 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +0200757 char *out;
758 const char *name, *prefix;
759 size_t out_len, prefix_len, name_len;
760 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200761 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +0200762 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
763 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200764
765 LY_ARRAY_NEW_RET(xml_ctx->ctx, *exts, e, LY_EMEM);
766
767 e->yin = 0;
768 /* store name and insubstmt info */
769 e->name = lydict_insert(xml_ctx->ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +0200770 e->insubstmt = subelem;
771 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200772 e->yin |= LYS_YIN;
773
David Sedlákb1a78352019-06-28 16:16:29 +0200774 /* store attributes as subelements */
775 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
776 if (!iter->prefix) {
777 new_subelem = calloc(1, sizeof(*new_subelem));
778 if (!e->child) {
779 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200780 } else {
David Sedlákb1a78352019-06-28 16:16:29 +0200781 last_subelem->next = new_subelem;
782 }
783 last_subelem = new_subelem;
784
785 last_subelem->flags |= LYS_YIN_ATTR;
786 last_subelem->stmt = lydict_insert(xml_ctx->ctx, iter->name, iter->name_len);
787 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM);
788 if (iter->dynamic_content) {
789 last_subelem->arg = lydict_insert_zc(xml_ctx->ctx, iter->content);
790 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM);
791 } else {
792 last_subelem->arg = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
793 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200794 }
795 }
796 }
David Sedlák1e2cdd02019-06-27 14:17:43 +0200797
David Sedlákf250ecf2019-07-01 11:02:05 +0200798 /* parse subelements */
David Sedlákb1a78352019-06-28 16:16:29 +0200799 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
800 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
801 if (ret == LY_EINVAL) {
802 while (xml_ctx->status == LYXML_ELEMENT) {
803 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
804 if (!name) {
805 /* end of extension instance reached */
806 break;
807 }
David Sedlákf250ecf2019-07-01 11:02:05 +0200808 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 +0200809 if (!e->child) {
810 e->child = new_subelem;
811 } else {
812 last_subelem->next = new_subelem;
813 }
814 last_subelem = new_subelem;
815 }
816 }
817 } else {
818 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
819 LY_CHECK_RET(name, LY_EINVAL);
820 }
821
822 return LY_SUCCESS;
823}
824
825LY_ERR
David Sedlákf250ecf2019-07-01 11:02:05 +0200826yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix,
827 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +0200828{
829 LY_ERR ret = LY_SUCCESS;
830 const char *temp_prefix, *temp_name;
831 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200832 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +0200833 int dynamic;
834 struct yin_arg_record *subelem_args = NULL;
835 struct lysp_stmt *last = NULL, *new = NULL;
836
837 /* allocate new structure for element */
838 *element = calloc(1, sizeof(**element));
839 (*element)->stmt = lydict_insert(xml_ctx->ctx, name, name_len);
840 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM);
841
842 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +0200843 /* load attributes */
David Sedlákb1a78352019-06-28 16:16:29 +0200844 while(xml_ctx->status == LYXML_ATTRIBUTE) {
845 /* add new element to linked-list */
846 new = calloc(1, sizeof(*last));
847 LY_CHECK_ERR_GOTO(ret, LOGMEM(xml_ctx->ctx), err);
848 if (!(*element)->child) {
849 /* save first */
850 (*element)->child = new;
851 } else {
852 last->next = new;
853 }
854 last = new;
855
856 last->flags |= LYS_YIN_ATTR;
857 ret = lyxml_get_attribute(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
858 LY_CHECK_GOTO(ret, err);
859 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
860 LY_CHECK_GOTO(ret, err);
861 last->stmt = lydict_insert(xml_ctx->ctx, temp_name, temp_name_len);
862 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
863 /* attributes with prefix are ignored */
864 if (!temp_prefix) {
865 if (dynamic) {
866 last->arg = lydict_insert_zc(xml_ctx->ctx, out);
867 if (!last->arg) {
868 free(out);
869 LOGMEM(xml_ctx->ctx);
870 ret = LY_EMEM;
871 goto err;
872 }
873 } else {
874 last->arg = lydict_insert(xml_ctx->ctx, out, out_len);
875 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
876 }
877 }
878 }
879
880 /* parse content of element */
881 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
882 if (ret == LY_EINVAL) {
883 while (xml_ctx->status == LYXML_ELEMENT) {
884 /* parse subelements */
David Sedlákf250ecf2019-07-01 11:02:05 +0200885 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 +0200886 LY_CHECK_GOTO(ret, err);
887 if (!name) {
888 /* end of element reached */
889 break;
890 }
David Sedlákf250ecf2019-07-01 11:02:05 +0200891 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 +0200892 LY_CHECK_GOTO(ret, err);
893 last = last->next;
894 }
895 } else {
896 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +0200897 if (out_len != 0) {
898 if (dynamic) {
899 (*element)->arg = lydict_insert_zc(xml_ctx->ctx, out);
900 if (!(*element)->arg) {
901 free(out);
902 LOGMEM(xml_ctx->ctx);
903 ret = LY_EMEM;
904 goto err;
905 }
906 } else {
907 (*element)->arg = lydict_insert(xml_ctx->ctx, out, out_len);
908 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +0200909 }
David Sedlákb1a78352019-06-28 16:16:29 +0200910 }
911 /* read closing tag */
912 ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
913 LY_CHECK_GOTO(ret, err);
914 }
915
916 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
917 return LY_SUCCESS;
918
919err:
920 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200921 return ret;
922}
923
David Sedlák3ffbc522019-07-02 17:49:28 +0200924/**
925 * @brief Parse argument element.
926 *
927 * @param[in,out] xml_ctx Xml context.
928 * @param[in] attrs Attributes of this element.
929 * @param[in,out] data Data to read from, always moved to currently handled character.
930 * @param[in,out] arg_meta Meta information about destionation af prased data.
931 * @param[in,out] exts Extension instance to add to.
932 *
933 * @return LY_ERR values.
934 */
David Sedlák1e2cdd02019-06-27 14:17:43 +0200935LY_ERR
David Sedlák9494eb22019-06-21 16:06:53 +0200936yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +0200937 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +0200938{
David Sedlák3ffbc522019-07-02 17:49:28 +0200939 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200940 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +0200941
David Sedlák619db942019-07-03 14:47:30 +0200942 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 +0200943
David Sedlák3ffbc522019-07-02 17:49:28 +0200944 return yin_parse_content(xml_ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +0200945}
946
947LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200948yin_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 +0200949{
David Sedlák11900c82019-06-18 16:29:12 +0200950 struct lysp_ext *ex;
David Sedlák3ffbc522019-07-02 17:49:28 +0200951 LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM);
David Sedlák619db942019-07-03 14:47:30 +0200952 LY_CHECK_RET(yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name, YIN_ARG_MANDATORY, YANG_EXTENSION));
953
David Sedlák3ffbc522019-07-02 17:49:28 +0200954 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
955 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
956 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
957 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
958 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200959 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +0200960
David Sedlák3ffbc522019-07-02 17:49:28 +0200961 return yin_parse_content(xml_ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +0200962}
963
David Sedlák00250342019-06-21 14:19:39 +0200964/**
965 * @brief Parse module substatements.
966 *
967 * @param[in] xml_ctx Xml context.
968 * @param[in,out] data Data to read from.
969 * @param[out] mod Parsed module structure.
970 *
971 * @return LY_ERR values.
972 */
973LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200974yin_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 +0200975{
David Sedlák619db942019-07-03 14:47:30 +0200976 struct yin_subelement subelems[8] = {{YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
977 {YANG_IMPORT, *mod, 0},
978 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
979 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
980 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
981 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
982 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
983 /* TODO add YANG_EXTENSION */
984 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +0200985
David Sedlákf3b24f62018-11-02 10:40:47 +0100986
David Sedlák619db942019-07-03 14:47:30 +0200987 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 +0100988
David Sedlák619db942019-07-03 14:47:30 +0200989 return yin_parse_content(xml_ctx, subelems, 8, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +0200990}
991
992LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100993yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200994{
David Sedláke4889912018-11-02 09:52:40 +0100995 LY_ERR ret = LY_SUCCESS;
996 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200997 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200998 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100999 struct lysp_module *mod_p = NULL;
1000 const char *prefix, *name;
1001 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +02001002 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001003
David Sedlák3017da42019-02-15 09:48:04 +01001004 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +02001005 memset(&parser_ctx, 0, sizeof parser_ctx);
1006 xml_ctx->ctx = ctx;
1007 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +01001008
David Sedlák3017da42019-02-15 09:48:04 +01001009 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +02001010 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001011 LY_CHECK_GOTO(ret, cleanup);
1012 ret = yin_load_attributes(xml_ctx, &data, &args);
1013 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +02001014 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +01001015 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001016 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1017 ret = LY_EINVAL;
1018 goto cleanup;
1019 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02001020 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +02001021 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001022 ret = LY_EVALID;
1023 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001024 }
1025
David Sedlák3017da42019-02-15 09:48:04 +01001026 /* allocate module */
1027 mod_p = calloc(1, sizeof *mod_p);
1028 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1029 mod_p->mod = mod;
1030 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001031
David Sedlák00250342019-06-21 14:19:39 +02001032 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +02001033 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001034 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001035
David Sedlák3017da42019-02-15 09:48:04 +01001036 mod_p->parsing = 0;
1037 mod->parsed = mod_p;
1038
1039cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001040 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001041 lysp_module_free(mod_p);
1042 }
David Sedlák00250342019-06-21 14:19:39 +02001043 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +02001044 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001045 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001046}