blob: eda68c73f9b35d038526ebfd2d252234bdfa74af [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ák5545f5d2019-07-11 11:55:16 +020022#include <errno.h>
David Sedlákf75d55e2019-07-12 16:52:50 +020023#include <ctype.h>
David Sedlákf824ad52018-10-14 23:58:15 +020024
David Sedlákf824ad52018-10-14 23:58:15 +020025#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020026#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020027#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020028#include "tree.h"
29#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020030#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020031#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020032
David Sedlák2b214ac2019-06-06 16:11:03 +020033/**
34 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020035 *
David Sedlák2b214ac2019-06-06 16:11:03 +020036 * @param ns Namespace URI to check.
37 *
38 * @return true if ns equals YIN_NS_URI false otherwise.
39 */
40#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
41
David Sedlákf6251182019-06-06 10:22:13 +020042const char *const yin_attr_list[] = {
43 [YIN_ARG_NAME] = "name",
44 [YIN_ARG_TARGET_NODE] = "target-node",
45 [YIN_ARG_MODULE] = "module",
46 [YIN_ARG_VALUE] = "value",
47 [YIN_ARG_TEXT] = "text",
48 [YIN_ARG_CONDITION] = "condition",
49 [YIN_ARG_URI] = "uri",
50 [YIN_ARG_DATE] = "date",
51 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020052};
53
David Sedlák1bccdfa2019-06-17 15:55:27 +020054enum yang_keyword
David Sedlákc1771b12019-07-10 15:55:46 +020055yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
56 const char *prefix, size_t prefix_len, enum yang_keyword parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020057{
David Sedlák8f7a1172019-06-20 14:42:18 +020058 const char *start = NULL;
59 enum yang_keyword kw = YANG_NONE;
60 const struct lyxml_ns *ns = NULL;
61
62 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020063 return YANG_NONE;
64 }
65
David Sedlákda8ffa32019-07-08 14:17:10 +020066 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020067 if (ns) {
68 if (!IS_YIN_NS(ns->uri)) {
69 return YANG_CUSTOM;
70 }
71 } else {
72 /* elements without namespace are automatically unknown */
73 return YANG_NONE;
74 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020075
David Sedlák8f7a1172019-06-20 14:42:18 +020076 start = name;
77 kw = lysp_match_kw(NULL, &name);
78
79 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020080 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
81 if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) {
82 return YIN_VALUE;
83 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020084 return kw;
85 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020086 if (strncmp(start, "text", name_len) == 0) {
87 return YIN_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +020088 } else {
89 return YANG_NONE;
90 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020091 }
92}
93
David Sedlákc5b20842019-08-13 10:18:31 +020094enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +020095yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020096{
David Sedlákc5b20842019-08-13 10:18:31 +020097 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020098 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020099 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200100
David Sedlák94de2aa2019-02-15 12:42:11 +0100101#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
102#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100103#define IF_ARG_PREFIX_END }
104
David Sedlák1c8b2702019-02-22 11:03:02 +0100105 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100106 case 'c':
107 already_read += 1;
108 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
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 'd':
112 already_read += 1;
113 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200114 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200115
David Sedlák94de2aa2019-02-15 12:42:11 +0100116 case 'm':
117 already_read += 1;
118 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200119 break;
120
David Sedlák94de2aa2019-02-15 12:42:11 +0100121 case 'n':
122 already_read += 1;
123 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200124 break;
125
David Sedlák94de2aa2019-02-15 12:42:11 +0100126 case 't':
127 already_read += 1;
128 IF_ARG_PREFIX("a", 1)
129 IF_ARG("g", 1, YIN_ARG_TAG)
130 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
131 IF_ARG_PREFIX_END
132 else IF_ARG("ext", 3, YIN_ARG_TEXT)
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 'u':
136 already_read += 1;
137 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200138 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200139
David Sedlák94de2aa2019-02-15 12:42:11 +0100140 case 'v':
141 already_read += 1;
142 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200143 break;
144 }
145
David Sedlákc10e7902018-12-17 02:17:59 +0100146 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200147 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200148 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200149 }
150
David Sedlák18730132019-03-15 15:51:34 +0100151#undef IF_ARG
152#undef IF_ARG_PREFIX
153#undef IF_ARG_PREFIX_END
154
David Sedlák872c7b42018-10-26 13:15:20 +0200155 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200156}
157
David Sedlák4f03b932019-07-26 13:01:47 +0200158void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200159 (void)ctx; /* unused */
David Sedlákd2d676a2019-07-22 11:28:19 +0200160 if (record && record->dynamic_content) {
David Sedlák00250342019-06-21 14:19:39 +0200161 free(record->content);
162 }
163}
164
David Sedlák8f7a1172019-06-20 14:42:18 +0200165LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200166yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200167{
168 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200169 struct yin_arg_record *argument_record = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +0200170 const char *prefix, *name;
171 size_t prefix_len, name_len;
David Sedláka7406952019-04-05 10:33:07 +0200172
David Sedlák555c7202019-07-04 12:14:12 +0200173 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200174 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákc5b20842019-08-13 10:18:31 +0200175 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +0200176 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200177
David Sedlákda8ffa32019-07-08 14:17:10 +0200178 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
179 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlákc5b20842019-08-13 10:18:31 +0200180 argument_record->name = name;
181 argument_record->name_len = name_len;
182 argument_record->prefix = prefix;
183 argument_record->prefix_len = prefix_len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200184 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200185 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200186 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200187 }
188 }
189
David Sedlák8f7a1172019-06-20 14:42:18 +0200190cleanup:
191 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200192 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200193 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200194 }
195 return ret;
196}
197
David Sedlák4a650532019-07-10 11:55:18 +0200198LY_ERR
199yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
200{
201 int prefix = 0;
202 unsigned int c;
203 size_t utf8_char_len;
204 size_t already_read = 0;
205 while (already_read < len) {
206 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
207 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
208 already_read += utf8_char_len;
209 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
210
211 switch (val_type) {
212 case Y_IDENTIF_ARG:
213 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
214 break;
215 case Y_PREF_IDENTIF_ARG:
216 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
217 break;
218 case Y_STR_ARG:
219 case Y_MAYBE_STR_ARG:
220 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
221 break;
222 }
223 }
224
225 return LY_SUCCESS;
226}
227
David Sedlákb4e44562019-07-04 15:42:12 +0200228/**
229 * @brief Parse yin argument.
230 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200231 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200232 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200233 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200234 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
235 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200236 * @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 +0200237 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200238 * @param[in] current_element Identification of current element, used for logging.
239 *
240 * @return LY_ERR values.
241 */
242static LY_ERR
David Sedlákc5b20842019-08-13 10:18:31 +0200243yin_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 +0200244 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200245{
David Sedlákc5b20842019-08-13 10:18:31 +0200246 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák8f7a1172019-06-20 14:42:18 +0200247 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200248 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200249
David Sedlák1bccdfa2019-06-17 15:55:27 +0200250 /* validation of attributes */
David Sedlák1f90d252019-07-10 17:09:32 +0200251 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200252 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
253 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200254 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200255 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200256 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200257 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200258 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
David Sedlák292763b2019-07-09 11:10:53 +0200259 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200260 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200261 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200262 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200263 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200264 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200265 /* string is no longer supposed to be freed when the sized array is freed */
266 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200267 } else {
David Sedlák99295322019-07-17 11:34:18 +0200268 if (iter->content_len == 0) {
269 *arg_val = lydict_insert(ctx->xml_ctx.ctx, "", 0);
270 } else {
271 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
272 LY_CHECK_RET(!(*arg_val), LY_EMEM);
273 }
David Sedlák57715b12019-06-17 13:05:22 +0200274 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200275 } else {
David Sedlák1538a842019-08-08 15:38:51 +0200276 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, iter->name_len, iter->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200277 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200278 }
279 }
280 }
281
David Sedlák292763b2019-07-09 11:10:53 +0200282 /* anything else than Y_MAYBE_STR_ARG is mandatory */
283 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200284 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 +0200285 return LY_EVALID;
286 }
287
288 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200289}
290
David Sedlákd6e56892019-07-01 15:40:24 +0200291/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200292 * @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 +0200293 *
294 * @param[in] type Type of wanted record.
295 * @param[in] array_size Size of array.
296 * @param[in] array Searched array.
297 *
298 * @return Pointer to desired record on success, NULL if element is not in the array.
299 */
David Sedlákb4e44562019-07-04 15:42:12 +0200300static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200301get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200302{
David Sedlákb0faad82019-07-04 14:28:59 +0200303 signed char left = 0, right = array_size - 1, middle;
304
305 while (left <= right) {
306 middle = left + (right - left) / 2;
307
308 if (array[middle].type == type) {
309 return &array[middle];
310 }
311
312 if (array[middle].type < type) {
313 left = middle + 1;
314 } else {
315 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200316 }
317 }
318
319 return NULL;
320}
321
David Sedlákbba38e52019-07-09 15:20:01 +0200322/**
323 * @brief Helper function to check mandatory constraint of subelement.
324 *
325 * @param[in,out] ctx Yin parser context for logging and to store current state.
326 * @param[in] subelem_info Array of information about subelements.
327 * @param[in] subelem_info_size Size of subelem_info array.
328 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
329 *
330 * @return LY_ERR values.
331 */
332static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200333yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200334 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200335{
David Sedlákb0faad82019-07-04 14:28:59 +0200336 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200337 /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
David Sedlák21f87cd2019-07-03 16:53:23 +0200338 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200339 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
David Sedlák555c7202019-07-04 12:14:12 +0200340 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200341 return LY_EVALID;
342 }
343 }
344
345 return LY_SUCCESS;
346}
347
David Sedlákbba38e52019-07-09 15:20:01 +0200348/**
349 * @brief Helper function to check "first" constraint of subelement.
350 *
351 * @param[in,out] ctx Yin parser context for logging and to store current state.
352 * @param[in] subelem_info Array of information about subelements.
353 * @param[in] subelem_info_size Size of subelem_info array.
354 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
355 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
356 *
357 * @return LY_ERR values.
358 */
359static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200360yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200361 signed char subelem_info_size, enum yang_keyword current_element,
362 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200363{
David Sedlákb0faad82019-07-04 14:28:59 +0200364 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200365 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200366 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
367 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200368 return LY_EVALID;
369 }
370 }
371
372 return LY_SUCCESS;
373}
374
David Sedlákbba38e52019-07-09 15:20:01 +0200375/**
376 * @brief Helper function to check if array of information about subelements is in ascending order.
377 *
378 * @param[in] subelem_info Array of information about subelements.
379 * @param[in] subelem_info_size Size of subelem_info array.
380 *
381 * @return True iff subelem_info array is in ascending order, False otherwise.
382 */
David Sedlák5545f5d2019-07-11 11:55:16 +0200383#ifndef NDEBUG
David Sedlákbba38e52019-07-09 15:20:01 +0200384static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200385is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
386{
David Sedlák292763b2019-07-09 11:10:53 +0200387 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200388
389 for (signed char i = 0; i < subelem_info_size; ++i) {
390 if (subelem_info[i].type <= current) {
391 return false;
392 }
393 current = subelem_info[i].type;
394 }
395
396 return true;
397}
David Sedlák5545f5d2019-07-11 11:55:16 +0200398#endif
David Sedlákb0faad82019-07-04 14:28:59 +0200399
David Sedlákd6e56892019-07-01 15:40:24 +0200400/**
David Sedlákb4e44562019-07-04 15:42:12 +0200401 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
402 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200403 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200404 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200405 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200406 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200407 * @param[in] kw Type of current element.
408 * @param[out] value Where value of attribute should be stored.
409 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200410 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200411 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200412 *
David Sedlákd6e56892019-07-01 15:40:24 +0200413 * @return LY_ERR values.
414 */
David Sedlákb4e44562019-07-04 15:42:12 +0200415static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200416yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlákc5b20842019-08-13 10:18:31 +0200417 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 +0200418{
David Sedlák1f90d252019-07-10 17:09:32 +0200419 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200420 struct yin_subelement subelems[1] = {
421 {YANG_CUSTOM, NULL, 0}
422 };
David Sedlákb4e44562019-07-04 15:42:12 +0200423
David Sedlákda8ffa32019-07-08 14:17:10 +0200424 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200425}
426
427/**
David Sedlákd3983112019-07-12 11:20:56 +0200428 * @brief Parse pattern element.
429 *
430 * @param[in,out] ctx Yin parser context for logging and to store current state.
431 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
432 * @param[in,out] data Data to read from, always moved to currently handled character.
433 * @param[in,out] patterns Restrictions to add to.
434 *
435 * @return LY_ERR values.
436 */
437static LY_ERR
438yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
439 struct lysp_type *type)
440{
441 const char *real_value = NULL;
442 char *saved_value = NULL;
443 struct lysp_restr *restr;
444
445 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
446 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
447 size_t len = strlen(real_value);
448
449 saved_value = malloc(len + 2);
450 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
451 memmove(saved_value + 1, real_value, len);
452 FREE_STRING(ctx->xml_ctx.ctx, real_value);
453 saved_value[0] = 0x06;
454 saved_value[len + 1] = '\0';
455 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
456 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
457 type->flags |= LYS_SET_PATTERN;
458
459 struct yin_subelement subelems[6] = {
460 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
461 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
462 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
463 {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
464 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
465 {YANG_CUSTOM, NULL, 0}
466 };
467 return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
468}
469
David Sedlákc5b20842019-08-13 10:18:31 +0200470/**
471 * @brief Parse fraction-digits element.
472 *
473 * @param[in,out] ctx Yin parser context for logging and to store current state.
474 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
475 * @param[in,out] data Data to read from, always moved to currently handled character.
476 * @param[in,out] type Type structure to store value, flags and extension instances.
477 *
478 * @return LY_ERR values.
479 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200480static LY_ERR
481yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
482 struct lysp_type *type)
483{
484 const char *temp_val = NULL;
485 char *ptr;
486 unsigned long int num;
487
488 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_FRACTION_DIGITS));
489
490 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200491 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200492 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
493 return LY_EVALID;
494 }
495
496 errno = 0;
497 num = strtoul(temp_val, &ptr, 10);
498 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200499 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200500 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
501 return LY_EVALID;
502 }
503 if ((errno == ERANGE) || (num > 18)) {
David Sedlák1538a842019-08-08 15:38:51 +0200504 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200505 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
506 return LY_EVALID;
507 }
David Sedlák2ab5d8e2019-07-16 11:19:41 +0200508 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200509 type->fraction_digits = num;
510 type->flags |= LYS_SET_FRDIGITS;
511 struct yin_subelement subelems[1] = {
David Sedlákd1144562019-08-06 12:36:14 +0200512 {YANG_CUSTOM, NULL, 0}
David Sedlákf75d55e2019-07-12 16:52:50 +0200513 };
514 return yin_parse_content(ctx, subelems, 1, data, YANG_FRACTION_DIGITS, NULL, &type->exts);
515}
516
David Sedlák07869a52019-07-12 14:28:19 +0200517/**
David Sedlák43801c92019-08-05 15:58:54 +0200518 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200519 *
520 * @param[in,out] ctx YIN parser context for logging and to store current state.
521 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
522 * @param[in,out] data Data to read from, always moved to currently handled character.
523 * @param[in] enum_kw Identification of actual keyword, can be set to YANG_BIT or YANG_ENUM.
David Sedlákc5b20842019-08-13 10:18:31 +0200524 * @param[in,out] type Type structure to store enum value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200525 *
526 * @return LY_ERR values.
527 */
David Sedlákca36c422019-07-12 12:47:55 +0200528static LY_ERR
David Sedlák43801c92019-08-05 15:58:54 +0200529yin_parse_enum(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200530{
David Sedlák07869a52019-07-12 14:28:19 +0200531 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200532
David Sedlák43801c92019-08-05 15:58:54 +0200533 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM);
534 type->flags |= LYS_SET_ENUM;
535 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, YANG_ENUM));
536 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
537 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
538 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200539
540 struct yin_subelement subelems[6] = {
David Sedlák07869a52019-07-12 14:28:19 +0200541 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
542 {YANG_IF_FEATURE, &en->iffeatures, 0},
David Sedlák07869a52019-07-12 14:28:19 +0200543 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
544 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
David Sedlák43801c92019-08-05 15:58:54 +0200545 {YANG_VALUE, en, YIN_SUBELEM_UNIQUE},
David Sedlákca36c422019-07-12 12:47:55 +0200546 {YANG_CUSTOM, NULL, 0}
547 };
David Sedlák43801c92019-08-05 15:58:54 +0200548 return yin_parse_content(ctx, subelems, 6, data, YANG_ENUM, NULL, &en->exts);
549}
550
551/**
552 * @brief Parse bit element.
553 *
554 * @param[in,out] ctx YIN parser context for logging and to store current state.
555 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
556 * @param[in,out] data Data to read from, always moved to currently handled character.
557 * @param[in] enum_kw Identification of actual keyword, can be set to YANG_BIT or YANG_ENUM.
David Sedlákc5b20842019-08-13 10:18:31 +0200558 * @param[in,out] type Type structure to store bit value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200559 *
560 * @return LY_ERR values.
561 */
562static LY_ERR
563yin_parse_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
564 struct lysp_type *type)
565{
566 struct lysp_type_enum *en;
567
568 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->bits, en, LY_EMEM);
569 type->flags |= LYS_SET_BIT;
570 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, YANG_BIT));
571 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
572
573 struct yin_subelement subelems[6] = {
574 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
575 {YANG_IF_FEATURE, &en->iffeatures, 0},
576 {YANG_POSITION, en, YIN_SUBELEM_UNIQUE},
577 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
578 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
579 {YANG_CUSTOM, NULL, 0}
580 };
581 return yin_parse_content(ctx, subelems, 6, data, YANG_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200582}
583
David Sedlákd3983112019-07-12 11:20:56 +0200584/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200585 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
586 * more instances, such as base or if-feature.
587 *
588 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200589 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200590 * @param[in,out] data Data to read from, always moved to currently handled character.
591 * @param[in] kw Type of current element.
592 * @param[out] values Parsed values to add to.
593 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200594 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200595 * @param[in,out] exts Extension instance to add to.
596 *
597 * @return LY_ERR values.
598 */
599static LY_ERR
600yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlákc5b20842019-08-13 10:18:31 +0200601 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 +0200602{
603 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200604 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200605 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200606 struct yin_subelement subelems[1] = {
607 {YANG_CUSTOM, &index, 0}
608 };
609
David Sedlák1f90d252019-07-10 17:09:32 +0200610 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200611
612 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
613}
614
615/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200616 * @brief Parse require instance element.
617 *
618 * @param[in,out] ctx Yin parser context for logging and to store current state.
619 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
620 * @param[in,out] data Data to read from, always moved to currently handled character.
621 * @prama[out] type Type structure to store value, flag and extensions.
622 *
623 * @return LY_ERR values.
624 */
625static LY_ERR
626yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
627 const char **data, struct lysp_type *type)
628{
629 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200630 struct yin_subelement subelems[1] = {
631 {YANG_CUSTOM, NULL, 0}
632 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200633
634 type->flags |= LYS_SET_REQINST;
635 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
636 if (strcmp(temp_val, "true") == 0) {
637 type->require_instance = 1;
638 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák1538a842019-08-08 15:38:51 +0200639 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "require-instance");
David Sedlákcf5569a2019-07-11 13:31:34 +0200640 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
641 return LY_EVALID;
642 }
643 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
644
645 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
646}
647
648/**
David Sedlákce77bf52019-07-11 16:59:31 +0200649 * @brief Parse modifier element.
650 *
651 * @param[in,out] ctx Yin parser context for logging and to store current state.
652 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
653 * @param[in,out] data Data to read from, always moved to currently handled character.
654 * @param[in,out] pat Value to write to.
655 * @param[in,out] exts Extension instances to add to.
656 *
657 * @return LY_ERR values.
658 */
659static LY_ERR
660yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
661 const char **pat, struct lysp_ext_instance **exts)
662{
David Sedlákd3983112019-07-12 11:20:56 +0200663 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200664 const char *temp_val;
665 char *modified_val;
666 struct yin_subelement subelems[1] = {
667 {YANG_CUSTOM, NULL, 0}
668 };
669
670 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER));
671 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák1538a842019-08-08 15:38:51 +0200672 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "modifier");
David Sedlákce77bf52019-07-11 16:59:31 +0200673 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
674 return LY_EVALID;
675 }
David Sedlákd3983112019-07-12 11:20:56 +0200676 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200677
678 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200679 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200680 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200681 strcpy(modified_val, *pat);
682 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200683
684 /* modify the new value */
685 modified_val[0] = 0x15;
686 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
687
688 return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts);
689}
690
691/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200692 * @brief Parse a restriction element (length, range or one instance of must).
693 *
694 * @param[in,out] ctx Yin parser context for logging and to store current state.
695 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
696 * @param[in,out] data Data to read from, always moved to currently handled character.
697 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
698 * @param[in]
699 */
700static LY_ERR
701yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
702 enum yang_keyword restr_kw, struct lysp_restr *restr)
703{
704 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
705 struct yin_subelement subelems[5] = {
David Sedlák968ac342019-07-11 15:17:59 +0200706 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
707 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
708 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
709 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
710 {YANG_CUSTOM, NULL, 0}
711 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200712 /* argument of must is called condition, but argument of length and range is called value */
David Sedlákc5b20842019-08-13 10:18:31 +0200713 enum yin_argument arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200714 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
715
716 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
717}
718
719/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200720 * @brief Parse must element.
721 *
722 * @param[in,out] ctx YIN parser context for logging and to store current state.
723 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
724 * @param[in,out] data Data to read from, always moved to currently handled character.
725 * @param[in,out] restrs Restrictions to add to.
726 *
727 * @return LY_ERR values.
728 */
729static LY_ERR
730yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
731{
732 struct lysp_restr *restr;
733
734 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
735 return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr);
736}
737
738/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200739 * @brief Parse position or value element.
740 *
741 * @param[in,out] ctx YIN parser context for logging and to store current state.
742 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
743 * @param[in,out] data Data to read from, always moved to currently handled character.
744 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
745 * @param[out] enm Enum structure to save value, flags and extensions.
746 *
747 * @return LY_ERR values.
748 */
749static LY_ERR
750yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
751 enum yang_keyword kw, struct lysp_type_enum *enm)
752{
753 assert(kw == YANG_POSITION || kw == YANG_VALUE);
754 const char *temp_val = NULL;
755 char *ptr;
756 long int num;
757 unsigned long int unum;
758
759 /* set value flag */
760 enm->flags |= LYS_SET_VALUE;
761
762 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200763 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlákae5378f2019-07-17 11:37:59 +0200764 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') ||
765 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == YANG_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +0200766 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +0200767 goto error;
768 }
769
770 /* convert value */
771 errno = 0;
772 if (kw == YANG_VALUE) {
773 num = strtol(temp_val, &ptr, 10);
774 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlák1538a842019-08-08 15:38:51 +0200775 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +0200776 goto error;
777 }
778 } else {
779 unum = strtoul(temp_val, &ptr, 10);
780 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +0200781 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +0200782 goto error;
783 }
784 }
785 /* check if whole argument value was converted */
786 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200787 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +0200788 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +0200789 }
790 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +0200791 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +0200792 goto error;
793 }
794 /* save correctly ternary operator can't be used because num and unum have different signes */
795 if (kw == YANG_VALUE) {
796 enm->value = num;
797 } else {
798 enm->value = unum;
799 }
800 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
801
802 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200803 struct yin_subelement subelems[1] = {
804 {YANG_CUSTOM, NULL, 0}
805 };
David Sedlák5545f5d2019-07-11 11:55:16 +0200806 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
807
808 error:
809 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
810 return LY_EVALID;
811}
812
David Sedlák05404f62019-07-24 14:11:53 +0200813
814/**
815 * @brief Parse belongs-to element.
816 *
817 * @param[in] ctx Yin parser context for logging and to store current state.
818 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
819 * @param[in,out] data Data to read from, always moved to currently handled character.
820 * @param[out] submod Structure of submodule that is being parsed.
821 * @param[in,out] exts Extension instances to add to.
822 *
823 * @return LY_ERR values
824 */
825static LY_ERR
826yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
827 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
828{
829 struct yin_subelement subelems[2] = {
830 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
831 {YANG_CUSTOM, NULL, 0}
832 };
833 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, YANG_BELONGS_TO));
834
835 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
836}
837
David Sedlák5545f5d2019-07-11 11:55:16 +0200838/**
David Sedlákc1771b12019-07-10 15:55:46 +0200839 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200840 * text element as child
841 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200842 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákdf2a9732019-08-07 13:23:16 +0200843 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákcf5569a2019-07-11 13:31:34 +0200844 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200845 * @param[in] Type of element can be set to YANG_ORGANIZATION or YANG_CONTACT or YANG_DESCRIPTION or YANG_REFERENCE.
David Sedlákb4e44562019-07-04 15:42:12 +0200846 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200847 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200848 *
849 * @return LY_ERR values.
850 */
851static LY_ERR
David Sedlákdf2a9732019-08-07 13:23:16 +0200852yin_parse_meta_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
853 enum yang_keyword elem_type, const char **value, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +0200854{
855 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
856
David Sedlák968ac342019-07-11 15:17:59 +0200857 struct yin_subelement subelems[2] = {
858 {YANG_CUSTOM, NULL, 0},
859 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
860 };
David Sedlákdf2a9732019-08-07 13:23:16 +0200861 /* check attributes */
862 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
David Sedlákb4e44562019-07-04 15:42:12 +0200863
David Sedlákdf2a9732019-08-07 13:23:16 +0200864 /* parse content */
David Sedlákda8ffa32019-07-08 14:17:10 +0200865 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200866}
867
868/**
David Sedlákc1771b12019-07-10 15:55:46 +0200869 * @brief Parse error-message element.
870 *
871 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákdf2a9732019-08-07 13:23:16 +0200872 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákc1771b12019-07-10 15:55:46 +0200873 * @param[in,out] data Data to read from.
874 * @param[out] value Where the content of error-message element should be stored.
875 * @param[in,out] exts Extension instance to add to.
876 *
877 * @return LY_ERR values.
878 */
879static LY_ERR
David Sedlákdf2a9732019-08-07 13:23:16 +0200880yin_parse_err_msg_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
881 const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +0200882{
David Sedlák968ac342019-07-11 15:17:59 +0200883 struct yin_subelement subelems[2] = {
884 {YANG_CUSTOM, NULL, 0},
885 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
886 };
David Sedlákc1771b12019-07-10 15:55:46 +0200887
David Sedlákdf2a9732019-08-07 13:23:16 +0200888 /* check attributes */
889 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, YANG_ERROR_MESSAGE));
890
David Sedlákc1771b12019-07-10 15:55:46 +0200891 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
892}
893
894/**
David Sedlák374d2b32019-07-17 15:06:55 +0200895 * @brief parse type element.
896 *
897 * @brief Parse position or value element.
898 *
899 * @param[in,out] ctx YIN parser context for logging and to store current state.
900 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
901 * @param[in,out] data Data to read from, always moved to currently handled character.
902 * @param[in,out] type Type to wrote to.
903 * @param[in,out] exts Extension instance to add to.
904 *
905 * @return LY_ERR values.
906 */
907static LY_ERR
908yin_parse_type(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type)
909{
910 struct yin_subelement subelems[11] = {
911 {YANG_BASE, type, 0},
912 {YANG_BIT, type, 0},
913 {YANG_ENUM, type, 0},
914 {YANG_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
915 {YANG_LENGTH, type, YIN_SUBELEM_UNIQUE},
916 {YANG_PATH, type, YIN_SUBELEM_UNIQUE},
917 {YANG_PATTERN, type, 0},
918 {YANG_RANGE, type, YIN_SUBELEM_UNIQUE},
919 {YANG_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
920 {YANG_TYPE, type},
921 {YANG_CUSTOM, NULL, 0},
922 };
923 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, YANG_TYPE));
924 return yin_parse_content(ctx, subelems, 11, data, YANG_TYPE, NULL, &type->exts);
925}
926
David Sedlák1af868e2019-07-17 17:03:14 +0200927/**
928 * @brief Parse max-elements element.
929 *
930 * @param[in,out] ctx YIN parser context for logging and to store current state.
931 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
932 * @param[in,out] data Data to read from, always moved to currently handled character.
933 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +0200934 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +0200935 * @param[in,out] exts Extension instances to add to.
936 *
937 * @return LY_ERR values.
938 */
939static LY_ERR
940yin_parse_maxelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *max,
941 uint16_t *flags, struct lysp_ext_instance **exts)
942{
943 const char *temp_val = NULL;
944 char *ptr;
945 unsigned long int num;
946 struct yin_subelement subelems[1] = {
947 {YANG_CUSTOM, NULL, 0},
948 };
David Sedlák374d2b32019-07-17 15:06:55 +0200949
David Sedlák1af868e2019-07-17 17:03:14 +0200950 *flags |= LYS_SET_MAX;
951 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MAX_ELEMENTS));
952 if (!temp_val || temp_val[0] == '\0' || temp_val[0] == '0' || (temp_val[0] != 'u' && !isdigit(temp_val[0]))) {
David Sedlák1538a842019-08-08 15:38:51 +0200953 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +0200954 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
955 return LY_EVALID;
956 }
957
958 if (strcmp(temp_val, "unbounded")) {
959 errno = 0;
960 num = strtoul(temp_val, &ptr, 10);
961 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200962 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +0200963 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
964 return LY_EVALID;
965 }
966 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +0200967 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +0200968 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
969 return LY_EVALID;
970 }
971 *max = num;
972 }
973 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
974 return yin_parse_content(ctx, subelems, 1, data, YANG_MAX_ELEMENTS, NULL, exts);
975}
David Sedlák374d2b32019-07-17 15:06:55 +0200976
977/**
David Sedlák09e18c92019-07-18 11:17:11 +0200978 * @brief Parse max-elements element.
979 *
980 * @param[in,out] ctx YIN parser context for logging and to store current state.
981 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
982 * @param[in,out] data Data to read from, always moved to currently handled character.
983 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +0200984 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +0200985 * @param[in,out] exts Extension instances to add to.
986 *
987 * @return LY_ERR values.
988 */
989static LY_ERR
990yin_parse_minelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *min,
991 uint16_t *flags, struct lysp_ext_instance **exts)
992{
993 const char *temp_val = NULL;
994 char *ptr;
995 unsigned long int num;
996 struct yin_subelement subelems[1] = {
997 {YANG_CUSTOM, NULL, 0},
998 };
999
1000 *flags |= LYS_SET_MIN;
David Sedláka2dad212019-07-18 12:45:19 +02001001 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MIN_ELEMENTS));
David Sedlák09e18c92019-07-18 11:17:11 +02001002
1003 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) {
David Sedlák1538a842019-08-08 15:38:51 +02001004 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001005 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1006 return LY_EVALID;
1007 }
1008
1009 errno = 0;
1010 num = strtoul(temp_val, &ptr, 10);
1011 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001012 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001013 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1014 return LY_EVALID;
1015 }
1016 if (errno == ERANGE || num > UINT32_MAX) {
David Sedlák1538a842019-08-08 15:38:51 +02001017 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001018 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1019 return LY_EVALID;
1020 }
1021 *min = num;
1022 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001023 return yin_parse_content(ctx, subelems, 1, data, YANG_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001024}
1025
David Sedláka2dad212019-07-18 12:45:19 +02001026/**
1027 * @brief Parse min-elements or max-elements element.
1028 *
1029 * @param[in,out] ctx YIN parser context for logging and to store current state.
1030 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1031 * @param[in,out] data Data to read from, always moved to currently handled character.
1032 * @param[in] parent Identification of parent element.
1033 * @param[in] current Identification of current element.
1034 * @param[in] dest Where the parsed value and flags should be stored.
1035 *
1036 * @return LY_ERR values.
1037 */
David Sedlák09e18c92019-07-18 11:17:11 +02001038static LY_ERR
1039yin_parse_minmax(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1040 enum yang_keyword parent, enum yang_keyword current, void *dest)
1041{
1042 assert(current == YANG_MAX_ELEMENTS || current == YANG_MIN_ELEMENTS);
David Sedlák4ffcec82019-07-25 15:10:21 +02001043 assert(parent == YANG_LEAF_LIST || parent == YANG_REFINE || parent == YANG_LIST || parent == YANG_DEVIATE);
David Sedlák09e18c92019-07-18 11:17:11 +02001044 uint32_t *lim;
1045 uint16_t *flags;
1046 struct lysp_ext_instance **exts;
1047
1048 if (parent == YANG_LEAF_LIST) {
1049 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
1050 flags = &((struct lysp_node_leaflist *)dest)->flags;
1051 exts = &((struct lysp_node_leaflist *)dest)->exts;
1052 } else if (parent == YANG_REFINE) {
1053 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
1054 flags = &((struct lysp_refine *)dest)->flags;
1055 exts = &((struct lysp_refine *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001056 } else if (parent == YANG_LIST) {
David Sedlák09e18c92019-07-18 11:17:11 +02001057 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
1058 flags = &((struct lysp_node_list *)dest)->flags;
1059 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001060 } else {
1061 lim = ((struct minmax_dev_meta *)dest)->lim;
1062 flags = ((struct minmax_dev_meta *)dest)->flags;
1063 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001064 }
1065
1066 if (current == YANG_MAX_ELEMENTS) {
1067 LY_CHECK_RET(yin_parse_maxelements(ctx, attrs, data, lim, flags, exts));
1068 } else {
1069 LY_CHECK_RET(yin_parse_minelements(ctx, attrs, data, lim, flags, exts));
1070 }
1071
1072 return LY_SUCCESS;
1073}
1074
1075/**
David Sedláka2dad212019-07-18 12:45:19 +02001076 * @brief Parser ordered-by element.
1077 *
1078 * @param[in,out] ctx YIN parser context for logging and to store current state.
1079 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1080 * @param[in,out] data Data to read from, always moved to currently handled character.
1081 * @param[out] flags Flags to write to.
1082 * @param[in,out] exts Extension instance to add to.
1083 *
1084 * @return LY_ERR values.
1085 */
1086static LY_ERR
1087yin_parse_orderedby(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1088 uint16_t *flags, struct lysp_ext_instance **exts)
1089{
1090 const char *temp_val;
1091 struct yin_subelement subelems[1] = {
1092 {YANG_CUSTOM, NULL, 0},
1093 };
1094
1095 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_ORDERED_BY));
1096 if (strcmp(temp_val, "system") == 0) {
1097 *flags |= LYS_ORDBY_SYSTEM;
1098 } else if (strcmp(temp_val, "user") == 0) {
1099 *flags |= LYS_ORDBY_USER;
1100 } else {
David Sedlák1538a842019-08-08 15:38:51 +02001101 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "ordered-by");
David Sedláka2dad212019-07-18 12:45:19 +02001102 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1103 return LY_EVALID;
1104 }
1105 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1106
1107 return yin_parse_content(ctx, subelems, 1, data, YANG_ORDERED_BY, NULL, exts);
1108}
1109
1110/**
David Sedlák8a83bbb2019-07-18 14:46:00 +02001111 * @brief parse any-data or any-xml element.
1112 *
1113 * @param[in,out] ctx YIN parser context for logging and to store current state.
1114 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1115 * @param[in,out] data Data to read from, always moved to currently handled character.
1116 * @param[in] any_kw Identification of current element, can be set to YANG_ANY_DATA or YANG_ANY_XML
David Sedlák05404f62019-07-24 14:11:53 +02001117 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001118 *
1119 * @return LY_ERR values.
1120 */
1121static LY_ERR
1122yin_parse_any(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1123 enum yang_keyword any_kw, struct tree_node_meta *node_meta)
1124{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001125 struct lysp_node_anydata *any;
1126
David Sedlák8d552d62019-08-06 15:29:05 +02001127 /* create new sibling */
1128 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, any, next);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001129 any->nodetype = (any_kw == YANG_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
1130 any->parent = node_meta->parent;
1131
David Sedlák8a83bbb2019-07-18 14:46:00 +02001132 /* parser argument */
David Sedlák203ca3a2019-07-18 15:26:25 +02001133 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
David Sedlák8a83bbb2019-07-18 14:46:00 +02001134
1135 struct yin_subelement subelems[9] = {
1136 {YANG_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1137 {YANG_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1138 {YANG_IF_FEATURE, &any->iffeatures, 0},
1139 {YANG_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1140 {YANG_MUST, &any->musts, 0},
1141 {YANG_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1142 {YANG_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1143 {YANG_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1144 {YANG_CUSTOM, NULL, 0},
1145 };
1146 return yin_parse_content(ctx, subelems, 9, data, any_kw, NULL, &any->exts);
1147}
1148
1149/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001150 * @brief parse leaf element.
1151 *
1152 * @param[in,out] ctx YIN parser context for logging and to store current state.
1153 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1154 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001155 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák203ca3a2019-07-18 15:26:25 +02001156 *
1157 * @return LY_ERR values.
1158 */
1159static LY_ERR
1160yin_parse_leaf(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1161 struct tree_node_meta *node_meta)
1162{
David Sedlák203ca3a2019-07-18 15:26:25 +02001163 struct lysp_node_leaf *leaf;
1164
David Sedlák8d552d62019-08-06 15:29:05 +02001165 /* create structure new leaf */
1166 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, leaf, next);
David Sedlák203ca3a2019-07-18 15:26:25 +02001167 leaf->nodetype = LYS_LEAF;
1168 leaf->parent = node_meta->parent;
1169
David Sedlák203ca3a2019-07-18 15:26:25 +02001170 /* parser argument */
1171 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, YANG_LEAF));
1172
1173 /* parse content */
1174 struct yin_subelement subelems[12] = {
1175 {YANG_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1176 {YANG_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1177 {YANG_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1178 {YANG_IF_FEATURE, &leaf->iffeatures, 0},
1179 {YANG_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1180 {YANG_MUST, &leaf->musts, 0},
1181 {YANG_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1182 {YANG_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1183 {YANG_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1184 {YANG_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1185 {YANG_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1186 {YANG_CUSTOM, NULL, 0},
David Sedlák05404f62019-07-24 14:11:53 +02001187 };
David Sedlák203ca3a2019-07-18 15:26:25 +02001188 return yin_parse_content(ctx, subelems, 12, data, YANG_LEAF, NULL, &leaf->exts);
1189}
1190
1191/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001192 * @brief Parse leaf-list element.
1193 *
1194 * @param[in,out] ctx YIN parser context for logging and to store current state.
1195 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1196 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001197 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001198 *
1199 * @return LY_ERR values.
1200 */
1201static LY_ERR
1202yin_parse_leaflist(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1203 struct tree_node_meta *node_meta)
1204{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001205 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001206
David Sedlák8d552d62019-08-06 15:29:05 +02001207 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, llist, next);
1208
David Sedlákc3da3ef2019-07-19 12:56:08 +02001209 llist->nodetype = LYS_LEAFLIST;
1210 llist->parent = node_meta->parent;
1211
David Sedlákc3da3ef2019-07-19 12:56:08 +02001212 /* parse argument */
1213 yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, YANG_LEAF_LIST);
1214
1215 /* parse content */
1216 struct yin_subelement subelems[14] = {
1217 {YANG_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1218 {YANG_DEFAULT, &llist->dflts, 0},
1219 {YANG_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1220 {YANG_IF_FEATURE, &llist->iffeatures, 0},
1221 {YANG_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1222 {YANG_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1223 {YANG_MUST, &llist->musts, 0},
1224 {YANG_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1225 {YANG_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1226 {YANG_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1227 {YANG_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1228 {YANG_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1229 {YANG_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1230 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001231 };
David Sedlákc3da3ef2019-07-19 12:56:08 +02001232 LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, data, YANG_LEAF_LIST, NULL, &llist->exts));
1233
1234 /* invalid combination of subelements */
1235 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001236 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001237 return LY_EVALID;
1238 }
1239 if (llist->max && llist->min > llist->max) {
David Sedlák1538a842019-08-08 15:38:51 +02001240 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001241 return LY_EVALID;
1242 }
1243
1244 return LY_SUCCESS;
1245}
1246
1247/**
David Sedlák04e17b22019-07-19 15:29:48 +02001248 * @brief Parse typedef element.
1249 *
1250 * @param[in,out] ctx YIN parser context for logging and to store current state.
1251 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1252 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001253 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001254 *
1255 * @return LY_ERR values.
1256 */
1257static LY_ERR
1258yin_parse_typedef(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1259 struct typedef_meta *typedef_meta)
1260{
1261 struct lysp_tpdf *tpdf;
1262 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *typedef_meta->typedefs, tpdf, LY_EMEM);
1263
1264 /* parse argument */
1265 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, YANG_TYPEDEF));
1266
1267 /* parse content */
1268 struct yin_subelement subelems[7] = {
1269 {YANG_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1270 {YANG_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1271 {YANG_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1272 {YANG_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1273 {YANG_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1274 {YANG_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1275 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001276 };
David Sedlák04e17b22019-07-19 15:29:48 +02001277 LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, data, YANG_TYPEDEF, NULL, &tpdf->exts));
1278
1279 /* store data for collision check */
1280 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
1281 ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0);
1282 }
1283
1284 return LY_SUCCESS;
1285}
1286
1287/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001288 * @brief Parse refine element.
1289 *
1290 * @param[in,out] ctx YIN parser context for logging and to store current state.
1291 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1292 * @param[in,out] data Data to read from, always moved to currently handled character.
1293 * @param[in,out] refines Refines to add to.
1294 *
1295 * @return LY_ERR values.
1296 */
1297static LY_ERR
1298yin_parse_refine(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1299 struct lysp_refine **refines)
1300{
1301 struct lysp_refine *rf;
1302
1303 /* allocate new refine */
1304 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *refines, rf, LY_EMEM);
1305
1306 /* parse attribute */
1307 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, YANG_REFINE));
1308 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(rf->nodeid), "refine");
1309
1310 /* parse content */
1311 struct yin_subelement subelems[11] = {
1312 {YANG_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
1313 {YANG_DEFAULT, &rf->dflts, 0},
1314 {YANG_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
1315 {YANG_IF_FEATURE, &rf->iffeatures, 0},
1316 {YANG_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1317 {YANG_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1318 {YANG_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1319 {YANG_MUST, &rf->musts, 0},
1320 {YANG_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1321 {YANG_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1322 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001323 };
David Sedlákd2d676a2019-07-22 11:28:19 +02001324 return yin_parse_content(ctx, subelems, 11, data, YANG_REFINE, NULL, &rf->exts);
1325}
1326
1327/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001328 * @brief Parse uses element.
1329 *
1330 * @param[in,out] ctx YIN parser context for logging and to store current state.
1331 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1332 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001333 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001334 *
1335 * @return LY_ERR values.
1336 */
1337static LY_ERR
1338yin_parse_uses(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1339 struct tree_node_meta *node_meta)
1340{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001341 struct lysp_node_uses *uses;
1342
David Sedlák8d552d62019-08-06 15:29:05 +02001343 /* create new uses */
1344 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, uses, next);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001345 uses->nodetype = LYS_USES;
1346 uses->parent = node_meta->parent;
1347
David Sedlák0d6de5a2019-07-22 13:25:44 +02001348 /* parse argument */
1349 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, YANG_USES));
1350
1351 /* parse content */
1352 struct augment_meta augments = {(struct lysp_node *)uses, &uses->augments};
1353 struct yin_subelement subelems[8] = {
1354 {YANG_AUGMENT, &augments, 0},
1355 {YANG_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1356 {YANG_IF_FEATURE, &uses->iffeatures, 0},
1357 {YANG_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1358 {YANG_REFINE, &uses->refines, 0},
1359 {YANG_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1360 {YANG_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1361 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001362 };
David Sedlák0d6de5a2019-07-22 13:25:44 +02001363 LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, data, YANG_USES, NULL, &uses->exts));
1364 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
1365
1366 return LY_SUCCESS;
1367}
1368
1369/**
David Sedlákaa854b02019-07-22 14:17:10 +02001370 * @brief Parse revision element.
1371 *
1372 * @param[in,out] ctx YIN parser context for logging and to store current state.
1373 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1374 * @param[in,out] data Data to read from, always moved to currently handled character.
1375 * @param[in,out] revs Parsed revisions to add to.
1376 *
1377 * @return LY_ERR values.
1378 */
1379static LY_ERR
1380yin_parse_revision(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1381 struct lysp_revision **revs)
1382{
1383 struct lysp_revision *rev;
1384 const char *temp_date = NULL;
1385
1386 /* allocate new reivison */
1387 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *revs, rev, LY_EMEM);
1388
1389 /* parse argument */
1390 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_date, Y_STR_ARG, YANG_REVISION));
1391 /* check value */
1392 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
1393 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1394 return LY_EVALID;
1395 }
1396 strcpy(rev->date, temp_date);
1397 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1398
1399 /* parse content */
1400 struct yin_subelement subelems[3] = {
1401 {YANG_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1402 {YANG_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1403 {YANG_CUSTOM, NULL, 0},
1404 };
1405 return yin_parse_content(ctx, subelems, 3, data, YANG_REVISION, NULL, &rev->exts);
1406}
1407
David Sedlák5e13dea2019-07-22 16:06:45 +02001408/**
1409 * @brief Parse include element.
1410 *
1411 * @param[in,out] ctx YIN parser context for logging and to store current state.
1412 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1413 * @param[in,out] data Data to read from, always moved to currently handled character.
1414 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1415 *
1416 * @return LY_ERR values.
1417 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001418static LY_ERR
1419yin_parse_include(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1420 struct include_meta *inc_meta)
1421{
1422 struct lysp_include *inc;
1423
1424 /* allocate new include */
1425 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *inc_meta->includes, inc, LY_EMEM);
1426
1427 /* parse argument */
1428 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, YANG_INCLUDE));
1429
1430 /* submodules share the namespace with the module names, so there must not be
1431 * a module of the same name in the context, no need for revision matching */
1432 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xml_ctx.ctx, inc->name)) {
David Sedlák1538a842019-08-08 15:38:51 +02001433 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001434 return LY_EVALID;
1435 }
1436
1437 /* parse content */
1438 struct yin_subelement subelems[4] = {
1439 {YANG_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1440 {YANG_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1441 {YANG_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1442 {YANG_CUSTOM, NULL, 0},
1443 };
1444 return yin_parse_content(ctx, subelems, 4, data, YANG_INCLUDE, NULL, &inc->exts);
1445}
1446
David Sedlákaa854b02019-07-22 14:17:10 +02001447/**
David Sedlákdfbbb442019-08-06 16:33:21 +02001448 * @brief Parse revision date element.
1449 *
1450 * @param[in,out] ctx Yin parser context for logging and to store current state.
1451 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of revision-date element.
1452 * @param[in,out] data Data to read from, always moved to currently handled character.
1453 * @param[in,out] rev Array to store the parsed value in.
1454 * @param[in,out] exts Extension instances to add to.
1455 *
1456 * @return LY_ERR values.
1457 */
1458static LY_ERR
1459yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, char *rev,
1460 struct lysp_ext_instance **exts)
1461{
1462 const char *temp_rev;
1463 struct yin_subelement subelems[1] = {
1464 {YANG_CUSTOM, NULL, 0}
1465 };
1466
1467 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
1468 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
1469 FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID);
1470
1471 strcpy(rev, temp_rev);
1472 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
1473
1474 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
1475}
1476
1477/**
1478 * @brief Parse config element.
1479 *
1480 * @param[in] ctx Yin parser context for logging and to store current state.
1481 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1482 * @param[in,out] data Data to read from, always moved to currently handled character.
1483 * @param[in,out] flags Flags to add to.
1484 * @param[in,out] exts Extension instances to add to.
1485 *
1486 * @return LY_ERR values.
1487 */
1488static LY_ERR
1489yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1490 struct lysp_ext_instance **exts)
1491{
1492 const char *temp_val = NULL;
1493 struct yin_subelement subelems[1] = {
1494 {YANG_CUSTOM, NULL, 0}
1495 };
1496
1497 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
1498 if (strcmp(temp_val, "true") == 0) {
1499 *flags |= LYS_CONFIG_W;
1500 } else if (strcmp(temp_val, "false") == 0) {
1501 *flags |= LYS_CONFIG_R;
1502 } else {
David Sedlák1538a842019-08-08 15:38:51 +02001503 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "config");
David Sedlákdfbbb442019-08-06 16:33:21 +02001504 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1505 return LY_EVALID;
1506 }
1507 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1508
1509 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1510}
1511
1512/**
1513 * @brief Parse yang-version element.
1514 *
1515 * @param[in,out] ctx Yin parser context for logging and to store current state.
1516 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yang-version element.
1517 * @param[in] data Data to read from, always moved to currently handled character.
1518 * @param[out] version Storage for the parsed information.
1519 * @param[in,out] exts Extension instance to add to.
1520 *
1521 * @return LY_ERR values.
1522 */
1523static LY_ERR
1524yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1525 struct lysp_ext_instance **exts)
1526{
1527 const char *temp_version = NULL;
1528 struct yin_subelement subelems[1] = {
1529 {YANG_CUSTOM, NULL, 0}
1530 };
1531
1532 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
1533 if (strcmp(temp_version, "1.0") == 0) {
1534 *version = LYS_VERSION_1_0;
1535 } else if (strcmp(temp_version, "1.1") == 0) {
1536 *version = LYS_VERSION_1_1;
1537 } else {
David Sedlák1538a842019-08-08 15:38:51 +02001538 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "value", "yang-version");
David Sedlákdfbbb442019-08-06 16:33:21 +02001539 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1540 return LY_EVALID;
1541 }
1542 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1543 ctx->mod_version = *version;
1544
1545 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1546}
1547
1548/**
1549 * @brief Parse import element.
1550 *
1551 * @param[in,out] ctx Yin parser context for logging and to store current state.
1552 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1553 * @param[in,out] data Data to read from, always moved to currently handled character.
1554 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1555 *
1556 * @return LY_ERR values.
1557 */
1558static LY_ERR
1559yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct import_meta *imp_meta)
1560{
1561 struct lysp_import *imp;
1562 /* allocate new element in sized array for import */
1563 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *imp_meta->imports, imp, LY_EMEM);
1564
1565 struct yin_subelement subelems[5] = {
1566 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1567 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1568 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1569 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1570 {YANG_CUSTOM, NULL, 0}
1571 };
1572
1573 /* parse import attributes */
1574 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, YANG_IMPORT));
1575 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
1576 /* check prefix validity */
1577 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1578
1579 return LY_SUCCESS;
1580}
1581
1582/**
1583 * @brief Parse mandatory element.
1584 *
1585 * @param[in,out] ctx Yin parser context for logging and to store current state.
1586 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element.
1587 * @param[in,out] data Data to read from, always moved to currently handled character.
1588 * @param[in,out] flags Flags to add to.
1589 * @param[in,out] exts Extension instances to add to.
1590 *
1591 * @return LY_ERR values.
1592 */
1593static LY_ERR
1594yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1595 struct lysp_ext_instance **exts)
1596{
1597 const char *temp_val = NULL;
1598 struct yin_subelement subelems[1] = {
1599 {YANG_CUSTOM, NULL, 0}
1600 };
1601
1602 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
1603 if (strcmp(temp_val, "true") == 0) {
1604 *flags |= LYS_MAND_TRUE;
1605 } else if (strcmp(temp_val, "false") == 0) {
1606 *flags |= LYS_MAND_FALSE;
1607 } else {
David Sedlák1538a842019-08-08 15:38:51 +02001608 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "mandatory");
David Sedlákdfbbb442019-08-06 16:33:21 +02001609 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1610 return LY_EVALID;
1611 }
1612 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1613
1614 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1615}
1616
1617/**
1618 * @brief Parse status element.
1619 *
1620 * @param[in,out] ctx Yin parser context for logging and to store current state.
1621 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element.
1622 * @param[in,out] data Data to read from, always moved to currently handled character.
1623 * @param[in,out] flags Flags to add to.
1624 * @param[in,out] exts Extension instances to add to.
1625 *
1626 * @return LY_ERR values.
1627 */
1628static LY_ERR
1629yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1630 struct lysp_ext_instance **exts)
1631{
1632 const char *value = NULL;
1633 struct yin_subelement subelems[1] = {
1634 {YANG_CUSTOM, NULL, 0}
1635 };
1636
1637 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, YANG_STATUS));
1638 if (strcmp(value, "current") == 0) {
1639 *flags |= LYS_STATUS_CURR;
1640 } else if (strcmp(value, "deprecated") == 0) {
1641 *flags |= LYS_STATUS_DEPRC;
1642 } else if (strcmp(value, "obsolete") == 0) {
1643 *flags |= LYS_STATUS_OBSLT;
1644 } else {
David Sedlák1538a842019-08-08 15:38:51 +02001645 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "value", "status");
David Sedlákdfbbb442019-08-06 16:33:21 +02001646 FREE_STRING(ctx->xml_ctx.ctx, value);
1647 return LY_EVALID;
1648 }
1649 FREE_STRING(ctx->xml_ctx.ctx, value);
1650
1651 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
1652}
1653
1654/**
1655 * @brief Parse when element.
1656 *
1657 * @param[in,out] ctx Yin parser context for logging and to store current state.
1658 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of when element.
1659 * @param[in,out] data Data to read from, always moved to currently handled character.
1660 * @param[out] when_p When pointer to parse to.
1661 */
1662static LY_ERR
1663yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1664{
1665 struct lysp_when *when;
1666 when = calloc(1, sizeof *when);
1667 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1668 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
1669 *when_p = when;
1670 struct yin_subelement subelems[3] = {
1671 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1672 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1673 {YANG_CUSTOM, NULL, 0}
1674 };
1675
1676 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1677}
1678
1679/**
1680 * @brief Parse yin-elemenet element.
1681 *
1682 * @param[in,out] ctx Yin parser context for logging and to store current state.
1683 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yin-element element.
1684 * @param[in,out] data Data to read from, always moved to currently handled position.
1685 * @param[in,out] flags Flags to add to.
1686 * @prama[in,out] exts Extension instance to add to.
1687 *
1688 * @return LY_ERR values.
1689 */
1690static LY_ERR
1691yin_parse_yin_element_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1692 uint16_t *flags, struct lysp_ext_instance **exts)
1693{
1694 const char *temp_val = NULL;
1695 struct yin_subelement subelems[1] = {
1696 {YANG_CUSTOM, NULL, 0}
1697 };
1698
1699 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_YIN_ELEMENT));
1700 if (strcmp(temp_val, "true") == 0) {
1701 *flags |= LYS_YINELEM_TRUE;
1702 } else if (strcmp(temp_val, "false") == 0) {
1703 *flags |= LYS_YINELEM_FALSE;
1704 } else {
David Sedlák1538a842019-08-08 15:38:51 +02001705 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "yin-element");
David Sedlákdfbbb442019-08-06 16:33:21 +02001706 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1707 return LY_EVALID;
1708 }
1709 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1710
1711 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
1712}
1713
1714/**
1715 * @brief Parse argument element.
1716 *
1717 * @param[in,out] xml_ctx Xml context.
1718 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of argument element.
1719 * @param[in,out] data Data to read from, always moved to currently handled character.
1720 * @param[in,out] arg_meta Meta information about destionation af prased data.
1721 * @param[in,out] exts Extension instance to add to.
1722 *
1723 * @return LY_ERR values.
1724 */
1725static LY_ERR
1726yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1727 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
1728{
1729 struct yin_subelement subelems[2] = {
1730 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1731 {YANG_CUSTOM, NULL, 0}
1732 };
1733
1734 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, YANG_ARGUMENT));
1735
1736 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
1737}
1738
1739/**
1740 * @brief Parse the extension statement.
1741 *
1742 * @param[in,out] ctx Yin parser context for logging and to store current state.
1743 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension element.
1744 * @param[in,out] data Data to read from.
1745 * @param[in,out] extensions Extensions to add to.
1746 *
1747 * @return LY_ERR values.
1748 */
1749static LY_ERR
1750yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_ext **extensions)
1751{
1752 struct lysp_ext *ex;
1753 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
1754 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, YANG_EXTENSION));
1755
1756 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1757 struct yin_subelement subelems[5] = {
1758 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1759 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1760 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1761 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1762 {YANG_CUSTOM, NULL, 0}
1763 };
1764
1765 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
1766}
1767
1768/**
David Sedlák5e13dea2019-07-22 16:06:45 +02001769 * @brief Parse feature element.
1770 *
1771 * @param[in,out] ctx YIN parser context for logging and to store current state.
1772 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1773 * @param[in,out] data Data to read from, always moved to currently handled character.
1774 * @param[in,out] features Features to add to.
1775 *
1776 * @return LY_ERR values.
1777 */
1778static LY_ERR
1779yin_parse_feature(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1780 struct lysp_feature **features)
1781{
1782 struct lysp_feature *feat;
1783
1784 /* allocate new feature */
1785 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *features, feat, LY_EMEM);
1786
1787 /* parse argument */
1788 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, YANG_FEATURE));
1789
1790 /* parse content */
1791 struct yin_subelement subelems[5] = {
1792 {YANG_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
1793 {YANG_IF_FEATURE, &feat->iffeatures, 0},
1794 {YANG_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
1795 {YANG_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
1796 {YANG_CUSTOM, NULL, 0},
1797 };
1798 return yin_parse_content(ctx, subelems, 5, data, YANG_FEATURE, NULL, &feat->exts);
1799}
1800
1801/**
David Sedlák28794f22019-07-22 16:45:00 +02001802 * @brief Parse identity element.
1803 *
1804 * @param[in,out] ctx YIN parser context for logging and to store current state.
1805 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1806 * @param[in,out] data Data to read from, always moved to currently handled character.
1807 * @param[in,out] identities Identities to add to.
1808 *
1809 * @return LY_ERR values.
1810 */
1811static LY_ERR
1812yin_parse_identity(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1813 struct lysp_ident **identities)
1814{
1815 struct lysp_ident *ident;
1816
1817 /* allocate new identity */
1818 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *identities, ident, LY_EMEM);
1819
1820 /* parse argument */
1821 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, YANG_IDENTITY));
1822
1823 /* parse content */
1824 struct yin_subelement subelems[6] = {
1825 {YANG_BASE, &ident->bases, 0},
1826 {YANG_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
1827 {YANG_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
1828 {YANG_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
1829 {YANG_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
1830 {YANG_CUSTOM, NULL, 0},
1831 };
1832 return yin_parse_content(ctx, subelems, 6, data, YANG_IDENTITY, NULL, &ident->exts);
1833}
1834
1835/**
David Sedlákaf536aa2019-07-23 13:42:23 +02001836 * @brief Parse list element.
1837 *
1838 * @param[in,out] ctx YIN parser context for logging and to store current state.
1839 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1840 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001841 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákaf536aa2019-07-23 13:42:23 +02001842 *
1843 * @return LY_ERR values.
1844 */
1845static LY_ERR
David Sedlákf111bcb2019-07-23 17:15:51 +02001846yin_parse_list(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1847 struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02001848{
David Sedlákaf536aa2019-07-23 13:42:23 +02001849 struct lysp_node_list *list;
1850
David Sedlák8d552d62019-08-06 15:29:05 +02001851 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, list, next);
David Sedlákaf536aa2019-07-23 13:42:23 +02001852 list->nodetype = LYS_LIST;
1853 list->parent = node_meta->parent;
1854
David Sedlákaf536aa2019-07-23 13:42:23 +02001855 /* parse argument */
1856 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, YANG_LIST));
1857
1858 /* parse list content */
David Sedlák85d0eca2019-07-24 15:15:21 +02001859 struct action_meta act_meta = {(struct lysp_node *)list, &list->actions};
David Sedlákaf536aa2019-07-23 13:42:23 +02001860 struct tree_node_meta new_node_meta = {(struct lysp_node *)list, &list->child};
1861 struct typedef_meta typedef_meta = {(struct lysp_node *)list, &list->typedefs};
David Sedlák031b9e72019-07-23 15:19:37 +02001862 struct notif_meta notif_meta = {(struct lysp_node *)list, &list->notifs};
David Sedláke3ce9ef2019-07-23 16:34:30 +02001863 struct grouping_meta gr_meta = {(struct lysp_node *)list, &list->groupings};
David Sedlákaf536aa2019-07-23 13:42:23 +02001864 struct yin_subelement subelems[25] = {
David Sedlák85d0eca2019-07-24 15:15:21 +02001865 {YANG_ACTION, &act_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001866 {YANG_ANYDATA, &new_node_meta, 0},
1867 {YANG_ANYXML, &new_node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001868 {YANG_CHOICE, &new_node_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001869 {YANG_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE},
David Sedlákf111bcb2019-07-23 17:15:51 +02001870 {YANG_CONTAINER, &new_node_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001871 {YANG_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001872 {YANG_GROUPING, &gr_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001873 {YANG_IF_FEATURE, &list->iffeatures, 0},
1874 {YANG_KEY, &list->key, YIN_SUBELEM_UNIQUE},
1875 {YANG_LEAF, &new_node_meta, 0},
1876 {YANG_LEAF_LIST, &new_node_meta, 0},
1877 {YANG_LIST, &new_node_meta, 0},
1878 {YANG_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE},
1879 {YANG_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE},
1880 {YANG_MUST, &list->musts, 0},
David Sedlák031b9e72019-07-23 15:19:37 +02001881 {YANG_NOTIFICATION, &notif_meta, 0},
David Sedlákaf536aa2019-07-23 13:42:23 +02001882 {YANG_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE},
1883 {YANG_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE},
1884 {YANG_STATUS, &list->flags, YIN_SUBELEM_UNIQUE},
1885 {YANG_TYPEDEF, &typedef_meta, 0},
1886 {YANG_UNIQUE, &list->uniques, 0},
1887 {YANG_USES, &new_node_meta, 0},
1888 {YANG_WHEN, &list->when, YIN_SUBELEM_UNIQUE},
1889 {YANG_CUSTOM, NULL, 0},
1890 };
1891 LY_CHECK_RET(yin_parse_content(ctx, subelems, 25, data, YANG_LIST, NULL, &list->exts));
1892
1893 /* finalize parent pointers to the reallocated items */
1894 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
1895
1896 if (list->max && list->min > list->max) {
David Sedlák1538a842019-08-08 15:38:51 +02001897 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02001898 return LY_EVALID;
1899 }
1900
1901 return LY_SUCCESS;
1902}
1903
1904/**
David Sedlák031b9e72019-07-23 15:19:37 +02001905 * @brief Parse notification element.
1906 *
1907 * @param[in,out] ctx YIN parser context for logging and to store current state.
1908 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1909 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001910 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02001911 *
1912 * @return LY_ERR values.
1913 */
1914static LY_ERR
1915yin_parse_notification(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1916 struct notif_meta *notif_meta)
1917{
1918 struct lysp_notif *notif;
1919
1920 /* allocate new notification */
1921 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *notif_meta->notifs, notif, LY_EMEM);
1922 notif->nodetype = LYS_NOTIF;
1923 notif->parent = notif_meta->parent;
1924
1925 /* parse argument */
1926 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, YANG_NOTIFICATION));
1927
1928 /* parse notification content */
1929 struct tree_node_meta node_meta = {(struct lysp_node *)notif, &notif->data};
1930 struct typedef_meta typedef_meta = {(struct lysp_node *)notif, &notif->typedefs};
David Sedláke3ce9ef2019-07-23 16:34:30 +02001931 struct grouping_meta gr_meta = {(struct lysp_node *)notif, &notif->groupings};
David Sedlák031b9e72019-07-23 15:19:37 +02001932 struct yin_subelement subelems[16] = {
1933 {YANG_ANYDATA, &node_meta, 0},
1934 {YANG_ANYXML, &node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001935 {YANG_CHOICE, &node_meta, 0},
David Sedlákf111bcb2019-07-23 17:15:51 +02001936 {YANG_CONTAINER, &node_meta, 0},
David Sedlák031b9e72019-07-23 15:19:37 +02001937 {YANG_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001938 {YANG_GROUPING, &gr_meta, 0},
David Sedlák031b9e72019-07-23 15:19:37 +02001939 {YANG_IF_FEATURE, &notif->iffeatures, 0},
1940 {YANG_LEAF, &node_meta, 0},
1941 {YANG_LEAF_LIST, &node_meta, 0},
1942 {YANG_LIST, &node_meta, 0},
1943 {YANG_MUST, &notif->musts, YIN_SUBELEM_VER2},
1944 {YANG_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE},
1945 {YANG_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE},
1946 {YANG_TYPEDEF, &typedef_meta, 0},
1947 {YANG_USES, &node_meta, 0},
1948 {YANG_CUSTOM, NULL, 0},
1949 };
1950 LY_CHECK_RET(yin_parse_content(ctx, subelems, 16, data, YANG_NOTIFICATION, NULL, &notif->exts));
1951
1952 /* finalize parent pointers to the reallocated items */
1953 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
1954
1955 return LY_SUCCESS;
1956}
1957
1958/**
David Sedláke3ce9ef2019-07-23 16:34:30 +02001959 * @brief Parse notification element.
1960 *
1961 * @param[in,out] ctx YIN parser context for logging and to store current state.
1962 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1963 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001964 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02001965 *
1966 * @return LY_ERR values.
1967 */
1968static LY_ERR
1969yin_parse_grouping(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1970 struct grouping_meta *gr_meta)
1971{
1972 struct lysp_grp *grp;
1973
1974 /* create new grouping */
1975 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *gr_meta->groupings, grp, LY_EMEM);
1976 grp->nodetype = LYS_GROUPING;
1977 grp->parent = gr_meta->parent;
1978
1979 /* parse argument */
1980 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, YANG_GROUPING));
1981
1982 /* parse grouping content */
David Sedlák85d0eca2019-07-24 15:15:21 +02001983 struct action_meta act_meta = {(struct lysp_node *)grp, &grp->actions};
David Sedláke3ce9ef2019-07-23 16:34:30 +02001984 struct tree_node_meta node_meta = {(struct lysp_node *)grp, &grp->data};
1985 struct typedef_meta typedef_meta = {(struct lysp_node *)grp, &grp->typedefs};
1986 struct grouping_meta sub_grouping = {(struct lysp_node *)grp, &grp->groupings};
1987 struct notif_meta notif_meta = {(struct lysp_node *)grp, &grp->notifs};
1988 struct yin_subelement subelems[16] = {
David Sedlák85d0eca2019-07-24 15:15:21 +02001989 {YANG_ACTION, &act_meta, 0},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001990 {YANG_ANYDATA, &node_meta, 0},
1991 {YANG_ANYXML, &node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02001992 {YANG_CHOICE, &node_meta, 0},
David Sedláke3ce9ef2019-07-23 16:34:30 +02001993 {YANG_CONTAINER, &node_meta, 0},
1994 {YANG_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE},
1995 {YANG_GROUPING, &sub_grouping, 0},
1996 {YANG_LEAF, &node_meta, 0},
1997 {YANG_LEAF_LIST, &node_meta, 0},
1998 {YANG_LIST, &node_meta, 0},
1999 {YANG_NOTIFICATION, &notif_meta, 0},
2000 {YANG_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE},
2001 {YANG_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE},
2002 {YANG_TYPEDEF, &typedef_meta, 0},
2003 {YANG_USES, &node_meta, 0},
2004 {YANG_CUSTOM, NULL, 0},
2005 };
2006 LY_CHECK_RET(yin_parse_content(ctx, subelems, 16, data, YANG_GROUPING, NULL, &grp->exts));
2007 /* finalize parent pointers to the reallocated items */
2008 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2009
2010 return LY_SUCCESS;
2011}
2012
2013/**
David Sedlákf111bcb2019-07-23 17:15:51 +02002014 * @brief Parse list element.
2015 *
2016 * @param[in,out] ctx YIN parser context for logging and to store current state.
2017 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2018 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02002019 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákf111bcb2019-07-23 17:15:51 +02002020 *
2021 * @return LY_ERR values.
2022 */
2023static LY_ERR
2024yin_parse_container(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2025 struct tree_node_meta *node_meta)
2026{
David Sedlákf111bcb2019-07-23 17:15:51 +02002027 struct lysp_node_container *cont;
2028
2029 /* create new container */
David Sedlák8d552d62019-08-06 15:29:05 +02002030 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, cont, next);
David Sedlákf111bcb2019-07-23 17:15:51 +02002031 cont->nodetype = LYS_CONTAINER;
2032 cont->parent = node_meta->parent;
2033
David Sedlákf111bcb2019-07-23 17:15:51 +02002034 /* parse aegument */
2035 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, YANG_CONTAINER));
2036
2037 /* parse container content */
David Sedlák85d0eca2019-07-24 15:15:21 +02002038 struct action_meta act_meta = {(struct lysp_node *)cont, &cont->actions};
David Sedlákf111bcb2019-07-23 17:15:51 +02002039 struct tree_node_meta new_node_meta = {(struct lysp_node *)cont, &cont->child};
2040 struct grouping_meta grp_meta = {(struct lysp_node *)cont, &cont->groupings};
2041 struct typedef_meta typedef_meta = {(struct lysp_node *)cont, &cont->typedefs};
2042 struct notif_meta notif_meta = {(struct lysp_node *)cont, &cont->notifs};
2043 struct yin_subelement subelems[21] = {
David Sedlák85d0eca2019-07-24 15:15:21 +02002044 {YANG_ACTION, &act_meta, YIN_SUBELEM_VER2},
David Sedlákf111bcb2019-07-23 17:15:51 +02002045 {YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
2046 {YANG_ANYXML, &new_node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02002047 {YANG_CHOICE, &new_node_meta, 0},
David Sedlákf111bcb2019-07-23 17:15:51 +02002048 {YANG_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE},
2049 {YANG_CONTAINER, &new_node_meta, 0},
2050 {YANG_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE},
2051 {YANG_GROUPING, &grp_meta, 0},
2052 {YANG_IF_FEATURE, &cont->iffeatures, 0},
2053 {YANG_LEAF, &new_node_meta, 0},
2054 {YANG_LEAF_LIST, &new_node_meta, 0},
2055 {YANG_LIST, &new_node_meta, 0},
2056 {YANG_MUST, &cont->musts, 0},
2057 {YANG_NOTIFICATION, &notif_meta, YIN_SUBELEM_VER2},
2058 {YANG_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE},
2059 {YANG_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE},
2060 {YANG_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE},
2061 {YANG_TYPEDEF, &typedef_meta, 0},
2062 {YANG_USES, &new_node_meta, 0},
2063 {YANG_WHEN, &cont->when, YIN_SUBELEM_UNIQUE},
2064 {YANG_CUSTOM, NULL, 0},
2065 };
2066 LY_CHECK_RET(yin_parse_content(ctx, subelems, 21, data, YANG_CONTAINER, NULL, &cont->exts));
2067 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
2068
2069 return LY_SUCCESS;
2070}
2071
2072/**
David Sedlák5379d392019-07-24 10:42:03 +02002073 * @brief Parse case element.
2074 *
2075 * @param[in,out] ctx YIN parser context for logging and to store current state.
2076 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2077 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02002078 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlák5379d392019-07-24 10:42:03 +02002079 *
2080 * @return LY_ERR values.
2081 */
2082static LY_ERR
2083yin_parse_case(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2084 struct tree_node_meta *node_meta)
2085{
David Sedlák5379d392019-07-24 10:42:03 +02002086 struct lysp_node_case *cas;
2087
2088 /* create new case */
David Sedlák8d552d62019-08-06 15:29:05 +02002089 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, cas, next);
David Sedlák5379d392019-07-24 10:42:03 +02002090 cas->nodetype = LYS_CASE;
2091 cas->parent = node_meta->parent;
2092
David Sedlák5379d392019-07-24 10:42:03 +02002093 /* parse argument */
2094 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, YANG_CASE));
2095
2096 /* parse case content */
2097 struct tree_node_meta new_node_meta = {(struct lysp_node *)cas, &cas->child};
2098 struct yin_subelement subelems[14] = {
2099 {YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
2100 {YANG_ANYXML, &new_node_meta, 0},
David Sedlákb7abcfa2019-07-24 12:33:35 +02002101 {YANG_CHOICE, &new_node_meta, 0},
David Sedlák5379d392019-07-24 10:42:03 +02002102 {YANG_CONTAINER, &new_node_meta, 0},
2103 {YANG_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE},
2104 {YANG_IF_FEATURE, &cas->iffeatures, 0},
2105 {YANG_LEAF, &new_node_meta, 0},
2106 {YANG_LEAF_LIST, &new_node_meta, 0},
2107 {YANG_LIST, &new_node_meta, 0},
2108 {YANG_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE},
2109 {YANG_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE},
2110 {YANG_USES, &new_node_meta, 0},
2111 {YANG_WHEN, &cas->when, YIN_SUBELEM_UNIQUE},
2112 {YANG_CUSTOM, NULL, 0},
2113 };
2114 return yin_parse_content(ctx, subelems, 14, data, YANG_CASE, NULL, &cas->exts);
2115}
2116
2117/**
David Sedlák05404f62019-07-24 14:11:53 +02002118 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002119 *
2120 * @param[in,out] ctx YIN parser context for logging and to store current state.
2121 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2122 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02002123 * @param[in] node_meta Meta information about parent node and siblings.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002124 *
2125 * @return LY_ERR values.
2126 */
2127LY_ERR
2128yin_parse_choice(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2129 struct tree_node_meta *node_meta)
2130{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002131 struct lysp_node_choice *choice;
2132
2133 /* create new choice */
David Sedlák8d552d62019-08-06 15:29:05 +02002134 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, choice, next);
2135
David Sedlákb7abcfa2019-07-24 12:33:35 +02002136 choice->nodetype = LYS_CHOICE;
2137 choice->parent = node_meta->parent;
2138
David Sedlákb7abcfa2019-07-24 12:33:35 +02002139 /* parse argument */
2140 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, YANG_CHOICE));
2141
2142 /* parse choice content */
2143 struct tree_node_meta new_node_meta = {(struct lysp_node *)choice, &choice->child};
2144 struct yin_subelement subelems[17] = {
2145 {YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
2146 {YANG_ANYXML, &new_node_meta, 0},
2147 {YANG_CASE, &new_node_meta, 0},
2148 {YANG_CHOICE, &new_node_meta, YIN_SUBELEM_VER2},
2149 {YANG_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE},
2150 {YANG_CONTAINER, &new_node_meta, 0},
2151 {YANG_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE},
2152 {YANG_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE},
2153 {YANG_IF_FEATURE, &choice->iffeatures, 0},
2154 {YANG_LEAF, &new_node_meta, 0},
2155 {YANG_LEAF_LIST, &new_node_meta, 0},
2156 {YANG_LIST, &new_node_meta, 0},
2157 {YANG_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE},
2158 {YANG_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE},
2159 {YANG_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE},
2160 {YANG_WHEN, &choice->when, YIN_SUBELEM_UNIQUE},
2161 {YANG_CUSTOM, NULL, 0},
2162 };
2163 return yin_parse_content(ctx, subelems, 17, data, YANG_CHOICE, NULL, &choice->exts);
2164}
2165
2166/**
David Sedlák05404f62019-07-24 14:11:53 +02002167 * @brief Parse input or output element.
2168 *
2169 * @param[in,out] ctx YIN parser context for logging and to store current state.
2170 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2171 * @param[in,out] data Data to read from, always moved to currently handled character.
2172 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2173 *
2174 * @return LY_ERR values.
2175 */
2176static LY_ERR
2177yin_parse_inout(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword inout_kw,
2178 struct inout_meta *inout_meta)
2179{
2180 /* initiate structure */
2181 inout_meta->inout_p->nodetype = (inout_kw == YANG_INPUT) ? LYS_INPUT : LYS_OUTPUT;
2182 inout_meta->inout_p->parent = inout_meta->parent;
2183
2184 /* check attributes */
2185 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
2186
2187 /* parser input/output content */
2188 struct tree_node_meta node_meta = {(struct lysp_node *)inout_meta->inout_p, &inout_meta->inout_p->data};
2189 struct grouping_meta grp_meta = {(struct lysp_node *)inout_meta->inout_p, &inout_meta->inout_p->groupings};
2190 struct typedef_meta typedef_meta = {(struct lysp_node *)inout_meta->inout_p, &inout_meta->inout_p->typedefs};
2191 struct yin_subelement subelems[12] = {
2192 {YANG_ANYDATA, &node_meta, YIN_SUBELEM_VER2},
2193 {YANG_ANYXML, &node_meta, 0},
2194 {YANG_CHOICE, &node_meta, 0},
2195 {YANG_CONTAINER, &node_meta, 0},
2196 {YANG_GROUPING, &grp_meta, 0},
2197 {YANG_LEAF, &node_meta, 0},
2198 {YANG_LEAF_LIST, &node_meta, 0},
2199 {YANG_LIST, &node_meta, 0},
2200 {YANG_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2},
2201 {YANG_TYPEDEF, &typedef_meta, 0},
2202 {YANG_USES, &node_meta, 0},
2203 {YANG_CUSTOM, NULL, 0},
2204 };
2205 LY_CHECK_RET(yin_parse_content(ctx, subelems, 12, data, inout_kw, NULL, &inout_meta->inout_p->exts));
2206
2207 /* finalize parent pointers to the reallocated items */
2208 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings,
2209 NULL, NULL, NULL));
2210
2211 return LY_SUCCESS;
2212}
2213
David Sedlák992fb7c2019-07-24 16:51:01 +02002214/**
2215 * @brief Parse action element.
2216 *
2217 * @param[in,out] ctx YIN parser context for logging and to store current state.
2218 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2219 * @param[in,out] data Data to read from, always moved to currently handled character.
2220 * @param[in] act_meta Meta information about parent node and actions to add to.
2221 *
2222 * @return LY_ERR values.
2223 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002224static LY_ERR
2225yin_parse_action(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2226 struct action_meta *act_meta)
2227{
2228 struct lysp_action *act;
2229
2230 /* create new action */
2231 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *act_meta->actions, act, LY_EMEM);
2232 act->nodetype = LYS_ACTION;
2233 act->parent = act_meta->parent;
2234
2235 /* parse argument */
2236 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, YANG_ACTION));
2237
2238 /* parse content */
2239 struct grouping_meta grp_meta = {(struct lysp_node *)act, &act->groupings};
2240 struct typedef_meta typedef_meta = {(struct lysp_node *)act, &act->typedefs};
2241 struct inout_meta input = {(struct lysp_node *)act, &act->input};
2242 struct inout_meta output = {(struct lysp_node *)act, &act->output};
2243 struct yin_subelement subelems[9] = {
2244 {YANG_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE},
2245 {YANG_GROUPING, &grp_meta, 0},
2246 {YANG_IF_FEATURE, &act->iffeatures, 0},
2247 {YANG_INPUT, &input, YIN_SUBELEM_UNIQUE},
2248 {YANG_OUTPUT, &output, YIN_SUBELEM_UNIQUE},
2249 {YANG_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE},
2250 {YANG_STATUS, &act->flags, YIN_SUBELEM_UNIQUE},
2251 {YANG_TYPEDEF, &typedef_meta, 0},
2252 {YANG_CUSTOM, NULL, 0},
2253 };
2254 LY_CHECK_RET(yin_parse_content(ctx, subelems, 9, data, YANG_ACTION, NULL, &act->exts));
2255 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
2256
2257 return LY_SUCCESS;
2258}
2259
David Sedlák05404f62019-07-24 14:11:53 +02002260/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002261 * @brief Parse augment element.
2262 *
2263 * @param[in,out] ctx YIN parser context for logging and to store current state.
2264 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2265 * @param[in,out] data Data to read from, always moved to currently handled character.
2266 * @param[in] aug_meta Meta information about parent node and augments to add to.
2267 *
2268 * @return LY_ERR values.
2269 */
2270static LY_ERR
2271yin_parse_augment(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2272 struct augment_meta *aug_meta)
2273{
2274 struct lysp_augment *aug;
2275
2276 /* create new augment */
2277 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *aug_meta->augments, aug, LY_EMEM);
2278 aug->nodetype = LYS_AUGMENT;
2279 aug->parent = aug_meta->parent;
2280
2281 /* parse argument */
2282 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, YANG_AUGMENT));
2283 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
2284
2285 /* parser augment content */
2286 struct action_meta act_meta = {(struct lysp_node *)aug, &aug->actions};
2287 struct tree_node_meta node_meta = {(struct lysp_node *)aug, &aug->child};
2288 struct notif_meta notif_meta = {(struct lysp_node *)aug, &aug->notifs};
2289 struct yin_subelement subelems[17] = {
2290 {YANG_ACTION, &act_meta, YIN_SUBELEM_VER2},
2291 {YANG_ANYDATA, &node_meta, YIN_SUBELEM_VER2},
2292 {YANG_ANYXML, &node_meta, 0},
2293 {YANG_CASE, &node_meta, 0},
2294 {YANG_CHOICE, &node_meta, 0},
2295 {YANG_CONTAINER, &node_meta, 0},
2296 {YANG_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE},
2297 {YANG_IF_FEATURE, &aug->iffeatures, 0},
2298 {YANG_LEAF, &node_meta, 0},
2299 {YANG_LEAF_LIST, &node_meta, 0},
2300 {YANG_LIST, &node_meta, 0},
2301 {YANG_NOTIFICATION, &notif_meta, YIN_SUBELEM_VER2},
2302 {YANG_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE},
2303 {YANG_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE},
2304 {YANG_USES, &node_meta, 0},
2305 {YANG_WHEN, &aug->when, YIN_SUBELEM_UNIQUE},
2306 {YANG_CUSTOM, NULL, 0},
2307 };
2308 LY_CHECK_RET(yin_parse_content(ctx, subelems, 17, data, YANG_AUGMENT, NULL, &aug->exts));
2309
2310 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
2311
2312 return LY_SUCCESS;
2313}
2314
David Sedlák8b754462019-07-25 16:22:13 +02002315/**
2316 * @brief Parse deviate element.
2317 *
2318 * @param[in,out] ctx YIN parser context for logging and to store current state.
2319 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2320 * @param[in,out] data Data to read from, always moved to currently handled character.
2321 * @param[in] deviates Deviates to add to.
2322 *
2323 * @return LY_ERR values.
2324 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002325static LY_ERR
2326yin_parse_deviate(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2327 struct lysp_deviate **deviates)
2328{
2329 LY_ERR ret = LY_SUCCESS;
2330 uint8_t dev_mod;
2331 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002332 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002333 struct lysp_deviate_add *d_add = NULL;
2334 struct lysp_deviate_rpl *d_rpl = NULL;
2335 struct lysp_deviate_del *d_del = NULL;
2336
2337 /* parse argument */
2338 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_DEVIATE));
2339
2340 if (strcmp(temp_val, "not-supported") == 0) {
2341 dev_mod = LYS_DEV_NOT_SUPPORTED;
2342 } else if (strcmp(temp_val, "add") == 0) {
2343 dev_mod = LYS_DEV_ADD;
2344 } else if (strcmp(temp_val, "replace") == 0) {
2345 dev_mod = LYS_DEV_REPLACE;
2346 } else if (strcmp(temp_val, "delete") == 0) {
2347 dev_mod = LYS_DEV_DELETE;
2348 } else {
David Sedlák1538a842019-08-08 15:38:51 +02002349 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "deviate");
David Sedlák4ffcec82019-07-25 15:10:21 +02002350 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2351 return LY_EVALID;
2352 }
2353 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2354
2355 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2356 d = calloc(1, sizeof *d);
2357 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2358 struct yin_subelement subelems[1] = {
2359 {YANG_CUSTOM, NULL, 0}
2360 };
2361 ret = yin_parse_content(ctx, subelems, 1, data, YANG_DEVIATE, NULL, &d->exts);
2362
2363 } else if (dev_mod == LYS_DEV_ADD) {
2364 d_add = calloc(1, sizeof *d_add);
2365 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2366 d = (struct lysp_deviate *)d_add;
2367 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2368 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
2369 struct yin_subelement subelems[9] = {
2370 {YANG_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
2371 {YANG_DEFAULT, &d_add->dflts, 0},
2372 {YANG_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2373 {YANG_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2374 {YANG_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2375 {YANG_MUST, &d_add->musts, 0},
2376 {YANG_UNIQUE, &d_add->uniques, 0},
2377 {YANG_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2378 {YANG_CUSTOM, NULL, 0},
2379 };
2380 ret = yin_parse_content(ctx, subelems, 9, data, YANG_DEVIATE, NULL, &d_add->exts);
2381
2382 } else if (dev_mod == LYS_DEV_REPLACE) {
2383 d_rpl = calloc(1, sizeof *d_rpl);
2384 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2385 d = (struct lysp_deviate *)d_rpl;
2386 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2387 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
2388 struct yin_subelement subelems[8] = {
2389 {YANG_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2390 {YANG_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
2391 {YANG_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2392 {YANG_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2393 {YANG_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2394 {YANG_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2395 {YANG_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2396 {YANG_CUSTOM, NULL, 0},
2397 };
2398 ret = yin_parse_content(ctx, subelems, 8, data, YANG_DEVIATE, NULL, &d_rpl->exts);
2399
2400 } else {
2401 d_del = calloc(1, sizeof *d_del);
2402 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2403 d = (struct lysp_deviate *)d_del;
2404 struct yin_subelement subelems[5] = {
2405 {YANG_DEFAULT, &d_del->dflts, 0},
2406 {YANG_MUST, &d_del->musts, 0},
2407 {YANG_UNIQUE, &d_del->uniques, 0},
2408 {YANG_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2409 {YANG_CUSTOM, NULL, 0},
2410 };
2411 ret = yin_parse_content(ctx, subelems, 5, data, YANG_DEVIATE, NULL, &d_del->exts);
2412 }
2413 LY_CHECK_GOTO(ret, cleanup);
2414
2415 d->mod = dev_mod;
2416 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002417 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002418
2419 return ret;
2420
2421cleanup:
2422 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002423 return ret;
2424}
2425
David Sedlák992fb7c2019-07-24 16:51:01 +02002426/**
David Sedlák8b754462019-07-25 16:22:13 +02002427 * @brief Parse deviation element.
2428 *
2429 * @param[in,out] ctx YIN parser context for logging and to store current state.
2430 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2431 * @param[in,out] data Data to read from, always moved to currently handled character.
2432 * @param[in] deviations Deviations to add to.
2433 *
2434 * @return LY_ERR values.
2435 */
2436static LY_ERR
2437yin_parse_deviation(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2438 struct lysp_deviation **deviations)
2439{
2440 struct lysp_deviation *dev;
2441
2442 /* create new deviation */
2443 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *deviations, dev, LY_EMEM);
2444
2445 /* parse argument */
David Sedlák1538a842019-08-08 15:38:51 +02002446 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, YANG_DEVIATION));
David Sedlák8b754462019-07-25 16:22:13 +02002447 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
2448 struct yin_subelement subelems[4] = {
2449 {YANG_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2450 {YANG_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2451 {YANG_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2452 {YANG_CUSTOM, NULL, 0},
2453 };
David Sedlák1538a842019-08-08 15:38:51 +02002454 return yin_parse_content(ctx, subelems, 4, data, YANG_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002455}
2456
2457/**
David Sedlákb4e44562019-07-04 15:42:12 +02002458 * @brief Map keyword type to substatement info.
2459 *
2460 * @param[in] kw Keyword type.
2461 *
2462 * @return correct LYEXT_SUBSTMT information.
2463 */
2464static LYEXT_SUBSTMT
2465kw2lyext_substmt(enum yang_keyword kw)
2466{
2467 switch (kw) {
2468 case YANG_ARGUMENT:
2469 return LYEXT_SUBSTMT_ARGUMENT;
2470 case YANG_BASE:
2471 return LYEXT_SUBSTMT_BASE;
2472 case YANG_BELONGS_TO:
2473 return LYEXT_SUBSTMT_BELONGSTO;
2474 case YANG_CONTACT:
2475 return LYEXT_SUBSTMT_CONTACT;
2476 case YANG_DEFAULT:
2477 return LYEXT_SUBSTMT_DEFAULT;
2478 case YANG_DESCRIPTION:
2479 return LYEXT_SUBSTMT_DESCRIPTION;
2480 case YANG_ERROR_APP_TAG:
2481 return LYEXT_SUBSTMT_ERRTAG;
2482 case YANG_ERROR_MESSAGE:
2483 return LYEXT_SUBSTMT_ERRMSG;
2484 case YANG_KEY:
2485 return LYEXT_SUBSTMT_KEY;
2486 case YANG_NAMESPACE:
2487 return LYEXT_SUBSTMT_NAMESPACE;
2488 case YANG_ORGANIZATION:
2489 return LYEXT_SUBSTMT_ORGANIZATION;
2490 case YANG_PATH:
2491 return LYEXT_SUBSTMT_PATH;
2492 case YANG_PREFIX:
2493 return LYEXT_SUBSTMT_PREFIX;
2494 case YANG_PRESENCE:
2495 return LYEXT_SUBSTMT_PRESENCE;
2496 case YANG_REFERENCE:
2497 return LYEXT_SUBSTMT_REFERENCE;
2498 case YANG_REVISION_DATE:
2499 return LYEXT_SUBSTMT_REVISIONDATE;
2500 case YANG_UNITS:
2501 return LYEXT_SUBSTMT_UNITS;
2502 case YANG_VALUE:
2503 return LYEXT_SUBSTMT_VALUE;
2504 case YANG_YANG_VERSION:
2505 return LYEXT_SUBSTMT_VERSION;
2506 case YANG_MODIFIER:
2507 return LYEXT_SUBSTMT_MODIFIER;
2508 case YANG_REQUIRE_INSTANCE:
2509 return LYEXT_SUBSTMT_REQINSTANCE;
2510 case YANG_YIN_ELEMENT:
2511 return LYEXT_SUBSTMT_YINELEM;
2512 case YANG_CONFIG:
2513 return LYEXT_SUBSTMT_CONFIG;
2514 case YANG_MANDATORY:
2515 return LYEXT_SUBSTMT_MANDATORY;
2516 case YANG_ORDERED_BY:
2517 return LYEXT_SUBSTMT_ORDEREDBY;
2518 case YANG_STATUS:
2519 return LYEXT_SUBSTMT_STATUS;
2520 case YANG_FRACTION_DIGITS:
2521 return LYEXT_SUBSTMT_FRACDIGITS;
2522 case YANG_MAX_ELEMENTS:
2523 return LYEXT_SUBSTMT_MAX;
2524 case YANG_MIN_ELEMENTS:
2525 return LYEXT_SUBSTMT_MIN;
2526 case YANG_POSITION:
2527 return LYEXT_SUBSTMT_POSITION;
2528 case YANG_UNIQUE:
2529 return LYEXT_SUBSTMT_UNIQUE;
2530 case YANG_IF_FEATURE:
2531 return LYEXT_SUBSTMT_IFFEATURE;
2532 default:
2533 return LYEXT_SUBSTMT_SELF;
2534 }
2535}
2536
David Sedlákc5b20842019-08-13 10:18:31 +02002537/**
2538 * @brief map keyword to keyword-group.
2539 *
2540 * @param[in] ctx YIN parser context used for logging.
2541 * @param[in] kw Keyword that is child of module or submodule.
2542 * @param[out] group Group of keyword.
2543 *
2544 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2545 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002546static LY_ERR
2547kw2kw_group(struct yin_parser_ctx *ctx, enum yang_keyword kw, enum yang_module_stmt *group)
2548{
2549 switch (kw) {
2550 /* module header */
2551 case YANG_NONE:
2552 case YANG_NAMESPACE:
2553 case YANG_PREFIX:
2554 case YANG_BELONGS_TO:
2555 case YANG_YANG_VERSION:
2556 *group = Y_MOD_MODULE_HEADER;
2557 break;
2558 /* linkage */
2559 case YANG_INCLUDE:
2560 case YANG_IMPORT:
2561 *group = Y_MOD_LINKAGE;
2562 break;
2563 /* meta */
2564 case YANG_ORGANIZATION:
2565 case YANG_CONTACT:
2566 case YANG_DESCRIPTION:
2567 case YANG_REFERENCE:
2568 *group = Y_MOD_META;
2569 break;
2570 /* revision */
2571 case YANG_REVISION:
2572 *group = Y_MOD_REVISION;
2573 break;
2574 /* body */
2575 case YANG_ANYDATA:
2576 case YANG_ANYXML:
2577 case YANG_AUGMENT:
2578 case YANG_CHOICE:
2579 case YANG_CONTAINER:
2580 case YANG_DEVIATION:
2581 case YANG_EXTENSION:
2582 case YANG_FEATURE:
2583 case YANG_GROUPING:
2584 case YANG_IDENTITY:
2585 case YANG_LEAF:
2586 case YANG_LEAF_LIST:
2587 case YANG_LIST:
2588 case YANG_NOTIFICATION:
2589 case YANG_RPC:
2590 case YANG_TYPEDEF:
2591 case YANG_USES:
2592 case YANG_CUSTOM:
2593 *group = Y_MOD_BODY;
2594 break;
2595 default:
2596 LOGINT(ctx->xml_ctx.ctx);
2597 return LY_EINT;
2598 }
2599
2600 return LY_SUCCESS;
2601}
2602
David Sedlákc5b20842019-08-13 10:18:31 +02002603/**
2604 * @brief Check if relative order of two keywords is valid.
2605 *
2606 * @param[in] ctx YIN parser context used for logging.
2607 * @param[in] kw Current keyword.
2608 * @param[in] next_kw Next keyword.
2609 * @param[in] parrent Identification of parrent element, can be se to to YANG_MODULE of YANG_SUBMODULE,
2610 * because relative order is required only in module and submodule sub-elements, used for logging.
2611 *
2612 * @return LY_SUCCESS on succes and LY_EVALID if relative order is invalid.
2613 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002614static LY_ERR
2615yin_check_relative_order(struct yin_parser_ctx *ctx, enum yang_keyword kw, enum yang_keyword next_kw, enum yang_keyword parrent)
2616{
2617 assert(parrent == YANG_MODULE || parrent == YANG_SUBMODULE);
2618 enum yang_module_stmt gr, next_gr;
2619
2620 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2621 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2622
2623 if (gr > next_gr) {
2624 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2625 return LY_EVALID;
2626 }
2627
2628 return LY_SUCCESS;
2629}
2630
David Sedlákd6e56892019-07-01 15:40:24 +02002631LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02002632yin_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 +02002633 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 +02002634{
2635 LY_ERR ret = LY_SUCCESS;
David Sedlák8e7bda82019-07-16 17:57:50 +02002636 char *out = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +02002637 const char *prefix, *name;
2638 size_t out_len = 0, prefix_len, name_len;
David Sedlák8e7bda82019-07-16 17:57:50 +02002639 int dynamic = 0;
David Sedlák1af868e2019-07-17 17:03:14 +02002640 struct yin_arg_record *attrs = NULL;
David Sedláke6cd89e2019-08-07 12:46:02 +02002641 enum yang_keyword kw = YANG_NONE, last_kw = YANG_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02002642 struct yin_subelement *subelem = NULL;
David Sedlák374d2b32019-07-17 15:06:55 +02002643 struct lysp_type *type, *nested_type;
David Sedlák09e18c92019-07-18 11:17:11 +02002644
David Sedlákb0faad82019-07-04 14:28:59 +02002645 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +02002646
David Sedlákda8ffa32019-07-08 14:17:10 +02002647 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
2648 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +02002649 /* current element has subelements as content */
2650 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002651 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákc5b20842019-08-13 10:18:31 +02002652 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlákd6e56892019-07-01 15:40:24 +02002653 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc5b20842019-08-13 10:18:31 +02002654 if (!name) {
David Sedlákd6e56892019-07-01 15:40:24 +02002655 /* end of current element reached */
2656 break;
2657 }
David Sedlák1af868e2019-07-17 17:03:14 +02002658 ret = yin_load_attributes(ctx, data, &attrs);
David Sedlákd6e56892019-07-01 15:40:24 +02002659 LY_CHECK_GOTO(ret, cleanup);
David Sedláke6cd89e2019-08-07 12:46:02 +02002660 last_kw = kw;
David Sedlákc5b20842019-08-13 10:18:31 +02002661 kw = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +02002662
2663 /* check if this element can be child of current element */
David Sedlák1af868e2019-07-17 17:03:14 +02002664 subelem = get_record(kw, subelem_info_size, subelem_info);
2665 if (!subelem) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002666 if (current_element == YANG_DEVIATE && isdevsub(kw)) {
2667 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
2668 } else {
David Sedlákc5b20842019-08-13 10:18:31 +02002669 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(current_element));
David Sedlák4ffcec82019-07-25 15:10:21 +02002670 }
David Sedlákd6e56892019-07-01 15:40:24 +02002671 ret = LY_EVALID;
2672 goto cleanup;
2673 }
2674
David Sedláke6cd89e2019-08-07 12:46:02 +02002675 if (current_element == YANG_MODULE || current_element == YANG_SUBMODULE) {
2676 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
2677 LY_CHECK_GOTO(ret, cleanup);
2678 }
David Sedlák5f8191e2019-07-08 16:35:52 +02002679
David Sedlák4ffcec82019-07-25 15:10:21 +02002680 /* flag check */
David Sedlák1af868e2019-07-17 17:03:14 +02002681 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002682 /* subelement uniquenes */
David Sedlák1538a842019-08-08 15:38:51 +02002683 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +02002684 return LY_EVALID;
2685 }
David Sedlák1af868e2019-07-17 17:03:14 +02002686 if (subelem->flags & YIN_SUBELEM_FIRST) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002687 /* subelement is supposed to be defined as first subelement */
David Sedlák1af868e2019-07-17 17:03:14 +02002688 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
David Sedlák66d7c842019-07-11 15:06:04 +02002689 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +02002690 }
David Sedlák0c2bab92019-07-22 15:33:19 +02002691 if (subelem->flags & YIN_SUBELEM_VER2) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002692 /* subelement is supported only in version 1.1 or higher */
David Sedlák0c2bab92019-07-22 15:33:19 +02002693 if (ctx->mod_version < 2) {
David Sedlák1538a842019-08-08 15:38:51 +02002694 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák9bb1c042019-07-22 16:45:37 +02002695 ret = LY_EVALID;
2696 goto cleanup;
David Sedlák0c2bab92019-07-22 15:33:19 +02002697 }
2698 }
David Sedlák4ffcec82019-07-25 15:10:21 +02002699 /* note that element was parsed for easy uniqueness check in next iterations */
David Sedlák1af868e2019-07-17 17:03:14 +02002700 subelem->flags |= YIN_SUBELEM_PARSED;
David Sedlák21f87cd2019-07-03 16:53:23 +02002701
David Sedlákd6e56892019-07-01 15:40:24 +02002702 switch (kw) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002703 /* call responsible function */
David Sedlákd6e56892019-07-01 15:40:24 +02002704 case YANG_CUSTOM:
David Sedlákc5b20842019-08-13 10:18:31 +02002705 ret = yin_parse_extension_instance(ctx, attrs, data, name2fullname(name, prefix_len),
2706 namelen2fulllen(name_len, prefix_len),
David Sedlák1af868e2019-07-17 17:03:14 +02002707 kw2lyext_substmt(current_element),
2708 (subelem->dest) ? *((uint32_t*)subelem->dest) : 0, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002709 break;
2710 case YANG_ACTION:
David Sedlákeaa45792019-07-24 15:25:01 +02002711 case YANG_RPC:
David Sedlák85d0eca2019-07-24 15:15:21 +02002712 ret = yin_parse_action(ctx, attrs, data, (struct action_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002713 break;
2714 case YANG_ANYDATA:
David Sedlákd6e56892019-07-01 15:40:24 +02002715 case YANG_ANYXML:
David Sedlák8a83bbb2019-07-18 14:46:00 +02002716 ret = yin_parse_any(ctx, attrs, data, kw, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002717 break;
2718 case YANG_ARGUMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02002719 ret = yin_parse_argument_element(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002720 break;
2721 case YANG_AUGMENT:
David Sedlák992fb7c2019-07-24 16:51:01 +02002722 ret = yin_parse_augment(ctx, attrs, data, (struct augment_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002723 break;
2724 case YANG_BASE:
David Sedláka62750b2019-07-16 11:21:31 +02002725 if (current_element == YANG_TYPE) {
David Sedlák1af868e2019-07-17 17:03:14 +02002726 type = (struct lysp_type *)subelem->dest;
2727 ret = yin_parse_simple_elements(ctx, attrs, data, kw, &type->bases, YIN_ARG_NAME,
David Sedlák292763b2019-07-09 11:10:53 +02002728 Y_PREF_IDENTIF_ARG, exts);
2729 type->flags |= LYS_SET_BASE;
David Sedláka62750b2019-07-16 11:21:31 +02002730 } else if (current_element == YANG_IDENTITY) {
David Sedlák1af868e2019-07-17 17:03:14 +02002731 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
David Sedlák292763b2019-07-09 11:10:53 +02002732 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
2733 } else {
2734 LOGINT(ctx->xml_ctx.ctx);
2735 ret = LY_EINT;
2736 }
David Sedlákd6e56892019-07-01 15:40:24 +02002737 break;
2738 case YANG_BELONGS_TO:
David Sedlák1af868e2019-07-17 17:03:14 +02002739 ret = yin_parse_belongs_to(ctx, attrs, data, (struct lysp_submodule *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002740 break;
2741 case YANG_BIT:
David Sedlák43801c92019-08-05 15:58:54 +02002742 ret = yin_parse_bit(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002743 break;
2744 case YANG_CASE:
David Sedlák5379d392019-07-24 10:42:03 +02002745 ret = yin_parse_case(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002746 break;
2747 case YANG_CHOICE:
David Sedlákb7abcfa2019-07-24 12:33:35 +02002748 ret = yin_parse_choice(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002749 break;
2750 case YANG_CONFIG:
David Sedlák1af868e2019-07-17 17:03:14 +02002751 ret = yin_parse_config(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002752 break;
2753 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +02002754 case YANG_DESCRIPTION:
2755 case YANG_ORGANIZATION:
2756 case YANG_REFERENCE:
David Sedlákdf2a9732019-08-07 13:23:16 +02002757 ret = yin_parse_meta_element(ctx, attrs, data, kw, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002758 break;
2759 case YANG_CONTAINER:
David Sedlákf111bcb2019-07-23 17:15:51 +02002760 ret = yin_parse_container(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002761 break;
2762 case YANG_DEFAULT:
David Sedlákc3da3ef2019-07-19 12:56:08 +02002763 if (subelem->flags & YIN_SUBELEM_UNIQUE) {
2764 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
2765 YIN_ARG_VALUE, Y_STR_ARG, exts);
2766 } else {
2767 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
2768 YIN_ARG_VALUE, Y_STR_ARG, exts);
2769 }
David Sedlákd6e56892019-07-01 15:40:24 +02002770 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002771 case YANG_DEVIATE:
David Sedlák4ffcec82019-07-25 15:10:21 +02002772 ret = yin_parse_deviate(ctx, attrs, data, (struct lysp_deviate **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002773 break;
2774 case YANG_DEVIATION:
David Sedlák8b754462019-07-25 16:22:13 +02002775 ret = yin_parse_deviation(ctx, attrs, data, (struct lysp_deviation **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002776 break;
David Sedlák43801c92019-08-05 15:58:54 +02002777 case YANG_ENUM:
2778 ret = yin_parse_enum(ctx, attrs, data, (struct lysp_type *)subelem->dest);
2779 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002780 case YANG_ERROR_APP_TAG:
David Sedlák1af868e2019-07-17 17:03:14 +02002781 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedlák2ce1be62019-07-10 16:15:09 +02002782 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002783 break;
2784 case YANG_ERROR_MESSAGE:
David Sedlákdf2a9732019-08-07 13:23:16 +02002785 ret = yin_parse_err_msg_element(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002786 break;
2787 case YANG_EXTENSION:
David Sedlák1af868e2019-07-17 17:03:14 +02002788 ret = yin_parse_extension(ctx, attrs, data, (struct lysp_ext **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002789 break;
2790 case YANG_FEATURE:
David Sedlák5e13dea2019-07-22 16:06:45 +02002791 ret = yin_parse_feature(ctx, attrs, data, (struct lysp_feature **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002792 break;
2793 case YANG_FRACTION_DIGITS:
David Sedlák1af868e2019-07-17 17:03:14 +02002794 ret = yin_parse_fracdigits(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002795 break;
2796 case YANG_GROUPING:
David Sedláke3ce9ef2019-07-23 16:34:30 +02002797 ret = yin_parse_grouping(ctx, attrs, data, (struct grouping_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002798 break;
2799 case YANG_IDENTITY:
David Sedlák28794f22019-07-22 16:45:00 +02002800 ret = yin_parse_identity(ctx, attrs, data, (struct lysp_ident **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002801 break;
2802 case YANG_IF_FEATURE:
David Sedlák1af868e2019-07-17 17:03:14 +02002803 ret = yin_parse_simple_elements(ctx, attrs, data, kw,
2804 (const char ***)subelem->dest, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002805 break;
2806 case YANG_IMPORT:
David Sedlák298ff6d2019-07-26 14:29:03 +02002807 ret = yin_parse_import(ctx, attrs, data, (struct import_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002808 break;
2809 case YANG_INCLUDE:
David Sedlák0c2bab92019-07-22 15:33:19 +02002810 ret = yin_parse_include(ctx, attrs, data, (struct include_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002811 break;
2812 case YANG_INPUT:
David Sedlák05404f62019-07-24 14:11:53 +02002813 case YANG_OUTPUT:
2814 ret = yin_parse_inout(ctx, attrs, data, kw, (struct inout_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002815 break;
2816 case YANG_KEY:
David Sedlák12470a82019-07-19 13:44:36 +02002817 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest, YIN_ARG_VALUE,
2818 Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002819 break;
2820 case YANG_LEAF:
David Sedlák203ca3a2019-07-18 15:26:25 +02002821 ret = yin_parse_leaf(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002822 break;
2823 case YANG_LEAF_LIST:
David Sedlákc3da3ef2019-07-19 12:56:08 +02002824 ret = yin_parse_leaflist(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002825 break;
2826 case YANG_LENGTH:
David Sedlák1af868e2019-07-17 17:03:14 +02002827 type = (struct lysp_type *)subelem->dest;
David Sedlák438ae432019-07-11 15:36:54 +02002828 type->length = calloc(1, sizeof *type->length);
2829 LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02002830 ret = yin_parse_restriction(ctx, attrs, data, kw, type->length);
David Sedlák438ae432019-07-11 15:36:54 +02002831 type->flags |= LYS_SET_LENGTH;
David Sedlákd6e56892019-07-01 15:40:24 +02002832 break;
2833 case YANG_LIST:
David Sedlákaf536aa2019-07-23 13:42:23 +02002834 ret = yin_parse_list(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002835 break;
2836 case YANG_MANDATORY:
David Sedlák1af868e2019-07-17 17:03:14 +02002837 ret = yin_parse_mandatory(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002838 break;
2839 case YANG_MAX_ELEMENTS:
David Sedlákd6e56892019-07-01 15:40:24 +02002840 case YANG_MIN_ELEMENTS:
David Sedlák09e18c92019-07-18 11:17:11 +02002841 ret = yin_parse_minmax(ctx, attrs, data, current_element, kw, subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002842 break;
2843 case YANG_MODIFIER:
David Sedlák1af868e2019-07-17 17:03:14 +02002844 ret = yin_parse_modifier(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002845 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002846 case YANG_MUST:
David Sedlák1af868e2019-07-17 17:03:14 +02002847 ret = yin_parse_must(ctx, attrs, data, (struct lysp_restr **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002848 break;
2849 case YANG_NAMESPACE:
David Sedlák1af868e2019-07-17 17:03:14 +02002850 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedlák2ce1be62019-07-10 16:15:09 +02002851 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002852 break;
2853 case YANG_NOTIFICATION:
David Sedlák031b9e72019-07-23 15:19:37 +02002854 ret = yin_parse_notification(ctx, attrs, data, (struct notif_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002855 break;
2856 case YANG_ORDERED_BY:
David Sedláka2dad212019-07-18 12:45:19 +02002857 ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002858 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002859 case YANG_PATH:
David Sedlák1af868e2019-07-17 17:03:14 +02002860 type = (struct lysp_type *)subelem->dest;
2861 ret = yin_parse_simple_element(ctx, attrs, data, kw, &type->path,
David Sedlákd1144562019-08-06 12:36:14 +02002862 YIN_ARG_VALUE, Y_STR_ARG, &type->exts);
David Sedlák58979872019-07-12 11:42:43 +02002863 type->flags |= LYS_SET_PATH;
David Sedlákd6e56892019-07-01 15:40:24 +02002864 break;
2865 case YANG_PATTERN:
David Sedlák1af868e2019-07-17 17:03:14 +02002866 ret = yin_parse_pattern(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002867 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02002868 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02002869 case YANG_POSITION:
David Sedlák1af868e2019-07-17 17:03:14 +02002870 ret = yin_parse_value_pos_element(ctx, attrs, data, kw,
2871 (struct lysp_type_enum *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002872 break;
2873 case YANG_PREFIX:
David Sedlák1af868e2019-07-17 17:03:14 +02002874 ret = yin_parse_simple_element(ctx, attrs, data, kw,
2875 (const char **)subelem->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002876 break;
2877 case YANG_PRESENCE:
David Sedlákcb39f642019-07-19 13:19:55 +02002878 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest, YIN_ARG_VALUE,
2879 Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002880 break;
2881 case YANG_RANGE:
David Sedlák1af868e2019-07-17 17:03:14 +02002882 type = (struct lysp_type *)subelem->dest;
David Sedlákb7296dd2019-07-11 14:58:38 +02002883 type->range = calloc(1, sizeof *type->range);
David Sedlák66d7c842019-07-11 15:06:04 +02002884 LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02002885 ret = yin_parse_restriction(ctx, attrs, data, kw, type->range);
David Sedlák438ae432019-07-11 15:36:54 +02002886 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +02002887 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002888 case YANG_REFINE:
David Sedlákd2d676a2019-07-22 11:28:19 +02002889 ret = yin_parse_refine(ctx, attrs, data, (struct lysp_refine **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002890 break;
2891 case YANG_REQUIRE_INSTANCE:
David Sedlák1af868e2019-07-17 17:03:14 +02002892 ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002893 break;
2894 case YANG_REVISION:
David Sedlákaa854b02019-07-22 14:17:10 +02002895 ret = yin_parse_revision(ctx, attrs, data, (struct lysp_revision **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002896 break;
2897 case YANG_REVISION_DATE:
David Sedlák1af868e2019-07-17 17:03:14 +02002898 ret = yin_parse_revision_date(ctx, attrs, data, (char *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002899 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002900 case YANG_STATUS:
David Sedlák1af868e2019-07-17 17:03:14 +02002901 ret = yin_parse_status(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002902 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002903 case YANG_TYPE:
David Sedlák4ffcec82019-07-25 15:10:21 +02002904 if (current_element == YANG_DEVIATE) {
2905 *(struct lysp_type **)subelem->dest = calloc(1, sizeof **(struct lysp_type **)subelem->dest);
2906 LY_CHECK_ERR_GOTO(!(*(struct lysp_type **)subelem->dest), LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
2907 type = *((struct lysp_type **)subelem->dest);
2908 } else {
2909 type = (struct lysp_type *)subelem->dest;
2910 }
David Sedlák374d2b32019-07-17 15:06:55 +02002911 /* type as child of another type */
David Sedlák374d2b32019-07-17 15:06:55 +02002912 if (current_element == YANG_TYPE) {
2913 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, type->types, nested_type, ret, cleanup);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002914 type->flags |= LYS_SET_TYPE;
David Sedlák374d2b32019-07-17 15:06:55 +02002915 type = nested_type;
2916 }
David Sedlák1af868e2019-07-17 17:03:14 +02002917 ret = yin_parse_type(ctx, attrs, data, type);
David Sedlákd6e56892019-07-01 15:40:24 +02002918 break;
2919 case YANG_TYPEDEF:
David Sedlák04e17b22019-07-19 15:29:48 +02002920 ret = yin_parse_typedef(ctx, attrs, data, (struct typedef_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002921 break;
2922 case YANG_UNIQUE:
David Sedlák1af868e2019-07-17 17:03:14 +02002923 ret = yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subelem->dest,
David Sedláka5b1d382019-07-10 16:31:09 +02002924 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002925 break;
2926 case YANG_UNITS:
David Sedlák1af868e2019-07-17 17:03:14 +02002927 ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
David Sedláka5b1d382019-07-10 16:31:09 +02002928 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002929 break;
2930 case YANG_USES:
David Sedlák0d6de5a2019-07-22 13:25:44 +02002931 ret = yin_parse_uses(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002932 break;
David Sedlákd6e56892019-07-01 15:40:24 +02002933 case YANG_WHEN:
David Sedlák1af868e2019-07-17 17:03:14 +02002934 ret = yin_parse_when(ctx, attrs, data, (struct lysp_when **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002935 break;
2936 case YANG_YANG_VERSION:
David Sedlák1af868e2019-07-17 17:03:14 +02002937 ret = yin_parse_yangversion(ctx, attrs, data, (uint8_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002938 break;
2939 case YANG_YIN_ELEMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02002940 ret = yin_parse_yin_element_element(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02002941 break;
2942 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02002943 case YIN_VALUE:
David Sedlák1af868e2019-07-17 17:03:14 +02002944 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02002945 break;
2946 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02002947 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02002948 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02002949 }
David Sedlák3ffbc522019-07-02 17:49:28 +02002950 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02002951 FREE_ARRAY(ctx, attrs, free_arg_rec);
2952 attrs = NULL;
2953 subelem = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02002954 }
2955 } else {
2956 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02002957 /* save text content, if text_content isn't set, it's just ignored */
David Sedlákc5b20842019-08-13 10:18:31 +02002958 /* no resources are allocated in this branch, no need to use cleanup label */
David Sedlák3b4df842019-07-17 11:39:46 +02002959 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG, out, out_len));
David Sedlák3ffbc522019-07-02 17:49:28 +02002960 if (text_content) {
2961 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002962 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02002963 if (!*text_content) {
2964 free(out);
2965 return LY_EMEM;
2966 }
2967 } else {
2968 if (out_len == 0) {
David Sedlák99295322019-07-17 11:34:18 +02002969 *text_content = lydict_insert(ctx->xml_ctx.ctx, "", 0);
David Sedlák3ffbc522019-07-02 17:49:28 +02002970 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02002971 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02002972 }
2973 }
2974 }
David Sedlákd6e56892019-07-01 15:40:24 +02002975 /* load closing element */
David Sedlákc5b20842019-08-13 10:18:31 +02002976 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákd6e56892019-07-01 15:40:24 +02002977 }
2978 }
David Sedlák8b754462019-07-25 16:22:13 +02002979 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
2980 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02002981
2982cleanup:
David Sedlák1af868e2019-07-17 17:03:14 +02002983 FREE_ARRAY(ctx, attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02002984 return ret;
2985}
2986
David Sedlák619db942019-07-03 14:47:30 +02002987LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02002988yin_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 +02002989 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02002990{
2991 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02002992 char *out;
2993 const char *name, *prefix;
2994 size_t out_len, prefix_len, name_len;
2995 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02002996 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02002997 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
2998 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02002999
David Sedlákda8ffa32019-07-08 14:17:10 +02003000 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003001
3002 e->yin = 0;
3003 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02003004 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02003005 e->insubstmt = subelem;
3006 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003007 e->yin |= LYS_YIN;
3008
David Sedlákb1a78352019-06-28 16:16:29 +02003009 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02003010 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02003011 if (!iter->prefix) {
3012 new_subelem = calloc(1, sizeof(*new_subelem));
3013 if (!e->child) {
3014 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003015 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02003016 last_subelem->next = new_subelem;
3017 }
3018 last_subelem = new_subelem;
3019
3020 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02003021 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
3022 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02003023 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003024 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
3025 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02003026 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003027 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
3028 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003029 }
3030 }
3031 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02003032
David Sedlákf250ecf2019-07-01 11:02:05 +02003033 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02003034 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
3035 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003036 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003037 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
3038 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02003039 if (!name) {
3040 /* end of extension instance reached */
3041 break;
3042 }
David Sedlák4ffcec82019-07-25 15:10:21 +02003043 LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +02003044 if (!e->child) {
3045 e->child = new_subelem;
3046 } else {
3047 last_subelem->next = new_subelem;
3048 }
3049 last_subelem = new_subelem;
3050 }
David Sedlák555c7202019-07-04 12:14:12 +02003051 } else {
3052 /* save text content */
3053 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003054 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02003055 if (!e->argument) {
3056 free(out);
3057 return LY_EMEM;
3058 }
3059 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003060 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02003061 LY_CHECK_RET(!e->argument, LY_EMEM);
3062 }
David Sedlákda8ffa32019-07-08 14:17:10 +02003063 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02003064 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02003065 }
David Sedlákb1a78352019-06-28 16:16:29 +02003066 }
3067
3068 return LY_SUCCESS;
3069}
3070
3071LY_ERR
David Sedlák4ffcec82019-07-25 15:10:21 +02003072yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char **data,
3073 struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02003074{
3075 LY_ERR ret = LY_SUCCESS;
3076 const char *temp_prefix, *temp_name;
3077 char *out = NULL;
David Sedlák4ffcec82019-07-25 15:10:21 +02003078 size_t out_len, temp_name_len, temp_prefix_len, prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02003079 int dynamic;
3080 struct yin_arg_record *subelem_args = NULL;
3081 struct lysp_stmt *last = NULL, *new = NULL;
3082
3083 /* allocate new structure for element */
3084 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02003085 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
3086 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02003087
3088 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02003089 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02003090 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02003091 /* add new element to linked-list */
3092 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02003093 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02003094 if (!(*element)->child) {
3095 /* save first */
3096 (*element)->child = new;
3097 } else {
3098 last->next = new;
3099 }
3100 last = new;
3101
3102 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02003103 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 +02003104 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02003105 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003106 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02003107 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
3108 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02003109 /* attributes with prefix are ignored */
3110 if (!temp_prefix) {
3111 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003112 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02003113 if (!last->arg) {
3114 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02003115 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02003116 ret = LY_EMEM;
3117 goto err;
3118 }
3119 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003120 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
3121 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02003122 }
3123 }
3124 }
3125
3126 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02003127 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003128 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003129 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02003130 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02003131 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 +02003132 LY_CHECK_GOTO(ret, err);
3133 if (!name) {
3134 /* end of element reached */
3135 break;
3136 }
David Sedlák4ffcec82019-07-25 15:10:21 +02003137 ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, data, &last->next);
David Sedlákb1a78352019-06-28 16:16:29 +02003138 LY_CHECK_GOTO(ret, err);
3139 last = last->next;
3140 }
3141 } else {
3142 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02003143 if (out_len != 0) {
3144 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003145 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02003146 if (!(*element)->arg) {
3147 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02003148 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02003149 ret = LY_EMEM;
3150 goto err;
3151 }
3152 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003153 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
3154 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02003155 }
David Sedlákb1a78352019-06-28 16:16:29 +02003156 }
3157 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02003158 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 +02003159 LY_CHECK_GOTO(ret, err);
3160 }
3161
David Sedlákda8ffa32019-07-08 14:17:10 +02003162 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02003163 return LY_SUCCESS;
3164
3165err:
David Sedlákda8ffa32019-07-08 14:17:10 +02003166 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003167 return ret;
3168}
3169
3170LY_ERR
David Sedlák4f03b932019-07-26 13:01:47 +02003171yin_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 +02003172{
David Sedlák4f03b932019-07-26 13:01:47 +02003173 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, YANG_MODULE));
3174 struct tree_node_meta node_meta = {NULL, &mod->data};
3175 struct augment_meta aug_meta = {NULL, &mod->augments};
3176 struct grouping_meta grp_meta = {NULL, &mod->groupings};
3177 struct include_meta inc_meta = {mod->mod->name, &mod->includes};
3178 struct notif_meta notif_meta = {NULL, &mod->notifs};
3179 struct action_meta act_meta = {NULL, &mod->rpcs};
3180 struct typedef_meta tpdf_meta = {NULL, &mod->typedefs};
David Sedlák298ff6d2019-07-26 14:29:03 +02003181 struct import_meta imp_meta = {mod->mod->prefix, &mod->imports};
David Sedlák4f03b932019-07-26 13:01:47 +02003182 struct yin_subelement subelems[28] = {
3183 {YANG_ANYDATA, &node_meta, YIN_SUBELEM_VER2},
3184 {YANG_ANYXML, &node_meta, 0},
3185 {YANG_AUGMENT, &aug_meta, 0},
3186 {YANG_CHOICE, &node_meta, 0},
3187 {YANG_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE},
3188 {YANG_CONTAINER, &node_meta, 0},
3189 {YANG_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE},
3190 {YANG_DEVIATION, &mod->deviations, 0},
3191 {YANG_EXTENSION, &mod->extensions, 0},
3192 {YANG_FEATURE, &mod->features, 0},
3193 {YANG_GROUPING, &grp_meta, 0},
3194 {YANG_IDENTITY, &mod->identities, 0},
David Sedlák298ff6d2019-07-26 14:29:03 +02003195 {YANG_IMPORT, &imp_meta, 0},
David Sedlák4f03b932019-07-26 13:01:47 +02003196 {YANG_INCLUDE, &inc_meta, 0},
3197 {YANG_LEAF, &node_meta, 0},
3198 {YANG_LEAF_LIST, &node_meta, 0},
3199 {YANG_LIST, &node_meta, 0},
3200 {YANG_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
3201 {YANG_NOTIFICATION, &notif_meta, 0},
3202 {YANG_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE},
3203 {YANG_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
3204 {YANG_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE},
3205 {YANG_REVISION, &mod->revs, 0},
3206 {YANG_RPC, &act_meta, 0},
3207 {YANG_TYPEDEF, &tpdf_meta, 0},
3208 {YANG_USES, &node_meta, 0},
3209 {YANG_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák968ac342019-07-11 15:17:59 +02003210 {YANG_CUSTOM, NULL, 0}
David Sedlák4f03b932019-07-26 13:01:47 +02003211 };
David Sedlák3b4db242018-10-19 16:11:01 +02003212
David Sedlák4f03b932019-07-26 13:01:47 +02003213 return yin_parse_content(ctx, subelems, 28, data, YANG_MODULE, NULL, &mod->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02003214}
3215
3216LY_ERR
David Sedlák298ff6d2019-07-26 14:29:03 +02003217yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs, const char **data, struct lysp_submodule *submod)
3218{
3219 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, YANG_SUBMODULE));
3220 struct tree_node_meta node_meta = {NULL, &submod->data};
3221 struct augment_meta aug_meta = {NULL, &submod->augments};
3222 struct grouping_meta grp_meta = {NULL, &submod->groupings};
3223 struct include_meta inc_meta = {submod->name, &submod->includes};
3224 struct notif_meta notif_meta = {NULL, &submod->notifs};
3225 struct action_meta act_meta = {NULL, &submod->rpcs};
3226 struct typedef_meta tpdf_meta = {NULL, &submod->typedefs};
3227 struct import_meta imp_meta = {submod->prefix, &submod->imports};
3228 struct yin_subelement subelems[27] = {
3229 {YANG_ANYDATA, &node_meta, YIN_SUBELEM_VER2},
3230 {YANG_ANYXML, &node_meta, 0},
3231 {YANG_AUGMENT, &aug_meta, 0},
3232 {YANG_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
3233 {YANG_CHOICE, &node_meta, 0},
3234 {YANG_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE},
3235 {YANG_CONTAINER, &node_meta, 0},
3236 {YANG_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE},
3237 {YANG_DEVIATION, &submod->deviations, 0},
3238 {YANG_EXTENSION, &submod->extensions, 0},
3239 {YANG_FEATURE, &submod->features, 0},
3240 {YANG_GROUPING, &grp_meta, 0},
3241 {YANG_IDENTITY, &submod->identities, 0},
3242 {YANG_IMPORT, &imp_meta, 0},
3243 {YANG_INCLUDE, &inc_meta, 0},
3244 {YANG_LEAF, &node_meta, 0},
3245 {YANG_LEAF_LIST, &node_meta, 0},
3246 {YANG_LIST, &node_meta, 0},
3247 {YANG_NOTIFICATION, &notif_meta, 0},
3248 {YANG_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE},
3249 {YANG_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE},
3250 {YANG_REVISION, &submod->revs, 0},
3251 {YANG_RPC, &act_meta, 0},
3252 {YANG_TYPEDEF, &tpdf_meta, 0},
3253 {YANG_USES, &node_meta, 0},
3254 {YANG_YANG_VERSION, &submod->version, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
3255 {YANG_CUSTOM, NULL, 0}
3256 };
3257
3258 return yin_parse_content(ctx, subelems, 27, data, YANG_SUBMODULE, NULL, &submod->exts);
3259}
3260
3261LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02003262yin_parse_submodule(struct yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003263{
3264 enum yang_keyword kw = YANG_NONE;
3265 LY_ERR ret = LY_SUCCESS;
3266 const char *prefix, *name;
3267 size_t prefix_len, name_len;
3268 struct yin_arg_record *attrs = NULL;
3269 struct lysp_submodule *mod_p = NULL;
3270
3271 /* create context */
3272 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003273 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
David Sedlák8985a142019-07-31 16:43:06 +02003274 (*yin_ctx)->xml_ctx.ctx = ctx;
3275 (*yin_ctx)->xml_ctx.line = 1;
3276
David Sedlák1b623122019-08-05 15:27:49 +02003277 /* map the typedefs and groupings list from main context to the submodule's context */
3278 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3279 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3280
David Sedlák8985a142019-07-31 16:43:06 +02003281 /* check submodule */
3282 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3283 LY_CHECK_GOTO(ret, cleanup);
3284 ret = yin_load_attributes(*yin_ctx, &data, &attrs);
3285 LY_CHECK_GOTO(ret, cleanup);
3286 kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
3287
3288 if (kw == YANG_MODULE) {
3289 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3290 ret = LY_EINVAL;
3291 goto cleanup;
3292 } else if (kw != YANG_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003293 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003294 ret = LY_EVALID;
3295 goto cleanup;
3296 }
3297
3298 mod_p = calloc(1, sizeof *mod_p);
3299 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
3300 mod_p->parsing = 1;
3301
3302 ret = yin_parse_submod(*yin_ctx, attrs, &data, mod_p);
3303 LY_CHECK_GOTO(ret, cleanup);
3304
David Sedlák6d781b62019-08-02 15:22:52 +02003305 name = NULL;
3306 if ((*yin_ctx)->xml_ctx.status == LYXML_ELEMENT) {
3307 const char *temp_data = data;
3308 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3309 data = temp_data;
3310 }
3311 if ((*yin_ctx)->xml_ctx.status != LYXML_END || name) {
David Sedlák1538a842019-08-08 15:38:51 +02003312 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003313 ret = LY_EVALID;
3314 goto cleanup;
3315 }
3316
David Sedlák8985a142019-07-31 16:43:06 +02003317 mod_p->parsing = 0;
3318 *submod = mod_p;
3319
3320cleanup:
3321 if (ret) {
3322 lysp_submodule_free(ctx, mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003323 yin_parser_ctx_free(*yin_ctx);
3324 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003325 }
3326
3327 FREE_ARRAY(*yin_ctx, attrs, free_arg_rec);
3328 return ret;
3329}
3330
3331LY_ERR
3332yin_parse_module(struct yin_parser_ctx **yin_ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003333{
David Sedláke4889912018-11-02 09:52:40 +01003334 LY_ERR ret = LY_SUCCESS;
3335 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003336 struct lysp_module *mod_p = NULL;
3337 const char *prefix, *name;
3338 size_t prefix_len, name_len;
David Sedlák1f90d252019-07-10 17:09:32 +02003339 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003340
David Sedlák8985a142019-07-31 16:43:06 +02003341 /* create context */
3342 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003343 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
David Sedlák8985a142019-07-31 16:43:06 +02003344 (*yin_ctx)->xml_ctx.ctx = mod->ctx;
3345 (*yin_ctx)->xml_ctx.line = 1;
David Sedlákda8ffa32019-07-08 14:17:10 +02003346
David Sedlák8985a142019-07-31 16:43:06 +02003347 /* check module */
3348 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02003349 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8985a142019-07-31 16:43:06 +02003350 ret = yin_load_attributes(*yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02003351 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8985a142019-07-31 16:43:06 +02003352 kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01003353 if (kw == YANG_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003354 LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
David Sedlák3017da42019-02-15 09:48:04 +01003355 ret = LY_EINVAL;
3356 goto cleanup;
3357 } else if (kw != YANG_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003358 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003359 ret = LY_EVALID;
3360 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003361 }
3362
David Sedlák3017da42019-02-15 09:48:04 +01003363 /* allocate module */
3364 mod_p = calloc(1, sizeof *mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003365 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
David Sedlák3017da42019-02-15 09:48:04 +01003366 mod_p->mod = mod;
3367 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01003368
David Sedlák00250342019-06-21 14:19:39 +02003369 /* parse module substatements */
David Sedlák8985a142019-07-31 16:43:06 +02003370 ret = yin_parse_mod(*yin_ctx, attrs, &data, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003371 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003372
David Sedlák1b623122019-08-05 15:27:49 +02003373 /* check trailing characters */
David Sedlák6d781b62019-08-02 15:22:52 +02003374 if ((*yin_ctx)->xml_ctx.status == LYXML_ELEMENT) {
3375 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3376 }
3377 if ((*yin_ctx)->xml_ctx.status != LYXML_END || name) {
David Sedlák1538a842019-08-08 15:38:51 +02003378 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003379
3380 ret = LY_EVALID;
3381 goto cleanup;
3382 }
3383
David Sedlák3017da42019-02-15 09:48:04 +01003384 mod_p->parsing = 0;
3385 mod->parsed = mod_p;
3386
3387cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003388 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003389 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003390 yin_parser_ctx_free(*yin_ctx);
3391 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003392 }
David Sedlák8985a142019-07-31 16:43:06 +02003393 FREE_ARRAY(*yin_ctx, attrs, free_arg_rec);
David Sedlák2e411422018-12-17 02:35:39 +01003394 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003395}