blob: 060d523b1eb2e217e4a56990cd85e805e41aadd0 [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ák2b214ac2019-06-06 16:11:03 +020028/**
29 * @brief check if given string is URI of yin namespace.
30 * @param ns Namespace URI to check.
31 *
32 * @return true if ns equals YIN_NS_URI false otherwise.
33 */
34#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
35
David Sedlákf6251182019-06-06 10:22:13 +020036const char *const yin_attr_list[] = {
37 [YIN_ARG_NAME] = "name",
38 [YIN_ARG_TARGET_NODE] = "target-node",
39 [YIN_ARG_MODULE] = "module",
40 [YIN_ARG_VALUE] = "value",
41 [YIN_ARG_TEXT] = "text",
42 [YIN_ARG_CONDITION] = "condition",
43 [YIN_ARG_URI] = "uri",
44 [YIN_ARG_DATE] = "date",
45 [YIN_ARG_TAG] = "tag",
46 [YIN_ARG_XMLNS] = "xmlns",
47};
48
David Sedlák716753a2019-06-20 17:10:11 +020049static void
David Sedlákc8dd80e2019-06-19 16:08:48 +020050check_kw_ns(struct lyxml_context *xml_ctx, const char *prefix, size_t prefix_len, enum yang_keyword *kw)
51{
52 const struct lyxml_ns *ns = NULL;
53
54 ns = lyxml_ns_get(xml_ctx, prefix, prefix_len);
55
56 if (!ns) {
57 *kw = YANG_NONE;
58 } else {
59 if (!IS_YIN_NS(ns->uri)) {
60 *kw = YANG_CUSTOM;
61 }
62 }
63}
64
David Sedlák1bccdfa2019-06-17 15:55:27 +020065enum yang_keyword
David Sedlák8f7a1172019-06-20 14:42:18 +020066yin_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 +020067{
David Sedlák8f7a1172019-06-20 14:42:18 +020068 const char *start = NULL;
69 enum yang_keyword kw = YANG_NONE;
70 const struct lyxml_ns *ns = NULL;
71
72 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020073 return YANG_NONE;
74 }
75
David Sedlák8f7a1172019-06-20 14:42:18 +020076 ns = lyxml_ns_get(xml_ctx, prefix, prefix_len);
77 if (ns) {
78 if (!IS_YIN_NS(ns->uri)) {
79 return YANG_CUSTOM;
80 }
81 } else {
82 /* elements without namespace are automatically unknown */
83 return YANG_NONE;
84 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020085
David Sedlák8f7a1172019-06-20 14:42:18 +020086
87 start = name;
88 kw = lysp_match_kw(NULL, &name);
89
90 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020091 return kw;
92 } else {
93 return YANG_NONE;
94 }
95}
96
97
David Sedlák872c7b42018-10-26 13:15:20 +020098enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020099yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200100{
David Sedláka7406952019-04-05 10:33:07 +0200101 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200102 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200103 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200104
David Sedlák94de2aa2019-02-15 12:42:11 +0100105#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
106#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100107#define IF_ARG_PREFIX_END }
108
David Sedlák1c8b2702019-02-22 11:03:02 +0100109 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +0100110 case 'x':
111 already_read += 1;
112 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
113 break;
David Sedlák94de2aa2019-02-15 12:42:11 +0100114 case 'c':
115 already_read += 1;
116 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200117 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200118
David Sedlák94de2aa2019-02-15 12:42:11 +0100119 case 'd':
120 already_read += 1;
121 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200122 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200123
David Sedlák94de2aa2019-02-15 12:42:11 +0100124 case 'm':
125 already_read += 1;
126 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200127 break;
128
David Sedlák94de2aa2019-02-15 12:42:11 +0100129 case 'n':
130 already_read += 1;
131 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200132 break;
133
David Sedlák94de2aa2019-02-15 12:42:11 +0100134 case 't':
135 already_read += 1;
136 IF_ARG_PREFIX("a", 1)
137 IF_ARG("g", 1, YIN_ARG_TAG)
138 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
139 IF_ARG_PREFIX_END
140 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200141 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200142
David Sedlák94de2aa2019-02-15 12:42:11 +0100143 case 'u':
144 already_read += 1;
145 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200146 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200147
David Sedlák94de2aa2019-02-15 12:42:11 +0100148 case 'v':
149 already_read += 1;
150 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200151 break;
152 }
153
David Sedlákc10e7902018-12-17 02:17:59 +0100154 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200155 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200156 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200157 }
158
David Sedlák18730132019-03-15 15:51:34 +0100159#undef IF_ARG
160#undef IF_ARG_PREFIX
161#undef IF_ARG_PREFIX_END
162
David Sedlák872c7b42018-10-26 13:15:20 +0200163 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200164}
165
David Sedlák8f7a1172019-06-20 14:42:18 +0200166LY_ERR
167yin_load_attributes(struct lyxml_context *xml_ctx, const char **data, struct yin_arg_record **args)
David Sedláka7406952019-04-05 10:33:07 +0200168{
169 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200170 struct yin_arg_record *argument_record = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200171
David Sedlák7ff55a92019-06-17 11:11:41 +0200172 /* load all attributes first */
David Sedlák57715b12019-06-17 13:05:22 +0200173 while (xml_ctx->status == LYXML_ATTRIBUTE) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200174 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *args, argument_record, ret, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200175 ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len,
176 &argument_record->name, &argument_record->name_len);
177 LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200178
David Sedlák7ff55a92019-06-17 11:11:41 +0200179 if (xml_ctx->status == LYXML_ATTR_CONTENT) {
David Sedlák57715b12019-06-17 13:05:22 +0200180 argument_record->content = NULL;
181 argument_record->content_len = 0;
182 argument_record->dynamic_content = 0;
183 ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len,
184 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák7ff55a92019-06-17 11:11:41 +0200185 LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup);
186 }
187 }
188
David Sedlák8f7a1172019-06-20 14:42:18 +0200189cleanup:
190 if (ret != LY_SUCCESS) {
191 LY_ARRAY_FREE(*args);
192 }
193 return ret;
194}
195
196LY_ERR
197yin_parse_attribute(struct lyxml_context *xml_ctx, struct yin_arg_record **args, enum YIN_ARGUMENT arg_type, const char **arg_val)
198{
199 LY_ERR ret = LY_SUCCESS;
200 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
201 struct yin_arg_record *iter = NULL;
202 const struct lyxml_ns *ns = NULL;
203
David Sedlák1bccdfa2019-06-17 15:55:27 +0200204 /* validation of attributes */
David Sedlák8f7a1172019-06-20 14:42:18 +0200205 LY_ARRAY_FOR(*args, struct yin_arg_record, iter) {
David Sedlák7ff55a92019-06-17 11:11:41 +0200206 ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len);
David Sedlák2b214ac2019-06-06 16:11:03 +0200207 if (ns && IS_YIN_NS(ns->uri)) {
David Sedlák060b00e2019-06-19 11:12:06 +0200208 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200209 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200210 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200211 } else if (arg == arg_type) {
David Sedlák57715b12019-06-17 13:05:22 +0200212 if (iter->dynamic_content) {
213 *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content);
214 } else {
215 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
216 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
217 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200218 } else {
David Sedlák57715b12019-06-17 13:05:22 +0200219 if (iter->dynamic_content) {
220 free(iter->content);
221 }
David Sedlák7ff55a92019-06-17 11:11:41 +0200222 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name);
223 ret = LY_EVALID;
224 goto cleanup;
David Sedláka7406952019-04-05 10:33:07 +0200225 }
226 }
227 }
228
David Sedlák7ff55a92019-06-17 11:11:41 +0200229cleanup:
David Sedlák7ff55a92019-06-17 11:11:41 +0200230 return ret;
David Sedláka7406952019-04-05 10:33:07 +0200231}
232
David Sedlák0daba9a2019-03-22 14:07:49 +0100233LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200234yin_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 +0100235{
236 LY_ERR ret = LY_SUCCESS;
237 char *buf = NULL, *out = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200238 const char *prefix = NULL, *name = NULL;
239 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200240 int dynamic = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100241
David Sedlák8f7a1172019-06-20 14:42:18 +0200242 ret = yin_parse_attribute(xml_ctx, args, YIN_ARG_NONE, NULL);
David Sedlák68826732019-06-05 10:50:58 +0200243 LY_CHECK_RET(ret);
David Sedlák0daba9a2019-03-22 14:07:49 +0100244 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
245
246 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
247 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
248 LY_CHECK_RET(ret);
David Sedlák57715b12019-06-17 13:05:22 +0200249 if (dynamic) {
250 *value = lydict_insert_zc(xml_ctx->ctx, buf);
251 } else {
252 *value = lydict_insert(xml_ctx->ctx, out, out_len);
253 LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM);
254 }
David Sedlák0daba9a2019-03-22 14:07:49 +0100255 }
256
David Sedlák15a92662019-06-18 11:55:15 +0200257 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
258 LY_CHECK_RET(ret);
259
260 return LY_SUCCESS;
261}
262
David Sedlák15a92662019-06-18 11:55:15 +0200263LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200264yin_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 +0200265{
266 LY_ERR ret = LY_SUCCESS;
267 char *buf = NULL, *out = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200268 const char *prefix = NULL, *name = NULL, *content_start = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200269 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
270 int dynamic = 0;
David Sedlák15a92662019-06-18 11:55:15 +0200271 enum YIN_ARGUMENT arg = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200272 struct yin_arg_record *subelem_args = NULL;
273 enum yang_keyword kw = YANG_NONE;
David Sedlák15a92662019-06-18 11:55:15 +0200274
David Sedlák8f7a1172019-06-20 14:42:18 +0200275 ret = yin_parse_attribute(xml_ctx, args, YIN_ARG_NONE, NULL);
David Sedlák15a92662019-06-18 11:55:15 +0200276 LY_CHECK_RET(ret);
277 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
278
279 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200280 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as child of meta element."), LY_EINVAL);
David Sedlák15a92662019-06-18 11:55:15 +0200281
David Sedlák8f7a1172019-06-20 14:42:18 +0200282 /* first element should be argument element <text> */
283 content_start = *data;
284 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
285 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &subelem_args));
286 arg = yin_match_argument_name(name, name_len);
287 if (arg == YIN_ARG_TEXT) {
288 yin_parse_text_element(xml_ctx, &subelem_args, data, value);
289 } else {
290 /* first element is not argument element, go back to start of element content and try to parse normal subelements */
291 *data = content_start;
292 }
293
David Sedlák15a92662019-06-18 11:55:15 +0200294 /* loop over all child elements and parse them */
295 while (xml_ctx->status == LYXML_ELEMENT) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200296 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
297 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &subelem_args));
298 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák15a92662019-06-18 11:55:15 +0200299
David Sedlák8f7a1172019-06-20 14:42:18 +0200300 if (!name) {
David Sedlák15a92662019-06-18 11:55:15 +0200301 /* end of meta element reached */
302 break;
303 }
304
David Sedlák8f7a1172019-06-20 14:42:18 +0200305 switch (kw) {
306 case YANG_CUSTOM:
307 // TODO parse extension instance
308 break;
309
310 default:
311 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
312 return LY_EVALID;
313 }
David Sedlák15a92662019-06-18 11:55:15 +0200314 }
315
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100316 return LY_SUCCESS;
David Sedlák0daba9a2019-03-22 14:07:49 +0100317}
318
David Sedlák81e04022019-04-05 15:05:46 +0200319static LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200320yin_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 +0200321{
322 LY_ERR ret = LY_SUCCESS;
David Sedlákda63c082019-06-04 13:52:23 +0200323 const char *temp_rev;
David Sedlákcd0c9512019-03-29 13:23:06 +0100324
David Sedlák81e04022019-04-05 15:05:46 +0200325 if (rev[0]) {
David Sedlákb3077192019-06-19 10:55:37 +0200326 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
David Sedlák81e04022019-04-05 15:05:46 +0200327 return LY_EVALID;
328 }
329
David Sedlák8f7a1172019-06-20 14:42:18 +0200330 LY_CHECK_RET(yin_parse_attribute(xml_ctx, args, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200331 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200332 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
333
334 strcpy(rev, temp_rev);
335 lydict_remove(xml_ctx->ctx, temp_rev);
336 /* TODO extension */
David Sedlák81e04022019-04-05 15:05:46 +0200337
David Sedlák81e04022019-04-05 15:05:46 +0200338 return ret;
339}
David Sedlákcd0c9512019-03-29 13:23:06 +0100340
David Sedlákda63c082019-06-04 13:52:23 +0200341LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200342yin_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 +0100343{
344 LY_ERR ret = LY_SUCCESS;
345 enum yang_keyword kw;
346 struct lysp_import *imp;
347 const char *prefix, *name;
David Sedlák8f7a1172019-06-20 14:42:18 +0200348 struct yin_arg_record *subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100349 size_t prefix_len, name_len;
350
David Sedlák81e04022019-04-05 15:05:46 +0200351 /* allocate sized array for imports */
David Sedlák8f7a1172019-06-20 14:42:18 +0200352 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, *imports, imp, ret, validation_err);
David Sedlák736fd0d2019-02-15 16:06:31 +0100353
David Sedláka7406952019-04-05 10:33:07 +0200354 /* parse import attributes */
David Sedlák8f7a1172019-06-20 14:42:18 +0200355 LY_CHECK_GOTO((ret = yin_parse_attribute(xml_ctx, import_args, YIN_ARG_MODULE, &imp->name)) != LY_SUCCESS, validation_err);
David Sedlák736fd0d2019-02-15 16:06:31 +0100356 while ((ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS && name != NULL)) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200357 LY_CHECK_GOTO(yin_load_attributes(xml_ctx, data, &subelem_args) != LY_SUCCESS, validation_err);
358 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100359 switch (kw) {
360 case YANG_PREFIX:
David Sedlák8f7a1172019-06-20 14:42:18 +0200361 LY_CHECK_ERR_GOTO(imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), validation_err);
362 LY_CHECK_GOTO(yin_parse_attribute(xml_ctx, &subelem_args, YIN_ARG_VALUE, &imp->prefix) != LY_SUCCESS, validation_err);
363 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 +0100364 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100365 case YANG_DESCRIPTION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200366 LY_CHECK_ERR_GOTO(imp->dsc, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "description"), validation_err);
367 yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->dsc);
David Sedlákcd0c9512019-03-29 13:23:06 +0100368 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100369 case YANG_REFERENCE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200370 LY_CHECK_ERR_GOTO(imp->ref, LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPSTMT, "reference"), validation_err);
371 yin_parse_meta_element(xml_ctx, &subelem_args, data, &imp->ref);
David Sedlákcd0c9512019-03-29 13:23:06 +0100372 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100373 case YANG_REVISION_DATE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200374 yin_parse_revision_date(xml_ctx, &subelem_args, data, imp->rev, &imp->exts);
David Sedlák3a55bc12019-06-03 08:57:29 +0200375 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100376 case YANG_CUSTOM:
377 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200378 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100379 default:
David Sedlák916b1902019-06-06 11:35:05 +0200380 LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import");
David Sedlák8f7a1172019-06-20 14:42:18 +0200381 goto validation_err;
David Sedlák736fd0d2019-02-15 16:06:31 +0100382 }
David Sedlák8f7a1172019-06-20 14:42:18 +0200383 /* TODO add free_argument_instance function and use FREE_ARRAY */
384 LY_ARRAY_FREE(subelem_args);
385 subelem_args = NULL;
David Sedlák736fd0d2019-02-15 16:06:31 +0100386 }
387
David Sedlák8f7a1172019-06-20 14:42:18 +0200388 LY_CHECK_ERR_GOTO(!imp->prefix, LOGVAL_PARSER(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), validation_err);
389 LY_ARRAY_FREE(subelem_args);
David Sedlák736fd0d2019-02-15 16:06:31 +0100390 return ret;
David Sedlák8f7a1172019-06-20 14:42:18 +0200391
392validation_err:
393 LY_ARRAY_FREE(subelem_args);
394 return LY_EVALID;
David Sedlák736fd0d2019-02-15 16:06:31 +0100395}
396
David Sedlák11900c82019-06-18 16:29:12 +0200397LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200398yin_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 +0200399{
400 LY_ERR ret = LY_SUCCESS;
401 enum yang_keyword kw = YANG_NONE;
402 const char *value = NULL, *prefix = NULL, *name = NULL;
403 char *out;
404 size_t prefix_len = 0, name_len = 0, out_len = 0;
405 int dynamic = 0;
406
407 if (*flags & LYS_STATUS_MASK) {
David Sedlákb3077192019-06-19 10:55:37 +0200408 LOGVAL_PARSER(xml_ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200409 return LY_EVALID;
410 }
411
David Sedlák8f7a1172019-06-20 14:42:18 +0200412 LY_CHECK_RET(yin_parse_attribute(xml_ctx, status_args, YIN_ARG_VALUE, &value));
David Sedlák11900c82019-06-18 16:29:12 +0200413 if (strcmp(value, "current") == 0) {
414 *flags |= LYS_STATUS_CURR;
415 } else if (strcmp(value, "deprecated") == 0) {
416 *flags |= LYS_STATUS_DEPRC;
417 } else if (strcmp(value, "obsolete") == 0) {
418 *flags |= LYS_STATUS_OBSLT;
419 } else {
David Sedlákb3077192019-06-19 10:55:37 +0200420 LOGVAL_PARSER(xml_ctx, LY_VCODE_INVAL_YIN, value, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200421 lydict_remove(xml_ctx->ctx, value);
422 return LY_EVALID;
423 }
424 lydict_remove(xml_ctx->ctx, value);
425
David Sedlákb6e65972019-06-19 10:44:13 +0200426 /* TODO check if dynamic was set to 1 in case of error */
427 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
428 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
429 /* if there are any xml subelements parse them, unknown text content is silently ignored */
430 if (ret == LY_EINVAL) {
431 /* load subelements */
432 while (xml_ctx->status == LYXML_ELEMENT) {
433 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
434 if (!name) {
435 break;
436 }
David Sedlák11900c82019-06-18 16:29:12 +0200437
David Sedlák8f7a1172019-06-20 14:42:18 +0200438 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákb6e65972019-06-19 10:44:13 +0200439 switch (kw) {
440 case YANG_CUSTOM:
441 /* TODO parse extension instance */
442 break;
443 default:
David Sedlákb3077192019-06-19 10:55:37 +0200444 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 6, "status");
David Sedlákb6e65972019-06-19 10:44:13 +0200445 }
446 }
David Sedlák11900c82019-06-18 16:29:12 +0200447 }
448 }
449
450 return ret;
451}
452
David Sedlák11900c82019-06-18 16:29:12 +0200453LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200454yin_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 +0200455{
456 LY_ERR ret = LY_SUCCESS;
457 struct lysp_ext *ex;
458 const char *prefix = NULL, *name = NULL;
459 char *out = NULL;
460 size_t out_len = 0, prefix_len = 0, name_len = 0;
461 int dynamic = 0;
462 enum yang_keyword kw = YANG_NONE;
David Sedlák8f7a1172019-06-20 14:42:18 +0200463 struct yin_arg_record *subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200464
465 LY_ARRAY_NEW_RET(xml_ctx->ctx, *extensions, ex, LY_EMEM);
David Sedlák8f7a1172019-06-20 14:42:18 +0200466 yin_parse_attribute(xml_ctx, extension_args, YIN_ARG_NAME, &ex->name);
David Sedlák11900c82019-06-18 16:29:12 +0200467 ret = lyxml_get_string(xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200468 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX_YIN, "Expected new element after extension element."), LY_EINVAL);
David Sedlák11900c82019-06-18 16:29:12 +0200469
470 while (xml_ctx->status == LYXML_ELEMENT) {
471 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
472 LY_CHECK_RET(ret);
473 if (!name) {
474 break;
475 }
David Sedlák8f7a1172019-06-20 14:42:18 +0200476 yin_load_attributes(xml_ctx, data, &subelem_args);
477 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlák11900c82019-06-18 16:29:12 +0200478 switch (kw) {
479 case YANG_ARGUMENT:
David Sedlák554e36d2019-06-20 16:00:04 +0200480 /* TODO */
David Sedlák11900c82019-06-18 16:29:12 +0200481 break;
482 case YANG_DESCRIPTION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200483 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->dsc));
David Sedlák11900c82019-06-18 16:29:12 +0200484 break;
485 case YANG_REFERENCE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200486 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, &subelem_args, data, &ex->ref));
David Sedlák11900c82019-06-18 16:29:12 +0200487 break;
488 case YANG_STATUS:
David Sedlák8f7a1172019-06-20 14:42:18 +0200489 LY_CHECK_RET(yin_parse_status(xml_ctx, &subelem_args, data, &ex->flags, &ex->exts));
David Sedlák11900c82019-06-18 16:29:12 +0200490 break;
491 case YANG_CUSTOM:
492 /* TODO parse extension instance */
493 break;
494 default:
David Sedlákb3077192019-06-19 10:55:37 +0200495 LOGVAL_PARSER(xml_ctx, LY_VCODE_INCHILDSTMT_YIN, name_len, name, 9, "extension");
David Sedlák11900c82019-06-18 16:29:12 +0200496 return LY_EVALID;
497 }
David Sedlák288c1472019-06-20 16:09:48 +0200498 LY_ARRAY_FREE(subelem_args);
499 subelem_args = NULL;
David Sedlák11900c82019-06-18 16:29:12 +0200500 }
501
502 return ret;
503}
504
David Sedlák716753a2019-06-20 17:10:11 +0200505static LY_ERR
David Sedlák8f7a1172019-06-20 14:42:18 +0200506yin_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 +0200507{
David Sedlákf3b24f62018-11-02 10:40:47 +0100508 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100509 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100510 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200511 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100512 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200513
David Sedlákf3b24f62018-11-02 10:40:47 +0100514 char *buf = NULL, *out = NULL;
515 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200516 int dynamic = 0;
David Sedlák8f7a1172019-06-20 14:42:18 +0200517 struct yin_arg_record *substmt_args = NULL;
David Sedlákf3b24f62018-11-02 10:40:47 +0100518
David Sedlák8f7a1172019-06-20 14:42:18 +0200519 yin_parse_attribute(xml_ctx, mod_args, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlákb3077192019-06-19 10:55:37 +0200520 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 +0100521 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlákb3077192019-06-19 10:55:37 +0200522 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 +0100523
David Sedlák736fd0d2019-02-15 16:06:31 +0100524 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100525 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100526#define CHECK_ORDER(SECTION) \
527 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
528
529 switch (kw) {
530 /* module header */
531 case YANG_NAMESPACE:
532 case YANG_PREFIX:
533 CHECK_ORDER(Y_MOD_MODULE_HEADER);
534 break;
535 case YANG_YANG_VERSION:
536 CHECK_ORDER(Y_MOD_MODULE_HEADER);
537 break;
538 /* linkage */
539 case YANG_INCLUDE:
540 case YANG_IMPORT:
541 CHECK_ORDER(Y_MOD_LINKAGE);
542 break;
543 /* meta */
544 case YANG_ORGANIZATION:
545 case YANG_CONTACT:
546 case YANG_DESCRIPTION:
547 case YANG_REFERENCE:
548 CHECK_ORDER(Y_MOD_META);
549 break;
550
551 /* revision */
552 case YANG_REVISION:
553 CHECK_ORDER(Y_MOD_REVISION);
554 break;
555 /* body */
556 case YANG_ANYDATA:
557 case YANG_ANYXML:
558 case YANG_AUGMENT:
559 case YANG_CHOICE:
560 case YANG_CONTAINER:
561 case YANG_DEVIATION:
562 case YANG_EXTENSION:
563 case YANG_FEATURE:
564 case YANG_GROUPING:
565 case YANG_IDENTITY:
566 case YANG_LEAF:
567 case YANG_LEAF_LIST:
568 case YANG_LIST:
569 case YANG_NOTIFICATION:
570 case YANG_RPC:
571 case YANG_TYPEDEF:
572 case YANG_USES:
573 case YANG_CUSTOM:
574 mod_stmt = Y_MOD_BODY;
575 break;
576 default:
577 /* error will be handled in the next switch */
578 break;
579 }
580#undef CHECK_ORDER
581
David Sedlák68826732019-06-05 10:50:58 +0200582 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákc136b972019-03-08 13:39:06 +0100583 if (name) {
David Sedlák8f7a1172019-06-20 14:42:18 +0200584 LY_CHECK_RET(yin_load_attributes(xml_ctx, data, &substmt_args));
585 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100586 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100587
David Sedlákc136b972019-03-08 13:39:06 +0100588 /* module header */
589 case YANG_NAMESPACE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200590 LY_CHECK_RET(yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_URI, &(*mod)->mod->ns));
David Sedlákc136b972019-03-08 13:39:06 +0100591 break;
592 case YANG_PREFIX:
David Sedlák8f7a1172019-06-20 14:42:18 +0200593 LY_CHECK_RET(yin_parse_attribute(xml_ctx, &substmt_args, YIN_ARG_VALUE, &(*mod)->mod->prefix));
David Sedlákc136b972019-03-08 13:39:06 +0100594 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100595
David Sedlákc136b972019-03-08 13:39:06 +0100596 /* linkage */
597 case YANG_IMPORT:
David Sedlák8f7a1172019-06-20 14:42:18 +0200598 yin_parse_import(xml_ctx, &substmt_args, (*mod)->mod->prefix, data, &(*mod)->imports);
David Sedlákc136b972019-03-08 13:39:06 +0100599 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100600
David Sedlákc136b972019-03-08 13:39:06 +0100601 /* meta */
602 case YANG_ORGANIZATION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200603 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100604 break;
605 case YANG_CONTACT:
David Sedlák8f7a1172019-06-20 14:42:18 +0200606 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100607 break;
608 case YANG_DESCRIPTION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200609 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100610 break;
611 case YANG_REFERENCE:
David Sedlák8f7a1172019-06-20 14:42:18 +0200612 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, &substmt_args, data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100613 break;
David Sedlák11900c82019-06-18 16:29:12 +0200614 /* revision */
615
616 /*body */
617 case YANG_EXTENSION:
David Sedlák8f7a1172019-06-20 14:42:18 +0200618 LY_CHECK_RET(yin_parse_extension(xml_ctx, &substmt_args, data, &(*mod)->extensions));
David Sedlák11900c82019-06-18 16:29:12 +0200619 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100620
David Sedlákc136b972019-03-08 13:39:06 +0100621 default:
David Sedlák18730132019-03-15 15:51:34 +0100622 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100623 break;
624 }
David Sedlák8f7a1172019-06-20 14:42:18 +0200625 LY_ARRAY_FREE(substmt_args);
626 substmt_args = NULL;
David Sedlák4a4c0722018-11-26 17:03:10 +0100627 }
628 }
629
David Sedlák68826732019-06-05 10:50:58 +0200630 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200631}
632
633LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100634yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200635{
David Sedláke4889912018-11-02 09:52:40 +0100636 LY_ERR ret = LY_SUCCESS;
637 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200638 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200639 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100640 struct lysp_module *mod_p = NULL;
641 const char *prefix, *name;
642 size_t prefix_len, name_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200643 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200644
David Sedlák3017da42019-02-15 09:48:04 +0100645 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200646 memset(&parser_ctx, 0, sizeof parser_ctx);
647 xml_ctx->ctx = ctx;
648 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100649
David Sedlák3017da42019-02-15 09:48:04 +0100650 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200651 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200652 yin_load_attributes(xml_ctx, &data, &args);
David Sedlák3017da42019-02-15 09:48:04 +0100653 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák8f7a1172019-06-20 14:42:18 +0200654 kw = yin_match_keyword(xml_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100655 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100656 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
657 ret = LY_EINVAL;
658 goto cleanup;
659 } else if (kw != YANG_MODULE) {
David Sedlákb3077192019-06-19 10:55:37 +0200660 LOGVAL_PARSER(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200661 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100662 ret = LY_EVALID;
663 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100664 }
665
David Sedlák3017da42019-02-15 09:48:04 +0100666 /* allocate module */
667 mod_p = calloc(1, sizeof *mod_p);
668 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
669 mod_p->mod = mod;
670 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100671
David Sedlák3017da42019-02-15 09:48:04 +0100672 /* parser module substatements */
David Sedlák8f7a1172019-06-20 14:42:18 +0200673 ret = yin_parse_mod(xml_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100674 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100675
David Sedlák3017da42019-02-15 09:48:04 +0100676 mod_p->parsing = 0;
677 mod->parsed = mod_p;
678
679cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +0200680 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +0100681 lysp_module_free(mod_p);
682 }
David Sedlák8f7a1172019-06-20 14:42:18 +0200683 LY_ARRAY_FREE(args);
David Sedlákaadab9c2019-04-05 15:01:27 +0200684 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100685 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200686}