blob: af8b4008532e71ef0cf10e901f734b3f046eed4f [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 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
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ák3b4db242018-10-19 16:11:01 +020014#include <stdio.h>
15#include <stdlib.h>
16#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020017#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020018
19#include "common.h"
20#include "context.h"
21#include "libyang.h"
David Sedlák3b4db242018-10-19 16:11:01 +020022#include "xml.h"
23#include "tree_schema_internal.h"
David Sedlákf824ad52018-10-14 23:58:15 +020024
David Sedlák872c7b42018-10-26 13:15:20 +020025enum YIN_ARGUMENT {
David Sedláka7406952019-04-05 10:33:07 +020026 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any known yin argument keyword */
David Sedlák18730132019-03-15 15:51:34 +010027 YIN_ARG_NAME, /**< argument name */
David Sedláka7406952019-04-05 10:33:07 +020028 YIN_ARG_TARGET_NODE, /**< argument target-node */
David Sedlák18730132019-03-15 15:51:34 +010029 YIN_ARG_MODULE, /**< argument module */
30 YIN_ARG_VALUE, /**< argument value */
31 YIN_ARG_TEXT, /**< argument text */
32 YIN_ARG_CONDITION, /**< argument condition */
33 YIN_ARG_URI, /**< argument uri */
34 YIN_ARG_DATE, /**< argument data */
35 YIN_ARG_TAG, /**< argument tag */
36 YIN_ARG_XMLNS, /**< argument xmlns */
David Sedláka7406952019-04-05 10:33:07 +020037 YIN_ARG_NONE, /**< special value to specify no valid argument except xmlns, do not confuse with YIN_ARG_NONE */
David Sedlák872c7b42018-10-26 13:15:20 +020038};
David Sedlákf824ad52018-10-14 23:58:15 +020039
David Sedlákc136b972019-03-08 13:39:06 +010040/**
David Sedlákd5318ee2019-02-15 10:14:50 +010041 * @brief Match argument name.
42 *
43 * @param[in] name String representing name.
44 * @param[in] len Lenght of the name.
45 *
46 * @reurn YIN_ARGUMENT value.
47 */
David Sedlák872c7b42018-10-26 13:15:20 +020048enum YIN_ARGUMENT
David Sedláke4889912018-11-02 09:52:40 +010049match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020050{
David Sedláka7406952019-04-05 10:33:07 +020051 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020052 size_t already_read = 0;
David Sedláka7406952019-04-05 10:33:07 +020053 LY_CHECK_RET(len == 0, YIN_ARG_UNKNOWN);
David Sedlák3b4db242018-10-19 16:11:01 +020054
David Sedlák94de2aa2019-02-15 12:42:11 +010055#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
56#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010057#define IF_ARG_PREFIX_END }
58
David Sedlák1c8b2702019-02-22 11:03:02 +010059 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +010060 case 'x':
61 already_read += 1;
62 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
63 break;
David Sedlák94de2aa2019-02-15 12:42:11 +010064 case 'c':
65 already_read += 1;
66 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +020067 break;
David Sedlák872c7b42018-10-26 13:15:20 +020068
David Sedlák94de2aa2019-02-15 12:42:11 +010069 case 'd':
70 already_read += 1;
71 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +020072 break;
David Sedlák872c7b42018-10-26 13:15:20 +020073
David Sedlák94de2aa2019-02-15 12:42:11 +010074 case 'm':
75 already_read += 1;
76 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +020077 break;
78
David Sedlák94de2aa2019-02-15 12:42:11 +010079 case 'n':
80 already_read += 1;
81 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +020082 break;
83
David Sedlák94de2aa2019-02-15 12:42:11 +010084 case 't':
85 already_read += 1;
86 IF_ARG_PREFIX("a", 1)
87 IF_ARG("g", 1, YIN_ARG_TAG)
88 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
89 IF_ARG_PREFIX_END
90 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +020091 break;
David Sedlák872c7b42018-10-26 13:15:20 +020092
David Sedlák94de2aa2019-02-15 12:42:11 +010093 case 'u':
94 already_read += 1;
95 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +020096 break;
David Sedlák872c7b42018-10-26 13:15:20 +020097
David Sedlák94de2aa2019-02-15 12:42:11 +010098 case 'v':
99 already_read += 1;
100 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200101 break;
102 }
103
David Sedlákc10e7902018-12-17 02:17:59 +0100104 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200105 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200106 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200107 }
108
David Sedlák18730132019-03-15 15:51:34 +0100109#undef IF_ARG
110#undef IF_ARG_PREFIX
111#undef IF_ARG_PREFIX_END
112
David Sedlák872c7b42018-10-26 13:15:20 +0200113 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200114}
115
David Sedlák18730132019-03-15 15:51:34 +0100116/**
117 * @brief parse xmlns statement
118 *
119 * @param[in] xml_ctx XML parser context.
120 * @param[in, out] data Data to reda from.
121 * @param[in] prefix
122 */
123LY_ERR
124parse_xmlns(struct lyxml_context *xml_ctx, const char **data, const char *prefix, size_t prefix_len, char *element)
125{
126 char *buf = NULL, *out = NULL;
127 size_t buf_len = 0, out_len = 0;
128 int dynamic = 0;
129 LY_ERR ret = LY_SUCCESS;
130
131 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
132 LY_CHECK_RET(ret != LY_SUCCESS, ret);
133 LY_CHECK_ERR_RET(out_len == 0, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Missing value of xmlns attribute"), LY_EEXIST);
134 lyxml_ns_add(xml_ctx, element, prefix, prefix_len, out, out_len);
135
136 return LY_SUCCESS;
137}
138
David Sedlák0daba9a2019-03-22 14:07:49 +0100139/**
David Sedláka7406952019-04-05 10:33:07 +0200140 * @brief parse yin argument and xmlns definition
141 *
142 * @param[in] xml_ctx XML parser context.
143 * @param[in, out] data Data to read from.
144 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments).
145 * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is spedified as YIN_ARG_NONE.
146 * @param[in] elem_name Name of currently parse element, used to create namespace scoping
147 */
148static LY_ERR
149yin_parse_attribute(struct lyxml_context *xml_ctx, const char **data, enum YIN_ARGUMENT arg_type, const char **arg_val, char *elem_name)
150{
151 LY_ERR ret = LY_SUCCESS;
152 const char *prefix, *name;
153 size_t prefix_len, name_len;
154
155 char *buf = NULL, *out = NULL;
156 size_t buf_len = 0, out_len = 0;
157 int dynamic;
158 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
159
160 while (xml_ctx->status == LYXML_ATTRIBUTE) {
161 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
162 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
163
164 arg = match_argument_name(name, name_len);
165
166 if (arg == YIN_ARG_XMLNS) {
167 parse_xmlns(xml_ctx, data, prefix, prefix_len, elem_name);
168 } else if (arg == arg_type) {
169 LY_CHECK_RET(ret);
170 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
171 LY_CHECK_RET(ret);
172 *arg_val = lydict_insert(xml_ctx->ctx, out, out_len);
173 LY_CHECK_ERR_RET(!(*arg_val), LOGMEM(xml_ctx->ctx), LY_EMEM);
174 } else {
175 /* unrecognized attribute, still can be namespace definition eg. xmlns:foo=.... */
176 /* TODO this might be extension and not ns definition */
177 if (match_argument_name(prefix, prefix_len) == YIN_ARG_XMLNS) {
178 /* in this case prefix of namespace is actually name of attribute */
179 parse_xmlns(xml_ctx, data, name, name_len, elem_name);
180 } else {
181 /* unrecognized or unexpected attribute */
182 LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in namespace element");
183 return LY_EVALID;
184 }
185 }
186 }
187
188 return LY_SUCCESS;
189}
190
191/**
David Sedlák0daba9a2019-03-22 14:07:49 +0100192 * @brief Parse content of whole element as text.
193 *
194 * @param[in] xml_ctx Xml context.
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100195 * @param[in] element_name Name of element, name is necesary to remove xmlns definitions at the end of element
David Sedlák0daba9a2019-03-22 14:07:49 +0100196 * @param[in] data Data to read from.
197 * @param[out] value Where content of element should be stored.
198 */
199LY_ERR
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100200parse_text_element(struct lyxml_context *xml_ctx, char *element_name, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100201{
202 LY_ERR ret = LY_SUCCESS;
203 char *buf = NULL, *out = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200204 size_t buf_len = 0, out_len = 0, prefix_len, name_len;
David Sedlák0daba9a2019-03-22 14:07:49 +0100205 int dynamic;
David Sedlák0daba9a2019-03-22 14:07:49 +0100206 const char *prefix, *name;
David Sedlák0daba9a2019-03-22 14:07:49 +0100207
David Sedláka7406952019-04-05 10:33:07 +0200208 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL, element_name);
209 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlák0daba9a2019-03-22 14:07:49 +0100210 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
211
212 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
213 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
214 LY_CHECK_RET(ret);
215 *value = lydict_insert(xml_ctx->ctx, out, out_len);
216 LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM);
217 }
218
219 lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100220 lyxml_ns_rm(xml_ctx, element_name);
221 return LY_SUCCESS;
David Sedlák0daba9a2019-03-22 14:07:49 +0100222}
223
David Sedlák3017da42019-02-15 09:48:04 +0100224// LY_ERR
225// parser_belongs_to(struct lyxml_context *xml_ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext **extensions)
226// {
227// enum yang_keyword kw = YANG_NONE;
228// LY_ERR ret = LY_SUCCESS;
229// const char *prefix_out, *name;
230// size_t prefix_len, name_len;
David Sedláka5004e62018-12-16 23:54:47 +0100231
David Sedlák3017da42019-02-15 09:48:04 +0100232// char *buf = NULL, *out = NULL;
233// size_t buf_len = 0, out_len = 0;
234// int dynamic;
David Sedláka5004e62018-12-16 23:54:47 +0100235
David Sedlák3017da42019-02-15 09:48:04 +0100236// /* check if belongs-to has argument module */
237// ret = lyxml_get_attribute(xml_ctx, data, &prefix_out, &prefix_len, &name, &name_len);
238// LY_CHECK_RET1(ret);
239// if (match_argument_name(name, name_len) != YIN_ARG_MODULE) {
240// LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"module\".", name);
241// return LY_EINVAL;
242// }
David Sedláka5004e62018-12-16 23:54:47 +0100243
David Sedlák3017da42019-02-15 09:48:04 +0100244// /* read content of argument */
245// ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
246// LY_CHECK_RET1(ret);
247// *belongsto = lydict_insert(xml_ctx->ctx, out, out_len);
248// LY_CHECK_ERR_RET(!belongsto, LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedláka5004e62018-12-16 23:54:47 +0100249
David Sedlák3017da42019-02-15 09:48:04 +0100250// /* read substatements */
251// while (xml_ctx->status == LYXML_ATTRIBUTE) {
252// ret = lyxml_get_attribute(xml_ctx, data, &prefix_out, &prefix_len, &name, &name_len);
253// LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), ret);
254// kw = match_keyword(name);
David Sedláka5004e62018-12-16 23:54:47 +0100255
David Sedlák3017da42019-02-15 09:48:04 +0100256// switch (kw) {
257// case YANG_PREFIX:
258// ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
259// *prefix = lydict_insert(xml_ctx->ctx, out, out_len);
260// break;
261// case YANG_CUSTOM:
262// /* TODO parse extension */
263// break;
264// default:
265// LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected attribute");
266// return LY_EVALID;
267// }
268// }
David Sedláka5004e62018-12-16 23:54:47 +0100269
David Sedlák3017da42019-02-15 09:48:04 +0100270// if (!prefix) {
271// LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing prefix");
272// return LY_EVALID;
273// }
David Sedlákc10e7902018-12-17 02:17:59 +0100274
David Sedlák3017da42019-02-15 09:48:04 +0100275// return LY_SUCCESS;
276// }
David Sedláka5004e62018-12-16 23:54:47 +0100277
David Sedlákd5318ee2019-02-15 10:14:50 +0100278/**
279 * @brief Parse namespace statement.
280 *
281 * @param[in] xml_ctx xml context.
282 * @param[in, out] data Data to read from.
David Sedlák81e04022019-04-05 15:05:46 +0200283 * @param[in, out] namespace Where namespace value should be stored.
David Sedlákd5318ee2019-02-15 10:14:50 +0100284 *
285 * @return LY_ERR values.
286 */
David Sedláka5004e62018-12-16 23:54:47 +0100287LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200288parse_namespace(struct lyxml_context *xml_ctx, const char **data, const char **namespace)
David Sedlák4a4c0722018-11-26 17:03:10 +0100289{
290 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100291
David Sedlák81e04022019-04-05 15:05:46 +0200292 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, namespace, "namespace");
David Sedláka7406952019-04-05 10:33:07 +0200293 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlák4a4c0722018-11-26 17:03:10 +0100294
David Sedlák24d90b92019-03-29 10:00:35 +0100295 /* remove local xmlns definitions */
296 lyxml_ns_rm(xml_ctx, "namespace");
David Sedlák4a4c0722018-11-26 17:03:10 +0100297 return LY_SUCCESS;
298}
299
David Sedlákd5318ee2019-02-15 10:14:50 +0100300/**
301 * @brief Parse prefix statement.
302 *
303 * @param[in] xml_ctx Xml context.
304 * @param[in, out] data Data to reda from.
David Sedlák81e04022019-04-05 15:05:46 +0200305 * @param[out] prefix Where to store prefix value.
David Sedlákd5318ee2019-02-15 10:14:50 +0100306 *
307 * @return LY_ERR values.
308 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100309LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200310parse_prefix(struct lyxml_context *xml_ctx, const char **data, const char **prefix)
David Sedlák4a4c0722018-11-26 17:03:10 +0100311{
312 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100313
David Sedlákcd0c9512019-03-29 13:23:06 +0100314 /* parse attributes */
David Sedlák81e04022019-04-05 15:05:46 +0200315 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, prefix, "prefix");
David Sedlákcd0c9512019-03-29 13:23:06 +0100316 LY_CHECK_RET(ret != LY_SUCCESS, ret);
317 /* remove local xmlns definitions */
318 ret = lyxml_ns_rm(xml_ctx, "prefix");
319 return ret;
David Sedlák4a4c0722018-11-26 17:03:10 +0100320}
321
David Sedlák81e04022019-04-05 15:05:46 +0200322static LY_ERR
323yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
324{
325 LY_ERR ret = LY_SUCCESS;
David Sedlákcd0c9512019-03-29 13:23:06 +0100326
David Sedlák81e04022019-04-05 15:05:46 +0200327 if (rev[0]) {
328 LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
329 return LY_EVALID;
330 }
331
332 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, (const char **)&rev, "revision-date");
333 LY_CHECK_RET(ret != LY_SUCCESS, ret);
334 LY_CHECK_RET(lysp_check_date((struct ly_parser_ctx *)xml_ctx, rev, strlen(rev), "revision-date") != LY_SUCCESS, LY_EVALID);
335
336 ret = lyxml_ns_rm(xml_ctx, "import");
337 return ret;
338}
David Sedlákcd0c9512019-03-29 13:23:06 +0100339
David Sedlák736fd0d2019-02-15 16:06:31 +0100340static LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200341yin_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 +0100342{
343 LY_ERR ret = LY_SUCCESS;
344 enum yang_keyword kw;
345 struct lysp_import *imp;
346 const char *prefix, *name;
347 size_t prefix_len, name_len;
348
David Sedlák81e04022019-04-05 15:05:46 +0200349 /* allocate sized array for imports */
350 LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100351
David Sedláka7406952019-04-05 10:33:07 +0200352 /* parse import attributes */
David Sedlák81e04022019-04-05 15:05:46 +0200353 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name, "import");
David Sedlák736fd0d2019-02-15 16:06:31 +0100354 while ((ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS && name != NULL)) {
David Sedlák18730132019-03-15 15:51:34 +0100355 kw = match_keyword(name, name_len, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100356 switch (kw) {
357 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200358 LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID);
359 parse_prefix(xml_ctx, data, &imp->prefix);
David Sedlákcd0c9512019-03-29 13:23:06 +0100360 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100361 case YANG_DESCRIPTION:
David Sedlák81e04022019-04-05 15:05:46 +0200362 LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID);
363 parse_text_element(xml_ctx, "description", data, &imp->dsc);
David Sedlákcd0c9512019-03-29 13:23:06 +0100364 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100365 case YANG_REFERENCE:
David Sedlák81e04022019-04-05 15:05:46 +0200366 LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID);
367 parse_text_element(xml_ctx, "reference", data, &imp->ref);
David Sedlákcd0c9512019-03-29 13:23:06 +0100368 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100369 case YANG_REVISION_DATE:
David Sedlák81e04022019-04-05 15:05:46 +0200370 yin_parse_revision_date(xml_ctx, data, &imp->rev, &imp->exts);
David Sedlák3a55bc12019-06-03 08:57:29 +0200371 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100372 case YANG_CUSTOM:
373 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200374 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100375 default:
376 /* TODO log error */
377 return LY_EVALID;
378 }
379 }
380
381 /* TODO add log macro and log error */
382 LY_CHECK_RET(!imp->prefix);
David Sedláka7406952019-04-05 10:33:07 +0200383 ret = lyxml_ns_rm(xml_ctx, "import");
David Sedlák736fd0d2019-02-15 16:06:31 +0100384 return ret;
385}
386
David Sedlákd5318ee2019-02-15 10:14:50 +0100387/**
388 * @brief Parse module substatements.
389 *
390 * @param[in] xml_ctx xml context.
391 * @param[in, out] data Data to read from.
392 * @param[out] mod Parsed module structure
393 *
394 * @return LY_ERR values.
395 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100396LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100397parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200398{
David Sedlákf3b24f62018-11-02 10:40:47 +0100399 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100400 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100401 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200402 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100403 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedláka7406952019-04-05 10:33:07 +0200404 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák3b4db242018-10-19 16:11:01 +0200405
David Sedlákf3b24f62018-11-02 10:40:47 +0100406 char *buf = NULL, *out = NULL;
407 size_t buf_len = 0, out_len = 0;
408 int dynamic;
409
David Sedlák18730132019-03-15 15:51:34 +0100410 /* parse module attributes */
411 while (xml_ctx->status == LYXML_ATTRIBUTE) {
412 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
413 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
414
415 arg = match_argument_name(name, name_len);
416
417 switch (arg) {
418 case YIN_ARG_XMLNS:
419 parse_xmlns(xml_ctx, data, prefix, prefix_len, "module");
420 break;
421 case YIN_ARG_NAME:
422 /* check for multiple definitions of name */
423 LY_CHECK_ERR_RET((*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Duplicit definition of module name \"%s\"", (*mod)->mod->name), LY_EEXIST);
424
425 /* read module name */
426 if (xml_ctx->status != LYXML_ATTR_CONTENT) {
427 LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\".");
428 }
429 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
430 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
431 (*mod)->mod->name = lydict_insert(xml_ctx->ctx, out, out_len);
432 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
433 break;
434 default:
435 /* unrecognized attribute, still can be namespace definition eg. xmlns:foo=.... */
436 if (match_argument_name(prefix, prefix_len) == YIN_ARG_XMLNS) {
437 /* in this case prefix of namespace is actually name of attribute */
438 parse_xmlns(xml_ctx, data, name, name_len, "module");
439 } else {
440 /* unrecognized or unexpected attribute */
441 LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in module element");
442 return LY_EVALID;
443 }
444 break;
445 }
David Sedlák3b4db242018-10-19 16:11:01 +0200446 }
447
David Sedlák18730132019-03-15 15:51:34 +0100448 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module", (*mod)->mod->name), LY_ENOTFOUND);
David Sedlákf3b24f62018-11-02 10:40:47 +0100449
David Sedlákc136b972019-03-08 13:39:06 +0100450 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák18730132019-03-15 15:51:34 +0100451 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 +0100452
David Sedlák736fd0d2019-02-15 16:06:31 +0100453 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100454 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100455/* TODO ADD error log to macro */
456#define CHECK_ORDER(SECTION) \
457 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
458
459 switch (kw) {
460 /* module header */
461 case YANG_NAMESPACE:
462 case YANG_PREFIX:
463 CHECK_ORDER(Y_MOD_MODULE_HEADER);
464 break;
465 case YANG_YANG_VERSION:
466 CHECK_ORDER(Y_MOD_MODULE_HEADER);
467 break;
468 /* linkage */
469 case YANG_INCLUDE:
470 case YANG_IMPORT:
471 CHECK_ORDER(Y_MOD_LINKAGE);
472 break;
473 /* meta */
474 case YANG_ORGANIZATION:
475 case YANG_CONTACT:
476 case YANG_DESCRIPTION:
477 case YANG_REFERENCE:
478 CHECK_ORDER(Y_MOD_META);
479 break;
480
481 /* revision */
482 case YANG_REVISION:
483 CHECK_ORDER(Y_MOD_REVISION);
484 break;
485 /* body */
486 case YANG_ANYDATA:
487 case YANG_ANYXML:
488 case YANG_AUGMENT:
489 case YANG_CHOICE:
490 case YANG_CONTAINER:
491 case YANG_DEVIATION:
492 case YANG_EXTENSION:
493 case YANG_FEATURE:
494 case YANG_GROUPING:
495 case YANG_IDENTITY:
496 case YANG_LEAF:
497 case YANG_LEAF_LIST:
498 case YANG_LIST:
499 case YANG_NOTIFICATION:
500 case YANG_RPC:
501 case YANG_TYPEDEF:
502 case YANG_USES:
503 case YANG_CUSTOM:
504 mod_stmt = Y_MOD_BODY;
505 break;
506 default:
507 /* error will be handled in the next switch */
508 break;
509 }
510#undef CHECK_ORDER
511
David Sedlák4a4c0722018-11-26 17:03:10 +0100512 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlákc136b972019-03-08 13:39:06 +0100513 LY_CHECK_RET(ret != LY_SUCCESS, LY_EMEM);
David Sedlák4a4c0722018-11-26 17:03:10 +0100514
David Sedlákc136b972019-03-08 13:39:06 +0100515 if (name) {
David Sedlák18730132019-03-15 15:51:34 +0100516 kw = match_keyword(name, name_len, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100517 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100518
David Sedlákc136b972019-03-08 13:39:06 +0100519 /* module header */
520 case YANG_NAMESPACE:
David Sedlák81e04022019-04-05 15:05:46 +0200521 LY_CHECK_RET(parse_namespace(xml_ctx, data, &(*mod)->mod->ns));
David Sedlákc136b972019-03-08 13:39:06 +0100522 break;
523 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200524 LY_CHECK_RET(parse_prefix(xml_ctx, data, &(*mod)->mod->prefix));
David Sedlákc136b972019-03-08 13:39:06 +0100525 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100526
David Sedlákc136b972019-03-08 13:39:06 +0100527 /* linkage */
528 case YANG_IMPORT:
David Sedlák81e04022019-04-05 15:05:46 +0200529 yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports);
David Sedlákc136b972019-03-08 13:39:06 +0100530 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100531
David Sedlákc136b972019-03-08 13:39:06 +0100532 /* meta */
533 case YANG_ORGANIZATION:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100534 LY_CHECK_RET(parse_text_element(xml_ctx, "organization", data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100535 break;
536 case YANG_CONTACT:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100537 LY_CHECK_RET(parse_text_element(xml_ctx, "contact", data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100538 break;
539 case YANG_DESCRIPTION:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100540 LY_CHECK_RET(parse_text_element(xml_ctx, "description", data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100541 break;
542 case YANG_REFERENCE:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100543 LY_CHECK_RET(parse_text_element(xml_ctx, "reference", data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100544 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100545
David Sedlákc136b972019-03-08 13:39:06 +0100546 default:
David Sedlák18730132019-03-15 15:51:34 +0100547 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100548 break;
549 }
David Sedlák4a4c0722018-11-26 17:03:10 +0100550 }
551 }
552
David Sedlák18730132019-03-15 15:51:34 +0100553 lyxml_ns_rm(xml_ctx, "module");
David Sedlákf3b24f62018-11-02 10:40:47 +0100554 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200555}
556
David Sedlákd5318ee2019-02-15 10:14:50 +0100557/**
558 * @brief Parse yin submodule.
559 *
560 * @param[in] ctx Context of YANG schemas.
561 * @param[in] data Data to read from.
562 * @param[out] submod Module to write to.
563 *
564 * @return LY_ERR values.
565 */
566LY_ERR
567yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod)
568{
569 LY_ERR ret = LY_SUCCESS;
570 enum yang_keyword kw = YANG_NONE;
571 struct lyxml_context xml_ctx;
572 struct lysp_submodule *mod_p = NULL;
573 const char *prefix, *name;
574 size_t prefix_len, name_len;
David Sedlák3017da42019-02-15 09:48:04 +0100575
David Sedlákd5318ee2019-02-15 10:14:50 +0100576 /* initialize xml context */
577 memset(&xml_ctx, 0, sizeof xml_ctx);
578 xml_ctx.ctx = ctx;
579 xml_ctx.line = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100580
David Sedlákd5318ee2019-02-15 10:14:50 +0100581 /* check submodule */
582 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
583 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100584 kw = match_keyword(name, name_len, prefix_len);
David Sedlákd5318ee2019-02-15 10:14:50 +0100585 if (kw == YANG_MODULE) {
586 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
587 ret = LY_EINVAL;
588 goto cleanup;
589 } else if (kw != YANG_SUBMODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100590 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
591 ly_stmt2str(kw));
David Sedlákd5318ee2019-02-15 10:14:50 +0100592 ret = LY_EVALID;
593 goto cleanup;
594 }
David Sedlák3017da42019-02-15 09:48:04 +0100595
David Sedlákd5318ee2019-02-15 10:14:50 +0100596 /* allocate module */
597 mod_p = calloc(1, sizeof *mod_p);
598 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
599 mod_p->parsing = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100600
David Sedlákd5318ee2019-02-15 10:14:50 +0100601 /* parser submodule substatements */
602 //ret = parse_submod(&xml_ctx, &data, mod_p);
603 LY_CHECK_GOTO(ret, cleanup);
David Sedlák3017da42019-02-15 09:48:04 +0100604
David Sedlákd5318ee2019-02-15 10:14:50 +0100605 mod_p->parsing = 0;
606 *submod = mod_p;
David Sedlák3017da42019-02-15 09:48:04 +0100607
David Sedlákd5318ee2019-02-15 10:14:50 +0100608cleanup:
609 if (ret) {
610 lysp_submodule_free(ctx, mod_p);
611 }
David Sedlák3017da42019-02-15 09:48:04 +0100612
David Sedlákd5318ee2019-02-15 10:14:50 +0100613 lyxml_context_clear(&xml_ctx);
614 return ret;
615}
David Sedlák3017da42019-02-15 09:48:04 +0100616
David Sedlákd5318ee2019-02-15 10:14:50 +0100617/**
618 * @brief Parse yin module.
619 *
620 * @param[in] ctx Context of YANG schemas.
621 * @param[in] data Data to read from.
622 * @param[out] mod Module to write to.
623 *
624 * @return LY_ERR values.
625 */
David Sedlák3b4db242018-10-19 16:11:01 +0200626LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100627yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200628{
David Sedláke4889912018-11-02 09:52:40 +0100629 LY_ERR ret = LY_SUCCESS;
630 enum yang_keyword kw = YANG_NONE;
David Sedlákaadab9c2019-04-05 15:01:27 +0200631 struct ly_parser_ctx parser_ctx;
632 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100633 struct lysp_module *mod_p = NULL;
634 const char *prefix, *name;
635 size_t prefix_len, name_len;
David Sedlák3b4db242018-10-19 16:11:01 +0200636
David Sedlák3017da42019-02-15 09:48:04 +0100637 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200638 memset(&parser_ctx, 0, sizeof parser_ctx);
639 xml_ctx->ctx = ctx;
640 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100641
David Sedlák3017da42019-02-15 09:48:04 +0100642 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200643 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák3017da42019-02-15 09:48:04 +0100644 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100645 kw = match_keyword(name, name_len, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100646 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100647 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
648 ret = LY_EINVAL;
649 goto cleanup;
650 } else if (kw != YANG_MODULE) {
David Sedlákaadab9c2019-04-05 15:01:27 +0200651 LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák0a875b42019-03-07 22:24:05 +0100652 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100653 ret = LY_EVALID;
654 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100655 }
656
David Sedlák3017da42019-02-15 09:48:04 +0100657 /* allocate module */
658 mod_p = calloc(1, sizeof *mod_p);
659 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
660 mod_p->mod = mod;
661 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100662
David Sedlák3017da42019-02-15 09:48:04 +0100663 /* parser module substatements */
David Sedlákaadab9c2019-04-05 15:01:27 +0200664 ret = parse_mod(xml_ctx, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100665 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100666
David Sedlák3017da42019-02-15 09:48:04 +0100667 mod_p->parsing = 0;
668 mod->parsed = mod_p;
669
670cleanup:
671 if (ret) {
672 lysp_module_free(mod_p);
673 }
674
David Sedlákaadab9c2019-04-05 15:01:27 +0200675 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100676 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200677}