blob: 7ae164972ef8a5f2d2cab758cb63ad87fbda0478 [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
David Sedlákecf5eb82019-06-03 14:12:44 +020014#include "common.h"
15
David Sedlák3b4db242018-10-19 16:11:01 +020016#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020019#include <string.h>
David Sedlákf824ad52018-10-14 23:58:15 +020020
David Sedlákf824ad52018-10-14 23:58:15 +020021#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020022#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020023#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "tree.h"
25#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020026#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020027#include "parser_yin.h"
David Sedlákf824ad52018-10-14 23:58:15 +020028
David Sedlák2b214ac2019-06-06 16:11:03 +020029/**
30 * @brief check if given string is URI of yin namespace.
31 * @param ns Namespace URI to check.
32 *
33 * @return true if ns equals YIN_NS_URI false otherwise.
34 */
35#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
36
David Sedlákf6251182019-06-06 10:22:13 +020037const char *const yin_attr_list[] = {
38 [YIN_ARG_NAME] = "name",
39 [YIN_ARG_TARGET_NODE] = "target-node",
40 [YIN_ARG_MODULE] = "module",
41 [YIN_ARG_VALUE] = "value",
42 [YIN_ARG_TEXT] = "text",
43 [YIN_ARG_CONDITION] = "condition",
44 [YIN_ARG_URI] = "uri",
45 [YIN_ARG_DATE] = "date",
46 [YIN_ARG_TAG] = "tag",
47 [YIN_ARG_XMLNS] = "xmlns",
48};
49
David Sedlák872c7b42018-10-26 13:15:20 +020050enum YIN_ARGUMENT
David Sedláke4889912018-11-02 09:52:40 +010051match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020052{
David Sedláka7406952019-04-05 10:33:07 +020053 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020054 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020055 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020056
David Sedlák94de2aa2019-02-15 12:42:11 +010057#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
58#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010059#define IF_ARG_PREFIX_END }
60
David Sedlák1c8b2702019-02-22 11:03:02 +010061 switch (*name) {
David Sedlák18730132019-03-15 15:51:34 +010062 case 'x':
63 already_read += 1;
64 IF_ARG("mlns", 4, YIN_ARG_XMLNS);
65 break;
David Sedlák94de2aa2019-02-15 12:42:11 +010066 case 'c':
67 already_read += 1;
68 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +020069 break;
David Sedlák872c7b42018-10-26 13:15:20 +020070
David Sedlák94de2aa2019-02-15 12:42:11 +010071 case 'd':
72 already_read += 1;
73 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +020074 break;
David Sedlák872c7b42018-10-26 13:15:20 +020075
David Sedlák94de2aa2019-02-15 12:42:11 +010076 case 'm':
77 already_read += 1;
78 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +020079 break;
80
David Sedlák94de2aa2019-02-15 12:42:11 +010081 case 'n':
82 already_read += 1;
83 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +020084 break;
85
David Sedlák94de2aa2019-02-15 12:42:11 +010086 case 't':
87 already_read += 1;
88 IF_ARG_PREFIX("a", 1)
89 IF_ARG("g", 1, YIN_ARG_TAG)
90 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
91 IF_ARG_PREFIX_END
92 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +020093 break;
David Sedlák872c7b42018-10-26 13:15:20 +020094
David Sedlák94de2aa2019-02-15 12:42:11 +010095 case 'u':
96 already_read += 1;
97 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +020098 break;
David Sedlák872c7b42018-10-26 13:15:20 +020099
David Sedlák94de2aa2019-02-15 12:42:11 +0100100 case 'v':
101 already_read += 1;
102 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200103 break;
104 }
105
David Sedlákc10e7902018-12-17 02:17:59 +0100106 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200107 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200108 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200109 }
110
David Sedlák18730132019-03-15 15:51:34 +0100111#undef IF_ARG
112#undef IF_ARG_PREFIX
113#undef IF_ARG_PREFIX_END
114
David Sedlák872c7b42018-10-26 13:15:20 +0200115 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200116}
117
David Sedlák18730132019-03-15 15:51:34 +0100118/**
David Sedlák2b214ac2019-06-06 16:11:03 +0200119 * @brief parse yin argument, arg_val is unchanged if argument arg_type wasn't found.
David Sedláka7406952019-04-05 10:33:07 +0200120 *
121 * @param[in] xml_ctx XML parser context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200122 * @param[in,out] data Data to read from.
David Sedláka7406952019-04-05 10:33:07 +0200123 * @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 +0200124 * @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 +0200125 *
126 * @return LY_ERR values.
David Sedláka7406952019-04-05 10:33:07 +0200127 */
128static LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200129yin_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 +0200130{
131 LY_ERR ret = LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200132
David Sedlák7ff55a92019-06-17 11:11:41 +0200133 char *buf = NULL;
134 size_t buf_len = 0;
David Sedláka7406952019-04-05 10:33:07 +0200135 int dynamic;
136 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák7ff55a92019-06-17 11:11:41 +0200137 struct yin_arg_record *argument_array = NULL, *argument_record = NULL, *iter = NULL;
138 const struct lyxml_ns *ns = NULL;
David Sedláka7406952019-04-05 10:33:07 +0200139
David Sedlák7ff55a92019-06-17 11:11:41 +0200140 /* load all attributes first */
141 while(xml_ctx->status == LYXML_ATTRIBUTE) {
142 LY_ARRAY_NEW_GOTO(xml_ctx->ctx, argument_array, argument_record, ret, cleanup);
143 ret = lyxml_get_attribute(xml_ctx, data, &argument_record->prefix, &argument_record->prefix_len,
144 &argument_record->name, &argument_record->name_len);
145 LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200146
David Sedlák7ff55a92019-06-17 11:11:41 +0200147 if (xml_ctx->status == LYXML_ATTR_CONTENT) {
148 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &argument_record->content, &argument_record->content_len, &dynamic);
149 LY_CHECK_ERR_GOTO(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), cleanup);
150 }
151 }
152
153 /* check validation of attributes */
154 LY_ARRAY_FOR(argument_array, struct yin_arg_record, iter) {
155 ns = lyxml_ns_get(xml_ctx, iter->prefix, iter->prefix_len);
David Sedlák2b214ac2019-06-06 16:11:03 +0200156 if (ns && IS_YIN_NS(ns->uri)) {
David Sedlák7ff55a92019-06-17 11:11:41 +0200157 arg = match_argument_name(iter->name, iter->name_len);
158 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200159 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200160 } else if (arg == arg_type) {
161 *arg_val = lydict_insert(xml_ctx->ctx, iter->content, iter->content_len);
162 LY_CHECK_ERR_GOTO(!(*arg_val), LOGMEM(xml_ctx->ctx); ret = LY_EMEM, cleanup);
David Sedlák2b214ac2019-06-06 16:11:03 +0200163 } else {
David Sedlák7ff55a92019-06-17 11:11:41 +0200164 LOGERR(xml_ctx->ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\".", iter->name_len, iter->name);
165 ret = LY_EVALID;
166 goto cleanup;
David Sedláka7406952019-04-05 10:33:07 +0200167 }
168 }
169 }
170
David Sedlák7ff55a92019-06-17 11:11:41 +0200171cleanup:
172 LY_ARRAY_FREE(argument_array);
173 return ret;
David Sedláka7406952019-04-05 10:33:07 +0200174}
175
176/**
David Sedlák0daba9a2019-03-22 14:07:49 +0100177 * @brief Parse content of whole element as text.
178 *
179 * @param[in] xml_ctx Xml context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200180 * @param[in,out] data Data to read from.
David Sedlák0daba9a2019-03-22 14:07:49 +0100181 * @param[out] value Where content of element should be stored.
David Sedlákb666bcc2019-06-05 15:00:05 +0200182 *
183 * @return LY_ERR values.
David Sedlák0daba9a2019-03-22 14:07:49 +0100184 */
185LY_ERR
David Sedlákecf5eb82019-06-03 14:12:44 +0200186parse_text_element(struct lyxml_context *xml_ctx, const char **data, const char **value)
David Sedlák0daba9a2019-03-22 14:07:49 +0100187{
188 LY_ERR ret = LY_SUCCESS;
189 char *buf = NULL, *out = NULL;
David Sedlákecf5eb82019-06-03 14:12:44 +0200190 size_t buf_len = 0, out_len = 0;
David Sedlák0daba9a2019-03-22 14:07:49 +0100191 int dynamic;
David Sedlák0daba9a2019-03-22 14:07:49 +0100192
David Sedlákecf5eb82019-06-03 14:12:44 +0200193 ret = yin_parse_attribute(xml_ctx, data, YIN_ARG_NONE, NULL);
David Sedlák68826732019-06-05 10:50:58 +0200194 LY_CHECK_RET(ret);
David Sedlák0daba9a2019-03-22 14:07:49 +0100195 LY_CHECK_RET(xml_ctx->status != LYXML_ELEM_CONTENT, LY_EVALID);
196
197 if (xml_ctx->status == LYXML_ELEM_CONTENT) {
198 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
199 LY_CHECK_RET(ret);
200 *value = lydict_insert(xml_ctx->ctx, out, out_len);
201 LY_CHECK_ERR_RET(!(*value), LOGMEM(xml_ctx->ctx), LY_EMEM);
202 }
203
David Sedlák6b7ac2d2019-03-22 14:40:29 +0100204 return LY_SUCCESS;
David Sedlák0daba9a2019-03-22 14:07:49 +0100205}
206
David Sedlákb666bcc2019-06-05 15:00:05 +0200207/**
David Sedlák2b214ac2019-06-06 16:11:03 +0200208 * @brief Parse revision date.
David Sedlákb666bcc2019-06-05 15:00:05 +0200209 *
210 * @param[in] xml_ctx Xml context.
211 * @param[in,out] data Data to read from.
212 * @param[in,out] rev Array to store the parsed value in.
213 * @param[in,out] exts Extension instances to add to.
214 *
David Sedlák2b214ac2019-06-06 16:11:03 +0200215 * @return LY_ERR values.
David Sedlákb666bcc2019-06-05 15:00:05 +0200216 */
David Sedlák81e04022019-04-05 15:05:46 +0200217static LY_ERR
218yin_parse_revision_date(struct lyxml_context *xml_ctx, const char **data, char *rev, struct lysp_ext_instance **exts)
219{
220 LY_ERR ret = LY_SUCCESS;
David Sedlákda63c082019-06-04 13:52:23 +0200221 const char *temp_rev;
David Sedlákcd0c9512019-03-29 13:23:06 +0100222
David Sedlák81e04022019-04-05 15:05:46 +0200223 if (rev[0]) {
224 LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "revision-date");
225 return LY_EVALID;
226 }
227
David Sedlák68826732019-06-05 10:50:58 +0200228 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_DATE, &temp_rev))
David Sedlák81e04022019-04-05 15:05:46 +0200229 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda63c082019-06-04 13:52:23 +0200230 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)xml_ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
231
232 strcpy(rev, temp_rev);
233 lydict_remove(xml_ctx->ctx, temp_rev);
234 /* TODO extension */
David Sedlák81e04022019-04-05 15:05:46 +0200235
David Sedlák81e04022019-04-05 15:05:46 +0200236 return ret;
237}
David Sedlákcd0c9512019-03-29 13:23:06 +0100238
David Sedlákda63c082019-06-04 13:52:23 +0200239LY_ERR
David Sedlák81e04022019-04-05 15:05:46 +0200240yin_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 +0100241{
242 LY_ERR ret = LY_SUCCESS;
243 enum yang_keyword kw;
244 struct lysp_import *imp;
245 const char *prefix, *name;
246 size_t prefix_len, name_len;
247
David Sedlák81e04022019-04-05 15:05:46 +0200248 /* allocate sized array for imports */
249 LY_ARRAY_NEW_RET(xml_ctx->ctx, *imports, imp, LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100250
David Sedláka7406952019-04-05 10:33:07 +0200251 /* parse import attributes */
David Sedlák68826732019-06-05 10:50:58 +0200252 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_MODULE, &imp->name));
253
David Sedlák736fd0d2019-02-15 16:06:31 +0100254 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 +0100255 kw = match_keyword(name, name_len, prefix_len);
David Sedlák736fd0d2019-02-15 16:06:31 +0100256 switch (kw) {
257 case YANG_PREFIX:
David Sedlák81e04022019-04-05 15:05:46 +0200258 LY_CHECK_ERR_RET(imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "prefix"), LY_EVALID);
David Sedlák68826732019-06-05 10:50:58 +0200259 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &imp->prefix));
David Sedlákda63c082019-06-04 13:52:23 +0200260 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 +0100261 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100262 case YANG_DESCRIPTION:
David Sedlák81e04022019-04-05 15:05:46 +0200263 LY_CHECK_ERR_RET(imp->dsc, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "description"), LY_EVALID);
David Sedlákecf5eb82019-06-03 14:12:44 +0200264 parse_text_element(xml_ctx, data, &imp->dsc);
David Sedlákcd0c9512019-03-29 13:23:06 +0100265 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100266 case YANG_REFERENCE:
David Sedlák81e04022019-04-05 15:05:46 +0200267 LY_CHECK_ERR_RET(imp->ref, LOGVAL_YANG(xml_ctx, LY_VCODE_DUPSTMT, "reference"), LY_EVALID);
David Sedlákecf5eb82019-06-03 14:12:44 +0200268 parse_text_element(xml_ctx, data, &imp->ref);
David Sedlákcd0c9512019-03-29 13:23:06 +0100269 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100270 case YANG_REVISION_DATE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200271 yin_parse_revision_date(xml_ctx, data, imp->rev, &imp->exts);
David Sedlák3a55bc12019-06-03 08:57:29 +0200272 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100273 case YANG_CUSTOM:
274 /* TODO parse extension */
David Sedlák3a55bc12019-06-03 08:57:29 +0200275 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100276 default:
David Sedlák916b1902019-06-06 11:35:05 +0200277 LOGERR(xml_ctx->ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, "import");
David Sedlák736fd0d2019-02-15 16:06:31 +0100278 return LY_EVALID;
279 }
280 }
281
David Sedlák916b1902019-06-06 11:35:05 +0200282 LY_CHECK_ERR_RET(!imp->prefix, LOGVAL_YANG(xml_ctx, LY_VCODE_MISSATTR, "prefix", "import"), LY_EVALID);
David Sedlák736fd0d2019-02-15 16:06:31 +0100283 return ret;
284}
285
David Sedlákd5318ee2019-02-15 10:14:50 +0100286/**
287 * @brief Parse module substatements.
288 *
289 * @param[in] xml_ctx xml context.
David Sedlákb666bcc2019-06-05 15:00:05 +0200290 * @param[in,out] data Data to read from.
David Sedlák2b214ac2019-06-06 16:11:03 +0200291 * @param[out] mod Parsed module structure.
David Sedlákd5318ee2019-02-15 10:14:50 +0100292 *
293 * @return LY_ERR values.
294 */
David Sedlák4a4c0722018-11-26 17:03:10 +0100295LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100296parse_mod(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200297{
David Sedlákf3b24f62018-11-02 10:40:47 +0100298 LY_ERR ret = LY_SUCCESS;
David Sedlák4a4c0722018-11-26 17:03:10 +0100299 enum yang_keyword kw = YANG_NONE;
David Sedláke4889912018-11-02 09:52:40 +0100300 const char *prefix, *name;
David Sedlák3b4db242018-10-19 16:11:01 +0200301 size_t prefix_len, name_len;
David Sedlák4b4713f2019-02-15 13:47:45 +0100302 enum yang_module_stmt mod_stmt = Y_MOD_MODULE_HEADER;
David Sedlák3b4db242018-10-19 16:11:01 +0200303
David Sedlákf3b24f62018-11-02 10:40:47 +0100304 char *buf = NULL, *out = NULL;
305 size_t buf_len = 0, out_len = 0;
306 int dynamic;
307
David Sedlák2b214ac2019-06-06 16:11:03 +0200308 yin_parse_attribute(xml_ctx, data, YIN_ARG_NAME, &(*mod)->mod->name);
David Sedlák18730132019-03-15 15:51:34 +0100309
David Sedlák2b214ac2019-06-06 16:11:03 +0200310 LY_CHECK_ERR_RET(!(*mod)->mod->name, LOGVAL_YANG(xml_ctx, LYVE_SYNTAX_YIN, "Missing argument name of a module"), LY_EVALID);
David Sedlákc136b972019-03-08 13:39:06 +0100311 ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
David Sedlák18730132019-03-15 15:51:34 +0100312 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 +0100313
David Sedlák736fd0d2019-02-15 16:06:31 +0100314 /* loop over all elements and parse them */
David Sedlákc136b972019-03-08 13:39:06 +0100315 while (xml_ctx->status != LYXML_END) {
David Sedlák4b4713f2019-02-15 13:47:45 +0100316#define CHECK_ORDER(SECTION) \
317 if (mod_stmt > SECTION) {return LY_EVALID;}mod_stmt = SECTION
318
319 switch (kw) {
320 /* module header */
321 case YANG_NAMESPACE:
322 case YANG_PREFIX:
323 CHECK_ORDER(Y_MOD_MODULE_HEADER);
324 break;
325 case YANG_YANG_VERSION:
326 CHECK_ORDER(Y_MOD_MODULE_HEADER);
327 break;
328 /* linkage */
329 case YANG_INCLUDE:
330 case YANG_IMPORT:
331 CHECK_ORDER(Y_MOD_LINKAGE);
332 break;
333 /* meta */
334 case YANG_ORGANIZATION:
335 case YANG_CONTACT:
336 case YANG_DESCRIPTION:
337 case YANG_REFERENCE:
338 CHECK_ORDER(Y_MOD_META);
339 break;
340
341 /* revision */
342 case YANG_REVISION:
343 CHECK_ORDER(Y_MOD_REVISION);
344 break;
345 /* body */
346 case YANG_ANYDATA:
347 case YANG_ANYXML:
348 case YANG_AUGMENT:
349 case YANG_CHOICE:
350 case YANG_CONTAINER:
351 case YANG_DEVIATION:
352 case YANG_EXTENSION:
353 case YANG_FEATURE:
354 case YANG_GROUPING:
355 case YANG_IDENTITY:
356 case YANG_LEAF:
357 case YANG_LEAF_LIST:
358 case YANG_LIST:
359 case YANG_NOTIFICATION:
360 case YANG_RPC:
361 case YANG_TYPEDEF:
362 case YANG_USES:
363 case YANG_CUSTOM:
364 mod_stmt = Y_MOD_BODY;
365 break;
366 default:
367 /* error will be handled in the next switch */
368 break;
369 }
370#undef CHECK_ORDER
371
David Sedlák68826732019-06-05 10:50:58 +0200372 LY_CHECK_RET(lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák4a4c0722018-11-26 17:03:10 +0100373
David Sedlákc136b972019-03-08 13:39:06 +0100374 if (name) {
David Sedlák18730132019-03-15 15:51:34 +0100375 kw = match_keyword(name, name_len, prefix_len);
David Sedlákc136b972019-03-08 13:39:06 +0100376 switch (kw) {
David Sedlák736fd0d2019-02-15 16:06:31 +0100377
David Sedlákc136b972019-03-08 13:39:06 +0100378 /* module header */
379 case YANG_NAMESPACE:
David Sedlák68826732019-06-05 10:50:58 +0200380 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_URI, &(*mod)->mod->ns));
David Sedlákc136b972019-03-08 13:39:06 +0100381 break;
382 case YANG_PREFIX:
David Sedlák68826732019-06-05 10:50:58 +0200383 LY_CHECK_RET(yin_parse_attribute(xml_ctx, data, YIN_ARG_VALUE, &(*mod)->mod->prefix));
David Sedlákc136b972019-03-08 13:39:06 +0100384 break;
David Sedlák4a4c0722018-11-26 17:03:10 +0100385
David Sedlákc136b972019-03-08 13:39:06 +0100386 /* linkage */
387 case YANG_IMPORT:
David Sedlák81e04022019-04-05 15:05:46 +0200388 yin_parse_import(xml_ctx, (*mod)->mod->prefix, data, &(*mod)->imports);
David Sedlákc136b972019-03-08 13:39:06 +0100389 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100390
David Sedlákc136b972019-03-08 13:39:06 +0100391 /* meta */
392 case YANG_ORGANIZATION:
David Sedlákecf5eb82019-06-03 14:12:44 +0200393 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->org));
David Sedlákc136b972019-03-08 13:39:06 +0100394 break;
395 case YANG_CONTACT:
David Sedlákecf5eb82019-06-03 14:12:44 +0200396 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->contact));
David Sedlákc136b972019-03-08 13:39:06 +0100397 break;
398 case YANG_DESCRIPTION:
David Sedlákecf5eb82019-06-03 14:12:44 +0200399 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->dsc));
David Sedlákc136b972019-03-08 13:39:06 +0100400 break;
401 case YANG_REFERENCE:
David Sedlákecf5eb82019-06-03 14:12:44 +0200402 LY_CHECK_RET(parse_text_element(xml_ctx, data, &(*mod)->mod->ref));
David Sedlákc136b972019-03-08 13:39:06 +0100403 break;
David Sedlák736fd0d2019-02-15 16:06:31 +0100404
David Sedlákc136b972019-03-08 13:39:06 +0100405 default:
David Sedlák18730132019-03-15 15:51:34 +0100406 return LY_EVALID;
David Sedlákc136b972019-03-08 13:39:06 +0100407 break;
408 }
David Sedlák4a4c0722018-11-26 17:03:10 +0100409 }
410 }
411
David Sedlák68826732019-06-05 10:50:58 +0200412 return LY_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200413}
414
David Sedlákd5318ee2019-02-15 10:14:50 +0100415/**
416 * @brief Parse yin submodule.
417 *
418 * @param[in] ctx Context of YANG schemas.
David Sedlákb666bcc2019-06-05 15:00:05 +0200419 * @param[in,out] data Data to read from.
420 * @param[out] submod Parsed submodule structure.
David Sedlákd5318ee2019-02-15 10:14:50 +0100421 *
422 * @return LY_ERR values.
423 */
424LY_ERR
425yin_parse_submodule(struct ly_ctx *ctx, const char *data, struct lysp_submodule **submod)
426{
427 LY_ERR ret = LY_SUCCESS;
428 enum yang_keyword kw = YANG_NONE;
429 struct lyxml_context xml_ctx;
430 struct lysp_submodule *mod_p = NULL;
431 const char *prefix, *name;
432 size_t prefix_len, name_len;
David Sedlák3017da42019-02-15 09:48:04 +0100433
David Sedlákd5318ee2019-02-15 10:14:50 +0100434 /* initialize xml context */
435 memset(&xml_ctx, 0, sizeof xml_ctx);
436 xml_ctx.ctx = ctx;
437 xml_ctx.line = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100438
David Sedlákd5318ee2019-02-15 10:14:50 +0100439 /* check submodule */
440 ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
441 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100442 kw = match_keyword(name, name_len, prefix_len);
David Sedlákd5318ee2019-02-15 10:14:50 +0100443 if (kw == YANG_MODULE) {
444 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
445 ret = LY_EINVAL;
446 goto cleanup;
447 } else if (kw != YANG_SUBMODULE) {
David Sedlák0a875b42019-03-07 22:24:05 +0100448 LOGVAL_YANG(&xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
449 ly_stmt2str(kw));
David Sedlákd5318ee2019-02-15 10:14:50 +0100450 ret = LY_EVALID;
451 goto cleanup;
452 }
David Sedlák3017da42019-02-15 09:48:04 +0100453
David Sedlákd5318ee2019-02-15 10:14:50 +0100454 /* allocate module */
455 mod_p = calloc(1, sizeof *mod_p);
456 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
457 mod_p->parsing = 1;
David Sedlák3017da42019-02-15 09:48:04 +0100458
David Sedlákd5318ee2019-02-15 10:14:50 +0100459 /* parser submodule substatements */
David Sedlákb666bcc2019-06-05 15:00:05 +0200460 /* ret = parse_submod(&xml_ctx, &data, mod_p); */
David Sedlákd5318ee2019-02-15 10:14:50 +0100461 LY_CHECK_GOTO(ret, cleanup);
David Sedlák3017da42019-02-15 09:48:04 +0100462
David Sedlákd5318ee2019-02-15 10:14:50 +0100463 mod_p->parsing = 0;
464 *submod = mod_p;
David Sedlák3017da42019-02-15 09:48:04 +0100465
David Sedlákd5318ee2019-02-15 10:14:50 +0100466cleanup:
467 if (ret) {
468 lysp_submodule_free(ctx, mod_p);
469 }
David Sedlák3017da42019-02-15 09:48:04 +0100470
David Sedlákd5318ee2019-02-15 10:14:50 +0100471 lyxml_context_clear(&xml_ctx);
472 return ret;
473}
David Sedlák3017da42019-02-15 09:48:04 +0100474
David Sedlák3b4db242018-10-19 16:11:01 +0200475LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +0100476yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +0200477{
David Sedláke4889912018-11-02 09:52:40 +0100478 LY_ERR ret = LY_SUCCESS;
479 enum yang_keyword kw = YANG_NONE;
David Sedlákecf5eb82019-06-03 14:12:44 +0200480 struct lys_parser_ctx parser_ctx;
David Sedlákaadab9c2019-04-05 15:01:27 +0200481 struct lyxml_context *xml_ctx = (struct lyxml_context *)&parser_ctx;
David Sedlák3017da42019-02-15 09:48:04 +0100482 struct lysp_module *mod_p = NULL;
483 const char *prefix, *name;
484 size_t prefix_len, name_len;
David Sedlák3b4db242018-10-19 16:11:01 +0200485
David Sedlák3017da42019-02-15 09:48:04 +0100486 /* initialize xml context */
David Sedlákaadab9c2019-04-05 15:01:27 +0200487 memset(&parser_ctx, 0, sizeof parser_ctx);
488 xml_ctx->ctx = ctx;
489 xml_ctx->line = 1;
David Sedláke4889912018-11-02 09:52:40 +0100490
David Sedlák3017da42019-02-15 09:48:04 +0100491 /* check submodule */
David Sedlákaadab9c2019-04-05 15:01:27 +0200492 ret = lyxml_get_element(xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák3017da42019-02-15 09:48:04 +0100493 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák18730132019-03-15 15:51:34 +0100494 kw = match_keyword(name, name_len, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +0100495 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +0100496 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
497 ret = LY_EINVAL;
498 goto cleanup;
499 } else if (kw != YANG_MODULE) {
David Sedlákaadab9c2019-04-05 15:01:27 +0200500 LOGVAL_YANG(xml_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +0200501 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +0100502 ret = LY_EVALID;
503 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +0100504 }
505
David Sedlák3017da42019-02-15 09:48:04 +0100506 /* allocate module */
507 mod_p = calloc(1, sizeof *mod_p);
508 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
509 mod_p->mod = mod;
510 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +0100511
David Sedlák3017da42019-02-15 09:48:04 +0100512 /* parser module substatements */
David Sedlákaadab9c2019-04-05 15:01:27 +0200513 ret = parse_mod(xml_ctx, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +0100514 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +0100515
David Sedlák3017da42019-02-15 09:48:04 +0100516 mod_p->parsing = 0;
517 mod->parsed = mod_p;
518
519cleanup:
520 if (ret) {
521 lysp_module_free(mod_p);
522 }
523
David Sedlákaadab9c2019-04-05 15:01:27 +0200524 lyxml_context_clear(xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +0100525 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +0200526}