blob: cd5c433ae07dee2d256a7775135f83a911a93345 [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ák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
David Sedlák3b4db242018-10-19 16:11:01 +020017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020020#include <string.h>
David Sedlákd6e56892019-07-01 15:40:24 +020021#include <stdbool.h>
David Sedlákf824ad52018-10-14 23:58:15 +020022
David Sedlákf824ad52018-10-14 23:58:15 +020023#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020025#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020026#include "tree.h"
27#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020028#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020030
David Sedlák2b214ac2019-06-06 16:11:03 +020031/**
32 * @brief check if given string is URI of yin namespace.
33 * @param ns Namespace URI to check.
34 *
35 * @return true if ns equals YIN_NS_URI false otherwise.
36 */
37#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
38
David Sedlákf6251182019-06-06 10:22:13 +020039const char *const yin_attr_list[] = {
40 [YIN_ARG_NAME] = "name",
41 [YIN_ARG_TARGET_NODE] = "target-node",
42 [YIN_ARG_MODULE] = "module",
43 [YIN_ARG_VALUE] = "value",
44 [YIN_ARG_TEXT] = "text",
45 [YIN_ARG_CONDITION] = "condition",
46 [YIN_ARG_URI] = "uri",
47 [YIN_ARG_DATE] = "date",
48 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020049};
50
David Sedlák1bccdfa2019-06-17 15:55:27 +020051enum yang_keyword
David Sedlákda8ffa32019-07-08 14:17:10 +020052yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
David Sedlák1bccdfa2019-06-17 15:55:27 +020053{
David Sedlák8f7a1172019-06-20 14:42:18 +020054 const char *start = NULL;
55 enum yang_keyword kw = YANG_NONE;
56 const struct lyxml_ns *ns = NULL;
57
58 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020059 return YANG_NONE;
60 }
61
David Sedlákda8ffa32019-07-08 14:17:10 +020062 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020063 if (ns) {
64 if (!IS_YIN_NS(ns->uri)) {
65 return YANG_CUSTOM;
66 }
67 } else {
68 /* elements without namespace are automatically unknown */
69 return YANG_NONE;
70 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020071
David Sedlák8f7a1172019-06-20 14:42:18 +020072
73 start = name;
74 kw = lysp_match_kw(NULL, &name);
75
76 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020077 return kw;
78 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020079 if (strncmp(start, "text", name_len) == 0) {
80 return YIN_TEXT;
81 } else if (strncmp(start, "value", name_len) == 0) {
82 return YIN_VALUE;
83 } else {
84 return YANG_NONE;
85 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020086 }
87}
88
David Sedlák872c7b42018-10-26 13:15:20 +020089enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020090yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020091{
David Sedláka7406952019-04-05 10:33:07 +020092 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020093 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020094 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020095
David Sedlák94de2aa2019-02-15 12:42:11 +010096#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
97#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +010098#define IF_ARG_PREFIX_END }
99
David Sedlák1c8b2702019-02-22 11:03:02 +0100100 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100101 case 'c':
102 already_read += 1;
103 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200104 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200105
David Sedlák94de2aa2019-02-15 12:42:11 +0100106 case 'd':
107 already_read += 1;
108 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200109 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200110
David Sedlák94de2aa2019-02-15 12:42:11 +0100111 case 'm':
112 already_read += 1;
113 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200114 break;
115
David Sedlák94de2aa2019-02-15 12:42:11 +0100116 case 'n':
117 already_read += 1;
118 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200119 break;
120
David Sedlák94de2aa2019-02-15 12:42:11 +0100121 case 't':
122 already_read += 1;
123 IF_ARG_PREFIX("a", 1)
124 IF_ARG("g", 1, YIN_ARG_TAG)
125 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
126 IF_ARG_PREFIX_END
127 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200128 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200129
David Sedlák94de2aa2019-02-15 12:42:11 +0100130 case 'u':
131 already_read += 1;
132 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200133 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200134
David Sedlák94de2aa2019-02-15 12:42:11 +0100135 case 'v':
136 already_read += 1;
137 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200138 break;
139 }
140
David Sedlákc10e7902018-12-17 02:17:59 +0100141 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200142 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200143 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200144 }
145
David Sedlák18730132019-03-15 15:51:34 +0100146#undef IF_ARG
147#undef IF_ARG_PREFIX
148#undef IF_ARG_PREFIX_END
149
David Sedlák872c7b42018-10-26 13:15:20 +0200150 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200151}
152
David Sedlákb4e44562019-07-04 15:42:12 +0200153/**
154 * @brief free argument record, content loaded from lyxml_get_string() can be
155 * dynamically allocated in some cases so it must be also freed.
156 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200157static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
158 (void)ctx; /* unused */
David Sedlák00250342019-06-21 14:19:39 +0200159 if (record->dynamic_content) {
160 free(record->content);
161 }
162}
163
David Sedlák8f7a1172019-06-20 14:42:18 +0200164LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200165yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200166{
167 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200168 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200169 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200170
David Sedlák555c7202019-07-04 12:14:12 +0200171 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200172 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
173 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200174 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200175
David Sedlákda8ffa32019-07-08 14:17:10 +0200176 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
177 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200178 argument_record->name = name.value;
179 argument_record->name_len = name.len;
180 argument_record->prefix = prefix.value;
181 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200182 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200183 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200184 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200185 }
186 }
187
David Sedlák8f7a1172019-06-20 14:42:18 +0200188cleanup:
189 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200190 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200191 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200192 }
193 return ret;
194}
195
David Sedlákb4e44562019-07-04 15:42:12 +0200196/**
197 * @brief Parse yin argument.
198 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200199 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200200 * @param[in] attrs Array of attributes.
201 * @param[in,out] data Data to read from.
202 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without special argument).
203 * @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ák292763b2019-07-09 11:10:53 +0200204 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200205 * @param[in] current_element Identification of current element, used for logging.
206 *
207 * @return LY_ERR values.
208 */
209static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200210yin_parse_attribute(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, enum YIN_ARGUMENT arg_type,
David Sedlák292763b2019-07-09 11:10:53 +0200211 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200212{
David Sedlák8f7a1172019-06-20 14:42:18 +0200213 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
214 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200215 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200216
David Sedlák1bccdfa2019-06-17 15:55:27 +0200217 /* validation of attributes */
David Sedlákb4e44562019-07-04 15:42:12 +0200218 LY_ARRAY_FOR(*attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200219 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
220 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200221 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200222 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200223 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200224 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200225 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
226 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200227 found = true;
David Sedlák57715b12019-06-17 13:05:22 +0200228 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200229 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200230 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200231 /* string is no longer supposed to be freed when the sized array is freed */
232 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200233 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200234 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200235 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200236 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200237 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200238 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\" of %s element.", iter->name_len, iter->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200239 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200240 }
241 }
242 }
243
David Sedlák292763b2019-07-09 11:10:53 +0200244 /* anything else than Y_MAYBE_STR_ARG is mandatory */
245 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200246 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.", yin_attr2str(arg_type), ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200247 return LY_EVALID;
248 }
249
David Sedlák292763b2019-07-09 11:10:53 +0200250 /* TODO check validity according to val_type */
251
David Sedlák619db942019-07-03 14:47:30 +0200252 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200253}
254
David Sedlákd6e56892019-07-01 15:40:24 +0200255/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200256 * @brief Get record with given type. Array must be sorted in ascending order by array[n].type.
David Sedlákd6e56892019-07-01 15:40:24 +0200257 *
258 * @param[in] type Type of wanted record.
259 * @param[in] array_size Size of array.
260 * @param[in] array Searched array.
261 *
262 * @return Pointer to desired record on success, NULL if element is not in the array.
263 */
David Sedlákb4e44562019-07-04 15:42:12 +0200264static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200265get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200266{
David Sedlákb0faad82019-07-04 14:28:59 +0200267 signed char left = 0, right = array_size - 1, middle;
268
269 while (left <= right) {
270 middle = left + (right - left) / 2;
271
272 if (array[middle].type == type) {
273 return &array[middle];
274 }
275
276 if (array[middle].type < type) {
277 left = middle + 1;
278 } else {
279 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200280 }
281 }
282
283 return NULL;
284}
285
David Sedlák21f87cd2019-07-03 16:53:23 +0200286LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200287yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200288 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200289{
David Sedlákb0faad82019-07-04 14:28:59 +0200290 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200291 /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */
292 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200293 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory subelement %s of %s element.",
David Sedlák555c7202019-07-04 12:14:12 +0200294 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200295 return LY_EVALID;
296 }
297 }
298
299 return LY_SUCCESS;
300}
301
302LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200303yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200304 signed char subelem_info_size, enum yang_keyword current_element, struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200305{
David Sedlákb0faad82019-07-04 14:28:59 +0200306 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200307 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200308 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Subelement %s of %s element must be defined as first subelement.",
David Sedlák555c7202019-07-04 12:14:12 +0200309 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200310 return LY_EVALID;
311 }
312 }
313
314 return LY_SUCCESS;
315}
316
David Sedlákb0faad82019-07-04 14:28:59 +0200317/* TODO add something like ifdef NDEBUG around this function, this is supposed to be checked only in debug mode */
318bool
319is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
320{
David Sedlák292763b2019-07-09 11:10:53 +0200321 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200322
323 for (signed char i = 0; i < subelem_info_size; ++i) {
324 if (subelem_info[i].type <= current) {
325 return false;
326 }
327 current = subelem_info[i].type;
328 }
329
330 return true;
331}
332
David Sedlákd6e56892019-07-01 15:40:24 +0200333/**
David Sedlákb4e44562019-07-04 15:42:12 +0200334 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
335 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200336 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200337 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200338 * @param[in] attrs Attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200339 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200340 * @param[in] kw Type of current element.
341 * @param[out] value Where value of attribute should be stored.
342 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200343 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200344 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200345 *
David Sedlákd6e56892019-07-01 15:40:24 +0200346 * @return LY_ERR values.
347 */
David Sedlákb4e44562019-07-04 15:42:12 +0200348static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200349yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlák292763b2019-07-09 11:10:53 +0200350 const char **value, enum YIN_ARGUMENT arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200351{
David Sedlák292763b2019-07-09 11:10:53 +0200352 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlákb4e44562019-07-04 15:42:12 +0200353 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
354
David Sedlákda8ffa32019-07-08 14:17:10 +0200355 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200356}
357
358/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200359 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
360 * more instances, such as base or if-feature.
361 *
362 * @param[in,out] ctx YIN parser context for logging and to store current state.
363 * @param[in] attrs Attributes of current element.
364 * @param[in,out] data Data to read from, always moved to currently handled character.
365 * @param[in] kw Type of current element.
366 * @param[out] values Parsed values to add to.
367 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200368 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200369 * @param[in,out] exts Extension instance to add to.
370 *
371 * @return LY_ERR values.
372 */
373static LY_ERR
374yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlák292763b2019-07-09 11:10:53 +0200375 const char ***values, enum YIN_ARGUMENT arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
David Sedlák5f8191e2019-07-08 16:35:52 +0200376{
377 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200378 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200379 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
380 struct yin_subelement subelems[1] = {{YANG_CUSTOM, &index, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200381 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200382
383 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
384}
385
386/**
David Sedlákb4e44562019-07-04 15:42:12 +0200387 * @brief function to parse meta tags (description, contact, ...) eg. elements with
388 * text element as child
389 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200390 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200391 * @param[in] args Sized array of arguments of current element.
392 * @param[in,out] data Data to read from.
393 * @param[out] value Where the content of meta element should be stored.
394 *
395 * @return LY_ERR values.
396 */
397static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200398yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200399 const char **value, struct lysp_ext_instance **exts)
400{
401 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
402
403 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
404 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
405
David Sedlákda8ffa32019-07-08 14:17:10 +0200406 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200407}
408
409/**
410 * @brief Map keyword type to substatement info.
411 *
412 * @param[in] kw Keyword type.
413 *
414 * @return correct LYEXT_SUBSTMT information.
415 */
416static LYEXT_SUBSTMT
417kw2lyext_substmt(enum yang_keyword kw)
418{
419 switch (kw) {
420 case YANG_ARGUMENT:
421 return LYEXT_SUBSTMT_ARGUMENT;
422 case YANG_BASE:
423 return LYEXT_SUBSTMT_BASE;
424 case YANG_BELONGS_TO:
425 return LYEXT_SUBSTMT_BELONGSTO;
426 case YANG_CONTACT:
427 return LYEXT_SUBSTMT_CONTACT;
428 case YANG_DEFAULT:
429 return LYEXT_SUBSTMT_DEFAULT;
430 case YANG_DESCRIPTION:
431 return LYEXT_SUBSTMT_DESCRIPTION;
432 case YANG_ERROR_APP_TAG:
433 return LYEXT_SUBSTMT_ERRTAG;
434 case YANG_ERROR_MESSAGE:
435 return LYEXT_SUBSTMT_ERRMSG;
436 case YANG_KEY:
437 return LYEXT_SUBSTMT_KEY;
438 case YANG_NAMESPACE:
439 return LYEXT_SUBSTMT_NAMESPACE;
440 case YANG_ORGANIZATION:
441 return LYEXT_SUBSTMT_ORGANIZATION;
442 case YANG_PATH:
443 return LYEXT_SUBSTMT_PATH;
444 case YANG_PREFIX:
445 return LYEXT_SUBSTMT_PREFIX;
446 case YANG_PRESENCE:
447 return LYEXT_SUBSTMT_PRESENCE;
448 case YANG_REFERENCE:
449 return LYEXT_SUBSTMT_REFERENCE;
450 case YANG_REVISION_DATE:
451 return LYEXT_SUBSTMT_REVISIONDATE;
452 case YANG_UNITS:
453 return LYEXT_SUBSTMT_UNITS;
454 case YANG_VALUE:
455 return LYEXT_SUBSTMT_VALUE;
456 case YANG_YANG_VERSION:
457 return LYEXT_SUBSTMT_VERSION;
458 case YANG_MODIFIER:
459 return LYEXT_SUBSTMT_MODIFIER;
460 case YANG_REQUIRE_INSTANCE:
461 return LYEXT_SUBSTMT_REQINSTANCE;
462 case YANG_YIN_ELEMENT:
463 return LYEXT_SUBSTMT_YINELEM;
464 case YANG_CONFIG:
465 return LYEXT_SUBSTMT_CONFIG;
466 case YANG_MANDATORY:
467 return LYEXT_SUBSTMT_MANDATORY;
468 case YANG_ORDERED_BY:
469 return LYEXT_SUBSTMT_ORDEREDBY;
470 case YANG_STATUS:
471 return LYEXT_SUBSTMT_STATUS;
472 case YANG_FRACTION_DIGITS:
473 return LYEXT_SUBSTMT_FRACDIGITS;
474 case YANG_MAX_ELEMENTS:
475 return LYEXT_SUBSTMT_MAX;
476 case YANG_MIN_ELEMENTS:
477 return LYEXT_SUBSTMT_MIN;
478 case YANG_POSITION:
479 return LYEXT_SUBSTMT_POSITION;
480 case YANG_UNIQUE:
481 return LYEXT_SUBSTMT_UNIQUE;
482 case YANG_IF_FEATURE:
483 return LYEXT_SUBSTMT_IFFEATURE;
484 default:
485 return LYEXT_SUBSTMT_SELF;
486 }
487}
488
David Sedlák9c40a922019-07-08 17:04:43 +0200489
490/**
491 * @brief Parse belongs-to element.
492 *
493 * @param[in] ctx Yin parser context for logging and to store current state.
494 * @param[in] attrs Array of attributes of belongs-to element.
495 * @param[in,out] data Data to read from, always moved to currently handled character.
496 * @param[out] submod Structure of submodule that is being parsed.
497 * @param[in,out] exts Extension instances to add to.
498 *
499 * @return LY_ERR values
500 */
501static LY_ERR
502yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
503 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
504{
505 struct yin_subelement subelems[2] = {{YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
506 {YANG_CUSTOM, NULL, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200507 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, YANG_BELONGS_TO));
David Sedlák9c40a922019-07-08 17:04:43 +0200508
509 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
510}
511
David Sedlákd6e56892019-07-01 15:40:24 +0200512LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200513yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, signed char subelem_info_size,
David Sedlák3ffbc522019-07-02 17:49:28 +0200514 const char **data, enum yang_keyword current_element, const char **text_content, struct lysp_ext_instance **exts)
David Sedlákd6e56892019-07-01 15:40:24 +0200515{
516 LY_ERR ret = LY_SUCCESS;
517 struct sized_string prefix, name;
518 char *out;
519 size_t out_len;
520 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200521 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200522 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200523 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200524 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200525 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200526 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200527
David Sedlákda8ffa32019-07-08 14:17:10 +0200528 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
529 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200530 /* current element has subelements as content */
531 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200532 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
533 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200534 LY_CHECK_GOTO(ret, cleanup);
535 if (!name.value) {
536 /* end of current element reached */
537 break;
538 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200539 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200540 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200541 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200542
543 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200544 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
545 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200546 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name.len, name.value, ly_stmt2str(current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200547 ret = LY_EVALID;
548 goto cleanup;
549 }
550
David Sedlák5f8191e2019-07-08 16:35:52 +0200551 /* TODO check relative order */
552
David Sedlák21f87cd2019-07-03 16:53:23 +0200553 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200554 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200555 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Redefinition of %s element in %s element.", ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200556 return LY_EVALID;
557 }
558 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200559 LY_CHECK_RET(yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec));
David Sedlák21f87cd2019-07-03 16:53:23 +0200560 }
561 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
562
David Sedlákd6e56892019-07-01 15:40:24 +0200563 switch (kw) {
564 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200565 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlákda8ffa32019-07-08 14:17:10 +0200566 ret = yin_parse_extension_instance(ctx, &subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200567 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200568 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200569 break;
570 case YANG_ACTION:
571 break;
572 case YANG_ANYDATA:
573 break;
574 case YANG_ANYXML:
575 break;
576 case YANG_ARGUMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200577 ret = yin_parse_argument_element(ctx, &subelem_attrs, data, (struct yin_argument_meta *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200578 break;
579 case YANG_AUGMENT:
580 break;
581 case YANG_BASE:
David Sedlák292763b2019-07-09 11:10:53 +0200582 if (current_element == YANG_IDENTITY) {
583 type = (struct lysp_type *)subelem_info_rec->dest;
584 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
585 Y_PREF_IDENTIF_ARG, exts);
586 type->flags |= LYS_SET_BASE;
587 } else if (current_element == YANG_TYPE) {
588 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
589 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
590 } else {
591 LOGINT(ctx->xml_ctx.ctx);
592 ret = LY_EINT;
593 }
David Sedlákd6e56892019-07-01 15:40:24 +0200594 break;
595 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +0200596 ret = yin_parse_belongs_to(ctx, subelem_attrs, data, (struct lysp_submodule *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200597 break;
598 case YANG_BIT:
599 break;
600 case YANG_CASE:
601 break;
602 case YANG_CHOICE:
603 break;
604 case YANG_CONFIG:
605 break;
606 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200607 case YANG_DESCRIPTION:
608 case YANG_ORGANIZATION:
609 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200610 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200611 break;
612 case YANG_CONTAINER:
613 break;
614 case YANG_DEFAULT:
615 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200616 case YANG_DEVIATE:
617 break;
618 case YANG_DEVIATION:
619 break;
620 case YANG_ENUM:
621 break;
622 case YANG_ERROR_APP_TAG:
623 break;
624 case YANG_ERROR_MESSAGE:
625 break;
626 case YANG_EXTENSION:
David Sedlákda8ffa32019-07-08 14:17:10 +0200627 ret = yin_parse_extension(ctx, &subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200628 break;
629 case YANG_FEATURE:
630 break;
631 case YANG_FRACTION_DIGITS:
632 break;
633 case YANG_GROUPING:
634 break;
635 case YANG_IDENTITY:
636 break;
637 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200638 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200639 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200640 break;
641 case YANG_IMPORT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200642 ret = yin_parse_import(ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200643 break;
644 case YANG_INCLUDE:
645 break;
646 case YANG_INPUT:
647 break;
648 case YANG_KEY:
649 break;
650 case YANG_LEAF:
651 break;
652 case YANG_LEAF_LIST:
653 break;
654 case YANG_LENGTH:
655 break;
656 case YANG_LIST:
657 break;
658 case YANG_MANDATORY:
659 break;
660 case YANG_MAX_ELEMENTS:
661 break;
662 case YANG_MIN_ELEMENTS:
663 break;
664 case YANG_MODIFIER:
665 break;
666 case YANG_MODULE:
667 break;
668 case YANG_MUST:
669 break;
670 case YANG_NAMESPACE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200671 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200672 (const char **)subelem_info_rec->dest, YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200673 break;
674 case YANG_NOTIFICATION:
675 break;
676 case YANG_ORDERED_BY:
677 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200678 case YANG_OUTPUT:
679 break;
680 case YANG_PATH:
681 break;
682 case YANG_PATTERN:
683 break;
684 case YANG_POSITION:
685 break;
686 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +0200687 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200688 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200689 break;
690 case YANG_PRESENCE:
691 break;
692 case YANG_RANGE:
693 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200694 case YANG_REFINE:
695 break;
696 case YANG_REQUIRE_INSTANCE:
697 break;
698 case YANG_REVISION:
699 break;
700 case YANG_REVISION_DATE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200701 ret = yin_parse_revision_date(ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200702 break;
703 case YANG_RPC:
704 break;
705 case YANG_STATUS:
David Sedlákda8ffa32019-07-08 14:17:10 +0200706 ret = yin_parse_status(ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200707 break;
708 case YANG_SUBMODULE:
709 break;
710 case YANG_TYPE:
711 break;
712 case YANG_TYPEDEF:
713 break;
714 case YANG_UNIQUE:
715 break;
716 case YANG_UNITS:
717 break;
718 case YANG_USES:
719 break;
720 case YANG_VALUE:
721 break;
722 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200723 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200724 break;
725 case YANG_YANG_VERSION:
726 break;
727 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200728 ret = yin_parse_yin_element_element(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200729 break;
730 case YIN_TEXT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200731 ret = yin_parse_content(ctx, NULL, 0, data, YIN_TEXT, (const char **)subelem_info_rec->dest, NULL);
David Sedlák3ffbc522019-07-02 17:49:28 +0200732 break;
733 case YIN_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +0200734 break;
735 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200736 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200737 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200738 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200739 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200740 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200741 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200742 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200743 }
744 } else {
745 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200746 /* save text content, if text_content isn't set, it's just ignored */
747 if (text_content) {
748 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200749 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200750 if (!*text_content) {
751 free(out);
752 return LY_EMEM;
753 }
754 } else {
755 if (out_len == 0) {
756 *text_content = NULL;
757 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200758 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200759 }
760 }
761 }
David Sedlákd6e56892019-07-01 15:40:24 +0200762 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +0200763 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
David Sedlákd6e56892019-07-01 15:40:24 +0200764 }
David Sedlák555c7202019-07-04 12:14:12 +0200765
David Sedlákda8ffa32019-07-08 14:17:10 +0200766 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200767 }
768
769cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +0200770 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200771 return ret;
772}
773
David Sedlák619db942019-07-03 14:47:30 +0200774LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200775yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, char *rev,
David Sedlák619db942019-07-03 14:47:30 +0200776 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200777{
778 LY_ERR ret = LY_SUCCESS;
David Sedlák3ffbc522019-07-02 17:49:28 +0200779 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200780 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200781
David Sedlák292763b2019-07-09 11:10:53 +0200782 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
David Sedlák81e04022019-04-05 15:05:46 +0200783 LY_CHECK_RET(ret != LY_SUCCESS, ret);
David Sedlákda8ffa32019-07-08 14:17:10 +0200784 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
David Sedlákda63c082019-06-04 13:52:23 +0200785
786 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +0200787 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200788
David Sedlákda8ffa32019-07-08 14:17:10 +0200789 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200790}
David Sedlák00250342019-06-21 14:19:39 +0200791
David Sedlák3ffbc522019-07-02 17:49:28 +0200792LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200793yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, struct lysp_module *mod)
David Sedlák736fd0d2019-02-15 16:06:31 +0100794{
David Sedlák736fd0d2019-02-15 16:06:31 +0100795 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200796 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +0200797 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200798
David Sedlákb0faad82019-07-04 14:28:59 +0200799 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
800 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +0200801 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
802 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200803 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100804
David Sedláka7406952019-04-05 10:33:07 +0200805 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +0200806 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, YANG_IMPORT));
David Sedlákda8ffa32019-07-08 14:17:10 +0200807 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200808 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +0200809 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, mod->imports, mod->mod->prefix, &imp->prefix), LY_EVALID);
David Sedlák00250342019-06-21 14:19:39 +0200810
David Sedlákda8ffa32019-07-08 14:17:10 +0200811 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100812}
813
David Sedlák11900c82019-06-18 16:29:12 +0200814LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200815yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200816{
David Sedlák3ffbc522019-07-02 17:49:28 +0200817 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200818 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200819
David Sedlák11900c82019-06-18 16:29:12 +0200820 if (*flags & LYS_STATUS_MASK) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200821 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUPELEM, "status");
David Sedlák11900c82019-06-18 16:29:12 +0200822 return LY_EVALID;
823 }
824
David Sedlák292763b2019-07-09 11:10:53 +0200825 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, YANG_STATUS));
David Sedlák11900c82019-06-18 16:29:12 +0200826 if (strcmp(value, "current") == 0) {
827 *flags |= LYS_STATUS_CURR;
828 } else if (strcmp(value, "deprecated") == 0) {
829 *flags |= LYS_STATUS_DEPRC;
830 } else if (strcmp(value, "obsolete") == 0) {
831 *flags |= LYS_STATUS_OBSLT;
832 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200833 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
834 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +0200835 return LY_EVALID;
836 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200837 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +0200838
David Sedlákda8ffa32019-07-08 14:17:10 +0200839 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +0200840}
841
David Sedlák11900c82019-06-18 16:29:12 +0200842LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +0200843yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
844{
845 struct lysp_when *when;
846 when = calloc(1, sizeof *when);
847 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
848 yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
849 *when_p = when;
850 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
851 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
852 {YANG_CUSTOM, NULL, 0}};
853
854 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
855}
856
857LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200858yin_parse_yin_element_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +0200859 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +0200860{
David Sedlák3ffbc522019-07-02 17:49:28 +0200861 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200862 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +0200863
David Sedlák292763b2019-07-09 11:10:53 +0200864 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_YIN_ELEMENT));
David Sedlák2721d3d2019-06-21 15:37:41 +0200865 if (strcmp(temp_val, "true") == 0) {
866 *flags |= LYS_YINELEM_TRUE;
867 } else if (strcmp(temp_val, "false") == 0) {
868 *flags |= LYS_YINELEM_FALSE;
869 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200870 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
871 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +0200872 return LY_EVALID;
873 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200874 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +0200875
David Sedlákda8ffa32019-07-08 14:17:10 +0200876 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +0200877}
878
879LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200880yin_parse_extension_instance(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, const char *ext_name,
David Sedlák619db942019-07-03 14:47:30 +0200881 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +0200882{
883 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +0200884 char *out;
885 const char *name, *prefix;
886 size_t out_len, prefix_len, name_len;
887 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200888 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +0200889 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
890 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200891
David Sedlákda8ffa32019-07-08 14:17:10 +0200892 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200893
894 e->yin = 0;
895 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +0200896 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +0200897 e->insubstmt = subelem;
898 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200899 e->yin |= LYS_YIN;
900
David Sedlákb1a78352019-06-28 16:16:29 +0200901 /* store attributes as subelements */
902 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
903 if (!iter->prefix) {
904 new_subelem = calloc(1, sizeof(*new_subelem));
905 if (!e->child) {
906 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +0200907 } else {
David Sedlákb1a78352019-06-28 16:16:29 +0200908 last_subelem->next = new_subelem;
909 }
910 last_subelem = new_subelem;
911
912 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +0200913 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
914 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +0200915 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200916 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
917 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +0200918 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200919 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
920 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +0200921 }
922 }
923 }
David Sedlák1e2cdd02019-06-27 14:17:43 +0200924
David Sedlákf250ecf2019-07-01 11:02:05 +0200925 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +0200926 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
927 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +0200928 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200929 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
930 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +0200931 if (!name) {
932 /* end of extension instance reached */
933 break;
934 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200935 LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, prefix, prefix_len, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +0200936 if (!e->child) {
937 e->child = new_subelem;
938 } else {
939 last_subelem->next = new_subelem;
940 }
941 last_subelem = new_subelem;
942 }
David Sedlák555c7202019-07-04 12:14:12 +0200943 } else {
944 /* save text content */
945 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200946 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +0200947 if (!e->argument) {
948 free(out);
949 return LY_EMEM;
950 }
951 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200952 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +0200953 LY_CHECK_RET(!e->argument, LY_EMEM);
954 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200955 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +0200956 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +0200957 }
David Sedlákb1a78352019-06-28 16:16:29 +0200958 }
959
960 return LY_SUCCESS;
961}
962
963LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200964yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix,
David Sedlákf250ecf2019-07-01 11:02:05 +0200965 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +0200966{
967 LY_ERR ret = LY_SUCCESS;
968 const char *temp_prefix, *temp_name;
969 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200970 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +0200971 int dynamic;
972 struct yin_arg_record *subelem_args = NULL;
973 struct lysp_stmt *last = NULL, *new = NULL;
974
975 /* allocate new structure for element */
976 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +0200977 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
978 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +0200979
980 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +0200981 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200982 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +0200983 /* add new element to linked-list */
984 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +0200985 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +0200986 if (!(*element)->child) {
987 /* save first */
988 (*element)->child = new;
989 } else {
990 last->next = new;
991 }
992 last = new;
993
994 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +0200995 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +0200996 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +0200997 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +0200998 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +0200999 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1000 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001001 /* attributes with prefix are ignored */
1002 if (!temp_prefix) {
1003 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001004 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001005 if (!last->arg) {
1006 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001007 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001008 ret = LY_EMEM;
1009 goto err;
1010 }
1011 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001012 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1013 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001014 }
1015 }
1016 }
1017
1018 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001019 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001020 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001021 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001022 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001023 ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &temp_prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +02001024 LY_CHECK_GOTO(ret, err);
1025 if (!name) {
1026 /* end of element reached */
1027 break;
1028 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001029 ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, data, &last->next);
David Sedlákb1a78352019-06-28 16:16:29 +02001030 LY_CHECK_GOTO(ret, err);
1031 last = last->next;
1032 }
1033 } else {
1034 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001035 if (out_len != 0) {
1036 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001037 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001038 if (!(*element)->arg) {
1039 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001040 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001041 ret = LY_EMEM;
1042 goto err;
1043 }
1044 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001045 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1046 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001047 }
David Sedlákb1a78352019-06-28 16:16:29 +02001048 }
1049 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001050 ret = lyxml_get_element(&ctx->xml_ctx, data, &temp_prefix, &prefix_len, &temp_name, &temp_name_len);
David Sedlákb1a78352019-06-28 16:16:29 +02001051 LY_CHECK_GOTO(ret, err);
1052 }
1053
David Sedlákda8ffa32019-07-08 14:17:10 +02001054 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001055 return LY_SUCCESS;
1056
1057err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001058 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001059 return ret;
1060}
1061
1062LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001063yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001064 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001065{
David Sedlák3ffbc522019-07-02 17:49:28 +02001066 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001067 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001068
David Sedlák292763b2019-07-09 11:10:53 +02001069 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, YANG_ARGUMENT));
David Sedlák9494eb22019-06-21 16:06:53 +02001070
David Sedlákda8ffa32019-07-08 14:17:10 +02001071 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001072}
1073
1074LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001075yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, struct lysp_ext **extensions)
David Sedlák11900c82019-06-18 16:29:12 +02001076{
David Sedlák11900c82019-06-18 16:29:12 +02001077 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001078 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001079 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, YANG_EXTENSION));
David Sedlák619db942019-07-03 14:47:30 +02001080
David Sedlák3ffbc522019-07-02 17:49:28 +02001081 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1082 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1083 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1084 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1085 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001086 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001087
David Sedlákda8ffa32019-07-08 14:17:10 +02001088 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001089}
1090
David Sedlák00250342019-06-21 14:19:39 +02001091/**
1092 * @brief Parse module substatements.
1093 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001094 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001095 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001096 * @param[in,out] data Data to read from.
1097 * @param[out] mod Parsed module structure.
1098 *
1099 * @return LY_ERR values.
1100 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001101static LY_ERR
1102yin_parse_mod(struct yin_parser_ctx *ctx, struct yin_arg_record **mod_attrs, const char **data, struct lysp_module **mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001103{
David Sedlákda8ffa32019-07-08 14:17:10 +02001104 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001105 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1106 {YANG_EXTENSION, &(*mod)->exts, 0},
1107 {YANG_IMPORT, *mod, 0},
1108 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1109 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001110 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1111 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001112 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001113
David Sedlák292763b2019-07-09 11:10:53 +02001114 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &(*mod)->mod->name, Y_IDENTIF_ARG, YANG_MODULE));
David Sedlák4a4c0722018-11-26 17:03:10 +01001115
David Sedlákda8ffa32019-07-08 14:17:10 +02001116 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001117}
1118
1119LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001120yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001121{
David Sedláke4889912018-11-02 09:52:40 +01001122 LY_ERR ret = LY_SUCCESS;
1123 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001124 struct lysp_module *mod_p = NULL;
1125 const char *prefix, *name;
1126 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001127
David Sedlák8f7a1172019-06-20 14:42:18 +02001128 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001129
David Sedlákda8ffa32019-07-08 14:17:10 +02001130 struct yin_parser_ctx yin_ctx;
1131
1132 /* initialize context */
1133 memset(&yin_ctx, 0, sizeof yin_ctx);
1134 yin_ctx.xml_ctx.ctx = ctx;
1135 yin_ctx.xml_ctx.line = 1;
1136
David Sedláke4889912018-11-02 09:52:40 +01001137
David Sedlák3017da42019-02-15 09:48:04 +01001138 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001139 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001140 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001141 ret = yin_load_attributes(&yin_ctx, &data, &args);
David Sedlák00250342019-06-21 14:19:39 +02001142 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001143 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +01001144 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001145 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1146 ret = LY_EINVAL;
1147 goto cleanup;
1148 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001149 LOGVAL_PARSER((struct lys_parser_ctx *)&yin_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +02001150 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001151 ret = LY_EVALID;
1152 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001153 }
1154
David Sedlák3017da42019-02-15 09:48:04 +01001155 /* allocate module */
1156 mod_p = calloc(1, sizeof *mod_p);
1157 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1158 mod_p->mod = mod;
1159 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001160
David Sedlák00250342019-06-21 14:19:39 +02001161 /* parse module substatements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001162 ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001163 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001164
David Sedlák3017da42019-02-15 09:48:04 +01001165 mod_p->parsing = 0;
1166 mod->parsed = mod_p;
1167
1168cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001169 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001170 lysp_module_free(mod_p);
1171 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001172 FREE_ARRAY(&yin_ctx, args, free_arg_rec);
1173 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001174 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001175}