blob: 875b5a7fb4133ac35231e411800979f3dbb3c6d7 [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á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ák872c7b42018-10-26 13:15:20 +020029enum YIN_ARGUMENT
David Sedláke4889912018-11-02 09:52:40 +010030match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020031{
David Sedláka7406952019-04-05 10:33:07 +020032 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020033 size_t already_read = 0;
David Sedláka7406952019-04-05 10:33:07 +020034 LY_CHECK_RET(len == 0, YIN_ARG_UNKNOWN);
David Sedlák3b4db242018-10-19 16:11:01 +020035
David Sedlák94de2aa2019-02-15 12:42:11 +010036#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
37#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010038#define IF_ARG_PREFIX_END }
39
David Sedlák1c8b2702019-02-22 11:03:02 +010040 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +010041 case 'x':
42 already_read += 1;
43 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
44 break;
David Sedlák94de2aa2019-02-15 12:42:11 +010045 case 'c':
46 already_read += 1;
47 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +020048 break;
David Sedlák872c7b42018-10-26 13:15:20 +020049
David Sedlák94de2aa2019-02-15 12:42:11 +010050 case 'd':
51 already_read += 1;
52 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +020053 break;
David Sedlák872c7b42018-10-26 13:15:20 +020054
David Sedlák94de2aa2019-02-15 12:42:11 +010055 case 'm':
56 already_read += 1;
57 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +020058 break;
59
David Sedlák94de2aa2019-02-15 12:42:11 +010060 case 'n':
61 already_read += 1;
62 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +020063 break;
64
David Sedlák94de2aa2019-02-15 12:42:11 +010065 case 't':
66 already_read += 1;
67 IF_ARG_PREFIX("a", 1)
68 IF_ARG("g", 1, YIN_ARG_TAG)
69 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
70 IF_ARG_PREFIX_END
71 else IF_ARG("ext", 3, YIN_ARG_TEXT)
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 'u':
75 already_read += 1;
76 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +020077 break;
David Sedlák872c7b42018-10-26 13:15:20 +020078
David Sedlák94de2aa2019-02-15 12:42:11 +010079 case 'v':
80 already_read += 1;
81 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +020082 break;
83 }
84
David Sedlákc10e7902018-12-17 02:17:59 +010085 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +020086 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +020087 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020088 }
89
David Sedlák18730132019-03-15 15:51:34 +010090#undef IF_ARG
91#undef IF_ARG_PREFIX
92#undef IF_ARG_PREFIX_END
93
David Sedlák872c7b42018-10-26 13:15:20 +020094 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +020095}
96
David Sedlák18730132019-03-15 15:51:34 +010097/**
David Sedlákecf5eb82019-06-03 14:12:44 +020098 * @brief parse yin argument
David Sedláka7406952019-04-05 10:33:07 +020099 *
100 * @param[in] xml_ctx XML parser context.
101 * @param[in, out] data Data to read from.
102 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special arguments).
103 * @param[out] arg_val Where value of argument should be stored. Can be NULL if arg_type is spedified as YIN_ARG_NONE.
David Sedláka7406952019-04-05 10:33:07 +0200104 */
105static LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200106yin_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 +0200107{
108 LY_ERR ret = LY_SUCCESS;
109 const char *prefix, *name;
110 size_t prefix_len, name_len;
111
112 char *buf = NULL, *out = NULL;
113 size_t buf_len = 0, out_len = 0;
114 int dynamic;
115 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
116
117 while (xml_ctx->status == LYXML_ATTRIBUTE) {
118 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
119 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
120
121 arg = match_argument_name(name, name_len);
122
David Sedlákecf5eb82019-06-03 14:12:44 +0200123 if (arg == arg_type) {
David Sedláka7406952019-04-05 10:33:07 +0200124 LY_CHECK_RET(ret);
125 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
126 LY_CHECK_RET(ret);
127 *arg_val = lydict_insert(xml_ctx->ctx, out, out_len);
128 LY_CHECK_ERR_RET(!(*arg_val), LOGMEM(xml_ctx->ctx), LY_EMEM);
David Sedlákecf5eb82019-06-03 14:12:44 +0200129 } else if (arg_type == YIN_ARG_NONE) {
130 continue;
David Sedláka7406952019-04-05 10:33:07 +0200131 } else {
David Sedlákecf5eb82019-06-03 14:12:44 +0200132 /* unrecognized or unexpected attribute */
David Sedlákda63c082019-06-04 13:52:23 +0200133 /* TODO add name of attribute to error message */
David Sedlákecf5eb82019-06-03 14:12:44 +0200134 if (name) {
David Sedlákda63c082019-06-04 13:52:23 +0200135 LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid attribute");
David Sedláka7406952019-04-05 10:33:07 +0200136 return LY_EVALID;
137 }
138 }
139 }
140
141 return LY_SUCCESS;
142}
143
144/**
David Sedlák0daba9a2019-03-22 14:07:49 +0100145 * @brief Parse content of whole element as text.
146 *
147 * @param[in] xml_ctx Xml context.
148 * @param[in] data Data to read from.
149 * @param[out] value Where content of element should be stored.
150 */
151LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200152parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100153{
154 LY_ERR ret = LY_SUCCESS;
155 char *buf = NULL, *out = NULL;
David Sedlákecf5eb82019-06-03 14:12:44 +0200156 size_t buf_len = 0, out_len = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100157 int dynamic;
David Sedlák0daba9a2019-03-22 14:07:49 +0100158
David Sedlákecf5eb82019-06-03 14:12:44 +0200159 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL);
David Sedláka7406952019-04-05 10:33:07 +0200160 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlák0daba9a2019-03-22 14:07:49 +0100161 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
162
163 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
164 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
165 LY_CHECK_RET(ret);
166 *value = lydict_insert(xml_ctx->ctx, out, out_len);
167 LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM);
168 }
169
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100170 return LY_SUCCESS;
David Sedlák0daba9a2019-03-22 14:07:49 +0100171}
172
David Sedláka5004e62018-12-16 23:54:47 +0100173LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200174parse_namespace(struct lyxml_context *xml_ctx, const char **data, const char **namespace)
David Sedlák4a4c0722018-11-26 17:03:10 +0100175{
176 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100177
David Sedlákecf5eb82019-06-03 14:12:44 +0200178 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, namespace);
David Sedláka7406952019-04-05 10:33:07 +0200179 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlák4a4c0722018-11-26 17:03:10 +0100180
David Sedlákd9d3a312019-06-04 09:47:10 +0200181 return ret;
David Sedlák4a4c0722018-11-26 17:03:10 +0100182}
183
David Sedlákd5318ee2019-02-15 10:14:50 +0100184/**
185 * @brief Parse prefix statement.
186 *
187 * @param[in] xml_ctx Xml context.
188 * @param[in, out] data Data to reda from.
David Sedlák81e04022019-04-05 15:05:46 +0200189 * @param[out] prefix Where to store prefix value.
David Sedlákd5318ee2019-02-15 10:14:50 +0100190 *
191 * @return LY_ERR values.
192 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100193LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200194parse_prefix(struct lyxml_context *xml_ctx, const char **data, const char **prefix)
David Sedlák4a4c0722018-11-26 17:03:10 +0100195{
196 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100197
David Sedlákcd0c9512019-03-29 13:23:06 +0100198 /* parse attributes */
David Sedlákecf5eb82019-06-03 14:12:44 +0200199 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, prefix);
David Sedlákcd0c9512019-03-29 13:23:06 +0100200 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákecf5eb82019-06-03 14:12:44 +0200201
David Sedlákcd0c9512019-03-29 13:23:06 +0100202 return ret;
David Sedlák4a4c0722018-11-26 17:03:10 +0100203}
204
David Sedlák81e04022019-04-05 15:05:46 +0200205static LY_ERR
206yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
207{
208 LY_ERR ret = LY_SUCCESS;
David Sedlákda63c082019-06-04 13:52:23 +0200209 const char *temp_rev;
David Sedlákcd0c9512019-03-29 13:23:06 +0100210
David Sedlák81e04022019-04-05 15:05:46 +0200211 if (rev[0]) {
212 LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
213 return LY_EVALID;
214 }
215
David Sedlákda63c082019-06-04 13:52:23 +0200216 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, &temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200217 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200218 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
219
220 strcpy(rev, temp_rev);
221 lydict_remove(xml_ctx->ctx, temp_rev);
222 /* TODO extension */
David Sedlák81e04022019-04-05 15:05:46 +0200223
David Sedlák81e04022019-04-05 15:05:46 +0200224 return ret;
225}
David Sedlákcd0c9512019-03-29 13:23:06 +0100226
David Sedlákda63c082019-06-04 13:52:23 +0200227LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200228yin_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 +0100229{
230 LY_ERR ret = LY_SUCCESS;
231 enum yang_keyword kw;
232 struct lysp_import *imp;
233 const char *prefix, *name;
234 size_t prefix_len, name_len;
235
David Sedlák81e04022019-04-05 15:05:46 +0200236 /* allocate sized array for imports */
237 LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100238
David Sedláka7406952019-04-05 10:33:07 +0200239 /* parse import attributes */
David Sedlákecf5eb82019-06-03 14:12:44 +0200240 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name);
David Sedlák736fd0d2019-02-15 16:06:31 +0100241 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 +0100242 kw = match_keyword(name, name_len, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100243 switch (kw) {
244 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200245 LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID);
246 parse_prefix(xml_ctx, data, &imp->prefix);
David Sedlákda63c082019-06-04 13:52:23 +0200247 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 +0100248 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100249 case YANG_DESCRIPTION:
David Sedlák81e04022019-04-05 15:05:46 +0200250 LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID);
David Sedlákecf5eb82019-06-03 14:12:44 +0200251 parse_text_element(xml_ctx, data, &imp->dsc);
David Sedlákcd0c9512019-03-29 13:23:06 +0100252 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100253 case YANG_REFERENCE:
David Sedlák81e04022019-04-05 15:05:46 +0200254 LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID);
David Sedlákecf5eb82019-06-03 14:12:44 +0200255 parse_text_element(xml_ctx, data, &imp->ref);
David Sedlákcd0c9512019-03-29 13:23:06 +0100256 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100257 case YANG_REVISION_DATE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200258 yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts);
David Sedlák3a55bc12019-06-03 08:57:29 +0200259 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100260 case YANG_CUSTOM:
261 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200262 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100263 default:
264 /* TODO log error */
265 return LY_EVALID;
266 }
267 }
268
269 /* TODO add log macro and log error */
270 LY_CHECK_RET(!imp->prefix);
271 return ret;
272}
273
David Sedlákd5318ee2019-02-15 10:14:50 +0100274/**
275 * @brief Parse module substatements.
276 *
277 * @param[in] xml_ctx xml context.
278 * @param[in, out] data Data to read from.
279 * @param[out] mod Parsed module structure
280 *
281 * @return LY_ERR values.
282 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100283LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100284parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200285{
David Sedlákf3b24f62018-11-02 10:40:47 +0100286 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100287 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100288 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200289 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100290 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedláka7406952019-04-05 10:33:07 +0200291 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák3b4db242018-10-19 16:11:01 +0200292
David Sedlákf3b24f62018-11-02 10:40:47 +0100293 char *buf = NULL, *out = NULL;
294 size_t buf_len = 0, out_len = 0;
295 int dynamic;
296
David Sedlák18730132019-03-15 15:51:34 +0100297 /* parse module attributes */
298 while (xml_ctx->status == LYXML_ATTRIBUTE) {
299 ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
300 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
301
302 arg = match_argument_name(name, name_len);
303
304 switch (arg) {
David Sedlák18730132019-03-15 15:51:34 +0100305 case YIN_ARG_NAME:
306 /* check for multiple definitions of name */
David Sedlákda63c082019-06-04 13:52:23 +0200307 LY_CHECK_ERR_RET((*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Duplicit definition of module name \"%s\"",
308 (*mod)->mod->name), LY_EEXIST);
David Sedlák18730132019-03-15 15:51:34 +0100309
310 /* read module name */
311 if (xml_ctx->status != LYXML_ATTR_CONTENT) {
312 LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\".");
313 }
314 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
315 LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
316 (*mod)->mod->name = lydict_insert(xml_ctx->ctx, out, out_len);
317 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
318 break;
David Sedlákecf5eb82019-06-03 14:12:44 +0200319
David Sedlák18730132019-03-15 15:51:34 +0100320 default:
David Sedlákecf5eb82019-06-03 14:12:44 +0200321 /* unrecognized attribute */
322 /* unrecognized or unexpected attribute */
323 /* TODO probably still can be from extension */
324 if (arg != YIN_ARG_UNKNOWN || name) {
David Sedlák18730132019-03-15 15:51:34 +0100325 LOGERR(xml_ctx->ctx, LY_EDENIED, "Invalid argument in module element");
326 return LY_EVALID;
327 }
David Sedlákecf5eb82019-06-03 14:12:44 +0200328
David Sedlák18730132019-03-15 15:51:34 +0100329 break;
330 }
David Sedlák3b4db242018-10-19 16:11:01 +0200331 }
332
David Sedlák18730132019-03-15 15:51:34 +0100333 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 +0100334
David Sedlákc136b972019-03-08 13:39:06 +0100335 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák18730132019-03-15 15:51:34 +0100336 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 +0100337
David Sedlák736fd0d2019-02-15 16:06:31 +0100338 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100339 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100340/* TODO ADD error log to macro */
341#define CHECK_ORDER(SECTION) \
342 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
343
344 switch (kw) {
345 /* module header */
346 case YANG_NAMESPACE:
347 case YANG_PREFIX:
348 CHECK_ORDER(Y_MOD_MODULE_HEADER);
349 break;
350 case YANG_YANG_VERSION:
351 CHECK_ORDER(Y_MOD_MODULE_HEADER);
352 break;
353 /* linkage */
354 case YANG_INCLUDE:
355 case YANG_IMPORT:
356 CHECK_ORDER(Y_MOD_LINKAGE);
357 break;
358 /* meta */
359 case YANG_ORGANIZATION:
360 case YANG_CONTACT:
361 case YANG_DESCRIPTION:
362 case YANG_REFERENCE:
363 CHECK_ORDER(Y_MOD_META);
364 break;
365
366 /* revision */
367 case YANG_REVISION:
368 CHECK_ORDER(Y_MOD_REVISION);
369 break;
370 /* body */
371 case YANG_ANYDATA:
372 case YANG_ANYXML:
373 case YANG_AUGMENT:
374 case YANG_CHOICE:
375 case YANG_CONTAINER:
376 case YANG_DEVIATION:
377 case YANG_EXTENSION:
378 case YANG_FEATURE:
379 case YANG_GROUPING:
380 case YANG_IDENTITY:
381 case YANG_LEAF:
382 case YANG_LEAF_LIST:
383 case YANG_LIST:
384 case YANG_NOTIFICATION:
385 case YANG_RPC:
386 case YANG_TYPEDEF:
387 case YANG_USES:
388 case YANG_CUSTOM:
389 mod_stmt = Y_MOD_BODY;
390 break;
391 default:
392 /* error will be handled in the next switch */
393 break;
394 }
395#undef CHECK_ORDER
396
David Sedlák4a4c0722018-11-26 17:03:10 +0100397 ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlákc136b972019-03-08 13:39:06 +0100398 LY_CHECK_RET(ret != LY_SUCCESS, LY_EMEM);
David Sedlák4a4c0722018-11-26 17:03:10 +0100399
David Sedlákc136b972019-03-08 13:39:06 +0100400 if (name) {
David Sedlák18730132019-03-15 15:51:34 +0100401 kw = match_keyword(name, name_len, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100402 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100403
David Sedlákc136b972019-03-08 13:39:06 +0100404 /* module header */
405 case YANG_NAMESPACE:
David Sedlák81e04022019-04-05 15:05:46 +0200406 LY_CHECK_RET(parse_namespace(xml_ctx, data, &(*mod)->mod->ns));
David Sedlákc136b972019-03-08 13:39:06 +0100407 break;
408 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200409 LY_CHECK_RET(parse_prefix(xml_ctx, data, &(*mod)->mod->prefix));
David Sedlákc136b972019-03-08 13:39:06 +0100410 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100411
David Sedlákc136b972019-03-08 13:39:06 +0100412 /* linkage */
413 case YANG_IMPORT:
David Sedlák81e04022019-04-05 15:05:46 +0200414 yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports);
David Sedlákc136b972019-03-08 13:39:06 +0100415 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100416
David Sedlákc136b972019-03-08 13:39:06 +0100417 /* meta */
418 case YANG_ORGANIZATION:
David Sedlákecf5eb82019-06-03 14:12:44 +0200419 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100420 break;
421 case YANG_CONTACT:
David Sedlákecf5eb82019-06-03 14:12:44 +0200422 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100423 break;
424 case YANG_DESCRIPTION:
David Sedlákecf5eb82019-06-03 14:12:44 +0200425 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100426 break;
427 case YANG_REFERENCE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200428 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100429 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100430
David Sedlákc136b972019-03-08 13:39:06 +0100431 default:
David Sedlák18730132019-03-15 15:51:34 +0100432 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100433 break;
434 }
David Sedlák4a4c0722018-11-26 17:03:10 +0100435 }
436 }
437
David Sedlákf3b24f62018-11-02 10:40:47 +0100438 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200439}
440
David Sedlákd5318ee2019-02-15 10:14:50 +0100441/**
442 * @brief Parse yin submodule.
443 *
444 * @param[in] ctx Context of YANG schemas.
445 * @param[in] data Data to read from.
446 * @param[out] submod Module to write to.
447 *
448 * @return LY_ERR values.
449 */
450LY_ERR
451yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod)
452{
453 LY_ERR ret = LY_SUCCESS;
454 enum yang_keyword kw = YANG_NONE;
455 struct lyxml_context xml_ctx;
456 struct lysp_submodule *mod_p = NULL;
457 const char *prefix, *name;
458 size_t prefix_len, name_len;
David Sedlák3017da42019-02-15 09:48:04 +0100459
David Sedlákd5318ee2019-02-15 10:14:50 +0100460 /* initialize xml context */
461 memset(&xml_ctx, 0, sizeof xml_ctx);
462 xml_ctx.ctx = ctx;
463 xml_ctx.line = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100464
David Sedlákd5318ee2019-02-15 10:14:50 +0100465 /* check submodule */
466 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
467 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100468 kw = match_keyword(name, name_len, prefix_len);
David Sedlákd5318ee2019-02-15 10:14:50 +0100469 if (kw == YANG_MODULE) {
470 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
471 ret = LY_EINVAL;
472 goto cleanup;
473 } else if (kw != YANG_SUBMODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100474 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
475 ly_stmt2str(kw));
David Sedlákd5318ee2019-02-15 10:14:50 +0100476 ret = LY_EVALID;
477 goto cleanup;
478 }
David Sedlák3017da42019-02-15 09:48:04 +0100479
David Sedlákd5318ee2019-02-15 10:14:50 +0100480 /* allocate module */
481 mod_p = calloc(1, sizeof *mod_p);
482 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
483 mod_p->parsing = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100484
David Sedlákd5318ee2019-02-15 10:14:50 +0100485 /* parser submodule substatements */
486 //ret = parse_submod(&xml_ctx, &data, mod_p);
487 LY_CHECK_GOTO(ret, cleanup);
David Sedlák3017da42019-02-15 09:48:04 +0100488
David Sedlákd5318ee2019-02-15 10:14:50 +0100489 mod_p->parsing = 0;
490 *submod = mod_p;
David Sedlák3017da42019-02-15 09:48:04 +0100491
David Sedlákd5318ee2019-02-15 10:14:50 +0100492cleanup:
493 if (ret) {
494 lysp_submodule_free(ctx, mod_p);
495 }
David Sedlák3017da42019-02-15 09:48:04 +0100496
David Sedlákd5318ee2019-02-15 10:14:50 +0100497 lyxml_context_clear(&xml_ctx);
498 return ret;
499}
David Sedlák3017da42019-02-15 09:48:04 +0100500
David Sedlák3b4db242018-10-19 16:11:01 +0200501LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100502yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200503{
David Sedláke4889912018-11-02 09:52:40 +0100504 LY_ERR ret = LY_SUCCESS;
505 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200506 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200507 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100508 struct lysp_module *mod_p = NULL;
509 const char *prefix, *name;
510 size_t prefix_len, name_len;
David Sedlák3b4db242018-10-19 16:11:01 +0200511
David Sedlák3017da42019-02-15 09:48:04 +0100512 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200513 memset(&parser_ctx, 0, sizeof parser_ctx);
514 xml_ctx->ctx = ctx;
515 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100516
David Sedlák3017da42019-02-15 09:48:04 +0100517 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200518 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák3017da42019-02-15 09:48:04 +0100519 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100520 kw = match_keyword(name, name_len, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100521 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100522 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
523 ret = LY_EINVAL;
524 goto cleanup;
525 } else if (kw != YANG_MODULE) {
David Sedlákaadab9c2019-04-05 15:01:27 +0200526 LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák0a875b42019-03-07 22:24:05 +0100527 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100528 ret = LY_EVALID;
529 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100530 }
531
David Sedlák3017da42019-02-15 09:48:04 +0100532 /* allocate module */
533 mod_p = calloc(1, sizeof *mod_p);
534 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
535 mod_p->mod = mod;
536 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100537
David Sedlák3017da42019-02-15 09:48:04 +0100538 /* parser module substatements */
David Sedlákaadab9c2019-04-05 15:01:27 +0200539 ret = parse_mod(xml_ctx, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100540 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100541
David Sedlák3017da42019-02-15 09:48:04 +0100542 mod_p->parsing = 0;
543 mod->parsed = mod_p;
544
545cleanup:
546 if (ret) {
547 lysp_module_free(mod_p);
548 }
549
David Sedlákaadab9c2019-04-05 15:01:27 +0200550 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100551 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200552}