blob: 84f5b1b2640e79396629efcd3e297721544f6404 [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ákf824ad52018-10-14 23:58:15 +020023
David Sedlákf824ad52018-10-14 23:58:15 +020024#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020025#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020026#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020027#include "tree.h"
28#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020029#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020030#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020031
David Sedlák2b214ac2019-06-06 16:11:03 +020032/**
33 * @brief check if given string is URI of yin namespace.
34 * @param ns Namespace URI to check.
35 *
36 * @return true if ns equals YIN_NS_URI false otherwise.
37 */
38#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
39
David Sedláke1a30302019-07-10 13:49:38 +020040static LY_ERR
41yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
42 struct lysp_ext_instance **exts);
43
David Sedlákf6251182019-06-06 10:22:13 +020044const char *const yin_attr_list[] = {
45 [YIN_ARG_NAME] = "name",
46 [YIN_ARG_TARGET_NODE] = "target-node",
47 [YIN_ARG_MODULE] = "module",
48 [YIN_ARG_VALUE] = "value",
49 [YIN_ARG_TEXT] = "text",
50 [YIN_ARG_CONDITION] = "condition",
51 [YIN_ARG_URI] = "uri",
52 [YIN_ARG_DATE] = "date",
53 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020054};
55
David Sedlák1bccdfa2019-06-17 15:55:27 +020056enum yang_keyword
David Sedlákc1771b12019-07-10 15:55:46 +020057yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
58 const char *prefix, size_t prefix_len, enum yang_keyword parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020059{
David Sedlák8f7a1172019-06-20 14:42:18 +020060 const char *start = NULL;
61 enum yang_keyword kw = YANG_NONE;
62 const struct lyxml_ns *ns = NULL;
63
64 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020065 return YANG_NONE;
66 }
67
David Sedlákda8ffa32019-07-08 14:17:10 +020068 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020069 if (ns) {
70 if (!IS_YIN_NS(ns->uri)) {
71 return YANG_CUSTOM;
72 }
73 } else {
74 /* elements without namespace are automatically unknown */
75 return YANG_NONE;
76 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020077
David Sedlák8f7a1172019-06-20 14:42:18 +020078 start = name;
79 kw = lysp_match_kw(NULL, &name);
80
81 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020082 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
83 if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) {
84 return YIN_VALUE;
85 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020086 return kw;
87 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020088 if (strncmp(start, "text", name_len) == 0) {
89 return YIN_TEXT;
90 } else if (strncmp(start, "value", name_len) == 0) {
91 return YIN_VALUE;
92 } else {
93 return YANG_NONE;
94 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020095 }
96}
97
David Sedlák872c7b42018-10-26 13:15:20 +020098enum YIN_ARGUMENT
David Sedlák060b00e2019-06-19 11:12:06 +020099yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +0200100{
David Sedláka7406952019-04-05 10:33:07 +0200101 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200102 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200103 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200104
David Sedlák94de2aa2019-02-15 12:42:11 +0100105#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
106#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100107#define IF_ARG_PREFIX_END }
108
David Sedlák1c8b2702019-02-22 11:03:02 +0100109 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100110 case 'c':
111 already_read += 1;
112 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
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 'd':
116 already_read += 1;
117 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200118 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200119
David Sedlák94de2aa2019-02-15 12:42:11 +0100120 case 'm':
121 already_read += 1;
122 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200123 break;
124
David Sedlák94de2aa2019-02-15 12:42:11 +0100125 case 'n':
126 already_read += 1;
127 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200128 break;
129
David Sedlák94de2aa2019-02-15 12:42:11 +0100130 case 't':
131 already_read += 1;
132 IF_ARG_PREFIX("a", 1)
133 IF_ARG("g", 1, YIN_ARG_TAG)
134 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
135 IF_ARG_PREFIX_END
136 else IF_ARG("ext", 3, YIN_ARG_TEXT)
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 'u':
140 already_read += 1;
141 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200142 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200143
David Sedlák94de2aa2019-02-15 12:42:11 +0100144 case 'v':
145 already_read += 1;
146 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200147 break;
148 }
149
David Sedlákc10e7902018-12-17 02:17:59 +0100150 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200151 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200152 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200153 }
154
David Sedlák18730132019-03-15 15:51:34 +0100155#undef IF_ARG
156#undef IF_ARG_PREFIX
157#undef IF_ARG_PREFIX_END
158
David Sedlák872c7b42018-10-26 13:15:20 +0200159 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200160}
161
David Sedlákb4e44562019-07-04 15:42:12 +0200162/**
163 * @brief free argument record, content loaded from lyxml_get_string() can be
164 * dynamically allocated in some cases so it must be also freed.
165 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200166static void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
167 (void)ctx; /* unused */
David Sedlák00250342019-06-21 14:19:39 +0200168 if (record->dynamic_content) {
169 free(record->content);
170 }
171}
172
David Sedlák8f7a1172019-06-20 14:42:18 +0200173LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200174yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200175{
176 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200177 struct yin_arg_record *argument_record = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200178 struct sized_string prefix, name;
David Sedláka7406952019-04-05 10:33:07 +0200179
David Sedlák555c7202019-07-04 12:14:12 +0200180 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200181 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
182 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlák00250342019-06-21 14:19:39 +0200183 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200184
David Sedlákda8ffa32019-07-08 14:17:10 +0200185 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
186 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlák555c7202019-07-04 12:14:12 +0200187 argument_record->name = name.value;
188 argument_record->name_len = name.len;
189 argument_record->prefix = prefix.value;
190 argument_record->prefix_len = prefix.len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200191 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200192 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák00250342019-06-21 14:19:39 +0200193 LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200194 }
195 }
196
David Sedlák8f7a1172019-06-20 14:42:18 +0200197cleanup:
198 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200199 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200200 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200201 }
202 return ret;
203}
204
David Sedlák4a650532019-07-10 11:55:18 +0200205LY_ERR
206yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
207{
208 int prefix = 0;
209 unsigned int c;
210 size_t utf8_char_len;
211 size_t already_read = 0;
212 while (already_read < len) {
213 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
214 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
215 already_read += utf8_char_len;
216 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
217
218 switch (val_type) {
219 case Y_IDENTIF_ARG:
220 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
221 break;
222 case Y_PREF_IDENTIF_ARG:
223 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
224 break;
225 case Y_STR_ARG:
226 case Y_MAYBE_STR_ARG:
227 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
228 break;
229 }
230 }
231
232 return LY_SUCCESS;
233}
234
David Sedlákb4e44562019-07-04 15:42:12 +0200235/**
236 * @brief Parse yin argument.
237 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200238 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200239 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200240 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200241 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
242 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200243 * @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 +0200244 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200245 * @param[in] current_element Identification of current element, used for logging.
246 *
247 * @return LY_ERR values.
248 */
249static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +0200250yin_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 +0200251 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200252{
David Sedlák8f7a1172019-06-20 14:42:18 +0200253 enum YIN_ARGUMENT arg = YIN_ARG_UNKNOWN;
254 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200255 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200256
David Sedlák1bccdfa2019-06-17 15:55:27 +0200257 /* validation of attributes */
David Sedlák1f90d252019-07-10 17:09:32 +0200258 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200259 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
260 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200261 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200262 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200263 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200264 } else if (arg == arg_type) {
David Sedlák292763b2019-07-09 11:10:53 +0200265 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Duplicit definition of %s attribute in %s element",
266 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200267 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200268 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200269 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200270 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200271 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200272 /* string is no longer supposed to be freed when the sized array is freed */
273 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200274 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200275 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák619db942019-07-03 14:47:30 +0200276 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200277 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200278 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200279 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 +0200280 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200281 }
282 }
283 }
284
David Sedlák292763b2019-07-09 11:10:53 +0200285 /* anything else than Y_MAYBE_STR_ARG is mandatory */
286 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200287 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 +0200288 return LY_EVALID;
289 }
290
291 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200292}
293
David Sedlákd6e56892019-07-01 15:40:24 +0200294/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200295 * @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 +0200296 *
297 * @param[in] type Type of wanted record.
298 * @param[in] array_size Size of array.
299 * @param[in] array Searched array.
300 *
301 * @return Pointer to desired record on success, NULL if element is not in the array.
302 */
David Sedlákb4e44562019-07-04 15:42:12 +0200303static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200304get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200305{
David Sedlákb0faad82019-07-04 14:28:59 +0200306 signed char left = 0, right = array_size - 1, middle;
307
308 while (left <= right) {
309 middle = left + (right - left) / 2;
310
311 if (array[middle].type == type) {
312 return &array[middle];
313 }
314
315 if (array[middle].type < type) {
316 left = middle + 1;
317 } else {
318 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200319 }
320 }
321
322 return NULL;
323}
324
David Sedlákbba38e52019-07-09 15:20:01 +0200325/**
326 * @brief Helper function to check mandatory constraint of subelement.
327 *
328 * @param[in,out] ctx Yin parser context for logging and to store current state.
329 * @param[in] subelem_info Array of information about subelements.
330 * @param[in] subelem_info_size Size of subelem_info array.
331 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
332 *
333 * @return LY_ERR values.
334 */
335static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200336yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200337 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200338{
David Sedlákb0faad82019-07-04 14:28:59 +0200339 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200340 /* 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 +0200341 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200342 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 +0200343 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200344 return LY_EVALID;
345 }
346 }
347
348 return LY_SUCCESS;
349}
350
David Sedlákbba38e52019-07-09 15:20:01 +0200351/**
352 * @brief Helper function to check "first" constraint of subelement.
353 *
354 * @param[in,out] ctx Yin parser context for logging and to store current state.
355 * @param[in] subelem_info Array of information about subelements.
356 * @param[in] subelem_info_size Size of subelem_info array.
357 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
358 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
359 *
360 * @return LY_ERR values.
361 */
362static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200363yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200364 signed char subelem_info_size, enum yang_keyword current_element,
365 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200366{
David Sedlákb0faad82019-07-04 14:28:59 +0200367 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200368 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200369 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 +0200370 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200371 return LY_EVALID;
372 }
373 }
374
375 return LY_SUCCESS;
376}
377
David Sedlákbba38e52019-07-09 15:20:01 +0200378/**
379 * @brief Helper function to check if array of information about subelements is in ascending order.
380 *
381 * @param[in] subelem_info Array of information about subelements.
382 * @param[in] subelem_info_size Size of subelem_info array.
383 *
384 * @return True iff subelem_info array is in ascending order, False otherwise.
385 */
David Sedlák5545f5d2019-07-11 11:55:16 +0200386#ifndef NDEBUG
David Sedlákbba38e52019-07-09 15:20:01 +0200387static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200388is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
389{
David Sedlák292763b2019-07-09 11:10:53 +0200390 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200391
392 for (signed char i = 0; i < subelem_info_size; ++i) {
393 if (subelem_info[i].type <= current) {
394 return false;
395 }
396 current = subelem_info[i].type;
397 }
398
399 return true;
400}
David Sedlák5545f5d2019-07-11 11:55:16 +0200401#endif
David Sedlákb0faad82019-07-04 14:28:59 +0200402
David Sedlákd6e56892019-07-01 15:40:24 +0200403/**
David Sedlákb4e44562019-07-04 15:42:12 +0200404 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
405 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200406 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200407 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200408 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200409 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200410 * @param[in] kw Type of current element.
411 * @param[out] value Where value of attribute should be stored.
412 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200413 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200414 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200415 *
David Sedlákd6e56892019-07-01 15:40:24 +0200416 * @return LY_ERR values.
417 */
David Sedlákb4e44562019-07-04 15:42:12 +0200418static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200419yin_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 +0200420 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 +0200421{
David Sedlák1f90d252019-07-10 17:09:32 +0200422 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200423 struct yin_subelement subelems[1] = {
424 {YANG_CUSTOM, NULL, 0}
425 };
David Sedlákb4e44562019-07-04 15:42:12 +0200426
David Sedlákda8ffa32019-07-08 14:17:10 +0200427 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200428}
429
430/**
David Sedlákd3983112019-07-12 11:20:56 +0200431 * @brief Parse pattern element.
432 *
433 * @param[in,out] ctx Yin parser context for logging and to store current state.
434 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
435 * @param[in,out] data Data to read from, always moved to currently handled character.
436 * @param[in,out] patterns Restrictions to add to.
437 *
438 * @return LY_ERR values.
439 */
440static LY_ERR
441yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
442 struct lysp_type *type)
443{
444 const char *real_value = NULL;
445 char *saved_value = NULL;
446 struct lysp_restr *restr;
447
448 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
449 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
450 size_t len = strlen(real_value);
451
452 saved_value = malloc(len + 2);
453 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
454 memmove(saved_value + 1, real_value, len);
455 FREE_STRING(ctx->xml_ctx.ctx, real_value);
456 saved_value[0] = 0x06;
457 saved_value[len + 1] = '\0';
458 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
459 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
460 type->flags |= LYS_SET_PATTERN;
461
462 struct yin_subelement subelems[6] = {
463 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
464 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
465 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
466 {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
467 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
468 {YANG_CUSTOM, NULL, 0}
469 };
470 return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
471}
472
473/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200474 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
475 * more instances, such as base or if-feature.
476 *
477 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200478 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200479 * @param[in,out] data Data to read from, always moved to currently handled character.
480 * @param[in] kw Type of current element.
481 * @param[out] values Parsed values to add to.
482 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200483 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200484 * @param[in,out] exts Extension instance to add to.
485 *
486 * @return LY_ERR values.
487 */
488static LY_ERR
489yin_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 +0200490 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 +0200491{
492 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200493 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200494 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200495 struct yin_subelement subelems[1] = {
496 {YANG_CUSTOM, &index, 0}
497 };
498
David Sedlák1f90d252019-07-10 17:09:32 +0200499 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200500
501 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
502}
503
504/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200505 * @brief Parse require instance element.
506 *
507 * @param[in,out] ctx Yin parser context for logging and to store current state.
508 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
509 * @param[in,out] data Data to read from, always moved to currently handled character.
510 * @prama[out] type Type structure to store value, flag and extensions.
511 *
512 * @return LY_ERR values.
513 */
514static LY_ERR
515yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
516 const char **data, struct lysp_type *type)
517{
518 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200519 struct yin_subelement subelems[1] = {
520 {YANG_CUSTOM, NULL, 0}
521 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200522
523 type->flags |= LYS_SET_REQINST;
524 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
525 if (strcmp(temp_val, "true") == 0) {
526 type->require_instance = 1;
527 } else if (strcmp(temp_val, "false") != 0) {
528 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "require-instance");
529 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
530 return LY_EVALID;
531 }
532 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
533
534 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
535}
536
537/**
David Sedlákce77bf52019-07-11 16:59:31 +0200538 * @brief Parse modifier element.
539 *
540 * @param[in,out] ctx Yin parser context for logging and to store current state.
541 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
542 * @param[in,out] data Data to read from, always moved to currently handled character.
543 * @param[in,out] pat Value to write to.
544 * @param[in,out] exts Extension instances to add to.
545 *
546 * @return LY_ERR values.
547 */
548static LY_ERR
549yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
550 const char **pat, struct lysp_ext_instance **exts)
551{
David Sedlákd3983112019-07-12 11:20:56 +0200552 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200553 const char *temp_val;
554 char *modified_val;
555 struct yin_subelement subelems[1] = {
556 {YANG_CUSTOM, NULL, 0}
557 };
558
559 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER));
560 if (strcmp(temp_val, "invert-match") != 0) {
561 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "modifier");
562 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
563 return LY_EVALID;
564 }
David Sedlákd3983112019-07-12 11:20:56 +0200565 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200566
567 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200568 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200569 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200570 strcpy(modified_val, *pat);
571 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200572
573 /* modify the new value */
574 modified_val[0] = 0x15;
575 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
576
577 return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts);
578}
579
580/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200581 * @brief Parse a restriction element (length, range or one instance of must).
582 *
583 * @param[in,out] ctx Yin parser context for logging and to store current state.
584 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
585 * @param[in,out] data Data to read from, always moved to currently handled character.
586 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
587 * @param[in]
588 */
589static LY_ERR
590yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
591 enum yang_keyword restr_kw, struct lysp_restr *restr)
592{
593 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
594 struct yin_subelement subelems[5] = {
David Sedlák968ac342019-07-11 15:17:59 +0200595 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
596 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
597 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
598 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
599 {YANG_CUSTOM, NULL, 0}
600 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200601 /* argument of must is called condition, but argument of length and range is called value */
602 enum YIN_ARGUMENT arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
603 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
604
605 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
606}
607
608/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200609 * @brief Parse must element.
610 *
611 * @param[in,out] ctx YIN parser context for logging and to store current state.
612 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
613 * @param[in,out] data Data to read from, always moved to currently handled character.
614 * @param[in,out] restrs Restrictions to add to.
615 *
616 * @return LY_ERR values.
617 */
618static LY_ERR
619yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
620{
621 struct lysp_restr *restr;
622
623 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
624 return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr);
625}
626
627/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200628 * @brief Parse position or value element.
629 *
630 * @param[in,out] ctx YIN parser context for logging and to store current state.
631 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
632 * @param[in,out] data Data to read from, always moved to currently handled character.
633 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
634 * @param[out] enm Enum structure to save value, flags and extensions.
635 *
636 * @return LY_ERR values.
637 */
638static LY_ERR
639yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
640 enum yang_keyword kw, struct lysp_type_enum *enm)
641{
642 assert(kw == YANG_POSITION || kw == YANG_VALUE);
643 const char *temp_val = NULL;
644 char *ptr;
645 long int num;
646 unsigned long int unum;
647
648 /* set value flag */
649 enm->flags |= LYS_SET_VALUE;
650
651 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200652 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlák5545f5d2019-07-11 11:55:16 +0200653 if (!temp_val || (temp_val[0] == '+') || ((temp_val[0] == '0') && (temp_val[0] != '\0')) || ((kw == YANG_VALUE) && !strcmp(temp_val, "-0"))) {
654 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
655 goto error;
656 }
657
658 /* convert value */
659 errno = 0;
660 if (kw == YANG_VALUE) {
661 num = strtol(temp_val, &ptr, 10);
662 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
663 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
664 goto error;
665 }
666 } else {
667 unum = strtoul(temp_val, &ptr, 10);
668 if (unum > UINT64_C(4294967295)) {
669 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
670 goto error;
671 }
672 }
673 /* check if whole argument value was converted */
674 if (*ptr != '\0') {
675 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
676 }
677 if (errno == ERANGE) {
678 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw));
679 goto error;
680 }
681 /* save correctly ternary operator can't be used because num and unum have different signes */
682 if (kw == YANG_VALUE) {
683 enm->value = num;
684 } else {
685 enm->value = unum;
686 }
687 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
688
689 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +0200690 struct yin_subelement subelems[1] = {
691 {YANG_CUSTOM, NULL, 0}
692 };
David Sedlák5545f5d2019-07-11 11:55:16 +0200693 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
694
695 error:
696 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
697 return LY_EVALID;
698}
699
700/**
David Sedlákc1771b12019-07-10 15:55:46 +0200701 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200702 * text element as child
703 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200704 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200705 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200706 * @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 +0200707 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200708 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200709 *
710 * @return LY_ERR values.
711 */
712static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200713yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200714 const char **value, struct lysp_ext_instance **exts)
715{
716 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
717
David Sedlák968ac342019-07-11 15:17:59 +0200718 struct yin_subelement subelems[2] = {
719 {YANG_CUSTOM, NULL, 0},
720 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
721 };
David Sedlákb4e44562019-07-04 15:42:12 +0200722
David Sedlákda8ffa32019-07-08 14:17:10 +0200723 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200724}
725
726/**
David Sedlákc1771b12019-07-10 15:55:46 +0200727 * @brief Parse error-message element.
728 *
729 * @param[in,out] ctx Yin parser context for logging and to store current state.
730 * @param[in,out] data Data to read from.
731 * @param[out] value Where the content of error-message element should be stored.
732 * @param[in,out] exts Extension instance to add to.
733 *
734 * @return LY_ERR values.
735 */
736static LY_ERR
737yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
738 struct lysp_ext_instance **exts)
739{
David Sedlák968ac342019-07-11 15:17:59 +0200740 struct yin_subelement subelems[2] = {
741 {YANG_CUSTOM, NULL, 0},
742 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
743 };
David Sedlákc1771b12019-07-10 15:55:46 +0200744
745 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
746}
747
748/**
David Sedlákb4e44562019-07-04 15:42:12 +0200749 * @brief Map keyword type to substatement info.
750 *
751 * @param[in] kw Keyword type.
752 *
753 * @return correct LYEXT_SUBSTMT information.
754 */
755static LYEXT_SUBSTMT
756kw2lyext_substmt(enum yang_keyword kw)
757{
758 switch (kw) {
759 case YANG_ARGUMENT:
760 return LYEXT_SUBSTMT_ARGUMENT;
761 case YANG_BASE:
762 return LYEXT_SUBSTMT_BASE;
763 case YANG_BELONGS_TO:
764 return LYEXT_SUBSTMT_BELONGSTO;
765 case YANG_CONTACT:
766 return LYEXT_SUBSTMT_CONTACT;
767 case YANG_DEFAULT:
768 return LYEXT_SUBSTMT_DEFAULT;
769 case YANG_DESCRIPTION:
770 return LYEXT_SUBSTMT_DESCRIPTION;
771 case YANG_ERROR_APP_TAG:
772 return LYEXT_SUBSTMT_ERRTAG;
773 case YANG_ERROR_MESSAGE:
774 return LYEXT_SUBSTMT_ERRMSG;
775 case YANG_KEY:
776 return LYEXT_SUBSTMT_KEY;
777 case YANG_NAMESPACE:
778 return LYEXT_SUBSTMT_NAMESPACE;
779 case YANG_ORGANIZATION:
780 return LYEXT_SUBSTMT_ORGANIZATION;
781 case YANG_PATH:
782 return LYEXT_SUBSTMT_PATH;
783 case YANG_PREFIX:
784 return LYEXT_SUBSTMT_PREFIX;
785 case YANG_PRESENCE:
786 return LYEXT_SUBSTMT_PRESENCE;
787 case YANG_REFERENCE:
788 return LYEXT_SUBSTMT_REFERENCE;
789 case YANG_REVISION_DATE:
790 return LYEXT_SUBSTMT_REVISIONDATE;
791 case YANG_UNITS:
792 return LYEXT_SUBSTMT_UNITS;
793 case YANG_VALUE:
794 return LYEXT_SUBSTMT_VALUE;
795 case YANG_YANG_VERSION:
796 return LYEXT_SUBSTMT_VERSION;
797 case YANG_MODIFIER:
798 return LYEXT_SUBSTMT_MODIFIER;
799 case YANG_REQUIRE_INSTANCE:
800 return LYEXT_SUBSTMT_REQINSTANCE;
801 case YANG_YIN_ELEMENT:
802 return LYEXT_SUBSTMT_YINELEM;
803 case YANG_CONFIG:
804 return LYEXT_SUBSTMT_CONFIG;
805 case YANG_MANDATORY:
806 return LYEXT_SUBSTMT_MANDATORY;
807 case YANG_ORDERED_BY:
808 return LYEXT_SUBSTMT_ORDEREDBY;
809 case YANG_STATUS:
810 return LYEXT_SUBSTMT_STATUS;
811 case YANG_FRACTION_DIGITS:
812 return LYEXT_SUBSTMT_FRACDIGITS;
813 case YANG_MAX_ELEMENTS:
814 return LYEXT_SUBSTMT_MAX;
815 case YANG_MIN_ELEMENTS:
816 return LYEXT_SUBSTMT_MIN;
817 case YANG_POSITION:
818 return LYEXT_SUBSTMT_POSITION;
819 case YANG_UNIQUE:
820 return LYEXT_SUBSTMT_UNIQUE;
821 case YANG_IF_FEATURE:
822 return LYEXT_SUBSTMT_IFFEATURE;
823 default:
824 return LYEXT_SUBSTMT_SELF;
825 }
826}
827
David Sedlák9c40a922019-07-08 17:04:43 +0200828/**
829 * @brief Parse belongs-to element.
830 *
831 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200832 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200833 * @param[in,out] data Data to read from, always moved to currently handled character.
834 * @param[out] submod Structure of submodule that is being parsed.
835 * @param[in,out] exts Extension instances to add to.
836 *
837 * @return LY_ERR values
838 */
839static LY_ERR
840yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
841 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
842{
David Sedlák968ac342019-07-11 15:17:59 +0200843 struct yin_subelement subelems[2] = {
844 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
845 {YANG_CUSTOM, NULL, 0}
846 };
David Sedlák1f90d252019-07-10 17:09:32 +0200847 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 +0200848
849 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
850}
851
David Sedlákd6e56892019-07-01 15:40:24 +0200852LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200853yin_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 +0200854 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 +0200855{
856 LY_ERR ret = LY_SUCCESS;
857 struct sized_string prefix, name;
858 char *out;
859 size_t out_len;
860 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200861 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200862 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200863 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200864 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200865 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200866 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200867
David Sedlákda8ffa32019-07-08 14:17:10 +0200868 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
869 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200870 /* current element has subelements as content */
871 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200872 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
873 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200874 LY_CHECK_GOTO(ret, cleanup);
875 if (!name.value) {
876 /* end of current element reached */
877 break;
878 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200879 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200880 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +0200881 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +0200882
883 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200884 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
885 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200886 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 +0200887 ret = LY_EVALID;
888 goto cleanup;
889 }
890
David Sedlák5f8191e2019-07-08 16:35:52 +0200891 /* TODO check relative order */
892
David Sedlák21f87cd2019-07-03 16:53:23 +0200893 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200894 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200895 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 +0200896 return LY_EVALID;
897 }
898 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlák66d7c842019-07-11 15:06:04 +0200899 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem_info_rec);
900 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +0200901 }
902 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
903
David Sedlákd6e56892019-07-01 15:40:24 +0200904 switch (kw) {
905 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200906 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlák1f90d252019-07-10 17:09:32 +0200907 ret = yin_parse_extension_instance(ctx, subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200908 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200909 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200910 break;
911 case YANG_ACTION:
912 break;
913 case YANG_ANYDATA:
914 break;
915 case YANG_ANYXML:
916 break;
917 case YANG_ARGUMENT:
David Sedlák1f90d252019-07-10 17:09:32 +0200918 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 +0200919 break;
920 case YANG_AUGMENT:
921 break;
922 case YANG_BASE:
David Sedlák292763b2019-07-09 11:10:53 +0200923 if (current_element == YANG_IDENTITY) {
924 type = (struct lysp_type *)subelem_info_rec->dest;
925 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
926 Y_PREF_IDENTIF_ARG, exts);
927 type->flags |= LYS_SET_BASE;
928 } else if (current_element == YANG_TYPE) {
929 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
930 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
931 } else {
932 LOGINT(ctx->xml_ctx.ctx);
933 ret = LY_EINT;
934 }
David Sedlákd6e56892019-07-01 15:40:24 +0200935 break;
936 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +0200937 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 +0200938 break;
939 case YANG_BIT:
940 break;
941 case YANG_CASE:
942 break;
943 case YANG_CHOICE:
944 break;
945 case YANG_CONFIG:
David Sedláke1a30302019-07-10 13:49:38 +0200946 ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200947 break;
948 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200949 case YANG_DESCRIPTION:
950 case YANG_ORGANIZATION:
951 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200952 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200953 break;
954 case YANG_CONTAINER:
955 break;
956 case YANG_DEFAULT:
David Sedláke7084ce2019-07-10 16:44:15 +0200957 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
958 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200959 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200960 case YANG_DEVIATE:
961 break;
962 case YANG_DEVIATION:
963 break;
964 case YANG_ENUM:
965 break;
966 case YANG_ERROR_APP_TAG:
David Sedlák2ce1be62019-07-10 16:15:09 +0200967 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
968 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200969 break;
970 case YANG_ERROR_MESSAGE:
David Sedlákc1771b12019-07-10 15:55:46 +0200971 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200972 break;
973 case YANG_EXTENSION:
David Sedlák1f90d252019-07-10 17:09:32 +0200974 ret = yin_parse_extension(ctx, subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200975 break;
976 case YANG_FEATURE:
977 break;
978 case YANG_FRACTION_DIGITS:
979 break;
980 case YANG_GROUPING:
981 break;
982 case YANG_IDENTITY:
983 break;
984 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200985 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200986 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200987 break;
988 case YANG_IMPORT:
David Sedlák1f90d252019-07-10 17:09:32 +0200989 ret = yin_parse_import(ctx, subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200990 break;
991 case YANG_INCLUDE:
992 break;
993 case YANG_INPUT:
994 break;
995 case YANG_KEY:
996 break;
997 case YANG_LEAF:
998 break;
999 case YANG_LEAF_LIST:
1000 break;
1001 case YANG_LENGTH:
David Sedlák438ae432019-07-11 15:36:54 +02001002 type = (struct lysp_type *)subelem_info_rec->dest;
1003 type->length = calloc(1, sizeof *type->length);
1004 LY_CHECK_ERR_GOTO(!type->length, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
1005 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->length);
1006 type->flags |= LYS_SET_LENGTH;
David Sedlákd6e56892019-07-01 15:40:24 +02001007 break;
1008 case YANG_LIST:
1009 break;
1010 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +02001011 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001012 break;
1013 case YANG_MAX_ELEMENTS:
1014 break;
1015 case YANG_MIN_ELEMENTS:
1016 break;
1017 case YANG_MODIFIER:
David Sedlákce77bf52019-07-11 16:59:31 +02001018 ret = yin_parse_modifier(ctx, subelem_attrs, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001019 break;
1020 case YANG_MODULE:
1021 break;
1022 case YANG_MUST:
David Sedlákbc9ec9c2019-07-11 15:53:55 +02001023 ret = yin_parse_must(ctx, subelem_attrs, data, (struct lysp_restr **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001024 break;
1025 case YANG_NAMESPACE:
David Sedlák2ce1be62019-07-10 16:15:09 +02001026 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1027 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001028 break;
1029 case YANG_NOTIFICATION:
1030 break;
1031 case YANG_ORDERED_BY:
1032 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001033 case YANG_OUTPUT:
1034 break;
1035 case YANG_PATH:
David Sedlák58979872019-07-12 11:42:43 +02001036 type = (struct lysp_type *)subelem_info_rec->dest;
1037 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, &type->path,
1038 YIN_ARG_VALUE, Y_STR_ARG, exts);
1039 type->flags |= LYS_SET_PATH;
David Sedlákd6e56892019-07-01 15:40:24 +02001040 break;
1041 case YANG_PATTERN:
David Sedlákd3983112019-07-12 11:20:56 +02001042 ret = yin_parse_pattern(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001043 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02001044 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02001045 case YANG_POSITION:
David Sedlák5545f5d2019-07-11 11:55:16 +02001046 ret = yin_parse_value_pos_element(ctx, subelem_attrs, data, kw,
1047 (struct lysp_type_enum *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001048 break;
1049 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +02001050 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +02001051 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001052 break;
1053 case YANG_PRESENCE:
1054 break;
1055 case YANG_RANGE:
David Sedlákb7296dd2019-07-11 14:58:38 +02001056 type = (struct lysp_type *)subelem_info_rec->dest;
1057 type->range = calloc(1, sizeof *type->range);
David Sedlák66d7c842019-07-11 15:06:04 +02001058 LY_CHECK_ERR_GOTO(!type->range, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, cleanup);
David Sedlákb7296dd2019-07-11 14:58:38 +02001059 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->range);
David Sedlák438ae432019-07-11 15:36:54 +02001060 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +02001061 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001062 case YANG_REFINE:
1063 break;
1064 case YANG_REQUIRE_INSTANCE:
David Sedlákcf5569a2019-07-11 13:31:34 +02001065 ret = yin_pasrse_reqinstance(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001066 break;
1067 case YANG_REVISION:
1068 break;
1069 case YANG_REVISION_DATE:
David Sedlák1f90d252019-07-10 17:09:32 +02001070 ret = yin_parse_revision_date(ctx, subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001071 break;
1072 case YANG_RPC:
1073 break;
1074 case YANG_STATUS:
David Sedlák1f90d252019-07-10 17:09:32 +02001075 ret = yin_parse_status(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001076 break;
1077 case YANG_SUBMODULE:
1078 break;
1079 case YANG_TYPE:
1080 break;
1081 case YANG_TYPEDEF:
1082 break;
1083 case YANG_UNIQUE:
David Sedláka5b1d382019-07-10 16:31:09 +02001084 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
1085 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001086 break;
1087 case YANG_UNITS:
David Sedláka5b1d382019-07-10 16:31:09 +02001088 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
1089 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001090 break;
1091 case YANG_USES:
1092 break;
David Sedlákd6e56892019-07-01 15:40:24 +02001093 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +02001094 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02001095 break;
1096 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +02001097 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02001098 break;
1099 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +02001100 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 +02001101 break;
1102 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02001103 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +02001104 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02001105 break;
1106 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02001107 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02001108 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02001109 }
David Sedlák3ffbc522019-07-02 17:49:28 +02001110 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +02001111 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001112 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +02001113 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02001114 }
1115 } else {
1116 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02001117 /* save text content, if text_content isn't set, it's just ignored */
1118 if (text_content) {
1119 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001120 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02001121 if (!*text_content) {
1122 free(out);
1123 return LY_EMEM;
1124 }
1125 } else {
1126 if (out_len == 0) {
1127 *text_content = NULL;
1128 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001129 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02001130 }
1131 }
1132 }
David Sedlákd6e56892019-07-01 15:40:24 +02001133 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001134 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 +02001135 }
David Sedlák555c7202019-07-04 12:14:12 +02001136
David Sedlákda8ffa32019-07-08 14:17:10 +02001137 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02001138 }
1139
1140cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +02001141 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001142 return ret;
1143}
1144
David Sedlák619db942019-07-03 14:47:30 +02001145LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001146yin_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 +02001147 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +02001148{
David Sedlák3ffbc522019-07-02 17:49:28 +02001149 const char *temp_rev;
David Sedlák968ac342019-07-11 15:17:59 +02001150 struct yin_subelement subelems[1] = {
1151 {YANG_CUSTOM, NULL, 0}
1152 };
David Sedlák81e04022019-04-05 15:05:46 +02001153
David Sedlák292763b2019-07-09 11:10:53 +02001154 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 +02001155 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 +02001156
1157 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +02001158 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +02001159
David Sedlákda8ffa32019-07-08 14:17:10 +02001160 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001161}
David Sedlák00250342019-06-21 14:19:39 +02001162
David Sedláke1a30302019-07-10 13:49:38 +02001163/**
1164 * @brief Parse config element.
1165 *
1166 * @param[in] ctx Yin parser context for logging and to store current state.
1167 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1168 * @param[in,out] data Data to read from, always moved to currently handled character.
1169 * @param[in,out] flags Flags to add to.
1170 * @param[in,out] exts Extension instances to add to.
1171 *
1172 * @return LY_ERR values.
1173 */
1174static LY_ERR
1175yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1176 struct lysp_ext_instance **exts)
1177{
1178 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001179 struct yin_subelement subelems[1] = {
1180 {YANG_CUSTOM, NULL, 0}
1181 };
David Sedláke1a30302019-07-10 13:49:38 +02001182
David Sedlák1f90d252019-07-10 17:09:32 +02001183 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 +02001184 if (strcmp(temp_val, "true") == 0) {
1185 *flags |= LYS_CONFIG_W;
1186 } else if (strcmp(temp_val, "false") == 0) {
1187 *flags |= LYS_CONFIG_R;
1188 } else {
1189 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
1190 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1191 return LY_EVALID;
1192 }
1193 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1194
1195 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1196}
1197
David Sedlák3ffbc522019-07-02 17:49:28 +02001198LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +02001199yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1200 struct lysp_ext_instance **exts)
1201{
1202 const char *temp_version = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001203 struct yin_subelement subelems[1] = {
1204 {YANG_CUSTOM, NULL, 0}
1205 };
David Sedlák92147b02019-07-09 14:01:01 +02001206
David Sedlák1f90d252019-07-10 17:09:32 +02001207 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 +02001208 if (strcmp(temp_version, "1.0") == 0) {
1209 *version = LYS_VERSION_1_0;
1210 } else if (strcmp(temp_version, "1.1") == 0) {
1211 *version = LYS_VERSION_1_1;
1212 } else {
1213 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
1214 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1215 return LY_EVALID;
1216 }
1217 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1218 ctx->mod_version = *version;
1219
1220 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1221}
1222
1223LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001224yin_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 +01001225{
David Sedlák736fd0d2019-02-15 16:06:31 +01001226 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +02001227 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +02001228 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02001229
David Sedlák968ac342019-07-11 15:17:59 +02001230 struct yin_subelement subelems[5] = {
1231 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1232 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1233 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1234 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1235 {YANG_CUSTOM, NULL, 0}
1236 };
David Sedlák736fd0d2019-02-15 16:06:31 +01001237
David Sedlák92147b02019-07-09 14:01:01 +02001238 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +02001239 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 +02001240 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +02001241 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +02001242 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 +02001243
David Sedlákda8ffa32019-07-08 14:17:10 +02001244 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +01001245}
1246
David Sedlák11900c82019-06-18 16:29:12 +02001247LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +02001248yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1249 struct lysp_ext_instance **exts)
1250{
1251 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001252 struct yin_subelement subelems[1] = {
1253 {YANG_CUSTOM, NULL, 0}
1254 };
David Sedlák1fdb2522019-07-09 16:22:57 +02001255
David Sedlák1f90d252019-07-10 17:09:32 +02001256 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 +02001257 if (strcmp(temp_val, "true") == 0) {
1258 *flags |= LYS_MAND_TRUE;
1259 } else if (strcmp(temp_val, "false") == 0) {
1260 *flags |= LYS_MAND_FALSE;
1261 } else {
1262 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
1263 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1264 return LY_EVALID;
1265 }
1266 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1267
1268 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1269}
1270
1271LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001272yin_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 +02001273 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +02001274{
David Sedlák3ffbc522019-07-02 17:49:28 +02001275 const char *value = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001276 struct yin_subelement subelems[1] = {
1277 {YANG_CUSTOM, NULL, 0}
1278 };
David Sedlák3ffbc522019-07-02 17:49:28 +02001279
David Sedlák292763b2019-07-09 11:10:53 +02001280 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 +02001281 if (strcmp(value, "current") == 0) {
1282 *flags |= LYS_STATUS_CURR;
1283 } else if (strcmp(value, "deprecated") == 0) {
1284 *flags |= LYS_STATUS_DEPRC;
1285 } else if (strcmp(value, "obsolete") == 0) {
1286 *flags |= LYS_STATUS_OBSLT;
1287 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001288 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1289 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001290 return LY_EVALID;
1291 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001292 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001293
David Sedlákda8ffa32019-07-08 14:17:10 +02001294 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001295}
1296
David Sedlák11900c82019-06-18 16:29:12 +02001297LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001298yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1299{
1300 struct lysp_when *when;
1301 when = calloc(1, sizeof *when);
1302 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1f90d252019-07-10 17:09:32 +02001303 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
David Sedlák32eee7b2019-07-09 12:38:44 +02001304 *when_p = when;
David Sedlák968ac342019-07-11 15:17:59 +02001305 struct yin_subelement subelems[3] = {
1306 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1307 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1308 {YANG_CUSTOM, NULL, 0}
1309 };
David Sedlák32eee7b2019-07-09 12:38:44 +02001310
1311 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1312}
1313
1314LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001315yin_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 +02001316 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001317{
David Sedlák3ffbc522019-07-02 17:49:28 +02001318 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +02001319 struct yin_subelement subelems[1] = {
1320 {YANG_CUSTOM, NULL, 0}
1321 };
David Sedlák2721d3d2019-06-21 15:37:41 +02001322
David Sedlák1f90d252019-07-10 17:09:32 +02001323 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 +02001324 if (strcmp(temp_val, "true") == 0) {
1325 *flags |= LYS_YINELEM_TRUE;
1326 } else if (strcmp(temp_val, "false") == 0) {
1327 *flags |= LYS_YINELEM_FALSE;
1328 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001329 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1330 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001331 return LY_EVALID;
1332 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001333 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001334
David Sedlákda8ffa32019-07-08 14:17:10 +02001335 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001336}
1337
1338LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001339yin_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 +02001340 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001341{
1342 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001343 char *out;
1344 const char *name, *prefix;
1345 size_t out_len, prefix_len, name_len;
1346 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001347 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001348 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1349 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001350
David Sedlákda8ffa32019-07-08 14:17:10 +02001351 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001352
1353 e->yin = 0;
1354 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001355 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001356 e->insubstmt = subelem;
1357 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001358 e->yin |= LYS_YIN;
1359
David Sedlákb1a78352019-06-28 16:16:29 +02001360 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02001361 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02001362 if (!iter->prefix) {
1363 new_subelem = calloc(1, sizeof(*new_subelem));
1364 if (!e->child) {
1365 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001366 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001367 last_subelem->next = new_subelem;
1368 }
1369 last_subelem = new_subelem;
1370
1371 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001372 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1373 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001374 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001375 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1376 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001377 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001378 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1379 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001380 }
1381 }
1382 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001383
David Sedlákf250ecf2019-07-01 11:02:05 +02001384 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001385 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1386 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001387 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001388 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1389 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001390 if (!name) {
1391 /* end of extension instance reached */
1392 break;
1393 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001394 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 +02001395 if (!e->child) {
1396 e->child = new_subelem;
1397 } else {
1398 last_subelem->next = new_subelem;
1399 }
1400 last_subelem = new_subelem;
1401 }
David Sedlák555c7202019-07-04 12:14:12 +02001402 } else {
1403 /* save text content */
1404 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001405 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001406 if (!e->argument) {
1407 free(out);
1408 return LY_EMEM;
1409 }
1410 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001411 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001412 LY_CHECK_RET(!e->argument, LY_EMEM);
1413 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001414 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001415 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001416 }
David Sedlákb1a78352019-06-28 16:16:29 +02001417 }
1418
1419 return LY_SUCCESS;
1420}
1421
1422LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001423yin_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 +02001424 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001425{
1426 LY_ERR ret = LY_SUCCESS;
1427 const char *temp_prefix, *temp_name;
1428 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001429 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001430 int dynamic;
1431 struct yin_arg_record *subelem_args = NULL;
1432 struct lysp_stmt *last = NULL, *new = NULL;
1433
1434 /* allocate new structure for element */
1435 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001436 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1437 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001438
1439 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001440 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001441 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001442 /* add new element to linked-list */
1443 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001444 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001445 if (!(*element)->child) {
1446 /* save first */
1447 (*element)->child = new;
1448 } else {
1449 last->next = new;
1450 }
1451 last = new;
1452
1453 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001454 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 +02001455 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001456 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001457 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001458 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1459 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001460 /* attributes with prefix are ignored */
1461 if (!temp_prefix) {
1462 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001463 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001464 if (!last->arg) {
1465 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001466 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001467 ret = LY_EMEM;
1468 goto err;
1469 }
1470 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001471 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1472 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001473 }
1474 }
1475 }
1476
1477 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001478 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001479 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001480 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001481 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001482 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 +02001483 LY_CHECK_GOTO(ret, err);
1484 if (!name) {
1485 /* end of element reached */
1486 break;
1487 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001488 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 +02001489 LY_CHECK_GOTO(ret, err);
1490 last = last->next;
1491 }
1492 } else {
1493 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001494 if (out_len != 0) {
1495 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001496 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001497 if (!(*element)->arg) {
1498 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001499 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001500 ret = LY_EMEM;
1501 goto err;
1502 }
1503 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001504 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1505 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001506 }
David Sedlákb1a78352019-06-28 16:16:29 +02001507 }
1508 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001509 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 +02001510 LY_CHECK_GOTO(ret, err);
1511 }
1512
David Sedlákda8ffa32019-07-08 14:17:10 +02001513 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001514 return LY_SUCCESS;
1515
1516err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001517 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001518 return ret;
1519}
1520
1521LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001522yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001523 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001524{
David Sedlák968ac342019-07-11 15:17:59 +02001525 struct yin_subelement subelems[2] = {
1526 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1527 {YANG_CUSTOM, NULL, 0}
1528 };
David Sedlák9494eb22019-06-21 16:06:53 +02001529
David Sedlák292763b2019-07-09 11:10:53 +02001530 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 +02001531
David Sedlákda8ffa32019-07-08 14:17:10 +02001532 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001533}
1534
1535LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001536yin_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 +02001537{
David Sedlák11900c82019-06-18 16:29:12 +02001538 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001539 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001540 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 +02001541
David Sedlák3ffbc522019-07-02 17:49:28 +02001542 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
David Sedlák968ac342019-07-11 15:17:59 +02001543 struct yin_subelement subelems[5] = {
1544 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1545 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1546 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1547 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
1548 {YANG_CUSTOM, NULL, 0}
1549 };
David Sedlák11900c82019-06-18 16:29:12 +02001550
David Sedlákda8ffa32019-07-08 14:17:10 +02001551 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001552}
1553
David Sedlák00250342019-06-21 14:19:39 +02001554/**
1555 * @brief Parse module substatements.
1556 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001557 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001558 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001559 * @param[in,out] data Data to read from.
1560 * @param[out] mod Parsed module structure.
1561 *
1562 * @return LY_ERR values.
1563 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001564static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001565yin_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 +02001566{
David Sedlák968ac342019-07-11 15:17:59 +02001567 struct yin_subelement subelems[9] = {
1568 {YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
1569 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1570 {YANG_EXTENSION, &(*mod)->exts, 0},
1571 {YANG_IMPORT, *mod, 0},
1572 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1573 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
1574 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1575 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
1576 {YANG_CUSTOM, NULL, 0}
1577 };
David Sedlák3b4db242018-10-19 16:11:01 +02001578
David Sedlák292763b2019-07-09 11:10:53 +02001579 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 +01001580
David Sedlákda8ffa32019-07-08 14:17:10 +02001581 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001582}
1583
1584LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001585yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001586{
David Sedláke4889912018-11-02 09:52:40 +01001587 LY_ERR ret = LY_SUCCESS;
1588 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001589 struct lysp_module *mod_p = NULL;
1590 const char *prefix, *name;
1591 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001592
David Sedlák1f90d252019-07-10 17:09:32 +02001593 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001594
David Sedlákda8ffa32019-07-08 14:17:10 +02001595 struct yin_parser_ctx yin_ctx;
1596
1597 /* initialize context */
1598 memset(&yin_ctx, 0, sizeof yin_ctx);
1599 yin_ctx.xml_ctx.ctx = ctx;
1600 yin_ctx.xml_ctx.line = 1;
1601
David Sedláke4889912018-11-02 09:52:40 +01001602
David Sedlák3017da42019-02-15 09:48:04 +01001603 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001604 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001605 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1f90d252019-07-10 17:09:32 +02001606 ret = yin_load_attributes(&yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02001607 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001608 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001609 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001610 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1611 ret = LY_EINVAL;
1612 goto cleanup;
1613 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001614 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 +02001615 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001616 ret = LY_EVALID;
1617 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001618 }
1619
David Sedlák3017da42019-02-15 09:48:04 +01001620 /* allocate module */
1621 mod_p = calloc(1, sizeof *mod_p);
1622 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1623 mod_p->mod = mod;
1624 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001625
David Sedlák00250342019-06-21 14:19:39 +02001626 /* parse module substatements */
David Sedlák1f90d252019-07-10 17:09:32 +02001627 ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001628 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001629
David Sedlák3017da42019-02-15 09:48:04 +01001630 mod_p->parsing = 0;
1631 mod->parsed = mod_p;
1632
1633cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001634 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001635 lysp_module_free(mod_p);
1636 }
David Sedlák1f90d252019-07-10 17:09:32 +02001637 FREE_ARRAY(&yin_ctx, attrs, free_arg_rec);
David Sedlákda8ffa32019-07-08 14:17:10 +02001638 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001639 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001640}