blob: 4926b1567f60abcd7e421113c080569e8863fd2c [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) {\
62 /* end of yin-element element reached */\
63 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;
238 const struct lyxml_ns *ns = NULL;
239
David Sedlák1bccdfa2019-06-17 15:55:27 +0200240 /* validation of attributes */
David Sedlák8f7a1172019-06-20 14:42:18 +0200241 LY_ARRAY_FOR(*args, struct yin_arg_record, iter) {
David Sedlák7ff55a92019-06-17 11:11:41 +0200242 ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len);
David Sedlák00250342019-06-21 14:19:39 +0200243 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
244 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200245 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200246 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200247 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200248 } else if (arg == arg_type) {
David Sedlák57715b12019-06-17 13:05:22 +0200249 if (iter->dynamic_content) {
250 *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content);
David Sedlák00250342019-06-21 14:19:39 +0200251 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
252 /* string is no longer supposed to be freed when the sized array is freed */
253 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200254 } else {
255 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
256 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
257 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200258 } else {
David Sedlák7ff55a92019-06-17 11:11:41 +0200259 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name);
260 ret = LY_EVALID;
261 goto cleanup;
David Sedláka7406952019-04-05 10:33:07 +0200262 }
263 }
264 }
265
David Sedlák7ff55a92019-06-17 11:11:41 +0200266cleanup:
David Sedlák7ff55a92019-06-17 11:11:41 +0200267 return ret;
David Sedláka7406952019-04-05 10:33:07 +0200268}
269
David Sedlák0daba9a2019-03-22 14:07:49 +0100270LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200271yin_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 +0100272{
273 LY_ERR ret = LY_SUCCESS;
274 char *buf = NULL, *out = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200275 const char *prefix = NULL, *name = NULL;
276 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200277 int dynamic = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100278
David Sedlák0daba9a2019-03-22 14:07:49 +0100279 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
280 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
281 LY_CHECK_RET(ret);
David Sedlák57715b12019-06-17 13:05:22 +0200282 if (dynamic) {
283 *value = lydict_insert_zc(xml_ctx->ctx, buf);
David Sedlák00250342019-06-21 14:19:39 +0200284 LY_CHECK_RET(!(*value), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200285 } else {
286 *value = lydict_insert(xml_ctx->ctx, out, out_len);
David Sedlák00250342019-06-21 14:19:39 +0200287 LY_CHECK_RET(!(*value), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200288 }
David Sedlák0daba9a2019-03-22 14:07:49 +0100289 }
290
David Sedlák00250342019-06-21 14:19:39 +0200291 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
292 /* probably should never happen, mixed content should be caught by lyxml_get_element call but
293 * I will just leave it here for now to be sure */
294 LY_CHECK_RET(name, LY_EVALID);
295 return ret;
David Sedlák15a92662019-06-18 11:55:15 +0200296}
297
David Sedlák00250342019-06-21 14:19:39 +0200298/**
299 * @brief function to parse meta tags eg. elements with text element as child
300 *
301 * @param[in] xml_ctx Xml context.
302 * @param[in] args Sized array of arguments of current elements.
303 * @param[in,out] data Data to read from.
304 * @param[out] value Where the content of meta tag should be stored.
305 *
306 * @return LY_ERR values.
307 */
David Sedlák15a92662019-06-18 11:55:15 +0200308LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200309yin_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 +0200310{
311 LY_ERR ret = LY_SUCCESS;
312 char *buf = NULL, *out = NULL;
David Sedlák00250342019-06-21 14:19:39 +0200313 const char *prefix = NULL, *name = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200314 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
315 int dynamic = 0;
David Sedlák15a92662019-06-18 11:55:15 +0200316 enum YIN_ARGUMENT arg = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200317 struct yin_arg_record *subelem_args = NULL;
318 enum yang_keyword kw = YANG_NONE;
David Sedlák15a92662019-06-18 11:55:15 +0200319
David Sedlák00250342019-06-21 14:19:39 +0200320 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 +0200321
322 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200323 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 +0200324
David Sedlák8f7a1172019-06-20 14:42:18 +0200325 /* first element should be argument element <text> */
David Sedlák8f7a1172019-06-20 14:42:18 +0200326 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák00250342019-06-21 14:19:39 +0200327 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
328 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200329 arg = yin_match_argument_name(name, name_len);
David Sedlák00250342019-06-21 14:19:39 +0200330 LY_CHECK_ERR_GOTO(arg != YIN_ARG_TEXT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element.");
331 ret = LY_EINVAL, cleanup);
332 ret = yin_parse_text_element(xml_ctx, &subelem_args, data, value);
333 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200334
David Sedlák15a92662019-06-18 11:55:15 +0200335 /* loop over all child elements and parse them */
336 while (xml_ctx->status == LYXML_ELEMENT) {
David Sedlák00250342019-06-21 14:19:39 +0200337 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
338 LY_CHECK_GOTO(ret, cleanup);
339 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
340 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200341 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák15a92662019-06-18 11:55:15 +0200342
David Sedlák8f7a1172019-06-20 14:42:18 +0200343 if (!name) {
David Sedlák15a92662019-06-18 11:55:15 +0200344 /* end of meta element reached */
345 break;
346 }
347
David Sedlák8f7a1172019-06-20 14:42:18 +0200348 switch (kw) {
349 case YANG_CUSTOM:
350 // TODO parse extension instance
351 break;
352
353 default:
354 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
355 return LY_EVALID;
356 }
David Sedlák15a92662019-06-18 11:55:15 +0200357 }
358
David Sedlák00250342019-06-21 14:19:39 +0200359cleanup:
360 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
361 return ret;
David Sedlák0daba9a2019-03-22 14:07:49 +0100362}
363
David Sedlák00250342019-06-21 14:19:39 +0200364/**
365 * @brief Parse revision date.
366 *
367 * @param[in] xml_ctx Xml context.
368 * @param[in] args Sized array of arguments of current element.
369 * @param[in,out] data Data to read from.
370 * @param[in,out] rev Array to store the parsed value in.
371 * @param[in,out] exts Extension instances to add to.
372 *
373 * @return LY_ERR values.
374 */
David Sedlák81e04022019-04-05 15:05:46 +0200375static LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200376yin_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 +0200377{
378 LY_ERR ret = LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200379 const char *temp_rev, *prefix = NULL, *name = NULL;
380 char *out;
381 struct yin_arg_record *subelem_args = NULL;
382 size_t prefix_len = 0, name_len = 0;
383 enum yang_keyword kw = YANG_NONE;
384 int dynamic;
David Sedlákcd0c9512019-03-29 13:23:06 +0100385
David Sedlák81e04022019-04-05 15:05:46 +0200386 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200387 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
David Sedlák81e04022019-04-05 15:05:46 +0200388 return LY_EVALID;
389 }
390
David Sedlák8f7a1172019-06-20 14:42:18 +0200391 LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200392 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200393 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
394
395 strcpy(rev, temp_rev);
396 lydict_remove(xml_ctx->ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200397
David Sedlák00250342019-06-21 14:19:39 +0200398 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
399 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
400 /* unknown element text content is ignored */
401 if (ret == LY_EINVAL) {
402 while (xml_ctx->status == LYXML_ELEMENT) {
403 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
404 LY_CHECK_GOTO(ret, cleanup);
405 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
406 LY_CHECK_GOTO(ret, cleanup);
407 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
408
409 if (!name) {
410 /* end of revisin-date element reached */
411 break;
412 }
413
414 switch (kw) {
415 case YANG_CUSTOM:
416 // TODO parse extension instance
417 break;
418
419 default:
420 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
421 return LY_EVALID;
422 }
423 }
424 } else {
425 /* load closing element */
426 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
427 LY_CHECK_RET(name, LY_EINVAL);
428 }
429 }
430
431cleanup:
432 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák81e04022019-04-05 15:05:46 +0200433 return ret;
434}
David Sedlákcd0c9512019-03-29 13:23:06 +0100435
David Sedlákda63c082019-06-04 13:52:23 +0200436LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200437yin_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 +0100438{
439 LY_ERR ret = LY_SUCCESS;
440 enum yang_keyword kw;
441 struct lysp_import *imp;
442 const char *prefix, *name;
David Sedlák00250342019-06-21 14:19:39 +0200443 char *out;
David Sedlák8f7a1172019-06-20 14:42:18 +0200444 struct yin_arg_record *subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100445 size_t prefix_len, name_len;
David Sedlák00250342019-06-21 14:19:39 +0200446 int dynamic;
David Sedlák736fd0d2019-02-15 16:06:31 +0100447
David Sedlák00250342019-06-21 14:19:39 +0200448 /* allocate new element in sized array for import */
David Sedlák8f7a1172019-06-20 14:42:18 +0200449 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *imports, imp, ret, validation_err);
David Sedlák736fd0d2019-02-15 16:06:31 +0100450
David Sedláka7406952019-04-05 10:33:07 +0200451 /* parse import attributes */
David Sedlák00250342019-06-21 14:19:39 +0200452 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name) != LY_SUCCESS, validation_err);
453
454 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
455 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after import element"), LY_EINVAL);
456
457 while (xml_ctx->status == LYXML_ELEMENT) {
458 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
459 LY_CHECK_GOTO(ret, validation_err);
460 if (!name) {
461 /* end of import element reached */
462 break;
463 }
464 LY_CHECK_GOTO(yin_load_attributes(xml_ctx, data, &subelem_args), validation_err);
David Sedlák8f7a1172019-06-20 14:42:18 +0200465 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák00250342019-06-21 14:19:39 +0200466
David Sedlák736fd0d2019-02-15 16:06:31 +0100467 switch (kw) {
468 case YANG_PREFIX:
David Sedlák8f7a1172019-06-20 14:42:18 +0200469 LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err);
470 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err);
471 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 +0100472 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100473 case YANG_DESCRIPTION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200474 LY_CHECK_ERR_GOTO(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), validation_err);
David Sedlák00250342019-06-21 14:19:39 +0200475 LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->dsc), validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100476 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100477 case YANG_REFERENCE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200478 LY_CHECK_ERR_GOTO(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), validation_err);
David Sedlák00250342019-06-21 14:19:39 +0200479 LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->ref), validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100480 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100481 case YANG_REVISION_DATE:
David Sedlák00250342019-06-21 14:19:39 +0200482 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 +0200483 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100484 case YANG_CUSTOM:
485 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200486 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100487 default:
David Sedlák916b1902019-06-06 11:35:05 +0200488 LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import");
David Sedlák8f7a1172019-06-20 14:42:18 +0200489 goto validation_err;
David Sedlák736fd0d2019-02-15 16:06:31 +0100490 }
David Sedlák4af209e2019-06-21 14:24:50 +0200491 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200492 subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100493 }
494
David Sedlák8f7a1172019-06-20 14:42:18 +0200495 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 +0100496 return ret;
David Sedlák8f7a1172019-06-20 14:42:18 +0200497
498validation_err:
David Sedlák4af209e2019-06-21 14:24:50 +0200499 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200500 return LY_EVALID;
David Sedlák736fd0d2019-02-15 16:06:31 +0100501}
502
David Sedlák11900c82019-06-18 16:29:12 +0200503LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200504yin_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 +0200505{
506 LY_ERR ret = LY_SUCCESS;
507 enum yang_keyword kw = YANG_NONE;
508 const char *value = NULL, *prefix = NULL, *name = NULL;
509 char *out;
510 size_t prefix_len = 0, name_len = 0, out_len = 0;
511 int dynamic = 0;
David Sedlák00250342019-06-21 14:19:39 +0200512 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200513 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200514 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200515 return LY_EVALID;
516 }
517
David Sedlák8f7a1172019-06-20 14:42:18 +0200518 LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value));
David Sedlák11900c82019-06-18 16:29:12 +0200519 if (strcmp(value, "current") == 0) {
520 *flags |= LYS_STATUS_CURR;
521 } else if (strcmp(value, "deprecated") == 0) {
522 *flags |= LYS_STATUS_DEPRC;
523 } else if (strcmp(value, "obsolete") == 0) {
524 *flags |= LYS_STATUS_OBSLT;
525 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200526 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200527 lydict_remove(xml_ctx->ctx, value);
528 return LY_EVALID;
529 }
530 lydict_remove(xml_ctx->ctx, value);
531
David Sedlákb6e65972019-06-19 10:44:13 +0200532 /* TODO check if dynamic was set to 1 in case of error */
533 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
534 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
535 /* if there are any xml subelements parse them, unknown text content is silently ignored */
536 if (ret == LY_EINVAL) {
537 /* load subelements */
538 while (xml_ctx->status == LYXML_ELEMENT) {
539 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
540 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200541 /* end of status reached */
David Sedlákb6e65972019-06-19 10:44:13 +0200542 break;
543 }
David Sedlák00250342019-06-21 14:19:39 +0200544 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
545 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200546 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákb6e65972019-06-19 10:44:13 +0200547 switch (kw) {
548 case YANG_CUSTOM:
549 /* TODO parse extension instance */
550 break;
551 default:
David Sedlákb3077192019-06-19 10:55:37 +0200552 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status");
David Sedlák00250342019-06-21 14:19:39 +0200553 ret = LY_EVALID;
554 goto cleanup;
David Sedlákb6e65972019-06-19 10:44:13 +0200555 }
David Sedlák00250342019-06-21 14:19:39 +0200556
557 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
558 subelem_args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200559 }
David Sedlák00250342019-06-21 14:19:39 +0200560 } else {
561 /* load closing element */
562 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
563 LY_CHECK_RET(name, LY_EINVAL);
David Sedlák11900c82019-06-18 16:29:12 +0200564 }
565 }
566
567 return ret;
David Sedlák00250342019-06-21 14:19:39 +0200568
569cleanup:
570 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
571 return ret;
David Sedlák11900c82019-06-18 16:29:12 +0200572}
573
David Sedlák11900c82019-06-18 16:29:12 +0200574LY_ERR
David Sedlák2721d3d2019-06-21 15:37:41 +0200575yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, struct lysp_ext **extensions)
576{
577 LY_ERR ret = LY_SUCCESS;
578 const char *temp_val = NULL, *prefix, *name;
David Sedlák2721d3d2019-06-21 15:37:41 +0200579 size_t prefix_len, name_len;
580 struct yin_arg_record *subelem_args = NULL;
David Sedlák2721d3d2019-06-21 15:37:41 +0200581 enum yang_keyword kw = YANG_NONE;
David Sedlák9bc65872019-06-24 20:21:08 +0200582 struct yin_arg_record temp_record;
David Sedlák2721d3d2019-06-21 15:37:41 +0200583
584 if (*flags & LYS_YINELEM_MASK) {
585 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "yin-element");
586 return LY_EVALID;
587 }
588
589 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_VALUE, &temp_val));
590 if (strcmp(temp_val, "true") == 0) {
591 *flags |= LYS_YINELEM_TRUE;
592 } else if (strcmp(temp_val, "false") == 0) {
593 *flags |= LYS_YINELEM_FALSE;
594 } else {
595 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
596 lydict_remove(xml_ctx->ctx, temp_val);
597 return LY_EVALID;
598 }
599 lydict_remove(xml_ctx->ctx, temp_val);
600
David Sedlák9bc65872019-06-24 20:21:08 +0200601 YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args);
David Sedlák9bc65872019-06-24 20:21:08 +0200602 switch (kw) {
603 case YANG_CUSTOM:
604 // TODO parse extension instance
605 break;
606
607 default:
608 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
609 return LY_EVALID;
610 }
611 YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record);
David Sedlák2721d3d2019-06-21 15:37:41 +0200612
613cleanup:
614 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
615 return ret;
616}
617
618LY_ERR
David Sedlák1e2cdd02019-06-27 14:17:43 +0200619yin_parse_extension_instance(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
620 const char *ext_name, int ext_name_len, LYEXT_SUBSTMT insubstmt,
621 uint32_t insubstmt_index, struct lysp_ext_instance **exts)
622{
623 LY_ERR ret = LY_SUCCESS;
624 struct lysp_ext_instance *e;
625 struct yin_arg_record *iter, *subelem_args = NULL, temp_record;
626 struct lysp_stmt *new, *current;
627 enum yang_keyword kw = YANG_NONE;
628
629 LY_ARRAY_NEW_RET(xml_ctx->ctx, *exts, e, LY_EMEM);
630
631 e->yin = 0;
632 /* store name and insubstmt info */
633 e->name = lydict_insert(xml_ctx->ctx, ext_name, ext_name_len);
634 e->insubstmt = insubstmt;
635 e->insubstmt_index = insubstmt_index;
636 e->yin |= LYS_YIN;
637
638 current = e->insubstmt_index;
639 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
640 if (iter->prefix_len == 0) {
641 new = calloc(1, sizeof(*new));
642 LY_CHECK_ERR_RET(new, LOGMEM(xml_ctx->ctx), LY_EMEM);
643 current->next = new;
644 current = new;
645 new->flags |= LYS_YIN_ATTR;
646 new->stmt = lydict_insert(xml_ctx->ctx, iter->name - iter->prefix_len + 1, iter->name_len + iter->prefix_len + 1);
647 if (iter->dynamic_content) {
648 new->arg = iter->content;
649 new->arg = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
650 } else {
651 new->arg = lydict_insert_zc(xml_ctx->ctx, iter->content);
652 }
653 }
654 }
655 YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args);
656 /* parse subelems */
657 YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record);
658
659cleanup:
660 return ret;
661}
662
663LY_ERR
David Sedlák9494eb22019-06-21 16:06:53 +0200664yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
665 uint16_t *flags, const char **argument, struct lysp_ext **extensions)
666{
667 LY_ERR ret = LY_SUCCESS;
668 const char *name, *prefix;
669 size_t name_len, prefix_len;
670 struct yin_arg_record *subelem_args = NULL;
David Sedlák9494eb22019-06-21 16:06:53 +0200671 enum yang_keyword kw = YANG_NONE;
David Sedlák9bc65872019-06-24 20:21:08 +0200672 struct yin_arg_record temp_record;
David Sedlák9494eb22019-06-21 16:06:53 +0200673
674 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, argument));
675
David Sedlák9bc65872019-06-24 20:21:08 +0200676 YIN_READ_SUBELEMS_START(xml_ctx, data, ret, cleanup, kw, temp_record, subelem_args);
677 if (!name) {
678 /* end of argument element reached */
679 break;
David Sedlák9494eb22019-06-21 16:06:53 +0200680 }
David Sedlák9bc65872019-06-24 20:21:08 +0200681
682 switch (kw) {
683 case YANG_YIN_ELEMENT:
684 yin_parse_yin_element_element(xml_ctx, &subelem_args, data, flags, extensions);
685 break;
686 case YANG_CUSTOM:
687 // TODO parse extension instance
688 break;
689
690 default:
691 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
692 return LY_EVALID;
693 }
694 YIN_READ_SUBELEMS_END(xml_ctx, data, temp_record);
David Sedlák9494eb22019-06-21 16:06:53 +0200695
696cleanup:
697 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
698 return ret;
699}
700
701LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200702yin_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 +0200703{
704 LY_ERR ret = LY_SUCCESS;
705 struct lysp_ext *ex;
706 const char *prefix = NULL, *name = NULL;
707 char *out = NULL;
708 size_t out_len = 0, prefix_len = 0, name_len = 0;
709 int dynamic = 0;
710 enum yang_keyword kw = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200711 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200712
David Sedlák00250342019-06-21 14:19:39 +0200713 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup);
714 ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name);
715 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200716 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200717 LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element.");
718 ret = LY_EINVAL, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200719
720 while (xml_ctx->status == LYXML_ELEMENT) {
721 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200722 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200723 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200724 /* end of extension element reached */
David Sedlák11900c82019-06-18 16:29:12 +0200725 break;
726 }
David Sedlák00250342019-06-21 14:19:39 +0200727 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
728 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200729 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák11900c82019-06-18 16:29:12 +0200730 switch (kw) {
731 case YANG_ARGUMENT:
David Sedlák9494eb22019-06-21 16:06:53 +0200732 ret = yin_parse_argument_element(xml_ctx, &subelem_args, data, &ex->flags, &ex->argument, extensions);
David Sedlák11900c82019-06-18 16:29:12 +0200733 break;
734 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200735 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc);
736 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200737 break;
738 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200739 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref);
740 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200741 break;
742 case YANG_STATUS:
David Sedlák00250342019-06-21 14:19:39 +0200743 ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts);
744 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200745 break;
746 case YANG_CUSTOM:
747 /* TODO parse extension instance */
748 break;
749 default:
David Sedlákb3077192019-06-19 10:55:37 +0200750 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension");
David Sedlák00250342019-06-21 14:19:39 +0200751 ret = LY_EVALID;
752 goto cleanup;
David Sedlák11900c82019-06-18 16:29:12 +0200753 }
David Sedlák00250342019-06-21 14:19:39 +0200754 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák288c1472019-06-20 16:09:48 +0200755 subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200756 }
757
David Sedlák00250342019-06-21 14:19:39 +0200758cleanup:
759 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák11900c82019-06-18 16:29:12 +0200760 return ret;
761}
762
David Sedlák00250342019-06-21 14:19:39 +0200763/**
764 * @brief Parse module substatements.
765 *
766 * @param[in] xml_ctx Xml context.
767 * @param[in,out] data Data to read from.
768 * @param[out] mod Parsed module structure.
769 *
770 * @return LY_ERR values.
771 */
772LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200773yin_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 +0200774{
David Sedlákf3b24f62018-11-02 10:40:47 +0100775 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100776 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100777 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200778 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100779 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200780
David Sedlákf3b24f62018-11-02 10:40:47 +0100781 char *buf = NULL, *out = NULL;
782 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200783 int dynamic = 0;
David Sedlák8f7a1172019-06-20 14:42:18 +0200784 struct yin_arg_record *substmt_args = NULL;
David Sedlákf3b24f62018-11-02 10:40:47 +0100785
David Sedlák8f7a1172019-06-20 14:42:18 +0200786 yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlákb3077192019-06-19 10:55:37 +0200787 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 +0100788 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200789 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 +0100790
David Sedlák736fd0d2019-02-15 16:06:31 +0100791 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100792 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100793#define CHECK_ORDER(SECTION) \
794 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
795
796 switch (kw) {
797 /* module header */
798 case YANG_NAMESPACE:
799 case YANG_PREFIX:
800 CHECK_ORDER(Y_MOD_MODULE_HEADER);
801 break;
802 case YANG_YANG_VERSION:
803 CHECK_ORDER(Y_MOD_MODULE_HEADER);
804 break;
805 /* linkage */
806 case YANG_INCLUDE:
807 case YANG_IMPORT:
808 CHECK_ORDER(Y_MOD_LINKAGE);
809 break;
810 /* meta */
811 case YANG_ORGANIZATION:
812 case YANG_CONTACT:
813 case YANG_DESCRIPTION:
814 case YANG_REFERENCE:
815 CHECK_ORDER(Y_MOD_META);
816 break;
817
818 /* revision */
819 case YANG_REVISION:
820 CHECK_ORDER(Y_MOD_REVISION);
821 break;
822 /* body */
823 case YANG_ANYDATA:
824 case YANG_ANYXML:
825 case YANG_AUGMENT:
826 case YANG_CHOICE:
827 case YANG_CONTAINER:
828 case YANG_DEVIATION:
829 case YANG_EXTENSION:
830 case YANG_FEATURE:
831 case YANG_GROUPING:
832 case YANG_IDENTITY:
833 case YANG_LEAF:
834 case YANG_LEAF_LIST:
835 case YANG_LIST:
836 case YANG_NOTIFICATION:
837 case YANG_RPC:
838 case YANG_TYPEDEF:
839 case YANG_USES:
840 case YANG_CUSTOM:
841 mod_stmt = Y_MOD_BODY;
842 break;
843 default:
844 /* error will be handled in the next switch */
845 break;
846 }
847#undef CHECK_ORDER
848
David Sedlák68826732019-06-05 10:50:58 +0200849 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákc136b972019-03-08 13:39:06 +0100850 if (name) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200851 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args));
852 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100853 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100854
David Sedlákc136b972019-03-08 13:39:06 +0100855 /* module header */
856 case YANG_NAMESPACE:
David Sedlák00250342019-06-21 14:19:39 +0200857 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns);
858 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100859 break;
860 case YANG_PREFIX:
David Sedlák00250342019-06-21 14:19:39 +0200861 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix);
862 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100863 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100864
David Sedlákc136b972019-03-08 13:39:06 +0100865 /* linkage */
866 case YANG_IMPORT:
David Sedlák00250342019-06-21 14:19:39 +0200867 ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports);
868 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100869 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100870
David Sedlákc136b972019-03-08 13:39:06 +0100871 /* meta */
872 case YANG_ORGANIZATION:
David Sedlák00250342019-06-21 14:19:39 +0200873 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org);
874 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100875 break;
876 case YANG_CONTACT:
David Sedlák00250342019-06-21 14:19:39 +0200877 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact);
878 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100879 break;
880 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200881 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc);
882 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100883 break;
884 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200885 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref);
886 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100887 break;
David Sedlák11900c82019-06-18 16:29:12 +0200888 /* revision */
889
890 /*body */
891 case YANG_EXTENSION:
David Sedlák00250342019-06-21 14:19:39 +0200892 ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions);
893 LY_CHECK_GOTO(ret, error);
David Sedlák11900c82019-06-18 16:29:12 +0200894 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100895
David Sedlákc136b972019-03-08 13:39:06 +0100896 default:
David Sedlák00250342019-06-21 14:19:39 +0200897 ret = LY_EVALID;
898 goto error;
David Sedlákc136b972019-03-08 13:39:06 +0100899 }
David Sedlák00250342019-06-21 14:19:39 +0200900 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200901 substmt_args = NULL;
David Sedlák4a4c0722018-11-26 17:03:10 +0100902 }
903 }
904
David Sedlák68826732019-06-05 10:50:58 +0200905 return LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200906
907error:
908 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
909 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200910}
911
912LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100913yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200914{
David Sedláke4889912018-11-02 09:52:40 +0100915 LY_ERR ret = LY_SUCCESS;
916 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200917 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200918 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100919 struct lysp_module *mod_p = NULL;
920 const char *prefix, *name;
921 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200922 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200923
David Sedlák3017da42019-02-15 09:48:04 +0100924 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200925 memset(&parser_ctx, 0, sizeof parser_ctx);
926 xml_ctx->ctx = ctx;
927 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100928
David Sedlák3017da42019-02-15 09:48:04 +0100929 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200930 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200931 LY_CHECK_GOTO(ret, cleanup);
932 ret = yin_load_attributes(xml_ctx, &data, &args);
933 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200934 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100935 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100936 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
937 ret = LY_EINVAL;
938 goto cleanup;
939 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +0200940 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200941 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100942 ret = LY_EVALID;
943 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100944 }
945
David Sedlák3017da42019-02-15 09:48:04 +0100946 /* allocate module */
947 mod_p = calloc(1, sizeof *mod_p);
948 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
949 mod_p->mod = mod;
950 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100951
David Sedlák00250342019-06-21 14:19:39 +0200952 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +0200953 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100954 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100955
David Sedlák3017da42019-02-15 09:48:04 +0100956 mod_p->parsing = 0;
957 mod->parsed = mod_p;
958
959cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +0200960 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +0100961 lysp_module_free(mod_p);
962 }
David Sedlák00250342019-06-21 14:19:39 +0200963 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +0200964 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100965 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200966}