blob: b66e31f060d60f70e7a4528d8fcc57bf6e73ae4f [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ák8f7a1172019-06-20 14:42:18 +0200447 LY_ARRAY_FREE(subelem_args);
448 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);
452 LY_ARRAY_FREE(subelem_args);
David Sedlák736fd0d2019-02-15 16:06:31 +0100453 return ret;
David Sedlák8f7a1172019-06-20 14:42:18 +0200454
455validation_err:
456 LY_ARRAY_FREE(subelem_args);
457 return LY_EVALID;
David Sedlák736fd0d2019-02-15 16:06:31 +0100458}
459
David Sedlák11900c82019-06-18 16:29:12 +0200460LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200461yin_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 +0200462{
463 LY_ERR ret = LY_SUCCESS;
464 enum yang_keyword kw = YANG_NONE;
465 const char *value = NULL, *prefix = NULL, *name = NULL;
466 char *out;
467 size_t prefix_len = 0, name_len = 0, out_len = 0;
468 int dynamic = 0;
David Sedlák00250342019-06-21 14:19:39 +0200469 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200470 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200471 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200472 return LY_EVALID;
473 }
474
David Sedlák8f7a1172019-06-20 14:42:18 +0200475 LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value));
David Sedlák11900c82019-06-18 16:29:12 +0200476 if (strcmp(value, "current") == 0) {
477 *flags |= LYS_STATUS_CURR;
478 } else if (strcmp(value, "deprecated") == 0) {
479 *flags |= LYS_STATUS_DEPRC;
480 } else if (strcmp(value, "obsolete") == 0) {
481 *flags |= LYS_STATUS_OBSLT;
482 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200483 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200484 lydict_remove(xml_ctx->ctx, value);
485 return LY_EVALID;
486 }
487 lydict_remove(xml_ctx->ctx, value);
488
David Sedlákb6e65972019-06-19 10:44:13 +0200489 /* TODO check if dynamic was set to 1 in case of error */
490 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
491 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
492 /* if there are any xml subelements parse them, unknown text content is silently ignored */
493 if (ret == LY_EINVAL) {
494 /* load subelements */
495 while (xml_ctx->status == LYXML_ELEMENT) {
496 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
497 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200498 /* end of status reached */
David Sedlákb6e65972019-06-19 10:44:13 +0200499 break;
500 }
David Sedlák00250342019-06-21 14:19:39 +0200501 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
502 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200503 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákb6e65972019-06-19 10:44:13 +0200504 switch (kw) {
505 case YANG_CUSTOM:
506 /* TODO parse extension instance */
507 break;
508 default:
David Sedlákb3077192019-06-19 10:55:37 +0200509 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status");
David Sedlák00250342019-06-21 14:19:39 +0200510 ret = LY_EVALID;
511 goto cleanup;
David Sedlákb6e65972019-06-19 10:44:13 +0200512 }
David Sedlák00250342019-06-21 14:19:39 +0200513
514 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
515 subelem_args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200516 }
David Sedlák00250342019-06-21 14:19:39 +0200517 } else {
518 /* load closing element */
519 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
520 LY_CHECK_RET(name, LY_EINVAL);
David Sedlák11900c82019-06-18 16:29:12 +0200521 }
522 }
523
524 return ret;
David Sedlák00250342019-06-21 14:19:39 +0200525
526cleanup:
527 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
528 return ret;
David Sedlák11900c82019-06-18 16:29:12 +0200529}
530
David Sedlák11900c82019-06-18 16:29:12 +0200531LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200532yin_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 +0200533{
534 LY_ERR ret = LY_SUCCESS;
535 struct lysp_ext *ex;
536 const char *prefix = NULL, *name = NULL;
537 char *out = NULL;
538 size_t out_len = 0, prefix_len = 0, name_len = 0;
539 int dynamic = 0;
540 enum yang_keyword kw = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200541 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200542
David Sedlák00250342019-06-21 14:19:39 +0200543 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup);
544 ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name);
545 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200546 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200547 LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element.");
548 ret = LY_EINVAL, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200549
550 while (xml_ctx->status == LYXML_ELEMENT) {
551 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200552 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200553 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200554 /* end of extension element reached */
David Sedlák11900c82019-06-18 16:29:12 +0200555 break;
556 }
David Sedlák00250342019-06-21 14:19:39 +0200557 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
558 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200559 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák11900c82019-06-18 16:29:12 +0200560 switch (kw) {
561 case YANG_ARGUMENT:
David Sedlák554e36d2019-06-20 16:00:04 +0200562 /* TODO */
David Sedlák11900c82019-06-18 16:29:12 +0200563 break;
564 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200565 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc);
566 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200567 break;
568 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200569 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref);
570 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200571 break;
572 case YANG_STATUS:
David Sedlák00250342019-06-21 14:19:39 +0200573 ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts);
574 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200575 break;
576 case YANG_CUSTOM:
577 /* TODO parse extension instance */
578 break;
579 default:
David Sedlákb3077192019-06-19 10:55:37 +0200580 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension");
David Sedlák00250342019-06-21 14:19:39 +0200581 ret = LY_EVALID;
582 goto cleanup;
David Sedlák11900c82019-06-18 16:29:12 +0200583 }
David Sedlák00250342019-06-21 14:19:39 +0200584 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák288c1472019-06-20 16:09:48 +0200585 subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200586 }
587
David Sedlák00250342019-06-21 14:19:39 +0200588cleanup:
589 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák11900c82019-06-18 16:29:12 +0200590 return ret;
591}
592
David Sedlák00250342019-06-21 14:19:39 +0200593/**
594 * @brief Parse module substatements.
595 *
596 * @param[in] xml_ctx Xml context.
597 * @param[in,out] data Data to read from.
598 * @param[out] mod Parsed module structure.
599 *
600 * @return LY_ERR values.
601 */
602LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200603yin_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 +0200604{
David Sedlákf3b24f62018-11-02 10:40:47 +0100605 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100606 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100607 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200608 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100609 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200610
David Sedlákf3b24f62018-11-02 10:40:47 +0100611 char *buf = NULL, *out = NULL;
612 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200613 int dynamic = 0;
David Sedlák8f7a1172019-06-20 14:42:18 +0200614 struct yin_arg_record *substmt_args = NULL;
David Sedlákf3b24f62018-11-02 10:40:47 +0100615
David Sedlák8f7a1172019-06-20 14:42:18 +0200616 yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlákb3077192019-06-19 10:55:37 +0200617 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 +0100618 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200619 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 +0100620
David Sedlák736fd0d2019-02-15 16:06:31 +0100621 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100622 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100623#define CHECK_ORDER(SECTION) \
624 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
625
626 switch (kw) {
627 /* module header */
628 case YANG_NAMESPACE:
629 case YANG_PREFIX:
630 CHECK_ORDER(Y_MOD_MODULE_HEADER);
631 break;
632 case YANG_YANG_VERSION:
633 CHECK_ORDER(Y_MOD_MODULE_HEADER);
634 break;
635 /* linkage */
636 case YANG_INCLUDE:
637 case YANG_IMPORT:
638 CHECK_ORDER(Y_MOD_LINKAGE);
639 break;
640 /* meta */
641 case YANG_ORGANIZATION:
642 case YANG_CONTACT:
643 case YANG_DESCRIPTION:
644 case YANG_REFERENCE:
645 CHECK_ORDER(Y_MOD_META);
646 break;
647
648 /* revision */
649 case YANG_REVISION:
650 CHECK_ORDER(Y_MOD_REVISION);
651 break;
652 /* body */
653 case YANG_ANYDATA:
654 case YANG_ANYXML:
655 case YANG_AUGMENT:
656 case YANG_CHOICE:
657 case YANG_CONTAINER:
658 case YANG_DEVIATION:
659 case YANG_EXTENSION:
660 case YANG_FEATURE:
661 case YANG_GROUPING:
662 case YANG_IDENTITY:
663 case YANG_LEAF:
664 case YANG_LEAF_LIST:
665 case YANG_LIST:
666 case YANG_NOTIFICATION:
667 case YANG_RPC:
668 case YANG_TYPEDEF:
669 case YANG_USES:
670 case YANG_CUSTOM:
671 mod_stmt = Y_MOD_BODY;
672 break;
673 default:
674 /* error will be handled in the next switch */
675 break;
676 }
677#undef CHECK_ORDER
678
David Sedlák68826732019-06-05 10:50:58 +0200679 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákc136b972019-03-08 13:39:06 +0100680 if (name) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200681 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args));
682 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100683 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100684
David Sedlákc136b972019-03-08 13:39:06 +0100685 /* module header */
686 case YANG_NAMESPACE:
David Sedlák00250342019-06-21 14:19:39 +0200687 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns);
688 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100689 break;
690 case YANG_PREFIX:
David Sedlák00250342019-06-21 14:19:39 +0200691 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix);
692 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100693 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100694
David Sedlákc136b972019-03-08 13:39:06 +0100695 /* linkage */
696 case YANG_IMPORT:
David Sedlák00250342019-06-21 14:19:39 +0200697 ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports);
698 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100699 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100700
David Sedlákc136b972019-03-08 13:39:06 +0100701 /* meta */
702 case YANG_ORGANIZATION:
David Sedlák00250342019-06-21 14:19:39 +0200703 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org);
704 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100705 break;
706 case YANG_CONTACT:
David Sedlák00250342019-06-21 14:19:39 +0200707 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact);
708 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100709 break;
710 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200711 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc);
712 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100713 break;
714 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200715 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref);
716 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100717 break;
David Sedlák11900c82019-06-18 16:29:12 +0200718 /* revision */
719
720 /*body */
721 case YANG_EXTENSION:
David Sedlák00250342019-06-21 14:19:39 +0200722 ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions);
723 LY_CHECK_GOTO(ret, error);
David Sedlák11900c82019-06-18 16:29:12 +0200724 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100725
David Sedlákc136b972019-03-08 13:39:06 +0100726 default:
David Sedlák00250342019-06-21 14:19:39 +0200727 ret = LY_EVALID;
728 goto error;
David Sedlákc136b972019-03-08 13:39:06 +0100729 }
David Sedlák00250342019-06-21 14:19:39 +0200730 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200731 substmt_args = NULL;
David Sedlák4a4c0722018-11-26 17:03:10 +0100732 }
733 }
734
David Sedlák68826732019-06-05 10:50:58 +0200735 return LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200736
737error:
738 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
739 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200740}
741
742LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100743yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200744{
David Sedláke4889912018-11-02 09:52:40 +0100745 LY_ERR ret = LY_SUCCESS;
746 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200747 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200748 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100749 struct lysp_module *mod_p = NULL;
750 const char *prefix, *name;
751 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200752 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200753
David Sedlák3017da42019-02-15 09:48:04 +0100754 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200755 memset(&parser_ctx, 0, sizeof parser_ctx);
756 xml_ctx->ctx = ctx;
757 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100758
David Sedlák3017da42019-02-15 09:48:04 +0100759 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200760 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200761 LY_CHECK_GOTO(ret, cleanup);
762 ret = yin_load_attributes(xml_ctx, &data, &args);
763 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200764 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100765 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100766 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
767 ret = LY_EINVAL;
768 goto cleanup;
769 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +0200770 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200771 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100772 ret = LY_EVALID;
773 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100774 }
775
David Sedlák3017da42019-02-15 09:48:04 +0100776 /* allocate module */
777 mod_p = calloc(1, sizeof *mod_p);
778 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
779 mod_p->mod = mod;
780 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100781
David Sedlák00250342019-06-21 14:19:39 +0200782 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +0200783 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100784 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100785
David Sedlák3017da42019-02-15 09:48:04 +0100786 mod_p->parsing = 0;
787 mod->parsed = mod_p;
788
789cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +0200790 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +0100791 lysp_module_free(mod_p);
792 }
David Sedlák00250342019-06-21 14:19:39 +0200793 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +0200794 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100795 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200796}