blob: 29e74bac8190f67143334723554cb2c30b338ed8 [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ákb4e44562019-07-04 15:42:12 +0200423 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
424
David Sedlákda8ffa32019-07-08 14:17:10 +0200425 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200426}
427
428/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200429 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
430 * more instances, such as base or if-feature.
431 *
432 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200433 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200434 * @param[in,out] data Data to read from, always moved to currently handled character.
435 * @param[in] kw Type of current element.
436 * @param[out] values Parsed values to add to.
437 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200438 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200439 * @param[in,out] exts Extension instance to add to.
440 *
441 * @return LY_ERR values.
442 */
443static LY_ERR
444yin_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 +0200445 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 +0200446{
447 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200448 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200449 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
450 struct yin_subelement subelems[1] = {{YANG_CUSTOM, &index, 0}};
David Sedlák1f90d252019-07-10 17:09:32 +0200451 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200452
453 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
454}
455
456/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200457 * @brief Parse require instance element.
458 *
459 * @param[in,out] ctx Yin parser context for logging and to store current state.
460 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
461 * @param[in,out] data Data to read from, always moved to currently handled character.
462 * @prama[out] type Type structure to store value, flag and extensions.
463 *
464 * @return LY_ERR values.
465 */
466static LY_ERR
467yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
468 const char **data, struct lysp_type *type)
469{
470 const char *temp_val = NULL;
471 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
472
473 type->flags |= LYS_SET_REQINST;
474 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
475 if (strcmp(temp_val, "true") == 0) {
476 type->require_instance = 1;
477 } else if (strcmp(temp_val, "false") != 0) {
478 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "require-instance");
479 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
480 return LY_EVALID;
481 }
482 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
483
484 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
485}
486
487/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200488 * @brief Parse a restriction element (length, range or one instance of must).
489 *
490 * @param[in,out] ctx Yin parser context for logging and to store current state.
491 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
492 * @param[in,out] data Data to read from, always moved to currently handled character.
493 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
494 * @param[in]
495 */
496static LY_ERR
497yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
498 enum yang_keyword restr_kw, struct lysp_restr *restr)
499{
500 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
501 struct yin_subelement subelems[5] = {
502 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
503 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
504 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
505 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
506 {YANG_CUSTOM, NULL, 0}
507 };
508 /* argument of must is called condition, but argument of length and range is called value */
509 enum YIN_ARGUMENT arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
510 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
511
512 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
513}
514
515/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200516 * @brief Parse position or value element.
517 *
518 * @param[in,out] ctx YIN parser context for logging and to store current state.
519 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
520 * @param[in,out] data Data to read from, always moved to currently handled character.
521 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
522 * @param[out] enm Enum structure to save value, flags and extensions.
523 *
524 * @return LY_ERR values.
525 */
526static LY_ERR
527yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
528 enum yang_keyword kw, struct lysp_type_enum *enm)
529{
530 assert(kw == YANG_POSITION || kw == YANG_VALUE);
531 const char *temp_val = NULL;
532 char *ptr;
533 long int num;
534 unsigned long int unum;
535
536 /* set value flag */
537 enm->flags |= LYS_SET_VALUE;
538
539 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200540 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 +0200541 if (!temp_val || (temp_val[0] == '+') || ((temp_val[0] == '0') && (temp_val[0] != '\0')) || ((kw == YANG_VALUE) && !strcmp(temp_val, "-0"))) {
542 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
543 goto error;
544 }
545
546 /* convert value */
547 errno = 0;
548 if (kw == YANG_VALUE) {
549 num = strtol(temp_val, &ptr, 10);
550 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
551 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
552 goto error;
553 }
554 } else {
555 unum = strtoul(temp_val, &ptr, 10);
556 if (unum > UINT64_C(4294967295)) {
557 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
558 goto error;
559 }
560 }
561 /* check if whole argument value was converted */
562 if (*ptr != '\0') {
563 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
564 }
565 if (errno == ERANGE) {
566 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw));
567 goto error;
568 }
569 /* save correctly ternary operator can't be used because num and unum have different signes */
570 if (kw == YANG_VALUE) {
571 enm->value = num;
572 } else {
573 enm->value = unum;
574 }
575 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
576
577 /* parse subelements */
578 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
579 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
580
581 error:
582 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
583 return LY_EVALID;
584}
585
586/**
David Sedlákc1771b12019-07-10 15:55:46 +0200587 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +0200588 * text element as child
589 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200590 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákcf5569a2019-07-11 13:31:34 +0200591 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +0200592 * @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 +0200593 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +0200594 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200595 *
596 * @return LY_ERR values.
597 */
598static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200599yin_parse_meta_element(struct yin_parser_ctx *ctx, const char **data, enum yang_keyword elem_type,
David Sedlákb4e44562019-07-04 15:42:12 +0200600 const char **value, struct lysp_ext_instance **exts)
601{
602 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
603
604 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
605 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
606
David Sedlákda8ffa32019-07-08 14:17:10 +0200607 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200608}
609
610/**
David Sedlákc1771b12019-07-10 15:55:46 +0200611 * @brief Parse error-message element.
612 *
613 * @param[in,out] ctx Yin parser context for logging and to store current state.
614 * @param[in,out] data Data to read from.
615 * @param[out] value Where the content of error-message element should be stored.
616 * @param[in,out] exts Extension instance to add to.
617 *
618 * @return LY_ERR values.
619 */
620static LY_ERR
621yin_parse_err_msg_element(struct yin_parser_ctx *ctx, const char **data, const char **value,
622 struct lysp_ext_instance **exts)
623{
624 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
625 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}};
626
627 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
628}
629
630/**
David Sedlákb4e44562019-07-04 15:42:12 +0200631 * @brief Map keyword type to substatement info.
632 *
633 * @param[in] kw Keyword type.
634 *
635 * @return correct LYEXT_SUBSTMT information.
636 */
637static LYEXT_SUBSTMT
638kw2lyext_substmt(enum yang_keyword kw)
639{
640 switch (kw) {
641 case YANG_ARGUMENT:
642 return LYEXT_SUBSTMT_ARGUMENT;
643 case YANG_BASE:
644 return LYEXT_SUBSTMT_BASE;
645 case YANG_BELONGS_TO:
646 return LYEXT_SUBSTMT_BELONGSTO;
647 case YANG_CONTACT:
648 return LYEXT_SUBSTMT_CONTACT;
649 case YANG_DEFAULT:
650 return LYEXT_SUBSTMT_DEFAULT;
651 case YANG_DESCRIPTION:
652 return LYEXT_SUBSTMT_DESCRIPTION;
653 case YANG_ERROR_APP_TAG:
654 return LYEXT_SUBSTMT_ERRTAG;
655 case YANG_ERROR_MESSAGE:
656 return LYEXT_SUBSTMT_ERRMSG;
657 case YANG_KEY:
658 return LYEXT_SUBSTMT_KEY;
659 case YANG_NAMESPACE:
660 return LYEXT_SUBSTMT_NAMESPACE;
661 case YANG_ORGANIZATION:
662 return LYEXT_SUBSTMT_ORGANIZATION;
663 case YANG_PATH:
664 return LYEXT_SUBSTMT_PATH;
665 case YANG_PREFIX:
666 return LYEXT_SUBSTMT_PREFIX;
667 case YANG_PRESENCE:
668 return LYEXT_SUBSTMT_PRESENCE;
669 case YANG_REFERENCE:
670 return LYEXT_SUBSTMT_REFERENCE;
671 case YANG_REVISION_DATE:
672 return LYEXT_SUBSTMT_REVISIONDATE;
673 case YANG_UNITS:
674 return LYEXT_SUBSTMT_UNITS;
675 case YANG_VALUE:
676 return LYEXT_SUBSTMT_VALUE;
677 case YANG_YANG_VERSION:
678 return LYEXT_SUBSTMT_VERSION;
679 case YANG_MODIFIER:
680 return LYEXT_SUBSTMT_MODIFIER;
681 case YANG_REQUIRE_INSTANCE:
682 return LYEXT_SUBSTMT_REQINSTANCE;
683 case YANG_YIN_ELEMENT:
684 return LYEXT_SUBSTMT_YINELEM;
685 case YANG_CONFIG:
686 return LYEXT_SUBSTMT_CONFIG;
687 case YANG_MANDATORY:
688 return LYEXT_SUBSTMT_MANDATORY;
689 case YANG_ORDERED_BY:
690 return LYEXT_SUBSTMT_ORDEREDBY;
691 case YANG_STATUS:
692 return LYEXT_SUBSTMT_STATUS;
693 case YANG_FRACTION_DIGITS:
694 return LYEXT_SUBSTMT_FRACDIGITS;
695 case YANG_MAX_ELEMENTS:
696 return LYEXT_SUBSTMT_MAX;
697 case YANG_MIN_ELEMENTS:
698 return LYEXT_SUBSTMT_MIN;
699 case YANG_POSITION:
700 return LYEXT_SUBSTMT_POSITION;
701 case YANG_UNIQUE:
702 return LYEXT_SUBSTMT_UNIQUE;
703 case YANG_IF_FEATURE:
704 return LYEXT_SUBSTMT_IFFEATURE;
705 default:
706 return LYEXT_SUBSTMT_SELF;
707 }
708}
709
David Sedlák9c40a922019-07-08 17:04:43 +0200710/**
711 * @brief Parse belongs-to element.
712 *
713 * @param[in] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200714 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák9c40a922019-07-08 17:04:43 +0200715 * @param[in,out] data Data to read from, always moved to currently handled character.
716 * @param[out] submod Structure of submodule that is being parsed.
717 * @param[in,out] exts Extension instances to add to.
718 *
719 * @return LY_ERR values
720 */
721static LY_ERR
722yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
723 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
724{
725 struct yin_subelement subelems[2] = {{YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
726 {YANG_CUSTOM, NULL, 0}};
David Sedlák1f90d252019-07-10 17:09:32 +0200727 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 +0200728
729 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
730}
731
David Sedlákd6e56892019-07-01 15:40:24 +0200732LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200733yin_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 +0200734 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 +0200735{
736 LY_ERR ret = LY_SUCCESS;
737 struct sized_string prefix, name;
738 char *out;
739 size_t out_len;
740 int dynamic;
David Sedlák3ffbc522019-07-02 17:49:28 +0200741 struct yin_arg_record *subelem_attrs = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200742 enum yang_keyword kw = YANG_NONE;
David Sedlák3ffbc522019-07-02 17:49:28 +0200743 struct yin_subelement *subelem_info_rec = NULL;
David Sedlákcb5d83f2019-07-09 09:32:53 +0200744 uint32_t index = 0;
David Sedlák292763b2019-07-09 11:10:53 +0200745 struct lysp_type *type;
David Sedlákb0faad82019-07-04 14:28:59 +0200746 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +0200747
David Sedlákda8ffa32019-07-08 14:17:10 +0200748 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
749 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +0200750 /* current element has subelements as content */
751 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200752 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
753 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len);
David Sedlákd6e56892019-07-01 15:40:24 +0200754 LY_CHECK_GOTO(ret, cleanup);
755 if (!name.value) {
756 /* end of current element reached */
757 break;
758 }
David Sedlákda8ffa32019-07-08 14:17:10 +0200759 ret = yin_load_attributes(ctx, data, &subelem_attrs);
David Sedlákd6e56892019-07-01 15:40:24 +0200760 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +0200761 kw = yin_match_keyword(ctx, name.value, name.len, prefix.value, prefix.len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +0200762
763 /* check if this element can be child of current element */
David Sedlák3ffbc522019-07-02 17:49:28 +0200764 subelem_info_rec = get_record(kw, subelem_info_size, subelem_info);
765 if (!subelem_info_rec) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200766 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 +0200767 ret = LY_EVALID;
768 goto cleanup;
769 }
770
David Sedlák5f8191e2019-07-08 16:35:52 +0200771 /* TODO check relative order */
772
David Sedlák21f87cd2019-07-03 16:53:23 +0200773 /* if element is unique and already defined log error */
David Sedlák555c7202019-07-04 12:14:12 +0200774 if ((subelem_info_rec->flags & YIN_SUBELEM_UNIQUE) && (subelem_info_rec->flags & YIN_SUBELEM_PARSED)) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200775 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 +0200776 return LY_EVALID;
777 }
778 if (subelem_info_rec->flags & YIN_SUBELEM_FIRST) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200779 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 +0200780 }
781 subelem_info_rec->flags |= YIN_SUBELEM_PARSED;
782
David Sedlákd6e56892019-07-01 15:40:24 +0200783 switch (kw) {
784 case YANG_CUSTOM:
David Sedlákcb5d83f2019-07-09 09:32:53 +0200785 index = (subelem_info_rec->dest) ? *((uint32_t*)subelem_info_rec->dest) : 0;
David Sedlák1f90d252019-07-10 17:09:32 +0200786 ret = yin_parse_extension_instance(ctx, subelem_attrs, data, name2fullname(name.value, prefix.len),
David Sedlák3ffbc522019-07-02 17:49:28 +0200787 namelen2fulllen(name.len, prefix.len),
David Sedlákcb5d83f2019-07-09 09:32:53 +0200788 kw2lyext_substmt(current_element), index, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200789 break;
790 case YANG_ACTION:
791 break;
792 case YANG_ANYDATA:
793 break;
794 case YANG_ANYXML:
795 break;
796 case YANG_ARGUMENT:
David Sedlák1f90d252019-07-10 17:09:32 +0200797 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 +0200798 break;
799 case YANG_AUGMENT:
800 break;
801 case YANG_BASE:
David Sedlák292763b2019-07-09 11:10:53 +0200802 if (current_element == YANG_IDENTITY) {
803 type = (struct lysp_type *)subelem_info_rec->dest;
804 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, &type->bases, YIN_ARG_NAME,
805 Y_PREF_IDENTIF_ARG, exts);
806 type->flags |= LYS_SET_BASE;
807 } else if (current_element == YANG_TYPE) {
808 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
809 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts);
810 } else {
811 LOGINT(ctx->xml_ctx.ctx);
812 ret = LY_EINT;
813 }
David Sedlákd6e56892019-07-01 15:40:24 +0200814 break;
815 case YANG_BELONGS_TO:
David Sedlák9c40a922019-07-08 17:04:43 +0200816 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 +0200817 break;
818 case YANG_BIT:
819 break;
820 case YANG_CASE:
821 break;
822 case YANG_CHOICE:
823 break;
824 case YANG_CONFIG:
David Sedláke1a30302019-07-10 13:49:38 +0200825 ret = yin_parse_config(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200826 break;
827 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +0200828 case YANG_DESCRIPTION:
829 case YANG_ORGANIZATION:
830 case YANG_REFERENCE:
David Sedlákda8ffa32019-07-08 14:17:10 +0200831 ret = yin_parse_meta_element(ctx, data, kw, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200832 break;
833 case YANG_CONTAINER:
834 break;
835 case YANG_DEFAULT:
David Sedláke7084ce2019-07-10 16:44:15 +0200836 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
837 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200838 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200839 case YANG_DEVIATE:
840 break;
841 case YANG_DEVIATION:
842 break;
843 case YANG_ENUM:
844 break;
845 case YANG_ERROR_APP_TAG:
David Sedlák2ce1be62019-07-10 16:15:09 +0200846 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
847 YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200848 break;
849 case YANG_ERROR_MESSAGE:
David Sedlákc1771b12019-07-10 15:55:46 +0200850 ret = yin_parse_err_msg_element(ctx, data, (const char **)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200851 break;
852 case YANG_EXTENSION:
David Sedlák1f90d252019-07-10 17:09:32 +0200853 ret = yin_parse_extension(ctx, subelem_attrs, data, (struct lysp_ext **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200854 break;
855 case YANG_FEATURE:
856 break;
857 case YANG_FRACTION_DIGITS:
858 break;
859 case YANG_GROUPING:
860 break;
861 case YANG_IDENTITY:
862 break;
863 case YANG_IF_FEATURE:
David Sedlák5f8191e2019-07-08 16:35:52 +0200864 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200865 (const char ***)subelem_info_rec->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200866 break;
867 case YANG_IMPORT:
David Sedlák1f90d252019-07-10 17:09:32 +0200868 ret = yin_parse_import(ctx, subelem_attrs, data, (struct lysp_module *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200869 break;
870 case YANG_INCLUDE:
871 break;
872 case YANG_INPUT:
873 break;
874 case YANG_KEY:
875 break;
876 case YANG_LEAF:
877 break;
878 case YANG_LEAF_LIST:
879 break;
880 case YANG_LENGTH:
881 break;
882 case YANG_LIST:
883 break;
884 case YANG_MANDATORY:
David Sedlák1fdb2522019-07-09 16:22:57 +0200885 ret = yin_parse_mandatory(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200886 break;
887 case YANG_MAX_ELEMENTS:
888 break;
889 case YANG_MIN_ELEMENTS:
890 break;
891 case YANG_MODIFIER:
892 break;
893 case YANG_MODULE:
894 break;
895 case YANG_MUST:
896 break;
897 case YANG_NAMESPACE:
David Sedlák2ce1be62019-07-10 16:15:09 +0200898 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
899 YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200900 break;
901 case YANG_NOTIFICATION:
902 break;
903 case YANG_ORDERED_BY:
904 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200905 case YANG_OUTPUT:
906 break;
907 case YANG_PATH:
908 break;
909 case YANG_PATTERN:
910 break;
David Sedlák5545f5d2019-07-11 11:55:16 +0200911 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +0200912 case YANG_POSITION:
David Sedlák5545f5d2019-07-11 11:55:16 +0200913 ret = yin_parse_value_pos_element(ctx, subelem_attrs, data, kw,
914 (struct lysp_type_enum *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200915 break;
916 case YANG_PREFIX:
David Sedlák5f8191e2019-07-08 16:35:52 +0200917 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
David Sedlák292763b2019-07-09 11:10:53 +0200918 (const char **)subelem_info_rec->dest, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200919 break;
920 case YANG_PRESENCE:
921 break;
922 case YANG_RANGE:
David Sedlákb7296dd2019-07-11 14:58:38 +0200923 type = (struct lysp_type *)subelem_info_rec->dest;
924 type->range = calloc(1, sizeof *type->range);
925 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xml_ctx.ctx), LY_EVALID);
926 ret = yin_parse_restriction(ctx, subelem_attrs, data, kw, type->range);
927 type->flags |= LYS_SET_RANGE;
David Sedlákd6e56892019-07-01 15:40:24 +0200928 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200929 case YANG_REFINE:
930 break;
931 case YANG_REQUIRE_INSTANCE:
David Sedlákcf5569a2019-07-11 13:31:34 +0200932 ret = yin_pasrse_reqinstance(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200933 break;
934 case YANG_REVISION:
935 break;
936 case YANG_REVISION_DATE:
David Sedlák1f90d252019-07-10 17:09:32 +0200937 ret = yin_parse_revision_date(ctx, subelem_attrs, data, (char *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200938 break;
939 case YANG_RPC:
940 break;
941 case YANG_STATUS:
David Sedlák1f90d252019-07-10 17:09:32 +0200942 ret = yin_parse_status(ctx, subelem_attrs, data, (uint16_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200943 break;
944 case YANG_SUBMODULE:
945 break;
946 case YANG_TYPE:
947 break;
948 case YANG_TYPEDEF:
949 break;
950 case YANG_UNIQUE:
David Sedláka5b1d382019-07-10 16:31:09 +0200951 ret = yin_parse_simple_elements(ctx, subelem_attrs, data, kw, (const char ***)subelem_info_rec->dest,
952 YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200953 break;
954 case YANG_UNITS:
David Sedláka5b1d382019-07-10 16:31:09 +0200955 ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw, (const char **)subelem_info_rec->dest,
956 YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200957 break;
958 case YANG_USES:
959 break;
David Sedlákd6e56892019-07-01 15:40:24 +0200960 case YANG_WHEN:
David Sedlák32eee7b2019-07-09 12:38:44 +0200961 ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
David Sedlákd6e56892019-07-01 15:40:24 +0200962 break;
963 case YANG_YANG_VERSION:
David Sedlák92147b02019-07-09 14:01:01 +0200964 ret = yin_parse_yangversion(ctx, subelem_attrs, data, (uint8_t *)subelem_info_rec->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +0200965 break;
966 case YANG_YIN_ELEMENT:
David Sedlákda8ffa32019-07-08 14:17:10 +0200967 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 +0200968 break;
969 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +0200970 case YIN_VALUE:
David Sedlákc1771b12019-07-10 15:55:46 +0200971 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem_info_rec->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +0200972 break;
973 default:
David Sedlákda8ffa32019-07-08 14:17:10 +0200974 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +0200975 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +0200976 }
David Sedlák3ffbc522019-07-02 17:49:28 +0200977 LY_CHECK_GOTO(ret, cleanup);
David Sedlákda8ffa32019-07-08 14:17:10 +0200978 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +0200979 subelem_attrs = NULL;
David Sedlák3ffbc522019-07-02 17:49:28 +0200980 subelem_info_rec = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +0200981 }
982 } else {
983 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +0200984 /* save text content, if text_content isn't set, it's just ignored */
985 if (text_content) {
986 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200987 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +0200988 if (!*text_content) {
989 free(out);
990 return LY_EMEM;
991 }
992 } else {
993 if (out_len == 0) {
994 *text_content = NULL;
995 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +0200996 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +0200997 }
998 }
999 }
David Sedlákd6e56892019-07-01 15:40:24 +02001000 /* load closing element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001001 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 +02001002 }
David Sedlák555c7202019-07-04 12:14:12 +02001003
David Sedlákda8ffa32019-07-08 14:17:10 +02001004 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02001005 }
1006
1007cleanup:
David Sedlákda8ffa32019-07-08 14:17:10 +02001008 FREE_ARRAY(ctx, subelem_attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02001009 return ret;
1010}
1011
David Sedlák619db942019-07-03 14:47:30 +02001012LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001013yin_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 +02001014 struct lysp_ext_instance **exts)
David Sedlák81e04022019-04-05 15:05:46 +02001015{
David Sedlák3ffbc522019-07-02 17:49:28 +02001016 const char *temp_rev;
David Sedlák619db942019-07-03 14:47:30 +02001017 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák81e04022019-04-05 15:05:46 +02001018
David Sedlák292763b2019-07-09 11:10:53 +02001019 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 +02001020 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 +02001021
1022 strcpy(rev, temp_rev);
David Sedlákda8ffa32019-07-08 14:17:10 +02001023 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
David Sedlák81e04022019-04-05 15:05:46 +02001024
David Sedlákda8ffa32019-07-08 14:17:10 +02001025 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02001026}
David Sedlák00250342019-06-21 14:19:39 +02001027
David Sedláke1a30302019-07-10 13:49:38 +02001028/**
1029 * @brief Parse config element.
1030 *
1031 * @param[in] ctx Yin parser context for logging and to store current state.
1032 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1033 * @param[in,out] data Data to read from, always moved to currently handled character.
1034 * @param[in,out] flags Flags to add to.
1035 * @param[in,out] exts Extension instances to add to.
1036 *
1037 * @return LY_ERR values.
1038 */
1039static LY_ERR
1040yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1041 struct lysp_ext_instance **exts)
1042{
1043 const char *temp_val = NULL;
1044 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
1045
David Sedlák1f90d252019-07-10 17:09:32 +02001046 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 +02001047 if (strcmp(temp_val, "true") == 0) {
1048 *flags |= LYS_CONFIG_W;
1049 } else if (strcmp(temp_val, "false") == 0) {
1050 *flags |= LYS_CONFIG_R;
1051 } else {
1052 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "config");
1053 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1054 return LY_EVALID;
1055 }
1056 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1057
1058 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1059}
1060
David Sedlák3ffbc522019-07-02 17:49:28 +02001061LY_ERR
David Sedlák92147b02019-07-09 14:01:01 +02001062yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1063 struct lysp_ext_instance **exts)
1064{
1065 const char *temp_version = NULL;
1066 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
1067
David Sedlák1f90d252019-07-10 17:09:32 +02001068 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 +02001069 if (strcmp(temp_version, "1.0") == 0) {
1070 *version = LYS_VERSION_1_0;
1071 } else if (strcmp(temp_version, "1.1") == 0) {
1072 *version = LYS_VERSION_1_1;
1073 } else {
1074 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_version, "yang-version");
1075 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1076 return LY_EVALID;
1077 }
1078 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1079 ctx->mod_version = *version;
1080
1081 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1082}
1083
1084LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001085yin_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 +01001086{
David Sedlák736fd0d2019-02-15 16:06:31 +01001087 struct lysp_import *imp;
David Sedlák00250342019-06-21 14:19:39 +02001088 /* allocate new element in sized array for import */
David Sedlákda8ffa32019-07-08 14:17:10 +02001089 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, mod->imports, imp, LY_EMEM);
David Sedlák3ffbc522019-07-02 17:49:28 +02001090
David Sedlákb0faad82019-07-04 14:28:59 +02001091 struct yin_subelement subelems[5] = {{YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1092 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
David Sedlák3ffbc522019-07-02 17:49:28 +02001093 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1094 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001095 {YANG_CUSTOM, NULL, 0}};
David Sedlák736fd0d2019-02-15 16:06:31 +01001096
David Sedlák92147b02019-07-09 14:01:01 +02001097 /* parse import attributes */
David Sedlák292763b2019-07-09 11:10:53 +02001098 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 +02001099 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
David Sedlák619db942019-07-03 14:47:30 +02001100 /* check prefix validity */
David Sedlákda8ffa32019-07-08 14:17:10 +02001101 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 +02001102
David Sedlákda8ffa32019-07-08 14:17:10 +02001103 return yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts);
David Sedlák736fd0d2019-02-15 16:06:31 +01001104}
1105
David Sedlák11900c82019-06-18 16:29:12 +02001106LY_ERR
David Sedlák1fdb2522019-07-09 16:22:57 +02001107yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1108 struct lysp_ext_instance **exts)
1109{
1110 const char *temp_val = NULL;
1111 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
1112
David Sedlák1f90d252019-07-10 17:09:32 +02001113 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 +02001114 if (strcmp(temp_val, "true") == 0) {
1115 *flags |= LYS_MAND_TRUE;
1116 } else if (strcmp(temp_val, "false") == 0) {
1117 *flags |= LYS_MAND_FALSE;
1118 } else {
1119 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "mandatory");
1120 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1121 return LY_EVALID;
1122 }
1123 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1124
1125 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1126}
1127
1128LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001129yin_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 +02001130 struct lysp_ext_instance **exts)
David Sedlák11900c82019-06-18 16:29:12 +02001131{
David Sedlák3ffbc522019-07-02 17:49:28 +02001132 const char *value = NULL;
David Sedlák619db942019-07-03 14:47:30 +02001133 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák3ffbc522019-07-02 17:49:28 +02001134
David Sedlák292763b2019-07-09 11:10:53 +02001135 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 +02001136 if (strcmp(value, "current") == 0) {
1137 *flags |= LYS_STATUS_CURR;
1138 } else if (strcmp(value, "deprecated") == 0) {
1139 *flags |= LYS_STATUS_DEPRC;
1140 } else if (strcmp(value, "obsolete") == 0) {
1141 *flags |= LYS_STATUS_OBSLT;
1142 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001143 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, value, "status");
1144 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001145 return LY_EVALID;
1146 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001147 FREE_STRING(ctx->xml_ctx.ctx, value);
David Sedlák11900c82019-06-18 16:29:12 +02001148
David Sedlákda8ffa32019-07-08 14:17:10 +02001149 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
David Sedlák11900c82019-06-18 16:29:12 +02001150}
1151
David Sedlák11900c82019-06-18 16:29:12 +02001152LY_ERR
David Sedlák32eee7b2019-07-09 12:38:44 +02001153yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1154{
1155 struct lysp_when *when;
1156 when = calloc(1, sizeof *when);
1157 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1f90d252019-07-10 17:09:32 +02001158 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
David Sedlák32eee7b2019-07-09 12:38:44 +02001159 *when_p = when;
1160 struct yin_subelement subelems[3] = {{YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1161 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1162 {YANG_CUSTOM, NULL, 0}};
1163
1164 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1165}
1166
1167LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001168yin_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 +02001169 uint16_t *flags, struct lysp_ext_instance **exts)
David Sedlák2721d3d2019-06-21 15:37:41 +02001170{
David Sedlák3ffbc522019-07-02 17:49:28 +02001171 const char *temp_val = NULL;
David Sedlák619db942019-07-03 14:47:30 +02001172 struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
David Sedlák2721d3d2019-06-21 15:37:41 +02001173
David Sedlák1f90d252019-07-10 17:09:32 +02001174 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 +02001175 if (strcmp(temp_val, "true") == 0) {
1176 *flags |= LYS_YINELEM_TRUE;
1177 } else if (strcmp(temp_val, "false") == 0) {
1178 *flags |= LYS_YINELEM_FALSE;
1179 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001180 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "yin-element");
1181 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001182 return LY_EVALID;
1183 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001184 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlák2721d3d2019-06-21 15:37:41 +02001185
David Sedlákda8ffa32019-07-08 14:17:10 +02001186 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
David Sedlák2721d3d2019-06-21 15:37:41 +02001187}
1188
1189LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001190yin_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 +02001191 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02001192{
1193 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02001194 char *out;
1195 const char *name, *prefix;
1196 size_t out_len, prefix_len, name_len;
1197 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001198 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02001199 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
1200 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001201
David Sedlákda8ffa32019-07-08 14:17:10 +02001202 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001203
1204 e->yin = 0;
1205 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02001206 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02001207 e->insubstmt = subelem;
1208 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001209 e->yin |= LYS_YIN;
1210
David Sedlákb1a78352019-06-28 16:16:29 +02001211 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02001212 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02001213 if (!iter->prefix) {
1214 new_subelem = calloc(1, sizeof(*new_subelem));
1215 if (!e->child) {
1216 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02001217 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02001218 last_subelem->next = new_subelem;
1219 }
1220 last_subelem = new_subelem;
1221
1222 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001223 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
1224 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001225 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001226 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
1227 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001228 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001229 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
1230 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001231 }
1232 }
1233 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02001234
David Sedlákf250ecf2019-07-01 11:02:05 +02001235 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001236 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
1237 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001238 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001239 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
1240 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02001241 if (!name) {
1242 /* end of extension instance reached */
1243 break;
1244 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001245 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 +02001246 if (!e->child) {
1247 e->child = new_subelem;
1248 } else {
1249 last_subelem->next = new_subelem;
1250 }
1251 last_subelem = new_subelem;
1252 }
David Sedlák555c7202019-07-04 12:14:12 +02001253 } else {
1254 /* save text content */
1255 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001256 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02001257 if (!e->argument) {
1258 free(out);
1259 return LY_EMEM;
1260 }
1261 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001262 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02001263 LY_CHECK_RET(!e->argument, LY_EMEM);
1264 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001265 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02001266 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02001267 }
David Sedlákb1a78352019-06-28 16:16:29 +02001268 }
1269
1270 return LY_SUCCESS;
1271}
1272
1273LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02001274yin_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 +02001275 size_t prefix_len, const char **data, struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02001276{
1277 LY_ERR ret = LY_SUCCESS;
1278 const char *temp_prefix, *temp_name;
1279 char *out = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +02001280 size_t out_len, temp_name_len, temp_prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02001281 int dynamic;
1282 struct yin_arg_record *subelem_args = NULL;
1283 struct lysp_stmt *last = NULL, *new = NULL;
1284
1285 /* allocate new structure for element */
1286 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02001287 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
1288 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02001289
1290 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02001291 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02001292 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02001293 /* add new element to linked-list */
1294 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02001295 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02001296 if (!(*element)->child) {
1297 /* save first */
1298 (*element)->child = new;
1299 } else {
1300 last->next = new;
1301 }
1302 last = new;
1303
1304 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02001305 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 +02001306 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001307 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001308 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02001309 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
1310 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001311 /* attributes with prefix are ignored */
1312 if (!temp_prefix) {
1313 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001314 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02001315 if (!last->arg) {
1316 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001317 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02001318 ret = LY_EMEM;
1319 goto err;
1320 }
1321 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001322 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1323 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001324 }
1325 }
1326 }
1327
1328 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02001329 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02001330 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001331 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02001332 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02001333 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 +02001334 LY_CHECK_GOTO(ret, err);
1335 if (!name) {
1336 /* end of element reached */
1337 break;
1338 }
David Sedlákda8ffa32019-07-08 14:17:10 +02001339 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 +02001340 LY_CHECK_GOTO(ret, err);
1341 last = last->next;
1342 }
1343 } else {
1344 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02001345 if (out_len != 0) {
1346 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001347 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02001348 if (!(*element)->arg) {
1349 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02001350 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02001351 ret = LY_EMEM;
1352 goto err;
1353 }
1354 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02001355 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
1356 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02001357 }
David Sedlákb1a78352019-06-28 16:16:29 +02001358 }
1359 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02001360 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 +02001361 LY_CHECK_GOTO(ret, err);
1362 }
1363
David Sedlákda8ffa32019-07-08 14:17:10 +02001364 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02001365 return LY_SUCCESS;
1366
1367err:
David Sedlákda8ffa32019-07-08 14:17:10 +02001368 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02001369 return ret;
1370}
1371
1372LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001373yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák3ffbc522019-07-02 17:49:28 +02001374 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
David Sedlák9494eb22019-06-21 16:06:53 +02001375{
David Sedlák3ffbc522019-07-02 17:49:28 +02001376 struct yin_subelement subelems[2] = {{YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001377 {YANG_CUSTOM, NULL, 0}};
David Sedlák9494eb22019-06-21 16:06:53 +02001378
David Sedlák292763b2019-07-09 11:10:53 +02001379 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 +02001380
David Sedlákda8ffa32019-07-08 14:17:10 +02001381 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
David Sedlák9494eb22019-06-21 16:06:53 +02001382}
1383
1384LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001385yin_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 +02001386{
David Sedlák11900c82019-06-18 16:29:12 +02001387 struct lysp_ext *ex;
David Sedlákda8ffa32019-07-08 14:17:10 +02001388 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
David Sedlák292763b2019-07-09 11:10:53 +02001389 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 +02001390
David Sedlák3ffbc522019-07-02 17:49:28 +02001391 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
1392 struct yin_subelement subelems[5] = {{YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
1393 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
1394 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
1395 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001396 {YANG_CUSTOM, NULL, 0}};
David Sedlák11900c82019-06-18 16:29:12 +02001397
David Sedlákda8ffa32019-07-08 14:17:10 +02001398 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
David Sedlák11900c82019-06-18 16:29:12 +02001399}
1400
David Sedlák00250342019-06-21 14:19:39 +02001401/**
1402 * @brief Parse module substatements.
1403 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001404 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +02001405 * @param[in] mod_attrs Attributes of module element.
David Sedlák00250342019-06-21 14:19:39 +02001406 * @param[in,out] data Data to read from.
1407 * @param[out] mod Parsed module structure.
1408 *
1409 * @return LY_ERR values.
1410 */
David Sedlákda8ffa32019-07-08 14:17:10 +02001411static LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02001412yin_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 +02001413{
David Sedlákda8ffa32019-07-08 14:17:10 +02001414 struct yin_subelement subelems[9] = {{YANG_CONTACT, &(*mod)->mod->contact, YIN_SUBELEM_UNIQUE},
David Sedlákb0faad82019-07-04 14:28:59 +02001415 {YANG_DESCRIPTION, &(*mod)->mod->dsc, YIN_SUBELEM_UNIQUE},
1416 {YANG_EXTENSION, &(*mod)->exts, 0},
1417 {YANG_IMPORT, *mod, 0},
1418 {YANG_NAMESPACE, &(*mod)->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1419 {YANG_ORGANIZATION, &(*mod)->mod->org, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001420 {YANG_PREFIX, &(*mod)->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1421 {YANG_REFERENCE, &(*mod)->mod->ref, YIN_SUBELEM_UNIQUE},
David Sedlák619db942019-07-03 14:47:30 +02001422 {YANG_CUSTOM, NULL, 0}};
David Sedlák3b4db242018-10-19 16:11:01 +02001423
David Sedlák292763b2019-07-09 11:10:53 +02001424 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 +01001425
David Sedlákda8ffa32019-07-08 14:17:10 +02001426 return yin_parse_content(ctx, subelems, 9, data, YANG_MODULE, NULL, &(*mod)->exts);
David Sedlák3b4db242018-10-19 16:11:01 +02001427}
1428
1429LY_ERR
David Sedlák3017da42019-02-15 09:48:04 +01001430yin_parse_module(struct ly_ctx *ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02001431{
David Sedláke4889912018-11-02 09:52:40 +01001432 LY_ERR ret = LY_SUCCESS;
1433 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01001434 struct lysp_module *mod_p = NULL;
1435 const char *prefix, *name;
1436 size_t prefix_len, name_len;
David Sedlákda8ffa32019-07-08 14:17:10 +02001437
David Sedlák1f90d252019-07-10 17:09:32 +02001438 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02001439
David Sedlákda8ffa32019-07-08 14:17:10 +02001440 struct yin_parser_ctx yin_ctx;
1441
1442 /* initialize context */
1443 memset(&yin_ctx, 0, sizeof yin_ctx);
1444 yin_ctx.xml_ctx.ctx = ctx;
1445 yin_ctx.xml_ctx.line = 1;
1446
David Sedláke4889912018-11-02 09:52:40 +01001447
David Sedlák3017da42019-02-15 09:48:04 +01001448 /* check submodule */
David Sedlákda8ffa32019-07-08 14:17:10 +02001449 ret = lyxml_get_element(&yin_ctx.xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02001450 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1f90d252019-07-10 17:09:32 +02001451 ret = yin_load_attributes(&yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02001452 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc1771b12019-07-10 15:55:46 +02001453 kw = yin_match_keyword(&yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01001454 if (kw == YANG_SUBMODULE) {
David Sedlák3017da42019-02-15 09:48:04 +01001455 LOGERR(ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
1456 ret = LY_EINVAL;
1457 goto cleanup;
1458 } else if (kw != YANG_MODULE) {
David Sedlákda8ffa32019-07-08 14:17:10 +02001459 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 +02001460 ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01001461 ret = LY_EVALID;
1462 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01001463 }
1464
David Sedlák3017da42019-02-15 09:48:04 +01001465 /* allocate module */
1466 mod_p = calloc(1, sizeof *mod_p);
1467 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
1468 mod_p->mod = mod;
1469 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01001470
David Sedlák00250342019-06-21 14:19:39 +02001471 /* parse module substatements */
David Sedlák1f90d252019-07-10 17:09:32 +02001472 ret = yin_parse_mod(&yin_ctx, attrs, &data, &mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01001473 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01001474
David Sedlák3017da42019-02-15 09:48:04 +01001475 mod_p->parsing = 0;
1476 mod->parsed = mod_p;
1477
1478cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02001479 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01001480 lysp_module_free(mod_p);
1481 }
David Sedlák1f90d252019-07-10 17:09:32 +02001482 FREE_ARRAY(&yin_ctx, attrs, free_arg_rec);
David Sedlákda8ffa32019-07-08 14:17:10 +02001483 lyxml_context_clear(&yin_ctx.xml_ctx);
David Sedlák2e411422018-12-17 02:35:39 +01001484 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02001485}