blob: 2e03c39b4badb5c05653b66e01cb84a0f3fc5f6a [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ákf824ad52018-10-14 23:58:15 +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
51yin_match_keyword(const char *data, size_t len, size_t prefix_len)
52{
53 if (!data || len == 0) {
54 return YANG_NONE;
55 }
56
57 const char *start = data;
58 enum yang_keyword kw = match_kw(NULL, &data);
59
60
61 if (prefix_len != 0) {
62 return YANG_CUSTOM;
63 }
64
65 if (data - start == (long int)len) {
66 return kw;
67 } else {
68 return YANG_NONE;
69 }
70}
71
72
David Sedlák872c7b42018-10-26 13:15:20 +020073enum YIN_ARGUMENT
David Sedláke4889912018-11-02 09:52:40 +010074match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020075{
David Sedláka7406952019-04-05 10:33:07 +020076 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020077 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020078 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020079
David Sedlák94de2aa2019-02-15 12:42:11 +010080#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
81#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010082#define IF_ARG_PREFIX_END }
83
David Sedlák1c8b2702019-02-22 11:03:02 +010084 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +010085 case 'x':
86 already_read += 1;
87 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
88 break;
David Sedlák94de2aa2019-02-15 12:42:11 +010089 case 'c':
90 already_read += 1;
91 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +020092 break;
David Sedlák872c7b42018-10-26 13:15:20 +020093
David Sedlák94de2aa2019-02-15 12:42:11 +010094 case 'd':
95 already_read += 1;
96 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +020097 break;
David Sedlák872c7b42018-10-26 13:15:20 +020098
David Sedlák94de2aa2019-02-15 12:42:11 +010099 case 'm':
100 already_read += 1;
101 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200102 break;
103
David Sedlák94de2aa2019-02-15 12:42:11 +0100104 case 'n':
105 already_read += 1;
106 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200107 break;
108
David Sedlák94de2aa2019-02-15 12:42:11 +0100109 case 't':
110 already_read += 1;
111 IF_ARG_PREFIX("a", 1)
112 IF_ARG("g", 1, YIN_ARG_TAG)
113 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
114 IF_ARG_PREFIX_END
115 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200116 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200117
David Sedlák94de2aa2019-02-15 12:42:11 +0100118 case 'u':
119 already_read += 1;
120 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200121 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200122
David Sedlák94de2aa2019-02-15 12:42:11 +0100123 case 'v':
124 already_read += 1;
125 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200126 break;
127 }
128
David Sedlákc10e7902018-12-17 02:17:59 +0100129 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200130 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200131 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200132 }
133
David Sedlák18730132019-03-15 15:51:34 +0100134#undef IF_ARG
135#undef IF_ARG_PREFIX
136#undef IF_ARG_PREFIX_END
137
David Sedlák872c7b42018-10-26 13:15:20 +0200138 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200139}
140
David Sedlák18730132019-03-15 15:51:34 +0100141/**
David Sedlák2b214ac2019-06-06 16:11:03 +0200142 * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found.
David Sedláka7406952019-04-05 10:33:07 +0200143 *
144 * @param[in] xml_ctx XML parser context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200145 * @param[in,out] data Data to read from.
David Sedláka7406952019-04-05 10:33:07 +0200146 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments).
David Sedlák68826732019-06-05 10:50:58 +0200147 * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is specified as YIN_ARG_NONE.
David Sedlákb666bcc2019-06-05 15:00:05 +0200148 *
149 * @return LY_ERR values.
David Sedláka7406952019-04-05 10:33:07 +0200150 */
151static LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200152yin_parse_attribute(struct lyxml_context *xml_ctx, const char **data, enum YIN_ARGUMENT arg_type, const char **arg_val)
David Sedláka7406952019-04-05 10:33:07 +0200153{
154 LY_ERR ret = LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200155 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák7ff55a92019-06-17 11:11:41 +0200156 struct yin_arg_record *argument_array = NULL, *argument_record = NULL, *iter = NULL;
157 const struct lyxml_ns *ns = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200158
David Sedlák7ff55a92019-06-17 11:11:41 +0200159 /* load all attributes first */
David Sedlák57715b12019-06-17 13:05:22 +0200160 while (xml_ctx->status == LYXML_ATTRIBUTE) {
David Sedlák7ff55a92019-06-17 11:11:41 +0200161 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, argument_array, argument_record, ret, cleanup);
162 ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len,
163 &argument_record->name, &argument_record->name_len);
164 LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200165
David Sedlák7ff55a92019-06-17 11:11:41 +0200166 if (xml_ctx->status == LYXML_ATTR_CONTENT) {
David Sedlák57715b12019-06-17 13:05:22 +0200167 argument_record->content = NULL;
168 argument_record->content_len = 0;
169 argument_record->dynamic_content = 0;
170 ret = lyxml_get_string(xml_ctx, data, &argument_record->content, &argument_record->content_len,
171 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák7ff55a92019-06-17 11:11:41 +0200172 LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup);
173 }
174 }
175
David Sedlák1bccdfa2019-06-17 15:55:27 +0200176 /* validation of attributes */
David Sedlák7ff55a92019-06-17 11:11:41 +0200177 LY_ARRAY_FOR(argument_array, struct yin_arg_record, iter) {
178 ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len);
David Sedlák2b214ac2019-06-06 16:11:03 +0200179 if (ns && IS_YIN_NS(ns->uri)) {
David Sedlák7ff55a92019-06-17 11:11:41 +0200180 arg = match_argument_name(iter->name, iter->name_len);
181 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200182 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200183 } else if (arg == arg_type) {
David Sedlák57715b12019-06-17 13:05:22 +0200184 if (iter->dynamic_content) {
185 *arg_val = lydict_insert_zc(xml_ctx->ctx, iter->content);
186 } else {
187 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
188 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
189 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200190 } else {
David Sedlák57715b12019-06-17 13:05:22 +0200191 if (iter->dynamic_content) {
192 free(iter->content);
193 }
David Sedlák7ff55a92019-06-17 11:11:41 +0200194 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name);
195 ret = LY_EVALID;
196 goto cleanup;
David Sedláka7406952019-04-05 10:33:07 +0200197 }
198 }
199 }
200
David Sedlák7ff55a92019-06-17 11:11:41 +0200201cleanup:
202 LY_ARRAY_FREE(argument_array);
203 return ret;
David Sedláka7406952019-04-05 10:33:07 +0200204}
205
206/**
David Sedlák0daba9a2019-03-22 14:07:49 +0100207 * @brief Parse content of whole element as text.
208 *
209 * @param[in] xml_ctx Xml context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200210 * @param[in,out] data Data to read from.
David Sedlák0daba9a2019-03-22 14:07:49 +0100211 * @param[out] value Where content of element should be stored.
David Sedlákb666bcc2019-06-05 15:00:05 +0200212 *
213 * @return LY_ERR values.
David Sedlák0daba9a2019-03-22 14:07:49 +0100214 */
215LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200216parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100217{
218 LY_ERR ret = LY_SUCCESS;
219 char *buf = NULL, *out = NULL;
David Sedlák15a92662019-06-18 11:55:15 +0200220 const char *prefix = NULL, *name = NULL;
221 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200222 int dynamic = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100223
David Sedlákecf5eb82019-06-03 14:12:44 +0200224 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL);
David Sedlák68826732019-06-05 10:50:58 +0200225 LY_CHECK_RET(ret);
David Sedlák0daba9a2019-03-22 14:07:49 +0100226 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
227
228 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
229 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
230 LY_CHECK_RET(ret);
David Sedlák57715b12019-06-17 13:05:22 +0200231 if (dynamic) {
232 *value = lydict_insert_zc(xml_ctx->ctx, buf);
233 } else {
234 *value = lydict_insert(xml_ctx->ctx, out, out_len);
235 LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM);
236 }
David Sedlák0daba9a2019-03-22 14:07:49 +0100237 }
238
David Sedlák15a92662019-06-18 11:55:15 +0200239 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
240 LY_CHECK_RET(ret);
241
242 return LY_SUCCESS;
243}
244
245/**
246 * @brief function to parse meta tags eg. elements with text element as child
247 *
248 * @param[in] xml_ctx Xml context.
249 * @param[in,out] data Data to read from.
250 * @param[out] value Where the content of meta tag should be stored.
251 *
252 * @return LY_ERR values.
253 */
254LY_ERR
255yin_parse_meta_element(struct lyxml_context *xml_ctx, const char **data, const char **value)
256{
257 LY_ERR ret = LY_SUCCESS;
258 char *buf = NULL, *out = NULL;
259 const char *prefix = NULL, *name = NULL;
260 size_t buf_len = 0, out_len = 0, prefix_len = 0, name_len = 0;
261 int dynamic = 0;
262 const struct lyxml_ns *ns = NULL;
263 enum YIN_ARGUMENT arg = YANG_NONE;
264
265 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL);
266 LY_CHECK_RET(ret);
267 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
268
269 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
270 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Expected \"text\" element as child of meta element."), LY_EINVAL);
271
272 /* loop over all child elements and parse them */
273 while (xml_ctx->status == LYXML_ELEMENT) {
274 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
275 LY_CHECK_RET(ret);
276 if (name) {
277 ns = lyxml_ns_get(xml_ctx, prefix, prefix_len);
278 /* check if child element is from yin namespace, elements from other namespaces are silently ignored */
279 if (IS_YIN_NS(ns->uri)) {
280 arg = match_argument_name(name, name_len);
281 if (arg == YIN_ARG_TEXT) {
282 parse_text_element(xml_ctx, data, value);
283 } else {
284 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected child element \"%.*s\".", name_len, name);
285 return LY_EVALID;
286 }
287 }
288
289 } else {
290 /* end of meta element reached */
291 break;
292 }
293
294 }
295
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100296 return LY_SUCCESS;
David Sedlák0daba9a2019-03-22 14:07:49 +0100297}
298
David Sedlákb666bcc2019-06-05 15:00:05 +0200299/**
David Sedlák2b214ac2019-06-06 16:11:03 +0200300 * @brief Parse revision date.
David Sedlákb666bcc2019-06-05 15:00:05 +0200301 *
302 * @param[in] xml_ctx Xml context.
303 * @param[in,out] data Data to read from.
304 * @param[in,out] rev Array to store the parsed value in.
305 * @param[in,out] exts Extension instances to add to.
306 *
David Sedlák2b214ac2019-06-06 16:11:03 +0200307 * @return LY_ERR values.
David Sedlákb666bcc2019-06-05 15:00:05 +0200308 */
David Sedlák81e04022019-04-05 15:05:46 +0200309static LY_ERR
310yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
311{
312 LY_ERR ret = LY_SUCCESS;
David Sedlákda63c082019-06-04 13:52:23 +0200313 const char *temp_rev;
David Sedlákcd0c9512019-03-29 13:23:06 +0100314
David Sedlák81e04022019-04-05 15:05:46 +0200315 if (rev[0]) {
316 LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
317 return LY_EVALID;
318 }
319
David Sedlák68826732019-06-05 10:50:58 +0200320 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200321 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200322 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
323
324 strcpy(rev, temp_rev);
325 lydict_remove(xml_ctx->ctx, temp_rev);
326 /* TODO extension */
David Sedlák81e04022019-04-05 15:05:46 +0200327
David Sedlák81e04022019-04-05 15:05:46 +0200328 return ret;
329}
David Sedlákcd0c9512019-03-29 13:23:06 +0100330
David Sedlákda63c082019-06-04 13:52:23 +0200331LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200332yin_parse_import(struct lyxml_context *xml_ctx, const char *module_prefix, const char **data, struct lysp_import **imports)
David Sedlák736fd0d2019-02-15 16:06:31 +0100333{
334 LY_ERR ret = LY_SUCCESS;
335 enum yang_keyword kw;
336 struct lysp_import *imp;
337 const char *prefix, *name;
338 size_t prefix_len, name_len;
339
David Sedlák81e04022019-04-05 15:05:46 +0200340 /* allocate sized array for imports */
341 LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100342
David Sedláka7406952019-04-05 10:33:07 +0200343 /* parse import attributes */
David Sedlák68826732019-06-05 10:50:58 +0200344 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name));
345
David Sedlák736fd0d2019-02-15 16:06:31 +0100346 while ((ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS && name != NULL)) {
David Sedlák1bccdfa2019-06-17 15:55:27 +0200347 kw = yin_match_keyword(name, name_len, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100348 switch (kw) {
349 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200350 LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID);
David Sedlák68826732019-06-05 10:50:58 +0200351 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &imp->prefix));
David Sedlákda63c082019-06-04 13:52:23 +0200352 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)xml_ctx, *imports, module_prefix, &imp->prefix), LY_EVALID);
David Sedlákcd0c9512019-03-29 13:23:06 +0100353 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100354 case YANG_DESCRIPTION:
David Sedlák81e04022019-04-05 15:05:46 +0200355 LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID);
David Sedlák15a92662019-06-18 11:55:15 +0200356 yin_parse_meta_element(xml_ctx, data, &imp->dsc);
David Sedlákcd0c9512019-03-29 13:23:06 +0100357 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100358 case YANG_REFERENCE:
David Sedlák81e04022019-04-05 15:05:46 +0200359 LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID);
David Sedlák15a92662019-06-18 11:55:15 +0200360 yin_parse_meta_element(xml_ctx, data, &imp->ref);
David Sedlákcd0c9512019-03-29 13:23:06 +0100361 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100362 case YANG_REVISION_DATE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200363 yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts);
David Sedlák3a55bc12019-06-03 08:57:29 +0200364 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100365 case YANG_CUSTOM:
366 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200367 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100368 default:
David Sedlák916b1902019-06-06 11:35:05 +0200369 LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import");
David Sedlák736fd0d2019-02-15 16:06:31 +0100370 return LY_EVALID;
371 }
372 }
373
David Sedlák916b1902019-06-06 11:35:05 +0200374 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100375 return ret;
376}
377
David Sedlákd5318ee2019-02-15 10:14:50 +0100378/**
379 * @brief Parse module substatements.
380 *
381 * @param[in] xml_ctx xml context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200382 * @param[in,out] data Data to read from.
David Sedlák2b214ac2019-06-06 16:11:03 +0200383 * @param[out] mod Parsed module structure.
David Sedlákd5318ee2019-02-15 10:14:50 +0100384 *
385 * @return LY_ERR values.
386 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100387LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100388parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200389{
David Sedlákf3b24f62018-11-02 10:40:47 +0100390 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100391 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100392 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200393 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100394 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200395
David Sedlákf3b24f62018-11-02 10:40:47 +0100396 char *buf = NULL, *out = NULL;
397 size_t buf_len = 0, out_len = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200398 int dynamic = 0;
David Sedlákf3b24f62018-11-02 10:40:47 +0100399
David Sedlák2b214ac2019-06-06 16:11:03 +0200400 yin_parse_attribute(xml_ctx, data, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlák18730132019-03-15 15:51:34 +0100401
David Sedlák2b214ac2019-06-06 16:11:03 +0200402 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module"), LY_EVALID);
David Sedlákc136b972019-03-08 13:39:06 +0100403 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák18730132019-03-15 15:51:34 +0100404 LY_CHECK_ERR_RET(ret != LY_EINVAL, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Expected new xml element after module element."), LY_EINVAL);
David Sedlák4a4c0722018-11-26 17:03:10 +0100405
David Sedlák736fd0d2019-02-15 16:06:31 +0100406 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100407 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100408#define CHECK_ORDER(SECTION) \
409 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
410
411 switch (kw) {
412 /* module header */
413 case YANG_NAMESPACE:
414 case YANG_PREFIX:
415 CHECK_ORDER(Y_MOD_MODULE_HEADER);
416 break;
417 case YANG_YANG_VERSION:
418 CHECK_ORDER(Y_MOD_MODULE_HEADER);
419 break;
420 /* linkage */
421 case YANG_INCLUDE:
422 case YANG_IMPORT:
423 CHECK_ORDER(Y_MOD_LINKAGE);
424 break;
425 /* meta */
426 case YANG_ORGANIZATION:
427 case YANG_CONTACT:
428 case YANG_DESCRIPTION:
429 case YANG_REFERENCE:
430 CHECK_ORDER(Y_MOD_META);
431 break;
432
433 /* revision */
434 case YANG_REVISION:
435 CHECK_ORDER(Y_MOD_REVISION);
436 break;
437 /* body */
438 case YANG_ANYDATA:
439 case YANG_ANYXML:
440 case YANG_AUGMENT:
441 case YANG_CHOICE:
442 case YANG_CONTAINER:
443 case YANG_DEVIATION:
444 case YANG_EXTENSION:
445 case YANG_FEATURE:
446 case YANG_GROUPING:
447 case YANG_IDENTITY:
448 case YANG_LEAF:
449 case YANG_LEAF_LIST:
450 case YANG_LIST:
451 case YANG_NOTIFICATION:
452 case YANG_RPC:
453 case YANG_TYPEDEF:
454 case YANG_USES:
455 case YANG_CUSTOM:
456 mod_stmt = Y_MOD_BODY;
457 break;
458 default:
459 /* error will be handled in the next switch */
460 break;
461 }
462#undef CHECK_ORDER
463
David Sedlák68826732019-06-05 10:50:58 +0200464 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák4a4c0722018-11-26 17:03:10 +0100465
David Sedlákc136b972019-03-08 13:39:06 +0100466 if (name) {
David Sedlák1bccdfa2019-06-17 15:55:27 +0200467 kw = yin_match_keyword(name, name_len, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100468 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100469
David Sedlákc136b972019-03-08 13:39:06 +0100470 /* module header */
471 case YANG_NAMESPACE:
David Sedlák68826732019-06-05 10:50:58 +0200472 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, &(*mod)->mod->ns));
David Sedlákc136b972019-03-08 13:39:06 +0100473 break;
474 case YANG_PREFIX:
David Sedlák68826732019-06-05 10:50:58 +0200475 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &(*mod)->mod->prefix));
David Sedlákc136b972019-03-08 13:39:06 +0100476 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100477
David Sedlákc136b972019-03-08 13:39:06 +0100478 /* linkage */
479 case YANG_IMPORT:
David Sedlák81e04022019-04-05 15:05:46 +0200480 yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports);
David Sedlákc136b972019-03-08 13:39:06 +0100481 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100482
David Sedlákc136b972019-03-08 13:39:06 +0100483 /* meta */
484 case YANG_ORGANIZATION:
David Sedlák15a92662019-06-18 11:55:15 +0200485 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100486 break;
487 case YANG_CONTACT:
David Sedlák15a92662019-06-18 11:55:15 +0200488 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100489 break;
490 case YANG_DESCRIPTION:
David Sedlák15a92662019-06-18 11:55:15 +0200491 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100492 break;
493 case YANG_REFERENCE:
David Sedlák15a92662019-06-18 11:55:15 +0200494 LY_CHECK_RET(yin_parse_meta_element(xml_ctx, data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100495 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100496
David Sedlákc136b972019-03-08 13:39:06 +0100497 default:
David Sedlák18730132019-03-15 15:51:34 +0100498 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100499 break;
500 }
David Sedlák4a4c0722018-11-26 17:03:10 +0100501 }
502 }
503
David Sedlák68826732019-06-05 10:50:58 +0200504 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200505}
506
David Sedlákd5318ee2019-02-15 10:14:50 +0100507/**
508 * @brief Parse yin submodule.
509 *
510 * @param[in] ctx Context of YANG schemas.
David Sedlákb666bcc2019-06-05 15:00:05 +0200511 * @param[in,out] data Data to read from.
512 * @param[out] submod Parsed submodule structure.
David Sedlákd5318ee2019-02-15 10:14:50 +0100513 *
514 * @return LY_ERR values.
515 */
516LY_ERR
517yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod)
518{
519 LY_ERR ret = LY_SUCCESS;
520 enum yang_keyword kw = YANG_NONE;
521 struct lyxml_context xml_ctx;
522 struct lysp_submodule *mod_p = NULL;
523 const char *prefix, *name;
524 size_t prefix_len, name_len;
David Sedlák3017da42019-02-15 09:48:04 +0100525
David Sedlákd5318ee2019-02-15 10:14:50 +0100526 /* initialize xml context */
527 memset(&xml_ctx, 0, sizeof xml_ctx);
528 xml_ctx.ctx = ctx;
529 xml_ctx.line = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100530
David Sedlákd5318ee2019-02-15 10:14:50 +0100531 /* check submodule */
532 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
533 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200534 kw = yin_match_keyword(name, name_len, prefix_len);
David Sedlákd5318ee2019-02-15 10:14:50 +0100535 if (kw == YANG_MODULE) {
536 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
537 ret = LY_EINVAL;
538 goto cleanup;
539 } else if (kw != YANG_SUBMODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100540 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
541 ly_stmt2str(kw));
David Sedlákd5318ee2019-02-15 10:14:50 +0100542 ret = LY_EVALID;
543 goto cleanup;
544 }
David Sedlák3017da42019-02-15 09:48:04 +0100545
David Sedlákd5318ee2019-02-15 10:14:50 +0100546 /* allocate module */
547 mod_p = calloc(1, sizeof *mod_p);
548 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
549 mod_p->parsing = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100550
David Sedlákd5318ee2019-02-15 10:14:50 +0100551 /* parser submodule substatements */
David Sedlákb666bcc2019-06-05 15:00:05 +0200552 /* ret = parse_submod(&xml_ctx, &data, mod_p); */
David Sedlákd5318ee2019-02-15 10:14:50 +0100553 LY_CHECK_GOTO(ret, cleanup);
David Sedlák3017da42019-02-15 09:48:04 +0100554
David Sedlákd5318ee2019-02-15 10:14:50 +0100555 mod_p->parsing = 0;
556 *submod = mod_p;
David Sedlák3017da42019-02-15 09:48:04 +0100557
David Sedlákd5318ee2019-02-15 10:14:50 +0100558cleanup:
559 if (ret) {
560 lysp_submodule_free(ctx, mod_p);
561 }
David Sedlák3017da42019-02-15 09:48:04 +0100562
David Sedlákd5318ee2019-02-15 10:14:50 +0100563 lyxml_context_clear(&xml_ctx);
564 return ret;
565}
David Sedlák3017da42019-02-15 09:48:04 +0100566
David Sedlák3b4db242018-10-19 16:11:01 +0200567LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100568yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200569{
David Sedláke4889912018-11-02 09:52:40 +0100570 LY_ERR ret = LY_SUCCESS;
571 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200572 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200573 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100574 struct lysp_module *mod_p = NULL;
575 const char *prefix, *name;
576 size_t prefix_len, name_len;
David Sedlák3b4db242018-10-19 16:11:01 +0200577
David Sedlák3017da42019-02-15 09:48:04 +0100578 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200579 memset(&parser_ctx, 0, sizeof parser_ctx);
580 xml_ctx->ctx = ctx;
581 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100582
David Sedlák3017da42019-02-15 09:48:04 +0100583 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200584 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák3017da42019-02-15 09:48:04 +0100585 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200586 kw = yin_match_keyword(name, name_len, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100587 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100588 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
589 ret = LY_EINVAL;
590 goto cleanup;
591 } else if (kw != YANG_MODULE) {
David Sedlákaadab9c2019-04-05 15:01:27 +0200592 LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200593 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100594 ret = LY_EVALID;
595 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100596 }
597
David Sedlák3017da42019-02-15 09:48:04 +0100598 /* allocate module */
599 mod_p = calloc(1, sizeof *mod_p);
600 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
601 mod_p->mod = mod;
602 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100603
David Sedlák3017da42019-02-15 09:48:04 +0100604 /* parser module substatements */
David Sedlákaadab9c2019-04-05 15:01:27 +0200605 ret = parse_mod(xml_ctx, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100606 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100607
David Sedlák3017da42019-02-15 09:48:04 +0100608 mod_p->parsing = 0;
609 mod->parsed = mod_p;
610
611cleanup:
612 if (ret) {
613 lysp_module_free(mod_p);
614 }
615
David Sedlákaadab9c2019-04-05 15:01:27 +0200616 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100617 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200618}