blob: 0b497419ae4de956d73751e041e11ac1e68647d7 [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ák3b4db242018-10-19 16:11:01 +020016#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020019#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020020
David Sedlákf824ad52018-10-14 23:58:15 +020021#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020022#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020023#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "tree.h"
25#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020026#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020027#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020028
David Sedlák2b214ac2019-06-06 16:11:03 +020029/**
30 * @brief check if given string is URI of yin namespace.
31 * @param ns Namespace URI to check.
32 *
33 * @return true if ns equals YIN_NS_URI false otherwise.
34 */
35#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
36
David Sedlák9bc65872019-06-24 20:21:08 +020037/**
38 * @brief loop over all subelements, automatically skip useles data and
39 * unify inconsistencies in xml parser status when loading full vs self closing tags.
40 * Use YIN_READ_SUBELEMS_END at the end.
41 *
42 * @param[in,out] CTX Xml parser context.
43 * @param[in,out] DATA Data to read from, always moved to currently handeled character.
44 * @param[in,out] RET Name of variable to store return values.
45 * @param[in,out] CLEANUP_LABEL Goto label that will be used in case of error.
46 * @param[in,out] KW Name of variable to store type of current subelement.
47 * @param[in] TMP Name of variable to store temporary values (type yin_arg_record).
48 * @param[in] SUBELEM_ARGS Name of variable to store array of subelements.
49 */
50#define YIN_READ_SUBELEMS_START(CTX, DATA, RET, CLEANUP_LABEL, KW, TMP, SUBELEM_ARGS)\
51if (xml_ctx->status == LYXML_ELEM_CONTENT) {\
52 ret = lyxml_get_string(CTX, DATA, &TMP.content, &TMP.content_len, &TMP.content, &TMP.content_len, &TMP.dynamic_content);\
53 /* unknown element text content is ignored */\
54 if (ret == LY_EINVAL) {\
55 while (CTX->status == LYXML_ELEMENT) {\
56 RET = lyxml_get_element(CTX, DATA, &TMP.prefix, &TMP.prefix_len, &TMP.name, &TMP.name_len);\
57 LY_CHECK_GOTO(RET, CLEANUP_LABEL);\
58 RET = yin_load_attributes(CTX, DATA, &SUBELEM_ARGS);\
59 LY_CHECK_GOTO(RET, CLEANUP_LABEL);\
60 KW = yin_match_keyword(CTX, TMP.name, TMP.name_len, TMP.prefix, TMP.prefix_len);\
61 if (!TMP.name) {\
David Sedlákb1a78352019-06-28 16:16:29 +020062 /* end of element reached */\
David Sedlák9bc65872019-06-24 20:21:08 +020063 break;\
64 }
65/**
66 * Closing part of macro YIN_READ_SUBELEM_START
67 *
68 * @param[in] CTX Xml parser context.
69 * @param[in,out] DATA Data to read from, always moved to currently handeled character.
70 * @param[in,out] TMP Name of variable to store temporary values (type yin_arg_record).
71 */
72#define YIN_READ_SUBELEMS_END(CTX, DATA, TMP)\
73 }\
74 } else {\
75 /* load closing element */\
David Sedlák1e2cdd02019-06-27 14:17:43 +020076 LY_CHECK_RET(lyxml_get_element(CTX, DATA, &TMP.prefix, &TMP.prefix_len, &TMP.name, &TMP.name_len));\
77 LY_CHECK_RET(TMP.name, LY_EINVAL);\
David Sedlák9bc65872019-06-24 20:21:08 +020078 }\
79}
80
David Sedlákf6251182019-06-06 10:22:13 +020081const char *const yin_attr_list[] = {
82 [YIN_ARG_NAME] = "name",
83 [YIN_ARG_TARGET_NODE] = "target-node",
84 [YIN_ARG_MODULE] = "module",
85 [YIN_ARG_VALUE] = "value",
86 [YIN_ARG_TEXT] = "text",
87 [YIN_ARG_CONDITION] = "condition",
88 [YIN_ARG_URI] = "uri",
89 [YIN_ARG_DATE] = "date",
90 [YIN_ARG_TAG] = "tag",
91 [YIN_ARG_XMLNS] = "xmlns",
92};
93
David Sedlák1bccdfa2019-06-17 15:55:27 +020094enum yang_keyword
David Sedlák8f7a1172019-06-20 14:42:18 +020095yin_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 +020096{
David Sedlák8f7a1172019-06-20 14:42:18 +020097 const char *start = NULL;
98 enum yang_keyword kw = YANG_NONE;
99 const struct lyxml_ns *ns = NULL;
100
101 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +0200102 return YANG_NONE;
103 }
104
David Sedlák8f7a1172019-06-20 14:42:18 +0200105 ns = lyxml_ns_get(xml_ctx, prefix, prefix_len);
106 if (ns) {
107 if (!IS_YIN_NS(ns->uri)) {
108 return YANG_CUSTOM;
109 }
110 } else {
111 /* elements without namespace are automatically unknown */
112 return YANG_NONE;
113 }
David Sedlák1bccdfa2019-06-17 15:55:27 +0200114
David Sedlák8f7a1172019-06-20 14:42:18 +0200115
116 start = name;
117 kw = lysp_match_kw(NULL, &name);
118
119 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +0200120 return kw;
121 } else {
122 return YANG_NONE;
123 }
124}
125
David Sedlák872c7b42018-10-26 13:15:20 +0200126enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +0200127yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200128{
David Sedláka7406952019-04-05 10:33:07 +0200129 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200130 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200131 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200132
David Sedlák94de2aa2019-02-15 12:42:11 +0100133#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
134#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100135#define IF_ARG_PREFIX_END }
136
David Sedlák1c8b2702019-02-22 11:03:02 +0100137 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +0100138 case 'x':
139 already_read += 1;
140 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
141 break;
David Sedlák94de2aa2019-02-15 12:42:11 +0100142 case 'c':
143 already_read += 1;
144 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200145 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200146
David Sedlák94de2aa2019-02-15 12:42:11 +0100147 case 'd':
148 already_read += 1;
149 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200150 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200151
David Sedlák94de2aa2019-02-15 12:42:11 +0100152 case 'm':
153 already_read += 1;
154 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200155 break;
156
David Sedlák94de2aa2019-02-15 12:42:11 +0100157 case 'n':
158 already_read += 1;
159 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200160 break;
161
David Sedlák94de2aa2019-02-15 12:42:11 +0100162 case 't':
163 already_read += 1;
164 IF_ARG_PREFIX("a", 1)
165 IF_ARG("g", 1, YIN_ARG_TAG)
166 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
167 IF_ARG_PREFIX_END
168 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200169 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200170
David Sedlák94de2aa2019-02-15 12:42:11 +0100171 case 'u':
172 already_read += 1;
173 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200174 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200175
David Sedlák94de2aa2019-02-15 12:42:11 +0100176 case 'v':
177 already_read += 1;
178 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200179 break;
180 }
181
David Sedlákc10e7902018-12-17 02:17:59 +0100182 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200183 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200184 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200185 }
186
David Sedlák18730132019-03-15 15:51:34 +0100187#undef IF_ARG
188#undef IF_ARG_PREFIX
189#undef IF_ARG_PREFIX_END
190
David Sedlák872c7b42018-10-26 13:15:20 +0200191 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200192}
193
David Sedlák00250342019-06-21 14:19:39 +0200194static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) {
195 (void)xml_ctx; /* unused */
196 if (record->dynamic_content) {
197 free(record->content);
198 }
199}
200
David Sedlák8f7a1172019-06-20 14:42:18 +0200201LY_ERR
202yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args)
David Sedláka7406952019-04-05 10:33:07 +0200203{
204 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200205 struct yin_arg_record *argument_record = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200206
David Sedlák7ff55a92019-06-17 11:11:41 +0200207 /* load all attributes first */
David Sedlák57715b12019-06-17 13:05:22 +0200208 while (xml_ctx->status == LYXML_ATTRIBUTE) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200209 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *args, argument_record, ret, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200210 ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len,
211 &argument_record->name, &argument_record->name_len);
David Sedlák00250342019-06-21 14:19:39 +0200212 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200213
David Sedlák7ff55a92019-06-17 11:11:41 +0200214 if (xml_ctx->status == LYXML_ATTR_CONTENT) {
David Sedlák57715b12019-06-17 13:05:22 +0200215 argument_record->content = NULL;
216 argument_record->content_len = 0;
217 argument_record->dynamic_content = 0;
218 ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len,
219 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200220 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200221 }
222 }
223
David Sedlák8f7a1172019-06-20 14:42:18 +0200224cleanup:
225 if (ret != LY_SUCCESS) {
David Sedlák00250342019-06-21 14:19:39 +0200226 FREE_ARRAY(xml_ctx, *args, free_arg_rec);
227 *args = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200228 }
229 return ret;
230}
231
232LY_ERR
233yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, const char **arg_val)
234{
235 LY_ERR ret = LY_SUCCESS;
236 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
237 struct yin_arg_record *iter = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200238
David Sedlák1bccdfa2019-06-17 15:55:27 +0200239 /* validation of attributes */
David Sedlák8f7a1172019-06-20 14:42:18 +0200240 LY_ARRAY_FOR(*args, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200241 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
242 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200243 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200244 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200245 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200246 } else if (arg == arg_type) {
David Sedlák57715b12019-06-17 13:05:22 +0200247 if (iter->dynamic_content) {
248 *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content);
David Sedlák00250342019-06-21 14:19:39 +0200249 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
250 /* string is no longer supposed to be freed when the sized array is freed */
251 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200252 } else {
253 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
254 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
255 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200256 } else {
David Sedlák7ff55a92019-06-17 11:11:41 +0200257 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name);
258 ret = LY_EVALID;
259 goto cleanup;
David Sedláka7406952019-04-05 10:33:07 +0200260 }
261 }
262 }
263
David Sedlák7ff55a92019-06-17 11:11:41 +0200264cleanup:
David Sedlák7ff55a92019-06-17 11:11:41 +0200265 return ret;
David Sedláka7406952019-04-05 10:33:07 +0200266}
267
David Sedlák0daba9a2019-03-22 14:07:49 +0100268LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200269yin_parse_text_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100270{
271 LY_ERR ret = LY_SUCCESS;
272 char *buf = NULL, *out = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200273 const char *prefix = NULL, *name = NULL;
274 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200275 int dynamic = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100276
David Sedlák0daba9a2019-03-22 14:07:49 +0100277 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
278 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
279 LY_CHECK_RET(ret);
David Sedlák57715b12019-06-17 13:05:22 +0200280 if (dynamic) {
281 *value = lydict_insert_zc(xml_ctx->ctx, buf);
David Sedlák00250342019-06-21 14:19:39 +0200282 LY_CHECK_RET(!(*value), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200283 } else {
284 *value = lydict_insert(xml_ctx->ctx, out, out_len);
David Sedlák00250342019-06-21 14:19:39 +0200285 LY_CHECK_RET(!(*value), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200286 }
David Sedlák0daba9a2019-03-22 14:07:49 +0100287 }
288
David Sedlák00250342019-06-21 14:19:39 +0200289 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
290 /* probably should never happen, mixed content should be caught by lyxml_get_element call but
291 * I will just leave it here for now to be sure */
292 LY_CHECK_RET(name, LY_EVALID);
293 return ret;
David Sedlák15a92662019-06-18 11:55:15 +0200294}
295
David Sedlák00250342019-06-21 14:19:39 +0200296/**
297 * @brief function to parse meta tags eg. elements with text element as child
298 *
299 * @param[in] xml_ctx Xml context.
300 * @param[in] args Sized array of arguments of current elements.
301 * @param[in,out] data Data to read from.
302 * @param[out] value Where the content of meta tag should be stored.
303 *
304 * @return LY_ERR values.
305 */
David Sedlák15a92662019-06-18 11:55:15 +0200306LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200307yin_parse_meta_element(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, const char **value)
David Sedlák15a92662019-06-18 11:55:15 +0200308{
309 LY_ERR ret = LY_SUCCESS;
310 char *buf = NULL, *out = NULL;
David Sedlák00250342019-06-21 14:19:39 +0200311 const char *prefix = NULL, *name = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200312 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
313 int dynamic = 0;
David Sedlák15a92662019-06-18 11:55:15 +0200314 enum YIN_ARGUMENT arg = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200315 struct yin_arg_record *subelem_args = NULL;
316 enum yang_keyword kw = YANG_NONE;
David Sedlák15a92662019-06-18 11:55:15 +0200317
David Sedlák00250342019-06-21 14:19:39 +0200318 LY_CHECK_ERR_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected content of meta element."), LY_EVALID);
David Sedlák15a92662019-06-18 11:55:15 +0200319
320 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200321 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element."), LY_EINVAL);
David Sedlák15a92662019-06-18 11:55:15 +0200322
David Sedlák8f7a1172019-06-20 14:42:18 +0200323 /* first element should be argument element <text> */
David Sedlák8f7a1172019-06-20 14:42:18 +0200324 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák00250342019-06-21 14:19:39 +0200325 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
326 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200327 arg = yin_match_argument_name(name, name_len);
David Sedlák00250342019-06-21 14:19:39 +0200328 LY_CHECK_ERR_GOTO(arg != YIN_ARG_TEXT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element.");
329 ret = LY_EINVAL, cleanup);
330 ret = yin_parse_text_element(xml_ctx, &subelem_args, data, value);
331 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200332
David Sedlák15a92662019-06-18 11:55:15 +0200333 /* loop over all child elements and parse them */
334 while (xml_ctx->status == LYXML_ELEMENT) {
David Sedlák00250342019-06-21 14:19:39 +0200335 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
336 LY_CHECK_GOTO(ret, cleanup);
337 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
338 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200339 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák15a92662019-06-18 11:55:15 +0200340
David Sedlák8f7a1172019-06-20 14:42:18 +0200341 if (!name) {
David Sedlák15a92662019-06-18 11:55:15 +0200342 /* end of meta element reached */
343 break;
344 }
345
David Sedlák8f7a1172019-06-20 14:42:18 +0200346 switch (kw) {
347 case YANG_CUSTOM:
348 // TODO parse extension instance
349 break;
350
351 default:
352 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
353 return LY_EVALID;
354 }
David Sedlák15a92662019-06-18 11:55:15 +0200355 }
356
David Sedlák00250342019-06-21 14:19:39 +0200357cleanup:
358 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
359 return ret;
David Sedlák0daba9a2019-03-22 14:07:49 +0100360}
361
David Sedlák00250342019-06-21 14:19:39 +0200362/**
363 * @brief Parse revision date.
364 *
365 * @param[in] xml_ctx Xml context.
366 * @param[in] args Sized array of arguments of current element.
367 * @param[in,out] data Data to read from.
368 * @param[in,out] rev Array to store the parsed value in.
369 * @param[in,out] exts Extension instances to add to.
370 *
371 * @return LY_ERR values.
372 */
David Sedlák81e04022019-04-05 15:05:46 +0200373static LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200374yin_parse_revision_date(struct lyxml_context *xml_ctx, struct yin_arg_record **args, const char **data, char *rev, struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200375{
376 LY_ERR ret = LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200377 const char *temp_rev, *prefix = NULL, *name = NULL;
378 char *out;
379 struct yin_arg_record *subelem_args = NULL;
380 size_t prefix_len = 0, name_len = 0;
381 enum yang_keyword kw = YANG_NONE;
382 int dynamic;
David Sedlákcd0c9512019-03-29 13:23:06 +0100383
David Sedlák81e04022019-04-05 15:05:46 +0200384 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200385 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
David Sedlák81e04022019-04-05 15:05:46 +0200386 return LY_EVALID;
387 }
388
David Sedlák8f7a1172019-06-20 14:42:18 +0200389 LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200390 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200391 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
392
393 strcpy(rev, temp_rev);
394 lydict_remove(xml_ctx->ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200395
David Sedlák00250342019-06-21 14:19:39 +0200396 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
397 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
398 /* unknown element text content is ignored */
399 if (ret == LY_EINVAL) {
400 while (xml_ctx->status == LYXML_ELEMENT) {
401 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
402 LY_CHECK_GOTO(ret, cleanup);
403 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
404 LY_CHECK_GOTO(ret, cleanup);
405 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
406
407 if (!name) {
408 /* end of revisin-date element reached */
409 break;
410 }
411
412 switch (kw) {
413 case YANG_CUSTOM:
414 // TODO parse extension instance
415 break;
416
417 default:
418 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
419 return LY_EVALID;
420 }
421 }
422 } else {
423 /* load closing element */
424 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
425 LY_CHECK_RET(name, LY_EINVAL);
426 }
427 }
428
429cleanup:
430 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák81e04022019-04-05 15:05:46 +0200431 return ret;
432}
David Sedlákcd0c9512019-03-29 13:23:06 +0100433
David Sedlákda63c082019-06-04 13:52:23 +0200434LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200435yin_parse_import(struct lyxml_context *xml_ctx, struct yin_arg_record **import_args, const char *module_prefix, const char **data, struct lysp_import **imports)
David Sedlák736fd0d2019-02-15 16:06:31 +0100436{
437 LY_ERR ret = LY_SUCCESS;
438 enum yang_keyword kw;
439 struct lysp_import *imp;
440 const char *prefix, *name;
David Sedlák00250342019-06-21 14:19:39 +0200441 char *out;
David Sedlák8f7a1172019-06-20 14:42:18 +0200442 struct yin_arg_record *subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100443 size_t prefix_len, name_len;
David Sedlák00250342019-06-21 14:19:39 +0200444 int dynamic;
David Sedlák736fd0d2019-02-15 16:06:31 +0100445
David Sedlák00250342019-06-21 14:19:39 +0200446 /* allocate new element in sized array for import */
David Sedlák8f7a1172019-06-20 14:42:18 +0200447 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *imports, imp, ret, validation_err);
David Sedlák736fd0d2019-02-15 16:06:31 +0100448
David Sedláka7406952019-04-05 10:33:07 +0200449 /* parse import attributes */
David Sedlák00250342019-06-21 14:19:39 +0200450 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name) != LY_SUCCESS, validation_err);
451
452 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
453 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after import element"), LY_EINVAL);
454
455 while (xml_ctx->status == LYXML_ELEMENT) {
456 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
457 LY_CHECK_GOTO(ret, validation_err);
458 if (!name) {
459 /* end of import element reached */
460 break;
461 }
462 LY_CHECK_GOTO(yin_load_attributes(xml_ctx, data, &subelem_args), validation_err);
David Sedlák8f7a1172019-06-20 14:42:18 +0200463 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák00250342019-06-21 14:19:39 +0200464
David Sedlák736fd0d2019-02-15 16:06:31 +0100465 switch (kw) {
466 case YANG_PREFIX:
David Sedlák8f7a1172019-06-20 14:42:18 +0200467 LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err);
468 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err);
469 LY_CHECK_GOTO(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, *imports, module_prefix, &imp->prefix) != LY_SUCCESS, validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100470 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100471 case YANG_DESCRIPTION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200472 LY_CHECK_ERR_GOTO(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), validation_err);
David Sedlák00250342019-06-21 14:19:39 +0200473 LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->dsc), validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100474 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100475 case YANG_REFERENCE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200476 LY_CHECK_ERR_GOTO(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), validation_err);
David Sedlák00250342019-06-21 14:19:39 +0200477 LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->ref), validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100478 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100479 case YANG_REVISION_DATE:
David Sedlák00250342019-06-21 14:19:39 +0200480 LY_CHECK_GOTO(yin_parse_revision_date(xml_ctx, &subelem_args, data, imp->rev, &imp->exts), validation_err);
David Sedlák3a55bc12019-06-03 08:57:29 +0200481 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100482 case YANG_CUSTOM:
483 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200484 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100485 default:
David Sedlák916b1902019-06-06 11:35:05 +0200486 LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import");
David Sedlák8f7a1172019-06-20 14:42:18 +0200487 goto validation_err;
David Sedlák736fd0d2019-02-15 16:06:31 +0100488 }
David Sedlák4af209e2019-06-21 14:24:50 +0200489 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200490 subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100491 }
492
David Sedlák8f7a1172019-06-20 14:42:18 +0200493 LY_CHECK_ERR_GOTO(!imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), validation_err);
David Sedlák736fd0d2019-02-15 16:06:31 +0100494 return ret;
David Sedlák8f7a1172019-06-20 14:42:18 +0200495
496validation_err:
David Sedlák4af209e2019-06-21 14:24:50 +0200497 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200498 return LY_EVALID;
David Sedlák736fd0d2019-02-15 16:06:31 +0100499}
500
David Sedlák11900c82019-06-18 16:29:12 +0200501LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200502yin_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 +0200503{
504 LY_ERR ret = LY_SUCCESS;
505 enum yang_keyword kw = YANG_NONE;
506 const char *value = NULL, *prefix = NULL, *name = NULL;
507 char *out;
508 size_t prefix_len = 0, name_len = 0, out_len = 0;
509 int dynamic = 0;
David Sedlák00250342019-06-21 14:19:39 +0200510 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200511 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200512 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200513 return LY_EVALID;
514 }
515
David Sedlák8f7a1172019-06-20 14:42:18 +0200516 LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value));
David Sedlák11900c82019-06-18 16:29:12 +0200517 if (strcmp(value, "current") == 0) {
518 *flags |= LYS_STATUS_CURR;
519 } else if (strcmp(value, "deprecated") == 0) {
520 *flags |= LYS_STATUS_DEPRC;
521 } else if (strcmp(value, "obsolete") == 0) {
522 *flags |= LYS_STATUS_OBSLT;
523 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200524 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200525 lydict_remove(xml_ctx->ctx, value);
526 return LY_EVALID;
527 }
528 lydict_remove(xml_ctx->ctx, value);
529
David Sedlákb6e65972019-06-19 10:44:13 +0200530 /* TODO check if dynamic was set to 1 in case of error */
531 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
532 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
533 /* if there are any xml subelements parse them, unknown text content is silently ignored */
534 if (ret == LY_EINVAL) {
535 /* load subelements */
536 while (xml_ctx->status == LYXML_ELEMENT) {
537 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
538 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200539 /* end of status reached */
David Sedlákb6e65972019-06-19 10:44:13 +0200540 break;
541 }
David Sedlák00250342019-06-21 14:19:39 +0200542 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
543 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200544 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákb6e65972019-06-19 10:44:13 +0200545 switch (kw) {
546 case YANG_CUSTOM:
547 /* TODO parse extension instance */
548 break;
549 default:
David Sedlákb3077192019-06-19 10:55:37 +0200550 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status");
David Sedlák00250342019-06-21 14:19:39 +0200551 ret = LY_EVALID;
552 goto cleanup;
David Sedlákb6e65972019-06-19 10:44:13 +0200553 }
David Sedlák00250342019-06-21 14:19:39 +0200554
555 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
556 subelem_args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200557 }
David Sedlák00250342019-06-21 14:19:39 +0200558 } else {
559 /* load closing element */
560 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
561 LY_CHECK_RET(name, LY_EINVAL);
David Sedlák11900c82019-06-18 16:29:12 +0200562 }
563 }
564
565 return ret;
David Sedlák00250342019-06-21 14:19:39 +0200566
567cleanup:
568 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
569 return ret;
David Sedlák11900c82019-06-18 16:29:12 +0200570}
571
David Sedlák11900c82019-06-18 16:29:12 +0200572LY_ERR
David Sedlák2721d3d2019-06-21 15:37:41 +0200573yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, struct lysp_ext **extensions)
574{
575 LY_ERR ret = LY_SUCCESS;
David Sedlák2453f9d2019-06-27 15:59:04 +0200576 const char *temp_val = NULL, *name = NULL;
577 size_t name_len = 0;
David Sedlák2721d3d2019-06-21 15:37:41 +0200578 struct yin_arg_record *subelem_args = NULL;
David Sedlák2721d3d2019-06-21 15:37:41 +0200579 enum yang_keyword kw = YANG_NONE;
David Sedlák9bc65872019-06-24 20:21:08 +0200580 struct yin_arg_record temp_record;
David Sedlák2721d3d2019-06-21 15:37:41 +0200581
582 if (*flags & LYS_YINELEM_MASK) {
583 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "yin-element");
584 return LY_EVALID;
585 }
586
587 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_VALUE, &temp_val));
588 if (strcmp(temp_val, "true") == 0) {
589 *flags |= LYS_YINELEM_TRUE;
590 } else if (strcmp(temp_val, "false") == 0) {
591 *flags |= LYS_YINELEM_FALSE;
592 } else {
593 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
594 lydict_remove(xml_ctx->ctx, temp_val);
595 return LY_EVALID;
596 }
597 lydict_remove(xml_ctx->ctx, temp_val);
598
David Sedlák9bc65872019-06-24 20:21:08 +0200599 YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args);
David Sedlák9bc65872019-06-24 20:21:08 +0200600 switch (kw) {
601 case YANG_CUSTOM:
602 // TODO parse extension instance
603 break;
604
605 default:
606 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
607 return LY_EVALID;
608 }
609 YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record);
David Sedlák2721d3d2019-06-21 15:37:41 +0200610
611cleanup:
612 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
613 return ret;
614}
615
616LY_ERR
David Sedlák1e2cdd02019-06-27 14:17:43 +0200617yin_parse_extension_instance(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
618 const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
619 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
620{
621 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +0200622 char *out;
623 const char *name, *prefix;
624 size_t out_len, prefix_len, name_len;
625 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200626 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +0200627 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
628 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200629
630 LY_ARRAY_NEW_RET(xml_ctx->ctx, *exts, e, LY_EMEM);
631
632 e->yin = 0;
633 /* store name and insubstmt info */
634 e->name = lydict_insert(xml_ctx->ctx, ext_name, ext_name_len);
635 e->insubstmt = insubstmt;
636 e->insubstmt_index = insubstmt_index;
637 e->yin |= LYS_YIN;
638
David Sedlákb1a78352019-06-28 16:16:29 +0200639 /* store attributes as subelements */
640 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
641 if (!iter->prefix) {
642 new_subelem = calloc(1, sizeof(*new_subelem));
643 if (!e->child) {
644 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200645 } else {
David Sedlákb1a78352019-06-28 16:16:29 +0200646 last_subelem->next = new_subelem;
647 }
648 last_subelem = new_subelem;
649
650 last_subelem->flags |= LYS_YIN_ATTR;
651 last_subelem->stmt = lydict_insert(xml_ctx->ctx, iter->name, iter->name_len);
652 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM);
653 if (iter->dynamic_content) {
654 last_subelem->arg = lydict_insert_zc(xml_ctx->ctx, iter->content);
655 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM);
656 } else {
657 last_subelem->arg = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
658 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200659 }
660 }
661 }
David Sedlák1e2cdd02019-06-27 14:17:43 +0200662
David Sedlákf250ecf2019-07-01 11:02:05 +0200663 /* parse subelements */
David Sedlákb1a78352019-06-28 16:16:29 +0200664 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
665 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
666 if (ret == LY_EINVAL) {
667 while (xml_ctx->status == LYXML_ELEMENT) {
668 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
669 if (!name) {
670 /* end of extension instance reached */
671 break;
672 }
David Sedlákf250ecf2019-07-01 11:02:05 +0200673 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 +0200674 if (!e->child) {
675 e->child = new_subelem;
676 } else {
677 last_subelem->next = new_subelem;
678 }
679 last_subelem = new_subelem;
680 }
681 }
682 } else {
683 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
684 LY_CHECK_RET(name, LY_EINVAL);
685 }
686
687 return LY_SUCCESS;
688}
689
690LY_ERR
David Sedlákf250ecf2019-07-01 11:02:05 +0200691yin_parse_element_generic(struct lyxml_context *xml_ctx, const char *name, size_t name_len, const char *prefix,
692 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +0200693{
694 LY_ERR ret = LY_SUCCESS;
695 const char *temp_prefix, *temp_name;
696 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200697 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +0200698 int dynamic;
699 struct yin_arg_record *subelem_args = NULL;
700 struct lysp_stmt *last = NULL, *new = NULL;
701
702 /* allocate new structure for element */
703 *element = calloc(1, sizeof(**element));
704 (*element)->stmt = lydict_insert(xml_ctx->ctx, name, name_len);
705 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(xml_ctx->ctx), LY_EMEM);
706
707 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +0200708 /* load attributes */
David Sedlákb1a78352019-06-28 16:16:29 +0200709 while(xml_ctx->status == LYXML_ATTRIBUTE) {
710 /* add new element to linked-list */
711 new = calloc(1, sizeof(*last));
712 LY_CHECK_ERR_GOTO(ret, LOGMEM(xml_ctx->ctx), err);
713 if (!(*element)->child) {
714 /* save first */
715 (*element)->child = new;
716 } else {
717 last->next = new;
718 }
719 last = new;
720
721 last->flags |= LYS_YIN_ATTR;
722 ret = lyxml_get_attribute(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
723 LY_CHECK_GOTO(ret, err);
724 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
725 LY_CHECK_GOTO(ret, err);
726 last->stmt = lydict_insert(xml_ctx->ctx, temp_name, temp_name_len);
727 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
728 /* attributes with prefix are ignored */
729 if (!temp_prefix) {
730 if (dynamic) {
731 last->arg = lydict_insert_zc(xml_ctx->ctx, out);
732 if (!last->arg) {
733 free(out);
734 LOGMEM(xml_ctx->ctx);
735 ret = LY_EMEM;
736 goto err;
737 }
738 } else {
739 last->arg = lydict_insert(xml_ctx->ctx, out, out_len);
740 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
741 }
742 }
743 }
744
745 /* parse content of element */
746 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
747 if (ret == LY_EINVAL) {
748 while (xml_ctx->status == LYXML_ELEMENT) {
749 /* parse subelements */
David Sedlákf250ecf2019-07-01 11:02:05 +0200750 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 +0200751 LY_CHECK_GOTO(ret, err);
752 if (!name) {
753 /* end of element reached */
754 break;
755 }
David Sedlákf250ecf2019-07-01 11:02:05 +0200756 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 +0200757 LY_CHECK_GOTO(ret, err);
758 last = last->next;
759 }
760 } else {
761 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +0200762 if (out_len != 0) {
763 if (dynamic) {
764 (*element)->arg = lydict_insert_zc(xml_ctx->ctx, out);
765 if (!(*element)->arg) {
766 free(out);
767 LOGMEM(xml_ctx->ctx);
768 ret = LY_EMEM;
769 goto err;
770 }
771 } else {
772 (*element)->arg = lydict_insert(xml_ctx->ctx, out, out_len);
773 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(xml_ctx->ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +0200774 }
David Sedlákb1a78352019-06-28 16:16:29 +0200775 }
776 /* read closing tag */
777 ret = lyxml_get_element(xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
778 LY_CHECK_GOTO(ret, err);
779 }
780
781 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
782 return LY_SUCCESS;
783
784err:
785 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200786 return ret;
787}
788
789LY_ERR
David Sedlák9494eb22019-06-21 16:06:53 +0200790yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
791 uint16_t *flags, const char **argument, struct lysp_ext **extensions)
792{
793 LY_ERR ret = LY_SUCCESS;
David Sedlák2453f9d2019-06-27 15:59:04 +0200794 const char *name = NULL;
795 size_t name_len = 0;
David Sedlák9494eb22019-06-21 16:06:53 +0200796 struct yin_arg_record *subelem_args = NULL;
David Sedlák9494eb22019-06-21 16:06:53 +0200797 enum yang_keyword kw = YANG_NONE;
David Sedlák9bc65872019-06-24 20:21:08 +0200798 struct yin_arg_record temp_record;
David Sedlák9494eb22019-06-21 16:06:53 +0200799
800 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, argument));
801
David Sedlák9bc65872019-06-24 20:21:08 +0200802 YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args);
803 if (!name) {
804 /* end of argument element reached */
805 break;
David Sedlák9494eb22019-06-21 16:06:53 +0200806 }
David Sedlák9bc65872019-06-24 20:21:08 +0200807
808 switch (kw) {
809 case YANG_YIN_ELEMENT:
810 yin_parse_yin_element_element(xml_ctx, &subelem_args, data, flags, extensions);
811 break;
812 case YANG_CUSTOM:
813 // TODO parse extension instance
814 break;
815
816 default:
817 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
818 return LY_EVALID;
819 }
820 YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record);
David Sedlák9494eb22019-06-21 16:06:53 +0200821
822cleanup:
823 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
824 return ret;
825}
826
827LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200828yin_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 +0200829{
830 LY_ERR ret = LY_SUCCESS;
831 struct lysp_ext *ex;
832 const char *prefix = NULL, *name = NULL;
833 char *out = NULL;
834 size_t out_len = 0, prefix_len = 0, name_len = 0;
835 int dynamic = 0;
836 enum yang_keyword kw = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200837 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200838
David Sedlák00250342019-06-21 14:19:39 +0200839 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup);
840 ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name);
841 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200842 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200843 LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element.");
844 ret = LY_EINVAL, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200845
846 while (xml_ctx->status == LYXML_ELEMENT) {
847 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200848 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200849 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200850 /* end of extension element reached */
David Sedlák11900c82019-06-18 16:29:12 +0200851 break;
852 }
David Sedlák00250342019-06-21 14:19:39 +0200853 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
854 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200855 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák11900c82019-06-18 16:29:12 +0200856 switch (kw) {
857 case YANG_ARGUMENT:
David Sedlák9494eb22019-06-21 16:06:53 +0200858 ret = yin_parse_argument_element(xml_ctx, &subelem_args, data, &ex->flags, &ex->argument, extensions);
David Sedlák11900c82019-06-18 16:29:12 +0200859 break;
860 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200861 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc);
862 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200863 break;
864 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200865 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref);
866 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200867 break;
868 case YANG_STATUS:
David Sedlák00250342019-06-21 14:19:39 +0200869 ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts);
870 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200871 break;
872 case YANG_CUSTOM:
873 /* TODO parse extension instance */
874 break;
875 default:
David Sedlákb3077192019-06-19 10:55:37 +0200876 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension");
David Sedlák00250342019-06-21 14:19:39 +0200877 ret = LY_EVALID;
878 goto cleanup;
David Sedlák11900c82019-06-18 16:29:12 +0200879 }
David Sedlák00250342019-06-21 14:19:39 +0200880 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák288c1472019-06-20 16:09:48 +0200881 subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200882 }
883
David Sedlák00250342019-06-21 14:19:39 +0200884cleanup:
885 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák11900c82019-06-18 16:29:12 +0200886 return ret;
887}
888
David Sedlák00250342019-06-21 14:19:39 +0200889/**
890 * @brief Parse module substatements.
891 *
892 * @param[in] xml_ctx Xml context.
893 * @param[in,out] data Data to read from.
894 * @param[out] mod Parsed module structure.
895 *
896 * @return LY_ERR values.
897 */
898LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200899yin_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 +0200900{
David Sedlákf3b24f62018-11-02 10:40:47 +0100901 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100902 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100903 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200904 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100905 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200906
David Sedlákf3b24f62018-11-02 10:40:47 +0100907 char *buf = NULL, *out = NULL;
908 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200909 int dynamic = 0;
David Sedlák8f7a1172019-06-20 14:42:18 +0200910 struct yin_arg_record *substmt_args = NULL;
David Sedlákf3b24f62018-11-02 10:40:47 +0100911
David Sedlák8f7a1172019-06-20 14:42:18 +0200912 yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlákb3077192019-06-19 10:55:37 +0200913 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module"), LY_EVALID);
David Sedlákc136b972019-03-08 13:39:06 +0100914 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200915 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new xml element after module element."), LY_EINVAL);
David Sedlák4a4c0722018-11-26 17:03:10 +0100916
David Sedlák736fd0d2019-02-15 16:06:31 +0100917 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100918 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100919#define CHECK_ORDER(SECTION) \
920 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
921
922 switch (kw) {
923 /* module header */
924 case YANG_NAMESPACE:
925 case YANG_PREFIX:
926 CHECK_ORDER(Y_MOD_MODULE_HEADER);
927 break;
928 case YANG_YANG_VERSION:
929 CHECK_ORDER(Y_MOD_MODULE_HEADER);
930 break;
931 /* linkage */
932 case YANG_INCLUDE:
933 case YANG_IMPORT:
934 CHECK_ORDER(Y_MOD_LINKAGE);
935 break;
936 /* meta */
937 case YANG_ORGANIZATION:
938 case YANG_CONTACT:
939 case YANG_DESCRIPTION:
940 case YANG_REFERENCE:
941 CHECK_ORDER(Y_MOD_META);
942 break;
943
944 /* revision */
945 case YANG_REVISION:
946 CHECK_ORDER(Y_MOD_REVISION);
947 break;
948 /* body */
949 case YANG_ANYDATA:
950 case YANG_ANYXML:
951 case YANG_AUGMENT:
952 case YANG_CHOICE:
953 case YANG_CONTAINER:
954 case YANG_DEVIATION:
955 case YANG_EXTENSION:
956 case YANG_FEATURE:
957 case YANG_GROUPING:
958 case YANG_IDENTITY:
959 case YANG_LEAF:
960 case YANG_LEAF_LIST:
961 case YANG_LIST:
962 case YANG_NOTIFICATION:
963 case YANG_RPC:
964 case YANG_TYPEDEF:
965 case YANG_USES:
966 case YANG_CUSTOM:
967 mod_stmt = Y_MOD_BODY;
968 break;
969 default:
970 /* error will be handled in the next switch */
971 break;
972 }
973#undef CHECK_ORDER
974
David Sedlák68826732019-06-05 10:50:58 +0200975 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákc136b972019-03-08 13:39:06 +0100976 if (name) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200977 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args));
978 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100979 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100980
David Sedlákc136b972019-03-08 13:39:06 +0100981 /* module header */
982 case YANG_NAMESPACE:
David Sedlák00250342019-06-21 14:19:39 +0200983 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns);
984 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100985 break;
986 case YANG_PREFIX:
David Sedlák00250342019-06-21 14:19:39 +0200987 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix);
988 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100989 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100990
David Sedlákc136b972019-03-08 13:39:06 +0100991 /* linkage */
992 case YANG_IMPORT:
David Sedlák00250342019-06-21 14:19:39 +0200993 ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports);
994 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100995 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100996
David Sedlákc136b972019-03-08 13:39:06 +0100997 /* meta */
998 case YANG_ORGANIZATION:
David Sedlák00250342019-06-21 14:19:39 +0200999 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org);
1000 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +01001001 break;
1002 case YANG_CONTACT:
David Sedlák00250342019-06-21 14:19:39 +02001003 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact);
1004 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +01001005 break;
1006 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +02001007 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc);
1008 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +01001009 break;
1010 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +02001011 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref);
1012 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +01001013 break;
David Sedlák11900c82019-06-18 16:29:12 +02001014 /* revision */
1015
1016 /*body */
1017 case YANG_EXTENSION:
David Sedlák00250342019-06-21 14:19:39 +02001018 ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions);
1019 LY_CHECK_GOTO(ret, error);
David Sedlák11900c82019-06-18 16:29:12 +02001020 break;
David Sedlák736fd0d2019-02-15 16:06:31 +01001021
David Sedlákc136b972019-03-08 13:39:06 +01001022 default:
David Sedlák00250342019-06-21 14:19:39 +02001023 ret = LY_EVALID;
1024 goto error;
David Sedlákc136b972019-03-08 13:39:06 +01001025 }
David Sedlák00250342019-06-21 14:19:39 +02001026 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +02001027 substmt_args = NULL;
David Sedlák4a4c0722018-11-26 17:03:10 +01001028 }
1029 }
1030
David Sedlák68826732019-06-05 10:50:58 +02001031 return LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +02001032
1033error:
1034 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
1035 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001036}
1037
1038LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001039yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001040{
David Sedláke4889912018-11-02 09:52:40 +01001041 LY_ERR ret = LY_SUCCESS;
1042 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +02001043 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +02001044 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +01001045 struct lysp_module *mod_p = NULL;
1046 const char *prefix, *name;
1047 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +02001048 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001049
David Sedlák3017da42019-02-15 09:48:04 +01001050 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +02001051 memset(&parser_ctx, 0, sizeof parser_ctx);
1052 xml_ctx->ctx = ctx;
1053 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +01001054
David Sedlák3017da42019-02-15 09:48:04 +01001055 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +02001056 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001057 LY_CHECK_GOTO(ret, cleanup);
1058 ret = yin_load_attributes(xml_ctx, &data, &args);
1059 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +02001060 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +01001061 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001062 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1063 ret = LY_EINVAL;
1064 goto cleanup;
1065 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +02001066 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +02001067 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001068 ret = LY_EVALID;
1069 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001070 }
1071
David Sedlák3017da42019-02-15 09:48:04 +01001072 /* allocate module */
1073 mod_p = calloc(1, sizeof *mod_p);
1074 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1075 mod_p->mod = mod;
1076 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001077
David Sedlák00250342019-06-21 14:19:39 +02001078 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +02001079 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001080 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001081
David Sedlák3017da42019-02-15 09:48:04 +01001082 mod_p->parsing = 0;
1083 mod->parsed = mod_p;
1084
1085cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001086 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001087 lysp_module_free(mod_p);
1088 }
David Sedlák00250342019-06-21 14:19:39 +02001089 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +02001090 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001091 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001092}