blob: 730a3e0c9a2a87236a95a14de6fa734361e786c3 [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.
35 * @param ns Namespace URI to check.
36 *
37 * @return true if ns equals YIN_NS_URI false otherwise.
38 */
39#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
40
David Sedláke1a30302019-07-10 13:49:38 +020041static LY_ERR
42yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
43 struct lysp_ext_instance **exts);
44
David Sedlákf6251182019-06-06 10:22:13 +020045const char *const yin_attr_list[] = {
46 [YIN_ARG_NAME] = "name",
47 [YIN_ARG_TARGET_NODE] = "target-node",
48 [YIN_ARG_MODULE] = "module",
49 [YIN_ARG_VALUE] = "value",
50 [YIN_ARG_TEXT] = "text",
51 [YIN_ARG_CONDITION] = "condition",
52 [YIN_ARG_URI] = "uri",
53 [YIN_ARG_DATE] = "date",
54 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020055};
56
David Sedlák1bccdfa2019-06-17 15:55:27 +020057enum yang_keyword
David Sedlákc1771b12019-07-10 15:55:46 +020058yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
59 const char *prefix, size_t prefix_len, enum yang_keyword parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020060{
David Sedlák8f7a1172019-06-20 14:42:18 +020061 const char *start = NULL;
62 enum yang_keyword kw = YANG_NONE;
63 const struct lyxml_ns *ns = NULL;
64
65 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020066 return YANG_NONE;
67 }
68
David Sedlákda8ffa32019-07-08 14:17:10 +020069 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020070 if (ns) {
71 if (!IS_YIN_NS(ns->uri)) {
72 return YANG_CUSTOM;
73 }
74 } else {
75 /* elements without namespace are automatically unknown */
76 return YANG_NONE;
77 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020078
David Sedlák8f7a1172019-06-20 14:42:18 +020079 start = name;
80 kw = lysp_match_kw(NULL, &name);
81
82 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020083 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
84 if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) {
85 return YIN_VALUE;
86 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020087 return kw;
88 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020089 if (strncmp(start, "text", name_len) == 0) {
90 return YIN_TEXT;
91 } else if (strncmp(start, "value", name_len) == 0) {
92 return YIN_VALUE;
93 } else {
94 return YANG_NONE;
95 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020096 }
97}
98
David Sedlák872c7b42018-10-26 13:15:20 +020099enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +0200100yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200101{
David Sedláka7406952019-04-05 10:33:07 +0200102 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200103 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200104 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200105
David Sedlák94de2aa2019-02-15 12:42:11 +0100106#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
107#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100108#define IF_ARG_PREFIX_END }
109
David Sedlák1c8b2702019-02-22 11:03:02 +0100110 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100111 case 'c':
112 already_read += 1;
113 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
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 'd':
117 already_read += 1;
118 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200119 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200120
David Sedlák94de2aa2019-02-15 12:42:11 +0100121 case 'm':
122 already_read += 1;
123 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200124 break;
125
David Sedlák94de2aa2019-02-15 12:42:11 +0100126 case 'n':
127 already_read += 1;
128 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200129 break;
130
David Sedlák94de2aa2019-02-15 12:42:11 +0100131 case 't':
132 already_read += 1;
133 IF_ARG_PREFIX("a", 1)
134 IF_ARG("g", 1, YIN_ARG_TAG)
135 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
136 IF_ARG_PREFIX_END
137 else IF_ARG("ext", 3, YIN_ARG_TEXT)
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 'u':
141 already_read += 1;
142 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200143 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200144
David Sedlák94de2aa2019-02-15 12:42:11 +0100145 case 'v':
146 already_read += 1;
147 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200148 break;
149 }
150
David Sedlákc10e7902018-12-17 02:17:59 +0100151 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200152 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200153 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200154 }
155
David Sedlák18730132019-03-15 15:51:34 +0100156#undef IF_ARG
157#undef IF_ARG_PREFIX
158#undef IF_ARG_PREFIX_END
159
David Sedlák872c7b42018-10-26 13:15:20 +0200160 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200161}
162
David Sedlákb4e44562019-07-04 15:42:12 +0200163/**
164 * @brief free argument record, content loaded from lyxml_get_string() can be
165 * dynamically allocated in some cases so it must be also freed.
166 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200167static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
168 (void)ctx; /* unused */
David Sedlák00250342019-06-21 14:19:39 +0200169 if (record->dynamic_content) {
170 free(record->content);
171 }
172}
173
David Sedlák8f7a1172019-06-20 14:42:18 +0200174LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200175yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200176{
177 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200178 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200179 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200180
David Sedlák555c7202019-07-04 12:14:12 +0200181 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200182 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
183 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200184 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200185
David Sedlákda8ffa32019-07-08 14:17:10 +0200186 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
187 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200188 argument_record->name = name.value;
189 argument_record->name_len = name.len;
190 argument_record->prefix = prefix.value;
191 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200192 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200193 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200194 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200195 }
196 }
197
David Sedlák8f7a1172019-06-20 14:42:18 +0200198cleanup:
199 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200200 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200201 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200202 }
203 return ret;
204}
205
David Sedlák4a650532019-07-10 11:55:18 +0200206LY_ERR
207yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
208{
209 int prefix = 0;
210 unsigned int c;
211 size_t utf8_char_len;
212 size_t already_read = 0;
213 while (already_read < len) {
214 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
215 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
216 already_read += utf8_char_len;
217 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
218
219 switch (val_type) {
220 case Y_IDENTIF_ARG:
221 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
222 break;
223 case Y_PREF_IDENTIF_ARG:
224 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
225 break;
226 case Y_STR_ARG:
227 case Y_MAYBE_STR_ARG:
228 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
229 break;
230 }
231 }
232
233 return LY_SUCCESS;
234}
235
David Sedlákb4e44562019-07-04 15:42:12 +0200236/**
237 * @brief Parse yin argument.
238 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200239 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200240 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200241 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200242 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
243 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200244 * @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 +0200245 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200246 * @param[in] current_element Identification of current element, used for logging.
247 *
248 * @return LY_ERR values.
249 */
250static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +0200251yin_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 +0200252 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200253{
David Sedlák8f7a1172019-06-20 14:42:18 +0200254 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
255 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200256 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200257
David Sedlák1bccdfa2019-06-17 15:55:27 +0200258 /* validation of attributes */
David Sedlák1f90d252019-07-10 17:09:32 +0200259 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200260 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
261 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200262 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200263 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200264 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200265 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200266 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
267 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200268 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200269 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200270 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200271 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200272 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200273 /* string is no longer supposed to be freed when the sized array is freed */
274 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200275 } else {
David Sedlák99295322019-07-17 11:34:18 +0200276 if (iter->content_len == 0) {
277 *arg_val = lydict_insert(ctx->xml_ctx.ctx, "", 0);
278 } else {
279 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
280 LY_CHECK_RET(!(*arg_val), LY_EMEM);
281 }
David Sedlák57715b12019-06-17 13:05:22 +0200282 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200283 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200284 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Unexpected attribute \"%.*s\" of %s element.", iter->name_len, iter->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200285 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200286 }
287 }
288 }
289
David Sedlák292763b2019-07-09 11:10:53 +0200290 /* anything else than Y_MAYBE_STR_ARG is mandatory */
291 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200292 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 +0200293 return LY_EVALID;
294 }
295
296 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200297}
298
David Sedlákd6e56892019-07-01 15:40:24 +0200299/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200300 * @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 +0200301 *
302 * @param[in] type Type of wanted record.
303 * @param[in] array_size Size of array.
304 * @param[in] array Searched array.
305 *
306 * @return Pointer to desired record on success, NULL if element is not in the array.
307 */
David Sedlákb4e44562019-07-04 15:42:12 +0200308static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200309get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200310{
David Sedlákb0faad82019-07-04 14:28:59 +0200311 signed char left = 0, right = array_size - 1, middle;
312
313 while (left <= right) {
314 middle = left + (right - left) / 2;
315
316 if (array[middle].type == type) {
317 return &array[middle];
318 }
319
320 if (array[middle].type < type) {
321 left = middle + 1;
322 } else {
323 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200324 }
325 }
326
327 return NULL;
328}
329
David Sedlákbba38e52019-07-09 15:20:01 +0200330/**
331 * @brief Helper function to check mandatory constraint of subelement.
332 *
333 * @param[in,out] ctx Yin parser context for logging and to store current state.
334 * @param[in] subelem_info Array of information about subelements.
335 * @param[in] subelem_info_size Size of subelem_info array.
336 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
337 *
338 * @return LY_ERR values.
339 */
340static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200341yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200342 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200343{
David Sedlákb0faad82019-07-04 14:28:59 +0200344 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200345 /* 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 +0200346 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200347 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory subelement %s of %s element.",
David Sedlák555c7202019-07-04 12:14:12 +0200348 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200349 return LY_EVALID;
350 }
351 }
352
353 return LY_SUCCESS;
354}
355
David Sedlákbba38e52019-07-09 15:20:01 +0200356/**
357 * @brief Helper function to check "first" constraint of subelement.
358 *
359 * @param[in,out] ctx Yin parser context for logging and to store current state.
360 * @param[in] subelem_info Array of information about subelements.
361 * @param[in] subelem_info_size Size of subelem_info array.
362 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
363 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
364 *
365 * @return LY_ERR values.
366 */
367static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200368yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200369 signed char subelem_info_size, enum yang_keyword current_element,
370 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200371{
David Sedlákb0faad82019-07-04 14:28:59 +0200372 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200373 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200374 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Subelement %s of %s element must be defined as first subelement.",
David Sedlák555c7202019-07-04 12:14:12 +0200375 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200376 return LY_EVALID;
377 }
378 }
379
380 return LY_SUCCESS;
381}
382
David Sedlákbba38e52019-07-09 15:20:01 +0200383/**
384 * @brief Helper function to check if array of information about subelements is in ascending order.
385 *
386 * @param[in] subelem_info Array of information about subelements.
387 * @param[in] subelem_info_size Size of subelem_info array.
388 *
389 * @return True iff subelem_info array is in ascending order, False otherwise.
390 */
David Sedlák5545f5d2019-07-11 11:55:16 +0200391#ifndef NDEBUG
David Sedlákbba38e52019-07-09 15:20:01 +0200392static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200393is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
394{
David Sedlák292763b2019-07-09 11:10:53 +0200395 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200396
397 for (signed char i = 0; i < subelem_info_size; ++i) {
398 if (subelem_info[i].type <= current) {
399 return false;
400 }
401 current = subelem_info[i].type;
402 }
403
404 return true;
405}
David Sedlák5545f5d2019-07-11 11:55:16 +0200406#endif
David Sedlákb0faad82019-07-04 14:28:59 +0200407
David Sedlákd6e56892019-07-01 15:40:24 +0200408/**
David Sedlákb4e44562019-07-04 15:42:12 +0200409 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
410 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200411 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200412 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200413 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200414 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200415 * @param[in] kw Type of current element.
416 * @param[out] value Where value of attribute should be stored.
417 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200418 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200419 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200420 *
David Sedlákd6e56892019-07-01 15:40:24 +0200421 * @return LY_ERR values.
422 */
David Sedlákb4e44562019-07-04 15:42:12 +0200423static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200424yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlák292763b2019-07-09 11:10:53 +0200425 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 +0200426{
David Sedlák1f90d252019-07-10 17:09:32 +0200427 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200428 struct yin_subelement subelems[1] = {
429 {YANG_CUSTOM, NULL, 0}
430 };
David Sedlákb4e44562019-07-04 15:42:12 +0200431
David Sedlákda8ffa32019-07-08 14:17:10 +0200432 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200433}
434
435/**
David Sedlákd3983112019-07-12 11:20:56 +0200436 * @brief Parse pattern element.
437 *
438 * @param[in,out] ctx Yin parser context for logging and to store current state.
439 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
440 * @param[in,out] data Data to read from, always moved to currently handled character.
441 * @param[in,out] patterns Restrictions to add to.
442 *
443 * @return LY_ERR values.
444 */
445static LY_ERR
446yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
447 struct lysp_type *type)
448{
449 const char *real_value = NULL;
450 char *saved_value = NULL;
451 struct lysp_restr *restr;
452
453 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
454 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
455 size_t len = strlen(real_value);
456
457 saved_value = malloc(len + 2);
458 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
459 memmove(saved_value + 1, real_value, len);
460 FREE_STRING(ctx->xml_ctx.ctx, real_value);
461 saved_value[0] = 0x06;
462 saved_value[len + 1] = '\0';
463 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
464 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
465 type->flags |= LYS_SET_PATTERN;
466
467 struct yin_subelement subelems[6] = {
468 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
469 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
470 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
471 {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
472 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
473 {YANG_CUSTOM, NULL, 0}
474 };
475 return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
476}
477
David Sedlákf75d55e2019-07-12 16:52:50 +0200478static LY_ERR
479yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
480 struct lysp_type *type)
481{
482 const char *temp_val = NULL;
483 char *ptr;
484 unsigned long int num;
485
486 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_FRACTION_DIGITS));
487
488 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
489 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
490 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
491 return LY_EVALID;
492 }
493
494 errno = 0;
495 num = strtoul(temp_val, &ptr, 10);
496 if (*ptr != '\0') {
497 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
498 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
499 return LY_EVALID;
500 }
501 if ((errno == ERANGE) || (num > 18)) {
502 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
503 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
504 return LY_EVALID;
505 }
David Sedlák2ab5d8e2019-07-16 11:19:41 +0200506 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200507 type->fraction_digits = num;
508 type->flags |= LYS_SET_FRDIGITS;
509 struct yin_subelement subelems[1] = {
510 {YANG_CUSTOM, &index, 0}
511 };
512 return yin_parse_content(ctx, subelems, 1, data, YANG_FRACTION_DIGITS, NULL, &type->exts);
513}
514
David Sedlák07869a52019-07-12 14:28:19 +0200515/**
516 * @brief Parse enum or bit element.
517 *
518 * @param[in,out] ctx YIN parser context for logging and to store current state.
519 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
520 * @param[in,out] data Data to read from, always moved to currently handled character.
521 * @param[in] enum_kw Identification of actual keyword, can be set to YANG_BIT or YANG_ENUM.
522 * @param[in,out] enums Enums or bits to add to.
523 *
524 * @return LY_ERR values.
525 */
David Sedlákca36c422019-07-12 12:47:55 +0200526static LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200527yin_parse_enum_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
528 enum yang_keyword enum_kw, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200529{
David Sedlák07869a52019-07-12 14:28:19 +0200530 assert(enum_kw == YANG_BIT || enum_kw == YANG_ENUM);
531 struct lysp_type_enum *en;
532 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM);
533 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, enum_kw));
534 type->flags |= (enum_kw == YANG_ENUM) ? LYS_SET_ENUM : LYS_SET_BIT;
535 if (enum_kw == YANG_ENUM) {
536 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
David Sedlákb9b892c2019-07-12 14:44:02 +0200537 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák07869a52019-07-12 14:28:19 +0200538 }
539 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, ly_stmt2str(enum_kw), en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200540
541 struct yin_subelement subelems[6] = {
David Sedlák07869a52019-07-12 14:28:19 +0200542 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
543 {YANG_IF_FEATURE, &en->iffeatures, 0},
David Sedlák07869a52019-07-12 14:28:19 +0200544 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
545 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200546 {(enum_kw == YANG_ENUM) ? YANG_VALUE : YANG_POSITION, en, YIN_SUBELEM_UNIQUE},
David Sedlákca36c422019-07-12 12:47:55 +0200547 {YANG_CUSTOM, NULL, 0}
548 };
David Sedlák07869a52019-07-12 14:28:19 +0200549 return yin_parse_content(ctx, subelems, 6, data, enum_kw, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200550}
551
David Sedlákd3983112019-07-12 11:20:56 +0200552/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200553 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
554 * more instances, such as base or if-feature.
555 *
556 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200557 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200558 * @param[in,out] data Data to read from, always moved to currently handled character.
559 * @param[in] kw Type of current element.
560 * @param[out] values Parsed values to add to.
561 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200562 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200563 * @param[in,out] exts Extension instance to add to.
564 *
565 * @return LY_ERR values.
566 */
567static LY_ERR
568yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlák292763b2019-07-09 11:10:53 +0200569 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 +0200570{
571 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200572 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200573 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200574 struct yin_subelement subelems[1] = {
575 {YANG_CUSTOM, &index, 0}
576 };
577
David Sedlák1f90d252019-07-10 17:09:32 +0200578 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200579
580 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
581}
582
583/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200584 * @brief Parse require instance element.
585 *
586 * @param[in,out] ctx Yin parser context for logging and to store current state.
587 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
588 * @param[in,out] data Data to read from, always moved to currently handled character.
589 * @prama[out] type Type structure to store value, flag and extensions.
590 *
591 * @return LY_ERR values.
592 */
593static LY_ERR
594yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
595 const char **data, struct lysp_type *type)
596{
597 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200598 struct yin_subelement subelems[1] = {
599 {YANG_CUSTOM, NULL, 0}
600 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200601
602 type->flags |= LYS_SET_REQINST;
603 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
604 if (strcmp(temp_val, "true") == 0) {
605 type->require_instance = 1;
606 } else if (strcmp(temp_val, "false") != 0) {
607 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "require-instance");
608 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
609 return LY_EVALID;
610 }
611 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
612
613 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
614}
615
616/**
David Sedlákce77bf52019-07-11 16:59:31 +0200617 * @brief Parse modifier element.
618 *
619 * @param[in,out] ctx Yin parser context for logging and to store current state.
620 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
621 * @param[in,out] data Data to read from, always moved to currently handled character.
622 * @param[in,out] pat Value to write to.
623 * @param[in,out] exts Extension instances to add to.
624 *
625 * @return LY_ERR values.
626 */
627static LY_ERR
628yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
629 const char **pat, struct lysp_ext_instance **exts)
630{
David Sedlákd3983112019-07-12 11:20:56 +0200631 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200632 const char *temp_val;
633 char *modified_val;
634 struct yin_subelement subelems[1] = {
635 {YANG_CUSTOM, NULL, 0}
636 };
637
638 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER));
639 if (strcmp(temp_val, "invert-match") != 0) {
640 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "modifier");
641 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
642 return LY_EVALID;
643 }
David Sedlákd3983112019-07-12 11:20:56 +0200644 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200645
646 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200647 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200648 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200649 strcpy(modified_val, *pat);
650 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200651
652 /* modify the new value */
653 modified_val[0] = 0x15;
654 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
655
656 return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts);
657}
658
659/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200660 * @brief Parse a restriction element (length, range or one instance of must).
661 *
662 * @param[in,out] ctx Yin parser context for logging and to store current state.
663 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
664 * @param[in,out] data Data to read from, always moved to currently handled character.
665 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
666 * @param[in]
667 */
668static LY_ERR
669yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
670 enum yang_keyword restr_kw, struct lysp_restr *restr)
671{
672 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
673 struct yin_subelement subelems[5] = {
David Sedlák968ac342019-07-11 15:17:59 +0200674 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
675 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
676 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
677 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
678 {YANG_CUSTOM, NULL, 0}
679 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200680 /* argument of must is called condition, but argument of length and range is called value */
681 enum YIN_ARGUMENT arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
682 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
683
684 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
685}
686
687/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200688 * @brief Parse must element.
689 *
690 * @param[in,out] ctx YIN parser context for logging and to store current state.
691 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
692 * @param[in,out] data Data to read from, always moved to currently handled character.
693 * @param[in,out] restrs Restrictions to add to.
694 *
695 * @return LY_ERR values.
696 */
697static LY_ERR
698yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
699{
700 struct lysp_restr *restr;
701
702 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
703 return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr);
704}
705
706/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200707 * @brief Parse position or value element.
708 *
709 * @param[in,out] ctx YIN parser context for logging and to store current state.
710 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
711 * @param[in,out] data Data to read from, always moved to currently handled character.
712 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
713 * @param[out] enm Enum structure to save value, flags and extensions.
714 *
715 * @return LY_ERR values.
716 */
717static LY_ERR
718yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
719 enum yang_keyword kw, struct lysp_type_enum *enm)
720{
721 assert(kw == YANG_POSITION || kw == YANG_VALUE);
722 const char *temp_val = NULL;
723 char *ptr;
724 long int num;
725 unsigned long int unum;
726
727 /* set value flag */
728 enm->flags |= LYS_SET_VALUE;
729
730 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200731 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 +0200732 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') ||
733 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == YANG_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200734 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
735 goto error;
736 }
737
738 /* convert value */
739 errno = 0;
740 if (kw == YANG_VALUE) {
741 num = strtol(temp_val, &ptr, 10);
742 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
743 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
744 goto error;
745 }
746 } else {
747 unum = strtoul(temp_val, &ptr, 10);
748 if (unum > UINT64_C(4294967295)) {
749 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
750 goto error;
751 }
752 }
753 /* check if whole argument value was converted */
754 if (*ptr != '\0') {
755 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +0200756 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +0200757 }
758 if (errno == ERANGE) {
759 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw));
760 goto error;
761 }
762 /* save correctly ternary operator can't be used because num and unum have different signes */
763 if (kw == YANG_VALUE) {
764 enm->value = num;
765 } else {
766 enm->value = unum;
767 }
768 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
769
770 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200771 struct yin_subelement subelems[1] = {
772 {YANG_CUSTOM, NULL, 0}
773 };
David Sedlák5545f5d2019-07-11 11:55:16 +0200774 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
775
776 error:
777 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
778 return LY_EVALID;
779}
780
781/**
David Sedlákc1771b12019-07-10 15:55:46 +0200782 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200783 * text element as child
784 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200785 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200786 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200787 * @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 +0200788 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200789 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200790 *
791 * @return LY_ERR values.
792 */
793static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200794yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200795 const char **value, struct lysp_ext_instance **exts)
796{
797 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
798
David Sedlák968ac342019-07-11 15:17:59 +0200799 struct yin_subelement subelems[2] = {
800 {YANG_CUSTOM, NULL, 0},
801 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
802 };
David Sedlákb4e44562019-07-04 15:42:12 +0200803
David Sedlákda8ffa32019-07-08 14:17:10 +0200804 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200805}
806
807/**
David Sedlákc1771b12019-07-10 15:55:46 +0200808 * @brief Parse error-message element.
809 *
810 * @param[in,out] ctx Yin parser context for logging and to store current state.
811 * @param[in,out] data Data to read from.
812 * @param[out] value Where the content of error-message element should be stored.
813 * @param[in,out] exts Extension instance to add to.
814 *
815 * @return LY_ERR values.
816 */
817static LY_ERR
818yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
819 struct lysp_ext_instance **exts)
820{
David Sedlák968ac342019-07-11 15:17:59 +0200821 struct yin_subelement subelems[2] = {
822 {YANG_CUSTOM, NULL, 0},
823 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
824 };
David Sedlákc1771b12019-07-10 15:55:46 +0200825
826 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
827}
828
829/**
David Sedlákb4e44562019-07-04 15:42:12 +0200830 * @brief Map keyword type to substatement info.
831 *
832 * @param[in] kw Keyword type.
833 *
834 * @return correct LYEXT_SUBSTMT information.
835 */
836static LYEXT_SUBSTMT
837kw2lyext_substmt(enum yang_keyword kw)
838{
839 switch (kw) {
840 case YANG_ARGUMENT:
841 return LYEXT_SUBSTMT_ARGUMENT;
842 case YANG_BASE:
843 return LYEXT_SUBSTMT_BASE;
844 case YANG_BELONGS_TO:
845 return LYEXT_SUBSTMT_BELONGSTO;
846 case YANG_CONTACT:
847 return LYEXT_SUBSTMT_CONTACT;
848 case YANG_DEFAULT:
849 return LYEXT_SUBSTMT_DEFAULT;
850 case YANG_DESCRIPTION:
851 return LYEXT_SUBSTMT_DESCRIPTION;
852 case YANG_ERROR_APP_TAG:
853 return LYEXT_SUBSTMT_ERRTAG;
854 case YANG_ERROR_MESSAGE:
855 return LYEXT_SUBSTMT_ERRMSG;
856 case YANG_KEY:
857 return LYEXT_SUBSTMT_KEY;
858 case YANG_NAMESPACE:
859 return LYEXT_SUBSTMT_NAMESPACE;
860 case YANG_ORGANIZATION:
861 return LYEXT_SUBSTMT_ORGANIZATION;
862 case YANG_PATH:
863 return LYEXT_SUBSTMT_PATH;
864 case YANG_PREFIX:
865 return LYEXT_SUBSTMT_PREFIX;
866 case YANG_PRESENCE:
867 return LYEXT_SUBSTMT_PRESENCE;
868 case YANG_REFERENCE:
869 return LYEXT_SUBSTMT_REFERENCE;
870 case YANG_REVISION_DATE:
871 return LYEXT_SUBSTMT_REVISIONDATE;
872 case YANG_UNITS:
873 return LYEXT_SUBSTMT_UNITS;
874 case YANG_VALUE:
875 return LYEXT_SUBSTMT_VALUE;
876 case YANG_YANG_VERSION:
877 return LYEXT_SUBSTMT_VERSION;
878 case YANG_MODIFIER:
879 return LYEXT_SUBSTMT_MODIFIER;
880 case YANG_REQUIRE_INSTANCE:
881 return LYEXT_SUBSTMT_REQINSTANCE;
882 case YANG_YIN_ELEMENT:
883 return LYEXT_SUBSTMT_YINELEM;
884 case YANG_CONFIG:
885 return LYEXT_SUBSTMT_CONFIG;
886 case YANG_MANDATORY:
887 return LYEXT_SUBSTMT_MANDATORY;
888 case YANG_ORDERED_BY:
889 return LYEXT_SUBSTMT_ORDEREDBY;
890 case YANG_STATUS:
891 return LYEXT_SUBSTMT_STATUS;
892 case YANG_FRACTION_DIGITS:
893 return LYEXT_SUBSTMT_FRACDIGITS;
894 case YANG_MAX_ELEMENTS:
895 return LYEXT_SUBSTMT_MAX;
896 case YANG_MIN_ELEMENTS:
897 return LYEXT_SUBSTMT_MIN;
898 case YANG_POSITION:
899 return LYEXT_SUBSTMT_POSITION;
900 case YANG_UNIQUE:
901 return LYEXT_SUBSTMT_UNIQUE;
902 case YANG_IF_FEATURE:
903 return LYEXT_SUBSTMT_IFFEATURE;
904 default:
905 return LYEXT_SUBSTMT_SELF;
906 }
907}
908
David Sedlák9c40a922019-07-08 17:04:43 +0200909/**
910 * @brief Parse belongs-to element.
911 *
912 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200913 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200914 * @param[in,out] data Data to read from, always moved to currently handled character.
915 * @param[out] submod Structure of submodule that is being parsed.
916 * @param[in,out] exts Extension instances to add to.
917 *
918 * @return LY_ERR values
919 */
920static LY_ERR
921yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
922 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
923{
David Sedlák968ac342019-07-11 15:17:59 +0200924 struct yin_subelement subelems[2] = {
925 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
926 {YANG_CUSTOM, NULL, 0}
927 };
David Sedlák1f90d252019-07-10 17:09:32 +0200928 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, YANG_BELONGS_TO));
David Sedlák9c40a922019-07-08 17:04:43 +0200929
930 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
931}
932
David Sedlákd6e56892019-07-01 15:40:24 +0200933LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200934yin_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 +0200935 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 +0200936{
937 LY_ERR ret = LY_SUCCESS;
938 struct sized_string prefix, name;
David Sedlák8e7bda82019-07-16 17:57:50 +0200939 char *out = NULL;
940 size_t out_len = 0;
941 int dynamic = 0;
David Sedlák3ffbc522019-07-02 17:49:28 +0200942 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200943 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200944 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200945 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200946 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200947 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200948
David Sedlákda8ffa32019-07-08 14:17:10 +0200949 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
950 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200951 /* current element has subelements as content */
952 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200953 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
954 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200955 LY_CHECK_GOTO(ret, cleanup);
956 if (!name.value) {
957 /* end of current element reached */
958 break;
959 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200960 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200961 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +0200962 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +0200963
964 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200965 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
966 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200967 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name.len, name.value, ly_stmt2str(current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200968 ret = LY_EVALID;
969 goto cleanup;
970 }
971
David Sedlák5f8191e2019-07-08 16:35:52 +0200972 /* TODO check relative order */
973
David Sedlák21f87cd2019-07-03 16:53:23 +0200974 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200975 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200976 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Redefinition of %s element in %s element.", ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200977 return LY_EVALID;
978 }
979 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlák66d7c842019-07-11 15:06:04 +0200980 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec);
981 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +0200982 }
983 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
984
David Sedlákd6e56892019-07-01 15:40:24 +0200985 switch (kw) {
986 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200987 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlák1f90d252019-07-10 17:09:32 +0200988 ret = yin_parse_extension_instance(ctx, subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200989 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200990 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200991 break;
992 case YANG_ACTION:
993 break;
994 case YANG_ANYDATA:
995 break;
996 case YANG_ANYXML:
997 break;
998 case YANG_ARGUMENT:
David Sedlák1f90d252019-07-10 17:09:32 +0200999 ret = yin_parse_argument_element(ctx, subelem_attrs, data, (struct yin_argument_meta *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001000 break;
1001 case YANG_AUGMENT:
1002 break;
1003 case YANG_BASE:
David Sedláka62750b2019-07-16 11:21:31 +02001004 if (current_element == YANG_TYPE) {
David Sedlák292763b2019-07-09 11:10:53 +02001005 type = (struct lysp_type *)subelem_info_rec->dest;
1006 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
1007 Y_PREF_IDENTIF_ARG, exts);
1008 type->flags |= LYS_SET_BASE;
David Sedláka62750b2019-07-16 11:21:31 +02001009 } else if (current_element == YANG_IDENTITY) {
David Sedlák292763b2019-07-09 11:10:53 +02001010 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
1011 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
1012 } else {
1013 LOGINT(ctx->xml_ctx.ctx);
1014 ret = LY_EINT;
1015 }
David Sedlákd6e56892019-07-01 15:40:24 +02001016 break;
1017 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +02001018 ret = yin_parse_belongs_to(ctx, subelem_attrs, data, (struct lysp_submodule *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001019 break;
1020 case YANG_BIT:
David Sedlák07869a52019-07-12 14:28:19 +02001021 case YANG_ENUM:
1022 ret = yin_parse_enum_bit(ctx, subelem_attrs, data, kw, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001023 break;
1024 case YANG_CASE:
1025 break;
1026 case YANG_CHOICE:
1027 break;
1028 case YANG_CONFIG:
David Sedláke1a30302019-07-10 13:49:38 +02001029 ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001030 break;
1031 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +02001032 case YANG_DESCRIPTION:
1033 case YANG_ORGANIZATION:
1034 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +02001035 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001036 break;
1037 case YANG_CONTAINER:
1038 break;
1039 case YANG_DEFAULT:
David Sedláke7084ce2019-07-10 16:44:15 +02001040 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1041 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001042 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001043 case YANG_DEVIATE:
1044 break;
1045 case YANG_DEVIATION:
1046 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001047 case YANG_ERROR_APP_TAG:
David Sedlák2ce1be62019-07-10 16:15:09 +02001048 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1049 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001050 break;
1051 case YANG_ERROR_MESSAGE:
David Sedlákc1771b12019-07-10 15:55:46 +02001052 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001053 break;
1054 case YANG_EXTENSION:
David Sedlák1f90d252019-07-10 17:09:32 +02001055 ret = yin_parse_extension(ctx, subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001056 break;
1057 case YANG_FEATURE:
1058 break;
1059 case YANG_FRACTION_DIGITS:
David Sedlákf75d55e2019-07-12 16:52:50 +02001060 ret = yin_parse_fracdigits(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001061 break;
1062 case YANG_GROUPING:
1063 break;
1064 case YANG_IDENTITY:
1065 break;
1066 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +02001067 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák32488102019-07-15 17:44:10 +02001068 (const char ***)subelem_info_rec->dest, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001069 break;
1070 case YANG_IMPORT:
David Sedlák1f90d252019-07-10 17:09:32 +02001071 ret = yin_parse_import(ctx, subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001072 break;
1073 case YANG_INCLUDE:
1074 break;
1075 case YANG_INPUT:
1076 break;
1077 case YANG_KEY:
1078 break;
1079 case YANG_LEAF:
1080 break;
1081 case YANG_LEAF_LIST:
1082 break;
1083 case YANG_LENGTH:
David Sedlák438ae432019-07-11 15:36:54 +02001084 type = (struct lysp_type *)subelem_info_rec->dest;
1085 type->length = calloc(1, sizeof *type->length);
1086 LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
1087 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->length);
1088 type->flags |= LYS_SET_LENGTH;
David Sedlákd6e56892019-07-01 15:40:24 +02001089 break;
1090 case YANG_LIST:
1091 break;
1092 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +02001093 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001094 break;
1095 case YANG_MAX_ELEMENTS:
1096 break;
1097 case YANG_MIN_ELEMENTS:
1098 break;
1099 case YANG_MODIFIER:
David Sedlákce77bf52019-07-11 16:59:31 +02001100 ret = yin_parse_modifier(ctx, subelem_attrs, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001101 break;
1102 case YANG_MODULE:
1103 break;
1104 case YANG_MUST:
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001105 ret = yin_parse_must(ctx, subelem_attrs, data, (struct lysp_restr **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001106 break;
1107 case YANG_NAMESPACE:
David Sedlák2ce1be62019-07-10 16:15:09 +02001108 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1109 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001110 break;
1111 case YANG_NOTIFICATION:
1112 break;
1113 case YANG_ORDERED_BY:
1114 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001115 case YANG_OUTPUT:
1116 break;
1117 case YANG_PATH:
David Sedlák58979872019-07-12 11:42:43 +02001118 type = (struct lysp_type *)subelem_info_rec->dest;
1119 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, &type->path,
1120 YIN_ARG_VALUE, Y_STR_ARG, exts);
1121 type->flags |= LYS_SET_PATH;
David Sedlákd6e56892019-07-01 15:40:24 +02001122 break;
1123 case YANG_PATTERN:
David Sedlákd3983112019-07-12 11:20:56 +02001124 ret = yin_parse_pattern(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001125 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02001126 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02001127 case YANG_POSITION:
David Sedlák5545f5d2019-07-11 11:55:16 +02001128 ret = yin_parse_value_pos_element(ctx, subelem_attrs, data, kw,
1129 (struct lysp_type_enum *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001130 break;
1131 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +02001132 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +02001133 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001134 break;
1135 case YANG_PRESENCE:
1136 break;
1137 case YANG_RANGE:
David Sedlákb7296dd2019-07-11 14:58:38 +02001138 type = (struct lysp_type *)subelem_info_rec->dest;
1139 type->range = calloc(1, sizeof *type->range);
David Sedlák66d7c842019-07-11 15:06:04 +02001140 LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlákb7296dd2019-07-11 14:58:38 +02001141 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->range);
David Sedlák438ae432019-07-11 15:36:54 +02001142 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +02001143 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001144 case YANG_REFINE:
1145 break;
1146 case YANG_REQUIRE_INSTANCE:
David Sedlákcf5569a2019-07-11 13:31:34 +02001147 ret = yin_pasrse_reqinstance(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001148 break;
1149 case YANG_REVISION:
1150 break;
1151 case YANG_REVISION_DATE:
David Sedlák1f90d252019-07-10 17:09:32 +02001152 ret = yin_parse_revision_date(ctx, subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001153 break;
1154 case YANG_RPC:
1155 break;
1156 case YANG_STATUS:
David Sedlák1f90d252019-07-10 17:09:32 +02001157 ret = yin_parse_status(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001158 break;
1159 case YANG_SUBMODULE:
1160 break;
1161 case YANG_TYPE:
1162 break;
1163 case YANG_TYPEDEF:
1164 break;
1165 case YANG_UNIQUE:
David Sedláka5b1d382019-07-10 16:31:09 +02001166 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
1167 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001168 break;
1169 case YANG_UNITS:
David Sedláka5b1d382019-07-10 16:31:09 +02001170 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1171 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001172 break;
1173 case YANG_USES:
1174 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001175 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +02001176 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001177 break;
1178 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +02001179 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001180 break;
1181 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +02001182 ret = yin_parse_yin_element_element(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001183 break;
1184 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02001185 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +02001186 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02001187 break;
1188 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02001189 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02001190 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02001191 }
David Sedlák3ffbc522019-07-02 17:49:28 +02001192 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001193 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001194 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +02001195 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02001196 }
1197 } else {
1198 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02001199 /* save text content, if text_content isn't set, it's just ignored */
David Sedláka62750b2019-07-16 11:21:31 +02001200 /* TODO add text validation */
David Sedlák3ffbc522019-07-02 17:49:28 +02001201 if (text_content) {
1202 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001203 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02001204 if (!*text_content) {
1205 free(out);
1206 return LY_EMEM;
1207 }
1208 } else {
1209 if (out_len == 0) {
David Sedlák99295322019-07-17 11:34:18 +02001210 *text_content = lydict_insert(ctx->xml_ctx.ctx, "", 0);
David Sedlák3ffbc522019-07-02 17:49:28 +02001211 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001212 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02001213 }
1214 }
1215 }
David Sedlákd6e56892019-07-01 15:40:24 +02001216 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001217 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
David Sedlákd6e56892019-07-01 15:40:24 +02001218 }
David Sedlák555c7202019-07-04 12:14:12 +02001219
David Sedlákda8ffa32019-07-08 14:17:10 +02001220 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02001221 }
1222
1223cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +02001224 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001225 return ret;
1226}
1227
David Sedlák619db942019-07-03 14:47:30 +02001228LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001229yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, char *rev,
David Sedlák619db942019-07-03 14:47:30 +02001230 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +02001231{
David Sedlák3ffbc522019-07-02 17:49:28 +02001232 const char *temp_rev;
David Sedlák968ac342019-07-11 15:17:59 +02001233 struct yin_subelement subelems[1] = {
1234 {YANG_CUSTOM, NULL, 0}
1235 };
David Sedlák81e04022019-04-05 15:05:46 +02001236
David Sedlák292763b2019-07-09 11:10:53 +02001237 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
David Sedláka304bc62019-07-17 10:17:58 +02001238 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
1239 FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID);
David Sedlákda63c082019-06-04 13:52:23 +02001240
1241 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +02001242 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +02001243
David Sedlákda8ffa32019-07-08 14:17:10 +02001244 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001245}
David Sedlák00250342019-06-21 14:19:39 +02001246
David Sedláke1a30302019-07-10 13:49:38 +02001247/**
1248 * @brief Parse config element.
1249 *
1250 * @param[in] ctx Yin parser context for logging and to store current state.
1251 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1252 * @param[in,out] data Data to read from, always moved to currently handled character.
1253 * @param[in,out] flags Flags to add to.
1254 * @param[in,out] exts Extension instances to add to.
1255 *
1256 * @return LY_ERR values.
1257 */
1258static LY_ERR
1259yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1260 struct lysp_ext_instance **exts)
1261{
1262 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001263 struct yin_subelement subelems[1] = {
1264 {YANG_CUSTOM, NULL, 0}
1265 };
David Sedláke1a30302019-07-10 13:49:38 +02001266
David Sedlák1f90d252019-07-10 17:09:32 +02001267 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
David Sedláke1a30302019-07-10 13:49:38 +02001268 if (strcmp(temp_val, "true") == 0) {
1269 *flags |= LYS_CONFIG_W;
1270 } else if (strcmp(temp_val, "false") == 0) {
1271 *flags |= LYS_CONFIG_R;
1272 } else {
1273 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
1274 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1275 return LY_EVALID;
1276 }
1277 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1278
1279 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1280}
1281
David Sedlák3ffbc522019-07-02 17:49:28 +02001282LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +02001283yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1284 struct lysp_ext_instance **exts)
1285{
1286 const char *temp_version = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001287 struct yin_subelement subelems[1] = {
1288 {YANG_CUSTOM, NULL, 0}
1289 };
David Sedlák92147b02019-07-09 14:01:01 +02001290
David Sedlák1f90d252019-07-10 17:09:32 +02001291 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
David Sedlák92147b02019-07-09 14:01:01 +02001292 if (strcmp(temp_version, "1.0") == 0) {
1293 *version = LYS_VERSION_1_0;
1294 } else if (strcmp(temp_version, "1.1") == 0) {
1295 *version = LYS_VERSION_1_1;
1296 } else {
1297 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
1298 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1299 return LY_EVALID;
1300 }
1301 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1302 ctx->mod_version = *version;
1303
1304 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1305}
1306
1307LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001308yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_module *mod)
David Sedlák736fd0d2019-02-15 16:06:31 +01001309{
David Sedlák736fd0d2019-02-15 16:06:31 +01001310 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +02001311 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +02001312 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02001313
David Sedlák968ac342019-07-11 15:17:59 +02001314 struct yin_subelement subelems[5] = {
1315 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1316 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1317 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1318 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1319 {YANG_CUSTOM, NULL, 0}
1320 };
David Sedlák736fd0d2019-02-15 16:06:31 +01001321
David Sedlák92147b02019-07-09 14:01:01 +02001322 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +02001323 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, YANG_IMPORT));
David Sedlákda8ffa32019-07-08 14:17:10 +02001324 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +02001325 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +02001326 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, mod->imports, mod->mod->prefix, &imp->prefix), LY_EVALID);
David Sedlák00250342019-06-21 14:19:39 +02001327
David Sedlákda8ffa32019-07-08 14:17:10 +02001328 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +01001329}
1330
David Sedlák11900c82019-06-18 16:29:12 +02001331LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +02001332yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1333 struct lysp_ext_instance **exts)
1334{
1335 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001336 struct yin_subelement subelems[1] = {
1337 {YANG_CUSTOM, NULL, 0}
1338 };
David Sedlák1fdb2522019-07-09 16:22:57 +02001339
David Sedlák1f90d252019-07-10 17:09:32 +02001340 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
David Sedlák1fdb2522019-07-09 16:22:57 +02001341 if (strcmp(temp_val, "true") == 0) {
1342 *flags |= LYS_MAND_TRUE;
1343 } else if (strcmp(temp_val, "false") == 0) {
1344 *flags |= LYS_MAND_FALSE;
1345 } else {
1346 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
1347 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1348 return LY_EVALID;
1349 }
1350 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1351
1352 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1353}
1354
1355LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001356yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
David Sedlák1fdb2522019-07-09 16:22:57 +02001357 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +02001358{
David Sedlák3ffbc522019-07-02 17:49:28 +02001359 const char *value = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001360 struct yin_subelement subelems[1] = {
1361 {YANG_CUSTOM, NULL, 0}
1362 };
David Sedlák3ffbc522019-07-02 17:49:28 +02001363
David Sedlák292763b2019-07-09 11:10:53 +02001364 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, YANG_STATUS));
David Sedlák11900c82019-06-18 16:29:12 +02001365 if (strcmp(value, "current") == 0) {
1366 *flags |= LYS_STATUS_CURR;
1367 } else if (strcmp(value, "deprecated") == 0) {
1368 *flags |= LYS_STATUS_DEPRC;
1369 } else if (strcmp(value, "obsolete") == 0) {
1370 *flags |= LYS_STATUS_OBSLT;
1371 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001372 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1373 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001374 return LY_EVALID;
1375 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001376 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001377
David Sedlákda8ffa32019-07-08 14:17:10 +02001378 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001379}
1380
David Sedlák11900c82019-06-18 16:29:12 +02001381LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001382yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1383{
1384 struct lysp_when *when;
1385 when = calloc(1, sizeof *when);
1386 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1f90d252019-07-10 17:09:32 +02001387 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
David Sedlák32eee7b2019-07-09 12:38:44 +02001388 *when_p = when;
David Sedlák968ac342019-07-11 15:17:59 +02001389 struct yin_subelement subelems[3] = {
1390 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1391 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1392 {YANG_CUSTOM, NULL, 0}
1393 };
David Sedlák32eee7b2019-07-09 12:38:44 +02001394
1395 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1396}
1397
1398LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001399yin_parse_yin_element_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001400 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001401{
David Sedlák3ffbc522019-07-02 17:49:28 +02001402 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001403 struct yin_subelement subelems[1] = {
1404 {YANG_CUSTOM, NULL, 0}
1405 };
David Sedlák2721d3d2019-06-21 15:37:41 +02001406
David Sedlák1f90d252019-07-10 17:09:32 +02001407 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_YIN_ELEMENT));
David Sedlák2721d3d2019-06-21 15:37:41 +02001408 if (strcmp(temp_val, "true") == 0) {
1409 *flags |= LYS_YINELEM_TRUE;
1410 } else if (strcmp(temp_val, "false") == 0) {
1411 *flags |= LYS_YINELEM_FALSE;
1412 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001413 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1414 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001415 return LY_EVALID;
1416 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001417 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001418
David Sedlákda8ffa32019-07-08 14:17:10 +02001419 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001420}
1421
1422LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001423yin_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 +02001424 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001425{
1426 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001427 char *out;
1428 const char *name, *prefix;
1429 size_t out_len, prefix_len, name_len;
1430 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001431 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001432 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1433 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001434
David Sedlákda8ffa32019-07-08 14:17:10 +02001435 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001436
1437 e->yin = 0;
1438 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001439 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001440 e->insubstmt = subelem;
1441 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001442 e->yin |= LYS_YIN;
1443
David Sedlákb1a78352019-06-28 16:16:29 +02001444 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02001445 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02001446 if (!iter->prefix) {
1447 new_subelem = calloc(1, sizeof(*new_subelem));
1448 if (!e->child) {
1449 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001450 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001451 last_subelem->next = new_subelem;
1452 }
1453 last_subelem = new_subelem;
1454
1455 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001456 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1457 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001458 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001459 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1460 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001461 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001462 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1463 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001464 }
1465 }
1466 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001467
David Sedlákf250ecf2019-07-01 11:02:05 +02001468 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001469 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1470 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001471 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001472 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1473 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001474 if (!name) {
1475 /* end of extension instance reached */
1476 break;
1477 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001478 LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, prefix, prefix_len, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +02001479 if (!e->child) {
1480 e->child = new_subelem;
1481 } else {
1482 last_subelem->next = new_subelem;
1483 }
1484 last_subelem = new_subelem;
1485 }
David Sedlák555c7202019-07-04 12:14:12 +02001486 } else {
1487 /* save text content */
1488 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001489 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001490 if (!e->argument) {
1491 free(out);
1492 return LY_EMEM;
1493 }
1494 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001495 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001496 LY_CHECK_RET(!e->argument, LY_EMEM);
1497 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001498 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001499 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001500 }
David Sedlákb1a78352019-06-28 16:16:29 +02001501 }
1502
1503 return LY_SUCCESS;
1504}
1505
1506LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001507yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix,
David Sedlákf250ecf2019-07-01 11:02:05 +02001508 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001509{
1510 LY_ERR ret = LY_SUCCESS;
1511 const char *temp_prefix, *temp_name;
1512 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001513 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001514 int dynamic;
1515 struct yin_arg_record *subelem_args = NULL;
1516 struct lysp_stmt *last = NULL, *new = NULL;
1517
1518 /* allocate new structure for element */
1519 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001520 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1521 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001522
1523 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001524 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001525 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001526 /* add new element to linked-list */
1527 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001528 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001529 if (!(*element)->child) {
1530 /* save first */
1531 (*element)->child = new;
1532 } else {
1533 last->next = new;
1534 }
1535 last = new;
1536
1537 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001538 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 +02001539 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001540 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001541 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001542 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1543 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001544 /* attributes with prefix are ignored */
1545 if (!temp_prefix) {
1546 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001547 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001548 if (!last->arg) {
1549 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001550 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001551 ret = LY_EMEM;
1552 goto err;
1553 }
1554 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001555 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1556 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001557 }
1558 }
1559 }
1560
1561 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001562 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001563 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001564 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001565 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001566 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 +02001567 LY_CHECK_GOTO(ret, err);
1568 if (!name) {
1569 /* end of element reached */
1570 break;
1571 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001572 ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, temp_prefix, temp_prefix_len, data, &last->next);
David Sedlákb1a78352019-06-28 16:16:29 +02001573 LY_CHECK_GOTO(ret, err);
1574 last = last->next;
1575 }
1576 } else {
1577 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001578 if (out_len != 0) {
1579 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001580 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001581 if (!(*element)->arg) {
1582 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001583 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001584 ret = LY_EMEM;
1585 goto err;
1586 }
1587 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001588 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1589 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001590 }
David Sedlákb1a78352019-06-28 16:16:29 +02001591 }
1592 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001593 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 +02001594 LY_CHECK_GOTO(ret, err);
1595 }
1596
David Sedlákda8ffa32019-07-08 14:17:10 +02001597 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001598 return LY_SUCCESS;
1599
1600err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001601 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001602 return ret;
1603}
1604
1605LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001606yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001607 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001608{
David Sedlák968ac342019-07-11 15:17:59 +02001609 struct yin_subelement subelems[2] = {
1610 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1611 {YANG_CUSTOM, NULL, 0}
1612 };
David Sedlák9494eb22019-06-21 16:06:53 +02001613
David Sedlák292763b2019-07-09 11:10:53 +02001614 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, YANG_ARGUMENT));
David Sedlák9494eb22019-06-21 16:06:53 +02001615
David Sedlákda8ffa32019-07-08 14:17:10 +02001616 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001617}
1618
1619LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001620yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_ext **extensions)
David Sedlák11900c82019-06-18 16:29:12 +02001621{
David Sedlák11900c82019-06-18 16:29:12 +02001622 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001623 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001624 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, YANG_EXTENSION));
David Sedlák619db942019-07-03 14:47:30 +02001625
David Sedlák3ffbc522019-07-02 17:49:28 +02001626 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
David Sedlák968ac342019-07-11 15:17:59 +02001627 struct yin_subelement subelems[5] = {
1628 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1629 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1630 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1631 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1632 {YANG_CUSTOM, NULL, 0}
1633 };
David Sedlák11900c82019-06-18 16:29:12 +02001634
David Sedlákda8ffa32019-07-08 14:17:10 +02001635 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001636}
1637
David Sedlák00250342019-06-21 14:19:39 +02001638/**
1639 * @brief Parse module substatements.
1640 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001641 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001642 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001643 * @param[in,out] data Data to read from.
1644 * @param[out] mod Parsed module structure.
1645 *
1646 * @return LY_ERR values.
1647 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001648static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001649yin_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 +02001650{
David Sedlák968ac342019-07-11 15:17:59 +02001651 struct yin_subelement subelems[9] = {
1652 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
1653 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1654 {YANG_EXTENSION, &(*mod)->exts, 0},
1655 {YANG_IMPORT, *mod, 0},
1656 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1657 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
1658 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1659 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
1660 {YANG_CUSTOM, NULL, 0}
1661 };
David Sedlák3b4db242018-10-19 16:11:01 +02001662
David Sedlák292763b2019-07-09 11:10:53 +02001663 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &(*mod)->mod->name, Y_IDENTIF_ARG, YANG_MODULE));
David Sedlák4a4c0722018-11-26 17:03:10 +01001664
David Sedlákda8ffa32019-07-08 14:17:10 +02001665 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001666}
1667
1668LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001669yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001670{
David Sedláke4889912018-11-02 09:52:40 +01001671 LY_ERR ret = LY_SUCCESS;
1672 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001673 struct lysp_module *mod_p = NULL;
1674 const char *prefix, *name;
1675 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001676
David Sedlák1f90d252019-07-10 17:09:32 +02001677 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001678
David Sedlákda8ffa32019-07-08 14:17:10 +02001679 struct yin_parser_ctx yin_ctx;
1680
1681 /* initialize context */
1682 memset(&yin_ctx, 0, sizeof yin_ctx);
1683 yin_ctx.xml_ctx.ctx = ctx;
1684 yin_ctx.xml_ctx.line = 1;
1685
David Sedláke4889912018-11-02 09:52:40 +01001686
David Sedlák3017da42019-02-15 09:48:04 +01001687 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001688 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001689 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1f90d252019-07-10 17:09:32 +02001690 ret = yin_load_attributes(&yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02001691 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001692 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001693 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001694 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1695 ret = LY_EINVAL;
1696 goto cleanup;
1697 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001698 LOGVAL_PARSER((struct lys_parser_ctx *)&yin_ctx, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".",
David Sedlák79e50cb2019-06-05 16:33:09 +02001699 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001700 ret = LY_EVALID;
1701 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001702 }
1703
David Sedlák3017da42019-02-15 09:48:04 +01001704 /* allocate module */
1705 mod_p = calloc(1, sizeof *mod_p);
1706 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1707 mod_p->mod = mod;
1708 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001709
David Sedlák00250342019-06-21 14:19:39 +02001710 /* parse module substatements */
David Sedlák1f90d252019-07-10 17:09:32 +02001711 ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001712 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001713
David Sedlák3017da42019-02-15 09:48:04 +01001714 mod_p->parsing = 0;
1715 mod->parsed = mod_p;
1716
1717cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001718 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001719 lysp_module_free(mod_p);
1720 }
David Sedlák1f90d252019-07-10 17:09:32 +02001721 FREE_ARRAY(&yin_ctx, attrs, free_arg_rec);
David Sedlákda8ffa32019-07-08 14:17:10 +02001722 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001723 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001724}