blob: ce6dcbdeeaa54588fa54fa065e085eead3ae3de5 [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ákf6251182019-06-06 10:22:13 +020029const char *const yin_attr_list[] = {
30 [YIN_ARG_NAME] = "name",
31 [YIN_ARG_TARGET_NODE] = "target-node",
32 [YIN_ARG_MODULE] = "module",
33 [YIN_ARG_VALUE] = "value",
34 [YIN_ARG_TEXT] = "text",
35 [YIN_ARG_CONDITION] = "condition",
36 [YIN_ARG_URI] = "uri",
37 [YIN_ARG_DATE] = "date",
38 [YIN_ARG_TAG] = "tag",
39 [YIN_ARG_XMLNS] = "xmlns",
40};
41
David Sedlák872c7b42018-10-26 13:15:20 +020042enum YIN_ARGUMENT
David Sedláke4889912018-11-02 09:52:40 +010043match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020044{
David Sedláka7406952019-04-05 10:33:07 +020045 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020046 size_t already_read = 0;
David Sedláka7406952019-04-05 10:33:07 +020047 LY_CHECK_RET(len == 0, YIN_ARG_UNKNOWN);
David Sedlák3b4db242018-10-19 16:11:01 +020048
David Sedlák94de2aa2019-02-15 12:42:11 +010049#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
50#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010051#define IF_ARG_PREFIX_END }
52
David Sedlák1c8b2702019-02-22 11:03:02 +010053 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +010054 case 'x':
55 already_read += 1;
56 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
57 break;
David Sedlák94de2aa2019-02-15 12:42:11 +010058 case 'c':
59 already_read += 1;
60 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +020061 break;
David Sedlák872c7b42018-10-26 13:15:20 +020062
David Sedlák94de2aa2019-02-15 12:42:11 +010063 case 'd':
64 already_read += 1;
65 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +020066 break;
David Sedlák872c7b42018-10-26 13:15:20 +020067
David Sedlák94de2aa2019-02-15 12:42:11 +010068 case 'm':
69 already_read += 1;
70 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +020071 break;
72
David Sedlák94de2aa2019-02-15 12:42:11 +010073 case 'n':
74 already_read += 1;
75 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +020076 break;
77
David Sedlák94de2aa2019-02-15 12:42:11 +010078 case 't':
79 already_read += 1;
80 IF_ARG_PREFIX("a", 1)
81 IF_ARG("g", 1, YIN_ARG_TAG)
82 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
83 IF_ARG_PREFIX_END
84 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +020085 break;
David Sedlák872c7b42018-10-26 13:15:20 +020086
David Sedlák94de2aa2019-02-15 12:42:11 +010087 case 'u':
88 already_read += 1;
89 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +020090 break;
David Sedlák872c7b42018-10-26 13:15:20 +020091
David Sedlák94de2aa2019-02-15 12:42:11 +010092 case 'v':
93 already_read += 1;
94 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +020095 break;
96 }
97
David Sedlákc10e7902018-12-17 02:17:59 +010098 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +020099 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200100 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200101 }
102
David Sedlák18730132019-03-15 15:51:34 +0100103#undef IF_ARG
104#undef IF_ARG_PREFIX
105#undef IF_ARG_PREFIX_END
106
David Sedlák872c7b42018-10-26 13:15:20 +0200107 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200108}
109
David Sedlák18730132019-03-15 15:51:34 +0100110/**
David Sedlákf6251182019-06-06 10:22:13 +0200111 * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found
David Sedláka7406952019-04-05 10:33:07 +0200112 *
113 * @param[in] xml_ctx XML parser context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200114 * @param[in,out] data Data to read from.
David Sedláka7406952019-04-05 10:33:07 +0200115 * @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 +0200116 * @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 +0200117 *
118 * @return LY_ERR values.
David Sedláka7406952019-04-05 10:33:07 +0200119 */
120static LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200121yin_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 +0200122{
123 LY_ERR ret = LY_SUCCESS;
124 const char *prefix, *name;
125 size_t prefix_len, name_len;
126
127 char *buf = NULL, *out = NULL;
128 size_t buf_len = 0, out_len = 0;
129 int dynamic;
130 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
131
132 while (xml_ctx->status == LYXML_ATTRIBUTE) {
133 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
134 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
135
136 arg = match_argument_name(name, name_len);
137
David Sedlákecf5eb82019-06-03 14:12:44 +0200138 if (arg == arg_type) {
David Sedláka7406952019-04-05 10:33:07 +0200139 LY_CHECK_RET(ret);
140 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
141 LY_CHECK_RET(ret);
142 *arg_val = lydict_insert(xml_ctx->ctx, out, out_len);
143 LY_CHECK_ERR_RET(!(*arg_val), LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlákecf5eb82019-06-03 14:12:44 +0200144 } else if (arg_type == YIN_ARG_NONE) {
145 continue;
David Sedláka7406952019-04-05 10:33:07 +0200146 } else {
David Sedlákecf5eb82019-06-03 14:12:44 +0200147 /* unrecognized or unexpected attribute */
148 if (name) {
David Sedlákf6251182019-06-06 10:22:13 +0200149 if (arg_type != YIN_ARG_NONE) {
150 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Invalid attribute \"%.*s\", expected \"%s\".", name, name_len, yin_attr2str(arg_type));
151 } else {
152 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", name_len, name);
153 }
David Sedláka7406952019-04-05 10:33:07 +0200154 return LY_EVALID;
155 }
156 }
157 }
158
159 return LY_SUCCESS;
160}
161
162/**
David Sedlák0daba9a2019-03-22 14:07:49 +0100163 * @brief Parse content of whole element as text.
164 *
165 * @param[in] xml_ctx Xml context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200166 * @param[in,out] data Data to read from.
David Sedlák0daba9a2019-03-22 14:07:49 +0100167 * @param[out] value Where content of element should be stored.
David Sedlákb666bcc2019-06-05 15:00:05 +0200168 *
169 * @return LY_ERR values.
David Sedlák0daba9a2019-03-22 14:07:49 +0100170 */
171LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200172parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100173{
174 LY_ERR ret = LY_SUCCESS;
175 char *buf = NULL, *out = NULL;
David Sedlákecf5eb82019-06-03 14:12:44 +0200176 size_t buf_len = 0, out_len = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100177 int dynamic;
David Sedlák0daba9a2019-03-22 14:07:49 +0100178
David Sedlákecf5eb82019-06-03 14:12:44 +0200179 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL);
David Sedlák68826732019-06-05 10:50:58 +0200180 LY_CHECK_RET(ret);
David Sedlák0daba9a2019-03-22 14:07:49 +0100181 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
182
183 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
184 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
185 LY_CHECK_RET(ret);
186 *value = lydict_insert(xml_ctx->ctx, out, out_len);
187 LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM);
188 }
189
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100190 return LY_SUCCESS;
David Sedlák0daba9a2019-03-22 14:07:49 +0100191}
192
David Sedlákb666bcc2019-06-05 15:00:05 +0200193/**
194 * @brief Parse revision date
195 *
196 * @param[in] xml_ctx Xml context.
197 * @param[in,out] data Data to read from.
198 * @param[in,out] rev Array to store the parsed value in.
199 * @param[in,out] exts Extension instances to add to.
200 *
201 * @return LY_ERR values
202 */
David Sedlák81e04022019-04-05 15:05:46 +0200203static LY_ERR
204yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
205{
206 LY_ERR ret = LY_SUCCESS;
David Sedlákda63c082019-06-04 13:52:23 +0200207 const char *temp_rev;
David Sedlákcd0c9512019-03-29 13:23:06 +0100208
David Sedlák81e04022019-04-05 15:05:46 +0200209 if (rev[0]) {
210 LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
211 return LY_EVALID;
212 }
213
David Sedlák68826732019-06-05 10:50:58 +0200214 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200215 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200216 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
217
218 strcpy(rev, temp_rev);
219 lydict_remove(xml_ctx->ctx, temp_rev);
220 /* TODO extension */
David Sedlák81e04022019-04-05 15:05:46 +0200221
David Sedlák81e04022019-04-05 15:05:46 +0200222 return ret;
223}
David Sedlákcd0c9512019-03-29 13:23:06 +0100224
David Sedlákda63c082019-06-04 13:52:23 +0200225LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200226yin_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 +0100227{
228 LY_ERR ret = LY_SUCCESS;
229 enum yang_keyword kw;
230 struct lysp_import *imp;
231 const char *prefix, *name;
232 size_t prefix_len, name_len;
233
David Sedlák81e04022019-04-05 15:05:46 +0200234 /* allocate sized array for imports */
235 LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100236
David Sedláka7406952019-04-05 10:33:07 +0200237 /* parse import attributes */
David Sedlák68826732019-06-05 10:50:58 +0200238 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name));
239
David Sedlák736fd0d2019-02-15 16:06:31 +0100240 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 +0100241 kw = match_keyword(name, name_len, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100242 switch (kw) {
243 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200244 LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID);
David Sedlák68826732019-06-05 10:50:58 +0200245 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &imp->prefix));
David Sedlákda63c082019-06-04 13:52:23 +0200246 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 +0100247 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100248 case YANG_DESCRIPTION:
David Sedlák81e04022019-04-05 15:05:46 +0200249 LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID);
David Sedlákecf5eb82019-06-03 14:12:44 +0200250 parse_text_element(xml_ctx, data, &imp->dsc);
David Sedlákcd0c9512019-03-29 13:23:06 +0100251 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100252 case YANG_REFERENCE:
David Sedlák81e04022019-04-05 15:05:46 +0200253 LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID);
David Sedlákecf5eb82019-06-03 14:12:44 +0200254 parse_text_element(xml_ctx, data, &imp->ref);
David Sedlákcd0c9512019-03-29 13:23:06 +0100255 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100256 case YANG_REVISION_DATE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200257 yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts);
David Sedlák3a55bc12019-06-03 08:57:29 +0200258 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100259 case YANG_CUSTOM:
260 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200261 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100262 default:
263 /* TODO log error */
264 return LY_EVALID;
265 }
266 }
267
268 /* TODO add log macro and log error */
David Sedlákf6251182019-06-06 10:22:13 +0200269 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_MISSTMT, "prefix", "import"), LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100270 return ret;
271}
272
David Sedlákd5318ee2019-02-15 10:14:50 +0100273/**
274 * @brief Parse module substatements.
275 *
276 * @param[in] xml_ctx xml context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200277 * @param[in,out] data Data to read from.
David Sedlákd5318ee2019-02-15 10:14:50 +0100278 * @param[out] mod Parsed module structure
279 *
280 * @return LY_ERR values.
281 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100282LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100283parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200284{
David Sedlákf3b24f62018-11-02 10:40:47 +0100285 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100286 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100287 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200288 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100289 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedláka7406952019-04-05 10:33:07 +0200290 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák3b4db242018-10-19 16:11:01 +0200291
David Sedlákf3b24f62018-11-02 10:40:47 +0100292 char *buf = NULL, *out = NULL;
293 size_t buf_len = 0, out_len = 0;
294 int dynamic;
295
David Sedlák18730132019-03-15 15:51:34 +0100296 /* parse module attributes */
297 while (xml_ctx->status == LYXML_ATTRIBUTE) {
298 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
299 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
300
301 arg = match_argument_name(name, name_len);
302
303 switch (arg) {
David Sedlák18730132019-03-15 15:51:34 +0100304 case YIN_ARG_NAME:
305 /* check for multiple definitions of name */
David Sedlákda63c082019-06-04 13:52:23 +0200306 LY_CHECK_ERR_RET((*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Duplicit definition of module name \"%s\"",
307 (*mod)->mod->name), LY_EEXIST);
David Sedlák18730132019-03-15 15:51:34 +0100308
309 /* read module name */
310 if (xml_ctx->status != LYXML_ATTR_CONTENT) {
311 LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\".");
312 }
313 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
314 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
315 (*mod)->mod->name = lydict_insert(xml_ctx->ctx, out, out_len);
316 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
317 break;
David Sedlákecf5eb82019-06-03 14:12:44 +0200318
David Sedlák18730132019-03-15 15:51:34 +0100319 default:
David Sedlákecf5eb82019-06-03 14:12:44 +0200320 /* unrecognized attribute */
321 /* unrecognized or unexpected attribute */
322 /* TODO probably still can be from extension */
323 if (arg != YIN_ARG_UNKNOWN || name) {
David Sedlák18730132019-03-15 15:51:34 +0100324 LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in module element");
325 return LY_EVALID;
326 }
David Sedlákecf5eb82019-06-03 14:12:44 +0200327
David Sedlák18730132019-03-15 15:51:34 +0100328 break;
329 }
David Sedlák3b4db242018-10-19 16:11:01 +0200330 }
331
David Sedlák392af4f2019-06-04 16:02:42 +0200332 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module", (*mod)->mod->name), LY_EVALID);
David Sedlákf3b24f62018-11-02 10:40:47 +0100333
David Sedlákc136b972019-03-08 13:39:06 +0100334 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák18730132019-03-15 15:51:34 +0100335 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 +0100336
David Sedlák736fd0d2019-02-15 16:06:31 +0100337 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100338 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100339/* TODO ADD error log to macro */
340#define CHECK_ORDER(SECTION) \
341 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
342
343 switch (kw) {
344 /* module header */
345 case YANG_NAMESPACE:
346 case YANG_PREFIX:
347 CHECK_ORDER(Y_MOD_MODULE_HEADER);
348 break;
349 case YANG_YANG_VERSION:
350 CHECK_ORDER(Y_MOD_MODULE_HEADER);
351 break;
352 /* linkage */
353 case YANG_INCLUDE:
354 case YANG_IMPORT:
355 CHECK_ORDER(Y_MOD_LINKAGE);
356 break;
357 /* meta */
358 case YANG_ORGANIZATION:
359 case YANG_CONTACT:
360 case YANG_DESCRIPTION:
361 case YANG_REFERENCE:
362 CHECK_ORDER(Y_MOD_META);
363 break;
364
365 /* revision */
366 case YANG_REVISION:
367 CHECK_ORDER(Y_MOD_REVISION);
368 break;
369 /* body */
370 case YANG_ANYDATA:
371 case YANG_ANYXML:
372 case YANG_AUGMENT:
373 case YANG_CHOICE:
374 case YANG_CONTAINER:
375 case YANG_DEVIATION:
376 case YANG_EXTENSION:
377 case YANG_FEATURE:
378 case YANG_GROUPING:
379 case YANG_IDENTITY:
380 case YANG_LEAF:
381 case YANG_LEAF_LIST:
382 case YANG_LIST:
383 case YANG_NOTIFICATION:
384 case YANG_RPC:
385 case YANG_TYPEDEF:
386 case YANG_USES:
387 case YANG_CUSTOM:
388 mod_stmt = Y_MOD_BODY;
389 break;
390 default:
391 /* error will be handled in the next switch */
392 break;
393 }
394#undef CHECK_ORDER
395
David Sedlák68826732019-06-05 10:50:58 +0200396 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák4a4c0722018-11-26 17:03:10 +0100397
David Sedlákc136b972019-03-08 13:39:06 +0100398 if (name) {
David Sedlák18730132019-03-15 15:51:34 +0100399 kw = match_keyword(name, name_len, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100400 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100401
David Sedlákc136b972019-03-08 13:39:06 +0100402 /* module header */
403 case YANG_NAMESPACE:
David Sedlák68826732019-06-05 10:50:58 +0200404 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, &(*mod)->mod->ns));
David Sedlákc136b972019-03-08 13:39:06 +0100405 break;
406 case YANG_PREFIX:
David Sedlák68826732019-06-05 10:50:58 +0200407 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &(*mod)->mod->prefix));
David Sedlákc136b972019-03-08 13:39:06 +0100408 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100409
David Sedlákc136b972019-03-08 13:39:06 +0100410 /* linkage */
411 case YANG_IMPORT:
David Sedlák81e04022019-04-05 15:05:46 +0200412 yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports);
David Sedlákc136b972019-03-08 13:39:06 +0100413 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100414
David Sedlákc136b972019-03-08 13:39:06 +0100415 /* meta */
416 case YANG_ORGANIZATION:
David Sedlákecf5eb82019-06-03 14:12:44 +0200417 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100418 break;
419 case YANG_CONTACT:
David Sedlákecf5eb82019-06-03 14:12:44 +0200420 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100421 break;
422 case YANG_DESCRIPTION:
David Sedlákecf5eb82019-06-03 14:12:44 +0200423 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100424 break;
425 case YANG_REFERENCE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200426 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100427 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100428
David Sedlákc136b972019-03-08 13:39:06 +0100429 default:
David Sedlák18730132019-03-15 15:51:34 +0100430 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100431 break;
432 }
David Sedlák4a4c0722018-11-26 17:03:10 +0100433 }
434 }
435
David Sedlák68826732019-06-05 10:50:58 +0200436 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200437}
438
David Sedlákd5318ee2019-02-15 10:14:50 +0100439/**
440 * @brief Parse yin submodule.
441 *
442 * @param[in] ctx Context of YANG schemas.
David Sedlákb666bcc2019-06-05 15:00:05 +0200443 * @param[in,out] data Data to read from.
444 * @param[out] submod Parsed submodule structure.
David Sedlákd5318ee2019-02-15 10:14:50 +0100445 *
446 * @return LY_ERR values.
447 */
448LY_ERR
449yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod)
450{
451 LY_ERR ret = LY_SUCCESS;
452 enum yang_keyword kw = YANG_NONE;
453 struct lyxml_context xml_ctx;
454 struct lysp_submodule *mod_p = NULL;
455 const char *prefix, *name;
456 size_t prefix_len, name_len;
David Sedlák3017da42019-02-15 09:48:04 +0100457
David Sedlákd5318ee2019-02-15 10:14:50 +0100458 /* initialize xml context */
459 memset(&xml_ctx, 0, sizeof xml_ctx);
460 xml_ctx.ctx = ctx;
461 xml_ctx.line = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100462
David Sedlákd5318ee2019-02-15 10:14:50 +0100463 /* check submodule */
464 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
465 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100466 kw = match_keyword(name, name_len, prefix_len);
David Sedlákd5318ee2019-02-15 10:14:50 +0100467 if (kw == YANG_MODULE) {
468 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
469 ret = LY_EINVAL;
470 goto cleanup;
471 } else if (kw != YANG_SUBMODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100472 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
473 ly_stmt2str(kw));
David Sedlákd5318ee2019-02-15 10:14:50 +0100474 ret = LY_EVALID;
475 goto cleanup;
476 }
David Sedlák3017da42019-02-15 09:48:04 +0100477
David Sedlákd5318ee2019-02-15 10:14:50 +0100478 /* allocate module */
479 mod_p = calloc(1, sizeof *mod_p);
480 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
481 mod_p->parsing = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100482
David Sedlákd5318ee2019-02-15 10:14:50 +0100483 /* parser submodule substatements */
David Sedlákb666bcc2019-06-05 15:00:05 +0200484 /* ret = parse_submod(&xml_ctx, &data, mod_p); */
David Sedlákd5318ee2019-02-15 10:14:50 +0100485 LY_CHECK_GOTO(ret, cleanup);
David Sedlák3017da42019-02-15 09:48:04 +0100486
David Sedlákd5318ee2019-02-15 10:14:50 +0100487 mod_p->parsing = 0;
488 *submod = mod_p;
David Sedlák3017da42019-02-15 09:48:04 +0100489
David Sedlákd5318ee2019-02-15 10:14:50 +0100490cleanup:
491 if (ret) {
492 lysp_submodule_free(ctx, mod_p);
493 }
David Sedlák3017da42019-02-15 09:48:04 +0100494
David Sedlákd5318ee2019-02-15 10:14:50 +0100495 lyxml_context_clear(&xml_ctx);
496 return ret;
497}
David Sedlák3017da42019-02-15 09:48:04 +0100498
David Sedlák3b4db242018-10-19 16:11:01 +0200499LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100500yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200501{
David Sedláke4889912018-11-02 09:52:40 +0100502 LY_ERR ret = LY_SUCCESS;
503 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200504 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200505 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100506 struct lysp_module *mod_p = NULL;
507 const char *prefix, *name;
508 size_t prefix_len, name_len;
David Sedlák3b4db242018-10-19 16:11:01 +0200509
David Sedlák3017da42019-02-15 09:48:04 +0100510 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200511 memset(&parser_ctx, 0, sizeof parser_ctx);
512 xml_ctx->ctx = ctx;
513 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100514
David Sedlák3017da42019-02-15 09:48:04 +0100515 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200516 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák3017da42019-02-15 09:48:04 +0100517 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100518 kw = match_keyword(name, name_len, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100519 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100520 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
521 ret = LY_EINVAL;
522 goto cleanup;
523 } else if (kw != YANG_MODULE) {
David Sedlákaadab9c2019-04-05 15:01:27 +0200524 LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200525 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100526 ret = LY_EVALID;
527 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100528 }
529
David Sedlák3017da42019-02-15 09:48:04 +0100530 /* allocate module */
531 mod_p = calloc(1, sizeof *mod_p);
532 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
533 mod_p->mod = mod;
534 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100535
David Sedlák3017da42019-02-15 09:48:04 +0100536 /* parser module substatements */
David Sedlákaadab9c2019-04-05 15:01:27 +0200537 ret = parse_mod(xml_ctx, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100538 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100539
David Sedlák3017da42019-02-15 09:48:04 +0100540 mod_p->parsing = 0;
541 mod->parsed = mod_p;
542
543cleanup:
544 if (ret) {
545 lysp_module_free(mod_p);
546 }
547
David Sedlákaadab9c2019-04-05 15:01:27 +0200548 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100549 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200550}