blob: 4f1df8836966d018ef00b300da0b762854b841a4 [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.
283 * @param[in, out] mod_p Module to write to.
284 *
285 * @return LY_ERR values.
286 */
David Sedláka5004e62018-12-16 23:54:47 +0100287LY_ERR
David Sedlák0daba9a2019-03-22 14:07:49 +0100288parse_namespace(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
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áka7406952019-04-05 10:33:07 +0200292 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, &((*mod)->mod->ns), "namespace");
293 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ák24d90b92019-03-29 10:00:35 +0100305 * @param[out] mod Module to write to.
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ák24d90b92019-03-29 10:00:35 +0100310parse_prefix(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
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áka7406952019-04-05 10:33:07 +0200315 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &(*mod)->mod->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ákcd0c9512019-03-29 13:23:06 +0100322// static LY_ERR
323// yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
324// {
325
326// return LY_SUCCESS;
327// }
328
David Sedlák736fd0d2019-02-15 16:06:31 +0100329static LY_ERR
David Sedlákcd0c9512019-03-29 13:23:06 +0100330yin_parse_import(struct lyxml_context *xml_ctx, const char *module_prefix, const char **data, struct lysp_module **mod)
David Sedlák736fd0d2019-02-15 16:06:31 +0100331{
332 LY_ERR ret = LY_SUCCESS;
333 enum yang_keyword kw;
334 struct lysp_import *imp;
335 const char *prefix, *name;
336 size_t prefix_len, name_len;
337
338 char *buf = NULL, *out = NULL;
339 size_t buf_len = 0, out_len = 0;
340 int dynamic;
341
David Sedláka7406952019-04-05 10:33:07 +0200342 /* parse import attributes */
343 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &(*mod)->mod->prefix, "import");
David Sedlákcd0c9512019-03-29 13:23:06 +0100344
345 /* valid subelements description, prefix, reference, revision-data */
David Sedlák736fd0d2019-02-15 16:06:31 +0100346 /* allocate sized array for imports */
David Sedlákcd0c9512019-03-29 13:23:06 +0100347 LY_ARRAY_NEW_RET(xml_ctx->ctx, (*mod)->imports, imp, LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100348
349 /* get value */
350 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
351 LY_CHECK_RET(ret);
352 if (match_argument_name(name, name_len) != YIN_ARG_MODULE) {
David Sedlák1c8b2702019-02-22 11:03:02 +0100353 LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"module\".", name);
David Sedlák736fd0d2019-02-15 16:06:31 +0100354 return LY_EVALID;
355 }
356 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
357 LY_CHECK_RET(ret);
358 imp->name = lydict_insert(xml_ctx->ctx, out, out_len);
359 LY_CHECK_ERR_RET(!imp->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
360
David Sedlák736fd0d2019-02-15 16:06:31 +0100361 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 +0100362 kw = match_keyword(name, name_len, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100363 switch (kw) {
364 case YANG_PREFIX:
David Sedlákcd0c9512019-03-29 13:23:06 +0100365 parse_prefix(xml_ctx, data, mod);
366 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100367 case YANG_DESCRIPTION:
David Sedlákcd0c9512019-03-29 13:23:06 +0100368 parse_text_element(xml_ctx, "description", data, &((*mod)->mod->dsc));
369 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100370 case YANG_REFERENCE:
David Sedlákcd0c9512019-03-29 13:23:06 +0100371 parse_text_element(xml_ctx, "reference", data, &((*mod)->imports->ref));
372 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100373 case YANG_REVISION_DATE:
David Sedlákcd0c9512019-03-29 13:23:06 +0100374 /* this is attribute of import element */
David Sedlák736fd0d2019-02-15 16:06:31 +0100375 case YANG_CUSTOM:
376 /* TODO parse extension */
377 default:
378 /* TODO log error */
379 return LY_EVALID;
380 }
381 }
382
383 /* TODO add log macro and log error */
384 LY_CHECK_RET(!imp->prefix);
David Sedláka7406952019-04-05 10:33:07 +0200385 ret = lyxml_ns_rm(xml_ctx, "import");
David Sedlák736fd0d2019-02-15 16:06:31 +0100386 return ret;
387}
388
David Sedlákd5318ee2019-02-15 10:14:50 +0100389/**
390 * @brief Parse module substatements.
391 *
392 * @param[in] xml_ctx xml context.
393 * @param[in, out] data Data to read from.
394 * @param[out] mod Parsed module structure
395 *
396 * @return LY_ERR values.
397 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100398LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100399parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200400{
David Sedlákf3b24f62018-11-02 10:40:47 +0100401 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100402 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100403 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200404 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100405 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedláka7406952019-04-05 10:33:07 +0200406 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák3b4db242018-10-19 16:11:01 +0200407
David Sedlákf3b24f62018-11-02 10:40:47 +0100408 char *buf = NULL, *out = NULL;
409 size_t buf_len = 0, out_len = 0;
410 int dynamic;
411
David Sedlák18730132019-03-15 15:51:34 +0100412 /* parse module attributes */
413 while (xml_ctx->status == LYXML_ATTRIBUTE) {
414 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
415 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
416
417 arg = match_argument_name(name, name_len);
418
419 switch (arg) {
420 case YIN_ARG_XMLNS:
421 parse_xmlns(xml_ctx, data, prefix, prefix_len, "module");
422 break;
423 case YIN_ARG_NAME:
424 /* check for multiple definitions of name */
425 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);
426
427 /* read module name */
428 if (xml_ctx->status != LYXML_ATTR_CONTENT) {
429 LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\".");
430 }
431 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
432 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
433 (*mod)->mod->name = lydict_insert(xml_ctx->ctx, out, out_len);
434 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
435 break;
436 default:
437 /* unrecognized attribute, still can be namespace definition eg. xmlns:foo=.... */
438 if (match_argument_name(prefix, prefix_len) == YIN_ARG_XMLNS) {
439 /* in this case prefix of namespace is actually name of attribute */
440 parse_xmlns(xml_ctx, data, name, name_len, "module");
441 } else {
442 /* unrecognized or unexpected attribute */
443 LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in module element");
444 return LY_EVALID;
445 }
446 break;
447 }
David Sedlák3b4db242018-10-19 16:11:01 +0200448 }
449
David Sedlák18730132019-03-15 15:51:34 +0100450 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 +0100451
David Sedlákc136b972019-03-08 13:39:06 +0100452 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák18730132019-03-15 15:51:34 +0100453 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 +0100454
David Sedlák736fd0d2019-02-15 16:06:31 +0100455 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100456 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100457/* TODO ADD error log to macro */
458#define CHECK_ORDER(SECTION) \
459 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
460
461 switch (kw) {
462 /* module header */
463 case YANG_NAMESPACE:
464 case YANG_PREFIX:
465 CHECK_ORDER(Y_MOD_MODULE_HEADER);
466 break;
467 case YANG_YANG_VERSION:
468 CHECK_ORDER(Y_MOD_MODULE_HEADER);
469 break;
470 /* linkage */
471 case YANG_INCLUDE:
472 case YANG_IMPORT:
473 CHECK_ORDER(Y_MOD_LINKAGE);
474 break;
475 /* meta */
476 case YANG_ORGANIZATION:
477 case YANG_CONTACT:
478 case YANG_DESCRIPTION:
479 case YANG_REFERENCE:
480 CHECK_ORDER(Y_MOD_META);
481 break;
482
483 /* revision */
484 case YANG_REVISION:
485 CHECK_ORDER(Y_MOD_REVISION);
486 break;
487 /* body */
488 case YANG_ANYDATA:
489 case YANG_ANYXML:
490 case YANG_AUGMENT:
491 case YANG_CHOICE:
492 case YANG_CONTAINER:
493 case YANG_DEVIATION:
494 case YANG_EXTENSION:
495 case YANG_FEATURE:
496 case YANG_GROUPING:
497 case YANG_IDENTITY:
498 case YANG_LEAF:
499 case YANG_LEAF_LIST:
500 case YANG_LIST:
501 case YANG_NOTIFICATION:
502 case YANG_RPC:
503 case YANG_TYPEDEF:
504 case YANG_USES:
505 case YANG_CUSTOM:
506 mod_stmt = Y_MOD_BODY;
507 break;
508 default:
509 /* error will be handled in the next switch */
510 break;
511 }
512#undef CHECK_ORDER
513
David Sedlák4a4c0722018-11-26 17:03:10 +0100514 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlákc136b972019-03-08 13:39:06 +0100515 LY_CHECK_RET(ret != LY_SUCCESS, LY_EMEM);
David Sedlák4a4c0722018-11-26 17:03:10 +0100516
David Sedlákc136b972019-03-08 13:39:06 +0100517 if (name) {
David Sedlák18730132019-03-15 15:51:34 +0100518 kw = match_keyword(name, name_len, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100519 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100520
David Sedlákc136b972019-03-08 13:39:06 +0100521 /* module header */
522 case YANG_NAMESPACE:
523 LY_CHECK_RET(parse_namespace(xml_ctx, data, mod));
524 break;
525 case YANG_PREFIX:
526 LY_CHECK_RET(parse_prefix(xml_ctx, data, mod));
David Sedlákc136b972019-03-08 13:39:06 +0100527 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100528
David Sedlákc136b972019-03-08 13:39:06 +0100529 /* linkage */
530 case YANG_IMPORT:
David Sedlákcd0c9512019-03-29 13:23:06 +0100531 yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, mod);
David Sedlákc136b972019-03-08 13:39:06 +0100532 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100533
David Sedlákc136b972019-03-08 13:39:06 +0100534 /* meta */
535 case YANG_ORGANIZATION:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100536 LY_CHECK_RET(parse_text_element(xml_ctx, "organization", data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100537 break;
538 case YANG_CONTACT:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100539 LY_CHECK_RET(parse_text_element(xml_ctx, "contact", data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100540 break;
541 case YANG_DESCRIPTION:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100542 LY_CHECK_RET(parse_text_element(xml_ctx, "description", data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100543 break;
544 case YANG_REFERENCE:
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100545 LY_CHECK_RET(parse_text_element(xml_ctx, "reference", data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100546 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100547
David Sedlákc136b972019-03-08 13:39:06 +0100548 default:
David Sedlák18730132019-03-15 15:51:34 +0100549 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100550 break;
551 }
David Sedlák4a4c0722018-11-26 17:03:10 +0100552 }
553 }
554
David Sedlák18730132019-03-15 15:51:34 +0100555 lyxml_ns_rm(xml_ctx, "module");
David Sedlákf3b24f62018-11-02 10:40:47 +0100556 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200557}
558
David Sedlákd5318ee2019-02-15 10:14:50 +0100559/**
560 * @brief Parse yin submodule.
561 *
562 * @param[in] ctx Context of YANG schemas.
563 * @param[in] data Data to read from.
564 * @param[out] submod Module to write to.
565 *
566 * @return LY_ERR values.
567 */
568LY_ERR
569yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod)
570{
571 LY_ERR ret = LY_SUCCESS;
572 enum yang_keyword kw = YANG_NONE;
573 struct lyxml_context xml_ctx;
574 struct lysp_submodule *mod_p = NULL;
575 const char *prefix, *name;
576 size_t prefix_len, name_len;
David Sedlák3017da42019-02-15 09:48:04 +0100577
David Sedlákd5318ee2019-02-15 10:14:50 +0100578 /* initialize xml context */
579 memset(&xml_ctx, 0, sizeof xml_ctx);
580 xml_ctx.ctx = ctx;
581 xml_ctx.line = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100582
David Sedlákd5318ee2019-02-15 10:14:50 +0100583 /* check submodule */
584 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
585 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100586 kw = match_keyword(name, name_len, prefix_len);
David Sedlákd5318ee2019-02-15 10:14:50 +0100587 if (kw == YANG_MODULE) {
588 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
589 ret = LY_EINVAL;
590 goto cleanup;
591 } else if (kw != YANG_SUBMODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100592 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
593 ly_stmt2str(kw));
David Sedlákd5318ee2019-02-15 10:14:50 +0100594 ret = LY_EVALID;
595 goto cleanup;
596 }
David Sedlák3017da42019-02-15 09:48:04 +0100597
David Sedlákd5318ee2019-02-15 10:14:50 +0100598 /* allocate module */
599 mod_p = calloc(1, sizeof *mod_p);
600 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
601 mod_p->parsing = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100602
David Sedlákd5318ee2019-02-15 10:14:50 +0100603 /* parser submodule substatements */
604 //ret = parse_submod(&xml_ctx, &data, mod_p);
605 LY_CHECK_GOTO(ret, cleanup);
David Sedlák3017da42019-02-15 09:48:04 +0100606
David Sedlákd5318ee2019-02-15 10:14:50 +0100607 mod_p->parsing = 0;
608 *submod = mod_p;
David Sedlák3017da42019-02-15 09:48:04 +0100609
David Sedlákd5318ee2019-02-15 10:14:50 +0100610cleanup:
611 if (ret) {
612 lysp_submodule_free(ctx, mod_p);
613 }
David Sedlák3017da42019-02-15 09:48:04 +0100614
David Sedlákd5318ee2019-02-15 10:14:50 +0100615 lyxml_context_clear(&xml_ctx);
616 return ret;
617}
David Sedlák3017da42019-02-15 09:48:04 +0100618
David Sedlákd5318ee2019-02-15 10:14:50 +0100619/**
620 * @brief Parse yin module.
621 *
622 * @param[in] ctx Context of YANG schemas.
623 * @param[in] data Data to read from.
624 * @param[out] mod Module to write to.
625 *
626 * @return LY_ERR values.
627 */
David Sedlák3b4db242018-10-19 16:11:01 +0200628LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100629yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200630{
David Sedláke4889912018-11-02 09:52:40 +0100631 LY_ERR ret = LY_SUCCESS;
632 enum yang_keyword kw = YANG_NONE;
David Sedlák3b4db242018-10-19 16:11:01 +0200633 struct lyxml_context xml_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100634 struct lysp_module *mod_p = NULL;
635 const char *prefix, *name;
636 size_t prefix_len, name_len;
David Sedlák3b4db242018-10-19 16:11:01 +0200637
David Sedlák3017da42019-02-15 09:48:04 +0100638 /* initialize xml context */
David Sedláke4889912018-11-02 09:52:40 +0100639 memset(&xml_ctx, 0, sizeof xml_ctx);
640 xml_ctx.ctx = ctx;
641 xml_ctx.line = 1;
642
David Sedlák3017da42019-02-15 09:48:04 +0100643 /* check submodule */
David Sedláke4889912018-11-02 09:52:40 +0100644 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák3017da42019-02-15 09:48:04 +0100645 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100646 kw = match_keyword(name, name_len, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100647 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100648 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
649 ret = LY_EINVAL;
650 goto cleanup;
651 } else if (kw != YANG_MODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100652 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
653 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100654 ret = LY_EVALID;
655 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100656 }
657
David Sedlák3017da42019-02-15 09:48:04 +0100658 /* allocate module */
659 mod_p = calloc(1, sizeof *mod_p);
660 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
661 mod_p->mod = mod;
662 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100663
David Sedlák3017da42019-02-15 09:48:04 +0100664 /* parser module substatements */
665 ret = parse_mod(&xml_ctx, &data, &mod_p);
666 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100667
David Sedlák3017da42019-02-15 09:48:04 +0100668 mod_p->parsing = 0;
669 mod->parsed = mod_p;
670
671cleanup:
672 if (ret) {
673 lysp_module_free(mod_p);
674 }
675
David Sedlák2e411422018-12-17 02:35:39 +0100676 lyxml_context_clear(&xml_ctx);
677 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200678}