blob: 1520df8a6077550f6ac084f11d334d82c01fc5aa [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ák9494eb22019-06-21 16:06:53 +0200597yin_parse_argument_element(struct lyxml_context *xml_ctx, struct yin_arg_record **attrs, const char **data,
598 uint16_t *flags, const char **argument, struct lysp_ext **extensions)
599{
600 LY_ERR ret = LY_SUCCESS;
601 const char *name, *prefix;
602 size_t name_len, prefix_len;
603 struct yin_arg_record *subelem_args = NULL;
604 char *out;
605 int dynamic;
606 enum yang_keyword kw = YANG_NONE;
607
608 LY_CHECK_RET(yin_parse_attribute(xml_ctx, attrs, YIN_ARG_NAME, argument));
609
610 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
611 ret = lyxml_get_string(xml_ctx, data, &out, &name_len, &out, &name_len, &dynamic);
612 /* unknown element text content is ignored */
613 if (ret == LY_EINVAL) {
614 while (xml_ctx->status == LYXML_ELEMENT) {
615 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
616 LY_CHECK_GOTO(ret, cleanup);
617 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
618 LY_CHECK_GOTO(ret, cleanup);
619 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
620
621 if (!name) {
622 /* end of yin-element element reached */
623 break;
624 }
625
626 switch (kw) {
627 case YANG_YIN_ELEMENT:
628 yin_parse_yin_element_element(xml_ctx, &subelem_args, data, flags, extensions);
629 break;
630 case YANG_CUSTOM:
631 // TODO parse extension instance
632 break;
633
634 default:
635 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
636 return LY_EVALID;
637 }
638 }
639 } else {
640 /* load closing element */
641 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
642 LY_CHECK_RET(name, LY_EINVAL);
643 }
644 }
645
646cleanup:
647 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
648 return ret;
649}
650
651LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200652yin_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 +0200653{
654 LY_ERR ret = LY_SUCCESS;
655 struct lysp_ext *ex;
656 const char *prefix = NULL, *name = NULL;
657 char *out = NULL;
658 size_t out_len = 0, prefix_len = 0, name_len = 0;
659 int dynamic = 0;
660 enum yang_keyword kw = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200661 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200662
David Sedlák00250342019-06-21 14:19:39 +0200663 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *extensions, ex, ret, cleanup);
664 ret = yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name);
665 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200666 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlák00250342019-06-21 14:19:39 +0200667 LY_CHECK_ERR_GOTO(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element.");
668 ret = LY_EINVAL, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200669
670 while (xml_ctx->status == LYXML_ELEMENT) {
671 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200672 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200673 if (!name) {
David Sedlák00250342019-06-21 14:19:39 +0200674 /* end of extension element reached */
David Sedlák11900c82019-06-18 16:29:12 +0200675 break;
676 }
David Sedlák00250342019-06-21 14:19:39 +0200677 ret = yin_load_attributes(xml_ctx, data, &subelem_args);
678 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200679 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák11900c82019-06-18 16:29:12 +0200680 switch (kw) {
681 case YANG_ARGUMENT:
David Sedlák9494eb22019-06-21 16:06:53 +0200682 ret = yin_parse_argument_element(xml_ctx, &subelem_args, data, &ex->flags, &ex->argument, extensions);
David Sedlák11900c82019-06-18 16:29:12 +0200683 break;
684 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200685 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc);
686 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200687 break;
688 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200689 ret = yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref);
690 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200691 break;
692 case YANG_STATUS:
David Sedlák00250342019-06-21 14:19:39 +0200693 ret = yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts);
694 LY_CHECK_GOTO(ret, cleanup);
David Sedlák11900c82019-06-18 16:29:12 +0200695 break;
696 case YANG_CUSTOM:
697 /* TODO parse extension instance */
698 break;
699 default:
David Sedlákb3077192019-06-19 10:55:37 +0200700 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension");
David Sedlák00250342019-06-21 14:19:39 +0200701 ret = LY_EVALID;
702 goto cleanup;
David Sedlák11900c82019-06-18 16:29:12 +0200703 }
David Sedlák00250342019-06-21 14:19:39 +0200704 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák288c1472019-06-20 16:09:48 +0200705 subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200706 }
707
David Sedlák00250342019-06-21 14:19:39 +0200708cleanup:
709 FREE_ARRAY(xml_ctx, subelem_args, free_arg_rec);
David Sedlák11900c82019-06-18 16:29:12 +0200710 return ret;
711}
712
David Sedlák00250342019-06-21 14:19:39 +0200713/**
714 * @brief Parse module substatements.
715 *
716 * @param[in] xml_ctx Xml context.
717 * @param[in,out] data Data to read from.
718 * @param[out] mod Parsed module structure.
719 *
720 * @return LY_ERR values.
721 */
722LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200723yin_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 +0200724{
David Sedlákf3b24f62018-11-02 10:40:47 +0100725 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100726 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100727 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200728 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100729 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200730
David Sedlákf3b24f62018-11-02 10:40:47 +0100731 char *buf = NULL, *out = NULL;
732 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200733 int dynamic = 0;
David Sedlák8f7a1172019-06-20 14:42:18 +0200734 struct yin_arg_record *substmt_args = NULL;
David Sedlákf3b24f62018-11-02 10:40:47 +0100735
David Sedlák8f7a1172019-06-20 14:42:18 +0200736 yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlákb3077192019-06-19 10:55:37 +0200737 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 +0100738 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200739 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 +0100740
David Sedlák736fd0d2019-02-15 16:06:31 +0100741 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100742 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100743#define CHECK_ORDER(SECTION) \
744 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
745
746 switch (kw) {
747 /* module header */
748 case YANG_NAMESPACE:
749 case YANG_PREFIX:
750 CHECK_ORDER(Y_MOD_MODULE_HEADER);
751 break;
752 case YANG_YANG_VERSION:
753 CHECK_ORDER(Y_MOD_MODULE_HEADER);
754 break;
755 /* linkage */
756 case YANG_INCLUDE:
757 case YANG_IMPORT:
758 CHECK_ORDER(Y_MOD_LINKAGE);
759 break;
760 /* meta */
761 case YANG_ORGANIZATION:
762 case YANG_CONTACT:
763 case YANG_DESCRIPTION:
764 case YANG_REFERENCE:
765 CHECK_ORDER(Y_MOD_META);
766 break;
767
768 /* revision */
769 case YANG_REVISION:
770 CHECK_ORDER(Y_MOD_REVISION);
771 break;
772 /* body */
773 case YANG_ANYDATA:
774 case YANG_ANYXML:
775 case YANG_AUGMENT:
776 case YANG_CHOICE:
777 case YANG_CONTAINER:
778 case YANG_DEVIATION:
779 case YANG_EXTENSION:
780 case YANG_FEATURE:
781 case YANG_GROUPING:
782 case YANG_IDENTITY:
783 case YANG_LEAF:
784 case YANG_LEAF_LIST:
785 case YANG_LIST:
786 case YANG_NOTIFICATION:
787 case YANG_RPC:
788 case YANG_TYPEDEF:
789 case YANG_USES:
790 case YANG_CUSTOM:
791 mod_stmt = Y_MOD_BODY;
792 break;
793 default:
794 /* error will be handled in the next switch */
795 break;
796 }
797#undef CHECK_ORDER
798
David Sedlák68826732019-06-05 10:50:58 +0200799 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákc136b972019-03-08 13:39:06 +0100800 if (name) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200801 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args));
802 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100803 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100804
David Sedlákc136b972019-03-08 13:39:06 +0100805 /* module header */
806 case YANG_NAMESPACE:
David Sedlák00250342019-06-21 14:19:39 +0200807 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns);
808 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100809 break;
810 case YANG_PREFIX:
David Sedlák00250342019-06-21 14:19:39 +0200811 ret = yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix);
812 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100813 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100814
David Sedlákc136b972019-03-08 13:39:06 +0100815 /* linkage */
816 case YANG_IMPORT:
David Sedlák00250342019-06-21 14:19:39 +0200817 ret = yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports);
818 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100819 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100820
David Sedlákc136b972019-03-08 13:39:06 +0100821 /* meta */
822 case YANG_ORGANIZATION:
David Sedlák00250342019-06-21 14:19:39 +0200823 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org);
824 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100825 break;
826 case YANG_CONTACT:
David Sedlák00250342019-06-21 14:19:39 +0200827 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact);
828 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100829 break;
830 case YANG_DESCRIPTION:
David Sedlák00250342019-06-21 14:19:39 +0200831 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc);
832 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100833 break;
834 case YANG_REFERENCE:
David Sedlák00250342019-06-21 14:19:39 +0200835 ret = yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref);
836 LY_CHECK_GOTO(ret, error);
David Sedlákc136b972019-03-08 13:39:06 +0100837 break;
David Sedlák11900c82019-06-18 16:29:12 +0200838 /* revision */
839
840 /*body */
841 case YANG_EXTENSION:
David Sedlák00250342019-06-21 14:19:39 +0200842 ret = yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions);
843 LY_CHECK_GOTO(ret, error);
David Sedlák11900c82019-06-18 16:29:12 +0200844 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100845
David Sedlákc136b972019-03-08 13:39:06 +0100846 default:
David Sedlák00250342019-06-21 14:19:39 +0200847 ret = LY_EVALID;
848 goto error;
David Sedlákc136b972019-03-08 13:39:06 +0100849 }
David Sedlák00250342019-06-21 14:19:39 +0200850 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
David Sedlák8f7a1172019-06-20 14:42:18 +0200851 substmt_args = NULL;
David Sedlák4a4c0722018-11-26 17:03:10 +0100852 }
853 }
854
David Sedlák68826732019-06-05 10:50:58 +0200855 return LY_SUCCESS;
David Sedlák00250342019-06-21 14:19:39 +0200856
857error:
858 FREE_ARRAY(xml_ctx, substmt_args, free_arg_rec);
859 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200860}
861
862LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100863yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200864{
David Sedláke4889912018-11-02 09:52:40 +0100865 LY_ERR ret = LY_SUCCESS;
866 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200867 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200868 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100869 struct lysp_module *mod_p = NULL;
870 const char *prefix, *name;
871 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200872 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200873
David Sedlák3017da42019-02-15 09:48:04 +0100874 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200875 memset(&parser_ctx, 0, sizeof parser_ctx);
876 xml_ctx->ctx = ctx;
877 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100878
David Sedlák3017da42019-02-15 09:48:04 +0100879 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200880 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200881 LY_CHECK_GOTO(ret, cleanup);
882 ret = yin_load_attributes(xml_ctx, &data, &args);
883 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200884 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100885 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100886 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
887 ret = LY_EINVAL;
888 goto cleanup;
889 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +0200890 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200891 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100892 ret = LY_EVALID;
893 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100894 }
895
David Sedlák3017da42019-02-15 09:48:04 +0100896 /* allocate module */
897 mod_p = calloc(1, sizeof *mod_p);
898 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
899 mod_p->mod = mod;
900 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100901
David Sedlák00250342019-06-21 14:19:39 +0200902 /* parse module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +0200903 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100904 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100905
David Sedlák3017da42019-02-15 09:48:04 +0100906 mod_p->parsing = 0;
907 mod->parsed = mod_p;
908
909cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +0200910 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +0100911 lysp_module_free(mod_p);
912 }
David Sedlák00250342019-06-21 14:19:39 +0200913 FREE_ARRAY(xml_ctx, args, free_arg_rec);
David Sedlákaadab9c2019-04-05 15:01:27 +0200914 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100915 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200916}