blob: 7ab51b6106b52a0e807ab83588c6e7da0eec0a7a [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ákda8ffa32019-07-08 14:17:10 +0200276 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200277 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200278 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200279 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200280 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 +0200281 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200282 }
283 }
284 }
285
David Sedlák292763b2019-07-09 11:10:53 +0200286 /* anything else than Y_MAYBE_STR_ARG is mandatory */
287 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200288 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 +0200289 return LY_EVALID;
290 }
291
292 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200293}
294
David Sedlákd6e56892019-07-01 15:40:24 +0200295/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200296 * @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 +0200297 *
298 * @param[in] type Type of wanted record.
299 * @param[in] array_size Size of array.
300 * @param[in] array Searched array.
301 *
302 * @return Pointer to desired record on success, NULL if element is not in the array.
303 */
David Sedlákb4e44562019-07-04 15:42:12 +0200304static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200305get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200306{
David Sedlákb0faad82019-07-04 14:28:59 +0200307 signed char left = 0, right = array_size - 1, middle;
308
309 while (left <= right) {
310 middle = left + (right - left) / 2;
311
312 if (array[middle].type == type) {
313 return &array[middle];
314 }
315
316 if (array[middle].type < type) {
317 left = middle + 1;
318 } else {
319 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200320 }
321 }
322
323 return NULL;
324}
325
David Sedlákbba38e52019-07-09 15:20:01 +0200326/**
327 * @brief Helper function to check mandatory constraint of subelement.
328 *
329 * @param[in,out] ctx Yin parser context for logging and to store current state.
330 * @param[in] subelem_info Array of information about subelements.
331 * @param[in] subelem_info_size Size of subelem_info array.
332 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
333 *
334 * @return LY_ERR values.
335 */
336static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200337yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200338 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200339{
David Sedlákb0faad82019-07-04 14:28:59 +0200340 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200341 /* 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 +0200342 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200343 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 +0200344 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200345 return LY_EVALID;
346 }
347 }
348
349 return LY_SUCCESS;
350}
351
David Sedlákbba38e52019-07-09 15:20:01 +0200352/**
353 * @brief Helper function to check "first" constraint of subelement.
354 *
355 * @param[in,out] ctx Yin parser context for logging and to store current state.
356 * @param[in] subelem_info Array of information about subelements.
357 * @param[in] subelem_info_size Size of subelem_info array.
358 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
359 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
360 *
361 * @return LY_ERR values.
362 */
363static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200364yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200365 signed char subelem_info_size, enum yang_keyword current_element,
366 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200367{
David Sedlákb0faad82019-07-04 14:28:59 +0200368 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200369 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200370 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 +0200371 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200372 return LY_EVALID;
373 }
374 }
375
376 return LY_SUCCESS;
377}
378
David Sedlákbba38e52019-07-09 15:20:01 +0200379/**
380 * @brief Helper function to check if array of information about subelements is in ascending order.
381 *
382 * @param[in] subelem_info Array of information about subelements.
383 * @param[in] subelem_info_size Size of subelem_info array.
384 *
385 * @return True iff subelem_info array is in ascending order, False otherwise.
386 */
David Sedlák5545f5d2019-07-11 11:55:16 +0200387#ifndef NDEBUG
David Sedlákbba38e52019-07-09 15:20:01 +0200388static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200389is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
390{
David Sedlák292763b2019-07-09 11:10:53 +0200391 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200392
393 for (signed char i = 0; i < subelem_info_size; ++i) {
394 if (subelem_info[i].type <= current) {
395 return false;
396 }
397 current = subelem_info[i].type;
398 }
399
400 return true;
401}
David Sedlák5545f5d2019-07-11 11:55:16 +0200402#endif
David Sedlákb0faad82019-07-04 14:28:59 +0200403
David Sedlákd6e56892019-07-01 15:40:24 +0200404/**
David Sedlákb4e44562019-07-04 15:42:12 +0200405 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
406 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200407 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200408 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200409 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200410 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200411 * @param[in] kw Type of current element.
412 * @param[out] value Where value of attribute should be stored.
413 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200414 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200415 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200416 *
David Sedlákd6e56892019-07-01 15:40:24 +0200417 * @return LY_ERR values.
418 */
David Sedlákb4e44562019-07-04 15:42:12 +0200419static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200420yin_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 +0200421 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 +0200422{
David Sedlák1f90d252019-07-10 17:09:32 +0200423 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200424 struct yin_subelement subelems[1] = {
425 {YANG_CUSTOM, NULL, 0}
426 };
David Sedlákb4e44562019-07-04 15:42:12 +0200427
David Sedlákda8ffa32019-07-08 14:17:10 +0200428 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200429}
430
431/**
David Sedlákd3983112019-07-12 11:20:56 +0200432 * @brief Parse pattern element.
433 *
434 * @param[in,out] ctx Yin parser context for logging and to store current state.
435 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
436 * @param[in,out] data Data to read from, always moved to currently handled character.
437 * @param[in,out] patterns Restrictions to add to.
438 *
439 * @return LY_ERR values.
440 */
441static LY_ERR
442yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
443 struct lysp_type *type)
444{
445 const char *real_value = NULL;
446 char *saved_value = NULL;
447 struct lysp_restr *restr;
448
449 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
450 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
451 size_t len = strlen(real_value);
452
453 saved_value = malloc(len + 2);
454 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
455 memmove(saved_value + 1, real_value, len);
456 FREE_STRING(ctx->xml_ctx.ctx, real_value);
457 saved_value[0] = 0x06;
458 saved_value[len + 1] = '\0';
459 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
460 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
461 type->flags |= LYS_SET_PATTERN;
462
463 struct yin_subelement subelems[6] = {
464 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
465 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
466 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
467 {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
468 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
469 {YANG_CUSTOM, NULL, 0}
470 };
471 return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
472}
473
David Sedlákf75d55e2019-07-12 16:52:50 +0200474static LY_ERR
475yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
476 struct lysp_type *type)
477{
478 const char *temp_val = NULL;
479 char *ptr;
480 unsigned long int num;
481
482 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_FRACTION_DIGITS));
483
484 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
485 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
486 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
487 return LY_EVALID;
488 }
489
490 errno = 0;
491 num = strtoul(temp_val, &ptr, 10);
492 if (*ptr != '\0') {
493 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
494 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
495 return LY_EVALID;
496 }
497 if ((errno == ERANGE) || (num > 18)) {
498 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, "fraction-digits");
499 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
500 return LY_EVALID;
501 }
David Sedlák2ab5d8e2019-07-16 11:19:41 +0200502 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200503 type->fraction_digits = num;
504 type->flags |= LYS_SET_FRDIGITS;
505 struct yin_subelement subelems[1] = {
506 {YANG_CUSTOM, &index, 0}
507 };
508 return yin_parse_content(ctx, subelems, 1, data, YANG_FRACTION_DIGITS, NULL, &type->exts);
509}
510
David Sedlák07869a52019-07-12 14:28:19 +0200511/**
512 * @brief Parse enum or bit element.
513 *
514 * @param[in,out] ctx YIN parser context for logging and to store current state.
515 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
516 * @param[in,out] data Data to read from, always moved to currently handled character.
517 * @param[in] enum_kw Identification of actual keyword, can be set to YANG_BIT or YANG_ENUM.
518 * @param[in,out] enums Enums or bits to add to.
519 *
520 * @return LY_ERR values.
521 */
David Sedlákca36c422019-07-12 12:47:55 +0200522static LY_ERR
David Sedlák07869a52019-07-12 14:28:19 +0200523yin_parse_enum_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
524 enum yang_keyword enum_kw, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200525{
David Sedlák07869a52019-07-12 14:28:19 +0200526 assert(enum_kw == YANG_BIT || enum_kw == YANG_ENUM);
527 struct lysp_type_enum *en;
528 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM);
529 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, enum_kw));
530 type->flags |= (enum_kw == YANG_ENUM) ? LYS_SET_ENUM : LYS_SET_BIT;
531 if (enum_kw == YANG_ENUM) {
532 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 +0200533 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
David Sedlák07869a52019-07-12 14:28:19 +0200534 }
535 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, ly_stmt2str(enum_kw), en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200536
537 struct yin_subelement subelems[6] = {
David Sedlák07869a52019-07-12 14:28:19 +0200538 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
539 {YANG_IF_FEATURE, &en->iffeatures, 0},
David Sedlák07869a52019-07-12 14:28:19 +0200540 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
541 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200542 {(enum_kw == YANG_ENUM) ? YANG_VALUE : YANG_POSITION, en, YIN_SUBELEM_UNIQUE},
David Sedlákca36c422019-07-12 12:47:55 +0200543 {YANG_CUSTOM, NULL, 0}
544 };
David Sedlák07869a52019-07-12 14:28:19 +0200545 return yin_parse_content(ctx, subelems, 6, data, enum_kw, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200546}
547
David Sedlákd3983112019-07-12 11:20:56 +0200548/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200549 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
550 * more instances, such as base or if-feature.
551 *
552 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200553 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200554 * @param[in,out] data Data to read from, always moved to currently handled character.
555 * @param[in] kw Type of current element.
556 * @param[out] values Parsed values to add to.
557 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200558 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200559 * @param[in,out] exts Extension instance to add to.
560 *
561 * @return LY_ERR values.
562 */
563static LY_ERR
564yin_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 +0200565 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 +0200566{
567 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200568 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200569 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200570 struct yin_subelement subelems[1] = {
571 {YANG_CUSTOM, &index, 0}
572 };
573
David Sedlák1f90d252019-07-10 17:09:32 +0200574 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200575
576 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
577}
578
579/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200580 * @brief Parse require instance element.
581 *
582 * @param[in,out] ctx Yin parser context for logging and to store current state.
583 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
584 * @param[in,out] data Data to read from, always moved to currently handled character.
585 * @prama[out] type Type structure to store value, flag and extensions.
586 *
587 * @return LY_ERR values.
588 */
589static LY_ERR
590yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
591 const char **data, struct lysp_type *type)
592{
593 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200594 struct yin_subelement subelems[1] = {
595 {YANG_CUSTOM, NULL, 0}
596 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200597
598 type->flags |= LYS_SET_REQINST;
599 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
600 if (strcmp(temp_val, "true") == 0) {
601 type->require_instance = 1;
602 } else if (strcmp(temp_val, "false") != 0) {
603 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "require-instance");
604 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
605 return LY_EVALID;
606 }
607 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
608
609 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
610}
611
612/**
David Sedlákce77bf52019-07-11 16:59:31 +0200613 * @brief Parse modifier element.
614 *
615 * @param[in,out] ctx Yin parser context for logging and to store current state.
616 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
617 * @param[in,out] data Data to read from, always moved to currently handled character.
618 * @param[in,out] pat Value to write to.
619 * @param[in,out] exts Extension instances to add to.
620 *
621 * @return LY_ERR values.
622 */
623static LY_ERR
624yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
625 const char **pat, struct lysp_ext_instance **exts)
626{
David Sedlákd3983112019-07-12 11:20:56 +0200627 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200628 const char *temp_val;
629 char *modified_val;
630 struct yin_subelement subelems[1] = {
631 {YANG_CUSTOM, NULL, 0}
632 };
633
634 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER));
635 if (strcmp(temp_val, "invert-match") != 0) {
636 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "modifier");
637 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
638 return LY_EVALID;
639 }
David Sedlákd3983112019-07-12 11:20:56 +0200640 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200641
642 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200643 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200644 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200645 strcpy(modified_val, *pat);
646 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200647
648 /* modify the new value */
649 modified_val[0] = 0x15;
650 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
651
652 return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts);
653}
654
655/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200656 * @brief Parse a restriction element (length, range or one instance of must).
657 *
658 * @param[in,out] ctx Yin parser context for logging and to store current state.
659 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
660 * @param[in,out] data Data to read from, always moved to currently handled character.
661 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
662 * @param[in]
663 */
664static LY_ERR
665yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
666 enum yang_keyword restr_kw, struct lysp_restr *restr)
667{
668 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
669 struct yin_subelement subelems[5] = {
David Sedlák968ac342019-07-11 15:17:59 +0200670 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
671 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
672 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
673 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
674 {YANG_CUSTOM, NULL, 0}
675 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200676 /* argument of must is called condition, but argument of length and range is called value */
677 enum YIN_ARGUMENT arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
678 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
679
680 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
681}
682
683/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200684 * @brief Parse must element.
685 *
686 * @param[in,out] ctx YIN parser context for logging and to store current state.
687 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
688 * @param[in,out] data Data to read from, always moved to currently handled character.
689 * @param[in,out] restrs Restrictions to add to.
690 *
691 * @return LY_ERR values.
692 */
693static LY_ERR
694yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
695{
696 struct lysp_restr *restr;
697
698 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
699 return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr);
700}
701
702/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200703 * @brief Parse position or value element.
704 *
705 * @param[in,out] ctx YIN parser context for logging and to store current state.
706 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
707 * @param[in,out] data Data to read from, always moved to currently handled character.
708 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
709 * @param[out] enm Enum structure to save value, flags and extensions.
710 *
711 * @return LY_ERR values.
712 */
713static LY_ERR
714yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
715 enum yang_keyword kw, struct lysp_type_enum *enm)
716{
717 assert(kw == YANG_POSITION || kw == YANG_VALUE);
718 const char *temp_val = NULL;
719 char *ptr;
720 long int num;
721 unsigned long int unum;
722
723 /* set value flag */
724 enm->flags |= LYS_SET_VALUE;
725
726 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200727 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +0200728 if (!temp_val || (temp_val[0] == '+') || ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == YANG_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200729 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
730 goto error;
731 }
732
733 /* convert value */
734 errno = 0;
735 if (kw == YANG_VALUE) {
736 num = strtol(temp_val, &ptr, 10);
737 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
738 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
739 goto error;
740 }
741 } else {
742 unum = strtoul(temp_val, &ptr, 10);
743 if (unum > UINT64_C(4294967295)) {
744 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
745 goto error;
746 }
747 }
748 /* check if whole argument value was converted */
749 if (*ptr != '\0') {
750 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 +0200751 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +0200752 }
753 if (errno == ERANGE) {
754 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw));
755 goto error;
756 }
757 /* save correctly ternary operator can't be used because num and unum have different signes */
758 if (kw == YANG_VALUE) {
759 enm->value = num;
760 } else {
761 enm->value = unum;
762 }
763 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
764
765 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200766 struct yin_subelement subelems[1] = {
767 {YANG_CUSTOM, NULL, 0}
768 };
David Sedlák5545f5d2019-07-11 11:55:16 +0200769 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
770
771 error:
772 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
773 return LY_EVALID;
774}
775
776/**
David Sedlákc1771b12019-07-10 15:55:46 +0200777 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200778 * text element as child
779 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200780 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200781 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200782 * @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 +0200783 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200784 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200785 *
786 * @return LY_ERR values.
787 */
788static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200789yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200790 const char **value, struct lysp_ext_instance **exts)
791{
792 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
793
David Sedlák968ac342019-07-11 15:17:59 +0200794 struct yin_subelement subelems[2] = {
795 {YANG_CUSTOM, NULL, 0},
796 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
797 };
David Sedlákb4e44562019-07-04 15:42:12 +0200798
David Sedlákda8ffa32019-07-08 14:17:10 +0200799 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200800}
801
802/**
David Sedlákc1771b12019-07-10 15:55:46 +0200803 * @brief Parse error-message element.
804 *
805 * @param[in,out] ctx Yin parser context for logging and to store current state.
806 * @param[in,out] data Data to read from.
807 * @param[out] value Where the content of error-message element should be stored.
808 * @param[in,out] exts Extension instance to add to.
809 *
810 * @return LY_ERR values.
811 */
812static LY_ERR
813yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
814 struct lysp_ext_instance **exts)
815{
David Sedlák968ac342019-07-11 15:17:59 +0200816 struct yin_subelement subelems[2] = {
817 {YANG_CUSTOM, NULL, 0},
818 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
819 };
David Sedlákc1771b12019-07-10 15:55:46 +0200820
821 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
822}
823
824/**
David Sedlákb4e44562019-07-04 15:42:12 +0200825 * @brief Map keyword type to substatement info.
826 *
827 * @param[in] kw Keyword type.
828 *
829 * @return correct LYEXT_SUBSTMT information.
830 */
831static LYEXT_SUBSTMT
832kw2lyext_substmt(enum yang_keyword kw)
833{
834 switch (kw) {
835 case YANG_ARGUMENT:
836 return LYEXT_SUBSTMT_ARGUMENT;
837 case YANG_BASE:
838 return LYEXT_SUBSTMT_BASE;
839 case YANG_BELONGS_TO:
840 return LYEXT_SUBSTMT_BELONGSTO;
841 case YANG_CONTACT:
842 return LYEXT_SUBSTMT_CONTACT;
843 case YANG_DEFAULT:
844 return LYEXT_SUBSTMT_DEFAULT;
845 case YANG_DESCRIPTION:
846 return LYEXT_SUBSTMT_DESCRIPTION;
847 case YANG_ERROR_APP_TAG:
848 return LYEXT_SUBSTMT_ERRTAG;
849 case YANG_ERROR_MESSAGE:
850 return LYEXT_SUBSTMT_ERRMSG;
851 case YANG_KEY:
852 return LYEXT_SUBSTMT_KEY;
853 case YANG_NAMESPACE:
854 return LYEXT_SUBSTMT_NAMESPACE;
855 case YANG_ORGANIZATION:
856 return LYEXT_SUBSTMT_ORGANIZATION;
857 case YANG_PATH:
858 return LYEXT_SUBSTMT_PATH;
859 case YANG_PREFIX:
860 return LYEXT_SUBSTMT_PREFIX;
861 case YANG_PRESENCE:
862 return LYEXT_SUBSTMT_PRESENCE;
863 case YANG_REFERENCE:
864 return LYEXT_SUBSTMT_REFERENCE;
865 case YANG_REVISION_DATE:
866 return LYEXT_SUBSTMT_REVISIONDATE;
867 case YANG_UNITS:
868 return LYEXT_SUBSTMT_UNITS;
869 case YANG_VALUE:
870 return LYEXT_SUBSTMT_VALUE;
871 case YANG_YANG_VERSION:
872 return LYEXT_SUBSTMT_VERSION;
873 case YANG_MODIFIER:
874 return LYEXT_SUBSTMT_MODIFIER;
875 case YANG_REQUIRE_INSTANCE:
876 return LYEXT_SUBSTMT_REQINSTANCE;
877 case YANG_YIN_ELEMENT:
878 return LYEXT_SUBSTMT_YINELEM;
879 case YANG_CONFIG:
880 return LYEXT_SUBSTMT_CONFIG;
881 case YANG_MANDATORY:
882 return LYEXT_SUBSTMT_MANDATORY;
883 case YANG_ORDERED_BY:
884 return LYEXT_SUBSTMT_ORDEREDBY;
885 case YANG_STATUS:
886 return LYEXT_SUBSTMT_STATUS;
887 case YANG_FRACTION_DIGITS:
888 return LYEXT_SUBSTMT_FRACDIGITS;
889 case YANG_MAX_ELEMENTS:
890 return LYEXT_SUBSTMT_MAX;
891 case YANG_MIN_ELEMENTS:
892 return LYEXT_SUBSTMT_MIN;
893 case YANG_POSITION:
894 return LYEXT_SUBSTMT_POSITION;
895 case YANG_UNIQUE:
896 return LYEXT_SUBSTMT_UNIQUE;
897 case YANG_IF_FEATURE:
898 return LYEXT_SUBSTMT_IFFEATURE;
899 default:
900 return LYEXT_SUBSTMT_SELF;
901 }
902}
903
David Sedlák9c40a922019-07-08 17:04:43 +0200904/**
905 * @brief Parse belongs-to element.
906 *
907 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200908 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200909 * @param[in,out] data Data to read from, always moved to currently handled character.
910 * @param[out] submod Structure of submodule that is being parsed.
911 * @param[in,out] exts Extension instances to add to.
912 *
913 * @return LY_ERR values
914 */
915static LY_ERR
916yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
917 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
918{
David Sedlák968ac342019-07-11 15:17:59 +0200919 struct yin_subelement subelems[2] = {
920 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
921 {YANG_CUSTOM, NULL, 0}
922 };
David Sedlák1f90d252019-07-10 17:09:32 +0200923 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 +0200924
925 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
926}
927
David Sedlákd6e56892019-07-01 15:40:24 +0200928LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200929yin_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 +0200930 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 +0200931{
932 LY_ERR ret = LY_SUCCESS;
933 struct sized_string prefix, name;
David Sedlák8e7bda82019-07-16 17:57:50 +0200934 char *out = NULL;
935 size_t out_len = 0;
936 int dynamic = 0;
David Sedlák3ffbc522019-07-02 17:49:28 +0200937 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200938 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200939 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200940 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200941 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200942 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200943
David Sedlákda8ffa32019-07-08 14:17:10 +0200944 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
945 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200946 /* current element has subelements as content */
947 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200948 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
949 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200950 LY_CHECK_GOTO(ret, cleanup);
951 if (!name.value) {
952 /* end of current element reached */
953 break;
954 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200955 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200956 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +0200957 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +0200958
959 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200960 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
961 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200962 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 +0200963 ret = LY_EVALID;
964 goto cleanup;
965 }
966
David Sedlák5f8191e2019-07-08 16:35:52 +0200967 /* TODO check relative order */
968
David Sedlák21f87cd2019-07-03 16:53:23 +0200969 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200970 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200971 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 +0200972 return LY_EVALID;
973 }
974 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlák66d7c842019-07-11 15:06:04 +0200975 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec);
976 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +0200977 }
978 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
979
David Sedlákd6e56892019-07-01 15:40:24 +0200980 switch (kw) {
981 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200982 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlák1f90d252019-07-10 17:09:32 +0200983 ret = yin_parse_extension_instance(ctx, subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200984 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200985 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200986 break;
987 case YANG_ACTION:
988 break;
989 case YANG_ANYDATA:
990 break;
991 case YANG_ANYXML:
992 break;
993 case YANG_ARGUMENT:
David Sedlák1f90d252019-07-10 17:09:32 +0200994 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 +0200995 break;
996 case YANG_AUGMENT:
997 break;
998 case YANG_BASE:
David Sedláka62750b2019-07-16 11:21:31 +0200999 if (current_element == YANG_TYPE) {
David Sedlák292763b2019-07-09 11:10:53 +02001000 type = (struct lysp_type *)subelem_info_rec->dest;
1001 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
1002 Y_PREF_IDENTIF_ARG, exts);
1003 type->flags |= LYS_SET_BASE;
David Sedláka62750b2019-07-16 11:21:31 +02001004 } else if (current_element == YANG_IDENTITY) {
David Sedlák292763b2019-07-09 11:10:53 +02001005 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
1006 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
1007 } else {
1008 LOGINT(ctx->xml_ctx.ctx);
1009 ret = LY_EINT;
1010 }
David Sedlákd6e56892019-07-01 15:40:24 +02001011 break;
1012 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +02001013 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 +02001014 break;
1015 case YANG_BIT:
David Sedlák07869a52019-07-12 14:28:19 +02001016 case YANG_ENUM:
1017 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 +02001018 break;
1019 case YANG_CASE:
1020 break;
1021 case YANG_CHOICE:
1022 break;
1023 case YANG_CONFIG:
David Sedláke1a30302019-07-10 13:49:38 +02001024 ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001025 break;
1026 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +02001027 case YANG_DESCRIPTION:
1028 case YANG_ORGANIZATION:
1029 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +02001030 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001031 break;
1032 case YANG_CONTAINER:
1033 break;
1034 case YANG_DEFAULT:
David Sedláke7084ce2019-07-10 16:44:15 +02001035 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1036 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001037 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001038 case YANG_DEVIATE:
1039 break;
1040 case YANG_DEVIATION:
1041 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001042 case YANG_ERROR_APP_TAG:
David Sedlák2ce1be62019-07-10 16:15:09 +02001043 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1044 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001045 break;
1046 case YANG_ERROR_MESSAGE:
David Sedlákc1771b12019-07-10 15:55:46 +02001047 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001048 break;
1049 case YANG_EXTENSION:
David Sedlák1f90d252019-07-10 17:09:32 +02001050 ret = yin_parse_extension(ctx, subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001051 break;
1052 case YANG_FEATURE:
1053 break;
1054 case YANG_FRACTION_DIGITS:
David Sedlákf75d55e2019-07-12 16:52:50 +02001055 ret = yin_parse_fracdigits(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001056 break;
1057 case YANG_GROUPING:
1058 break;
1059 case YANG_IDENTITY:
1060 break;
1061 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +02001062 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák32488102019-07-15 17:44:10 +02001063 (const char ***)subelem_info_rec->dest, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001064 break;
1065 case YANG_IMPORT:
David Sedlák1f90d252019-07-10 17:09:32 +02001066 ret = yin_parse_import(ctx, subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001067 break;
1068 case YANG_INCLUDE:
1069 break;
1070 case YANG_INPUT:
1071 break;
1072 case YANG_KEY:
1073 break;
1074 case YANG_LEAF:
1075 break;
1076 case YANG_LEAF_LIST:
1077 break;
1078 case YANG_LENGTH:
David Sedlák438ae432019-07-11 15:36:54 +02001079 type = (struct lysp_type *)subelem_info_rec->dest;
1080 type->length = calloc(1, sizeof *type->length);
1081 LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
1082 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->length);
1083 type->flags |= LYS_SET_LENGTH;
David Sedlákd6e56892019-07-01 15:40:24 +02001084 break;
1085 case YANG_LIST:
1086 break;
1087 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +02001088 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001089 break;
1090 case YANG_MAX_ELEMENTS:
1091 break;
1092 case YANG_MIN_ELEMENTS:
1093 break;
1094 case YANG_MODIFIER:
David Sedlákce77bf52019-07-11 16:59:31 +02001095 ret = yin_parse_modifier(ctx, subelem_attrs, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001096 break;
1097 case YANG_MODULE:
1098 break;
1099 case YANG_MUST:
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001100 ret = yin_parse_must(ctx, subelem_attrs, data, (struct lysp_restr **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001101 break;
1102 case YANG_NAMESPACE:
David Sedlák2ce1be62019-07-10 16:15:09 +02001103 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1104 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001105 break;
1106 case YANG_NOTIFICATION:
1107 break;
1108 case YANG_ORDERED_BY:
1109 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001110 case YANG_OUTPUT:
1111 break;
1112 case YANG_PATH:
David Sedlák58979872019-07-12 11:42:43 +02001113 type = (struct lysp_type *)subelem_info_rec->dest;
1114 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, &type->path,
1115 YIN_ARG_VALUE, Y_STR_ARG, exts);
1116 type->flags |= LYS_SET_PATH;
David Sedlákd6e56892019-07-01 15:40:24 +02001117 break;
1118 case YANG_PATTERN:
David Sedlákd3983112019-07-12 11:20:56 +02001119 ret = yin_parse_pattern(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001120 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02001121 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02001122 case YANG_POSITION:
David Sedlák5545f5d2019-07-11 11:55:16 +02001123 ret = yin_parse_value_pos_element(ctx, subelem_attrs, data, kw,
1124 (struct lysp_type_enum *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001125 break;
1126 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +02001127 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +02001128 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001129 break;
1130 case YANG_PRESENCE:
1131 break;
1132 case YANG_RANGE:
David Sedlákb7296dd2019-07-11 14:58:38 +02001133 type = (struct lysp_type *)subelem_info_rec->dest;
1134 type->range = calloc(1, sizeof *type->range);
David Sedlák66d7c842019-07-11 15:06:04 +02001135 LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlákb7296dd2019-07-11 14:58:38 +02001136 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->range);
David Sedlák438ae432019-07-11 15:36:54 +02001137 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +02001138 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001139 case YANG_REFINE:
1140 break;
1141 case YANG_REQUIRE_INSTANCE:
David Sedlákcf5569a2019-07-11 13:31:34 +02001142 ret = yin_pasrse_reqinstance(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001143 break;
1144 case YANG_REVISION:
1145 break;
1146 case YANG_REVISION_DATE:
David Sedlák1f90d252019-07-10 17:09:32 +02001147 ret = yin_parse_revision_date(ctx, subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001148 break;
1149 case YANG_RPC:
1150 break;
1151 case YANG_STATUS:
David Sedlák1f90d252019-07-10 17:09:32 +02001152 ret = yin_parse_status(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001153 break;
1154 case YANG_SUBMODULE:
1155 break;
1156 case YANG_TYPE:
1157 break;
1158 case YANG_TYPEDEF:
1159 break;
1160 case YANG_UNIQUE:
David Sedláka5b1d382019-07-10 16:31:09 +02001161 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
1162 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001163 break;
1164 case YANG_UNITS:
David Sedláka5b1d382019-07-10 16:31:09 +02001165 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1166 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001167 break;
1168 case YANG_USES:
1169 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001170 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +02001171 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001172 break;
1173 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +02001174 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001175 break;
1176 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +02001177 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 +02001178 break;
1179 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02001180 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +02001181 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02001182 break;
1183 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02001184 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02001185 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02001186 }
David Sedlák3ffbc522019-07-02 17:49:28 +02001187 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001188 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001189 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +02001190 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02001191 }
1192 } else {
1193 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02001194 /* save text content, if text_content isn't set, it's just ignored */
David Sedláka62750b2019-07-16 11:21:31 +02001195 /* TODO add text validation */
David Sedlák3ffbc522019-07-02 17:49:28 +02001196 if (text_content) {
1197 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001198 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02001199 if (!*text_content) {
1200 free(out);
1201 return LY_EMEM;
1202 }
1203 } else {
1204 if (out_len == 0) {
1205 *text_content = NULL;
1206 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001207 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02001208 }
1209 }
1210 }
David Sedlákd6e56892019-07-01 15:40:24 +02001211 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001212 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 +02001213 }
David Sedlák555c7202019-07-04 12:14:12 +02001214
David Sedlákda8ffa32019-07-08 14:17:10 +02001215 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02001216 }
1217
1218cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +02001219 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001220 return ret;
1221}
1222
David Sedlák619db942019-07-03 14:47:30 +02001223LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001224yin_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 +02001225 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +02001226{
David Sedlák3ffbc522019-07-02 17:49:28 +02001227 const char *temp_rev;
David Sedlák968ac342019-07-11 15:17:59 +02001228 struct yin_subelement subelems[1] = {
1229 {YANG_CUSTOM, NULL, 0}
1230 };
David Sedlák81e04022019-04-05 15:05:46 +02001231
David Sedlák292763b2019-07-09 11:10:53 +02001232 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
David Sedlákda8ffa32019-07-08 14:17:10 +02001233 LY_CHECK_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS, LY_EVALID);
David Sedlákda63c082019-06-04 13:52:23 +02001234
1235 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +02001236 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +02001237
David Sedlákda8ffa32019-07-08 14:17:10 +02001238 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001239}
David Sedlák00250342019-06-21 14:19:39 +02001240
David Sedláke1a30302019-07-10 13:49:38 +02001241/**
1242 * @brief Parse config element.
1243 *
1244 * @param[in] ctx Yin parser context for logging and to store current state.
1245 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1246 * @param[in,out] data Data to read from, always moved to currently handled character.
1247 * @param[in,out] flags Flags to add to.
1248 * @param[in,out] exts Extension instances to add to.
1249 *
1250 * @return LY_ERR values.
1251 */
1252static LY_ERR
1253yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1254 struct lysp_ext_instance **exts)
1255{
1256 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001257 struct yin_subelement subelems[1] = {
1258 {YANG_CUSTOM, NULL, 0}
1259 };
David Sedláke1a30302019-07-10 13:49:38 +02001260
David Sedlák1f90d252019-07-10 17:09:32 +02001261 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 +02001262 if (strcmp(temp_val, "true") == 0) {
1263 *flags |= LYS_CONFIG_W;
1264 } else if (strcmp(temp_val, "false") == 0) {
1265 *flags |= LYS_CONFIG_R;
1266 } else {
1267 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
1268 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1269 return LY_EVALID;
1270 }
1271 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1272
1273 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1274}
1275
David Sedlák3ffbc522019-07-02 17:49:28 +02001276LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +02001277yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1278 struct lysp_ext_instance **exts)
1279{
1280 const char *temp_version = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001281 struct yin_subelement subelems[1] = {
1282 {YANG_CUSTOM, NULL, 0}
1283 };
David Sedlák92147b02019-07-09 14:01:01 +02001284
David Sedlák1f90d252019-07-10 17:09:32 +02001285 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 +02001286 if (strcmp(temp_version, "1.0") == 0) {
1287 *version = LYS_VERSION_1_0;
1288 } else if (strcmp(temp_version, "1.1") == 0) {
1289 *version = LYS_VERSION_1_1;
1290 } else {
1291 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
1292 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1293 return LY_EVALID;
1294 }
1295 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1296 ctx->mod_version = *version;
1297
1298 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1299}
1300
1301LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001302yin_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 +01001303{
David Sedlák736fd0d2019-02-15 16:06:31 +01001304 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +02001305 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +02001306 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02001307
David Sedlák968ac342019-07-11 15:17:59 +02001308 struct yin_subelement subelems[5] = {
1309 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1310 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1311 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1312 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1313 {YANG_CUSTOM, NULL, 0}
1314 };
David Sedlák736fd0d2019-02-15 16:06:31 +01001315
David Sedlák92147b02019-07-09 14:01:01 +02001316 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +02001317 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 +02001318 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +02001319 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +02001320 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 +02001321
David Sedlákda8ffa32019-07-08 14:17:10 +02001322 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +01001323}
1324
David Sedlák11900c82019-06-18 16:29:12 +02001325LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +02001326yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1327 struct lysp_ext_instance **exts)
1328{
1329 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001330 struct yin_subelement subelems[1] = {
1331 {YANG_CUSTOM, NULL, 0}
1332 };
David Sedlák1fdb2522019-07-09 16:22:57 +02001333
David Sedlák1f90d252019-07-10 17:09:32 +02001334 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 +02001335 if (strcmp(temp_val, "true") == 0) {
1336 *flags |= LYS_MAND_TRUE;
1337 } else if (strcmp(temp_val, "false") == 0) {
1338 *flags |= LYS_MAND_FALSE;
1339 } else {
1340 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
1341 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1342 return LY_EVALID;
1343 }
1344 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1345
1346 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1347}
1348
1349LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001350yin_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 +02001351 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +02001352{
David Sedlák3ffbc522019-07-02 17:49:28 +02001353 const char *value = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001354 struct yin_subelement subelems[1] = {
1355 {YANG_CUSTOM, NULL, 0}
1356 };
David Sedlák3ffbc522019-07-02 17:49:28 +02001357
David Sedlák292763b2019-07-09 11:10:53 +02001358 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 +02001359 if (strcmp(value, "current") == 0) {
1360 *flags |= LYS_STATUS_CURR;
1361 } else if (strcmp(value, "deprecated") == 0) {
1362 *flags |= LYS_STATUS_DEPRC;
1363 } else if (strcmp(value, "obsolete") == 0) {
1364 *flags |= LYS_STATUS_OBSLT;
1365 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001366 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1367 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001368 return LY_EVALID;
1369 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001370 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001371
David Sedlákda8ffa32019-07-08 14:17:10 +02001372 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001373}
1374
David Sedlák11900c82019-06-18 16:29:12 +02001375LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001376yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1377{
1378 struct lysp_when *when;
1379 when = calloc(1, sizeof *when);
1380 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1f90d252019-07-10 17:09:32 +02001381 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
David Sedlák32eee7b2019-07-09 12:38:44 +02001382 *when_p = when;
David Sedlák968ac342019-07-11 15:17:59 +02001383 struct yin_subelement subelems[3] = {
1384 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1385 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1386 {YANG_CUSTOM, NULL, 0}
1387 };
David Sedlák32eee7b2019-07-09 12:38:44 +02001388
1389 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1390}
1391
1392LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001393yin_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 +02001394 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001395{
David Sedlák3ffbc522019-07-02 17:49:28 +02001396 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001397 struct yin_subelement subelems[1] = {
1398 {YANG_CUSTOM, NULL, 0}
1399 };
David Sedlák2721d3d2019-06-21 15:37:41 +02001400
David Sedlák1f90d252019-07-10 17:09:32 +02001401 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 +02001402 if (strcmp(temp_val, "true") == 0) {
1403 *flags |= LYS_YINELEM_TRUE;
1404 } else if (strcmp(temp_val, "false") == 0) {
1405 *flags |= LYS_YINELEM_FALSE;
1406 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001407 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1408 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001409 return LY_EVALID;
1410 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001411 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001412
David Sedlákda8ffa32019-07-08 14:17:10 +02001413 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001414}
1415
1416LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001417yin_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 +02001418 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001419{
1420 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001421 char *out;
1422 const char *name, *prefix;
1423 size_t out_len, prefix_len, name_len;
1424 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001425 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001426 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1427 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001428
David Sedlákda8ffa32019-07-08 14:17:10 +02001429 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001430
1431 e->yin = 0;
1432 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001433 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001434 e->insubstmt = subelem;
1435 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001436 e->yin |= LYS_YIN;
1437
David Sedlákb1a78352019-06-28 16:16:29 +02001438 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02001439 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02001440 if (!iter->prefix) {
1441 new_subelem = calloc(1, sizeof(*new_subelem));
1442 if (!e->child) {
1443 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001444 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001445 last_subelem->next = new_subelem;
1446 }
1447 last_subelem = new_subelem;
1448
1449 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001450 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1451 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001452 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001453 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1454 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001455 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001456 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1457 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001458 }
1459 }
1460 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001461
David Sedlákf250ecf2019-07-01 11:02:05 +02001462 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001463 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1464 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001465 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001466 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1467 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001468 if (!name) {
1469 /* end of extension instance reached */
1470 break;
1471 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001472 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 +02001473 if (!e->child) {
1474 e->child = new_subelem;
1475 } else {
1476 last_subelem->next = new_subelem;
1477 }
1478 last_subelem = new_subelem;
1479 }
David Sedlák555c7202019-07-04 12:14:12 +02001480 } else {
1481 /* save text content */
1482 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001483 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001484 if (!e->argument) {
1485 free(out);
1486 return LY_EMEM;
1487 }
1488 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001489 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001490 LY_CHECK_RET(!e->argument, LY_EMEM);
1491 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001492 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001493 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001494 }
David Sedlákb1a78352019-06-28 16:16:29 +02001495 }
1496
1497 return LY_SUCCESS;
1498}
1499
1500LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001501yin_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 +02001502 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001503{
1504 LY_ERR ret = LY_SUCCESS;
1505 const char *temp_prefix, *temp_name;
1506 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001507 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001508 int dynamic;
1509 struct yin_arg_record *subelem_args = NULL;
1510 struct lysp_stmt *last = NULL, *new = NULL;
1511
1512 /* allocate new structure for element */
1513 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001514 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1515 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001516
1517 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001518 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001519 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001520 /* add new element to linked-list */
1521 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001522 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001523 if (!(*element)->child) {
1524 /* save first */
1525 (*element)->child = new;
1526 } else {
1527 last->next = new;
1528 }
1529 last = new;
1530
1531 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001532 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 +02001533 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001534 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001535 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001536 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1537 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001538 /* attributes with prefix are ignored */
1539 if (!temp_prefix) {
1540 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001541 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001542 if (!last->arg) {
1543 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001544 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001545 ret = LY_EMEM;
1546 goto err;
1547 }
1548 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001549 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1550 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001551 }
1552 }
1553 }
1554
1555 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001556 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001557 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001558 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001559 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001560 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 +02001561 LY_CHECK_GOTO(ret, err);
1562 if (!name) {
1563 /* end of element reached */
1564 break;
1565 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001566 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 +02001567 LY_CHECK_GOTO(ret, err);
1568 last = last->next;
1569 }
1570 } else {
1571 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001572 if (out_len != 0) {
1573 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001574 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001575 if (!(*element)->arg) {
1576 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001577 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001578 ret = LY_EMEM;
1579 goto err;
1580 }
1581 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001582 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1583 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001584 }
David Sedlákb1a78352019-06-28 16:16:29 +02001585 }
1586 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001587 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 +02001588 LY_CHECK_GOTO(ret, err);
1589 }
1590
David Sedlákda8ffa32019-07-08 14:17:10 +02001591 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001592 return LY_SUCCESS;
1593
1594err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001595 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001596 return ret;
1597}
1598
1599LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001600yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001601 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001602{
David Sedlák968ac342019-07-11 15:17:59 +02001603 struct yin_subelement subelems[2] = {
1604 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1605 {YANG_CUSTOM, NULL, 0}
1606 };
David Sedlák9494eb22019-06-21 16:06:53 +02001607
David Sedlák292763b2019-07-09 11:10:53 +02001608 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 +02001609
David Sedlákda8ffa32019-07-08 14:17:10 +02001610 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001611}
1612
1613LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001614yin_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 +02001615{
David Sedlák11900c82019-06-18 16:29:12 +02001616 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001617 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001618 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 +02001619
David Sedlák3ffbc522019-07-02 17:49:28 +02001620 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
David Sedlák968ac342019-07-11 15:17:59 +02001621 struct yin_subelement subelems[5] = {
1622 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1623 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1624 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1625 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1626 {YANG_CUSTOM, NULL, 0}
1627 };
David Sedlák11900c82019-06-18 16:29:12 +02001628
David Sedlákda8ffa32019-07-08 14:17:10 +02001629 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001630}
1631
David Sedlák00250342019-06-21 14:19:39 +02001632/**
1633 * @brief Parse module substatements.
1634 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001635 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001636 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001637 * @param[in,out] data Data to read from.
1638 * @param[out] mod Parsed module structure.
1639 *
1640 * @return LY_ERR values.
1641 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001642static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001643yin_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 +02001644{
David Sedlák968ac342019-07-11 15:17:59 +02001645 struct yin_subelement subelems[9] = {
1646 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
1647 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1648 {YANG_EXTENSION, &(*mod)->exts, 0},
1649 {YANG_IMPORT, *mod, 0},
1650 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1651 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
1652 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1653 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
1654 {YANG_CUSTOM, NULL, 0}
1655 };
David Sedlák3b4db242018-10-19 16:11:01 +02001656
David Sedlák292763b2019-07-09 11:10:53 +02001657 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 +01001658
David Sedlákda8ffa32019-07-08 14:17:10 +02001659 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001660}
1661
1662LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001663yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001664{
David Sedláke4889912018-11-02 09:52:40 +01001665 LY_ERR ret = LY_SUCCESS;
1666 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001667 struct lysp_module *mod_p = NULL;
1668 const char *prefix, *name;
1669 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001670
David Sedlák1f90d252019-07-10 17:09:32 +02001671 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001672
David Sedlákda8ffa32019-07-08 14:17:10 +02001673 struct yin_parser_ctx yin_ctx;
1674
1675 /* initialize context */
1676 memset(&yin_ctx, 0, sizeof yin_ctx);
1677 yin_ctx.xml_ctx.ctx = ctx;
1678 yin_ctx.xml_ctx.line = 1;
1679
David Sedláke4889912018-11-02 09:52:40 +01001680
David Sedlák3017da42019-02-15 09:48:04 +01001681 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001682 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001683 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1f90d252019-07-10 17:09:32 +02001684 ret = yin_load_attributes(&yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02001685 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001686 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001687 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001688 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1689 ret = LY_EINVAL;
1690 goto cleanup;
1691 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001692 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 +02001693 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001694 ret = LY_EVALID;
1695 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001696 }
1697
David Sedlák3017da42019-02-15 09:48:04 +01001698 /* allocate module */
1699 mod_p = calloc(1, sizeof *mod_p);
1700 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1701 mod_p->mod = mod;
1702 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001703
David Sedlák00250342019-06-21 14:19:39 +02001704 /* parse module substatements */
David Sedlák1f90d252019-07-10 17:09:32 +02001705 ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001706 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001707
David Sedlák3017da42019-02-15 09:48:04 +01001708 mod_p->parsing = 0;
1709 mod->parsed = mod_p;
1710
1711cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001712 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001713 lysp_module_free(mod_p);
1714 }
David Sedlák1f90d252019-07-10 17:09:32 +02001715 FREE_ARRAY(&yin_ctx, attrs, free_arg_rec);
David Sedlákda8ffa32019-07-08 14:17:10 +02001716 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001717 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001718}