blob: b01812438ebc8a9f6355ec0b46935d64b27b7ab4 [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ákf6251182019-06-06 10:22:13 +020037const char *const yin_attr_list[] = {
38 [YIN_ARG_NAME] = "name",
39 [YIN_ARG_TARGET_NODE] = "target-node",
40 [YIN_ARG_MODULE] = "module",
41 [YIN_ARG_VALUE] = "value",
42 [YIN_ARG_TEXT] = "text",
43 [YIN_ARG_CONDITION] = "condition",
44 [YIN_ARG_URI] = "uri",
45 [YIN_ARG_DATE] = "date",
46 [YIN_ARG_TAG] = "tag",
47 [YIN_ARG_XMLNS] = "xmlns",
48};
49
David Sedlák1bccdfa2019-06-17 15:55:27 +020050enum yang_keyword
David Sedlák8f7a1172019-06-20 14:42:18 +020051yin_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 +020052{
David Sedlák8f7a1172019-06-20 14:42:18 +020053 const char *start = NULL;
54 enum yang_keyword kw = YANG_NONE;
55 const struct lyxml_ns *ns = NULL;
56
57 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020058 return YANG_NONE;
59 }
60
David Sedlák8f7a1172019-06-20 14:42:18 +020061 ns = lyxml_ns_get(xml_ctx, prefix, prefix_len);
62 if (ns) {
63 if (!IS_YIN_NS(ns->uri)) {
64 return YANG_CUSTOM;
65 }
66 } else {
67 /* elements without namespace are automatically unknown */
68 return YANG_NONE;
69 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020070
David Sedlák8f7a1172019-06-20 14:42:18 +020071
72 start = name;
73 kw = lysp_match_kw(NULL, &name);
74
75 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020076 return kw;
77 } else {
78 return YANG_NONE;
79 }
80}
81
David Sedlák872c7b42018-10-26 13:15:20 +020082enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020083yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020084{
David Sedláka7406952019-04-05 10:33:07 +020085 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020086 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020087 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020088
David Sedlák94de2aa2019-02-15 12:42:11 +010089#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
90#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010091#define IF_ARG_PREFIX_END }
92
David Sedlák1c8b2702019-02-22 11:03:02 +010093 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +010094 case 'x':
95 already_read += 1;
96 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
97 break;
David Sedlák94de2aa2019-02-15 12:42:11 +010098 case 'c':
99 already_read += 1;
100 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200101 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200102
David Sedlák94de2aa2019-02-15 12:42:11 +0100103 case 'd':
104 already_read += 1;
105 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200106 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200107
David Sedlák94de2aa2019-02-15 12:42:11 +0100108 case 'm':
109 already_read += 1;
110 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200111 break;
112
David Sedlák94de2aa2019-02-15 12:42:11 +0100113 case 'n':
114 already_read += 1;
115 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200116 break;
117
David Sedlák94de2aa2019-02-15 12:42:11 +0100118 case 't':
119 already_read += 1;
120 IF_ARG_PREFIX("a", 1)
121 IF_ARG("g", 1, YIN_ARG_TAG)
122 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
123 IF_ARG_PREFIX_END
124 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200125 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200126
David Sedlák94de2aa2019-02-15 12:42:11 +0100127 case 'u':
128 already_read += 1;
129 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200130 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200131
David Sedlák94de2aa2019-02-15 12:42:11 +0100132 case 'v':
133 already_read += 1;
134 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200135 break;
136 }
137
David Sedlákc10e7902018-12-17 02:17:59 +0100138 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200139 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200140 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200141 }
142
David Sedlák18730132019-03-15 15:51:34 +0100143#undef IF_ARG
144#undef IF_ARG_PREFIX
145#undef IF_ARG_PREFIX_END
146
David Sedlák872c7b42018-10-26 13:15:20 +0200147 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200148}
149
David Sedlák00250342019-06-21 14:19:39 +0200150static void free_arg_rec(struct lyxml_context *xml_ctx, struct yin_arg_record *record) {
151 (void)xml_ctx; /* unused */
152 if (record->dynamic_content) {
153 free(record->content);
154 }
155}
156
David Sedlák8f7a1172019-06-20 14:42:18 +0200157LY_ERR
158yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args)
David Sedláka7406952019-04-05 10:33:07 +0200159{
160 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200161 struct yin_arg_record *argument_record = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200162
David Sedlák7ff55a92019-06-17 11:11:41 +0200163 /* load all attributes first */
David Sedlák57715b12019-06-17 13:05:22 +0200164 while (xml_ctx->status == LYXML_ATTRIBUTE) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200165 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *args, argument_record, ret, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200166 ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len,
167 &argument_record->name, &argument_record->name_len);
David Sedlák00250342019-06-21 14:19:39 +0200168 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200169
David Sedlák7ff55a92019-06-17 11:11:41 +0200170 if (xml_ctx->status == LYXML_ATTR_CONTENT) {
David Sedlák57715b12019-06-17 13:05:22 +0200171 argument_record->content = NULL;
172 argument_record->content_len = 0;
173 argument_record->dynamic_content = 0;
174 ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len,
175 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200176 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200177 }
178 }
179
David Sedlák8f7a1172019-06-20 14:42:18 +0200180cleanup:
181 if (ret != LY_SUCCESS) {
David Sedlák00250342019-06-21 14:19:39 +0200182 FREE_ARRAY(xml_ctx, *args, free_arg_rec);
183 *args = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200184 }
185 return ret;
186}
187
188LY_ERR
189yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, const char **arg_val)
190{
191 LY_ERR ret = LY_SUCCESS;
192 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
193 struct yin_arg_record *iter = NULL;
194 const struct lyxml_ns *ns = NULL;
195
David Sedlák1bccdfa2019-06-17 15:55:27 +0200196 /* validation of attributes */
David Sedlák8f7a1172019-06-20 14:42:18 +0200197 LY_ARRAY_FOR(*args, struct yin_arg_record, iter) {
David Sedlák7ff55a92019-06-17 11:11:41 +0200198 ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len);
David Sedlák00250342019-06-21 14:19:39 +0200199 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
200 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200201 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200202 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200203 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200204 } else if (arg == arg_type) {
David Sedlák57715b12019-06-17 13:05:22 +0200205 if (iter->dynamic_content) {
206 *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content);
David Sedlák00250342019-06-21 14:19:39 +0200207 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
208 /* string is no longer supposed to be freed when the sized array is freed */
209 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200210 } else {
211 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
212 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
213 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200214 } else {
David Sedlák7ff55a92019-06-17 11:11:41 +0200215 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name);
216 ret = LY_EVALID;
217 goto cleanup;
David Sedláka7406952019-04-05 10:33:07 +0200218 }
219 }
220 }
221
David Sedlák7ff55a92019-06-17 11:11:41 +0200222cleanup:
David Sedlák7ff55a92019-06-17 11:11:41 +0200223 return ret;
David Sedláka7406952019-04-05 10:33:07 +0200224}
225
David Sedlák0daba9a2019-03-22 14:07:49 +0100226LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200227yin_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 +0100228{
229 LY_ERR ret = LY_SUCCESS;
230 char *buf = NULL, *out = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200231 const char *prefix = NULL, *name = NULL;
232 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200233 int dynamic = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100234
David Sedlák0daba9a2019-03-22 14:07:49 +0100235 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
236 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
237 LY_CHECK_RET(ret);
David Sedlák57715b12019-06-17 13:05:22 +0200238 if (dynamic) {
239 *value = lydict_insert_zc(xml_ctx->ctx, buf);
David Sedlák00250342019-06-21 14:19:39 +0200240 LY_CHECK_RET(!(*value), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200241 } else {
242 *value = lydict_insert(xml_ctx->ctx, out, out_len);
David Sedlák00250342019-06-21 14:19:39 +0200243 LY_CHECK_RET(!(*value), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200244 }
David Sedlák0daba9a2019-03-22 14:07:49 +0100245 }
246
David Sedlák00250342019-06-21 14:19:39 +0200247 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
248 /* probably should never happen, mixed content should be caught by lyxml_get_element call but
249 * I will just leave it here for now to be sure */
250 LY_CHECK_RET(name, LY_EVALID);
251 return ret;
David Sedlák15a92662019-06-18 11:55:15 +0200252}
253
David Sedlák00250342019-06-21 14:19:39 +0200254/**
255 * @brief function to parse meta tags eg. elements with text element as child
256 *
257 * @param[in] xml_ctx Xml context.
258 * @param[in] args Sized array of arguments of current elements.
259 * @param[in,out] data Data to read from.
260 * @param[out] value Where the content of meta tag should be stored.
261 *
262 * @return LY_ERR values.
263 */
David Sedlák15a92662019-06-18 11:55:15 +0200264LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200265yin_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 +0200266{
267 LY_ERR ret = LY_SUCCESS;
268 char *buf = NULL, *out = NULL;
David Sedlák00250342019-06-21 14:19:39 +0200269 const char *prefix = NULL, *name = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200270 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
271 int dynamic = 0;
David Sedlák15a92662019-06-18 11:55:15 +0200272 enum YIN_ARGUMENT arg = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200273 struct yin_arg_record *subelem_args = NULL;
274 enum yang_keyword kw = YANG_NONE;
David Sedlák15a92662019-06-18 11:55:15 +0200275
David Sedlák00250342019-06-21 14:19:39 +0200276 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 +0200277
278 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200279 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 +0200280
David Sedlák8f7a1172019-06-20 14:42:18 +0200281 /* first element should be argument element <text> */
David Sedlák8f7a1172019-06-20 14:42:18 +0200282 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák00250342019-06-21 14:19:39 +0200283 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
284 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200285 arg = yin_match_argument_name(name, name_len);
David Sedlák00250342019-06-21 14:19:39 +0200286 LY_CHECK_ERR_GOTO(arg != YIN_ARG_TEXT, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as first child of meta element.");
287 ret = LY_EINVAL, cleanup);
288 ret = yin_parse_text_element(xml_ctx, &subelem_args, data, value);
289 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200290
David Sedlák15a92662019-06-18 11:55:15 +0200291 /* loop over all child elements and parse them */
292 while (xml_ctx->status == LYXML_ELEMENT) {
David Sedlák00250342019-06-21 14:19:39 +0200293 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
294 LY_CHECK_GOTO(ret, cleanup);
295 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
296 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200297 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák15a92662019-06-18 11:55:15 +0200298
David Sedlák8f7a1172019-06-20 14:42:18 +0200299 if (!name) {
David Sedlák15a92662019-06-18 11:55:15 +0200300 /* end of meta element reached */
301 break;
302 }
303
David Sedlák8f7a1172019-06-20 14:42:18 +0200304 switch (kw) {
305 case YANG_CUSTOM:
306 // TODO parse extension instance
307 break;
308
309 default:
310 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
311 return LY_EVALID;
312 }
David Sedlák15a92662019-06-18 11:55:15 +0200313 }
314
David Sedlák00250342019-06-21 14:19:39 +0200315cleanup:
316 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
317 return ret;
David Sedlák0daba9a2019-03-22 14:07:49 +0100318}
319
David Sedlák00250342019-06-21 14:19:39 +0200320/**
321 * @brief Parse revision date.
322 *
323 * @param[in] xml_ctx Xml context.
324 * @param[in] args Sized array of arguments of current element.
325 * @param[in,out] data Data to read from.
326 * @param[in,out] rev Array to store the parsed value in.
327 * @param[in,out] exts Extension instances to add to.
328 *
329 * @return LY_ERR values.
330 */
David Sedlák81e04022019-04-05 15:05:46 +0200331static LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200332yin_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 +0200333{
334 LY_ERR ret = LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200335 const char *temp_rev, *prefix = NULL, *name = NULL;
336 char *out;
337 struct yin_arg_record *subelem_args = NULL;
338 size_t prefix_len = 0, name_len = 0;
339 enum yang_keyword kw = YANG_NONE;
340 int dynamic;
David Sedlákcd0c9512019-03-29 13:23:06 +0100341
David Sedlák81e04022019-04-05 15:05:46 +0200342 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200343 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
David Sedlák81e04022019-04-05 15:05:46 +0200344 return LY_EVALID;
345 }
346
David Sedlák8f7a1172019-06-20 14:42:18 +0200347 LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200348 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200349 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
350
351 strcpy(rev, temp_rev);
352 lydict_remove(xml_ctx->ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200353
David Sedlák00250342019-06-21 14:19:39 +0200354 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
355 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
356 /* unknown element text content is ignored */
357 if (ret == LY_EINVAL) {
358 while (xml_ctx->status == LYXML_ELEMENT) {
359 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
360 LY_CHECK_GOTO(ret, cleanup);
361 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
362 LY_CHECK_GOTO(ret, cleanup);
363 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
364
365 if (!name) {
366 /* end of revisin-date element reached */
367 break;
368 }
369
370 switch (kw) {
371 case YANG_CUSTOM:
372 // TODO parse extension instance
373 break;
374
375 default:
376 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
377 return LY_EVALID;
378 }
379 }
380 } else {
381 /* load closing element */
382 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
383 LY_CHECK_RET(name, LY_EINVAL);
384 }
385 }
386
387cleanup:
388 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák81e04022019-04-05 15:05:46 +0200389 return ret;
390}
David Sedlákcd0c9512019-03-29 13:23:06 +0100391
David Sedlákda63c082019-06-04 13:52:23 +0200392LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200393yin_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 +0100394{
395 LY_ERR ret = LY_SUCCESS;
396 enum yang_keyword kw;
397 struct lysp_import *imp;
398 const char *prefix, *name;
David Sedlák00250342019-06-21 14:19:39 +0200399 char *out;
David Sedlák8f7a1172019-06-20 14:42:18 +0200400 struct yin_arg_record *subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100401 size_t prefix_len, name_len;
David Sedlák00250342019-06-21 14:19:39 +0200402 int dynamic;
David Sedlák736fd0d2019-02-15 16:06:31 +0100403
David Sedlák00250342019-06-21 14:19:39 +0200404 /* allocate new element in sized array for import */
David Sedlák8f7a1172019-06-20 14:42:18 +0200405 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *imports, imp, ret, validation_err);
David Sedlák736fd0d2019-02-15 16:06:31 +0100406
David Sedláka7406952019-04-05 10:33:07 +0200407 /* parse import attributes */
David Sedlák00250342019-06-21 14:19:39 +0200408 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name) != LY_SUCCESS, validation_err);
409
410 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
411 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after import element"), LY_EINVAL);
412
413 while (xml_ctx->status == LYXML_ELEMENT) {
414 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
415 LY_CHECK_GOTO(ret, validation_err);
416 if (!name) {
417 /* end of import element reached */
418 break;
419 }
420 LY_CHECK_GOTO(yin_load_attributes(xml_ctx, data, &subelem_args), validation_err);
David Sedlák8f7a1172019-06-20 14:42:18 +0200421 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák00250342019-06-21 14:19:39 +0200422
David Sedlák736fd0d2019-02-15 16:06:31 +0100423 switch (kw) {
424 case YANG_PREFIX:
David Sedlák8f7a1172019-06-20 14:42:18 +0200425 LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err);
426 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err);
427 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 +0100428 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100429 case YANG_DESCRIPTION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200430 LY_CHECK_ERR_GOTO(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), validation_err);
David Sedlák00250342019-06-21 14:19:39 +0200431 LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->dsc), validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100432 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100433 case YANG_REFERENCE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200434 LY_CHECK_ERR_GOTO(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), validation_err);
David Sedlák00250342019-06-21 14:19:39 +0200435 LY_CHECK_GOTO(yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->ref), validation_err);
David Sedlákcd0c9512019-03-29 13:23:06 +0100436 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100437 case YANG_REVISION_DATE:
David Sedlák00250342019-06-21 14:19:39 +0200438 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 +0200439 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100440 case YANG_CUSTOM:
441 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200442 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100443 default:
David Sedlák916b1902019-06-06 11:35:05 +0200444 LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import");
David Sedlák8f7a1172019-06-20 14:42:18 +0200445 goto validation_err;
David Sedlák736fd0d2019-02-15 16:06:31 +0100446 }
David Sedlák4af209e2019-06-21 14:24:50 +0200447 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200448 subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100449 }
450
David Sedlák8f7a1172019-06-20 14:42:18 +0200451 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 +0100452 return ret;
David Sedlák8f7a1172019-06-20 14:42:18 +0200453
454validation_err:
David Sedlák4af209e2019-06-21 14:24:50 +0200455 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200456 return LY_EVALID;
David Sedlák736fd0d2019-02-15 16:06:31 +0100457}
458
David Sedlák11900c82019-06-18 16:29:12 +0200459LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200460yin_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 +0200461{
462 LY_ERR ret = LY_SUCCESS;
463 enum yang_keyword kw = YANG_NONE;
464 const char *value = NULL, *prefix = NULL, *name = NULL;
465 char *out;
466 size_t prefix_len = 0, name_len = 0, out_len = 0;
467 int dynamic = 0;
David Sedlák00250342019-06-21 14:19:39 +0200468 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200469 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200470 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200471 return LY_EVALID;
472 }
473
David Sedlák8f7a1172019-06-20 14:42:18 +0200474 LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value));
David Sedlák11900c82019-06-18 16:29:12 +0200475 if (strcmp(value, "current") == 0) {
476 *flags |= LYS_STATUS_CURR;
477 } else if (strcmp(value, "deprecated") == 0) {
478 *flags |= LYS_STATUS_DEPRC;
479 } else if (strcmp(value, "obsolete") == 0) {
480 *flags |= LYS_STATUS_OBSLT;
481 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200482 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200483 lydict_remove(xml_ctx->ctx, value);
484 return LY_EVALID;
485 }
486 lydict_remove(xml_ctx->ctx, value);
487
David Sedlákb6e65972019-06-19 10:44:13 +0200488 /* TODO check if dynamic was set to 1 in case of error */
489 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
490 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
491 /* if there are any xml subelements parse them, unknown text content is silently ignored */
492 if (ret == LY_EINVAL) {
493 /* load subelements */
494 while (xml_ctx->status == LYXML_ELEMENT) {
495 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
496 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200497 /* end of status reached */
David Sedlákb6e65972019-06-19 10:44:13 +0200498 break;
499 }
David Sedlák00250342019-06-21 14:19:39 +0200500 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
501 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200502 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákb6e65972019-06-19 10:44:13 +0200503 switch (kw) {
504 case YANG_CUSTOM:
505 /* TODO parse extension instance */
506 break;
507 default:
David Sedlákb3077192019-06-19 10:55:37 +0200508 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status");
David Sedlák00250342019-06-21 14:19:39 +0200509 ret = LY_EVALID;
510 goto cleanup;
David Sedlákb6e65972019-06-19 10:44:13 +0200511 }
David Sedlák00250342019-06-21 14:19:39 +0200512
513 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
514 subelem_args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200515 }
David Sedlák00250342019-06-21 14:19:39 +0200516 } else {
517 /* load closing element */
518 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
519 LY_CHECK_RET(name, LY_EINVAL);
David Sedlák11900c82019-06-18 16:29:12 +0200520 }
521 }
522
523 return ret;
David Sedlák00250342019-06-21 14:19:39 +0200524
525cleanup:
526 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
527 return ret;
David Sedlák11900c82019-06-18 16:29:12 +0200528}
529
David Sedlák11900c82019-06-18 16:29:12 +0200530LY_ERR
David Sedlák2721d3d2019-06-21 15:37:41 +0200531yin_parse_yin_element_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, struct lysp_ext **extensions)
532{
533 LY_ERR ret = LY_SUCCESS;
534 const char *temp_val = NULL, *prefix, *name;
535 char *out;
536 size_t prefix_len, name_len;
537 struct yin_arg_record *subelem_args = NULL;
538 int dynamic;
539 enum yang_keyword kw = YANG_NONE;
540
541 if (*flags & LYS_YINELEM_MASK) {
542 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "yin-element");
543 return LY_EVALID;
544 }
545
546 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_VALUE, &temp_val));
547 if (strcmp(temp_val, "true") == 0) {
548 *flags |= LYS_YINELEM_TRUE;
549 } else if (strcmp(temp_val, "false") == 0) {
550 *flags |= LYS_YINELEM_FALSE;
551 } else {
552 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
553 lydict_remove(xml_ctx->ctx, temp_val);
554 return LY_EVALID;
555 }
556 lydict_remove(xml_ctx->ctx, temp_val);
557
558 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
559 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
560 /* unknown element text content is ignored */
561 if (ret == LY_EINVAL) {
562 while (xml_ctx->status == LYXML_ELEMENT) {
563 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
564 LY_CHECK_GOTO(ret, cleanup);
565 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
566 LY_CHECK_GOTO(ret, cleanup);
567 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
568
569 if (!name) {
570 /* end of yin-element element reached */
571 break;
572 }
573
574 switch (kw) {
575 case YANG_CUSTOM:
576 // TODO parse extension instance
577 break;
578
579 default:
580 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
581 return LY_EVALID;
582 }
583 }
584 } else {
585 /* load closing element */
586 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
587 LY_CHECK_RET(name, LY_EINVAL);
588 }
589 }
590
591cleanup:
592 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
593 return ret;
594}
595
596LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200597yin_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 +0200598{
599 LY_ERR ret = LY_SUCCESS;
600 struct lysp_ext *ex;
601 const char *prefix = NULL, *name = NULL;
602 char *out = NULL;
603 size_t out_len = 0, prefix_len = 0, name_len = 0;
604 int dynamic = 0;
605 enum yang_keyword kw = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200606 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200607
David Sedlák00250342019-06-21 14:19:39 +0200608 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup);
609 ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name);
610 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200611 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200612 LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element.");
613 ret = LY_EINVAL, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200614
615 while (xml_ctx->status == LYXML_ELEMENT) {
616 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200617 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200618 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200619 /* end of extension element reached */
David Sedlák11900c82019-06-18 16:29:12 +0200620 break;
621 }
David Sedlák00250342019-06-21 14:19:39 +0200622 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
623 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200624 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák11900c82019-06-18 16:29:12 +0200625 switch (kw) {
626 case YANG_ARGUMENT:
David Sedlák554e36d2019-06-20 16:00:04 +0200627 /* TODO */
David Sedlák11900c82019-06-18 16:29:12 +0200628 break;
629 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200630 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc);
631 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200632 break;
633 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200634 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref);
635 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200636 break;
637 case YANG_STATUS:
David Sedlák00250342019-06-21 14:19:39 +0200638 ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts);
639 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200640 break;
641 case YANG_CUSTOM:
642 /* TODO parse extension instance */
643 break;
644 default:
David Sedlákb3077192019-06-19 10:55:37 +0200645 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension");
David Sedlák00250342019-06-21 14:19:39 +0200646 ret = LY_EVALID;
647 goto cleanup;
David Sedlák11900c82019-06-18 16:29:12 +0200648 }
David Sedlák00250342019-06-21 14:19:39 +0200649 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák288c1472019-06-20 16:09:48 +0200650 subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200651 }
652
David Sedlák00250342019-06-21 14:19:39 +0200653cleanup:
654 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák11900c82019-06-18 16:29:12 +0200655 return ret;
656}
657
David Sedlák00250342019-06-21 14:19:39 +0200658/**
659 * @brief Parse module substatements.
660 *
661 * @param[in] xml_ctx Xml context.
662 * @param[in,out] data Data to read from.
663 * @param[out] mod Parsed module structure.
664 *
665 * @return LY_ERR values.
666 */
667LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200668yin_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 +0200669{
David Sedlákf3b24f62018-11-02 10:40:47 +0100670 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100671 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100672 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200673 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100674 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200675
David Sedlákf3b24f62018-11-02 10:40:47 +0100676 char *buf = NULL, *out = NULL;
677 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200678 int dynamic = 0;
David Sedlák8f7a1172019-06-20 14:42:18 +0200679 struct yin_arg_record *substmt_args = NULL;
David Sedlákf3b24f62018-11-02 10:40:47 +0100680
David Sedlák8f7a1172019-06-20 14:42:18 +0200681 yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlákb3077192019-06-19 10:55:37 +0200682 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 +0100683 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200684 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 +0100685
David Sedlák736fd0d2019-02-15 16:06:31 +0100686 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100687 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100688#define CHECK_ORDER(SECTION) \
689 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
690
691 switch (kw) {
692 /* module header */
693 case YANG_NAMESPACE:
694 case YANG_PREFIX:
695 CHECK_ORDER(Y_MOD_MODULE_HEADER);
696 break;
697 case YANG_YANG_VERSION:
698 CHECK_ORDER(Y_MOD_MODULE_HEADER);
699 break;
700 /* linkage */
701 case YANG_INCLUDE:
702 case YANG_IMPORT:
703 CHECK_ORDER(Y_MOD_LINKAGE);
704 break;
705 /* meta */
706 case YANG_ORGANIZATION:
707 case YANG_CONTACT:
708 case YANG_DESCRIPTION:
709 case YANG_REFERENCE:
710 CHECK_ORDER(Y_MOD_META);
711 break;
712
713 /* revision */
714 case YANG_REVISION:
715 CHECK_ORDER(Y_MOD_REVISION);
716 break;
717 /* body */
718 case YANG_ANYDATA:
719 case YANG_ANYXML:
720 case YANG_AUGMENT:
721 case YANG_CHOICE:
722 case YANG_CONTAINER:
723 case YANG_DEVIATION:
724 case YANG_EXTENSION:
725 case YANG_FEATURE:
726 case YANG_GROUPING:
727 case YANG_IDENTITY:
728 case YANG_LEAF:
729 case YANG_LEAF_LIST:
730 case YANG_LIST:
731 case YANG_NOTIFICATION:
732 case YANG_RPC:
733 case YANG_TYPEDEF:
734 case YANG_USES:
735 case YANG_CUSTOM:
736 mod_stmt = Y_MOD_BODY;
737 break;
738 default:
739 /* error will be handled in the next switch */
740 break;
741 }
742#undef CHECK_ORDER
743
David Sedlák68826732019-06-05 10:50:58 +0200744 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákc136b972019-03-08 13:39:06 +0100745 if (name) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200746 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args));
747 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100748 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100749
David Sedlákc136b972019-03-08 13:39:06 +0100750 /* module header */
751 case YANG_NAMESPACE:
David Sedlák00250342019-06-21 14:19:39 +0200752 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns);
753 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100754 break;
755 case YANG_PREFIX:
David Sedlák00250342019-06-21 14:19:39 +0200756 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix);
757 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100758 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100759
David Sedlákc136b972019-03-08 13:39:06 +0100760 /* linkage */
761 case YANG_IMPORT:
David Sedlák00250342019-06-21 14:19:39 +0200762 ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports);
763 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100764 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100765
David Sedlákc136b972019-03-08 13:39:06 +0100766 /* meta */
767 case YANG_ORGANIZATION:
David Sedlák00250342019-06-21 14:19:39 +0200768 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org);
769 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100770 break;
771 case YANG_CONTACT:
David Sedlák00250342019-06-21 14:19:39 +0200772 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact);
773 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100774 break;
775 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200776 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc);
777 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100778 break;
779 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200780 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref);
781 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100782 break;
David Sedlák11900c82019-06-18 16:29:12 +0200783 /* revision */
784
785 /*body */
786 case YANG_EXTENSION:
David Sedlák00250342019-06-21 14:19:39 +0200787 ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions);
788 LY_CHECK_GOTO(ret, error);
David Sedlák11900c82019-06-18 16:29:12 +0200789 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100790
David Sedlákc136b972019-03-08 13:39:06 +0100791 default:
David Sedlák00250342019-06-21 14:19:39 +0200792 ret = LY_EVALID;
793 goto error;
David Sedlákc136b972019-03-08 13:39:06 +0100794 }
David Sedlák00250342019-06-21 14:19:39 +0200795 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200796 substmt_args = NULL;
David Sedlák4a4c0722018-11-26 17:03:10 +0100797 }
798 }
799
David Sedlák68826732019-06-05 10:50:58 +0200800 return LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200801
802error:
803 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
804 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200805}
806
807LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100808yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200809{
David Sedláke4889912018-11-02 09:52:40 +0100810 LY_ERR ret = LY_SUCCESS;
811 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200812 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200813 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100814 struct lysp_module *mod_p = NULL;
815 const char *prefix, *name;
816 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200817 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200818
David Sedlák3017da42019-02-15 09:48:04 +0100819 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200820 memset(&parser_ctx, 0, sizeof parser_ctx);
821 xml_ctx->ctx = ctx;
822 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100823
David Sedlák3017da42019-02-15 09:48:04 +0100824 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200825 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200826 LY_CHECK_GOTO(ret, cleanup);
827 ret = yin_load_attributes(xml_ctx, &data, &args);
828 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200829 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100830 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100831 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
832 ret = LY_EINVAL;
833 goto cleanup;
834 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +0200835 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200836 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100837 ret = LY_EVALID;
838 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100839 }
840
David Sedlák3017da42019-02-15 09:48:04 +0100841 /* allocate module */
842 mod_p = calloc(1, sizeof *mod_p);
843 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
844 mod_p->mod = mod;
845 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100846
David Sedlák00250342019-06-21 14:19:39 +0200847 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +0200848 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100849 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100850
David Sedlák3017da42019-02-15 09:48:04 +0100851 mod_p->parsing = 0;
852 mod->parsed = mod_p;
853
854cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +0200855 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +0100856 lysp_module_free(mod_p);
857 }
David Sedlák00250342019-06-21 14:19:39 +0200858 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +0200859 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100860 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200861}