blob: 13927aaf5685712816bc0d7a72572405790875a7 [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
David Sedlákecf5eb82019-06-03 14:12:44 +020014#include "common.h"
15
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
David Sedlák3b4db242018-10-19 16:11:01 +020017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020020#include <string.h>
David Sedlákd6e56892019-07-01 15:40:24 +020021#include <stdbool.h>
David Sedlákf824ad52018-10-14 23:58:15 +020022
David Sedlákf824ad52018-10-14 23:58:15 +020023#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020025#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020026#include "tree.h"
27#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020028#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020030
David Sedlák2b214ac2019-06-06 16:11:03 +020031/**
32 * @brief check if given string is URI of yin namespace.
33 * @param ns Namespace URI to check.
34 *
35 * @return true if ns equals YIN_NS_URI false otherwise.
36 */
37#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
38
David Sedláke1a30302019-07-10 13:49:38 +020039static LY_ERR
40yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
41 struct lysp_ext_instance **exts);
42
David Sedlákf6251182019-06-06 10:22:13 +020043const char *const yin_attr_list[] = {
44 [YIN_ARG_NAME] = "name",
45 [YIN_ARG_TARGET_NODE] = "target-node",
46 [YIN_ARG_MODULE] = "module",
47 [YIN_ARG_VALUE] = "value",
48 [YIN_ARG_TEXT] = "text",
49 [YIN_ARG_CONDITION] = "condition",
50 [YIN_ARG_URI] = "uri",
51 [YIN_ARG_DATE] = "date",
52 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020053};
54
David Sedlák1bccdfa2019-06-17 15:55:27 +020055enum yang_keyword
David Sedlákda8ffa32019-07-08 14:17:10 +020056yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len)
David Sedlák1bccdfa2019-06-17 15:55:27 +020057{
David Sedlák8f7a1172019-06-20 14:42:18 +020058 const char *start = NULL;
59 enum yang_keyword kw = YANG_NONE;
60 const struct lyxml_ns *ns = NULL;
61
62 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020063 return YANG_NONE;
64 }
65
David Sedlákda8ffa32019-07-08 14:17:10 +020066 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020067 if (ns) {
68 if (!IS_YIN_NS(ns->uri)) {
69 return YANG_CUSTOM;
70 }
71 } else {
72 /* elements without namespace are automatically unknown */
73 return YANG_NONE;
74 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020075
David Sedlák8f7a1172019-06-20 14:42:18 +020076
77 start = name;
78 kw = lysp_match_kw(NULL, &name);
79
80 if (name - start == (long int)name_len) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020081 return kw;
82 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020083 if (strncmp(start, "text", name_len) == 0) {
84 return YIN_TEXT;
85 } else if (strncmp(start, "value", name_len) == 0) {
86 return YIN_VALUE;
87 } else {
88 return YANG_NONE;
89 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020090 }
91}
92
David Sedlák872c7b42018-10-26 13:15:20 +020093enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020094yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020095{
David Sedláka7406952019-04-05 10:33:07 +020096 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020097 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +020098 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +020099
David Sedlák94de2aa2019-02-15 12:42:11 +0100100#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
101#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100102#define IF_ARG_PREFIX_END }
103
David Sedlák1c8b2702019-02-22 11:03:02 +0100104 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100105 case 'c':
106 already_read += 1;
107 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200108 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200109
David Sedlák94de2aa2019-02-15 12:42:11 +0100110 case 'd':
111 already_read += 1;
112 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200113 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200114
David Sedlák94de2aa2019-02-15 12:42:11 +0100115 case 'm':
116 already_read += 1;
117 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200118 break;
119
David Sedlák94de2aa2019-02-15 12:42:11 +0100120 case 'n':
121 already_read += 1;
122 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200123 break;
124
David Sedlák94de2aa2019-02-15 12:42:11 +0100125 case 't':
126 already_read += 1;
127 IF_ARG_PREFIX("a", 1)
128 IF_ARG("g", 1, YIN_ARG_TAG)
129 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
130 IF_ARG_PREFIX_END
131 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200132 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200133
David Sedlák94de2aa2019-02-15 12:42:11 +0100134 case 'u':
135 already_read += 1;
136 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200137 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200138
David Sedlák94de2aa2019-02-15 12:42:11 +0100139 case 'v':
140 already_read += 1;
141 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200142 break;
143 }
144
David Sedlákc10e7902018-12-17 02:17:59 +0100145 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200146 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200147 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200148 }
149
David Sedlák18730132019-03-15 15:51:34 +0100150#undef IF_ARG
151#undef IF_ARG_PREFIX
152#undef IF_ARG_PREFIX_END
153
David Sedlák872c7b42018-10-26 13:15:20 +0200154 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200155}
156
David Sedlákb4e44562019-07-04 15:42:12 +0200157/**
158 * @brief free argument record, content loaded from lyxml_get_string() can be
159 * dynamically allocated in some cases so it must be also freed.
160 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200161static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
162 (void)ctx; /* unused */
David Sedlák00250342019-06-21 14:19:39 +0200163 if (record->dynamic_content) {
164 free(record->content);
165 }
166}
167
David Sedlák8f7a1172019-06-20 14:42:18 +0200168LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200169yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200170{
171 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200172 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200173 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200174
David Sedlák555c7202019-07-04 12:14:12 +0200175 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200176 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
177 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200178 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200179
David Sedlákda8ffa32019-07-08 14:17:10 +0200180 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
181 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200182 argument_record->name = name.value;
183 argument_record->name_len = name.len;
184 argument_record->prefix = prefix.value;
185 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200186 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200187 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200188 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200189 }
190 }
191
David Sedlák8f7a1172019-06-20 14:42:18 +0200192cleanup:
193 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200194 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200195 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200196 }
197 return ret;
198}
199
David Sedlák4a650532019-07-10 11:55:18 +0200200LY_ERR
201yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
202{
203 int prefix = 0;
204 unsigned int c;
205 size_t utf8_char_len;
206 size_t already_read = 0;
207 while (already_read < len) {
208 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
209 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
210 already_read += utf8_char_len;
211 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
212
213 switch (val_type) {
214 case Y_IDENTIF_ARG:
215 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
216 break;
217 case Y_PREF_IDENTIF_ARG:
218 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
219 break;
220 case Y_STR_ARG:
221 case Y_MAYBE_STR_ARG:
222 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
223 break;
224 }
225 }
226
227 return LY_SUCCESS;
228}
229
David Sedlákb4e44562019-07-04 15:42:12 +0200230/**
231 * @brief Parse yin argument.
232 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200233 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200234 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200235 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200236 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
237 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200238 * @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 +0200239 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200240 * @param[in] current_element Identification of current element, used for logging.
241 *
242 * @return LY_ERR values.
243 */
244static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200245yin_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 +0200246 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200247{
David Sedlák8f7a1172019-06-20 14:42:18 +0200248 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
249 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200250 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200251
David Sedlák1bccdfa2019-06-17 15:55:27 +0200252 /* validation of attributes */
David Sedlákb4e44562019-07-04 15:42:12 +0200253 LY_ARRAY_FOR(*attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200254 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
255 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200256 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200257 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200258 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200259 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200260 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
261 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200262 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200263 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200264 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200265 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200266 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200267 /* string is no longer supposed to be freed when the sized array is freed */
268 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200269 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200270 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200271 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200272 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200273 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200274 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 +0200275 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200276 }
277 }
278 }
279
David Sedlák292763b2019-07-09 11:10:53 +0200280 /* anything else than Y_MAYBE_STR_ARG is mandatory */
281 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200282 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 +0200283 return LY_EVALID;
284 }
285
286 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200287}
288
David Sedlákd6e56892019-07-01 15:40:24 +0200289/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200290 * @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 +0200291 *
292 * @param[in] type Type of wanted record.
293 * @param[in] array_size Size of array.
294 * @param[in] array Searched array.
295 *
296 * @return Pointer to desired record on success, NULL if element is not in the array.
297 */
David Sedlákb4e44562019-07-04 15:42:12 +0200298static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200299get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200300{
David Sedlákb0faad82019-07-04 14:28:59 +0200301 signed char left = 0, right = array_size - 1, middle;
302
303 while (left <= right) {
304 middle = left + (right - left) / 2;
305
306 if (array[middle].type == type) {
307 return &array[middle];
308 }
309
310 if (array[middle].type < type) {
311 left = middle + 1;
312 } else {
313 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200314 }
315 }
316
317 return NULL;
318}
319
David Sedlákbba38e52019-07-09 15:20:01 +0200320/**
321 * @brief Helper function to check mandatory constraint of subelement.
322 *
323 * @param[in,out] ctx Yin parser context for logging and to store current state.
324 * @param[in] subelem_info Array of information about subelements.
325 * @param[in] subelem_info_size Size of subelem_info array.
326 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
327 *
328 * @return LY_ERR values.
329 */
330static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200331yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200332 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200333{
David Sedlákb0faad82019-07-04 14:28:59 +0200334 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200335 /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */
336 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200337 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 +0200338 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200339 return LY_EVALID;
340 }
341 }
342
343 return LY_SUCCESS;
344}
345
David Sedlákbba38e52019-07-09 15:20:01 +0200346/**
347 * @brief Helper function to check "first" constraint of subelement.
348 *
349 * @param[in,out] ctx Yin parser context for logging and to store current state.
350 * @param[in] subelem_info Array of information about subelements.
351 * @param[in] subelem_info_size Size of subelem_info array.
352 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
353 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
354 *
355 * @return LY_ERR values.
356 */
357static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200358yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200359 signed char subelem_info_size, enum yang_keyword current_element,
360 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200361{
David Sedlákb0faad82019-07-04 14:28:59 +0200362 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200363 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200364 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 +0200365 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200366 return LY_EVALID;
367 }
368 }
369
370 return LY_SUCCESS;
371}
372
David Sedlákb0faad82019-07-04 14:28:59 +0200373/* TODO add something like ifdef NDEBUG around this function, this is supposed to be checked only in debug mode */
David Sedlákbba38e52019-07-09 15:20:01 +0200374/**
375 * @brief Helper function to check if array of information about subelements is in ascending order.
376 *
377 * @param[in] subelem_info Array of information about subelements.
378 * @param[in] subelem_info_size Size of subelem_info array.
379 *
380 * @return True iff subelem_info array is in ascending order, False otherwise.
381 */
382static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200383is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
384{
David Sedlák292763b2019-07-09 11:10:53 +0200385 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200386
387 for (signed char i = 0; i < subelem_info_size; ++i) {
388 if (subelem_info[i].type <= current) {
389 return false;
390 }
391 current = subelem_info[i].type;
392 }
393
394 return true;
395}
396
David Sedlákd6e56892019-07-01 15:40:24 +0200397/**
David Sedlákb4e44562019-07-04 15:42:12 +0200398 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
399 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200400 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200401 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200402 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200403 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200404 * @param[in] kw Type of current element.
405 * @param[out] value Where value of attribute should be stored.
406 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200407 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200408 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200409 *
David Sedlákd6e56892019-07-01 15:40:24 +0200410 * @return LY_ERR values.
411 */
David Sedlákb4e44562019-07-04 15:42:12 +0200412static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200413yin_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 +0200414 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 +0200415{
David Sedlák292763b2019-07-09 11:10:53 +0200416 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlákb4e44562019-07-04 15:42:12 +0200417 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
418
David Sedlákda8ffa32019-07-08 14:17:10 +0200419 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200420}
421
422/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200423 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
424 * more instances, such as base or if-feature.
425 *
426 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200427 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200428 * @param[in,out] data Data to read from, always moved to currently handled character.
429 * @param[in] kw Type of current element.
430 * @param[out] values Parsed values to add to.
431 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200432 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200433 * @param[in,out] exts Extension instance to add to.
434 *
435 * @return LY_ERR values.
436 */
437static LY_ERR
438yin_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 +0200439 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 +0200440{
441 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200442 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200443 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
444 struct yin_subelement subelems[1] = {{YANG_CUSTOM, &index, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200445 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200446
447 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
448}
449
450/**
David Sedlákb4e44562019-07-04 15:42:12 +0200451 * @brief function to parse meta tags (description, contact, ...) eg. elements with
452 * text element as child
453 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200454 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200455 * @param[in,out] data Data to read from.
David Sedlákbba38e52019-07-09 15:20:01 +0200456 * @param[in] Type of element can be se to YANG_ORGANIZATION or YANG_CONTACT or YANG_DESCRIPTION or YANG_REFERENCE.
David Sedlákb4e44562019-07-04 15:42:12 +0200457 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200458 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200459 *
460 * @return LY_ERR values.
461 */
462static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200463yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200464 const char **value, struct lysp_ext_instance **exts)
465{
466 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
467
468 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
469 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
470
David Sedlákda8ffa32019-07-08 14:17:10 +0200471 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200472}
473
474/**
475 * @brief Map keyword type to substatement info.
476 *
477 * @param[in] kw Keyword type.
478 *
479 * @return correct LYEXT_SUBSTMT information.
480 */
481static LYEXT_SUBSTMT
482kw2lyext_substmt(enum yang_keyword kw)
483{
484 switch (kw) {
485 case YANG_ARGUMENT:
486 return LYEXT_SUBSTMT_ARGUMENT;
487 case YANG_BASE:
488 return LYEXT_SUBSTMT_BASE;
489 case YANG_BELONGS_TO:
490 return LYEXT_SUBSTMT_BELONGSTO;
491 case YANG_CONTACT:
492 return LYEXT_SUBSTMT_CONTACT;
493 case YANG_DEFAULT:
494 return LYEXT_SUBSTMT_DEFAULT;
495 case YANG_DESCRIPTION:
496 return LYEXT_SUBSTMT_DESCRIPTION;
497 case YANG_ERROR_APP_TAG:
498 return LYEXT_SUBSTMT_ERRTAG;
499 case YANG_ERROR_MESSAGE:
500 return LYEXT_SUBSTMT_ERRMSG;
501 case YANG_KEY:
502 return LYEXT_SUBSTMT_KEY;
503 case YANG_NAMESPACE:
504 return LYEXT_SUBSTMT_NAMESPACE;
505 case YANG_ORGANIZATION:
506 return LYEXT_SUBSTMT_ORGANIZATION;
507 case YANG_PATH:
508 return LYEXT_SUBSTMT_PATH;
509 case YANG_PREFIX:
510 return LYEXT_SUBSTMT_PREFIX;
511 case YANG_PRESENCE:
512 return LYEXT_SUBSTMT_PRESENCE;
513 case YANG_REFERENCE:
514 return LYEXT_SUBSTMT_REFERENCE;
515 case YANG_REVISION_DATE:
516 return LYEXT_SUBSTMT_REVISIONDATE;
517 case YANG_UNITS:
518 return LYEXT_SUBSTMT_UNITS;
519 case YANG_VALUE:
520 return LYEXT_SUBSTMT_VALUE;
521 case YANG_YANG_VERSION:
522 return LYEXT_SUBSTMT_VERSION;
523 case YANG_MODIFIER:
524 return LYEXT_SUBSTMT_MODIFIER;
525 case YANG_REQUIRE_INSTANCE:
526 return LYEXT_SUBSTMT_REQINSTANCE;
527 case YANG_YIN_ELEMENT:
528 return LYEXT_SUBSTMT_YINELEM;
529 case YANG_CONFIG:
530 return LYEXT_SUBSTMT_CONFIG;
531 case YANG_MANDATORY:
532 return LYEXT_SUBSTMT_MANDATORY;
533 case YANG_ORDERED_BY:
534 return LYEXT_SUBSTMT_ORDEREDBY;
535 case YANG_STATUS:
536 return LYEXT_SUBSTMT_STATUS;
537 case YANG_FRACTION_DIGITS:
538 return LYEXT_SUBSTMT_FRACDIGITS;
539 case YANG_MAX_ELEMENTS:
540 return LYEXT_SUBSTMT_MAX;
541 case YANG_MIN_ELEMENTS:
542 return LYEXT_SUBSTMT_MIN;
543 case YANG_POSITION:
544 return LYEXT_SUBSTMT_POSITION;
545 case YANG_UNIQUE:
546 return LYEXT_SUBSTMT_UNIQUE;
547 case YANG_IF_FEATURE:
548 return LYEXT_SUBSTMT_IFFEATURE;
549 default:
550 return LYEXT_SUBSTMT_SELF;
551 }
552}
553
David Sedlák9c40a922019-07-08 17:04:43 +0200554
555/**
556 * @brief Parse belongs-to element.
557 *
558 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200559 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200560 * @param[in,out] data Data to read from, always moved to currently handled character.
561 * @param[out] submod Structure of submodule that is being parsed.
562 * @param[in,out] exts Extension instances to add to.
563 *
564 * @return LY_ERR values
565 */
566static LY_ERR
567yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
568 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
569{
570 struct yin_subelement subelems[2] = {{YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
571 {YANG_CUSTOM, NULL, 0}};
David Sedlák292763b2019-07-09 11:10:53 +0200572 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 +0200573
574 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
575}
576
David Sedlákd6e56892019-07-01 15:40:24 +0200577LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200578yin_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 +0200579 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 +0200580{
581 LY_ERR ret = LY_SUCCESS;
582 struct sized_string prefix, name;
583 char *out;
584 size_t out_len;
585 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200586 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200587 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200588 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200589 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200590 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200591 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200592
David Sedlákda8ffa32019-07-08 14:17:10 +0200593 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
594 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200595 /* current element has subelements as content */
596 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200597 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
598 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200599 LY_CHECK_GOTO(ret, cleanup);
600 if (!name.value) {
601 /* end of current element reached */
602 break;
603 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200604 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200605 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200606 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200607
608 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200609 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
610 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200611 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 +0200612 ret = LY_EVALID;
613 goto cleanup;
614 }
615
David Sedlák5f8191e2019-07-08 16:35:52 +0200616 /* TODO check relative order */
617
David Sedlák21f87cd2019-07-03 16:53:23 +0200618 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200619 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200620 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 +0200621 return LY_EVALID;
622 }
623 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200624 LY_CHECK_RET(yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec));
David Sedlák21f87cd2019-07-03 16:53:23 +0200625 }
626 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
627
David Sedlákd6e56892019-07-01 15:40:24 +0200628 switch (kw) {
629 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200630 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlákda8ffa32019-07-08 14:17:10 +0200631 ret = yin_parse_extension_instance(ctx, &subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200632 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200633 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200634 break;
635 case YANG_ACTION:
636 break;
637 case YANG_ANYDATA:
638 break;
639 case YANG_ANYXML:
640 break;
641 case YANG_ARGUMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200642 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 +0200643 break;
644 case YANG_AUGMENT:
645 break;
646 case YANG_BASE:
David Sedlák292763b2019-07-09 11:10:53 +0200647 if (current_element == YANG_IDENTITY) {
648 type = (struct lysp_type *)subelem_info_rec->dest;
649 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
650 Y_PREF_IDENTIF_ARG, exts);
651 type->flags |= LYS_SET_BASE;
652 } else if (current_element == YANG_TYPE) {
653 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
654 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
655 } else {
656 LOGINT(ctx->xml_ctx.ctx);
657 ret = LY_EINT;
658 }
David Sedlákd6e56892019-07-01 15:40:24 +0200659 break;
660 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +0200661 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 +0200662 break;
663 case YANG_BIT:
664 break;
665 case YANG_CASE:
666 break;
667 case YANG_CHOICE:
668 break;
669 case YANG_CONFIG:
David Sedláke1a30302019-07-10 13:49:38 +0200670 ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200671 break;
672 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200673 case YANG_DESCRIPTION:
674 case YANG_ORGANIZATION:
675 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200676 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200677 break;
678 case YANG_CONTAINER:
679 break;
680 case YANG_DEFAULT:
681 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200682 case YANG_DEVIATE:
683 break;
684 case YANG_DEVIATION:
685 break;
686 case YANG_ENUM:
687 break;
688 case YANG_ERROR_APP_TAG:
689 break;
690 case YANG_ERROR_MESSAGE:
691 break;
692 case YANG_EXTENSION:
David Sedlákda8ffa32019-07-08 14:17:10 +0200693 ret = yin_parse_extension(ctx, &subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200694 break;
695 case YANG_FEATURE:
696 break;
697 case YANG_FRACTION_DIGITS:
698 break;
699 case YANG_GROUPING:
700 break;
701 case YANG_IDENTITY:
702 break;
703 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200704 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200705 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200706 break;
707 case YANG_IMPORT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200708 ret = yin_parse_import(ctx, &subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200709 break;
710 case YANG_INCLUDE:
711 break;
712 case YANG_INPUT:
713 break;
714 case YANG_KEY:
715 break;
716 case YANG_LEAF:
717 break;
718 case YANG_LEAF_LIST:
719 break;
720 case YANG_LENGTH:
721 break;
722 case YANG_LIST:
723 break;
724 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +0200725 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200726 break;
727 case YANG_MAX_ELEMENTS:
728 break;
729 case YANG_MIN_ELEMENTS:
730 break;
731 case YANG_MODIFIER:
732 break;
733 case YANG_MODULE:
734 break;
735 case YANG_MUST:
736 break;
737 case YANG_NAMESPACE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200738 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200739 (const char **)subelem_info_rec->dest, YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200740 break;
741 case YANG_NOTIFICATION:
742 break;
743 case YANG_ORDERED_BY:
744 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200745 case YANG_OUTPUT:
746 break;
747 case YANG_PATH:
748 break;
749 case YANG_PATTERN:
750 break;
751 case YANG_POSITION:
752 break;
753 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +0200754 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200755 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200756 break;
757 case YANG_PRESENCE:
758 break;
759 case YANG_RANGE:
760 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200761 case YANG_REFINE:
762 break;
763 case YANG_REQUIRE_INSTANCE:
764 break;
765 case YANG_REVISION:
766 break;
767 case YANG_REVISION_DATE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200768 ret = yin_parse_revision_date(ctx, &subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200769 break;
770 case YANG_RPC:
771 break;
772 case YANG_STATUS:
David Sedlákda8ffa32019-07-08 14:17:10 +0200773 ret = yin_parse_status(ctx, &subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200774 break;
775 case YANG_SUBMODULE:
776 break;
777 case YANG_TYPE:
778 break;
779 case YANG_TYPEDEF:
780 break;
781 case YANG_UNIQUE:
David Sedlákd4909d92019-07-09 16:33:10 +0200782 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
783 (const char ***)subelem_info_rec->dest, YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200784 break;
785 case YANG_UNITS:
786 break;
787 case YANG_USES:
788 break;
789 case YANG_VALUE:
790 break;
791 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200792 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200793 break;
794 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +0200795 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200796 break;
797 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200798 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 +0200799 break;
800 case YIN_TEXT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200801 ret = yin_parse_content(ctx, NULL, 0, data, YIN_TEXT, (const char **)subelem_info_rec->dest, NULL);
David Sedlák3ffbc522019-07-02 17:49:28 +0200802 break;
803 case YIN_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +0200804 break;
805 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200806 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200807 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200808 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200809 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200810 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200811 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200812 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200813 }
814 } else {
815 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200816 /* save text content, if text_content isn't set, it's just ignored */
817 if (text_content) {
818 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200819 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200820 if (!*text_content) {
821 free(out);
822 return LY_EMEM;
823 }
824 } else {
825 if (out_len == 0) {
826 *text_content = NULL;
827 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200828 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200829 }
830 }
831 }
David Sedlákd6e56892019-07-01 15:40:24 +0200832 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +0200833 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 +0200834 }
David Sedlák555c7202019-07-04 12:14:12 +0200835
David Sedlákda8ffa32019-07-08 14:17:10 +0200836 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +0200837 }
838
839cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +0200840 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200841 return ret;
842}
843
David Sedlák619db942019-07-03 14:47:30 +0200844LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200845yin_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 +0200846 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +0200847{
David Sedlák3ffbc522019-07-02 17:49:28 +0200848 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +0200849 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +0200850
David Sedlák292763b2019-07-09 11:10:53 +0200851 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 +0200852 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 +0200853
854 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +0200855 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +0200856
David Sedlákda8ffa32019-07-08 14:17:10 +0200857 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +0200858}
David Sedlák00250342019-06-21 14:19:39 +0200859
David Sedláke1a30302019-07-10 13:49:38 +0200860/**
861 * @brief Parse config element.
862 *
863 * @param[in] ctx Yin parser context for logging and to store current state.
864 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
865 * @param[in,out] data Data to read from, always moved to currently handled character.
866 * @param[in,out] flags Flags to add to.
867 * @param[in,out] exts Extension instances to add to.
868 *
869 * @return LY_ERR values.
870 */
871static LY_ERR
872yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
873 struct lysp_ext_instance **exts)
874{
875 const char *temp_val = NULL;
876 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
877
878 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
879 if (strcmp(temp_val, "true") == 0) {
880 *flags |= LYS_CONFIG_W;
881 } else if (strcmp(temp_val, "false") == 0) {
882 *flags |= LYS_CONFIG_R;
883 } else {
884 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
885 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
886 return LY_EVALID;
887 }
888 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
889
890 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
891}
892
David Sedlák3ffbc522019-07-02 17:49:28 +0200893LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +0200894yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
895 struct lysp_ext_instance **exts)
896{
897 const char *temp_version = NULL;
898 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
899
900 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
901 if (strcmp(temp_version, "1.0") == 0) {
902 *version = LYS_VERSION_1_0;
903 } else if (strcmp(temp_version, "1.1") == 0) {
904 *version = LYS_VERSION_1_1;
905 } else {
906 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
907 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
908 return LY_EVALID;
909 }
910 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
911 ctx->mod_version = *version;
912
913 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
914}
915
916LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200917yin_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 +0100918{
David Sedlák736fd0d2019-02-15 16:06:31 +0100919 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +0200920 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +0200921 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +0200922
David Sedlákb0faad82019-07-04 14:28:59 +0200923 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
924 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +0200925 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
926 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +0200927 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +0100928
David Sedlák92147b02019-07-09 14:01:01 +0200929 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +0200930 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 +0200931 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +0200932 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +0200933 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 +0200934
David Sedlákda8ffa32019-07-08 14:17:10 +0200935 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +0100936}
937
David Sedlák11900c82019-06-18 16:29:12 +0200938LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +0200939yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
940 struct lysp_ext_instance **exts)
941{
942 const char *temp_val = NULL;
943 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
944
945 LY_CHECK_RET(yin_parse_attribute(ctx, &attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
946 if (strcmp(temp_val, "true") == 0) {
947 *flags |= LYS_MAND_TRUE;
948 } else if (strcmp(temp_val, "false") == 0) {
949 *flags |= LYS_MAND_FALSE;
950 } else {
951 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
952 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
953 return LY_EVALID;
954 }
955 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
956
957 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
958}
959
960LY_ERR
961yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data, uint16_t *flags,
962 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +0200963{
David Sedlák3ffbc522019-07-02 17:49:28 +0200964 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200965 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +0200966
David Sedlák292763b2019-07-09 11:10:53 +0200967 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 +0200968 if (strcmp(value, "current") == 0) {
969 *flags |= LYS_STATUS_CURR;
970 } else if (strcmp(value, "deprecated") == 0) {
971 *flags |= LYS_STATUS_DEPRC;
972 } else if (strcmp(value, "obsolete") == 0) {
973 *flags |= LYS_STATUS_OBSLT;
974 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200975 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
976 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +0200977 return LY_EVALID;
978 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200979 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +0200980
David Sedlákda8ffa32019-07-08 14:17:10 +0200981 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +0200982}
983
David Sedlák11900c82019-06-18 16:29:12 +0200984LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +0200985yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
986{
987 struct lysp_when *when;
988 when = calloc(1, sizeof *when);
989 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
990 yin_parse_attribute(ctx, &attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
991 *when_p = when;
992 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
993 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
994 {YANG_CUSTOM, NULL, 0}};
995
996 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
997}
998
999LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001000yin_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 +02001001 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001002{
David Sedlák3ffbc522019-07-02 17:49:28 +02001003 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +02001004 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +02001005
David Sedlák292763b2019-07-09 11:10:53 +02001006 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 +02001007 if (strcmp(temp_val, "true") == 0) {
1008 *flags |= LYS_YINELEM_TRUE;
1009 } else if (strcmp(temp_val, "false") == 0) {
1010 *flags |= LYS_YINELEM_FALSE;
1011 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001012 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1013 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001014 return LY_EVALID;
1015 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001016 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001017
David Sedlákda8ffa32019-07-08 14:17:10 +02001018 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001019}
1020
1021LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001022yin_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 +02001023 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001024{
1025 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001026 char *out;
1027 const char *name, *prefix;
1028 size_t out_len, prefix_len, name_len;
1029 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001030 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001031 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1032 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001033
David Sedlákda8ffa32019-07-08 14:17:10 +02001034 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001035
1036 e->yin = 0;
1037 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001038 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001039 e->insubstmt = subelem;
1040 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001041 e->yin |= LYS_YIN;
1042
David Sedlákb1a78352019-06-28 16:16:29 +02001043 /* store attributes as subelements */
1044 LY_ARRAY_FOR_ITER(*attrs, struct yin_arg_record, iter) {
1045 if (!iter->prefix) {
1046 new_subelem = calloc(1, sizeof(*new_subelem));
1047 if (!e->child) {
1048 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001049 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001050 last_subelem->next = new_subelem;
1051 }
1052 last_subelem = new_subelem;
1053
1054 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001055 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1056 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001057 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001058 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1059 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001060 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001061 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1062 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001063 }
1064 }
1065 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001066
David Sedlákf250ecf2019-07-01 11:02:05 +02001067 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001068 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1069 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001070 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001071 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1072 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001073 if (!name) {
1074 /* end of extension instance reached */
1075 break;
1076 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001077 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 +02001078 if (!e->child) {
1079 e->child = new_subelem;
1080 } else {
1081 last_subelem->next = new_subelem;
1082 }
1083 last_subelem = new_subelem;
1084 }
David Sedlák555c7202019-07-04 12:14:12 +02001085 } else {
1086 /* save text content */
1087 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001088 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001089 if (!e->argument) {
1090 free(out);
1091 return LY_EMEM;
1092 }
1093 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001094 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001095 LY_CHECK_RET(!e->argument, LY_EMEM);
1096 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001097 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001098 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001099 }
David Sedlákb1a78352019-06-28 16:16:29 +02001100 }
1101
1102 return LY_SUCCESS;
1103}
1104
1105LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001106yin_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 +02001107 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001108{
1109 LY_ERR ret = LY_SUCCESS;
1110 const char *temp_prefix, *temp_name;
1111 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001112 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001113 int dynamic;
1114 struct yin_arg_record *subelem_args = NULL;
1115 struct lysp_stmt *last = NULL, *new = NULL;
1116
1117 /* allocate new structure for element */
1118 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001119 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1120 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001121
1122 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001123 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001124 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001125 /* add new element to linked-list */
1126 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001127 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001128 if (!(*element)->child) {
1129 /* save first */
1130 (*element)->child = new;
1131 } else {
1132 last->next = new;
1133 }
1134 last = new;
1135
1136 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001137 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 +02001138 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001139 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001140 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001141 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1142 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001143 /* attributes with prefix are ignored */
1144 if (!temp_prefix) {
1145 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001146 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001147 if (!last->arg) {
1148 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001149 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001150 ret = LY_EMEM;
1151 goto err;
1152 }
1153 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001154 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1155 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001156 }
1157 }
1158 }
1159
1160 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001161 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001162 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001163 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001164 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001165 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 +02001166 LY_CHECK_GOTO(ret, err);
1167 if (!name) {
1168 /* end of element reached */
1169 break;
1170 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001171 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 +02001172 LY_CHECK_GOTO(ret, err);
1173 last = last->next;
1174 }
1175 } else {
1176 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001177 if (out_len != 0) {
1178 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001179 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001180 if (!(*element)->arg) {
1181 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001182 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001183 ret = LY_EMEM;
1184 goto err;
1185 }
1186 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001187 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1188 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001189 }
David Sedlákb1a78352019-06-28 16:16:29 +02001190 }
1191 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001192 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 +02001193 LY_CHECK_GOTO(ret, err);
1194 }
1195
David Sedlákda8ffa32019-07-08 14:17:10 +02001196 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001197 return LY_SUCCESS;
1198
1199err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001200 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001201 return ret;
1202}
1203
1204LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001205yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record **attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001206 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001207{
David Sedlák3ffbc522019-07-02 17:49:28 +02001208 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001209 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001210
David Sedlák292763b2019-07-09 11:10:53 +02001211 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 +02001212
David Sedlákda8ffa32019-07-08 14:17:10 +02001213 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001214}
1215
1216LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001217yin_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 +02001218{
David Sedlák11900c82019-06-18 16:29:12 +02001219 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001220 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001221 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 +02001222
David Sedlák3ffbc522019-07-02 17:49:28 +02001223 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1224 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1225 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1226 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1227 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001228 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001229
David Sedlákda8ffa32019-07-08 14:17:10 +02001230 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001231}
1232
David Sedlák00250342019-06-21 14:19:39 +02001233/**
1234 * @brief Parse module substatements.
1235 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001236 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001237 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001238 * @param[in,out] data Data to read from.
1239 * @param[out] mod Parsed module structure.
1240 *
1241 * @return LY_ERR values.
1242 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001243static LY_ERR
1244yin_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 +02001245{
David Sedlákda8ffa32019-07-08 14:17:10 +02001246 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001247 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1248 {YANG_EXTENSION, &(*mod)->exts, 0},
1249 {YANG_IMPORT, *mod, 0},
1250 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1251 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001252 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1253 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001254 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001255
David Sedlák292763b2019-07-09 11:10:53 +02001256 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 +01001257
David Sedlákda8ffa32019-07-08 14:17:10 +02001258 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001259}
1260
1261LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001262yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001263{
David Sedláke4889912018-11-02 09:52:40 +01001264 LY_ERR ret = LY_SUCCESS;
1265 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001266 struct lysp_module *mod_p = NULL;
1267 const char *prefix, *name;
1268 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001269
David Sedlák8f7a1172019-06-20 14:42:18 +02001270 struct yin_arg_record *args = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001271
David Sedlákda8ffa32019-07-08 14:17:10 +02001272 struct yin_parser_ctx yin_ctx;
1273
1274 /* initialize context */
1275 memset(&yin_ctx, 0, sizeof yin_ctx);
1276 yin_ctx.xml_ctx.ctx = ctx;
1277 yin_ctx.xml_ctx.line = 1;
1278
David Sedláke4889912018-11-02 09:52:40 +01001279
David Sedlák3017da42019-02-15 09:48:04 +01001280 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001281 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001282 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001283 ret = yin_load_attributes(&yin_ctx, &data, &args);
David Sedlák00250342019-06-21 14:19:39 +02001284 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001285 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len);
David Sedláke4889912018-11-02 09:52:40 +01001286 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001287 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1288 ret = LY_EINVAL;
1289 goto cleanup;
1290 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001291 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 +02001292 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001293 ret = LY_EVALID;
1294 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001295 }
1296
David Sedlák3017da42019-02-15 09:48:04 +01001297 /* allocate module */
1298 mod_p = calloc(1, sizeof *mod_p);
1299 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1300 mod_p->mod = mod;
1301 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001302
David Sedlák00250342019-06-21 14:19:39 +02001303 /* parse module substatements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001304 ret = yin_parse_mod(&yin_ctx, &args, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001305 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001306
David Sedlák3017da42019-02-15 09:48:04 +01001307 mod_p->parsing = 0;
1308 mod->parsed = mod_p;
1309
1310cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001311 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001312 lysp_module_free(mod_p);
1313 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001314 FREE_ARRAY(&yin_ctx, args, free_arg_rec);
1315 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001316 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001317}