blob: c4716314714207ce2a1d860dd1750d2249a9b4f0 [file] [log] [blame]
David Sedlákf824ad52018-10-14 23:58:15 +02001/**
2 * @file parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
David Sedlák3b4db242018-10-19 16:11:01 +02004 * @brief YIN parser.
5 *
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
David Sedlák3b4db242018-10-19 16:11:01 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
David Sedlákf824ad52018-10-14 23:58:15 +020013 */
David Sedlákecf5eb82019-06-03 14:12:44 +020014#include "common.h"
15
David Sedlák3ffbc522019-07-02 17:49:28 +020016#include <assert.h>
David Sedlák3b4db242018-10-19 16:11:01 +020017#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
David Sedlák872c7b42018-10-26 13:15:20 +020020#include <string.h>
David Sedlákd6e56892019-07-01 15:40:24 +020021#include <stdbool.h>
David Sedlák5545f5d2019-07-11 11:55:16 +020022#include <errno.h>
David Sedlákf75d55e2019-07-12 16:52:50 +020023#include <ctype.h>
David Sedlák81497a32019-08-13 16:56:26 +020024#include <stdarg.h>
David Sedlákf824ad52018-10-14 23:58:15 +020025
David Sedlákf824ad52018-10-14 23:58:15 +020026#include "context.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020027#include "dict.h"
David Sedlák3b4db242018-10-19 16:11:01 +020028#include "xml.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020029#include "tree.h"
30#include "tree_schema.h"
David Sedlák3b4db242018-10-19 16:11:01 +020031#include "tree_schema_internal.h"
David Sedlákecf5eb82019-06-03 14:12:44 +020032#include "parser_yin.h"
David Sedlák00250342019-06-21 14:19:39 +020033
David Sedlák2b214ac2019-06-06 16:11:03 +020034/**
35 * @brief check if given string is URI of yin namespace.
David Sedlák8985a142019-07-31 16:43:06 +020036 *
David Sedlák2b214ac2019-06-06 16:11:03 +020037 * @param ns Namespace URI to check.
38 *
39 * @return true if ns equals YIN_NS_URI false otherwise.
40 */
41#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
42
David Sedlákf6251182019-06-06 10:22:13 +020043const char *const yin_attr_list[] = {
44 [YIN_ARG_NAME] = "name",
45 [YIN_ARG_TARGET_NODE] = "target-node",
46 [YIN_ARG_MODULE] = "module",
47 [YIN_ARG_VALUE] = "value",
48 [YIN_ARG_TEXT] = "text",
49 [YIN_ARG_CONDITION] = "condition",
50 [YIN_ARG_URI] = "uri",
51 [YIN_ARG_DATE] = "date",
52 [YIN_ARG_TAG] = "tag",
David Sedlákf6251182019-06-06 10:22:13 +020053};
54
David Sedlák1bccdfa2019-06-17 15:55:27 +020055enum yang_keyword
David Sedlákc1771b12019-07-10 15:55:46 +020056yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
57 const char *prefix, size_t prefix_len, enum yang_keyword parrent)
David Sedlák1bccdfa2019-06-17 15:55:27 +020058{
David Sedlák8f7a1172019-06-20 14:42:18 +020059 const char *start = NULL;
60 enum yang_keyword kw = YANG_NONE;
61 const struct lyxml_ns *ns = NULL;
62
63 if (!name || name_len == 0) {
David Sedlák1bccdfa2019-06-17 15:55:27 +020064 return YANG_NONE;
65 }
66
David Sedlákda8ffa32019-07-08 14:17:10 +020067 ns = lyxml_ns_get(&ctx->xml_ctx, prefix, prefix_len);
David Sedlák8f7a1172019-06-20 14:42:18 +020068 if (ns) {
69 if (!IS_YIN_NS(ns->uri)) {
70 return YANG_CUSTOM;
71 }
72 } else {
73 /* elements without namespace are automatically unknown */
74 return YANG_NONE;
75 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020076
David Sedlák8f7a1172019-06-20 14:42:18 +020077 start = name;
78 kw = lysp_match_kw(NULL, &name);
79
80 if (name - start == (long int)name_len) {
David Sedlákc1771b12019-07-10 15:55:46 +020081 /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
82 if (kw == YANG_VALUE && parrent == YANG_ERROR_MESSAGE) {
83 return YIN_VALUE;
84 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020085 return kw;
86 } else {
David Sedlák3ffbc522019-07-02 17:49:28 +020087 if (strncmp(start, "text", name_len) == 0) {
88 return YIN_TEXT;
David Sedlák3ffbc522019-07-02 17:49:28 +020089 } else {
90 return YANG_NONE;
91 }
David Sedlák1bccdfa2019-06-17 15:55:27 +020092 }
93}
94
David Sedlákc5b20842019-08-13 10:18:31 +020095enum yin_argument
David Sedlák060b00e2019-06-19 11:12:06 +020096yin_match_argument_name(const char *name, size_t len)
David Sedlák3b4db242018-10-19 16:11:01 +020097{
David Sedlákc5b20842019-08-13 10:18:31 +020098 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +020099 size_t already_read = 0;
David Sedlák7ff55a92019-06-17 11:11:41 +0200100 LY_CHECK_RET(len == 0, YIN_ARG_NONE);
David Sedlák3b4db242018-10-19 16:11:01 +0200101
David Sedlák94de2aa2019-02-15 12:42:11 +0100102#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;arg=STMT;}
103#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {already_read+=LEN;
David Sedlákc10e7902018-12-17 02:17:59 +0100104#define IF_ARG_PREFIX_END }
105
David Sedlák1c8b2702019-02-22 11:03:02 +0100106 switch (*name) {
David Sedlák94de2aa2019-02-15 12:42:11 +0100107 case 'c':
108 already_read += 1;
109 IF_ARG("ondition", 8, YIN_ARG_CONDITION);
David Sedlák3b4db242018-10-19 16:11:01 +0200110 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200111
David Sedlák94de2aa2019-02-15 12:42:11 +0100112 case 'd':
113 already_read += 1;
114 IF_ARG("ate", 3, YIN_ARG_DATE);
David Sedlák3b4db242018-10-19 16:11:01 +0200115 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200116
David Sedlák94de2aa2019-02-15 12:42:11 +0100117 case 'm':
118 already_read += 1;
119 IF_ARG("odule", 5, YIN_ARG_MODULE);
David Sedlák872c7b42018-10-26 13:15:20 +0200120 break;
121
David Sedlák94de2aa2019-02-15 12:42:11 +0100122 case 'n':
123 already_read += 1;
124 IF_ARG("ame", 3, YIN_ARG_NAME);
David Sedlák872c7b42018-10-26 13:15:20 +0200125 break;
126
David Sedlák94de2aa2019-02-15 12:42:11 +0100127 case 't':
128 already_read += 1;
129 IF_ARG_PREFIX("a", 1)
130 IF_ARG("g", 1, YIN_ARG_TAG)
131 else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
132 IF_ARG_PREFIX_END
133 else IF_ARG("ext", 3, YIN_ARG_TEXT)
David Sedlák3b4db242018-10-19 16:11:01 +0200134 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200135
David Sedlák94de2aa2019-02-15 12:42:11 +0100136 case 'u':
137 already_read += 1;
138 IF_ARG("ri", 2, YIN_ARG_URI)
David Sedlák3b4db242018-10-19 16:11:01 +0200139 break;
David Sedlák872c7b42018-10-26 13:15:20 +0200140
David Sedlák94de2aa2019-02-15 12:42:11 +0100141 case 'v':
142 already_read += 1;
143 IF_ARG("alue", 4, YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200144 break;
145 }
146
David Sedlákc10e7902018-12-17 02:17:59 +0100147 /* whole argument must be matched */
David Sedlák872c7b42018-10-26 13:15:20 +0200148 if (already_read != len) {
David Sedláka7406952019-04-05 10:33:07 +0200149 arg = YIN_ARG_UNKNOWN;
David Sedlák872c7b42018-10-26 13:15:20 +0200150 }
151
David Sedlák18730132019-03-15 15:51:34 +0100152#undef IF_ARG
153#undef IF_ARG_PREFIX
154#undef IF_ARG_PREFIX_END
155
David Sedlák872c7b42018-10-26 13:15:20 +0200156 return arg;
David Sedlák3b4db242018-10-19 16:11:01 +0200157}
158
David Sedlák4f03b932019-07-26 13:01:47 +0200159void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200160 (void)ctx; /* unused */
David Sedlákd2d676a2019-07-22 11:28:19 +0200161 if (record && record->dynamic_content) {
David Sedlák00250342019-06-21 14:19:39 +0200162 free(record->content);
163 }
164}
165
David Sedlák81497a32019-08-13 16:56:26 +0200166#define IS_NODE_ELEM(kw) (kw == YANG_ANYXML || kw == YANG_ANYDATA || kw == YANG_LEAF || kw == YANG_LEAF_LIST || \
167 kw == YANG_TYPEDEF || kw == YANG_USES || kw == YANG_LIST || kw == YANG_NOTIFICATION || \
168 kw == YANG_GROUPING || kw == YANG_CONTAINER || kw == YANG_CASE || kw == YANG_CHOICE || \
169 kw == YANG_ACTION || kw == YANG_RPC || kw == YANG_AUGMENT)
170
171#define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == YANG_ARGUMENT || kw == YANG_IMPORT || kw == YANG_INCLUDE || kw == YANG_INPUT || kw == YANG_OUTPUT)
172
David Sedlák26ea1432019-08-14 13:42:23 +0200173/**
174 * @brief Free subelems information allocated on heap.
175 *
176 * @param[in] count Size of subelems array.
177 * @param[in] subelems Subelems array to free.
178 */
David Sedlák81497a32019-08-13 16:56:26 +0200179static void
180subelems_deallocator(size_t count, struct yin_subelement *subelems)
181{
182 for(size_t i = 0; i < count; ++i) {
183 if (HAS_META(subelems[i].type)) {
184 free(subelems[i].dest);
185 }
186 }
187
188 free(subelems);
189}
190
David Sedlák26ea1432019-08-14 13:42:23 +0200191/**
192 * @brief Allocate subelems information on heap.
193 *
194 * @param[in] ctx Yin parser context, used for logging.
195 * @param[in] count Number of subelements.
196 * @param[in] parent Parent node if any.
197 * @param[out] result Allocated subelems array.
198 *
199 * @return LY_SUCCESS on success LY_EMEM on memmory allocation failure.
200 */
David Sedlák81497a32019-08-13 16:56:26 +0200201static LY_ERR
202subelems_allocator(struct yin_parser_ctx *ctx, size_t count, struct lysp_node *parent,
203 struct yin_subelement **result, ...)
204{
205 va_list ap;
206
207 *result = calloc(count, sizeof **result);
208 LY_CHECK_GOTO(!(*result), MEM_ERR);
209
210 va_start(ap, result);
211 for (size_t i = 0; i < count; ++i) {
212 /* TYPE */
213 (*result)[i].type = va_arg(ap, enum yang_keyword);
214 /* DEST */
215 if (IS_NODE_ELEM((*result)[i].type)) {
216 struct tree_node_meta *node_meta = NULL;
217 node_meta = calloc(1, sizeof *node_meta);
218 LY_CHECK_GOTO(!node_meta, MEM_ERR);
219 node_meta->parent = parent;
220 node_meta->siblings = va_arg(ap, void *);
221 (*result)[i].dest = node_meta;
222 } else if ((*result)[i].type == YANG_ARGUMENT) {
223 struct yin_argument_meta *arg_meta = NULL;
224 arg_meta = calloc(1, sizeof *arg_meta);
225 LY_CHECK_GOTO(!arg_meta, MEM_ERR);
226 arg_meta->argument = va_arg(ap, const char **);
227 arg_meta->flags = va_arg(ap, uint16_t *);
228 (*result)[i].dest = arg_meta;
229 } else if ((*result)[i].type == YANG_IMPORT) {
230 struct import_meta *imp_meta = NULL;
231 imp_meta = calloc(1, sizeof *imp_meta);
232 LY_CHECK_GOTO(!imp_meta, MEM_ERR);
233 imp_meta->prefix = va_arg(ap, const char *);
234 imp_meta->imports = va_arg(ap, struct lysp_import **);
235 (*result)[i].dest = imp_meta;
236 } else if ((*result)[i].type == YANG_INCLUDE) {
237 struct include_meta *inc_meta = NULL;
238 inc_meta = calloc(1, sizeof *inc_meta);
239 LY_CHECK_GOTO(!inc_meta, MEM_ERR);
240 inc_meta->name = va_arg(ap, const char *);
241 inc_meta->includes = va_arg(ap, struct lysp_include **);
242 (*result)[i].dest = inc_meta;
243 } else if ((*result)[i].type == YANG_INPUT || (*result)[i].type == YANG_OUTPUT) {
244 struct inout_meta *inout_meta = NULL;
245 inout_meta = calloc(1, sizeof *inout_meta);
246 LY_CHECK_GOTO(!inout_meta, MEM_ERR);
247 inout_meta->parent = parent;
248 inout_meta->inout_p = va_arg(ap, struct lysp_action_inout *);
249 (*result)[i].dest = inout_meta;
250 } else {
251 (*result)[i].dest = va_arg(ap, void *);
252 }
253 /* FLAGS */
254 (*result)[i].flags = va_arg(ap, int);
255 }
256 va_end(ap);
257
258 return LY_SUCCESS;
259
260MEM_ERR:
261 subelems_deallocator(count, *result);
262 LOGMEM(ctx->xml_ctx.ctx);
263 return LY_EMEM;
264}
265
David Sedlák8f7a1172019-06-20 14:42:18 +0200266LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200267yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs)
David Sedláka7406952019-04-05 10:33:07 +0200268{
269 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200270 struct yin_arg_record *argument_record = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +0200271 const char *prefix, *name;
272 size_t prefix_len, name_len;
David Sedláka7406952019-04-05 10:33:07 +0200273
David Sedlák555c7202019-07-04 12:14:12 +0200274 /* load all attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +0200275 while (ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákc5b20842019-08-13 10:18:31 +0200276 ret = lyxml_get_attribute(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlák26ea1432019-08-14 13:42:23 +0200277 LY_CHECK_GOTO(ret, cleanup);
David Sedláka7406952019-04-05 10:33:07 +0200278
David Sedlákda8ffa32019-07-08 14:17:10 +0200279 if (ctx->xml_ctx.status == LYXML_ATTR_CONTENT) {
280 LY_ARRAY_NEW_GOTO(ctx->xml_ctx.ctx, *attrs, argument_record, ret, cleanup);
David Sedlákc5b20842019-08-13 10:18:31 +0200281 argument_record->name = name;
282 argument_record->name_len = name_len;
283 argument_record->prefix = prefix;
284 argument_record->prefix_len = prefix_len;
David Sedlákda8ffa32019-07-08 14:17:10 +0200285 ret = lyxml_get_string(&ctx->xml_ctx, data, &argument_record->content, &argument_record->content_len,
David Sedlák57715b12019-06-17 13:05:22 +0200286 &argument_record->content, &argument_record->content_len, &argument_record->dynamic_content);
David Sedlák26ea1432019-08-14 13:42:23 +0200287 LY_CHECK_GOTO(ret, cleanup);
David Sedlák7ff55a92019-06-17 11:11:41 +0200288 }
289 }
290
David Sedlák8f7a1172019-06-20 14:42:18 +0200291cleanup:
292 if (ret != LY_SUCCESS) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200293 FREE_ARRAY(ctx, *attrs, free_arg_rec);
David Sedlákb4e44562019-07-04 15:42:12 +0200294 *attrs = NULL;
David Sedlák8f7a1172019-06-20 14:42:18 +0200295 }
296 return ret;
297}
298
David Sedlák4a650532019-07-10 11:55:18 +0200299LY_ERR
300yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len)
301{
302 int prefix = 0;
303 unsigned int c;
304 size_t utf8_char_len;
305 size_t already_read = 0;
306 while (already_read < len) {
307 LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
308 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
309 already_read += utf8_char_len;
310 LY_CHECK_ERR_RET(already_read > len, LOGINT(ctx->xml_ctx.ctx), LY_EINT);
311
312 switch (val_type) {
313 case Y_IDENTIF_ARG:
314 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, NULL));
315 break;
316 case Y_PREF_IDENTIF_ARG:
317 LY_CHECK_RET(lysp_check_identifierchar((struct lys_parser_ctx *)ctx, c, !already_read, &prefix));
318 break;
319 case Y_STR_ARG:
320 case Y_MAYBE_STR_ARG:
321 LY_CHECK_RET(lysp_check_stringchar((struct lys_parser_ctx *)ctx, c));
322 break;
323 }
324 }
325
326 return LY_SUCCESS;
327}
328
David Sedlákb4e44562019-07-04 15:42:12 +0200329/**
330 * @brief Parse yin argument.
331 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200332 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200333 * @param[in] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlákb4e44562019-07-04 15:42:12 +0200334 * @param[in,out] data Data to read from.
David Sedlák4a650532019-07-10 11:55:18 +0200335 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
336 * special argument).
David Sedlákb4e44562019-07-04 15:42:12 +0200337 * @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 +0200338 * @param[in] val_type Type of expected value of attribute.
David Sedlákb4e44562019-07-04 15:42:12 +0200339 * @param[in] current_element Identification of current element, used for logging.
340 *
341 * @return LY_ERR values.
342 */
343static LY_ERR
David Sedlákc5b20842019-08-13 10:18:31 +0200344yin_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 +0200345 const char **arg_val, enum yang_arg val_type, enum yang_keyword current_element)
David Sedlák8f7a1172019-06-20 14:42:18 +0200346{
David Sedlákc5b20842019-08-13 10:18:31 +0200347 enum yin_argument arg = YIN_ARG_UNKNOWN;
David Sedlák8f7a1172019-06-20 14:42:18 +0200348 struct yin_arg_record *iter = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200349 bool found = false;
David Sedlák8f7a1172019-06-20 14:42:18 +0200350
David Sedlák1bccdfa2019-06-17 15:55:27 +0200351 /* validation of attributes */
David Sedlák1f90d252019-07-10 17:09:32 +0200352 LY_ARRAY_FOR(attrs, struct yin_arg_record, iter) {
David Sedlák00250342019-06-21 14:19:39 +0200353 /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
354 if (!iter->prefix) {
David Sedlák060b00e2019-06-19 11:12:06 +0200355 arg = yin_match_argument_name(iter->name, iter->name_len);
David Sedlák7ff55a92019-06-17 11:11:41 +0200356 if (arg == YIN_ARG_NONE) {
David Sedlák2b214ac2019-06-06 16:11:03 +0200357 continue;
David Sedlák7ff55a92019-06-17 11:11:41 +0200358 } else if (arg == arg_type) {
David Sedlák1538a842019-08-08 15:38:51 +0200359 LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_DUP_ATTR,
David Sedlák292763b2019-07-09 11:10:53 +0200360 yin_attr2str(arg), ly_stmt2str(current_element)), LY_EVALID);
David Sedlák619db942019-07-03 14:47:30 +0200361 found = true;
David Sedlák4a650532019-07-10 11:55:18 +0200362 LY_CHECK_RET(yin_validate_value(ctx, val_type, iter->content, iter->content_len));
David Sedlák57715b12019-06-17 13:05:22 +0200363 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +0200364 *arg_val = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
David Sedlák619db942019-07-03 14:47:30 +0200365 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák00250342019-06-21 14:19:39 +0200366 /* string is no longer supposed to be freed when the sized array is freed */
367 iter->dynamic_content = 0;
David Sedlák57715b12019-06-17 13:05:22 +0200368 } else {
David Sedlák99295322019-07-17 11:34:18 +0200369 if (iter->content_len == 0) {
370 *arg_val = lydict_insert(ctx->xml_ctx.ctx, "", 0);
371 } else {
372 *arg_val = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
David Sedlák99295322019-07-17 11:34:18 +0200373 }
David Sedlák26ea1432019-08-14 13:42:23 +0200374 LY_CHECK_RET(!(*arg_val), LY_EMEM);
David Sedlák57715b12019-06-17 13:05:22 +0200375 }
David Sedlák2b214ac2019-06-06 16:11:03 +0200376 } else {
David Sedlák1538a842019-08-08 15:38:51 +0200377 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_ATTR, iter->name_len, iter->name, ly_stmt2str(current_element));
David Sedlák619db942019-07-03 14:47:30 +0200378 return LY_EVALID;
David Sedláka7406952019-04-05 10:33:07 +0200379 }
380 }
381 }
382
David Sedlák292763b2019-07-09 11:10:53 +0200383 /* anything else than Y_MAYBE_STR_ARG is mandatory */
384 if (val_type != Y_MAYBE_STR_ARG && !found) {
David Sedlák9c40a922019-07-08 17:04:43 +0200385 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 +0200386 return LY_EVALID;
387 }
388
389 return LY_SUCCESS;
David Sedláka7406952019-04-05 10:33:07 +0200390}
391
David Sedlákd6e56892019-07-01 15:40:24 +0200392/**
David Sedlákda8ffa32019-07-08 14:17:10 +0200393 * @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 +0200394 *
395 * @param[in] type Type of wanted record.
396 * @param[in] array_size Size of array.
397 * @param[in] array Searched array.
398 *
399 * @return Pointer to desired record on success, NULL if element is not in the array.
400 */
David Sedlákb4e44562019-07-04 15:42:12 +0200401static struct yin_subelement *
David Sedlákb0faad82019-07-04 14:28:59 +0200402get_record(enum yang_keyword type, signed char array_size, struct yin_subelement *array)
David Sedlákd6e56892019-07-01 15:40:24 +0200403{
David Sedlákb0faad82019-07-04 14:28:59 +0200404 signed char left = 0, right = array_size - 1, middle;
405
406 while (left <= right) {
407 middle = left + (right - left) / 2;
408
409 if (array[middle].type == type) {
410 return &array[middle];
411 }
412
413 if (array[middle].type < type) {
414 left = middle + 1;
415 } else {
416 right = middle - 1;
David Sedlákd6e56892019-07-01 15:40:24 +0200417 }
418 }
419
420 return NULL;
421}
422
David Sedlákbba38e52019-07-09 15:20:01 +0200423/**
424 * @brief Helper function to check mandatory constraint of subelement.
425 *
426 * @param[in,out] ctx Yin parser context for logging and to store current state.
427 * @param[in] subelem_info Array of information about subelements.
428 * @param[in] subelem_info_size Size of subelem_info array.
429 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
430 *
431 * @return LY_ERR values.
432 */
433static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200434yin_check_subelem_mandatory_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedlákb0faad82019-07-04 14:28:59 +0200435 signed char subelem_info_size, enum yang_keyword current_element)
David Sedlák21f87cd2019-07-03 16:53:23 +0200436{
David Sedlákb0faad82019-07-04 14:28:59 +0200437 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák5545f5d2019-07-11 11:55:16 +0200438 /* 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 +0200439 if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
David Sedlák1538a842019-08-08 15:38:51 +0200440 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
David Sedlák555c7202019-07-04 12:14:12 +0200441 ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200442 return LY_EVALID;
443 }
444 }
445
446 return LY_SUCCESS;
447}
448
David Sedlákbba38e52019-07-09 15:20:01 +0200449/**
450 * @brief Helper function to check "first" constraint of subelement.
451 *
452 * @param[in,out] ctx Yin parser context for logging and to store current state.
453 * @param[in] subelem_info Array of information about subelements.
454 * @param[in] subelem_info_size Size of subelem_info array.
455 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
456 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement.
457 *
458 * @return LY_ERR values.
459 */
460static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200461yin_check_subelem_first_constraint(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info,
David Sedláke1a30302019-07-10 13:49:38 +0200462 signed char subelem_info_size, enum yang_keyword current_element,
463 struct yin_subelement *exp_first)
David Sedlák21f87cd2019-07-03 16:53:23 +0200464{
David Sedlákb0faad82019-07-04 14:28:59 +0200465 for (signed char i = 0; i < subelem_info_size; ++i) {
David Sedlák21f87cd2019-07-03 16:53:23 +0200466 if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
David Sedlák1538a842019-08-08 15:38:51 +0200467 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
468 ly_stmt2str(exp_first->type), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +0200469 return LY_EVALID;
470 }
471 }
472
473 return LY_SUCCESS;
474}
475
David Sedlákbba38e52019-07-09 15:20:01 +0200476/**
477 * @brief Helper function to check if array of information about subelements is in ascending order.
478 *
479 * @param[in] subelem_info Array of information about subelements.
480 * @param[in] subelem_info_size Size of subelem_info array.
481 *
482 * @return True iff subelem_info array is in ascending order, False otherwise.
483 */
David Sedlák5545f5d2019-07-11 11:55:16 +0200484#ifndef NDEBUG
David Sedlákbba38e52019-07-09 15:20:01 +0200485static bool
David Sedlákb0faad82019-07-04 14:28:59 +0200486is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
487{
David Sedlák292763b2019-07-09 11:10:53 +0200488 enum yang_keyword current = YANG_NONE; /* 0 (minimal value) */
David Sedlákb0faad82019-07-04 14:28:59 +0200489
490 for (signed char i = 0; i < subelem_info_size; ++i) {
491 if (subelem_info[i].type <= current) {
492 return false;
493 }
494 current = subelem_info[i].type;
495 }
496
497 return true;
498}
David Sedlák5545f5d2019-07-11 11:55:16 +0200499#endif
David Sedlákb0faad82019-07-04 14:28:59 +0200500
David Sedlákd6e56892019-07-01 15:40:24 +0200501/**
David Sedlákb4e44562019-07-04 15:42:12 +0200502 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
503 * for example prefix or namespace element.
David Sedlákd6e56892019-07-01 15:40:24 +0200504 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200505 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200506 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákd6e56892019-07-01 15:40:24 +0200507 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákb4e44562019-07-04 15:42:12 +0200508 * @param[in] kw Type of current element.
509 * @param[out] value Where value of attribute should be stored.
510 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200511 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlákda8ffa32019-07-08 14:17:10 +0200512 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +0200513 *
David Sedlákd6e56892019-07-01 15:40:24 +0200514 * @return LY_ERR values.
515 */
David Sedlákb4e44562019-07-04 15:42:12 +0200516static LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +0200517yin_parse_simple_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlákc5b20842019-08-13 10:18:31 +0200518 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 +0200519{
David Sedlák1f90d252019-07-10 17:09:32 +0200520 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák968ac342019-07-11 15:17:59 +0200521 struct yin_subelement subelems[1] = {
522 {YANG_CUSTOM, NULL, 0}
523 };
David Sedlákb4e44562019-07-04 15:42:12 +0200524
David Sedlákda8ffa32019-07-08 14:17:10 +0200525 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +0200526}
527
528/**
David Sedlák6542aed2019-08-14 10:47:43 +0200529 * @brief Parse path element.
530 *
531 * @param[in,out] ctx Yin parser context for logging and to store current state.
532 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
533 * @param[in,out] data Data to read from, always moved to currently handled character.
534 * @param[in] kw Type of current element.
535 * @param[out] type Type structure to store parsed value, flags and extension instances.
536 *
537 * @return LY_ERR values.
538 */
539static LY_ERR
540yin_parse_path(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
541 struct lysp_type *type)
542{
543 LY_CHECK_RET(yin_parse_simple_element(ctx, attrs, data, kw, &type->path,
544 YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
545 type->flags |= LYS_SET_PATH;
546
547 return LY_SUCCESS;
548}
549
550/**
David Sedlákd3983112019-07-12 11:20:56 +0200551 * @brief Parse pattern element.
552 *
553 * @param[in,out] ctx Yin parser context for logging and to store current state.
554 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
555 * @param[in,out] data Data to read from, always moved to currently handled character.
556 * @param[in,out] patterns Restrictions to add to.
557 *
558 * @return LY_ERR values.
559 */
560static LY_ERR
561yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
562 struct lysp_type *type)
563{
564 const char *real_value = NULL;
565 char *saved_value = NULL;
566 struct lysp_restr *restr;
567
568 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
569 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
570 size_t len = strlen(real_value);
571
572 saved_value = malloc(len + 2);
573 LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
574 memmove(saved_value + 1, real_value, len);
575 FREE_STRING(ctx->xml_ctx.ctx, real_value);
576 saved_value[0] = 0x06;
577 saved_value[len + 1] = '\0';
578 restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
579 LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
580 type->flags |= LYS_SET_PATTERN;
581
582 struct yin_subelement subelems[6] = {
583 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
584 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
585 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
586 {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
587 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
588 {YANG_CUSTOM, NULL, 0}
589 };
590 return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
591}
592
David Sedlákc5b20842019-08-13 10:18:31 +0200593/**
594 * @brief Parse fraction-digits element.
595 *
596 * @param[in,out] ctx Yin parser context for logging and to store current state.
597 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
598 * @param[in,out] data Data to read from, always moved to currently handled character.
599 * @param[in,out] type Type structure to store value, flags and extension instances.
600 *
601 * @return LY_ERR values.
602 */
David Sedlákf75d55e2019-07-12 16:52:50 +0200603static LY_ERR
604yin_parse_fracdigits(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
605 struct lysp_type *type)
606{
607 const char *temp_val = NULL;
608 char *ptr;
609 unsigned long int num;
610
611 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_FRACTION_DIGITS));
612
613 if (temp_val[0] == '\0' || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
David Sedlák1538a842019-08-08 15:38:51 +0200614 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200615 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
616 return LY_EVALID;
617 }
618
619 errno = 0;
620 num = strtoul(temp_val, &ptr, 10);
621 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +0200622 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200623 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
624 return LY_EVALID;
625 }
626 if ((errno == ERANGE) || (num > 18)) {
David Sedlák1538a842019-08-08 15:38:51 +0200627 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
David Sedlákf75d55e2019-07-12 16:52:50 +0200628 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
629 return LY_EVALID;
630 }
David Sedlák2ab5d8e2019-07-16 11:19:41 +0200631 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedlákf75d55e2019-07-12 16:52:50 +0200632 type->fraction_digits = num;
633 type->flags |= LYS_SET_FRDIGITS;
634 struct yin_subelement subelems[1] = {
David Sedlákd1144562019-08-06 12:36:14 +0200635 {YANG_CUSTOM, NULL, 0}
David Sedlákf75d55e2019-07-12 16:52:50 +0200636 };
637 return yin_parse_content(ctx, subelems, 1, data, YANG_FRACTION_DIGITS, NULL, &type->exts);
638}
639
David Sedlák07869a52019-07-12 14:28:19 +0200640/**
David Sedlák43801c92019-08-05 15:58:54 +0200641 * @brief Parse enum element.
David Sedlák07869a52019-07-12 14:28:19 +0200642 *
643 * @param[in,out] ctx YIN parser context for logging and to store current state.
644 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
645 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc5b20842019-08-13 10:18:31 +0200646 * @param[in,out] type Type structure to store enum value, flags and extension instances.
David Sedlák07869a52019-07-12 14:28:19 +0200647 *
648 * @return LY_ERR values.
649 */
David Sedlákca36c422019-07-12 12:47:55 +0200650static LY_ERR
David Sedlák43801c92019-08-05 15:58:54 +0200651yin_parse_enum(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_type *type)
David Sedlákca36c422019-07-12 12:47:55 +0200652{
David Sedlák07869a52019-07-12 14:28:19 +0200653 struct lysp_type_enum *en;
David Sedlák1e696782019-07-17 15:06:07 +0200654
David Sedlák43801c92019-08-05 15:58:54 +0200655 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->enums, en, LY_EMEM);
656 type->flags |= LYS_SET_ENUM;
657 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, YANG_ENUM));
658 LY_CHECK_RET(lysp_check_enum_name((struct lys_parser_ctx *)ctx, en->name, strlen(en->name)));
659 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(en->name), "enum");
660 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "enum", en->name);
David Sedlákca36c422019-07-12 12:47:55 +0200661
662 struct yin_subelement subelems[6] = {
David Sedlák07869a52019-07-12 14:28:19 +0200663 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
664 {YANG_IF_FEATURE, &en->iffeatures, 0},
David Sedlák07869a52019-07-12 14:28:19 +0200665 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
666 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
David Sedlák43801c92019-08-05 15:58:54 +0200667 {YANG_VALUE, en, YIN_SUBELEM_UNIQUE},
David Sedlákca36c422019-07-12 12:47:55 +0200668 {YANG_CUSTOM, NULL, 0}
669 };
David Sedlák43801c92019-08-05 15:58:54 +0200670 return yin_parse_content(ctx, subelems, 6, data, YANG_ENUM, NULL, &en->exts);
671}
672
673/**
674 * @brief Parse bit element.
675 *
676 * @param[in,out] ctx YIN parser context for logging and to store current state.
677 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
678 * @param[in,out] data Data to read from, always moved to currently handled character.
679 * @param[in] enum_kw Identification of actual keyword, can be set to YANG_BIT or YANG_ENUM.
David Sedlákc5b20842019-08-13 10:18:31 +0200680 * @param[in,out] type Type structure to store bit value, flags and extension instances.
David Sedlák43801c92019-08-05 15:58:54 +0200681 *
682 * @return LY_ERR values.
683 */
684static LY_ERR
685yin_parse_bit(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
686 struct lysp_type *type)
687{
688 struct lysp_type_enum *en;
689
690 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->bits, en, LY_EMEM);
691 type->flags |= LYS_SET_BIT;
692 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, YANG_BIT));
693 CHECK_UNIQUENESS((struct lys_parser_ctx *)ctx, type->enums, name, "bit", en->name);
694
695 struct yin_subelement subelems[6] = {
696 {YANG_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
697 {YANG_IF_FEATURE, &en->iffeatures, 0},
698 {YANG_POSITION, en, YIN_SUBELEM_UNIQUE},
699 {YANG_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
700 {YANG_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
701 {YANG_CUSTOM, NULL, 0}
702 };
703 return yin_parse_content(ctx, subelems, 6, data, YANG_BIT, NULL, &en->exts);
David Sedlákca36c422019-07-12 12:47:55 +0200704}
705
David Sedlákd3983112019-07-12 11:20:56 +0200706/**
David Sedlák5f8191e2019-07-08 16:35:52 +0200707 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
708 * more instances, such as base or if-feature.
709 *
710 * @param[in,out] ctx YIN parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200711 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlák5f8191e2019-07-08 16:35:52 +0200712 * @param[in,out] data Data to read from, always moved to currently handled character.
713 * @param[in] kw Type of current element.
714 * @param[out] values Parsed values to add to.
715 * @param[in] arg_type Expected type of attribute.
David Sedlák292763b2019-07-09 11:10:53 +0200716 * @param[in] arg_val_type Type of expected value of attribute.
David Sedlák5f8191e2019-07-08 16:35:52 +0200717 * @param[in,out] exts Extension instance to add to.
718 *
719 * @return LY_ERR values.
720 */
721static LY_ERR
722yin_parse_simple_elements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
David Sedlákc5b20842019-08-13 10:18:31 +0200723 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 +0200724{
725 const char **value;
David Sedlák5f8191e2019-07-08 16:35:52 +0200726 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *values, value, LY_EMEM);
David Sedlákcb5d83f2019-07-09 09:32:53 +0200727 uint32_t index = LY_ARRAY_SIZE(*values) - 1;
David Sedlák968ac342019-07-11 15:17:59 +0200728 struct yin_subelement subelems[1] = {
729 {YANG_CUSTOM, &index, 0}
730 };
731
David Sedlák1f90d252019-07-10 17:09:32 +0200732 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, value, arg_val_type, kw));
David Sedlák5f8191e2019-07-08 16:35:52 +0200733
734 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, exts);
735}
736
737/**
David Sedlák6542aed2019-08-14 10:47:43 +0200738 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
739 *
740 * @param[in,out] ctx YIN parser context for logging and to store current state.
741 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
742 * @param[in,out] data Data to read from, always moved to currently handled character.
743 * @param[in] kw Type of current element.
744 * @param[out] values Parsed values to add to.
745 * @param[in] arg_type Expected type of attribute.
746 * @param[in] arg_val_type Type of expected value of attribute.
747 * @param[in,out] exts Extension instance to add to.
748 *
749 * @return LY_ERR values.
750 */
751static LY_ERR
752yin_parse_simple_elem(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword kw,
753 struct yin_subelement *subinfo, enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
754{
755 if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
756 LY_CHECK_RET(yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subinfo->dest,
757 arg_type, arg_val_type, exts));
758 } else {
759 LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, kw, (const char ***)subinfo->dest,
760 arg_type, arg_val_type, exts));
761 }
762
763 return LY_SUCCESS;
764}
765
766/**
767 * @brief Parse base element.
768 *
769 * @param[in,out] ctx YIN parser context for logging and to store current state.
770 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
771 * @param[in,out] data Data to read from, always moved to currently handled character.
772 * @param[in] parent Identification of parent element.
773 * @param[out] dest Where parsed values should be stored.
774 * @param[in,out] exts Extension instance to add to.
775 *
776 * @return LY_ERR values.
777 */
778static LY_ERR
779yin_parse_base(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword parent,
780 void *dest, struct lysp_ext_instance **exts)
781{
782 struct lysp_type *type = NULL;
783
784 if (parent == YANG_TYPE) {
785 type = (struct lysp_type *)dest;
786 LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, YANG_BASE, &type->bases, YIN_ARG_NAME,
787 Y_PREF_IDENTIF_ARG, exts));
788 type->flags |= LYS_SET_BASE;
789 } else if (parent == YANG_IDENTITY) {
790 LY_CHECK_RET(yin_parse_simple_elements(ctx, attrs, data, YANG_BASE, (const char ***)dest,
791 YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
792 } else {
793 LOGINT(ctx->xml_ctx.ctx);
794 return LY_EINT;
795 }
796
797 return LY_SUCCESS;
798}
799
800/**
David Sedlákcf5569a2019-07-11 13:31:34 +0200801 * @brief Parse require instance element.
802 *
803 * @param[in,out] ctx Yin parser context for logging and to store current state.
804 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
805 * @param[in,out] data Data to read from, always moved to currently handled character.
806 * @prama[out] type Type structure to store value, flag and extensions.
807 *
808 * @return LY_ERR values.
809 */
810static LY_ERR
811yin_pasrse_reqinstance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
812 const char **data, struct lysp_type *type)
813{
814 const char *temp_val = NULL;
David Sedlák968ac342019-07-11 15:17:59 +0200815 struct yin_subelement subelems[1] = {
816 {YANG_CUSTOM, NULL, 0}
817 };
David Sedlákcf5569a2019-07-11 13:31:34 +0200818
819 type->flags |= LYS_SET_REQINST;
820 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_REQUIRE_INSTANCE));
821 if (strcmp(temp_val, "true") == 0) {
822 type->require_instance = 1;
823 } else if (strcmp(temp_val, "false") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200824 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
825 "require-instance", "true", "false");
David Sedlákcf5569a2019-07-11 13:31:34 +0200826 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
827 return LY_EVALID;
828 }
829 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
830
831 return yin_parse_content(ctx, subelems, 1, data, YANG_REQUIRE_INSTANCE, NULL, &type->exts);
832}
833
834/**
David Sedlákce77bf52019-07-11 16:59:31 +0200835 * @brief Parse modifier element.
836 *
837 * @param[in,out] ctx Yin parser context for logging and to store current state.
838 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
839 * @param[in,out] data Data to read from, always moved to currently handled character.
840 * @param[in,out] pat Value to write to.
841 * @param[in,out] exts Extension instances to add to.
842 *
843 * @return LY_ERR values.
844 */
845static LY_ERR
846yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
847 const char **pat, struct lysp_ext_instance **exts)
848{
David Sedlákd3983112019-07-12 11:20:56 +0200849 assert(**pat == 0x06);
David Sedlákce77bf52019-07-11 16:59:31 +0200850 const char *temp_val;
851 char *modified_val;
852 struct yin_subelement subelems[1] = {
853 {YANG_CUSTOM, NULL, 0}
854 };
855
856 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MODIFIER));
857 if (strcmp(temp_val, "invert-match") != 0) {
David Sedlák26ea1432019-08-14 13:42:23 +0200858 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
859 "modifier", "invert-match");
David Sedlákce77bf52019-07-11 16:59:31 +0200860 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
861 return LY_EVALID;
862 }
David Sedlákd3983112019-07-12 11:20:56 +0200863 lydict_remove(ctx->xml_ctx.ctx, temp_val);
David Sedlákce77bf52019-07-11 16:59:31 +0200864
865 /* allocate new value */
David Sedlákd3983112019-07-12 11:20:56 +0200866 modified_val = malloc(strlen(*pat) + 1);
David Sedlákce77bf52019-07-11 16:59:31 +0200867 LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákd3983112019-07-12 11:20:56 +0200868 strcpy(modified_val, *pat);
869 lydict_remove(ctx->xml_ctx.ctx, *pat);
David Sedlákce77bf52019-07-11 16:59:31 +0200870
871 /* modify the new value */
872 modified_val[0] = 0x15;
873 *pat = lydict_insert_zc(ctx->xml_ctx.ctx, modified_val);
874
875 return yin_parse_content(ctx, subelems, 1, data, YANG_MODIFIER, NULL, exts);
876}
877
878/**
David Sedlákb7296dd2019-07-11 14:58:38 +0200879 * @brief Parse a restriction element (length, range or one instance of must).
880 *
881 * @param[in,out] ctx Yin parser context for logging and to store current state.
882 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
883 * @param[in,out] data Data to read from, always moved to currently handled character.
884 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to YANG_MUST, YANG_LENGTH or YANG_RANGE.
David Sedlák6542aed2019-08-14 10:47:43 +0200885 * @param[in] restr Value to write to.
David Sedlákb7296dd2019-07-11 14:58:38 +0200886 */
887static LY_ERR
888yin_parse_restriction(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
889 enum yang_keyword restr_kw, struct lysp_restr *restr)
890{
891 assert(restr_kw == YANG_MUST || restr_kw == YANG_LENGTH || restr_kw == YANG_RANGE);
892 struct yin_subelement subelems[5] = {
David Sedlák968ac342019-07-11 15:17:59 +0200893 {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
894 {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
895 {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
896 {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
897 {YANG_CUSTOM, NULL, 0}
898 };
David Sedlákb7296dd2019-07-11 14:58:38 +0200899 /* argument of must is called condition, but argument of length and range is called value */
David Sedlákc5b20842019-08-13 10:18:31 +0200900 enum yin_argument arg_type = (restr_kw == YANG_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
David Sedlákb7296dd2019-07-11 14:58:38 +0200901 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, arg_type, &restr->arg, Y_STR_ARG, restr_kw));
902
903 return yin_parse_content(ctx, subelems, 5, data, restr_kw, NULL, &restr->exts);
904}
905
906/**
David Sedlák6542aed2019-08-14 10:47:43 +0200907 * @brief Parse range element.
908 *
909 * @param[in,out] ctx Yin parser context for logging and to store current state.
910 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
911 * @param[in,out] data Data to read from, always moved to currently handled character.
912 * @param[out] type Type structure to store parsed value and flags.
913 *
914 * @return LY_ERR values.
915 */
916static LY_ERR
917yin_parse_range(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
918 const char **data, struct lysp_type *type)
919{
920 type->range = calloc(1, sizeof *type->range);
921 LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
922 LY_CHECK_RET(yin_parse_restriction(ctx, attrs, data, YANG_RANGE, type->range));
923 type->flags |= LYS_SET_RANGE;
924
925 return LY_SUCCESS;
926}
927
928/**
929 * @brief Parse length element.
930 *
931 * @param[in,out] ctx Yin parser context for logging and to store current state.
932 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
933 * @param[in,out] data Data to read from, always moved to currently handled character.
934 * @param[out] type Type structure to store parsed value and flags.
935 *
936 * @return LY_ERR values.
937 */
938static LY_ERR
939yin_parse_length(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs,
940 const char **data, struct lysp_type *type)
941{
942 type->length = calloc(1, sizeof *type->length);
943 LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
944 LY_CHECK_RET(yin_parse_restriction(ctx, attrs, data, YANG_LENGTH, type->length));
945 type->flags |= LYS_SET_LENGTH;
946
947 return LY_SUCCESS;
948}
949
950/**
David Sedlákbc9ec9c2019-07-11 15:53:55 +0200951 * @brief Parse must element.
952 *
953 * @param[in,out] ctx YIN parser context for logging and to store current state.
954 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
955 * @param[in,out] data Data to read from, always moved to currently handled character.
956 * @param[in,out] restrs Restrictions to add to.
957 *
958 * @return LY_ERR values.
959 */
960static LY_ERR
961yin_parse_must(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_restr **restrs)
962{
963 struct lysp_restr *restr;
964
965 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *restrs, restr, LY_EMEM);
966 return yin_parse_restriction(ctx, attrs, data, YANG_MUST, restr);
967}
968
969/**
David Sedlák5545f5d2019-07-11 11:55:16 +0200970 * @brief Parse position or value element.
971 *
972 * @param[in,out] ctx YIN parser context for logging and to store current state.
973 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
974 * @param[in,out] data Data to read from, always moved to currently handled character.
975 * @param[in] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
976 * @param[out] enm Enum structure to save value, flags and extensions.
977 *
978 * @return LY_ERR values.
979 */
980static LY_ERR
981yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
982 enum yang_keyword kw, struct lysp_type_enum *enm)
983{
984 assert(kw == YANG_POSITION || kw == YANG_VALUE);
985 const char *temp_val = NULL;
986 char *ptr;
987 long int num;
988 unsigned long int unum;
989
990 /* set value flag */
991 enm->flags |= LYS_SET_VALUE;
992
993 /* get attribute value */
David Sedlákcf5569a2019-07-11 13:31:34 +0200994 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw));
David Sedlákae5378f2019-07-17 11:37:59 +0200995 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '+') ||
996 ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((kw == YANG_POSITION) && !strcmp(temp_val, "-0"))) {
David Sedlák1538a842019-08-08 15:38:51 +0200997 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +0200998 goto error;
999 }
1000
1001 /* convert value */
1002 errno = 0;
1003 if (kw == YANG_VALUE) {
1004 num = strtol(temp_val, &ptr, 10);
1005 if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
David Sedlák1538a842019-08-08 15:38:51 +02001006 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001007 goto error;
1008 }
1009 } else {
1010 unum = strtoul(temp_val, &ptr, 10);
1011 if (unum > UINT64_C(4294967295)) {
David Sedlák1538a842019-08-08 15:38:51 +02001012 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001013 goto error;
1014 }
1015 }
1016 /* check if whole argument value was converted */
1017 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001018 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlákebcd0eb2019-07-16 17:55:12 +02001019 goto error;
David Sedlák5545f5d2019-07-11 11:55:16 +02001020 }
1021 if (errno == ERANGE) {
David Sedlák1538a842019-08-08 15:38:51 +02001022 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", ly_stmt2str(kw));
David Sedlák5545f5d2019-07-11 11:55:16 +02001023 goto error;
1024 }
1025 /* save correctly ternary operator can't be used because num and unum have different signes */
1026 if (kw == YANG_VALUE) {
1027 enm->value = num;
1028 } else {
1029 enm->value = unum;
1030 }
1031 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1032
1033 /* parse subelements */
David Sedlák968ac342019-07-11 15:17:59 +02001034 struct yin_subelement subelems[1] = {
1035 {YANG_CUSTOM, NULL, 0}
1036 };
David Sedlák5545f5d2019-07-11 11:55:16 +02001037 return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
1038
1039 error:
1040 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1041 return LY_EVALID;
1042}
1043
David Sedlák05404f62019-07-24 14:11:53 +02001044
1045/**
1046 * @brief Parse belongs-to element.
1047 *
1048 * @param[in] ctx Yin parser context for logging and to store current state.
1049 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1050 * @param[in,out] data Data to read from, always moved to currently handled character.
1051 * @param[out] submod Structure of submodule that is being parsed.
1052 * @param[in,out] exts Extension instances to add to.
1053 *
1054 * @return LY_ERR values
1055 */
1056static LY_ERR
1057yin_parse_belongs_to(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1058 struct lysp_submodule *submod, struct lysp_ext_instance **exts)
1059{
1060 struct yin_subelement subelems[2] = {
1061 {YANG_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1062 {YANG_CUSTOM, NULL, 0}
1063 };
1064 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &submod->belongsto, Y_IDENTIF_ARG, YANG_BELONGS_TO));
1065
1066 return yin_parse_content(ctx, subelems, 2, data, YANG_BELONGS_TO, NULL, exts);
1067}
1068
David Sedlák5545f5d2019-07-11 11:55:16 +02001069/**
David Sedlákc1771b12019-07-10 15:55:46 +02001070 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
David Sedlákb4e44562019-07-04 15:42:12 +02001071 * text element as child
1072 *
David Sedlákda8ffa32019-07-08 14:17:10 +02001073 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákdf2a9732019-08-07 13:23:16 +02001074 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákcf5569a2019-07-11 13:31:34 +02001075 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákc1771b12019-07-10 15:55:46 +02001076 * @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 +02001077 * @param[out] value Where the content of meta element should be stored.
David Sedlákbba38e52019-07-09 15:20:01 +02001078 * @param[in,out] exts Extension instance to add to.
David Sedlákb4e44562019-07-04 15:42:12 +02001079 *
1080 * @return LY_ERR values.
1081 */
1082static LY_ERR
David Sedlákdf2a9732019-08-07 13:23:16 +02001083yin_parse_meta_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1084 enum yang_keyword elem_type, const char **value, struct lysp_ext_instance **exts)
David Sedlákb4e44562019-07-04 15:42:12 +02001085{
1086 assert(elem_type == YANG_ORGANIZATION || elem_type == YANG_CONTACT || elem_type == YANG_DESCRIPTION || elem_type == YANG_REFERENCE);
1087
David Sedlák968ac342019-07-11 15:17:59 +02001088 struct yin_subelement subelems[2] = {
1089 {YANG_CUSTOM, NULL, 0},
1090 {YIN_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1091 };
David Sedlákdf2a9732019-08-07 13:23:16 +02001092 /* check attributes */
1093 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, elem_type));
David Sedlákb4e44562019-07-04 15:42:12 +02001094
David Sedlákdf2a9732019-08-07 13:23:16 +02001095 /* parse content */
David Sedlákda8ffa32019-07-08 14:17:10 +02001096 return yin_parse_content(ctx, subelems, 2, data, elem_type, NULL, exts);
David Sedlákb4e44562019-07-04 15:42:12 +02001097}
1098
1099/**
David Sedlákc1771b12019-07-10 15:55:46 +02001100 * @brief Parse error-message element.
1101 *
1102 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákdf2a9732019-08-07 13:23:16 +02001103 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
David Sedlákc1771b12019-07-10 15:55:46 +02001104 * @param[in,out] data Data to read from.
1105 * @param[out] value Where the content of error-message element should be stored.
1106 * @param[in,out] exts Extension instance to add to.
1107 *
1108 * @return LY_ERR values.
1109 */
1110static LY_ERR
David Sedlákdf2a9732019-08-07 13:23:16 +02001111yin_parse_err_msg_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1112 const char **value, struct lysp_ext_instance **exts)
David Sedlákc1771b12019-07-10 15:55:46 +02001113{
David Sedlák968ac342019-07-11 15:17:59 +02001114 struct yin_subelement subelems[2] = {
1115 {YANG_CUSTOM, NULL, 0},
1116 {YIN_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1117 };
David Sedlákc1771b12019-07-10 15:55:46 +02001118
David Sedlákdf2a9732019-08-07 13:23:16 +02001119 /* check attributes */
1120 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, YANG_ERROR_MESSAGE));
1121
David Sedlákc1771b12019-07-10 15:55:46 +02001122 return yin_parse_content(ctx, subelems, 2, data, YANG_ERROR_MESSAGE, NULL, exts);
1123}
1124
1125/**
David Sedlák374d2b32019-07-17 15:06:55 +02001126 * @brief parse type element.
1127 *
David Sedlák6542aed2019-08-14 10:47:43 +02001128 * @brief Parse type element.
David Sedlák374d2b32019-07-17 15:06:55 +02001129 *
1130 * @param[in,out] ctx YIN parser context for logging and to store current state.
1131 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1132 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák6542aed2019-08-14 10:47:43 +02001133 * @param[in] parent Identification of parent element.
David Sedlák374d2b32019-07-17 15:06:55 +02001134 * @param[in,out] type Type to wrote to.
David Sedlák374d2b32019-07-17 15:06:55 +02001135 *
1136 * @return LY_ERR values.
1137 */
1138static LY_ERR
David Sedlák6542aed2019-08-14 10:47:43 +02001139yin_parse_type(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1140 enum yang_keyword parent, struct yin_subelement *subinfo)
David Sedlák374d2b32019-07-17 15:06:55 +02001141{
David Sedlák6542aed2019-08-14 10:47:43 +02001142 struct lysp_type *type = NULL;
1143 if (parent == YANG_DEVIATE) {
1144 *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
1145 LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1146 type = *((struct lysp_type **)subinfo->dest);
1147 } else {
1148 type = (struct lysp_type *)subinfo->dest;
1149 }
1150 /* type as child of another type */
1151 if (parent == YANG_TYPE) {
1152 struct lysp_type *nested_type = NULL;
1153 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->types, nested_type, LY_EMEM);
1154 type->flags |= LYS_SET_TYPE;
1155 type = nested_type;
1156 }
David Sedlák374d2b32019-07-17 15:06:55 +02001157 struct yin_subelement subelems[11] = {
1158 {YANG_BASE, type, 0},
1159 {YANG_BIT, type, 0},
1160 {YANG_ENUM, type, 0},
1161 {YANG_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1162 {YANG_LENGTH, type, YIN_SUBELEM_UNIQUE},
1163 {YANG_PATH, type, YIN_SUBELEM_UNIQUE},
1164 {YANG_PATTERN, type, 0},
1165 {YANG_RANGE, type, YIN_SUBELEM_UNIQUE},
1166 {YANG_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1167 {YANG_TYPE, type},
1168 {YANG_CUSTOM, NULL, 0},
1169 };
1170 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, YANG_TYPE));
1171 return yin_parse_content(ctx, subelems, 11, data, YANG_TYPE, NULL, &type->exts);
1172}
1173
David Sedlák1af868e2019-07-17 17:03:14 +02001174/**
1175 * @brief Parse max-elements element.
1176 *
1177 * @param[in,out] ctx YIN parser context for logging and to store current state.
1178 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1179 * @param[in,out] data Data to read from, always moved to currently handled character.
1180 * @param[in,out] max Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001181 * @param[in] flags Flags to write to.
David Sedlák1af868e2019-07-17 17:03:14 +02001182 * @param[in,out] exts Extension instances to add to.
1183 *
1184 * @return LY_ERR values.
1185 */
1186static LY_ERR
1187yin_parse_maxelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *max,
1188 uint16_t *flags, struct lysp_ext_instance **exts)
1189{
1190 const char *temp_val = NULL;
1191 char *ptr;
1192 unsigned long int num;
1193 struct yin_subelement subelems[1] = {
1194 {YANG_CUSTOM, NULL, 0},
1195 };
David Sedlák374d2b32019-07-17 15:06:55 +02001196
David Sedlák1af868e2019-07-17 17:03:14 +02001197 *flags |= LYS_SET_MAX;
1198 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MAX_ELEMENTS));
1199 if (!temp_val || temp_val[0] == '\0' || temp_val[0] == '0' || (temp_val[0] != 'u' && !isdigit(temp_val[0]))) {
David Sedlák1538a842019-08-08 15:38:51 +02001200 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +02001201 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1202 return LY_EVALID;
1203 }
1204
1205 if (strcmp(temp_val, "unbounded")) {
1206 errno = 0;
1207 num = strtoul(temp_val, &ptr, 10);
1208 if (*ptr != '\0') {
David Sedlák1538a842019-08-08 15:38:51 +02001209 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +02001210 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1211 return LY_EVALID;
1212 }
1213 if ((errno == ERANGE) || (num > UINT32_MAX)) {
David Sedlák1538a842019-08-08 15:38:51 +02001214 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
David Sedlák1af868e2019-07-17 17:03:14 +02001215 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1216 return LY_EVALID;
1217 }
1218 *max = num;
1219 }
1220 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1221 return yin_parse_content(ctx, subelems, 1, data, YANG_MAX_ELEMENTS, NULL, exts);
1222}
David Sedlák374d2b32019-07-17 15:06:55 +02001223
1224/**
David Sedlák09e18c92019-07-18 11:17:11 +02001225 * @brief Parse max-elements element.
1226 *
1227 * @param[in,out] ctx YIN parser context for logging and to store current state.
1228 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1229 * @param[in,out] data Data to read from, always moved to currently handled character.
1230 * @param[in,out] min Value to write to.
David Sedláka2dad212019-07-18 12:45:19 +02001231 * @param[in] flags Flags to write to.
David Sedlák09e18c92019-07-18 11:17:11 +02001232 * @param[in,out] exts Extension instances to add to.
1233 *
1234 * @return LY_ERR values.
1235 */
1236static LY_ERR
1237yin_parse_minelements(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint32_t *min,
1238 uint16_t *flags, struct lysp_ext_instance **exts)
1239{
1240 const char *temp_val = NULL;
1241 char *ptr;
1242 unsigned long int num;
1243 struct yin_subelement subelems[1] = {
1244 {YANG_CUSTOM, NULL, 0},
1245 };
1246
1247 *flags |= LYS_SET_MIN;
David Sedláka2dad212019-07-18 12:45:19 +02001248 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MIN_ELEMENTS));
David Sedlák09e18c92019-07-18 11:17:11 +02001249
1250 if (!temp_val || temp_val[0] == '\0' || (temp_val[0] == '0' && temp_val[1] != '\0')) {
David Sedlák1538a842019-08-08 15:38:51 +02001251 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001252 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1253 return LY_EVALID;
1254 }
1255
1256 errno = 0;
1257 num = strtoul(temp_val, &ptr, 10);
1258 if (ptr[0] != 0) {
David Sedlák1538a842019-08-08 15:38:51 +02001259 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001260 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1261 return LY_EVALID;
1262 }
1263 if (errno == ERANGE || num > UINT32_MAX) {
David Sedlák1538a842019-08-08 15:38:51 +02001264 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
David Sedlák09e18c92019-07-18 11:17:11 +02001265 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1266 return LY_EVALID;
1267 }
1268 *min = num;
1269 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
David Sedláka2dad212019-07-18 12:45:19 +02001270 return yin_parse_content(ctx, subelems, 1, data, YANG_MIN_ELEMENTS, NULL, exts);
David Sedlák09e18c92019-07-18 11:17:11 +02001271}
1272
David Sedláka2dad212019-07-18 12:45:19 +02001273/**
1274 * @brief Parse min-elements or max-elements element.
1275 *
1276 * @param[in,out] ctx YIN parser context for logging and to store current state.
1277 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1278 * @param[in,out] data Data to read from, always moved to currently handled character.
1279 * @param[in] parent Identification of parent element.
1280 * @param[in] current Identification of current element.
1281 * @param[in] dest Where the parsed value and flags should be stored.
1282 *
1283 * @return LY_ERR values.
1284 */
David Sedlák09e18c92019-07-18 11:17:11 +02001285static LY_ERR
1286yin_parse_minmax(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1287 enum yang_keyword parent, enum yang_keyword current, void *dest)
1288{
1289 assert(current == YANG_MAX_ELEMENTS || current == YANG_MIN_ELEMENTS);
David Sedlák4ffcec82019-07-25 15:10:21 +02001290 assert(parent == YANG_LEAF_LIST || parent == YANG_REFINE || parent == YANG_LIST || parent == YANG_DEVIATE);
David Sedlák09e18c92019-07-18 11:17:11 +02001291 uint32_t *lim;
1292 uint16_t *flags;
1293 struct lysp_ext_instance **exts;
1294
1295 if (parent == YANG_LEAF_LIST) {
1296 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
1297 flags = &((struct lysp_node_leaflist *)dest)->flags;
1298 exts = &((struct lysp_node_leaflist *)dest)->exts;
1299 } else if (parent == YANG_REFINE) {
1300 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
1301 flags = &((struct lysp_refine *)dest)->flags;
1302 exts = &((struct lysp_refine *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001303 } else if (parent == YANG_LIST) {
David Sedlák09e18c92019-07-18 11:17:11 +02001304 lim = (current == YANG_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
1305 flags = &((struct lysp_node_list *)dest)->flags;
1306 exts = &((struct lysp_node_list *)dest)->exts;
David Sedlák4ffcec82019-07-25 15:10:21 +02001307 } else {
1308 lim = ((struct minmax_dev_meta *)dest)->lim;
1309 flags = ((struct minmax_dev_meta *)dest)->flags;
1310 exts = ((struct minmax_dev_meta *)dest)->exts;
David Sedlák09e18c92019-07-18 11:17:11 +02001311 }
1312
1313 if (current == YANG_MAX_ELEMENTS) {
1314 LY_CHECK_RET(yin_parse_maxelements(ctx, attrs, data, lim, flags, exts));
1315 } else {
1316 LY_CHECK_RET(yin_parse_minelements(ctx, attrs, data, lim, flags, exts));
1317 }
1318
1319 return LY_SUCCESS;
1320}
1321
1322/**
David Sedláka2dad212019-07-18 12:45:19 +02001323 * @brief Parser ordered-by element.
1324 *
1325 * @param[in,out] ctx YIN parser context for logging and to store current state.
1326 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1327 * @param[in,out] data Data to read from, always moved to currently handled character.
1328 * @param[out] flags Flags to write to.
1329 * @param[in,out] exts Extension instance to add to.
1330 *
1331 * @return LY_ERR values.
1332 */
1333static LY_ERR
1334yin_parse_orderedby(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1335 uint16_t *flags, struct lysp_ext_instance **exts)
1336{
1337 const char *temp_val;
1338 struct yin_subelement subelems[1] = {
1339 {YANG_CUSTOM, NULL, 0},
1340 };
1341
1342 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_ORDERED_BY));
1343 if (strcmp(temp_val, "system") == 0) {
1344 *flags |= LYS_ORDBY_SYSTEM;
1345 } else if (strcmp(temp_val, "user") == 0) {
1346 *flags |= LYS_ORDBY_USER;
1347 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001348 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1349 "ordered-by", "system", "user");
David Sedláka2dad212019-07-18 12:45:19 +02001350 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1351 return LY_EVALID;
1352 }
1353 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1354
1355 return yin_parse_content(ctx, subelems, 1, data, YANG_ORDERED_BY, NULL, exts);
1356}
1357
1358/**
David Sedlák8a83bbb2019-07-18 14:46:00 +02001359 * @brief parse any-data or any-xml element.
1360 *
1361 * @param[in,out] ctx YIN parser context for logging and to store current state.
1362 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1363 * @param[in,out] data Data to read from, always moved to currently handled character.
1364 * @param[in] any_kw Identification of current element, can be set to YANG_ANY_DATA or YANG_ANY_XML
David Sedlákad83cf92019-08-13 12:53:53 +02001365 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák8a83bbb2019-07-18 14:46:00 +02001366 *
1367 * @return LY_ERR values.
1368 */
1369static LY_ERR
1370yin_parse_any(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1371 enum yang_keyword any_kw, struct tree_node_meta *node_meta)
1372{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001373 struct lysp_node_anydata *any;
1374
David Sedlák8d552d62019-08-06 15:29:05 +02001375 /* create new sibling */
1376 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, any, next);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001377 any->nodetype = (any_kw == YANG_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
1378 any->parent = node_meta->parent;
1379
David Sedlák8a83bbb2019-07-18 14:46:00 +02001380 /* parser argument */
David Sedlák203ca3a2019-07-18 15:26:25 +02001381 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
David Sedlák8a83bbb2019-07-18 14:46:00 +02001382
1383 struct yin_subelement subelems[9] = {
1384 {YANG_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1385 {YANG_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1386 {YANG_IF_FEATURE, &any->iffeatures, 0},
1387 {YANG_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1388 {YANG_MUST, &any->musts, 0},
1389 {YANG_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1390 {YANG_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1391 {YANG_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1392 {YANG_CUSTOM, NULL, 0},
1393 };
1394 return yin_parse_content(ctx, subelems, 9, data, any_kw, NULL, &any->exts);
1395}
1396
1397/**
David Sedlák203ca3a2019-07-18 15:26:25 +02001398 * @brief parse leaf element.
1399 *
1400 * @param[in,out] ctx YIN parser context for logging and to store current state.
1401 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1402 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02001403 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák203ca3a2019-07-18 15:26:25 +02001404 *
1405 * @return LY_ERR values.
1406 */
1407static LY_ERR
1408yin_parse_leaf(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1409 struct tree_node_meta *node_meta)
1410{
David Sedlák203ca3a2019-07-18 15:26:25 +02001411 struct lysp_node_leaf *leaf;
1412
David Sedlák8d552d62019-08-06 15:29:05 +02001413 /* create structure new leaf */
1414 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, leaf, next);
David Sedlák203ca3a2019-07-18 15:26:25 +02001415 leaf->nodetype = LYS_LEAF;
1416 leaf->parent = node_meta->parent;
1417
David Sedlák203ca3a2019-07-18 15:26:25 +02001418 /* parser argument */
1419 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, YANG_LEAF));
1420
1421 /* parse content */
1422 struct yin_subelement subelems[12] = {
1423 {YANG_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1424 {YANG_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1425 {YANG_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1426 {YANG_IF_FEATURE, &leaf->iffeatures, 0},
1427 {YANG_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1428 {YANG_MUST, &leaf->musts, 0},
1429 {YANG_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1430 {YANG_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1431 {YANG_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1432 {YANG_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1433 {YANG_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1434 {YANG_CUSTOM, NULL, 0},
David Sedlák05404f62019-07-24 14:11:53 +02001435 };
David Sedlák203ca3a2019-07-18 15:26:25 +02001436 return yin_parse_content(ctx, subelems, 12, data, YANG_LEAF, NULL, &leaf->exts);
1437}
1438
1439/**
David Sedlákc3da3ef2019-07-19 12:56:08 +02001440 * @brief Parse leaf-list element.
1441 *
1442 * @param[in,out] ctx YIN parser context for logging and to store current state.
1443 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1444 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02001445 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákc3da3ef2019-07-19 12:56:08 +02001446 *
1447 * @return LY_ERR values.
1448 */
1449static LY_ERR
1450yin_parse_leaflist(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1451 struct tree_node_meta *node_meta)
1452{
David Sedlákc3da3ef2019-07-19 12:56:08 +02001453 struct lysp_node_leaflist *llist;
David Sedlákc3da3ef2019-07-19 12:56:08 +02001454
David Sedlák8d552d62019-08-06 15:29:05 +02001455 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, llist, next);
1456
David Sedlákc3da3ef2019-07-19 12:56:08 +02001457 llist->nodetype = LYS_LEAFLIST;
1458 llist->parent = node_meta->parent;
1459
David Sedlákc3da3ef2019-07-19 12:56:08 +02001460 /* parse argument */
1461 yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, YANG_LEAF_LIST);
1462
1463 /* parse content */
1464 struct yin_subelement subelems[14] = {
1465 {YANG_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1466 {YANG_DEFAULT, &llist->dflts, 0},
1467 {YANG_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1468 {YANG_IF_FEATURE, &llist->iffeatures, 0},
1469 {YANG_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1470 {YANG_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1471 {YANG_MUST, &llist->musts, 0},
1472 {YANG_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1473 {YANG_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1474 {YANG_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1475 {YANG_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1476 {YANG_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1477 {YANG_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1478 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001479 };
David Sedlákc3da3ef2019-07-19 12:56:08 +02001480 LY_CHECK_RET(yin_parse_content(ctx, subelems, 14, data, YANG_LEAF_LIST, NULL, &llist->exts));
1481
1482 /* invalid combination of subelements */
1483 if ((llist->min) && (llist->dflts)) {
David Sedlák1538a842019-08-08 15:38:51 +02001484 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001485 return LY_EVALID;
1486 }
1487 if (llist->max && llist->min > llist->max) {
David Sedlák1538a842019-08-08 15:38:51 +02001488 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001489 return LY_EVALID;
1490 }
1491
1492 return LY_SUCCESS;
1493}
1494
1495/**
David Sedlák04e17b22019-07-19 15:29:48 +02001496 * @brief Parse typedef element.
1497 *
1498 * @param[in,out] ctx YIN parser context for logging and to store current state.
1499 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1500 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02001501 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
David Sedlák04e17b22019-07-19 15:29:48 +02001502 *
1503 * @return LY_ERR values.
1504 */
1505static LY_ERR
1506yin_parse_typedef(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02001507 struct tree_node_meta *typedef_meta)
David Sedlák04e17b22019-07-19 15:29:48 +02001508{
1509 struct lysp_tpdf *tpdf;
David Sedlák6881b512019-08-13 12:52:00 +02001510 struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->siblings;
1511 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *tpdfs, tpdf, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001512
1513 /* parse argument */
1514 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, YANG_TYPEDEF));
1515
1516 /* parse content */
1517 struct yin_subelement subelems[7] = {
1518 {YANG_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1519 {YANG_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1520 {YANG_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1521 {YANG_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1522 {YANG_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1523 {YANG_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1524 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001525 };
David Sedlák04e17b22019-07-19 15:29:48 +02001526 LY_CHECK_RET(yin_parse_content(ctx, subelems, 7, data, YANG_TYPEDEF, NULL, &tpdf->exts));
1527
1528 /* store data for collision check */
1529 if (typedef_meta->parent && !(typedef_meta->parent->nodetype & (LYS_GROUPING | LYS_ACTION | LYS_INOUT | LYS_NOTIF))) {
David Sedláke8b74df2019-08-14 14:18:22 +02001530 LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0) == -1, LY_EMEM);
David Sedlák04e17b22019-07-19 15:29:48 +02001531 }
1532
1533 return LY_SUCCESS;
1534}
1535
1536/**
David Sedlákd2d676a2019-07-22 11:28:19 +02001537 * @brief Parse refine element.
1538 *
1539 * @param[in,out] ctx YIN parser context for logging and to store current state.
1540 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1541 * @param[in,out] data Data to read from, always moved to currently handled character.
1542 * @param[in,out] refines Refines to add to.
1543 *
1544 * @return LY_ERR values.
1545 */
1546static LY_ERR
1547yin_parse_refine(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1548 struct lysp_refine **refines)
1549{
1550 struct lysp_refine *rf;
1551
1552 /* allocate new refine */
1553 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *refines, rf, LY_EMEM);
1554
1555 /* parse attribute */
1556 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, YANG_REFINE));
1557 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(rf->nodeid), "refine");
1558
1559 /* parse content */
1560 struct yin_subelement subelems[11] = {
1561 {YANG_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
1562 {YANG_DEFAULT, &rf->dflts, 0},
1563 {YANG_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
1564 {YANG_IF_FEATURE, &rf->iffeatures, 0},
1565 {YANG_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1566 {YANG_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1567 {YANG_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1568 {YANG_MUST, &rf->musts, 0},
1569 {YANG_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1570 {YANG_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1571 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001572 };
David Sedlákd2d676a2019-07-22 11:28:19 +02001573 return yin_parse_content(ctx, subelems, 11, data, YANG_REFINE, NULL, &rf->exts);
1574}
1575
1576/**
David Sedlák0d6de5a2019-07-22 13:25:44 +02001577 * @brief Parse uses element.
1578 *
1579 * @param[in,out] ctx YIN parser context for logging and to store current state.
1580 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1581 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02001582 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák0d6de5a2019-07-22 13:25:44 +02001583 *
1584 * @return LY_ERR values.
1585 */
1586static LY_ERR
1587yin_parse_uses(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1588 struct tree_node_meta *node_meta)
1589{
David Sedlák0d6de5a2019-07-22 13:25:44 +02001590 struct lysp_node_uses *uses;
1591
David Sedlák8d552d62019-08-06 15:29:05 +02001592 /* create new uses */
1593 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, uses, next);
David Sedlák0d6de5a2019-07-22 13:25:44 +02001594 uses->nodetype = LYS_USES;
1595 uses->parent = node_meta->parent;
1596
David Sedlák0d6de5a2019-07-22 13:25:44 +02001597 /* parse argument */
1598 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, YANG_USES));
1599
1600 /* parse content */
David Sedlák6881b512019-08-13 12:52:00 +02001601 struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
David Sedlák0d6de5a2019-07-22 13:25:44 +02001602 struct yin_subelement subelems[8] = {
1603 {YANG_AUGMENT, &augments, 0},
1604 {YANG_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1605 {YANG_IF_FEATURE, &uses->iffeatures, 0},
1606 {YANG_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1607 {YANG_REFINE, &uses->refines, 0},
1608 {YANG_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1609 {YANG_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1610 {YANG_CUSTOM, NULL, 0},
David Sedlákaa854b02019-07-22 14:17:10 +02001611 };
David Sedlák0d6de5a2019-07-22 13:25:44 +02001612 LY_CHECK_RET(yin_parse_content(ctx, subelems, 8, data, YANG_USES, NULL, &uses->exts));
1613 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, uses->augments, NULL, NULL));
1614
1615 return LY_SUCCESS;
1616}
1617
1618/**
David Sedlákaa854b02019-07-22 14:17:10 +02001619 * @brief Parse revision element.
1620 *
1621 * @param[in,out] ctx YIN parser context for logging and to store current state.
1622 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1623 * @param[in,out] data Data to read from, always moved to currently handled character.
1624 * @param[in,out] revs Parsed revisions to add to.
1625 *
1626 * @return LY_ERR values.
1627 */
1628static LY_ERR
1629yin_parse_revision(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1630 struct lysp_revision **revs)
1631{
1632 struct lysp_revision *rev;
1633 const char *temp_date = NULL;
1634
1635 /* allocate new reivison */
1636 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *revs, rev, LY_EMEM);
1637
1638 /* parse argument */
1639 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_date, Y_STR_ARG, YANG_REVISION));
1640 /* check value */
1641 if (lysp_check_date((struct lys_parser_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
1642 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1643 return LY_EVALID;
1644 }
1645 strcpy(rev->date, temp_date);
1646 FREE_STRING(ctx->xml_ctx.ctx, temp_date);
1647
1648 /* parse content */
1649 struct yin_subelement subelems[3] = {
1650 {YANG_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1651 {YANG_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1652 {YANG_CUSTOM, NULL, 0},
1653 };
1654 return yin_parse_content(ctx, subelems, 3, data, YANG_REVISION, NULL, &rev->exts);
1655}
1656
David Sedlák5e13dea2019-07-22 16:06:45 +02001657/**
1658 * @brief Parse include element.
1659 *
1660 * @param[in,out] ctx YIN parser context for logging and to store current state.
1661 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
1662 * @param[in,out] data Data to read from, always moved to currently handled character.
1663 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1664 *
1665 * @return LY_ERR values.
1666 */
David Sedlák0c2bab92019-07-22 15:33:19 +02001667static LY_ERR
1668yin_parse_include(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1669 struct include_meta *inc_meta)
1670{
1671 struct lysp_include *inc;
1672
1673 /* allocate new include */
1674 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *inc_meta->includes, inc, LY_EMEM);
1675
1676 /* parse argument */
1677 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, YANG_INCLUDE));
1678
1679 /* submodules share the namespace with the module names, so there must not be
1680 * a module of the same name in the context, no need for revision matching */
1681 if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xml_ctx.ctx, inc->name)) {
David Sedlák1538a842019-08-08 15:38:51 +02001682 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_NAME_COL, inc->name);
David Sedlák0c2bab92019-07-22 15:33:19 +02001683 return LY_EVALID;
1684 }
1685
1686 /* parse content */
1687 struct yin_subelement subelems[4] = {
1688 {YANG_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1689 {YANG_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1690 {YANG_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1691 {YANG_CUSTOM, NULL, 0},
1692 };
1693 return yin_parse_content(ctx, subelems, 4, data, YANG_INCLUDE, NULL, &inc->exts);
1694}
1695
David Sedlákaa854b02019-07-22 14:17:10 +02001696/**
David Sedlákdfbbb442019-08-06 16:33:21 +02001697 * @brief Parse revision date element.
1698 *
1699 * @param[in,out] ctx Yin parser context for logging and to store current state.
1700 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of revision-date element.
1701 * @param[in,out] data Data to read from, always moved to currently handled character.
1702 * @param[in,out] rev Array to store the parsed value in.
1703 * @param[in,out] exts Extension instances to add to.
1704 *
1705 * @return LY_ERR values.
1706 */
1707static LY_ERR
1708yin_parse_revision_date(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, char *rev,
1709 struct lysp_ext_instance **exts)
1710{
1711 const char *temp_rev;
1712 struct yin_subelement subelems[1] = {
1713 {YANG_CUSTOM, NULL, 0}
1714 };
1715
1716 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, YANG_REVISION_DATE));
1717 LY_CHECK_ERR_RET(lysp_check_date((struct lys_parser_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
1718 FREE_STRING(ctx->xml_ctx.ctx, temp_rev), LY_EVALID);
1719
1720 strcpy(rev, temp_rev);
1721 FREE_STRING(ctx->xml_ctx.ctx, temp_rev);
1722
1723 return yin_parse_content(ctx, subelems, 1, data, YANG_REVISION_DATE, NULL, exts);
1724}
1725
1726/**
1727 * @brief Parse config element.
1728 *
1729 * @param[in] ctx Yin parser context for logging and to store current state.
1730 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1731 * @param[in,out] data Data to read from, always moved to currently handled character.
1732 * @param[in,out] flags Flags to add to.
1733 * @param[in,out] exts Extension instances to add to.
1734 *
1735 * @return LY_ERR values.
1736 */
1737static LY_ERR
1738yin_parse_config(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1739 struct lysp_ext_instance **exts)
1740{
1741 const char *temp_val = NULL;
1742 struct yin_subelement subelems[1] = {
1743 {YANG_CUSTOM, NULL, 0}
1744 };
1745
1746 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_CONFIG));
1747 if (strcmp(temp_val, "true") == 0) {
1748 *flags |= LYS_CONFIG_W;
1749 } else if (strcmp(temp_val, "false") == 0) {
1750 *flags |= LYS_CONFIG_R;
1751 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001752 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
1753 "true", "false");
David Sedlákdfbbb442019-08-06 16:33:21 +02001754 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1755 return LY_EVALID;
1756 }
1757 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1758
1759 return yin_parse_content(ctx, subelems, 1, data, YANG_CONFIG, NULL, exts);
1760}
1761
1762/**
1763 * @brief Parse yang-version element.
1764 *
1765 * @param[in,out] ctx Yin parser context for logging and to store current state.
1766 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yang-version element.
1767 * @param[in] data Data to read from, always moved to currently handled character.
1768 * @param[out] version Storage for the parsed information.
1769 * @param[in,out] exts Extension instance to add to.
1770 *
1771 * @return LY_ERR values.
1772 */
1773static LY_ERR
1774yin_parse_yangversion(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint8_t *version,
1775 struct lysp_ext_instance **exts)
1776{
1777 const char *temp_version = NULL;
1778 struct yin_subelement subelems[1] = {
1779 {YANG_CUSTOM, NULL, 0}
1780 };
1781
1782 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, YANG_YANG_VERSION));
1783 if (strcmp(temp_version, "1.0") == 0) {
1784 *version = LYS_VERSION_1_0;
1785 } else if (strcmp(temp_version, "1.1") == 0) {
1786 *version = LYS_VERSION_1_1;
1787 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001788 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
1789 "yang-version", "1.0", "1.1");
David Sedlákdfbbb442019-08-06 16:33:21 +02001790 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1791 return LY_EVALID;
1792 }
1793 FREE_STRING(ctx->xml_ctx.ctx, temp_version);
1794 ctx->mod_version = *version;
1795
1796 return yin_parse_content(ctx, subelems, 1, data, YANG_YANG_VERSION, NULL, exts);
1797}
1798
1799/**
1800 * @brief Parse import element.
1801 *
1802 * @param[in,out] ctx Yin parser context for logging and to store current state.
1803 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of import element.
1804 * @param[in,out] data Data to read from, always moved to currently handled character.
1805 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
1806 *
1807 * @return LY_ERR values.
1808 */
1809static LY_ERR
1810yin_parse_import(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct import_meta *imp_meta)
1811{
1812 struct lysp_import *imp;
1813 /* allocate new element in sized array for import */
1814 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *imp_meta->imports, imp, LY_EMEM);
1815
1816 struct yin_subelement subelems[5] = {
1817 {YANG_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
1818 {YANG_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1819 {YANG_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
1820 {YANG_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
1821 {YANG_CUSTOM, NULL, 0}
1822 };
1823
1824 /* parse import attributes */
1825 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, YANG_IMPORT));
1826 LY_CHECK_RET(yin_parse_content(ctx, subelems, 5, data, YANG_IMPORT, NULL, &imp->exts));
1827 /* check prefix validity */
1828 LY_CHECK_RET(lysp_check_prefix((struct lys_parser_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
1829
1830 return LY_SUCCESS;
1831}
1832
1833/**
1834 * @brief Parse mandatory element.
1835 *
1836 * @param[in,out] ctx Yin parser context for logging and to store current state.
1837 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element.
1838 * @param[in,out] data Data to read from, always moved to currently handled character.
1839 * @param[in,out] flags Flags to add to.
1840 * @param[in,out] exts Extension instances to add to.
1841 *
1842 * @return LY_ERR values.
1843 */
1844static LY_ERR
1845yin_parse_mandatory(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1846 struct lysp_ext_instance **exts)
1847{
1848 const char *temp_val = NULL;
1849 struct yin_subelement subelems[1] = {
1850 {YANG_CUSTOM, NULL, 0}
1851 };
1852
1853 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_MANDATORY));
1854 if (strcmp(temp_val, "true") == 0) {
1855 *flags |= LYS_MAND_TRUE;
1856 } else if (strcmp(temp_val, "false") == 0) {
1857 *flags |= LYS_MAND_FALSE;
1858 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001859 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1860 "mandatory", "true", "false");
David Sedlákdfbbb442019-08-06 16:33:21 +02001861 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1862 return LY_EVALID;
1863 }
1864 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1865
1866 return yin_parse_content(ctx, subelems, 1, data, YANG_MANDATORY, NULL, exts);
1867}
1868
1869/**
1870 * @brief Parse status element.
1871 *
1872 * @param[in,out] ctx Yin parser context for logging and to store current state.
1873 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of status element.
1874 * @param[in,out] data Data to read from, always moved to currently handled character.
1875 * @param[in,out] flags Flags to add to.
1876 * @param[in,out] exts Extension instances to add to.
1877 *
1878 * @return LY_ERR values.
1879 */
1880static LY_ERR
1881yin_parse_status(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, uint16_t *flags,
1882 struct lysp_ext_instance **exts)
1883{
1884 const char *value = NULL;
1885 struct yin_subelement subelems[1] = {
1886 {YANG_CUSTOM, NULL, 0}
1887 };
1888
1889 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &value, Y_STR_ARG, YANG_STATUS));
1890 if (strcmp(value, "current") == 0) {
1891 *flags |= LYS_STATUS_CURR;
1892 } else if (strcmp(value, "deprecated") == 0) {
1893 *flags |= LYS_STATUS_DEPRC;
1894 } else if (strcmp(value, "obsolete") == 0) {
1895 *flags |= LYS_STATUS_OBSLT;
1896 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001897 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
1898 "status", "current", "deprecated", "obsolete");
David Sedlákdfbbb442019-08-06 16:33:21 +02001899 FREE_STRING(ctx->xml_ctx.ctx, value);
1900 return LY_EVALID;
1901 }
1902 FREE_STRING(ctx->xml_ctx.ctx, value);
1903
1904 return yin_parse_content(ctx, subelems, 1, data, YANG_STATUS, NULL, exts);
1905}
1906
1907/**
1908 * @brief Parse when element.
1909 *
1910 * @param[in,out] ctx Yin parser context for logging and to store current state.
1911 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of when element.
1912 * @param[in,out] data Data to read from, always moved to currently handled character.
1913 * @param[out] when_p When pointer to parse to.
1914 */
1915static LY_ERR
1916yin_parse_when(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_when **when_p)
1917{
1918 struct lysp_when *when;
1919 when = calloc(1, sizeof *when);
1920 LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
1921 yin_parse_attribute(ctx, attrs, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, YANG_WHEN);
1922 *when_p = when;
1923 struct yin_subelement subelems[3] = {
1924 {YANG_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
1925 {YANG_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
1926 {YANG_CUSTOM, NULL, 0}
1927 };
1928
1929 return yin_parse_content(ctx, subelems, 3, data, YANG_WHEN, NULL, &when->exts);
1930}
1931
1932/**
1933 * @brief Parse yin-elemenet element.
1934 *
1935 * @param[in,out] ctx Yin parser context for logging and to store current state.
1936 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of yin-element element.
1937 * @param[in,out] data Data to read from, always moved to currently handled position.
1938 * @param[in,out] flags Flags to add to.
1939 * @prama[in,out] exts Extension instance to add to.
1940 *
1941 * @return LY_ERR values.
1942 */
1943static LY_ERR
1944yin_parse_yin_element_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1945 uint16_t *flags, struct lysp_ext_instance **exts)
1946{
1947 const char *temp_val = NULL;
1948 struct yin_subelement subelems[1] = {
1949 {YANG_CUSTOM, NULL, 0}
1950 };
1951
1952 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_YIN_ELEMENT));
1953 if (strcmp(temp_val, "true") == 0) {
1954 *flags |= LYS_YINELEM_TRUE;
1955 } else if (strcmp(temp_val, "false") == 0) {
1956 *flags |= LYS_YINELEM_FALSE;
1957 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02001958 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1959 "yin-element", "true", "false");
David Sedlákdfbbb442019-08-06 16:33:21 +02001960 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1961 return LY_EVALID;
1962 }
1963 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
1964
1965 return yin_parse_content(ctx, subelems, 1, data, YANG_YIN_ELEMENT, NULL, exts);
1966}
1967
1968/**
1969 * @brief Parse argument element.
1970 *
1971 * @param[in,out] xml_ctx Xml context.
1972 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of argument element.
1973 * @param[in,out] data Data to read from, always moved to currently handled character.
1974 * @param[in,out] arg_meta Meta information about destionation af prased data.
1975 * @param[in,out] exts Extension instance to add to.
1976 *
1977 * @return LY_ERR values.
1978 */
1979static LY_ERR
1980yin_parse_argument_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
1981 struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
1982{
1983 struct yin_subelement subelems[2] = {
1984 {YANG_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
1985 {YANG_CUSTOM, NULL, 0}
1986 };
1987
1988 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, YANG_ARGUMENT));
1989
1990 return yin_parse_content(ctx, subelems, 2, data, YANG_ARGUMENT, NULL, exts);
1991}
1992
1993/**
1994 * @brief Parse the extension statement.
1995 *
1996 * @param[in,out] ctx Yin parser context for logging and to store current state.
1997 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension element.
1998 * @param[in,out] data Data to read from.
1999 * @param[in,out] extensions Extensions to add to.
2000 *
2001 * @return LY_ERR values.
2002 */
2003static LY_ERR
2004yin_parse_extension(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, struct lysp_ext **extensions)
2005{
2006 struct lysp_ext *ex;
2007 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *extensions, ex, LY_EMEM);
2008 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, YANG_EXTENSION));
2009
2010 struct yin_argument_meta arg_info = {&ex->flags, &ex->argument};
2011 struct yin_subelement subelems[5] = {
2012 {YANG_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2013 {YANG_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2014 {YANG_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2015 {YANG_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2016 {YANG_CUSTOM, NULL, 0}
2017 };
2018
2019 return yin_parse_content(ctx, subelems, 5, data, YANG_EXTENSION, NULL, &ex->exts);
2020}
2021
2022/**
David Sedlák5e13dea2019-07-22 16:06:45 +02002023 * @brief Parse feature element.
2024 *
2025 * @param[in,out] ctx YIN parser context for logging and to store current state.
2026 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2027 * @param[in,out] data Data to read from, always moved to currently handled character.
2028 * @param[in,out] features Features to add to.
2029 *
2030 * @return LY_ERR values.
2031 */
2032static LY_ERR
2033yin_parse_feature(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2034 struct lysp_feature **features)
2035{
2036 struct lysp_feature *feat;
2037
2038 /* allocate new feature */
2039 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *features, feat, LY_EMEM);
2040
2041 /* parse argument */
2042 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, YANG_FEATURE));
2043
2044 /* parse content */
2045 struct yin_subelement subelems[5] = {
2046 {YANG_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2047 {YANG_IF_FEATURE, &feat->iffeatures, 0},
2048 {YANG_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2049 {YANG_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2050 {YANG_CUSTOM, NULL, 0},
2051 };
2052 return yin_parse_content(ctx, subelems, 5, data, YANG_FEATURE, NULL, &feat->exts);
2053}
2054
2055/**
David Sedlák28794f22019-07-22 16:45:00 +02002056 * @brief Parse identity element.
2057 *
2058 * @param[in,out] ctx YIN parser context for logging and to store current state.
2059 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2060 * @param[in,out] data Data to read from, always moved to currently handled character.
2061 * @param[in,out] identities Identities to add to.
2062 *
2063 * @return LY_ERR values.
2064 */
2065static LY_ERR
2066yin_parse_identity(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2067 struct lysp_ident **identities)
2068{
2069 struct lysp_ident *ident;
2070
2071 /* allocate new identity */
2072 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *identities, ident, LY_EMEM);
2073
2074 /* parse argument */
2075 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, YANG_IDENTITY));
2076
2077 /* parse content */
2078 struct yin_subelement subelems[6] = {
2079 {YANG_BASE, &ident->bases, 0},
2080 {YANG_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2081 {YANG_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2082 {YANG_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2083 {YANG_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2084 {YANG_CUSTOM, NULL, 0},
2085 };
2086 return yin_parse_content(ctx, subelems, 6, data, YANG_IDENTITY, NULL, &ident->exts);
2087}
2088
2089/**
David Sedlákaf536aa2019-07-23 13:42:23 +02002090 * @brief Parse list element.
2091 *
2092 * @param[in,out] ctx YIN parser context for logging and to store current state.
2093 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2094 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002095 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákaf536aa2019-07-23 13:42:23 +02002096 *
2097 * @return LY_ERR values.
2098 */
2099static LY_ERR
David Sedlákf111bcb2019-07-23 17:15:51 +02002100yin_parse_list(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2101 struct tree_node_meta *node_meta)
David Sedlákaf536aa2019-07-23 13:42:23 +02002102{
David Sedlákaf536aa2019-07-23 13:42:23 +02002103 struct lysp_node_list *list;
David Sedlák81497a32019-08-13 16:56:26 +02002104 LY_ERR ret = LY_SUCCESS;
2105 struct yin_subelement *subelems = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002106
David Sedlák8d552d62019-08-06 15:29:05 +02002107 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, list, next);
David Sedlákaf536aa2019-07-23 13:42:23 +02002108 list->nodetype = LYS_LIST;
2109 list->parent = node_meta->parent;
2110
David Sedlákaf536aa2019-07-23 13:42:23 +02002111 /* parse argument */
2112 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, YANG_LIST));
2113
2114 /* parse list content */
David Sedlák81497a32019-08-13 16:56:26 +02002115 LY_CHECK_RET(subelems_allocator(ctx, 25, (struct lysp_node *)list, &subelems,
2116 YANG_ACTION, &list->actions, 0,
2117 YANG_ANYDATA, &list->child, 0,
2118 YANG_ANYXML, &list->child, 0,
2119 YANG_CHOICE, &list->child, 0,
2120 YANG_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2121 YANG_CONTAINER, &list->child, 0,
2122 YANG_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2123 YANG_GROUPING, &list->groupings, 0,
2124 YANG_IF_FEATURE, &list->iffeatures, 0,
2125 YANG_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2126 YANG_LEAF, &list->child, 0,
2127 YANG_LEAF_LIST, &list->child, 0,
2128 YANG_LIST, &list->child, 0,
2129 YANG_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2130 YANG_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2131 YANG_MUST, &list->musts, 0,
2132 YANG_NOTIFICATION, &list->notifs, 0,
2133 YANG_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2134 YANG_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2135 YANG_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2136 YANG_TYPEDEF, &list->typedefs, 0,
2137 YANG_UNIQUE, &list->uniques, 0,
2138 YANG_USES, &list->child, 0,
2139 YANG_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2140 YANG_CUSTOM, NULL, 0
2141 ));
2142 ret = yin_parse_content(ctx, subelems, 25, data, YANG_LIST, NULL, &list->exts);
2143 subelems_deallocator(25, subelems);
2144 LY_CHECK_RET(ret);
David Sedlákaf536aa2019-07-23 13:42:23 +02002145
2146 /* finalize parent pointers to the reallocated items */
2147 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, list->groupings, NULL, list->actions, list->notifs));
2148
2149 if (list->max && list->min > list->max) {
David Sedlák1538a842019-08-08 15:38:51 +02002150 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
David Sedlákaf536aa2019-07-23 13:42:23 +02002151 return LY_EVALID;
2152 }
2153
2154 return LY_SUCCESS;
2155}
2156
2157/**
David Sedlák031b9e72019-07-23 15:19:37 +02002158 * @brief Parse notification element.
2159 *
2160 * @param[in,out] ctx YIN parser context for logging and to store current state.
2161 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2162 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02002163 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedlák031b9e72019-07-23 15:19:37 +02002164 *
2165 * @return LY_ERR values.
2166 */
2167static LY_ERR
2168yin_parse_notification(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002169 struct tree_node_meta *notif_meta)
David Sedlák031b9e72019-07-23 15:19:37 +02002170{
2171 struct lysp_notif *notif;
David Sedlák6881b512019-08-13 12:52:00 +02002172 struct lysp_notif **notifs = (struct lysp_notif **)notif_meta->siblings;
David Sedlák81497a32019-08-13 16:56:26 +02002173 LY_ERR ret = LY_SUCCESS;
2174 struct yin_subelement *subelems = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002175
2176 /* allocate new notification */
David Sedlák6881b512019-08-13 12:52:00 +02002177 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *notifs, notif, LY_EMEM);
David Sedlák031b9e72019-07-23 15:19:37 +02002178 notif->nodetype = LYS_NOTIF;
2179 notif->parent = notif_meta->parent;
2180
2181 /* parse argument */
2182 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, YANG_NOTIFICATION));
2183
2184 /* parse notification content */
David Sedlák81497a32019-08-13 16:56:26 +02002185 LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)notif, &subelems,
2186 YANG_ANYDATA, &notif->data, 0,
2187 YANG_ANYXML, &notif->data, 0,
2188 YANG_CHOICE, &notif->data, 0,
2189 YANG_CONTAINER, &notif->data, 0,
2190 YANG_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2191 YANG_GROUPING, &notif->groupings, 0,
2192 YANG_IF_FEATURE, &notif->iffeatures, 0,
2193 YANG_LEAF, &notif->data, 0,
2194 YANG_LEAF_LIST, &notif->data, 0,
2195 YANG_LIST, &notif->data, 0,
2196 YANG_MUST, &notif->musts, YIN_SUBELEM_VER2,
2197 YANG_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2198 YANG_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2199 YANG_TYPEDEF, &notif->typedefs, 0,
2200 YANG_USES, &notif->data, 0,
2201 YANG_CUSTOM, NULL, 0
2202 ));
2203
2204 ret = yin_parse_content(ctx, subelems, 16, data, YANG_NOTIFICATION, NULL, &notif->exts);
2205 subelems_deallocator(16, subelems);
2206 LY_CHECK_RET(ret);
David Sedlák031b9e72019-07-23 15:19:37 +02002207
2208 /* finalize parent pointers to the reallocated items */
2209 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
2210
2211 return LY_SUCCESS;
2212}
2213
2214/**
David Sedláke3ce9ef2019-07-23 16:34:30 +02002215 * @brief Parse notification element.
2216 *
2217 * @param[in,out] ctx YIN parser context for logging and to store current state.
2218 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2219 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlák05404f62019-07-24 14:11:53 +02002220 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
David Sedláke3ce9ef2019-07-23 16:34:30 +02002221 *
2222 * @return LY_ERR values.
2223 */
2224static LY_ERR
2225yin_parse_grouping(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002226 struct tree_node_meta *gr_meta)
David Sedláke3ce9ef2019-07-23 16:34:30 +02002227{
2228 struct lysp_grp *grp;
David Sedlák6881b512019-08-13 12:52:00 +02002229 struct lysp_grp **grps = (struct lysp_grp **)gr_meta->siblings;
David Sedlák81497a32019-08-13 16:56:26 +02002230 LY_ERR ret = LY_SUCCESS;
2231 struct yin_subelement *subelems = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002232
2233 /* create new grouping */
David Sedlák6881b512019-08-13 12:52:00 +02002234 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *grps, grp, LY_EMEM);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002235 grp->nodetype = LYS_GROUPING;
2236 grp->parent = gr_meta->parent;
2237
2238 /* parse argument */
2239 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, YANG_GROUPING));
2240
2241 /* parse grouping content */
David Sedlák81497a32019-08-13 16:56:26 +02002242 LY_CHECK_RET(subelems_allocator(ctx, 16, (struct lysp_node *)grp, &subelems,
2243 YANG_ACTION, &grp->actions, 0,
2244 YANG_ANYDATA, &grp->data, 0,
2245 YANG_ANYXML, &grp->data, 0,
2246 YANG_CHOICE, &grp->data, 0,
2247 YANG_CONTAINER, &grp->data, 0,
2248 YANG_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2249 YANG_GROUPING, &grp->groupings, 0,
2250 YANG_LEAF, &grp->data, 0,
2251 YANG_LEAF_LIST, &grp->data, 0,
2252 YANG_LIST, &grp->data, 0,
2253 YANG_NOTIFICATION, &grp->notifs, 0,
2254 YANG_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2255 YANG_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2256 YANG_TYPEDEF, &grp->typedefs, 0,
2257 YANG_USES, &grp->data, 0,
2258 YANG_CUSTOM, NULL, 0
2259 ));
2260 ret = yin_parse_content(ctx, subelems, 16, data, YANG_GROUPING, NULL, &grp->exts);
2261 subelems_deallocator(16, subelems);
2262 LY_CHECK_RET(ret);
2263
David Sedláke3ce9ef2019-07-23 16:34:30 +02002264 /* finalize parent pointers to the reallocated items */
2265 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, grp->groupings, NULL, grp->actions, grp->notifs));
2266
2267 return LY_SUCCESS;
2268}
2269
2270/**
David Sedlákf111bcb2019-07-23 17:15:51 +02002271 * @brief Parse list element.
2272 *
2273 * @param[in,out] ctx YIN parser context for logging and to store current state.
2274 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2275 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002276 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákf111bcb2019-07-23 17:15:51 +02002277 *
2278 * @return LY_ERR values.
2279 */
2280static LY_ERR
2281yin_parse_container(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2282 struct tree_node_meta *node_meta)
2283{
David Sedlákf111bcb2019-07-23 17:15:51 +02002284 struct lysp_node_container *cont;
David Sedlák81497a32019-08-13 16:56:26 +02002285 LY_ERR ret = LY_SUCCESS;
2286 struct yin_subelement *subelems = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002287
2288 /* create new container */
David Sedlák8d552d62019-08-06 15:29:05 +02002289 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, cont, next);
David Sedlákf111bcb2019-07-23 17:15:51 +02002290 cont->nodetype = LYS_CONTAINER;
2291 cont->parent = node_meta->parent;
2292
David Sedlákf111bcb2019-07-23 17:15:51 +02002293 /* parse aegument */
2294 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cont->name, Y_IDENTIF_ARG, YANG_CONTAINER));
2295
2296 /* parse container content */
David Sedlák81497a32019-08-13 16:56:26 +02002297 LY_CHECK_RET(subelems_allocator(ctx, 21, (struct lysp_node *)cont, &subelems,
2298 YANG_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2299 YANG_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2300 YANG_ANYXML, &cont->child, 0,
2301 YANG_CHOICE, &cont->child, 0,
2302 YANG_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2303 YANG_CONTAINER, &cont->child, 0,
2304 YANG_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2305 YANG_GROUPING, &cont->groupings, 0,
2306 YANG_IF_FEATURE, &cont->iffeatures, 0,
2307 YANG_LEAF, &cont->child, 0,
2308 YANG_LEAF_LIST, &cont->child, 0,
2309 YANG_LIST, &cont->child, 0,
2310 YANG_MUST, &cont->musts, 0,
2311 YANG_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2312 YANG_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2313 YANG_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2314 YANG_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2315 YANG_TYPEDEF, &cont->typedefs, 0,
2316 YANG_USES, &cont->child, 0,
2317 YANG_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2318 YANG_CUSTOM, NULL, 0
2319 ));
2320 ret = yin_parse_content(ctx, subelems, 21, data, YANG_CONTAINER, NULL, &cont->exts);
2321 subelems_deallocator(21, subelems);
2322 LY_CHECK_RET(ret);
2323
David Sedlákf111bcb2019-07-23 17:15:51 +02002324 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, cont->groupings, NULL, cont->actions, cont->notifs));
2325
2326 return LY_SUCCESS;
2327}
2328
2329/**
David Sedlák5379d392019-07-24 10:42:03 +02002330 * @brief Parse case element.
2331 *
2332 * @param[in,out] ctx YIN parser context for logging and to store current state.
2333 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2334 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002335 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlák5379d392019-07-24 10:42:03 +02002336 *
2337 * @return LY_ERR values.
2338 */
2339static LY_ERR
2340yin_parse_case(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2341 struct tree_node_meta *node_meta)
2342{
David Sedlák5379d392019-07-24 10:42:03 +02002343 struct lysp_node_case *cas;
David Sedlák81497a32019-08-13 16:56:26 +02002344 LY_ERR ret = LY_SUCCESS;
2345 struct yin_subelement *subelems = NULL;;
David Sedlák5379d392019-07-24 10:42:03 +02002346
2347 /* create new case */
David Sedlák8d552d62019-08-06 15:29:05 +02002348 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, cas, next);
David Sedlák5379d392019-07-24 10:42:03 +02002349 cas->nodetype = LYS_CASE;
2350 cas->parent = node_meta->parent;
2351
David Sedlák5379d392019-07-24 10:42:03 +02002352 /* parse argument */
2353 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, YANG_CASE));
2354
2355 /* parse case content */
David Sedlák81497a32019-08-13 16:56:26 +02002356 LY_CHECK_RET(subelems_allocator(ctx, 14, (struct lysp_node *)cas, &subelems,
2357 YANG_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2358 YANG_ANYXML, &cas->child, 0,
2359 YANG_CHOICE, &cas->child, 0,
2360 YANG_CONTAINER, &cas->child, 0,
2361 YANG_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2362 YANG_IF_FEATURE, &cas->iffeatures, 0,
2363 YANG_LEAF, &cas->child, 0,
2364 YANG_LEAF_LIST, &cas->child, 0,
2365 YANG_LIST, &cas->child, 0,
2366 YANG_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2367 YANG_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2368 YANG_USES, &cas->child, 0,
2369 YANG_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2370 YANG_CUSTOM, NULL, 0
2371 ));
2372 ret = yin_parse_content(ctx, subelems, 14, data, YANG_CASE, NULL, &cas->exts);
2373 subelems_deallocator(14, subelems);
2374
2375 return ret;
David Sedlák5379d392019-07-24 10:42:03 +02002376}
2377
2378/**
David Sedlák05404f62019-07-24 14:11:53 +02002379 * @brief Parse choice element.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002380 *
2381 * @param[in,out] ctx YIN parser context for logging and to store current state.
2382 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2383 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákad83cf92019-08-13 12:53:53 +02002384 * @param[in] node_meta Meta information about parent node and siblings to add to.
David Sedlákb7abcfa2019-07-24 12:33:35 +02002385 *
2386 * @return LY_ERR values.
2387 */
2388LY_ERR
2389yin_parse_choice(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2390 struct tree_node_meta *node_meta)
2391{
David Sedlák81497a32019-08-13 16:56:26 +02002392 LY_ERR ret = LY_SUCCESS;
2393 struct yin_subelement *subelems = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002394 struct lysp_node_choice *choice;
2395
2396 /* create new choice */
David Sedlák8d552d62019-08-06 15:29:05 +02002397 LY_LIST_NEW_RET(ctx->xml_ctx.ctx, node_meta->siblings, choice, next);
2398
David Sedlákb7abcfa2019-07-24 12:33:35 +02002399 choice->nodetype = LYS_CHOICE;
2400 choice->parent = node_meta->parent;
2401
David Sedlákb7abcfa2019-07-24 12:33:35 +02002402 /* parse argument */
2403 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, YANG_CHOICE));
2404
2405 /* parse choice content */
David Sedlák81497a32019-08-13 16:56:26 +02002406 LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)choice, &subelems,
2407 YANG_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2408 YANG_ANYXML, &choice->child, 0,
2409 YANG_CASE, &choice->child, 0,
2410 YANG_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2411 YANG_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2412 YANG_CONTAINER, &choice->child, 0,
2413 YANG_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2414 YANG_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2415 YANG_IF_FEATURE, &choice->iffeatures, 0,
2416 YANG_LEAF, &choice->child, 0,
2417 YANG_LEAF_LIST, &choice->child, 0,
2418 YANG_LIST, &choice->child, 0,
2419 YANG_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2420 YANG_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2421 YANG_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2422 YANG_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2423 YANG_CUSTOM, NULL, 0
2424 ));
2425 ret = yin_parse_content(ctx, subelems, 17, data, YANG_CHOICE, NULL, &choice->exts);
2426 subelems_deallocator(17, subelems);
2427 return ret;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002428}
2429
2430/**
David Sedlák05404f62019-07-24 14:11:53 +02002431 * @brief Parse input or output element.
2432 *
2433 * @param[in,out] ctx YIN parser context for logging and to store current state.
2434 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2435 * @param[in,out] data Data to read from, always moved to currently handled character.
2436 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2437 *
2438 * @return LY_ERR values.
2439 */
2440static LY_ERR
2441yin_parse_inout(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data, enum yang_keyword inout_kw,
2442 struct inout_meta *inout_meta)
2443{
David Sedlák81497a32019-08-13 16:56:26 +02002444 LY_ERR ret = LY_SUCCESS;
2445 struct yin_subelement *subelems = NULL;
2446
David Sedlák05404f62019-07-24 14:11:53 +02002447 /* initiate structure */
2448 inout_meta->inout_p->nodetype = (inout_kw == YANG_INPUT) ? LYS_INPUT : LYS_OUTPUT;
2449 inout_meta->inout_p->parent = inout_meta->parent;
2450
2451 /* check attributes */
2452 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
2453
2454 /* parser input/output content */
David Sedlák81497a32019-08-13 16:56:26 +02002455 LY_CHECK_RET(subelems_allocator(ctx, 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
2456 YANG_ANYDATA, &inout_meta->inout_p->data, YIN_SUBELEM_VER2,
2457 YANG_ANYXML, &inout_meta->inout_p->data, 0,
2458 YANG_CHOICE, &inout_meta->inout_p->data, 0,
2459 YANG_CONTAINER, &inout_meta->inout_p->data, 0,
2460 YANG_GROUPING, &inout_meta->inout_p->groupings, 0,
2461 YANG_LEAF, &inout_meta->inout_p->data, 0,
2462 YANG_LEAF_LIST, &inout_meta->inout_p->data, 0,
2463 YANG_LIST, &inout_meta->inout_p->data, 0,
2464 YANG_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2465 YANG_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
2466 YANG_USES, &inout_meta->inout_p->data, 0,
2467 YANG_CUSTOM, NULL, 0
2468 ));
2469 ret = yin_parse_content(ctx, subelems, 12, data, inout_kw, NULL, &inout_meta->inout_p->exts);
2470 subelems_deallocator(12, subelems);
2471 LY_CHECK_RET(ret);
David Sedlák05404f62019-07-24 14:11:53 +02002472
2473 /* finalize parent pointers to the reallocated items */
David Sedlák81497a32019-08-13 16:56:26 +02002474 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, inout_meta->inout_p->groupings, NULL, NULL, NULL));
David Sedlák05404f62019-07-24 14:11:53 +02002475
2476 return LY_SUCCESS;
2477}
2478
David Sedlák992fb7c2019-07-24 16:51:01 +02002479/**
2480 * @brief Parse action element.
2481 *
2482 * @param[in,out] ctx YIN parser context for logging and to store current state.
2483 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2484 * @param[in,out] data Data to read from, always moved to currently handled character.
2485 * @param[in] act_meta Meta information about parent node and actions to add to.
2486 *
2487 * @return LY_ERR values.
2488 */
David Sedlák85d0eca2019-07-24 15:15:21 +02002489static LY_ERR
2490yin_parse_action(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002491 struct tree_node_meta *act_meta)
David Sedlák85d0eca2019-07-24 15:15:21 +02002492{
David Sedlák81497a32019-08-13 16:56:26 +02002493 struct lysp_action *act, **acts = (struct lysp_action **)act_meta->siblings;
2494 LY_ERR ret = LY_SUCCESS;
2495 struct yin_subelement *subelems = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002496
2497 /* create new action */
David Sedlák6881b512019-08-13 12:52:00 +02002498 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *acts, act, LY_EMEM);
David Sedlák85d0eca2019-07-24 15:15:21 +02002499 act->nodetype = LYS_ACTION;
2500 act->parent = act_meta->parent;
2501
2502 /* parse argument */
2503 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, YANG_ACTION));
2504
2505 /* parse content */
David Sedlák81497a32019-08-13 16:56:26 +02002506 LY_CHECK_RET(subelems_allocator(ctx, 9, (struct lysp_node *)act, &subelems,
2507 YANG_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2508 YANG_GROUPING, &act->groupings, 0,
2509 YANG_IF_FEATURE, &act->iffeatures, 0,
2510 YANG_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2511 YANG_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2512 YANG_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2513 YANG_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2514 YANG_TYPEDEF, &act->typedefs, 0,
2515 YANG_CUSTOM, NULL, 0
2516 ));
2517 ret = (yin_parse_content(ctx, subelems, 9, data, YANG_ACTION, NULL, &act->exts));
2518 subelems_deallocator(9, subelems);
2519 LY_CHECK_RET(ret);
2520
David Sedlák85d0eca2019-07-24 15:15:21 +02002521 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, act->groupings, NULL, NULL, NULL));
2522
2523 return LY_SUCCESS;
2524}
2525
David Sedlák05404f62019-07-24 14:11:53 +02002526/**
David Sedlák992fb7c2019-07-24 16:51:01 +02002527 * @brief Parse augment element.
2528 *
2529 * @param[in,out] ctx YIN parser context for logging and to store current state.
2530 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2531 * @param[in,out] data Data to read from, always moved to currently handled character.
2532 * @param[in] aug_meta Meta information about parent node and augments to add to.
2533 *
2534 * @return LY_ERR values.
2535 */
2536static LY_ERR
2537yin_parse_augment(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák6881b512019-08-13 12:52:00 +02002538 struct tree_node_meta *aug_meta)
David Sedlák992fb7c2019-07-24 16:51:01 +02002539{
2540 struct lysp_augment *aug;
David Sedlák6881b512019-08-13 12:52:00 +02002541 struct lysp_augment **augs = (struct lysp_augment **)aug_meta->siblings;
David Sedlák81497a32019-08-13 16:56:26 +02002542 LY_ERR ret = LY_SUCCESS;
2543 struct yin_subelement *subelems = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002544
2545 /* create new augment */
David Sedlák6881b512019-08-13 12:52:00 +02002546 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *augs, aug, LY_EMEM);
David Sedlák992fb7c2019-07-24 16:51:01 +02002547 aug->nodetype = LYS_AUGMENT;
2548 aug->parent = aug_meta->parent;
2549
2550 /* parse argument */
2551 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, YANG_AUGMENT));
2552 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
2553
2554 /* parser augment content */
David Sedlák81497a32019-08-13 16:56:26 +02002555 LY_CHECK_RET(subelems_allocator(ctx, 17, (struct lysp_node *)aug, &subelems,
2556 YANG_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2557 YANG_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2558 YANG_ANYXML, &aug->child, 0,
2559 YANG_CASE, &aug->child, 0,
2560 YANG_CHOICE, &aug->child, 0,
2561 YANG_CONTAINER, &aug->child, 0,
2562 YANG_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
2563 YANG_IF_FEATURE, &aug->iffeatures, 0,
2564 YANG_LEAF, &aug->child, 0,
2565 YANG_LEAF_LIST, &aug->child, 0,
2566 YANG_LIST, &aug->child, 0,
2567 YANG_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2568 YANG_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2569 YANG_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2570 YANG_USES, &aug->child, 0,
2571 YANG_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2572 YANG_CUSTOM, NULL, 0
2573 ));
2574 ret = yin_parse_content(ctx, subelems, 17, data, YANG_AUGMENT, NULL, &aug->exts);
2575 subelems_deallocator(17, subelems);
2576 LY_CHECK_RET(ret);
David Sedlák992fb7c2019-07-24 16:51:01 +02002577
2578 LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
2579
2580 return LY_SUCCESS;
2581}
2582
David Sedlák8b754462019-07-25 16:22:13 +02002583/**
2584 * @brief Parse deviate element.
2585 *
2586 * @param[in,out] ctx YIN parser context for logging and to store current state.
2587 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2588 * @param[in,out] data Data to read from, always moved to currently handled character.
2589 * @param[in] deviates Deviates to add to.
2590 *
2591 * @return LY_ERR values.
2592 */
David Sedlák4ffcec82019-07-25 15:10:21 +02002593static LY_ERR
2594yin_parse_deviate(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2595 struct lysp_deviate **deviates)
2596{
2597 LY_ERR ret = LY_SUCCESS;
2598 uint8_t dev_mod;
2599 const char *temp_val;
David Sedlák8d552d62019-08-06 15:29:05 +02002600 struct lysp_deviate *d;
David Sedlák4ffcec82019-07-25 15:10:21 +02002601 struct lysp_deviate_add *d_add = NULL;
2602 struct lysp_deviate_rpl *d_rpl = NULL;
2603 struct lysp_deviate_del *d_del = NULL;
2604
2605 /* parse argument */
2606 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, YANG_DEVIATE));
2607
2608 if (strcmp(temp_val, "not-supported") == 0) {
2609 dev_mod = LYS_DEV_NOT_SUPPORTED;
2610 } else if (strcmp(temp_val, "add") == 0) {
2611 dev_mod = LYS_DEV_ADD;
2612 } else if (strcmp(temp_val, "replace") == 0) {
2613 dev_mod = LYS_DEV_REPLACE;
2614 } else if (strcmp(temp_val, "delete") == 0) {
2615 dev_mod = LYS_DEV_DELETE;
2616 } else {
David Sedlák26ea1432019-08-14 13:42:23 +02002617 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
2618 "not-supported", "add", "replace", "delete");
David Sedlák4ffcec82019-07-25 15:10:21 +02002619 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2620 return LY_EVALID;
2621 }
2622 FREE_STRING(ctx->xml_ctx.ctx, temp_val);
2623
2624 if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2625 d = calloc(1, sizeof *d);
2626 LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2627 struct yin_subelement subelems[1] = {
2628 {YANG_CUSTOM, NULL, 0}
2629 };
2630 ret = yin_parse_content(ctx, subelems, 1, data, YANG_DEVIATE, NULL, &d->exts);
2631
2632 } else if (dev_mod == LYS_DEV_ADD) {
2633 d_add = calloc(1, sizeof *d_add);
2634 LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2635 d = (struct lysp_deviate *)d_add;
2636 struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2637 struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
2638 struct yin_subelement subelems[9] = {
2639 {YANG_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
2640 {YANG_DEFAULT, &d_add->dflts, 0},
2641 {YANG_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2642 {YANG_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2643 {YANG_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2644 {YANG_MUST, &d_add->musts, 0},
2645 {YANG_UNIQUE, &d_add->uniques, 0},
2646 {YANG_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2647 {YANG_CUSTOM, NULL, 0},
2648 };
2649 ret = yin_parse_content(ctx, subelems, 9, data, YANG_DEVIATE, NULL, &d_add->exts);
2650
2651 } else if (dev_mod == LYS_DEV_REPLACE) {
2652 d_rpl = calloc(1, sizeof *d_rpl);
2653 LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2654 d = (struct lysp_deviate *)d_rpl;
2655 struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2656 struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
2657 struct yin_subelement subelems[8] = {
2658 {YANG_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2659 {YANG_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
2660 {YANG_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2661 {YANG_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2662 {YANG_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2663 {YANG_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2664 {YANG_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2665 {YANG_CUSTOM, NULL, 0},
2666 };
2667 ret = yin_parse_content(ctx, subelems, 8, data, YANG_DEVIATE, NULL, &d_rpl->exts);
2668
2669 } else {
2670 d_del = calloc(1, sizeof *d_del);
2671 LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
2672 d = (struct lysp_deviate *)d_del;
2673 struct yin_subelement subelems[5] = {
2674 {YANG_DEFAULT, &d_del->dflts, 0},
2675 {YANG_MUST, &d_del->musts, 0},
2676 {YANG_UNIQUE, &d_del->uniques, 0},
2677 {YANG_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2678 {YANG_CUSTOM, NULL, 0},
2679 };
2680 ret = yin_parse_content(ctx, subelems, 5, data, YANG_DEVIATE, NULL, &d_del->exts);
2681 }
2682 LY_CHECK_GOTO(ret, cleanup);
2683
2684 d->mod = dev_mod;
2685 /* insert into siblings */
David Sedlák8d552d62019-08-06 15:29:05 +02002686 LY_LIST_INSERT(deviates, d, next);
David Sedlák4ffcec82019-07-25 15:10:21 +02002687
2688 return ret;
2689
2690cleanup:
2691 free(d);
David Sedlák4ffcec82019-07-25 15:10:21 +02002692 return ret;
2693}
2694
David Sedlák992fb7c2019-07-24 16:51:01 +02002695/**
David Sedlák8b754462019-07-25 16:22:13 +02002696 * @brief Parse deviation element.
2697 *
2698 * @param[in,out] ctx YIN parser context for logging and to store current state.
2699 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
2700 * @param[in,out] data Data to read from, always moved to currently handled character.
2701 * @param[in] deviations Deviations to add to.
2702 *
2703 * @return LY_ERR values.
2704 */
2705static LY_ERR
2706yin_parse_deviation(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
2707 struct lysp_deviation **deviations)
2708{
2709 struct lysp_deviation *dev;
2710
2711 /* create new deviation */
2712 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *deviations, dev, LY_EMEM);
2713
2714 /* parse argument */
David Sedlák1538a842019-08-08 15:38:51 +02002715 LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, YANG_DEVIATION));
David Sedlák8b754462019-07-25 16:22:13 +02002716 YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
2717 struct yin_subelement subelems[4] = {
2718 {YANG_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2719 {YANG_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2720 {YANG_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2721 {YANG_CUSTOM, NULL, 0},
2722 };
David Sedlák1538a842019-08-08 15:38:51 +02002723 return yin_parse_content(ctx, subelems, 4, data, YANG_DEVIATION, NULL, &dev->exts);
David Sedlák8b754462019-07-25 16:22:13 +02002724}
2725
2726/**
David Sedlákb4e44562019-07-04 15:42:12 +02002727 * @brief Map keyword type to substatement info.
2728 *
2729 * @param[in] kw Keyword type.
2730 *
2731 * @return correct LYEXT_SUBSTMT information.
2732 */
2733static LYEXT_SUBSTMT
2734kw2lyext_substmt(enum yang_keyword kw)
2735{
2736 switch (kw) {
2737 case YANG_ARGUMENT:
2738 return LYEXT_SUBSTMT_ARGUMENT;
2739 case YANG_BASE:
2740 return LYEXT_SUBSTMT_BASE;
2741 case YANG_BELONGS_TO:
2742 return LYEXT_SUBSTMT_BELONGSTO;
2743 case YANG_CONTACT:
2744 return LYEXT_SUBSTMT_CONTACT;
2745 case YANG_DEFAULT:
2746 return LYEXT_SUBSTMT_DEFAULT;
2747 case YANG_DESCRIPTION:
2748 return LYEXT_SUBSTMT_DESCRIPTION;
2749 case YANG_ERROR_APP_TAG:
2750 return LYEXT_SUBSTMT_ERRTAG;
2751 case YANG_ERROR_MESSAGE:
2752 return LYEXT_SUBSTMT_ERRMSG;
2753 case YANG_KEY:
2754 return LYEXT_SUBSTMT_KEY;
2755 case YANG_NAMESPACE:
2756 return LYEXT_SUBSTMT_NAMESPACE;
2757 case YANG_ORGANIZATION:
2758 return LYEXT_SUBSTMT_ORGANIZATION;
2759 case YANG_PATH:
2760 return LYEXT_SUBSTMT_PATH;
2761 case YANG_PREFIX:
2762 return LYEXT_SUBSTMT_PREFIX;
2763 case YANG_PRESENCE:
2764 return LYEXT_SUBSTMT_PRESENCE;
2765 case YANG_REFERENCE:
2766 return LYEXT_SUBSTMT_REFERENCE;
2767 case YANG_REVISION_DATE:
2768 return LYEXT_SUBSTMT_REVISIONDATE;
2769 case YANG_UNITS:
2770 return LYEXT_SUBSTMT_UNITS;
2771 case YANG_VALUE:
2772 return LYEXT_SUBSTMT_VALUE;
2773 case YANG_YANG_VERSION:
2774 return LYEXT_SUBSTMT_VERSION;
2775 case YANG_MODIFIER:
2776 return LYEXT_SUBSTMT_MODIFIER;
2777 case YANG_REQUIRE_INSTANCE:
2778 return LYEXT_SUBSTMT_REQINSTANCE;
2779 case YANG_YIN_ELEMENT:
2780 return LYEXT_SUBSTMT_YINELEM;
2781 case YANG_CONFIG:
2782 return LYEXT_SUBSTMT_CONFIG;
2783 case YANG_MANDATORY:
2784 return LYEXT_SUBSTMT_MANDATORY;
2785 case YANG_ORDERED_BY:
2786 return LYEXT_SUBSTMT_ORDEREDBY;
2787 case YANG_STATUS:
2788 return LYEXT_SUBSTMT_STATUS;
2789 case YANG_FRACTION_DIGITS:
2790 return LYEXT_SUBSTMT_FRACDIGITS;
2791 case YANG_MAX_ELEMENTS:
2792 return LYEXT_SUBSTMT_MAX;
2793 case YANG_MIN_ELEMENTS:
2794 return LYEXT_SUBSTMT_MIN;
2795 case YANG_POSITION:
2796 return LYEXT_SUBSTMT_POSITION;
2797 case YANG_UNIQUE:
2798 return LYEXT_SUBSTMT_UNIQUE;
2799 case YANG_IF_FEATURE:
2800 return LYEXT_SUBSTMT_IFFEATURE;
2801 default:
2802 return LYEXT_SUBSTMT_SELF;
2803 }
2804}
2805
David Sedlákc5b20842019-08-13 10:18:31 +02002806/**
2807 * @brief map keyword to keyword-group.
2808 *
2809 * @param[in] ctx YIN parser context used for logging.
2810 * @param[in] kw Keyword that is child of module or submodule.
2811 * @param[out] group Group of keyword.
2812 *
2813 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2814 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002815static LY_ERR
2816kw2kw_group(struct yin_parser_ctx *ctx, enum yang_keyword kw, enum yang_module_stmt *group)
2817{
2818 switch (kw) {
2819 /* module header */
2820 case YANG_NONE:
2821 case YANG_NAMESPACE:
2822 case YANG_PREFIX:
2823 case YANG_BELONGS_TO:
2824 case YANG_YANG_VERSION:
2825 *group = Y_MOD_MODULE_HEADER;
2826 break;
2827 /* linkage */
2828 case YANG_INCLUDE:
2829 case YANG_IMPORT:
2830 *group = Y_MOD_LINKAGE;
2831 break;
2832 /* meta */
2833 case YANG_ORGANIZATION:
2834 case YANG_CONTACT:
2835 case YANG_DESCRIPTION:
2836 case YANG_REFERENCE:
2837 *group = Y_MOD_META;
2838 break;
2839 /* revision */
2840 case YANG_REVISION:
2841 *group = Y_MOD_REVISION;
2842 break;
2843 /* body */
2844 case YANG_ANYDATA:
2845 case YANG_ANYXML:
2846 case YANG_AUGMENT:
2847 case YANG_CHOICE:
2848 case YANG_CONTAINER:
2849 case YANG_DEVIATION:
2850 case YANG_EXTENSION:
2851 case YANG_FEATURE:
2852 case YANG_GROUPING:
2853 case YANG_IDENTITY:
2854 case YANG_LEAF:
2855 case YANG_LEAF_LIST:
2856 case YANG_LIST:
2857 case YANG_NOTIFICATION:
2858 case YANG_RPC:
2859 case YANG_TYPEDEF:
2860 case YANG_USES:
2861 case YANG_CUSTOM:
2862 *group = Y_MOD_BODY;
2863 break;
2864 default:
2865 LOGINT(ctx->xml_ctx.ctx);
2866 return LY_EINT;
2867 }
2868
2869 return LY_SUCCESS;
2870}
2871
David Sedlákc5b20842019-08-13 10:18:31 +02002872/**
2873 * @brief Check if relative order of two keywords is valid.
2874 *
2875 * @param[in] ctx YIN parser context used for logging.
2876 * @param[in] kw Current keyword.
2877 * @param[in] next_kw Next keyword.
2878 * @param[in] parrent Identification of parrent element, can be se to to YANG_MODULE of YANG_SUBMODULE,
2879 * because relative order is required only in module and submodule sub-elements, used for logging.
2880 *
2881 * @return LY_SUCCESS on succes and LY_EVALID if relative order is invalid.
2882 */
David Sedláke6cd89e2019-08-07 12:46:02 +02002883static LY_ERR
2884yin_check_relative_order(struct yin_parser_ctx *ctx, enum yang_keyword kw, enum yang_keyword next_kw, enum yang_keyword parrent)
2885{
2886 assert(parrent == YANG_MODULE || parrent == YANG_SUBMODULE);
2887 enum yang_module_stmt gr, next_gr;
2888
2889 LY_CHECK_RET(kw2kw_group(ctx, kw, &gr));
2890 LY_CHECK_RET(kw2kw_group(ctx, next_kw, &next_gr));
2891
2892 if (gr > next_gr) {
2893 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INORDER_YIN, ly_stmt2str(parrent), ly_stmt2str(next_kw), ly_stmt2str(kw));
2894 return LY_EVALID;
2895 }
2896
2897 return LY_SUCCESS;
2898}
2899
David Sedlákd6e56892019-07-01 15:40:24 +02002900LY_ERR
David Sedlákda8ffa32019-07-08 14:17:10 +02002901yin_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 +02002902 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 +02002903{
2904 LY_ERR ret = LY_SUCCESS;
David Sedlák8e7bda82019-07-16 17:57:50 +02002905 char *out = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +02002906 const char *prefix, *name;
2907 size_t out_len = 0, prefix_len, name_len;
David Sedlák8e7bda82019-07-16 17:57:50 +02002908 int dynamic = 0;
David Sedlák1af868e2019-07-17 17:03:14 +02002909 struct yin_arg_record *attrs = NULL;
David Sedláke6cd89e2019-08-07 12:46:02 +02002910 enum yang_keyword kw = YANG_NONE, last_kw = YANG_NONE;
David Sedlák1af868e2019-07-17 17:03:14 +02002911 struct yin_subelement *subelem = NULL;
David Sedlák09e18c92019-07-18 11:17:11 +02002912
David Sedlákb0faad82019-07-04 14:28:59 +02002913 assert(is_ordered(subelem_info, subelem_info_size));
David Sedlákd6e56892019-07-01 15:40:24 +02002914
David Sedlákda8ffa32019-07-08 14:17:10 +02002915 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
2916 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákd6e56892019-07-01 15:40:24 +02002917 /* current element has subelements as content */
2918 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02002919 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákc5b20842019-08-13 10:18:31 +02002920 ret = lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
David Sedlákd6e56892019-07-01 15:40:24 +02002921 LY_CHECK_GOTO(ret, cleanup);
David Sedlákc5b20842019-08-13 10:18:31 +02002922 if (!name) {
David Sedlákd6e56892019-07-01 15:40:24 +02002923 /* end of current element reached */
2924 break;
2925 }
David Sedlák1af868e2019-07-17 17:03:14 +02002926 ret = yin_load_attributes(ctx, data, &attrs);
David Sedlákd6e56892019-07-01 15:40:24 +02002927 LY_CHECK_GOTO(ret, cleanup);
David Sedláke6cd89e2019-08-07 12:46:02 +02002928 last_kw = kw;
David Sedlákc5b20842019-08-13 10:18:31 +02002929 kw = yin_match_keyword(ctx, name, name_len, prefix, prefix_len, current_element);
David Sedlákd6e56892019-07-01 15:40:24 +02002930
2931 /* check if this element can be child of current element */
David Sedlák1af868e2019-07-17 17:03:14 +02002932 subelem = get_record(kw, subelem_info_size, subelem_info);
2933 if (!subelem) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002934 if (current_element == YANG_DEVIATE && isdevsub(kw)) {
2935 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INDEV_YIN, ly_stmt2str(kw));
2936 } else {
David Sedlákc5b20842019-08-13 10:18:31 +02002937 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, name_len, name, ly_stmt2str(current_element));
David Sedlák4ffcec82019-07-25 15:10:21 +02002938 }
David Sedlákd6e56892019-07-01 15:40:24 +02002939 ret = LY_EVALID;
2940 goto cleanup;
2941 }
2942
David Sedlák6542aed2019-08-14 10:47:43 +02002943 /* relative order is required only in module and submodule sub-elements */
David Sedláke6cd89e2019-08-07 12:46:02 +02002944 if (current_element == YANG_MODULE || current_element == YANG_SUBMODULE) {
2945 ret = yin_check_relative_order(ctx, last_kw, kw, current_element);
2946 LY_CHECK_GOTO(ret, cleanup);
2947 }
David Sedlák5f8191e2019-07-08 16:35:52 +02002948
David Sedlák4ffcec82019-07-25 15:10:21 +02002949 /* flag check */
David Sedlák1af868e2019-07-17 17:03:14 +02002950 if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002951 /* subelement uniquenes */
David Sedlák1538a842019-08-08 15:38:51 +02002952 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák21f87cd2019-07-03 16:53:23 +02002953 return LY_EVALID;
2954 }
David Sedlák1af868e2019-07-17 17:03:14 +02002955 if (subelem->flags & YIN_SUBELEM_FIRST) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002956 /* subelement is supposed to be defined as first subelement */
David Sedlák1af868e2019-07-17 17:03:14 +02002957 ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, current_element, subelem);
David Sedlák66d7c842019-07-11 15:06:04 +02002958 LY_CHECK_GOTO(ret, cleanup);
David Sedlák21f87cd2019-07-03 16:53:23 +02002959 }
David Sedlák0c2bab92019-07-22 15:33:19 +02002960 if (subelem->flags & YIN_SUBELEM_VER2) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002961 /* subelement is supported only in version 1.1 or higher */
David Sedlák0c2bab92019-07-22 15:33:19 +02002962 if (ctx->mod_version < 2) {
David Sedlák1538a842019-08-08 15:38:51 +02002963 LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
David Sedlák9bb1c042019-07-22 16:45:37 +02002964 ret = LY_EVALID;
2965 goto cleanup;
David Sedlák0c2bab92019-07-22 15:33:19 +02002966 }
2967 }
David Sedlák4ffcec82019-07-25 15:10:21 +02002968 /* note that element was parsed for easy uniqueness check in next iterations */
David Sedlák1af868e2019-07-17 17:03:14 +02002969 subelem->flags |= YIN_SUBELEM_PARSED;
David Sedlák21f87cd2019-07-03 16:53:23 +02002970
David Sedlákd6e56892019-07-01 15:40:24 +02002971 switch (kw) {
David Sedlák4ffcec82019-07-25 15:10:21 +02002972 /* call responsible function */
David Sedlákd6e56892019-07-01 15:40:24 +02002973 case YANG_CUSTOM:
David Sedlákc5b20842019-08-13 10:18:31 +02002974 ret = yin_parse_extension_instance(ctx, attrs, data, name2fullname(name, prefix_len),
2975 namelen2fulllen(name_len, prefix_len),
David Sedlák1af868e2019-07-17 17:03:14 +02002976 kw2lyext_substmt(current_element),
2977 (subelem->dest) ? *((uint32_t*)subelem->dest) : 0, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002978 break;
2979 case YANG_ACTION:
David Sedlákeaa45792019-07-24 15:25:01 +02002980 case YANG_RPC:
David Sedlák6881b512019-08-13 12:52:00 +02002981 ret = yin_parse_action(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002982 break;
2983 case YANG_ANYDATA:
David Sedlákd6e56892019-07-01 15:40:24 +02002984 case YANG_ANYXML:
David Sedlák8a83bbb2019-07-18 14:46:00 +02002985 ret = yin_parse_any(ctx, attrs, data, kw, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002986 break;
2987 case YANG_ARGUMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02002988 ret = yin_parse_argument_element(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002989 break;
2990 case YANG_AUGMENT:
David Sedlák6881b512019-08-13 12:52:00 +02002991 ret = yin_parse_augment(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02002992 break;
2993 case YANG_BASE:
David Sedlák6542aed2019-08-14 10:47:43 +02002994 ret = yin_parse_base(ctx, attrs, data, current_element, subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002995 break;
2996 case YANG_BELONGS_TO:
David Sedlák1af868e2019-07-17 17:03:14 +02002997 ret = yin_parse_belongs_to(ctx, attrs, data, (struct lysp_submodule *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02002998 break;
2999 case YANG_BIT:
David Sedlák43801c92019-08-05 15:58:54 +02003000 ret = yin_parse_bit(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003001 break;
3002 case YANG_CASE:
David Sedlák5379d392019-07-24 10:42:03 +02003003 ret = yin_parse_case(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003004 break;
3005 case YANG_CHOICE:
David Sedlákb7abcfa2019-07-24 12:33:35 +02003006 ret = yin_parse_choice(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003007 break;
3008 case YANG_CONFIG:
David Sedlák1af868e2019-07-17 17:03:14 +02003009 ret = yin_parse_config(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003010 break;
3011 case YANG_CONTACT:
David Sedlák619db942019-07-03 14:47:30 +02003012 case YANG_DESCRIPTION:
3013 case YANG_ORGANIZATION:
3014 case YANG_REFERENCE:
David Sedlákdf2a9732019-08-07 13:23:16 +02003015 ret = yin_parse_meta_element(ctx, attrs, data, kw, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003016 break;
3017 case YANG_CONTAINER:
David Sedlákf111bcb2019-07-23 17:15:51 +02003018 ret = yin_parse_container(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003019 break;
3020 case YANG_DEFAULT:
David Sedlák6542aed2019-08-14 10:47:43 +02003021 case YANG_ERROR_APP_TAG:
3022 case YANG_KEY:
3023 case YANG_PRESENCE:
3024 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003025 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003026 case YANG_DEVIATE:
David Sedlák4ffcec82019-07-25 15:10:21 +02003027 ret = yin_parse_deviate(ctx, attrs, data, (struct lysp_deviate **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003028 break;
3029 case YANG_DEVIATION:
David Sedlák8b754462019-07-25 16:22:13 +02003030 ret = yin_parse_deviation(ctx, attrs, data, (struct lysp_deviation **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003031 break;
David Sedlák43801c92019-08-05 15:58:54 +02003032 case YANG_ENUM:
3033 ret = yin_parse_enum(ctx, attrs, data, (struct lysp_type *)subelem->dest);
3034 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003035 case YANG_ERROR_MESSAGE:
David Sedlákdf2a9732019-08-07 13:23:16 +02003036 ret = yin_parse_err_msg_element(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003037 break;
3038 case YANG_EXTENSION:
David Sedlák1af868e2019-07-17 17:03:14 +02003039 ret = yin_parse_extension(ctx, attrs, data, (struct lysp_ext **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003040 break;
3041 case YANG_FEATURE:
David Sedlák5e13dea2019-07-22 16:06:45 +02003042 ret = yin_parse_feature(ctx, attrs, data, (struct lysp_feature **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003043 break;
3044 case YANG_FRACTION_DIGITS:
David Sedlák1af868e2019-07-17 17:03:14 +02003045 ret = yin_parse_fracdigits(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003046 break;
3047 case YANG_GROUPING:
David Sedlák6881b512019-08-13 12:52:00 +02003048 ret = yin_parse_grouping(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003049 break;
3050 case YANG_IDENTITY:
David Sedlák28794f22019-07-22 16:45:00 +02003051 ret = yin_parse_identity(ctx, attrs, data, (struct lysp_ident **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003052 break;
3053 case YANG_IF_FEATURE:
David Sedlák6542aed2019-08-14 10:47:43 +02003054 case YANG_UNITS:
3055 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003056 break;
3057 case YANG_IMPORT:
David Sedlák298ff6d2019-07-26 14:29:03 +02003058 ret = yin_parse_import(ctx, attrs, data, (struct import_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003059 break;
3060 case YANG_INCLUDE:
David Sedlák0c2bab92019-07-22 15:33:19 +02003061 ret = yin_parse_include(ctx, attrs, data, (struct include_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003062 break;
3063 case YANG_INPUT:
David Sedlák05404f62019-07-24 14:11:53 +02003064 case YANG_OUTPUT:
3065 ret = yin_parse_inout(ctx, attrs, data, kw, (struct inout_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003066 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003067 case YANG_LEAF:
David Sedlák203ca3a2019-07-18 15:26:25 +02003068 ret = yin_parse_leaf(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003069 break;
3070 case YANG_LEAF_LIST:
David Sedlákc3da3ef2019-07-19 12:56:08 +02003071 ret = yin_parse_leaflist(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003072 break;
3073 case YANG_LENGTH:
David Sedlák6542aed2019-08-14 10:47:43 +02003074 ret = yin_parse_length(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003075 break;
3076 case YANG_LIST:
David Sedlákaf536aa2019-07-23 13:42:23 +02003077 ret = yin_parse_list(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003078 break;
3079 case YANG_MANDATORY:
David Sedlák1af868e2019-07-17 17:03:14 +02003080 ret = yin_parse_mandatory(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003081 break;
3082 case YANG_MAX_ELEMENTS:
David Sedlákd6e56892019-07-01 15:40:24 +02003083 case YANG_MIN_ELEMENTS:
David Sedlák09e18c92019-07-18 11:17:11 +02003084 ret = yin_parse_minmax(ctx, attrs, data, current_element, kw, subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003085 break;
3086 case YANG_MODIFIER:
David Sedlák1af868e2019-07-17 17:03:14 +02003087 ret = yin_parse_modifier(ctx, attrs, data, (const char **)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003088 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003089 case YANG_MUST:
David Sedlák1af868e2019-07-17 17:03:14 +02003090 ret = yin_parse_must(ctx, attrs, data, (struct lysp_restr **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003091 break;
3092 case YANG_NAMESPACE:
David Sedlák6542aed2019-08-14 10:47:43 +02003093 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003094 break;
3095 case YANG_NOTIFICATION:
David Sedlák6881b512019-08-13 12:52:00 +02003096 ret = yin_parse_notification(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003097 break;
3098 case YANG_ORDERED_BY:
David Sedláka2dad212019-07-18 12:45:19 +02003099 ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003100 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003101 case YANG_PATH:
David Sedlák6542aed2019-08-14 10:47:43 +02003102 ret = yin_parse_path(ctx, attrs, data, kw, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003103 break;
3104 case YANG_PATTERN:
David Sedlák1af868e2019-07-17 17:03:14 +02003105 ret = yin_parse_pattern(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003106 break;
David Sedlák5545f5d2019-07-11 11:55:16 +02003107 case YANG_VALUE:
David Sedlákd6e56892019-07-01 15:40:24 +02003108 case YANG_POSITION:
David Sedlák6542aed2019-08-14 10:47:43 +02003109 ret = yin_parse_value_pos_element(ctx, attrs, data, kw, (struct lysp_type_enum *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003110 break;
3111 case YANG_PREFIX:
David Sedlák6542aed2019-08-14 10:47:43 +02003112 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_VALUE, Y_IDENTIF_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003113 break;
3114 case YANG_RANGE:
David Sedlák6542aed2019-08-14 10:47:43 +02003115 ret = yin_parse_range(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003116 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003117 case YANG_REFINE:
David Sedlákd2d676a2019-07-22 11:28:19 +02003118 ret = yin_parse_refine(ctx, attrs, data, (struct lysp_refine **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003119 break;
3120 case YANG_REQUIRE_INSTANCE:
David Sedlák1af868e2019-07-17 17:03:14 +02003121 ret = yin_pasrse_reqinstance(ctx, attrs, data, (struct lysp_type *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003122 break;
3123 case YANG_REVISION:
David Sedlákaa854b02019-07-22 14:17:10 +02003124 ret = yin_parse_revision(ctx, attrs, data, (struct lysp_revision **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003125 break;
3126 case YANG_REVISION_DATE:
David Sedlák1af868e2019-07-17 17:03:14 +02003127 ret = yin_parse_revision_date(ctx, attrs, data, (char *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003128 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003129 case YANG_STATUS:
David Sedlák1af868e2019-07-17 17:03:14 +02003130 ret = yin_parse_status(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003131 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003132 case YANG_TYPE:
David Sedlák6542aed2019-08-14 10:47:43 +02003133 ret = yin_parse_type(ctx, attrs, data, current_element, subelem);
David Sedlákd6e56892019-07-01 15:40:24 +02003134 break;
3135 case YANG_TYPEDEF:
David Sedlák6881b512019-08-13 12:52:00 +02003136 ret = yin_parse_typedef(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003137 break;
3138 case YANG_UNIQUE:
David Sedlák6542aed2019-08-14 10:47:43 +02003139 ret = yin_parse_simple_elem(ctx, attrs, data, kw, subelem, YIN_ARG_TAG, Y_STR_ARG, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003140 break;
3141 case YANG_USES:
David Sedlák0d6de5a2019-07-22 13:25:44 +02003142 ret = yin_parse_uses(ctx, attrs, data, (struct tree_node_meta *)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003143 break;
David Sedlákd6e56892019-07-01 15:40:24 +02003144 case YANG_WHEN:
David Sedlák1af868e2019-07-17 17:03:14 +02003145 ret = yin_parse_when(ctx, attrs, data, (struct lysp_when **)subelem->dest);
David Sedlákd6e56892019-07-01 15:40:24 +02003146 break;
3147 case YANG_YANG_VERSION:
David Sedlák1af868e2019-07-17 17:03:14 +02003148 ret = yin_parse_yangversion(ctx, attrs, data, (uint8_t *)subelem->dest, exts);
David Sedlákd6e56892019-07-01 15:40:24 +02003149 break;
3150 case YANG_YIN_ELEMENT:
David Sedlák1af868e2019-07-17 17:03:14 +02003151 ret = yin_parse_yin_element_element(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
David Sedlák3ffbc522019-07-02 17:49:28 +02003152 break;
3153 case YIN_TEXT:
David Sedlák3ffbc522019-07-02 17:49:28 +02003154 case YIN_VALUE:
David Sedlák1af868e2019-07-17 17:03:14 +02003155 ret = yin_parse_content(ctx, NULL, 0, data, kw, (const char **)subelem->dest, NULL);
David Sedlákd6e56892019-07-01 15:40:24 +02003156 break;
3157 default:
David Sedlákda8ffa32019-07-08 14:17:10 +02003158 LOGINT(ctx->xml_ctx.ctx);
David Sedlák21f87cd2019-07-03 16:53:23 +02003159 return LY_EINT;
David Sedlákd6e56892019-07-01 15:40:24 +02003160 }
David Sedlák3ffbc522019-07-02 17:49:28 +02003161 LY_CHECK_GOTO(ret, cleanup);
David Sedlák1af868e2019-07-17 17:03:14 +02003162 FREE_ARRAY(ctx, attrs, free_arg_rec);
3163 attrs = NULL;
3164 subelem = NULL;
David Sedlákd6e56892019-07-01 15:40:24 +02003165 }
3166 } else {
3167 /* elements with text or none content */
David Sedlák3ffbc522019-07-02 17:49:28 +02003168 /* save text content, if text_content isn't set, it's just ignored */
David Sedlákc5b20842019-08-13 10:18:31 +02003169 /* no resources are allocated in this branch, no need to use cleanup label */
David Sedlák3b4df842019-07-17 11:39:46 +02003170 LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG, out, out_len));
David Sedlák3ffbc522019-07-02 17:49:28 +02003171 if (text_content) {
3172 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003173 *text_content = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák3ffbc522019-07-02 17:49:28 +02003174 if (!*text_content) {
3175 free(out);
3176 return LY_EMEM;
3177 }
3178 } else {
3179 if (out_len == 0) {
David Sedlák99295322019-07-17 11:34:18 +02003180 *text_content = lydict_insert(ctx->xml_ctx.ctx, "", 0);
David Sedlák3ffbc522019-07-02 17:49:28 +02003181 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003182 *text_content = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák3ffbc522019-07-02 17:49:28 +02003183 }
3184 }
3185 }
David Sedlákd6e56892019-07-01 15:40:24 +02003186 /* load closing element */
David Sedlákc5b20842019-08-13 10:18:31 +02003187 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákd6e56892019-07-01 15:40:24 +02003188 }
3189 }
David Sedlák8b754462019-07-25 16:22:13 +02003190 /* mandatory subelemnts are checked only after whole element was succesfully parsed */
3191 LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
David Sedlákd6e56892019-07-01 15:40:24 +02003192
3193cleanup:
David Sedlák1af868e2019-07-17 17:03:14 +02003194 FREE_ARRAY(ctx, attrs, free_arg_rec);
David Sedlákd6e56892019-07-01 15:40:24 +02003195 return ret;
3196}
3197
David Sedlák619db942019-07-03 14:47:30 +02003198LY_ERR
David Sedlák1f90d252019-07-10 17:09:32 +02003199yin_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 +02003200 int ext_name_len, LYEXT_SUBSTMT subelem, uint32_t subelem_index, struct lysp_ext_instance **exts)
David Sedlák1e2cdd02019-06-27 14:17:43 +02003201{
3202 LY_ERR ret = LY_SUCCESS;
David Sedlákb1a78352019-06-28 16:16:29 +02003203 char *out;
3204 const char *name, *prefix;
3205 size_t out_len, prefix_len, name_len;
3206 int dynamic;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003207 struct lysp_ext_instance *e;
David Sedlákb1a78352019-06-28 16:16:29 +02003208 struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
3209 struct yin_arg_record *iter;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003210
David Sedlákda8ffa32019-07-08 14:17:10 +02003211 LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *exts, e, LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003212
3213 e->yin = 0;
3214 /* store name and insubstmt info */
David Sedlákda8ffa32019-07-08 14:17:10 +02003215 e->name = lydict_insert(ctx->xml_ctx.ctx, ext_name, ext_name_len);
David Sedlák619db942019-07-03 14:47:30 +02003216 e->insubstmt = subelem;
3217 e->insubstmt_index = subelem_index;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003218 e->yin |= LYS_YIN;
3219
David Sedlákb1a78352019-06-28 16:16:29 +02003220 /* store attributes as subelements */
David Sedlák1f90d252019-07-10 17:09:32 +02003221 LY_ARRAY_FOR_ITER(attrs, struct yin_arg_record, iter) {
David Sedlákb1a78352019-06-28 16:16:29 +02003222 if (!iter->prefix) {
3223 new_subelem = calloc(1, sizeof(*new_subelem));
3224 if (!e->child) {
3225 e->child = new_subelem;
David Sedlák1e2cdd02019-06-27 14:17:43 +02003226 } else {
David Sedlákb1a78352019-06-28 16:16:29 +02003227 last_subelem->next = new_subelem;
3228 }
3229 last_subelem = new_subelem;
3230
3231 last_subelem->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02003232 last_subelem->stmt = lydict_insert(ctx->xml_ctx.ctx, iter->name, iter->name_len);
3233 LY_CHECK_ERR_RET(!last_subelem->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02003234 if (iter->dynamic_content) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003235 last_subelem->arg = lydict_insert_zc(ctx->xml_ctx.ctx, iter->content);
3236 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02003237 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003238 last_subelem->arg = lydict_insert(ctx->xml_ctx.ctx, iter->content, iter->content_len);
3239 LY_CHECK_ERR_RET(!last_subelem->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003240 }
3241 }
3242 }
David Sedlák1e2cdd02019-06-27 14:17:43 +02003243
David Sedlákf250ecf2019-07-01 11:02:05 +02003244 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02003245 if (ctx->xml_ctx.status == LYXML_ELEM_CONTENT) {
3246 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003247 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003248 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
3249 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlákb1a78352019-06-28 16:16:29 +02003250 if (!name) {
3251 /* end of extension instance reached */
3252 break;
3253 }
David Sedlák4ffcec82019-07-25 15:10:21 +02003254 LY_CHECK_RET(yin_parse_element_generic(ctx, name, name_len, data, &new_subelem));
David Sedlákb1a78352019-06-28 16:16:29 +02003255 if (!e->child) {
3256 e->child = new_subelem;
3257 } else {
3258 last_subelem->next = new_subelem;
3259 }
3260 last_subelem = new_subelem;
3261 }
David Sedlák555c7202019-07-04 12:14:12 +02003262 } else {
3263 /* save text content */
3264 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003265 e->argument = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák555c7202019-07-04 12:14:12 +02003266 if (!e->argument) {
3267 free(out);
3268 return LY_EMEM;
3269 }
3270 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003271 e->argument = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
David Sedlák555c7202019-07-04 12:14:12 +02003272 LY_CHECK_RET(!e->argument, LY_EMEM);
3273 }
David Sedlákda8ffa32019-07-08 14:17:10 +02003274 LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák555c7202019-07-04 12:14:12 +02003275 LY_CHECK_RET(name, LY_EINT);
David Sedlákb1a78352019-06-28 16:16:29 +02003276 }
David Sedlákb1a78352019-06-28 16:16:29 +02003277 }
3278
3279 return LY_SUCCESS;
3280}
3281
3282LY_ERR
David Sedlák4ffcec82019-07-25 15:10:21 +02003283yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char **data,
3284 struct lysp_stmt **element)
David Sedlákb1a78352019-06-28 16:16:29 +02003285{
3286 LY_ERR ret = LY_SUCCESS;
3287 const char *temp_prefix, *temp_name;
3288 char *out = NULL;
David Sedlák4ffcec82019-07-25 15:10:21 +02003289 size_t out_len, temp_name_len, temp_prefix_len, prefix_len;
David Sedlákb1a78352019-06-28 16:16:29 +02003290 int dynamic;
3291 struct yin_arg_record *subelem_args = NULL;
3292 struct lysp_stmt *last = NULL, *new = NULL;
3293
3294 /* allocate new structure for element */
3295 *element = calloc(1, sizeof(**element));
David Sedlákda8ffa32019-07-08 14:17:10 +02003296 (*element)->stmt = lydict_insert(ctx->xml_ctx.ctx, name, name_len);
3297 LY_CHECK_ERR_RET(!(*element)->stmt, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
David Sedlákb1a78352019-06-28 16:16:29 +02003298
3299 last = (*element)->child;
David Sedlákf250ecf2019-07-01 11:02:05 +02003300 /* load attributes */
David Sedlákda8ffa32019-07-08 14:17:10 +02003301 while(ctx->xml_ctx.status == LYXML_ATTRIBUTE) {
David Sedlákb1a78352019-06-28 16:16:29 +02003302 /* add new element to linked-list */
3303 new = calloc(1, sizeof(*last));
David Sedlákda8ffa32019-07-08 14:17:10 +02003304 LY_CHECK_ERR_GOTO(ret, LOGMEM(ctx->xml_ctx.ctx), err);
David Sedlákb1a78352019-06-28 16:16:29 +02003305 if (!(*element)->child) {
3306 /* save first */
3307 (*element)->child = new;
3308 } else {
3309 last->next = new;
3310 }
3311 last = new;
3312
3313 last->flags |= LYS_YIN_ATTR;
David Sedlákda8ffa32019-07-08 14:17:10 +02003314 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 +02003315 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02003316 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003317 LY_CHECK_GOTO(ret, err);
David Sedlákda8ffa32019-07-08 14:17:10 +02003318 last->stmt = lydict_insert(ctx->xml_ctx.ctx, temp_name, temp_name_len);
3319 LY_CHECK_ERR_GOTO(!last->stmt, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02003320 /* attributes with prefix are ignored */
3321 if (!temp_prefix) {
3322 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003323 last->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlákb1a78352019-06-28 16:16:29 +02003324 if (!last->arg) {
3325 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02003326 LOGMEM(ctx->xml_ctx.ctx);
David Sedlákb1a78352019-06-28 16:16:29 +02003327 ret = LY_EMEM;
3328 goto err;
3329 }
3330 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003331 last->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
3332 LY_CHECK_ERR_GOTO(!last->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02003333 }
3334 }
3335 }
3336
3337 /* parse content of element */
David Sedlákda8ffa32019-07-08 14:17:10 +02003338 ret = lyxml_get_string(&ctx->xml_ctx, data, &out, &out_len, &out, &out_len, &dynamic);
David Sedlákb1a78352019-06-28 16:16:29 +02003339 if (ret == LY_EINVAL) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003340 while (ctx->xml_ctx.status == LYXML_ELEMENT) {
David Sedlákb1a78352019-06-28 16:16:29 +02003341 /* parse subelements */
David Sedlákda8ffa32019-07-08 14:17:10 +02003342 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 +02003343 LY_CHECK_GOTO(ret, err);
3344 if (!name) {
3345 /* end of element reached */
3346 break;
3347 }
David Sedlák4ffcec82019-07-25 15:10:21 +02003348 ret = yin_parse_element_generic(ctx, temp_name, temp_name_len, data, &last->next);
David Sedlákb1a78352019-06-28 16:16:29 +02003349 LY_CHECK_GOTO(ret, err);
3350 last = last->next;
3351 }
3352 } else {
3353 /* save element content */
David Sedlák5392a212019-07-01 09:19:10 +02003354 if (out_len != 0) {
3355 if (dynamic) {
David Sedlákda8ffa32019-07-08 14:17:10 +02003356 (*element)->arg = lydict_insert_zc(ctx->xml_ctx.ctx, out);
David Sedlák5392a212019-07-01 09:19:10 +02003357 if (!(*element)->arg) {
3358 free(out);
David Sedlákda8ffa32019-07-08 14:17:10 +02003359 LOGMEM(ctx->xml_ctx.ctx);
David Sedlák5392a212019-07-01 09:19:10 +02003360 ret = LY_EMEM;
3361 goto err;
3362 }
3363 } else {
David Sedlákda8ffa32019-07-08 14:17:10 +02003364 (*element)->arg = lydict_insert(ctx->xml_ctx.ctx, out, out_len);
3365 LY_CHECK_ERR_GOTO(!(*element)->arg, LOGMEM(ctx->xml_ctx.ctx); ret = LY_EMEM, err);
David Sedlákb1a78352019-06-28 16:16:29 +02003366 }
David Sedlákb1a78352019-06-28 16:16:29 +02003367 }
3368 /* read closing tag */
David Sedlákda8ffa32019-07-08 14:17:10 +02003369 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 +02003370 LY_CHECK_GOTO(ret, err);
3371 }
3372
David Sedlákda8ffa32019-07-08 14:17:10 +02003373 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlákb1a78352019-06-28 16:16:29 +02003374 return LY_SUCCESS;
3375
3376err:
David Sedlákda8ffa32019-07-08 14:17:10 +02003377 FREE_ARRAY(ctx, subelem_args, free_arg_rec);
David Sedlák1e2cdd02019-06-27 14:17:43 +02003378 return ret;
3379}
3380
3381LY_ERR
David Sedlák4f03b932019-07-26 13:01:47 +02003382yin_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 +02003383{
David Sedlák81497a32019-08-13 16:56:26 +02003384 LY_ERR ret = LY_SUCCESS;
3385 struct yin_subelement *subelems = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003386
David Sedlák81497a32019-08-13 16:56:26 +02003387 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, YANG_MODULE));
3388 LY_CHECK_RET(subelems_allocator(ctx, 28, NULL, &subelems,
3389 YANG_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3390 YANG_ANYXML, &mod->data, 0,
3391 YANG_AUGMENT, &mod->augments, 0,
3392 YANG_CHOICE, &mod->data, 0,
3393 YANG_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3394 YANG_CONTAINER, &mod->data, 0,
3395 YANG_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3396 YANG_DEVIATION, &mod->deviations, 0,
3397 YANG_EXTENSION, &mod->extensions, 0,
3398 YANG_FEATURE, &mod->features, 0,
3399 YANG_GROUPING, &mod->groupings, 0,
3400 YANG_IDENTITY, &mod->identities, 0,
3401 YANG_IMPORT, mod->mod->prefix, &mod->imports, 0,
3402 YANG_INCLUDE, mod->mod->name, &mod->includes, 0,
3403 YANG_LEAF, &mod->data, 0,
3404 YANG_LEAF_LIST, &mod->data, 0,
3405 YANG_LIST, &mod->data, 0,
3406 YANG_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3407 YANG_NOTIFICATION, &mod->notifs, 0,
3408 YANG_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3409 YANG_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3410 YANG_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3411 YANG_REVISION, &mod->revs, 0,
3412 YANG_RPC, &mod->rpcs, 0,
3413 YANG_TYPEDEF, &mod->typedefs, 0,
3414 YANG_USES, &mod->data, 0,
3415 YANG_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3416 YANG_CUSTOM, NULL, 0
3417 ));
3418
3419 ret = yin_parse_content(ctx, subelems, 28, data, YANG_MODULE, NULL, &mod->exts);
3420 subelems_deallocator(28, subelems);
3421
3422 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003423}
3424
3425LY_ERR
David Sedlák298ff6d2019-07-26 14:29:03 +02003426yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs, const char **data, struct lysp_submodule *submod)
3427{
David Sedlák81497a32019-08-13 16:56:26 +02003428 LY_ERR ret = LY_SUCCESS;
3429 struct yin_subelement *subelems = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02003430
David Sedlák81497a32019-08-13 16:56:26 +02003431 LY_CHECK_RET(yin_parse_attribute(ctx, mod_attrs, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, YANG_SUBMODULE));
3432 LY_CHECK_RET(subelems_allocator(ctx, 27, NULL, &subelems,
3433 YANG_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3434 YANG_ANYXML, &submod->data, 0,
3435 YANG_AUGMENT, &submod->augments, 0,
3436 YANG_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3437 YANG_CHOICE, &submod->data, 0,
3438 YANG_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3439 YANG_CONTAINER, &submod->data, 0,
3440 YANG_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3441 YANG_DEVIATION, &submod->deviations, 0,
3442 YANG_EXTENSION, &submod->extensions, 0,
3443 YANG_FEATURE, &submod->features, 0,
3444 YANG_GROUPING, &submod->groupings, 0,
3445 YANG_IDENTITY, &submod->identities, 0,
3446 YANG_IMPORT, submod->prefix, &submod->imports, 0,
3447 YANG_INCLUDE, submod->name, &submod->includes, 0,
3448 YANG_LEAF, &submod->data, 0,
3449 YANG_LEAF_LIST, &submod->data, 0,
3450 YANG_LIST, &submod->data, 0,
3451 YANG_NOTIFICATION, &submod->notifs, 0,
3452 YANG_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3453 YANG_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3454 YANG_REVISION, &submod->revs, 0,
3455 YANG_RPC, &submod->rpcs, 0,
3456 YANG_TYPEDEF, &submod->typedefs, 0,
3457 YANG_USES, &submod->data, 0,
3458 YANG_YANG_VERSION, &submod->version, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3459 YANG_CUSTOM, NULL, 0
3460 ));
3461
3462 ret = yin_parse_content(ctx, subelems, 27, data, YANG_SUBMODULE, NULL, &submod->exts);
3463 subelems_deallocator(27, subelems);
3464
3465 return ret;
David Sedlák298ff6d2019-07-26 14:29:03 +02003466}
3467
3468LY_ERR
David Sedlák1b623122019-08-05 15:27:49 +02003469yin_parse_submodule(struct yin_parser_ctx **yin_ctx, struct ly_ctx *ctx, struct lys_parser_ctx *main_ctx, const char *data, struct lysp_submodule **submod)
David Sedlák8985a142019-07-31 16:43:06 +02003470{
3471 enum yang_keyword kw = YANG_NONE;
3472 LY_ERR ret = LY_SUCCESS;
3473 const char *prefix, *name;
3474 size_t prefix_len, name_len;
3475 struct yin_arg_record *attrs = NULL;
3476 struct lysp_submodule *mod_p = NULL;
3477
3478 /* create context */
3479 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003480 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
David Sedlák8985a142019-07-31 16:43:06 +02003481 (*yin_ctx)->xml_ctx.ctx = ctx;
3482 (*yin_ctx)->xml_ctx.line = 1;
3483
David Sedlák1b623122019-08-05 15:27:49 +02003484 /* map the typedefs and groupings list from main context to the submodule's context */
3485 memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
3486 memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
3487
David Sedlák8985a142019-07-31 16:43:06 +02003488 /* check submodule */
3489 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3490 LY_CHECK_GOTO(ret, cleanup);
3491 ret = yin_load_attributes(*yin_ctx, &data, &attrs);
3492 LY_CHECK_GOTO(ret, cleanup);
3493 kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
3494
3495 if (kw == YANG_MODULE) {
3496 LOGERR(ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
3497 ret = LY_EINVAL;
3498 goto cleanup;
3499 } else if (kw != YANG_SUBMODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003500 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák8985a142019-07-31 16:43:06 +02003501 ret = LY_EVALID;
3502 goto cleanup;
3503 }
3504
3505 mod_p = calloc(1, sizeof *mod_p);
3506 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx), cleanup);
3507 mod_p->parsing = 1;
3508
3509 ret = yin_parse_submod(*yin_ctx, attrs, &data, mod_p);
3510 LY_CHECK_GOTO(ret, cleanup);
3511
David Sedlák6d781b62019-08-02 15:22:52 +02003512 name = NULL;
3513 if ((*yin_ctx)->xml_ctx.status == LYXML_ELEMENT) {
3514 const char *temp_data = data;
3515 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3516 data = temp_data;
3517 }
3518 if ((*yin_ctx)->xml_ctx.status != LYXML_END || name) {
David Sedlák1538a842019-08-08 15:38:51 +02003519 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, data, strlen(data) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003520 ret = LY_EVALID;
3521 goto cleanup;
3522 }
3523
David Sedlák8985a142019-07-31 16:43:06 +02003524 mod_p->parsing = 0;
3525 *submod = mod_p;
3526
3527cleanup:
3528 if (ret) {
3529 lysp_submodule_free(ctx, mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003530 yin_parser_ctx_free(*yin_ctx);
3531 *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003532 }
3533
3534 FREE_ARRAY(*yin_ctx, attrs, free_arg_rec);
3535 return ret;
3536}
3537
3538LY_ERR
3539yin_parse_module(struct yin_parser_ctx **yin_ctx, const char *data, struct lys_module *mod)
David Sedlák3b4db242018-10-19 16:11:01 +02003540{
David Sedláke4889912018-11-02 09:52:40 +01003541 LY_ERR ret = LY_SUCCESS;
3542 enum yang_keyword kw = YANG_NONE;
David Sedlák3017da42019-02-15 09:48:04 +01003543 struct lysp_module *mod_p = NULL;
3544 const char *prefix, *name;
3545 size_t prefix_len, name_len;
David Sedlák1f90d252019-07-10 17:09:32 +02003546 struct yin_arg_record *attrs = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +02003547
David Sedlák8985a142019-07-31 16:43:06 +02003548 /* create context */
3549 *yin_ctx = calloc(1, sizeof **yin_ctx);
David Sedlák1b623122019-08-05 15:27:49 +02003550 LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
David Sedlák8985a142019-07-31 16:43:06 +02003551 (*yin_ctx)->xml_ctx.ctx = mod->ctx;
3552 (*yin_ctx)->xml_ctx.line = 1;
David Sedlákda8ffa32019-07-08 14:17:10 +02003553
David Sedlák8985a142019-07-31 16:43:06 +02003554 /* check module */
3555 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák00250342019-06-21 14:19:39 +02003556 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8985a142019-07-31 16:43:06 +02003557 ret = yin_load_attributes(*yin_ctx, &data, &attrs);
David Sedlák00250342019-06-21 14:19:39 +02003558 LY_CHECK_GOTO(ret, cleanup);
David Sedlák8985a142019-07-31 16:43:06 +02003559 kw = yin_match_keyword(*yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE);
David Sedláke4889912018-11-02 09:52:40 +01003560 if (kw == YANG_SUBMODULE) {
David Sedlák8985a142019-07-31 16:43:06 +02003561 LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
David Sedlák3017da42019-02-15 09:48:04 +01003562 ret = LY_EINVAL;
3563 goto cleanup;
3564 } else if (kw != YANG_MODULE) {
David Sedlák1538a842019-08-08 15:38:51 +02003565 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, ly_stmt2str(kw));
David Sedlák3017da42019-02-15 09:48:04 +01003566 ret = LY_EVALID;
3567 goto cleanup;
David Sedláke4889912018-11-02 09:52:40 +01003568 }
3569
David Sedlák3017da42019-02-15 09:48:04 +01003570 /* allocate module */
3571 mod_p = calloc(1, sizeof *mod_p);
David Sedlák8985a142019-07-31 16:43:06 +02003572 LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
David Sedlák3017da42019-02-15 09:48:04 +01003573 mod_p->mod = mod;
3574 mod_p->parsing = 1;
David Sedláke4889912018-11-02 09:52:40 +01003575
David Sedlák00250342019-06-21 14:19:39 +02003576 /* parse module substatements */
David Sedlák8985a142019-07-31 16:43:06 +02003577 ret = yin_parse_mod(*yin_ctx, attrs, &data, mod_p);
David Sedlák3017da42019-02-15 09:48:04 +01003578 LY_CHECK_GOTO(ret, cleanup);
David Sedlák2e411422018-12-17 02:35:39 +01003579
David Sedlák1b623122019-08-05 15:27:49 +02003580 /* check trailing characters */
David Sedlák6d781b62019-08-02 15:22:52 +02003581 if ((*yin_ctx)->xml_ctx.status == LYXML_ELEMENT) {
3582 ret = lyxml_get_element(&(*yin_ctx)->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
3583 }
3584 if ((*yin_ctx)->xml_ctx.status != LYXML_END || name) {
David Sedlák1538a842019-08-08 15:38:51 +02003585 LOGVAL_PARSER((struct lys_parser_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, data, strlen(data) > 15 ? "..." : "");
David Sedlák6d781b62019-08-02 15:22:52 +02003586
3587 ret = LY_EVALID;
3588 goto cleanup;
3589 }
3590
David Sedlák3017da42019-02-15 09:48:04 +01003591 mod_p->parsing = 0;
3592 mod->parsed = mod_p;
3593
3594cleanup:
David Sedlák8f7a1172019-06-20 14:42:18 +02003595 if (ret != LY_SUCCESS) {
David Sedlák3017da42019-02-15 09:48:04 +01003596 lysp_module_free(mod_p);
David Sedlák1b623122019-08-05 15:27:49 +02003597 yin_parser_ctx_free(*yin_ctx);
3598 *yin_ctx = NULL;
David Sedlák3017da42019-02-15 09:48:04 +01003599 }
David Sedlák8985a142019-07-31 16:43:06 +02003600 FREE_ARRAY(*yin_ctx, attrs, free_arg_rec);
David Sedlák2e411422018-12-17 02:35:39 +01003601 return ret;
David Sedlák3b4db242018-10-19 16:11:01 +02003602}