Coverage Report

Created: 2025-10-27 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyang/src/parser_yin.c
Line
Count
Source
1
/**
2
 * @file parser_yin.c
3
 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
4
 * @author Michal Vasko <mvasko@cesnet.cz>
5
 * @brief YIN parser.
6
 *
7
 * Copyright (c) 2015 - 2024 CESNET, z.s.p.o.
8
 *
9
 * This source code is licensed under BSD 3-Clause License (the "License").
10
 * You may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     https://opensource.org/licenses/BSD-3-Clause
14
 */
15
#define _GNU_SOURCE
16
17
#include <assert.h>
18
#include <ctype.h>
19
#include <errno.h>
20
#include <stdarg.h>
21
#include <stdbool.h>
22
#include <stdint.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "compat.h"
28
#include "context.h"
29
#include "dict.h"
30
#include "in.h"
31
#include "in_internal.h"
32
#include "log.h"
33
#include "ly_common.h"
34
#include "parser_internal.h"
35
#include "parser_schema.h"
36
#include "path.h"
37
#include "set.h"
38
#include "tree.h"
39
#include "tree_data_internal.h"
40
#include "tree_edit.h"
41
#include "tree_schema.h"
42
#include "tree_schema_internal.h"
43
#include "xml.h"
44
45
struct lys_glob_unres;
46
47
/**
48
 * @brief Insert string into dictionary.
49
 *
50
 * @param[in] CTX libyang context.
51
 * @param[in] STRING string to store.
52
 * @param[in] LEN length of the string in WORD to store.
53
 * @param[in,out] DYNAMIC Set to 1 if @p STRING is dynamically allocated, 0 otherwise.
54
 * If set to 1, zerocopy version of lysdict_insert is used.
55
 * @param[out] TARGET pointer is set to @p STRING value stored in the dictionary.
56
 */
57
#define INSERT_STRING_RET(CTX, STRING, LEN, DYNAMIC, TARGET) \
58
0
    if (DYNAMIC) { \
59
0
        LY_CHECK_RET(lysdict_insert_zc(CTX, (char *)(STRING), &(TARGET))); \
60
0
    } else { \
61
0
        LY_CHECK_RET(lysdict_insert(CTX, LEN ? (STRING) : "", LEN, &(TARGET))); \
62
0
    } \
63
0
    DYNAMIC = 0
64
65
/**
66
 * @brief check if given string is URI of yin namespace.
67
 *
68
 * @param ns Namespace URI to check.
69
 *
70
 * @return true if ns equals YIN_NS_URI false otherwise.
71
 */
72
0
#define IS_YIN_NS(ns) (strcmp(ns, YIN_NS_URI) == 0)
73
74
enum yin_argument {
75
    YIN_ARG_UNKNOWN = 0,   /**< parsed argument can not be matched with any supported yin argument keyword */
76
    YIN_ARG_NAME,          /**< argument name */
77
    YIN_ARG_TARGET_NODE,   /**< argument target-node */
78
    YIN_ARG_MODULE,        /**< argument module */
79
    YIN_ARG_VALUE,         /**< argument value */
80
    YIN_ARG_TEXT,          /**< argument text */
81
    YIN_ARG_CONDITION,     /**< argument condition */
82
    YIN_ARG_URI,           /**< argument uri */
83
    YIN_ARG_DATE,          /**< argument data */
84
    YIN_ARG_TAG,           /**< argument tag */
85
    YIN_ARG_NONE           /**< empty (special value) */
86
};
87
88
const char * const yin_attr_list[] = {
89
    [YIN_ARG_NAME] = "name",
90
    [YIN_ARG_TARGET_NODE] = "target-node",
91
    [YIN_ARG_MODULE] = "module",
92
    [YIN_ARG_VALUE] = "value",
93
    [YIN_ARG_TEXT] = "text",
94
    [YIN_ARG_CONDITION] = "condition",
95
    [YIN_ARG_URI] = "uri",
96
    [YIN_ARG_DATE] = "date",
97
    [YIN_ARG_TAG] = "tag",
98
    [YIN_ARG_NONE] = "none",
99
};
100
101
#define yin_attr2str(STMT) yin_attr_list[STMT]
102
103
#define VALID_VALS1 " Only valid value is \"%s\"."
104
#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
105
#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
106
#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
107
108
/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
109
0
#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
110
0
                      kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS ||              \
111
0
                      kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE ||         \
112
0
                      kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
113
114
/* flags to set constraints of subelements */
115
0
#define YIN_SUBELEM_MANDATORY   0x01    /**< is set when subelement is mandatory */
116
0
#define YIN_SUBELEM_UNIQUE      0x02    /**< is set when subelement is unique */
117
0
#define YIN_SUBELEM_FIRST       0x04    /**< is set when subelement is actually yang argument mapped to yin element */
118
0
#define YIN_SUBELEM_VER2        0x08    /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
119
120
0
#define YIN_SUBELEM_PARSED      0x80    /**< is set during parsing when given subelement is encountered for the first
121
                                             time to simply check validity of given constraints */
122
123
struct yin_subelement {
124
    enum ly_stmt type;      /**< type of keyword */
125
    void *dest;             /**< meta infromation passed to responsible function (mostly information about where parsed
126
                                 subelement should be stored) */
127
    uint16_t flags;         /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY,
128
                                 YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
129
};
130
131
/* Meta information passed to yin_parse_argument function,
132
   holds information about where content of argument element will be stored. */
133
struct yin_argument_meta {
134
    uint16_t *flags;        /**< Argument flags */
135
    const char **argument;  /**< Argument value */
136
};
137
138
/**
139
 * @brief Meta information passed to functions working with tree_schema,
140
 *        that require additional information about parent node.
141
 */
142
struct tree_node_meta {
143
    struct lysp_node *parent;       /**< parent node */
144
    struct lysp_node **nodes;    /**< linked list of siblings */
145
};
146
147
/**
148
 * @brief Meta information passed to yin_parse_import function.
149
 */
150
struct import_meta {
151
    const char *prefix;             /**< module prefix. */
152
    struct lysp_import **imports;   /**< imports to add to. */
153
};
154
155
/**
156
 * @brief Meta information passed to yin_parse_include function.
157
 */
158
struct include_meta {
159
    const char *name;               /**< Module/submodule name. */
160
    struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
161
};
162
163
/**
164
 * @brief Meta information passed to yin_parse_inout function.
165
 */
166
struct inout_meta {
167
    struct lysp_node *parent;          /**< Parent node. */
168
    struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
169
};
170
171
/**
172
 * @brief Meta information passed to yin_parse_minmax function.
173
 */
174
struct minmax_dev_meta {
175
    uint32_t *lim;                      /**< min/max value to write to. */
176
    uint16_t *flags;                    /**< min/max flags to write to. */
177
    struct lysp_ext_instance **exts;    /**< extension instances to add to. */
178
};
179
180
LY_ERR yin_parse_content(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
181
        const void *parent, enum ly_stmt parent_stmt, const char **text_content, struct lysp_ext_instance **exts);
182
183
/**
184
 * @brief Match yang keyword from yin data.
185
 *
186
 * @param[in,out] ctx Yin parser context for logging and to store current state.
187
 * @param[in] name Start of keyword name
188
 * @param[in] name_len Lenght of keyword name.
189
 * @param[in] prefix Start of keyword prefix.
190
 * @param[in] prefix_len Lenght of prefix.
191
 * @param[in] parent Identification of parent element, use LY_STMT_NONE for elements without parent.
192
 * @return yang_keyword values.
193
 */
194
enum ly_stmt
195
yin_match_keyword(struct lysp_yin_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
196
        enum ly_stmt parent)
197
0
{
198
0
    const char *start = NULL;
199
0
    enum ly_stmt kw = LY_STMT_NONE;
200
0
    const struct lyxml_ns *ns = NULL;
201
0
    struct ly_in *in;
202
203
0
    if (!name || (name_len == 0)) {
204
0
        return LY_STMT_NONE;
205
0
    }
206
207
0
    ns = lyxml_ns_get(&ctx->xmlctx->ns, prefix, prefix_len);
208
0
    if (ns) {
209
0
        if (!IS_YIN_NS(ns->uri)) {
210
0
            return LY_STMT_EXTENSION_INSTANCE;
211
0
        }
212
0
    } else {
213
        /* elements without namespace are automatically unknown */
214
0
        return LY_STMT_NONE;
215
0
    }
216
217
0
    LY_CHECK_RET(ly_in_new_memory(name, &in), LY_STMT_NONE);
218
0
    start = in->current;
219
0
    kw = lysp_match_kw(in, NULL);
220
0
    name = in->current;
221
0
    ly_in_free(in, 0);
222
223
0
    if (name - start == (long)name_len) {
224
        /* this is done because of collision in yang statement value and yang argument mapped to yin element value */
225
0
        if ((kw == LY_STMT_VALUE) && (parent == LY_STMT_ERROR_MESSAGE)) {
226
0
            return LY_STMT_ARG_VALUE;
227
0
        }
228
0
        return kw;
229
0
    } else {
230
0
        if (strncmp(start, "text", name_len) == 0) {
231
0
            return LY_STMT_ARG_TEXT;
232
0
        } else {
233
0
            return LY_STMT_NONE;
234
0
        }
235
0
    }
236
0
}
237
238
/**
239
 * @brief Match argument name.
240
 *
241
 * @param[in] name String representing name.
242
 * @param[in] len Lenght of the name.
243
 * @return yin_argument values.
244
 */
245
enum yin_argument
246
yin_match_argument_name(const char *name, size_t len)
247
0
{
248
0
    enum yin_argument arg = YIN_ARG_UNKNOWN;
249
0
    size_t already_read = 0;
250
251
0
    LY_CHECK_RET(len == 0, YIN_ARG_NONE);
252
253
0
#define READ_INC(LEN) already_read += LEN
254
0
#define ARG_SET(STMT) arg=STMT
255
0
#define ARG_CHECK(STR, LEN) (!strncmp((name) + already_read, STR, LEN) && (READ_INC(LEN)))
256
257
0
    switch (*name) {
258
0
    case 'c':
259
0
        READ_INC(1);
260
0
        if (ARG_CHECK("ondition", 8)) {
261
0
            ARG_SET(YIN_ARG_CONDITION);
262
0
        }
263
0
        break;
264
265
0
    case 'd':
266
0
        READ_INC(1);
267
0
        if (ARG_CHECK("ate", 3)) {
268
0
            ARG_SET(YIN_ARG_DATE);
269
0
        }
270
0
        break;
271
272
0
    case 'm':
273
0
        READ_INC(1);
274
0
        if (ARG_CHECK("odule", 5)) {
275
0
            ARG_SET(YIN_ARG_MODULE);
276
0
        }
277
0
        break;
278
279
0
    case 'n':
280
0
        READ_INC(1);
281
0
        if (ARG_CHECK("ame", 3)) {
282
0
            ARG_SET(YIN_ARG_NAME);
283
0
        }
284
0
        break;
285
286
0
    case 't':
287
0
        READ_INC(1);
288
0
        if (ARG_CHECK("a", 1)) {
289
0
            if (ARG_CHECK("g", 1)) {
290
0
                ARG_SET(YIN_ARG_TAG);
291
0
            } else if (ARG_CHECK("rget-node", 9)) {
292
0
                ARG_SET(YIN_ARG_TARGET_NODE);
293
0
            }
294
0
        } else if (ARG_CHECK("ext", 3)) {
295
0
            ARG_SET(YIN_ARG_TEXT);
296
0
        }
297
0
        break;
298
299
0
    case 'u':
300
0
        READ_INC(1);
301
0
        if (ARG_CHECK("ri", 2)) {
302
0
            ARG_SET(YIN_ARG_URI);
303
0
        }
304
0
        break;
305
306
0
    case 'v':
307
0
        READ_INC(1);
308
0
        if (ARG_CHECK("alue", 4)) {
309
0
            ARG_SET(YIN_ARG_VALUE);
310
0
        }
311
0
        break;
312
0
    }
313
314
    /* whole argument must be matched */
315
0
    if (already_read != len) {
316
0
        arg = YIN_ARG_UNKNOWN;
317
0
    }
318
319
0
#undef ARG_CHECK
320
0
#undef ARG_SET
321
0
#undef READ_INC
322
323
0
    return arg;
324
0
}
325
326
0
#define IS_NODE_ELEM(kw) (kw == LY_STMT_ANYXML || kw == LY_STMT_ANYDATA || kw == LY_STMT_LEAF || kw == LY_STMT_LEAF_LIST || \
327
0
                          kw == LY_STMT_TYPEDEF || kw == LY_STMT_USES || kw == LY_STMT_LIST || kw == LY_STMT_NOTIFICATION || \
328
0
                          kw == LY_STMT_GROUPING || kw == LY_STMT_CONTAINER || kw == LY_STMT_CASE || kw == LY_STMT_CHOICE || \
329
0
                          kw == LY_STMT_ACTION || kw == LY_STMT_RPC || kw == LY_STMT_AUGMENT)
330
331
0
#define HAS_META(kw) (IS_NODE_ELEM(kw) || kw == LY_STMT_IMPORT || kw == LY_STMT_INCLUDE || kw == LY_STMT_INPUT || kw == LY_STMT_OUTPUT)
332
333
/**
334
 * @brief Free subelems information allocated on heap.
335
 *
336
 * @param[in] count Size of subelems array.
337
 * @param[in] subelems Subelems array to free.
338
 */
339
static void
340
subelems_deallocator(size_t count, struct yin_subelement *subelems)
341
0
{
342
0
    for (size_t i = 0; i < count; ++i) {
343
0
        if (HAS_META(subelems[i].type)) {
344
0
            free(subelems[i].dest);
345
0
        }
346
0
    }
347
348
0
    free(subelems);
349
0
}
350
351
/**
352
 * @brief Allocate subelems information on heap.
353
 *
354
 * @param[in] ctx YIN parser context, used for logging.
355
 * @param[in] count Number of subelements.
356
 * @param[in] parent Parent node if any.
357
 * @param[out] result Allocated subelems array.
358
 * @return LY_ERR values.
359
 */
360
static LY_ERR
361
subelems_allocator(struct lysp_yin_ctx *ctx, size_t count, struct lysp_node *parent,
362
        struct yin_subelement **result, ...)
363
0
{
364
0
    va_list ap;
365
366
0
    *result = calloc(count, sizeof **result);
367
0
    LY_CHECK_GOTO(!(*result), mem_err);
368
369
0
    va_start(ap, result);
370
0
    for (size_t i = 0; i < count; ++i) {
371
        /* TYPE */
372
0
        (*result)[i].type = va_arg(ap, enum ly_stmt);
373
        /* DEST */
374
0
        if (IS_NODE_ELEM((*result)[i].type)) {
375
0
            struct tree_node_meta *node_meta = NULL;
376
377
0
            node_meta = calloc(1, sizeof *node_meta);
378
0
            LY_CHECK_GOTO(!node_meta, mem_err);
379
0
            node_meta->parent = parent;
380
0
            node_meta->nodes = va_arg(ap, void *);
381
0
            (*result)[i].dest = node_meta;
382
0
        } else if ((*result)[i].type == LY_STMT_IMPORT) {
383
0
            struct import_meta *imp_meta = NULL;
384
385
0
            imp_meta = calloc(1, sizeof *imp_meta);
386
0
            LY_CHECK_GOTO(!imp_meta, mem_err);
387
0
            imp_meta->prefix = va_arg(ap, const char *);
388
0
            imp_meta->imports = va_arg(ap, struct lysp_import **);
389
0
            (*result)[i].dest = imp_meta;
390
0
        } else if ((*result)[i].type == LY_STMT_INCLUDE) {
391
0
            struct include_meta *inc_meta = NULL;
392
393
0
            inc_meta = calloc(1, sizeof *inc_meta);
394
0
            LY_CHECK_GOTO(!inc_meta, mem_err);
395
0
            inc_meta->name = va_arg(ap, const char *);
396
0
            inc_meta->includes = va_arg(ap, struct lysp_include **);
397
0
            (*result)[i].dest = inc_meta;
398
0
        } else if (((*result)[i].type == LY_STMT_INPUT) || ((*result)[i].type == LY_STMT_OUTPUT)) {
399
0
            struct inout_meta *inout_meta = NULL;
400
401
0
            inout_meta = calloc(1, sizeof *inout_meta);
402
0
            LY_CHECK_GOTO(!inout_meta, mem_err);
403
0
            inout_meta->parent = parent;
404
0
            inout_meta->inout_p = va_arg(ap, struct lysp_node_action_inout *);
405
0
            (*result)[i].dest = inout_meta;
406
0
        } else {
407
0
            (*result)[i].dest = va_arg(ap, void *);
408
0
        }
409
        /* FLAGS */
410
0
        (*result)[i].flags = va_arg(ap, int);
411
0
    }
412
0
    va_end(ap);
413
414
0
    return LY_SUCCESS;
415
416
0
mem_err:
417
0
    va_end(ap);
418
0
    subelems_deallocator(count, *result);
419
0
    LOGMEM(ctx->xmlctx->ctx);
420
0
    return LY_EMEM;
421
0
}
422
423
/**
424
 * @brief Check that val is valid UTF8 character sequence of val_type.
425
 *        Doesn't check empty string, only character validity.
426
 *
427
 * @param[in] ctx Yin parser context for logging.
428
 * @param[in] val_type Type of the input string to select method of checking character validity.
429
 * @return LY_ERR values.
430
 */
431
LY_ERR
432
yin_validate_value(struct lysp_yin_ctx *ctx, enum yang_arg val_type)
433
0
{
434
0
    uint8_t prefix = 0;
435
0
    uint32_t c, utf8_char_len, already_read = 0;
436
0
    const char *val;
437
438
0
    assert((ctx->xmlctx->status == LYXML_ELEM_CONTENT) || (ctx->xmlctx->status == LYXML_ATTR_CONTENT));
439
440
0
    val = ctx->xmlctx->value;
441
0
    while (already_read < ctx->xmlctx->value_len) {
442
0
        LY_CHECK_ERR_RET(ly_getutf8((const char **)&val, &c, &utf8_char_len),
443
0
                LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INCHAR, (val)[-utf8_char_len]), LY_EVALID);
444
445
0
        switch (val_type) {
446
0
        case Y_IDENTIF_ARG:
447
0
            LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, already_read ? 0 : 1, NULL));
448
0
            break;
449
0
        case Y_PREF_IDENTIF_ARG:
450
0
            LY_CHECK_RET(lysp_check_identifierchar((struct lysp_ctx *)ctx, c, already_read ? 0 : 1, &prefix));
451
0
            break;
452
0
        case Y_STR_ARG:
453
0
        case Y_MAYBE_STR_ARG:
454
0
            LY_CHECK_RET(lysp_check_stringchar((struct lysp_ctx *)ctx, c));
455
0
            break;
456
0
        }
457
458
0
        already_read += utf8_char_len;
459
0
        LY_CHECK_ERR_RET(already_read > ctx->xmlctx->value_len, LOGINT(ctx->xmlctx->ctx), LY_EINT);
460
0
    }
461
462
0
    if ((already_read == 0) &&
463
0
            ((val_type == Y_PREF_IDENTIF_ARG) || (val_type == Y_IDENTIF_ARG))) {
464
        /* Empty identifier or prefix*/
465
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Empty identifier is not allowed.");
466
0
        return LY_EVALID;
467
0
    }
468
469
0
    return LY_SUCCESS;
470
0
}
471
472
/**
473
 * @brief Parse yin attributes.
474
 *
475
 * @param[in,out] ctx YIN parser context for logging and to store current state.
476
 * @param[in] arg_type Type of argument that is expected in parsed element (use YIN_ARG_NONE for elements without
477
 *            special argument).
478
 * @param[out] arg_val Where value of argument should be stored. Can be NULL iff arg_type is specified as YIN_ARG_NONE.
479
 * @param[in] val_type Type of expected value of attribute.
480
 * @param[in] current_element Identification of current element, used for logging.
481
 * @return LY_ERR values.
482
 */
483
static LY_ERR
484
yin_parse_attribute(struct lysp_yin_ctx *ctx, enum yin_argument arg_type, const char **arg_val, enum yang_arg val_type,
485
        enum ly_stmt current_element)
486
0
{
487
0
    enum yin_argument arg = YIN_ARG_UNKNOWN;
488
0
    bool found = false;
489
490
    /* validation of attributes */
491
0
    while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
492
        /* yin arguments represented as attributes have no namespace, which in this case means no prefix */
493
0
        if (!ctx->xmlctx->prefix) {
494
0
            arg = yin_match_argument_name(ctx->xmlctx->name, ctx->xmlctx->name_len);
495
0
            if (arg == YIN_ARG_NONE) {
496
                /* skip it */
497
0
                LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
498
0
            } else if (arg == arg_type) {
499
0
                LY_CHECK_ERR_RET(found, LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_DUP_ATTR,
500
0
                        yin_attr2str(arg), lyplg_ext_stmt2str(current_element)), LY_EVALID);
501
0
                found = true;
502
503
                /* go to value */
504
0
                LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
505
0
                LY_CHECK_RET(yin_validate_value(ctx, val_type));
506
0
                INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg_val);
507
0
                LY_CHECK_RET(!(*arg_val), LY_EMEM);
508
0
            } else {
509
0
                LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_ATTR, (int)ctx->xmlctx->name_len,
510
0
                        ctx->xmlctx->name, lyplg_ext_stmt2str(current_element));
511
0
                return LY_EVALID;
512
0
            }
513
0
        } else {
514
            /* skip it */
515
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
516
0
        }
517
518
        /* next attribute */
519
0
        LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
520
0
    }
521
522
    /* anything else than Y_MAYBE_STR_ARG is mandatory */
523
0
    if ((val_type != Y_MAYBE_STR_ARG) && !found) {
524
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory attribute %s of %s element.",
525
0
                yin_attr2str(arg_type), lyplg_ext_stmt2str(current_element));
526
0
        return LY_EVALID;
527
0
    }
528
529
0
    return LY_SUCCESS;
530
0
}
531
532
/**
533
 * @brief Get record with given type.
534
 *
535
 * @param[in] type Type of wanted record.
536
 * @param[in] array_size Size of array.
537
 * @param[in] array Searched array.
538
 * @return Pointer to desired record on success, NULL if element is not in the array.
539
 */
540
static struct yin_subelement *
541
get_record(enum ly_stmt type, LY_ARRAY_COUNT_TYPE array_size, struct yin_subelement *array)
542
0
{
543
0
    for (LY_ARRAY_COUNT_TYPE u = 0; u < array_size; ++u) {
544
0
        if (array[u].type == type) {
545
0
            return &array[u];
546
0
        }
547
0
    }
548
0
    return NULL;
549
0
}
550
551
/**
552
 * @brief Helper function to check mandatory constraint of subelement.
553
 *
554
 * @param[in,out] ctx YIN parser context for logging and to store current state.
555
 * @param[in] subelem_info Array of information about subelements.
556
 * @param[in] subelem_info_size Size of subelem_info array.
557
 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
558
 * @return LY_ERR values.
559
 */
560
static LY_ERR
561
yin_check_subelem_mandatory_constraint(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info,
562
        signed char subelem_info_size, enum ly_stmt current_element)
563
0
{
564
0
    for (signed char i = 0; i < subelem_info_size; ++i) {
565
        /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
566
0
        if ((subelem_info[i].flags & YIN_SUBELEM_MANDATORY) && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
567
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_MAND_SUBELEM,
568
0
                    lyplg_ext_stmt2str(subelem_info[i].type), lyplg_ext_stmt2str(current_element));
569
0
            return LY_EVALID;
570
0
        }
571
0
    }
572
573
0
    return LY_SUCCESS;
574
0
}
575
576
/**
577
 * @brief Helper function to check "first" constraint of subelement.
578
 *
579
 * @param[in,out] ctx YIN parser context for logging and to store current state.
580
 * @param[in] subelem_info Array of information about subelements.
581
 * @param[in] subelem_info_size Size of subelem_info array.
582
 * @param[in] current_element Identification of element that is currently being parsed, used for logging.
583
 * @param[in] exp_first Record in subelem_info array that is expected to be defined as first subelement, used for logging.
584
 * @return LY_ERR values.
585
 */
586
static LY_ERR
587
yin_check_subelem_first_constraint(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info,
588
        signed char subelem_info_size, enum ly_stmt current_element,
589
        struct yin_subelement *exp_first)
590
0
{
591
0
    for (signed char i = 0; i < subelem_info_size; ++i) {
592
0
        if (subelem_info[i].flags & YIN_SUBELEM_PARSED) {
593
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
594
0
                    lyplg_ext_stmt2str(exp_first->type), lyplg_ext_stmt2str(current_element));
595
0
            return LY_EVALID;
596
0
        }
597
0
    }
598
599
0
    return LY_SUCCESS;
600
0
}
601
602
/**
603
 * @brief Parse simple element without any special constraints and argument mapped to yin attribute,
604
 * for example prefix or namespace element.
605
 *
606
 * @param[in,out] ctx YIN parser context for logging and to store current state.
607
 * @param[in] parent Current statement parent.
608
 * @param[in] parent_stmt Type of @p parent statement.
609
 * @param[out] value Where value of attribute should be stored.
610
 * @param[in] arg_type Expected type of attribute.
611
 * @param[in] arg_val_type Type of expected value of attribute.
612
 * @param[in,out] exts Extension instances to add to.
613
 * @return LY_ERR values.
614
 */
615
static LY_ERR
616
yin_parse_simple_element(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, const char **value,
617
        enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
618
0
{
619
0
    struct yin_subelement subelems[] = {
620
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
621
0
    };
622
623
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
624
0
    LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, parent_stmt));
625
626
0
    return yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, parent_stmt, NULL, exts);
627
0
}
628
629
/**
630
 * @brief Parse path element.
631
 *
632
 * @param[in,out] ctx YIN parser context for logging and to store current state.
633
 * @param[out] type Type structure to store parsed value, flags and extension instances.
634
 * @return LY_ERR values.
635
 */
636
static LY_ERR
637
yin_parse_path(struct lysp_yin_ctx *ctx, struct lysp_type *type)
638
0
{
639
0
    LY_ERR ret;
640
0
    const char *str_path;
641
642
0
    LY_CHECK_RET(yin_parse_simple_element(ctx, type, LY_STMT_PATH, &str_path, YIN_ARG_VALUE, Y_STR_ARG, &type->exts));
643
644
0
    ret = ly_path_parse(ctx->xmlctx->ctx, NULL, str_path, 0, 1, LY_PATH_BEGIN_EITHER,
645
0
            LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
646
0
    lysdict_remove(ctx->xmlctx->ctx, str_path);
647
0
    LY_CHECK_RET(ret);
648
0
    type->flags |= LYS_SET_PATH;
649
650
0
    return LY_SUCCESS;
651
0
}
652
653
/**
654
 * @brief Add extension array to context exts instances.
655
 *
656
 * @param[in] ctx YIN parser context.
657
 * @param[in] exts Parsed extension array that is final (no realloc anymore).
658
 * @return LY_ERR value.
659
 */
660
static LY_ERR
661
yin_unres_exts_add(struct lysp_yin_ctx *ctx, struct lysp_ext_instance *exts)
662
0
{
663
0
    if (!exts) {
664
0
        return LY_SUCCESS;
665
0
    }
666
667
0
    return ly_set_add(&ctx->ext_inst, exts, 1, NULL);
668
0
}
669
670
/**
671
 * @brief Parse pattern element.
672
 *
673
 * @param[in,out] ctx YIN parser context for logging and to store current state.
674
 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
675
 * @return LY_ERR values.
676
 */
677
static LY_ERR
678
yin_parse_pattern(struct lysp_yin_ctx *ctx, struct lysp_type *type)
679
0
{
680
0
    const char *real_value = NULL;
681
0
    char *saved_value = NULL;
682
0
    struct lysp_restr *restr;
683
684
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->patterns, restr, LY_EMEM);
685
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
686
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &real_value, Y_STR_ARG, LY_STMT_PATTERN));
687
0
    size_t len = strlen(real_value);
688
689
0
    saved_value = malloc(len + 2);
690
0
    LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
691
0
    memmove(saved_value + 1, real_value, len);
692
0
    lysdict_remove(ctx->xmlctx->ctx, real_value);
693
0
    saved_value[0] = LYSP_RESTR_PATTERN_ACK;
694
0
    saved_value[len + 1] = '\0';
695
0
    LY_CHECK_RET(lysdict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
696
0
    restr->arg.mod = PARSER_CUR_PMOD(ctx);
697
0
    type->flags |= LYS_SET_PATTERN;
698
699
0
    struct yin_subelement subelems[] = {
700
0
        {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
701
0
        {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
702
0
        {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
703
0
        {LY_STMT_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
704
0
        {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
705
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
706
0
    };
707
708
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr, LY_STMT_PATTERN, NULL, &restr->exts));
709
710
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
711
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, restr->exts));
712
713
0
    return LY_SUCCESS;
714
0
}
715
716
/**
717
 * @brief Parse fraction-digits element.
718
 *
719
 * @param[in,out] ctx YIN parser context for logging and to store current state.
720
 * @param[in,out] type Type structure to store value, flags and extension instances.
721
 * @return LY_ERR values.
722
 */
723
static LY_ERR
724
yin_parse_fracdigits(struct lysp_yin_ctx *ctx, struct lysp_type *type)
725
0
{
726
0
    const char *temp_val = NULL;
727
0
    char *ptr;
728
0
    unsigned long long num;
729
730
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
731
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_FRACTION_DIGITS));
732
733
0
    if ((temp_val[0] == '\0') || (temp_val[0] == '0') || !isdigit(temp_val[0])) {
734
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
735
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
736
0
        return LY_EVALID;
737
0
    }
738
739
0
    errno = 0;
740
0
    num = strtoull(temp_val, &ptr, LY_BASE_DEC);
741
0
    if (*ptr != '\0') {
742
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
743
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
744
0
        return LY_EVALID;
745
0
    }
746
0
    if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
747
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "fraction-digits");
748
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
749
0
        return LY_EVALID;
750
0
    }
751
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
752
0
    type->fraction_digits = num;
753
0
    type->flags |= LYS_SET_FRDIGITS;
754
0
    struct yin_subelement subelems[] = {
755
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
756
0
    };
757
758
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), type, LY_STMT_FRACTION_DIGITS, NULL, &type->exts));
759
760
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
761
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, type->exts));
762
763
0
    return LY_SUCCESS;
764
0
}
765
766
/**
767
 * @brief Parse enum element.
768
 *
769
 * @param[in,out] ctx YIN parser context for logging and to store current state.
770
 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
771
 * @return LY_ERR values.
772
 */
773
static LY_ERR
774
yin_parse_enum(struct lysp_yin_ctx *ctx, struct lysp_type *type)
775
0
{
776
0
    struct lysp_type_enum *en;
777
778
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->enums, en, LY_EMEM);
779
0
    type->flags |= LYS_SET_ENUM;
780
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
781
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_STR_ARG, LY_STMT_ENUM));
782
0
    LY_CHECK_RET(lysp_check_enum_name((struct lysp_ctx *)ctx, en->name, strlen(en->name)));
783
0
    CHECK_NONEMPTY((struct lysp_ctx *)ctx, strlen(en->name), "enum");
784
0
    CHECK_UNIQUENESS((struct lysp_ctx *)ctx, type->enums, name, "enum", en->name);
785
786
0
    struct yin_subelement subelems[] = {
787
0
        {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
788
0
        {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
789
0
        {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
790
0
        {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
791
0
        {LY_STMT_VALUE, en, YIN_SUBELEM_UNIQUE},
792
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
793
0
    };
794
795
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), en, LY_STMT_ENUM, NULL, &en->exts));
796
797
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
798
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, en->exts));
799
800
0
    return LY_SUCCESS;
801
0
}
802
803
/**
804
 * @brief Parse bit element.
805
 *
806
 * @param[in,out] ctx YIN parser context for logging and to store current state.
807
 * @param[in,out] type Type structure to store parsed value, flags and extension instances.
808
 * @return LY_ERR values.
809
 */
810
static LY_ERR
811
yin_parse_bit(struct lysp_yin_ctx *ctx, struct lysp_type *type)
812
0
{
813
0
    struct lysp_type_enum *en;
814
815
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->bits, en, LY_EMEM);
816
0
    type->flags |= LYS_SET_BIT;
817
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
818
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &en->name, Y_IDENTIF_ARG, LY_STMT_BIT));
819
0
    CHECK_UNIQUENESS((struct lysp_ctx *)ctx, type->bits, name, "bit", en->name);
820
821
0
    struct yin_subelement subelems[] = {
822
0
        {LY_STMT_DESCRIPTION, &en->dsc, YIN_SUBELEM_UNIQUE},
823
0
        {LY_STMT_IF_FEATURE, &en->iffeatures, 0},
824
0
        {LY_STMT_POSITION, en, YIN_SUBELEM_UNIQUE},
825
0
        {LY_STMT_REFERENCE, &en->ref, YIN_SUBELEM_UNIQUE},
826
0
        {LY_STMT_STATUS, &en->flags, YIN_SUBELEM_UNIQUE},
827
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
828
0
    };
829
830
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), en, LY_STMT_BIT, NULL, &en->exts));
831
832
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
833
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, en->exts));
834
835
0
    return LY_SUCCESS;
836
0
}
837
838
/**
839
 * @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
840
 * more instances, such as base or if-feature.
841
 *
842
 * @param[in,out] ctx YIN parser context for logging and to store current state.
843
 * @param[in] parent_stmt Type of parent statement.
844
 * @param[out] values Parsed values to add to.
845
 * @param[in] arg_type Expected type of attribute.
846
 * @param[in] arg_val_type Type of expected value of attribute.
847
 * @param[in,out] exts Extension instances to add to.
848
 * @return LY_ERR values.
849
 */
850
static LY_ERR
851
yin_parse_simple_elements(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, const char ***values,
852
        enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
853
0
{
854
0
    const char **value;
855
0
    LY_ARRAY_COUNT_TYPE index = LY_ARRAY_COUNT(*values);
856
0
    struct yin_subelement subelems[] = {
857
0
        {LY_STMT_EXTENSION_INSTANCE, &index, 0}
858
0
    };
859
860
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *values, value, LY_EMEM);
861
862
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
863
0
    LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, value, arg_val_type, parent_stmt));
864
865
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), *values, parent_stmt, NULL, exts));
866
867
0
    return LY_SUCCESS;
868
0
}
869
870
/**
871
 * @brief Parse simple element without any special constraints and argument mapped to yin attribute.
872
 *
873
 * @param[in,out] ctx YIN parser context for logging and to store current state.
874
 * @param[in] parent Current statement parent.
875
 * @param[in] parent_stmt Type of @p parent statement.
876
 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
877
 * @param[in] arg_type Expected type of attribute.
878
 * @param[in] arg_val_type Type of expected value of attribute.
879
 * @param[in,out] exts Extension instances to add to.
880
 * @return LY_ERR values.
881
 */
882
static LY_ERR
883
yin_parse_simple_elem(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, struct yin_subelement *subinfo,
884
        enum yin_argument arg_type, enum yang_arg arg_val_type, struct lysp_ext_instance **exts)
885
0
{
886
0
    if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
887
0
        LY_CHECK_RET(yin_parse_simple_element(ctx, parent, parent_stmt, (const char **)subinfo->dest, arg_type,
888
0
                arg_val_type, exts));
889
0
    } else {
890
0
        LY_CHECK_RET(yin_parse_simple_elements(ctx, parent_stmt, (const char ***)subinfo->dest, arg_type,
891
0
                arg_val_type, exts));
892
0
    }
893
894
0
    return LY_SUCCESS;
895
0
}
896
897
/**
898
 * @brief Parse base element.
899
 *
900
 * @param[in,out] ctx YIN parser context for logging and to store current state.
901
 * @param[in] parent_stmt Type of parent statement.
902
 * @param[out] dest Where parsed values should be stored.
903
 * @param[in,out] exts Extension instances to add to.
904
 * @return LY_ERR values.
905
 */
906
static LY_ERR
907
yin_parse_base(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, void *dest, struct lysp_ext_instance **exts)
908
0
{
909
0
    struct lysp_type *type;
910
911
0
    if (parent_stmt == LY_STMT_TYPE) {
912
0
        type = (struct lysp_type *)dest;
913
0
        LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, &type->bases, YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
914
0
        type->flags |= LYS_SET_BASE;
915
0
    } else if (parent_stmt == LY_STMT_IDENTITY) {
916
0
        LY_CHECK_RET(yin_parse_simple_elements(ctx, LY_STMT_BASE, (const char ***)dest, YIN_ARG_NAME, Y_PREF_IDENTIF_ARG, exts));
917
0
    } else {
918
0
        LOGINT(ctx->xmlctx->ctx);
919
0
        return LY_EINT;
920
0
    }
921
922
0
    return LY_SUCCESS;
923
0
}
924
925
/**
926
 * @brief Parse require-instance element.
927
 *
928
 * @param[in,out] ctx YIN parser context for logging and to store current state.
929
 * @param[out] type Type structure to store value, flag and extensions.
930
 * @return LY_ERR values.
931
 */
932
static LY_ERR
933
yin_pasrse_reqinstance(struct lysp_yin_ctx *ctx, struct lysp_type *type)
934
0
{
935
0
    const char *temp_val = NULL;
936
0
    struct yin_subelement subelems[] = {
937
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
938
0
    };
939
940
0
    type->flags |= LYS_SET_REQINST;
941
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
942
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_REQUIRE_INSTANCE));
943
0
    if (strcmp(temp_val, "true") == 0) {
944
0
        type->require_instance = 1;
945
0
    } else if (strcmp(temp_val, "false") != 0) {
946
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
947
0
                "require-instance", "true", "false");
948
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
949
0
        return LY_EVALID;
950
0
    }
951
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
952
953
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), type, LY_STMT_REQUIRE_INSTANCE, NULL, &type->exts));
954
955
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
956
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, type->exts));
957
958
0
    return LY_SUCCESS;
959
0
}
960
961
/**
962
 * @brief Parse modifier element.
963
 *
964
 * @param[in,out] ctx YIN parser context for logging and to store current state.
965
 * @param[in] parent Current statement parent.
966
 * @param[in,out] pat Value to write to.
967
 * @param[in,out] exts Extension instances to add to.
968
 * @return LY_ERR values.
969
 */
970
static LY_ERR
971
yin_parse_modifier(struct lysp_yin_ctx *ctx, const void *parent, const char **pat, struct lysp_ext_instance **exts)
972
0
{
973
0
    const char *temp_val;
974
0
    char *modified_val;
975
0
    struct yin_subelement subelems[] = {
976
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
977
0
    };
978
979
0
    assert(**pat == 0x06);
980
981
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
982
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MODIFIER));
983
0
    if (strcmp(temp_val, "invert-match") != 0) {
984
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS1, temp_val, "value",
985
0
                "modifier", "invert-match");
986
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
987
0
        return LY_EVALID;
988
0
    }
989
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
990
991
    /* allocate new value */
992
0
    modified_val = malloc(strlen(*pat) + 1);
993
0
    LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
994
0
    strcpy(modified_val, *pat);
995
0
    lysdict_remove(ctx->xmlctx->ctx, *pat);
996
997
    /* modify the new value */
998
0
    modified_val[0] = LYSP_RESTR_PATTERN_NACK;
999
0
    LY_CHECK_RET(lysdict_insert_zc(ctx->xmlctx->ctx, modified_val, pat));
1000
1001
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_MODIFIER, NULL, exts));
1002
1003
0
    return LY_SUCCESS;
1004
0
}
1005
1006
/**
1007
 * @brief Parse a restriction element (length, range or one instance of must).
1008
 *
1009
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1010
 * @param[in] restr_kw Identificaton of element that is being parsed, can be set to LY_STMT_MUST, LY_STMT_LENGTH or LY_STMT_RANGE.
1011
 * @param[in] restr Value to write to.
1012
 * @return LY_ERR values.
1013
 */
1014
static LY_ERR
1015
yin_parse_restriction(struct lysp_yin_ctx *ctx, enum ly_stmt restr_kw, struct lysp_restr *restr)
1016
0
{
1017
0
    struct yin_subelement subelems[] = {
1018
0
        {LY_STMT_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
1019
0
        {LY_STMT_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
1020
0
        {LY_STMT_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
1021
0
        {LY_STMT_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
1022
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1023
0
    };
1024
    /* argument of must is called condition, but argument of length and range is called value */
1025
0
    enum yin_argument arg_type = (restr_kw == LY_STMT_MUST) ? YIN_ARG_CONDITION : YIN_ARG_VALUE;
1026
1027
0
    assert(restr_kw == LY_STMT_MUST || restr_kw == LY_STMT_LENGTH || restr_kw == LY_STMT_RANGE);
1028
1029
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1030
0
    LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
1031
0
    restr->arg.mod = PARSER_CUR_PMOD(ctx);
1032
1033
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), restr, restr_kw, NULL, &restr->exts));
1034
1035
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1036
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, restr->exts));
1037
1038
0
    return LY_SUCCESS;
1039
0
}
1040
1041
/**
1042
 * @brief Parse range element.
1043
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1044
 * @param[out] type Type structure to store parsed value and flags.
1045
 * @return LY_ERR values.
1046
 */
1047
static LY_ERR
1048
yin_parse_range(struct lysp_yin_ctx *ctx, struct lysp_type *type)
1049
0
{
1050
0
    type->range = calloc(1, sizeof *type->range);
1051
0
    LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1052
0
    LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_RANGE, type->range));
1053
0
    type->flags |= LYS_SET_RANGE;
1054
1055
0
    return LY_SUCCESS;
1056
0
}
1057
1058
/**
1059
 * @brief Parse length element.
1060
 *
1061
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1062
 * @param[out] type Type structure to store parsed value and flags.
1063
 * @return LY_ERR values.
1064
 */
1065
static LY_ERR
1066
yin_parse_length(struct lysp_yin_ctx *ctx, struct lysp_type *type)
1067
0
{
1068
0
    type->length = calloc(1, sizeof *type->length);
1069
0
    LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1070
0
    LY_CHECK_RET(yin_parse_restriction(ctx, LY_STMT_LENGTH, type->length));
1071
0
    type->flags |= LYS_SET_LENGTH;
1072
1073
0
    return LY_SUCCESS;
1074
0
}
1075
1076
/**
1077
 * @brief Parse must element.
1078
 *
1079
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1080
 * @param[in,out] restrs Restrictions to add to.
1081
 * @return LY_ERR values.
1082
 */
1083
static LY_ERR
1084
yin_parse_must(struct lysp_yin_ctx *ctx, struct lysp_restr **restrs)
1085
0
{
1086
0
    struct lysp_restr *restr;
1087
1088
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *restrs, restr, LY_EMEM);
1089
0
    return yin_parse_restriction(ctx, LY_STMT_MUST, restr);
1090
0
}
1091
1092
/**
1093
 * @brief Parse a node id into an array.
1094
 *
1095
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1096
 * @param[in] parent_stmt Type of parent statement.
1097
 * @param[in] subinfo Information about subelement, is used to determin which function should be called and where to store parsed value.
1098
 * @param[in,out] exts Extension instances to add to.
1099
 * @return LY_ERR values.
1100
 */
1101
static LY_ERR
1102
yin_parse_qname(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct yin_subelement *subinfo,
1103
        struct lysp_ext_instance **exts)
1104
0
{
1105
0
    struct lysp_qname *qname, **qnames;
1106
1107
0
    switch (parent_stmt) {
1108
0
    case LY_STMT_DEFAULT:
1109
0
        if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
1110
0
            qname = (struct lysp_qname *)subinfo->dest;
1111
0
        } else {
1112
0
            qnames = (struct lysp_qname **)subinfo->dest;
1113
0
            LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1114
0
        }
1115
0
        qname->mod = PARSER_CUR_PMOD(ctx);
1116
0
        return yin_parse_simple_element(ctx, qname, parent_stmt, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
1117
0
    case LY_STMT_UNIQUE:
1118
0
        assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1119
1120
0
        qnames = (struct lysp_qname **)subinfo->dest;
1121
0
        LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1122
0
        qname->mod = PARSER_CUR_PMOD(ctx);
1123
0
        return yin_parse_simple_element(ctx, *qnames, parent_stmt, &qname->str, YIN_ARG_TAG, Y_STR_ARG, exts);
1124
0
    case LY_STMT_IF_FEATURE:
1125
0
        assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
1126
1127
0
        qnames = (struct lysp_qname **)subinfo->dest;
1128
0
        LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
1129
0
        qname->mod = PARSER_CUR_PMOD(ctx);
1130
0
        return yin_parse_simple_element(ctx, *qnames, parent_stmt, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
1131
0
    default:
1132
0
        break;
1133
0
    }
1134
1135
0
    LOGINT(ctx->xmlctx->ctx);
1136
0
    return LY_EINT;
1137
0
}
1138
1139
/**
1140
 * @brief Parse position or value element.
1141
 *
1142
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1143
 * @param[in] parent_stmt Type of parent statement.
1144
 * @param[out] enm Enum structure to save value, flags and extension instances.
1145
 * @return LY_ERR values.
1146
 */
1147
static LY_ERR
1148
yin_parse_value_pos(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_type_enum *enm)
1149
0
{
1150
0
    LY_ERR ret = LY_SUCCESS;
1151
0
    const char *temp_val = NULL;
1152
0
    char *ptr;
1153
0
    long long num = 0;
1154
0
    unsigned long long unum = 0;
1155
1156
0
    assert(parent_stmt == LY_STMT_POSITION || parent_stmt == LY_STMT_VALUE);
1157
1158
    /* set value flag */
1159
0
    enm->flags |= LYS_SET_VALUE;
1160
1161
    /* get attribute value */
1162
0
    LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
1163
0
    LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, parent_stmt), cleanup);
1164
0
    if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '+') ||
1165
0
            ((temp_val[0] == '0') && (temp_val[1] != '\0')) || ((parent_stmt == LY_STMT_POSITION) && !strcmp(temp_val, "-0"))) {
1166
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
1167
0
        ret = LY_EVALID;
1168
0
        goto cleanup;
1169
0
    }
1170
1171
    /* convert value */
1172
0
    errno = 0;
1173
0
    if (parent_stmt == LY_STMT_VALUE) {
1174
0
        num = strtoll(temp_val, &ptr, LY_BASE_DEC);
1175
0
        if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
1176
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
1177
0
            ret = LY_EVALID;
1178
0
            goto cleanup;
1179
0
        }
1180
0
    } else {
1181
0
        unum = strtoull(temp_val, &ptr, LY_BASE_DEC);
1182
0
        if (unum > UINT64_C(4294967295)) {
1183
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
1184
0
            ret = LY_EVALID;
1185
0
            goto cleanup;
1186
0
        }
1187
0
    }
1188
    /* check if whole argument value was converted */
1189
0
    if (*ptr != '\0') {
1190
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
1191
0
        ret = LY_EVALID;
1192
0
        goto cleanup;
1193
0
    }
1194
0
    if (errno == ERANGE) {
1195
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", lyplg_ext_stmt2str(parent_stmt));
1196
0
        ret = LY_EVALID;
1197
0
        goto cleanup;
1198
0
    }
1199
    /* save correctly ternary operator can't be used because num and unum have different signes */
1200
0
    if (parent_stmt == LY_STMT_VALUE) {
1201
0
        enm->value = num;
1202
0
    } else {
1203
0
        enm->value = unum;
1204
0
    }
1205
1206
    /* parse subelements */
1207
0
    struct yin_subelement subelems[] = {
1208
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1209
0
    };
1210
1211
0
    LY_CHECK_GOTO(ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), enm, parent_stmt, NULL, &enm->exts), cleanup);
1212
1213
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1214
0
    LY_CHECK_GOTO(ret = yin_unres_exts_add(ctx, enm->exts), cleanup);
1215
1216
0
cleanup:
1217
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
1218
0
    return ret;
1219
0
}
1220
1221
/**
1222
 * @brief Parse belongs-to element.
1223
 *
1224
 * @param[in] ctx YIN parser context for logging and to store current state.
1225
 * @param[out] submod Structure of submodule that is being parsed.
1226
 * @param[in,out] exts Extension instances to add to.
1227
 * @return LY_ERR values
1228
 */
1229
static LY_ERR
1230
yin_parse_belongs_to(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod, struct lysp_ext_instance **exts)
1231
0
{
1232
0
    const char *belongsto;
1233
0
    struct yin_subelement subelems[] = {
1234
0
        {LY_STMT_PREFIX, &submod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
1235
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1236
0
    };
1237
1238
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1239
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
1240
0
    if (PARSER_CUR_PMOD(ctx)->mod->name != belongsto) {
1241
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
1242
0
                belongsto, PARSER_CUR_PMOD(ctx)->mod->name);
1243
0
        lysdict_remove(ctx->xmlctx->ctx, belongsto);
1244
0
        return LY_EVALID;
1245
0
    }
1246
0
    lysdict_remove(ctx->xmlctx->ctx, belongsto);
1247
1248
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), submod, LY_STMT_BELONGS_TO, NULL, exts));
1249
1250
0
    return LY_SUCCESS;
1251
0
}
1252
1253
/**
1254
 * @brief Function to parse meta tags (description, contact, ...) eg. elements with
1255
 * text element as child.
1256
 *
1257
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1258
 * @param[in] parent Current statement parent.
1259
 * @param[in] parent_stmt Type of @p parent statement.
1260
 * @param[out] value Where the content of meta element should be stored.
1261
 * @param[in,out] exts Extension instances to add to.
1262
 * @return LY_ERR values.
1263
 */
1264
static LY_ERR
1265
yin_parse_meta(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, const char **value,
1266
        struct lysp_ext_instance **exts)
1267
0
{
1268
0
    struct yin_subelement subelems[] = {
1269
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1270
0
        {LY_STMT_ARG_TEXT, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1271
0
    };
1272
1273
0
    assert(parent_stmt == LY_STMT_ORGANIZATION || parent_stmt == LY_STMT_CONTACT || parent_stmt == LY_STMT_DESCRIPTION ||
1274
0
            parent_stmt == LY_STMT_REFERENCE);
1275
1276
    /* check attributes */
1277
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1278
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, parent_stmt));
1279
1280
    /* parse content */
1281
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, parent_stmt, NULL, exts));
1282
1283
0
    return LY_SUCCESS;
1284
0
}
1285
1286
/**
1287
 * @brief Parse error-message element.
1288
 *
1289
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1290
 * @param[in] parent Current statement parent.
1291
 * @param[out] value Where the content of error-message element should be stored.
1292
 * @param[in,out] exts Extension instances to add to.
1293
 * @return LY_ERR values.
1294
 */
1295
static LY_ERR
1296
yin_parse_err_msg(struct lysp_yin_ctx *ctx, const void *parent, const char **value, struct lysp_ext_instance **exts)
1297
0
{
1298
0
    struct yin_subelement subelems[] = {
1299
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1300
0
        {LY_STMT_ARG_VALUE, value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE | YIN_SUBELEM_FIRST}
1301
0
    };
1302
1303
    /* check attributes */
1304
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1305
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, LY_STMT_ERROR_MESSAGE));
1306
1307
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_ERROR_MESSAGE, NULL, exts));
1308
1309
0
    return LY_SUCCESS;
1310
0
}
1311
1312
/**
1313
 * @brief Parse type element.
1314
 *
1315
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1316
 * @param[in] parent_stmt Type of parent statement.
1317
 * @param[in,out] subinfo Information about subelement, is used to determin which function should be called and where
1318
 * to store parsed value.
1319
 * @return LY_ERR values.
1320
 */
1321
static LY_ERR
1322
yin_parse_type(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct yin_subelement *subinfo)
1323
0
{
1324
0
    struct lysp_type *type = NULL;
1325
1326
0
    if (parent_stmt == LY_STMT_DEVIATE) {
1327
0
        *(struct lysp_type **)subinfo->dest = calloc(1, sizeof **(struct lysp_type **)subinfo->dest);
1328
0
        LY_CHECK_ERR_RET(!(*(struct lysp_type **)subinfo->dest), LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
1329
0
        type = *((struct lysp_type **)subinfo->dest);
1330
0
    } else {
1331
0
        type = (struct lysp_type *)subinfo->dest;
1332
0
    }
1333
1334
    /* type as child of another type */
1335
0
    if (parent_stmt == LY_STMT_TYPE) {
1336
0
        struct lysp_type *nested_type = NULL;
1337
1338
0
        LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, type->types, nested_type, LY_EMEM);
1339
0
        type->flags |= LYS_SET_TYPE;
1340
0
        type = nested_type;
1341
0
    }
1342
1343
0
    type->pmod = PARSER_CUR_PMOD(ctx);
1344
1345
0
    struct yin_subelement subelems[] = {
1346
0
        {LY_STMT_BASE, type, 0},
1347
0
        {LY_STMT_BIT, type, 0},
1348
0
        {LY_STMT_ENUM, type, 0},
1349
0
        {LY_STMT_FRACTION_DIGITS, type, YIN_SUBELEM_UNIQUE},
1350
0
        {LY_STMT_LENGTH, type, YIN_SUBELEM_UNIQUE},
1351
0
        {LY_STMT_PATH, type, YIN_SUBELEM_UNIQUE},
1352
0
        {LY_STMT_PATTERN, type, 0},
1353
0
        {LY_STMT_RANGE, type, YIN_SUBELEM_UNIQUE},
1354
0
        {LY_STMT_REQUIRE_INSTANCE, type, YIN_SUBELEM_UNIQUE},
1355
0
        {LY_STMT_TYPE, type, 0},
1356
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1357
0
    };
1358
1359
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1360
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &type->name, Y_PREF_IDENTIF_ARG, LY_STMT_TYPE));
1361
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), type, LY_STMT_TYPE, NULL, &type->exts));
1362
1363
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1364
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, type->exts));
1365
1366
0
    return LY_SUCCESS;
1367
0
}
1368
1369
/**
1370
 * @brief Parse max-elements element.
1371
 *
1372
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1373
 * @param[in,out] max Value to write to.
1374
 * @param[in] flags Flags to write to.
1375
 * @param[in,out] exts Extension instances to add to.
1376
 * @return LY_ERR values.
1377
 */
1378
static LY_ERR
1379
yin_parse_maxelements(struct lysp_yin_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts)
1380
0
{
1381
0
    LY_ERR ret = LY_SUCCESS;
1382
0
    const char *temp_val = NULL;
1383
0
    char *ptr;
1384
0
    unsigned long long num;
1385
0
    struct yin_subelement subelems[] = {
1386
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1387
0
    };
1388
1389
0
    *flags |= LYS_SET_MAX;
1390
0
    LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
1391
0
    LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MAX_ELEMENTS), cleanup);
1392
0
    if (!temp_val || (temp_val[0] == '\0') || (temp_val[0] == '0') || ((temp_val[0] != 'u') && !isdigit(temp_val[0]))) {
1393
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
1394
0
        ret = LY_EVALID;
1395
0
        goto cleanup;
1396
0
    }
1397
1398
0
    if (strcmp(temp_val, "unbounded")) {
1399
0
        errno = 0;
1400
0
        num = strtoull(temp_val, &ptr, LY_BASE_DEC);
1401
0
        if (*ptr != '\0') {
1402
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "max-elements");
1403
0
            ret = LY_EVALID;
1404
0
            goto cleanup;
1405
0
        }
1406
0
        if ((errno == ERANGE) || (num > UINT32_MAX)) {
1407
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "max-elements");
1408
0
            ret = LY_EVALID;
1409
0
            goto cleanup;
1410
0
        }
1411
0
        *max = num;
1412
0
    }
1413
1414
0
    ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), max, LY_STMT_MAX_ELEMENTS, NULL, exts);
1415
0
    LY_CHECK_GOTO(ret, cleanup);
1416
1417
0
cleanup:
1418
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
1419
0
    return ret;
1420
0
}
1421
1422
/**
1423
 * @brief Parse min-elements element.
1424
 *
1425
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1426
 * @param[in,out] min Value to write to.
1427
 * @param[in] flags Flags to write to.
1428
 * @param[in,out] exts Extension instances to add to.
1429
 * @return LY_ERR values.
1430
 */
1431
static LY_ERR
1432
yin_parse_minelements(struct lysp_yin_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts)
1433
0
{
1434
0
    const char *temp_val = NULL;
1435
0
    char *ptr;
1436
0
    unsigned long long num;
1437
0
    struct yin_subelement subelems[] = {
1438
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1439
0
    };
1440
1441
0
    *flags |= LYS_SET_MIN;
1442
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1443
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MIN_ELEMENTS));
1444
1445
0
    if (!temp_val || (temp_val[0] == '\0') || ((temp_val[0] == '0') && (temp_val[1] != '\0'))) {
1446
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
1447
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
1448
0
        return LY_EVALID;
1449
0
    }
1450
1451
0
    errno = 0;
1452
0
    num = strtoull(temp_val, &ptr, LY_BASE_DEC);
1453
0
    if (ptr[0] != 0) {
1454
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN, temp_val, "value", "min-elements");
1455
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
1456
0
        return LY_EVALID;
1457
0
    }
1458
0
    if ((errno == ERANGE) || (num > UINT32_MAX)) {
1459
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_OOB_YIN, temp_val, "value", "min-elements");
1460
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
1461
0
        return LY_EVALID;
1462
0
    }
1463
0
    *min = num;
1464
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
1465
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), min, LY_STMT_MIN_ELEMENTS, NULL, exts));
1466
1467
0
    return LY_SUCCESS;
1468
0
}
1469
1470
/**
1471
 * @brief Parse min-elements or max-elements element.
1472
 *
1473
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1474
 * @param[in] parent_stmt Type of parent statement.
1475
 * @param[in] cur_stmt Type of current element.
1476
 * @param[in] dest Where the parsed value and flags should be stored.
1477
 * @return LY_ERR values.
1478
 */
1479
static LY_ERR
1480
yin_parse_minmax(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, enum ly_stmt cur_stmt, void *dest)
1481
0
{
1482
0
    uint32_t *lim;
1483
0
    uint16_t *flags;
1484
0
    struct lysp_ext_instance **exts;
1485
1486
0
    assert(cur_stmt == LY_STMT_MAX_ELEMENTS || cur_stmt == LY_STMT_MIN_ELEMENTS);
1487
0
    assert(parent_stmt == LY_STMT_LEAF_LIST || parent_stmt == LY_STMT_REFINE || parent_stmt == LY_STMT_LIST ||
1488
0
            parent_stmt == LY_STMT_DEVIATE);
1489
1490
0
    if (parent_stmt == LY_STMT_LEAF_LIST) {
1491
0
        lim = (cur_stmt == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_leaflist *)dest)->max : &((struct lysp_node_leaflist *)dest)->min;
1492
0
        flags = &((struct lysp_node_leaflist *)dest)->flags;
1493
0
        exts = &((struct lysp_node_leaflist *)dest)->exts;
1494
0
    } else if (parent_stmt == LY_STMT_REFINE) {
1495
0
        lim = (cur_stmt == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_refine *)dest)->max : &((struct lysp_refine *)dest)->min;
1496
0
        flags = &((struct lysp_refine *)dest)->flags;
1497
0
        exts = &((struct lysp_refine *)dest)->exts;
1498
0
    } else if (parent_stmt == LY_STMT_LIST) {
1499
0
        lim = (cur_stmt == LY_STMT_MAX_ELEMENTS) ? &((struct lysp_node_list *)dest)->max : &((struct lysp_node_list *)dest)->min;
1500
0
        flags = &((struct lysp_node_list *)dest)->flags;
1501
0
        exts = &((struct lysp_node_list *)dest)->exts;
1502
0
    } else {
1503
0
        lim = ((struct minmax_dev_meta *)dest)->lim;
1504
0
        flags = ((struct minmax_dev_meta *)dest)->flags;
1505
0
        exts = ((struct minmax_dev_meta *)dest)->exts;
1506
0
    }
1507
1508
0
    if (cur_stmt == LY_STMT_MAX_ELEMENTS) {
1509
0
        LY_CHECK_RET(yin_parse_maxelements(ctx, lim, flags, exts));
1510
0
    } else {
1511
0
        LY_CHECK_RET(yin_parse_minelements(ctx, lim, flags, exts));
1512
0
    }
1513
1514
0
    return LY_SUCCESS;
1515
0
}
1516
1517
/**
1518
 * @brief Parse ordered-by element.
1519
 *
1520
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1521
 * @param[in] parent Current statement parent.
1522
 * @param[out] flags Flags to write to.
1523
 * @param[in,out] exts Extension instances to add to.
1524
 * @return LY_ERR values.
1525
 */
1526
static LY_ERR
1527
yin_parse_orderedby(struct lysp_yin_ctx *ctx, const void *parent, uint16_t *flags, struct lysp_ext_instance **exts)
1528
0
{
1529
0
    const char *temp_val;
1530
0
    struct yin_subelement subelems[] = {
1531
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1532
0
    };
1533
1534
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1535
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_ORDERED_BY));
1536
0
    if (strcmp(temp_val, "system") == 0) {
1537
0
        *flags |= LYS_ORDBY_SYSTEM;
1538
0
    } else if (strcmp(temp_val, "user") == 0) {
1539
0
        *flags |= LYS_ORDBY_USER;
1540
0
    } else {
1541
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
1542
0
                "ordered-by", "system", "user");
1543
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
1544
0
        return LY_EVALID;
1545
0
    }
1546
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
1547
1548
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_ORDERED_BY, NULL, exts));
1549
1550
0
    return LY_SUCCESS;
1551
0
}
1552
1553
/**
1554
 * @brief Parse any-data or any-xml element.
1555
 *
1556
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1557
 * @param[in] any_stmt Type of current statement, can be set to LY_STMT_ANYDATA or LY_STMT_ANYXML
1558
 * @param[in] node_meta Meta information about parent node and siblings to add to.
1559
 * @return LY_ERR values.
1560
 */
1561
static LY_ERR
1562
yin_parse_any(struct lysp_yin_ctx *ctx, enum ly_stmt any_kw, struct tree_node_meta *node_meta)
1563
0
{
1564
0
    struct lysp_node_anydata *any;
1565
1566
    /* create new sibling */
1567
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, any, next, LY_EMEM);
1568
0
    any->nodetype = (any_kw == LY_STMT_ANYDATA) ? LYS_ANYDATA : LYS_ANYXML;
1569
0
    any->parent = node_meta->parent;
1570
1571
    /* parse argument */
1572
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1573
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &any->name, Y_IDENTIF_ARG, any_kw));
1574
1575
0
    struct yin_subelement subelems[] = {
1576
0
        {LY_STMT_CONFIG, &any->flags, YIN_SUBELEM_UNIQUE},
1577
0
        {LY_STMT_DESCRIPTION, &any->dsc, YIN_SUBELEM_UNIQUE},
1578
0
        {LY_STMT_IF_FEATURE, &any->iffeatures, 0},
1579
0
        {LY_STMT_MANDATORY, &any->flags, YIN_SUBELEM_UNIQUE},
1580
0
        {LY_STMT_MUST, &any->musts, 0},
1581
0
        {LY_STMT_REFERENCE, &any->ref, YIN_SUBELEM_UNIQUE},
1582
0
        {LY_STMT_STATUS, &any->flags, YIN_SUBELEM_UNIQUE},
1583
0
        {LY_STMT_WHEN, &any->when, YIN_SUBELEM_UNIQUE},
1584
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1585
0
    };
1586
1587
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), any, any_kw, NULL, &any->exts));
1588
1589
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1590
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, any->exts));
1591
1592
0
    return LY_SUCCESS;
1593
0
}
1594
1595
/**
1596
 * @brief parse leaf element.
1597
 *
1598
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1599
 * @param[in] node_meta Meta information about parent node and siblings to add to.
1600
 * @return LY_ERR values.
1601
 */
1602
static LY_ERR
1603
yin_parse_leaf(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
1604
0
{
1605
0
    struct lysp_node_leaf *leaf;
1606
1607
    /* create structure new leaf */
1608
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, leaf, next, LY_EMEM);
1609
0
    leaf->nodetype = LYS_LEAF;
1610
0
    leaf->parent = node_meta->parent;
1611
1612
    /* parser argument */
1613
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1614
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &leaf->name, Y_IDENTIF_ARG, LY_STMT_LEAF));
1615
1616
    /* parse content */
1617
0
    struct yin_subelement subelems[] = {
1618
0
        {LY_STMT_CONFIG, &leaf->flags, YIN_SUBELEM_UNIQUE},
1619
0
        {LY_STMT_DEFAULT, &leaf->dflt, YIN_SUBELEM_UNIQUE},
1620
0
        {LY_STMT_DESCRIPTION, &leaf->dsc, YIN_SUBELEM_UNIQUE},
1621
0
        {LY_STMT_IF_FEATURE, &leaf->iffeatures, 0},
1622
0
        {LY_STMT_MANDATORY, &leaf->flags, YIN_SUBELEM_UNIQUE},
1623
0
        {LY_STMT_MUST, &leaf->musts, 0},
1624
0
        {LY_STMT_REFERENCE, &leaf->ref, YIN_SUBELEM_UNIQUE},
1625
0
        {LY_STMT_STATUS, &leaf->flags, YIN_SUBELEM_UNIQUE},
1626
0
        {LY_STMT_TYPE, &leaf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1627
0
        {LY_STMT_UNITS, &leaf->units, YIN_SUBELEM_UNIQUE},
1628
0
        {LY_STMT_WHEN, &leaf->when, YIN_SUBELEM_UNIQUE},
1629
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1630
0
    };
1631
1632
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), leaf, LY_STMT_LEAF, NULL, &leaf->exts));
1633
1634
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1635
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, leaf->exts));
1636
1637
0
    return LY_SUCCESS;
1638
0
}
1639
1640
/**
1641
 * @brief Parse leaf-list element.
1642
 *
1643
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1644
 * @param[in] node_meta Meta information about parent node and siblings to add to.
1645
 * @return LY_ERR values.
1646
 */
1647
static LY_ERR
1648
yin_parse_leaflist(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
1649
0
{
1650
0
    struct lysp_node_leaflist *llist;
1651
1652
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, llist, next, LY_EMEM);
1653
1654
0
    llist->nodetype = LYS_LEAFLIST;
1655
0
    llist->parent = node_meta->parent;
1656
1657
    /* parse argument */
1658
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1659
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &llist->name, Y_IDENTIF_ARG, LY_STMT_LEAF_LIST));
1660
1661
    /* parse content */
1662
0
    struct yin_subelement subelems[] = {
1663
0
        {LY_STMT_CONFIG, &llist->flags, YIN_SUBELEM_UNIQUE},
1664
0
        {LY_STMT_DEFAULT, &llist->dflts, 0},
1665
0
        {LY_STMT_DESCRIPTION, &llist->dsc, YIN_SUBELEM_UNIQUE},
1666
0
        {LY_STMT_IF_FEATURE, &llist->iffeatures, 0},
1667
0
        {LY_STMT_MAX_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1668
0
        {LY_STMT_MIN_ELEMENTS, llist, YIN_SUBELEM_UNIQUE},
1669
0
        {LY_STMT_MUST, &llist->musts, 0},
1670
0
        {LY_STMT_ORDERED_BY, &llist->flags, YIN_SUBELEM_UNIQUE},
1671
0
        {LY_STMT_REFERENCE, &llist->ref, YIN_SUBELEM_UNIQUE},
1672
0
        {LY_STMT_STATUS, &llist->flags, YIN_SUBELEM_UNIQUE},
1673
0
        {LY_STMT_TYPE, &llist->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1674
0
        {LY_STMT_UNITS, &llist->units, YIN_SUBELEM_UNIQUE},
1675
0
        {LY_STMT_WHEN, &llist->when, YIN_SUBELEM_UNIQUE},
1676
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1677
0
    };
1678
1679
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), llist, LY_STMT_LEAF_LIST, NULL, &llist->exts));
1680
1681
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1682
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, llist->exts));
1683
1684
    /* check invalid combination of subelements */
1685
0
    if ((llist->min) && (llist->dflts)) {
1686
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INCHILDSTMSCOMB_YIN, "min-elements", "default", "leaf-list");
1687
0
        return LY_EVALID;
1688
0
    }
1689
0
    if (llist->max && (llist->min > llist->max)) {
1690
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_MINMAX, llist->min, llist->max);
1691
0
        return LY_EVALID;
1692
0
    }
1693
1694
0
    return LY_SUCCESS;
1695
0
}
1696
1697
/**
1698
 * @brief Parse typedef element.
1699
 *
1700
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1701
 * @param[in] typedef_meta Meta information about parent node and typedefs to add to.
1702
 * @return LY_ERR values.
1703
 */
1704
static LY_ERR
1705
yin_parse_typedef(struct lysp_yin_ctx *ctx, struct tree_node_meta *typedef_meta)
1706
0
{
1707
0
    struct lysp_tpdf *tpdf;
1708
0
    struct lysp_tpdf **tpdfs = (struct lysp_tpdf **)typedef_meta->nodes;
1709
1710
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *tpdfs, tpdf, LY_EMEM);
1711
1712
    /* parse argument */
1713
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1714
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &tpdf->name, Y_IDENTIF_ARG, LY_STMT_TYPEDEF));
1715
1716
    /* parse content */
1717
0
    struct yin_subelement subelems[] = {
1718
0
        {LY_STMT_DEFAULT, &tpdf->dflt, YIN_SUBELEM_UNIQUE},
1719
0
        {LY_STMT_DESCRIPTION, &tpdf->dsc, YIN_SUBELEM_UNIQUE},
1720
0
        {LY_STMT_REFERENCE, &tpdf->ref, YIN_SUBELEM_UNIQUE},
1721
0
        {LY_STMT_STATUS, &tpdf->flags, YIN_SUBELEM_UNIQUE},
1722
0
        {LY_STMT_TYPE, &tpdf->type, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_MANDATORY},
1723
0
        {LY_STMT_UNITS, &tpdf->units, YIN_SUBELEM_UNIQUE},
1724
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1725
0
    };
1726
1727
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), tpdf, LY_STMT_TYPEDEF, NULL, &tpdf->exts));
1728
1729
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1730
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, tpdf->exts));
1731
1732
    /* store data for collision check */
1733
0
    if (typedef_meta->parent) {
1734
0
        LY_CHECK_RET(ly_set_add(&ctx->tpdfs_nodes, typedef_meta->parent, 0, NULL));
1735
0
    }
1736
1737
0
    return LY_SUCCESS;
1738
0
}
1739
1740
/**
1741
 * @brief Parse refine element.
1742
 *
1743
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1744
 * @param[in,out] refines Refines to add to.
1745
 * @return LY_ERR values.
1746
 */
1747
static LY_ERR
1748
yin_parse_refine(struct lysp_yin_ctx *ctx, struct lysp_refine **refines)
1749
0
{
1750
0
    struct lysp_refine *rf;
1751
1752
    /* allocate new refine */
1753
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *refines, rf, LY_EMEM);
1754
1755
    /* parse attribute */
1756
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1757
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &rf->nodeid, Y_STR_ARG, LY_STMT_REFINE));
1758
0
    CHECK_NONEMPTY(ctx, strlen(rf->nodeid), "refine");
1759
1760
    /* parse content */
1761
0
    struct yin_subelement subelems[] = {
1762
0
        {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
1763
0
        {LY_STMT_DEFAULT, &rf->dflts, 0},
1764
0
        {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
1765
0
        {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
1766
0
        {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
1767
0
        {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1768
0
        {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
1769
0
        {LY_STMT_MUST, &rf->musts, 0},
1770
0
        {LY_STMT_PRESENCE, &rf->presence, YIN_SUBELEM_UNIQUE},
1771
0
        {LY_STMT_REFERENCE, &rf->ref, YIN_SUBELEM_UNIQUE},
1772
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1773
0
    };
1774
1775
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rf, LY_STMT_REFINE, NULL, &rf->exts));
1776
1777
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1778
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, rf->exts));
1779
1780
0
    return LY_SUCCESS;
1781
0
}
1782
1783
/**
1784
 * @brief Parse uses element.
1785
 *
1786
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1787
 * @param[in] node_meta Meta information about parent node and siblings to add to.
1788
 * @return LY_ERR values.
1789
 */
1790
static LY_ERR
1791
yin_parse_uses(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
1792
0
{
1793
0
    struct lysp_node_uses *uses;
1794
1795
    /* create new uses */
1796
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, uses, next, LY_EMEM);
1797
0
    uses->nodetype = LYS_USES;
1798
0
    uses->parent = node_meta->parent;
1799
1800
    /* parse argument */
1801
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1802
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &uses->name, Y_PREF_IDENTIF_ARG, LY_STMT_USES));
1803
1804
    /* parse content */
1805
0
    struct tree_node_meta augments = {(struct lysp_node *)uses, (struct lysp_node **)&uses->augments};
1806
0
    struct yin_subelement subelems[] = {
1807
0
        {LY_STMT_AUGMENT, &augments, 0},
1808
0
        {LY_STMT_DESCRIPTION, &uses->dsc, YIN_SUBELEM_UNIQUE},
1809
0
        {LY_STMT_IF_FEATURE, &uses->iffeatures, 0},
1810
0
        {LY_STMT_REFERENCE, &uses->ref, YIN_SUBELEM_UNIQUE},
1811
0
        {LY_STMT_REFINE, &uses->refines, 0},
1812
0
        {LY_STMT_STATUS, &uses->flags, YIN_SUBELEM_UNIQUE},
1813
0
        {LY_STMT_WHEN, &uses->when, YIN_SUBELEM_UNIQUE},
1814
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1815
0
    };
1816
1817
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), uses, LY_STMT_USES, NULL, &uses->exts));
1818
1819
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1820
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, uses->exts));
1821
1822
0
    return LY_SUCCESS;
1823
0
}
1824
1825
/**
1826
 * @brief Parse revision element.
1827
 *
1828
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1829
 * @param[in,out] revs Parsed revisions to add to.
1830
 * @return LY_ERR values.
1831
 */
1832
static LY_ERR
1833
yin_parse_revision(struct lysp_yin_ctx *ctx, struct lysp_revision **revs)
1834
0
{
1835
0
    struct lysp_revision *rev;
1836
0
    const char *temp_date = NULL;
1837
1838
    /* allocate new reivison */
1839
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *revs, rev, LY_EMEM);
1840
1841
    /* parse argument */
1842
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1843
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_date, Y_STR_ARG, LY_STMT_REVISION));
1844
    /* check value */
1845
0
    if (lysp_check_date((struct lysp_ctx *)ctx, temp_date, strlen(temp_date), "revision")) {
1846
0
        lysdict_remove(ctx->xmlctx->ctx, temp_date);
1847
0
        return LY_EVALID;
1848
0
    }
1849
0
    memcpy(rev->date, temp_date, LY_REV_SIZE);
1850
0
    lysdict_remove(ctx->xmlctx->ctx, temp_date);
1851
1852
    /* parse content */
1853
0
    struct yin_subelement subelems[] = {
1854
0
        {LY_STMT_DESCRIPTION, &rev->dsc, YIN_SUBELEM_UNIQUE},
1855
0
        {LY_STMT_REFERENCE, &rev->ref, YIN_SUBELEM_UNIQUE},
1856
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1857
0
    };
1858
1859
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rev, LY_STMT_REVISION, NULL, &rev->exts));
1860
1861
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1862
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, rev->exts));
1863
1864
0
    return LY_SUCCESS;
1865
0
}
1866
1867
/**
1868
 * @brief Parse include element.
1869
 *
1870
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1871
 * @param[in,out] inc_meta Meta informatinou about module/submodule name and includes to add to.
1872
 * @return LY_ERR values.
1873
 */
1874
static LY_ERR
1875
yin_parse_include(struct lysp_yin_ctx *ctx, struct include_meta *inc_meta)
1876
0
{
1877
0
    struct lysp_include *inc;
1878
1879
    /* allocate new include */
1880
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *inc_meta->includes, inc, LY_EMEM);
1881
1882
    /* parse argument */
1883
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1884
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &inc->name, Y_IDENTIF_ARG, LY_STMT_INCLUDE));
1885
1886
    /* submodules share the namespace with the module names, so there must not be
1887
     * a module of the same name in the context, no need for revision matching */
1888
0
    if (!strcmp(inc_meta->name, inc->name) || ly_ctx_get_module_latest(ctx->xmlctx->ctx, inc->name)) {
1889
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", inc->name);
1890
0
        return LY_EVALID;
1891
0
    }
1892
1893
    /* parse content */
1894
0
    struct yin_subelement subelems[] = {
1895
0
        {LY_STMT_DESCRIPTION, &inc->dsc, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1896
0
        {LY_STMT_REFERENCE, &inc->ref, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_VER2},
1897
0
        {LY_STMT_REVISION_DATE, &inc->rev, YIN_SUBELEM_UNIQUE},
1898
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
1899
0
    };
1900
1901
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), inc, LY_STMT_INCLUDE, NULL, &inc->exts));
1902
1903
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
1904
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, inc->exts));
1905
1906
0
    return LY_SUCCESS;
1907
0
}
1908
1909
/**
1910
 * @brief Parse revision-date element.
1911
 *
1912
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1913
 * @param[in,out] rev Array to store the parsed value in.
1914
 * @param[in,out] exts Extension instances to add to.
1915
 * @return LY_ERR values.
1916
 */
1917
static LY_ERR
1918
yin_parse_revision_date(struct lysp_yin_ctx *ctx, char *rev, struct lysp_ext_instance **exts)
1919
0
{
1920
0
    const char *temp_rev;
1921
0
    struct yin_subelement subelems[] = {
1922
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1923
0
    };
1924
1925
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1926
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, &temp_rev, Y_STR_ARG, LY_STMT_REVISION_DATE));
1927
0
    LY_CHECK_ERR_RET(lysp_check_date((struct lysp_ctx *)ctx, temp_rev, strlen(temp_rev), "revision-date") != LY_SUCCESS,
1928
0
            lysdict_remove(ctx->xmlctx->ctx, temp_rev), LY_EVALID);
1929
1930
0
    strcpy(rev, temp_rev);
1931
0
    lysdict_remove(ctx->xmlctx->ctx, temp_rev);
1932
1933
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), rev, LY_STMT_REVISION_DATE, NULL, exts));
1934
1935
0
    return LY_SUCCESS;
1936
0
}
1937
1938
/**
1939
 * @brief Parse config element.
1940
 *
1941
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1942
 * @param[in,out] flags Flags to add to.
1943
 * @param[in,out] exts Extension instances to add to.
1944
 * @return LY_ERR values.
1945
 */
1946
static LY_ERR
1947
yin_parse_config(struct lysp_yin_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
1948
0
{
1949
0
    const char *temp_val = NULL;
1950
0
    struct yin_subelement subelems[] = {
1951
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1952
0
    };
1953
1954
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1955
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_CONFIG));
1956
0
    if (strcmp(temp_val, "true") == 0) {
1957
0
        *flags |= LYS_CONFIG_W;
1958
0
    } else if (strcmp(temp_val, "false") == 0) {
1959
0
        *flags |= LYS_CONFIG_R;
1960
0
    } else {
1961
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value", "config",
1962
0
                "true", "false");
1963
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
1964
0
        return LY_EVALID;
1965
0
    }
1966
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
1967
1968
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), flags, LY_STMT_CONFIG, NULL, exts));
1969
1970
0
    return LY_SUCCESS;
1971
0
}
1972
1973
/**
1974
 * @brief Parse yang-version element.
1975
 *
1976
 * @param[in,out] ctx YIN parser context for logging and to store current state.
1977
 * @param[in] parent Current statement parent.
1978
 * @param[out] version Storage for the parsed information.
1979
 * @param[in,out] exts Extension instances to add to.
1980
 * @return LY_ERR values.
1981
 */
1982
static LY_ERR
1983
yin_parse_yangversion(struct lysp_yin_ctx *ctx, const void *parent, uint8_t *version, struct lysp_ext_instance **exts)
1984
0
{
1985
0
    const char *temp_version = NULL;
1986
0
    struct yin_subelement subelems[] = {
1987
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
1988
0
    };
1989
1990
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
1991
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_version, Y_STR_ARG, LY_STMT_YANG_VERSION));
1992
0
    if (strcmp(temp_version, "1") == 0) {
1993
0
        *version = LYS_VERSION_1_0;
1994
0
    } else if (strcmp(temp_version, "1.1") == 0) {
1995
0
        *version = LYS_VERSION_1_1;
1996
0
    } else {
1997
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_version, "value",
1998
0
                "yang-version", "1", "1.1");
1999
0
        lysdict_remove(ctx->xmlctx->ctx, temp_version);
2000
0
        return LY_EVALID;
2001
0
    }
2002
0
    lysdict_remove(ctx->xmlctx->ctx, temp_version);
2003
2004
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_YANG_VERSION, NULL, exts));
2005
2006
0
    return LY_SUCCESS;
2007
0
}
2008
2009
/**
2010
 * @brief Parse import element.
2011
 *
2012
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2013
 * @param[in,out] imp_meta Meta information about prefix and imports to add to.
2014
 * @return LY_ERR values.
2015
 */
2016
static LY_ERR
2017
yin_parse_import(struct lysp_yin_ctx *ctx, struct import_meta *imp_meta)
2018
0
{
2019
0
    struct lysp_import *imp;
2020
2021
    /* allocate new element in sized array for import */
2022
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *imp_meta->imports, imp, LY_EMEM);
2023
2024
0
    struct yin_subelement subelems[] = {
2025
0
        {LY_STMT_DESCRIPTION, &imp->dsc, YIN_SUBELEM_UNIQUE},
2026
0
        {LY_STMT_PREFIX, &imp->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE},
2027
0
        {LY_STMT_REFERENCE, &imp->ref, YIN_SUBELEM_UNIQUE},
2028
0
        {LY_STMT_REVISION_DATE, imp->rev, YIN_SUBELEM_UNIQUE},
2029
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2030
0
    };
2031
2032
    /* parse import attributes */
2033
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2034
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &imp->name, Y_IDENTIF_ARG, LY_STMT_IMPORT));
2035
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), imp, LY_STMT_IMPORT, NULL, &imp->exts));
2036
2037
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2038
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, imp->exts));
2039
2040
    /* check prefix validity */
2041
0
    LY_CHECK_RET(lysp_check_prefix((struct lysp_ctx *)ctx, *imp_meta->imports, imp_meta->prefix, &imp->prefix), LY_EVALID);
2042
2043
0
    return LY_SUCCESS;
2044
0
}
2045
2046
/**
2047
 * @brief Parse mandatory element.
2048
 *
2049
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2050
 * @param[in,out] flags Flags to add to.
2051
 * @param[in,out] exts Extension instances to add to.
2052
 * @return LY_ERR values.
2053
 */
2054
static LY_ERR
2055
yin_parse_mandatory(struct lysp_yin_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
2056
0
{
2057
0
    const char *temp_val = NULL;
2058
0
    struct yin_subelement subelems[] = {
2059
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2060
0
    };
2061
2062
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2063
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_MANDATORY));
2064
0
    if (strcmp(temp_val, "true") == 0) {
2065
0
        *flags |= LYS_MAND_TRUE;
2066
0
    } else if (strcmp(temp_val, "false") == 0) {
2067
0
        *flags |= LYS_MAND_FALSE;
2068
0
    } else {
2069
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
2070
0
                "mandatory", "true", "false");
2071
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
2072
0
        return LY_EVALID;
2073
0
    }
2074
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
2075
2076
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), flags, LY_STMT_MANDATORY, NULL, exts));
2077
2078
0
    return LY_SUCCESS;
2079
0
}
2080
2081
/**
2082
 * @brief Parse status element.
2083
 *
2084
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2085
 * @param[in,out] flags Flags to add to.
2086
 * @param[in,out] exts Extension instances to add to.
2087
 * @return LY_ERR values.
2088
 */
2089
static LY_ERR
2090
yin_parse_status(struct lysp_yin_ctx *ctx, uint16_t *flags, struct lysp_ext_instance **exts)
2091
0
{
2092
0
    const char *value = NULL;
2093
0
    struct yin_subelement subelems[] = {
2094
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2095
0
    };
2096
2097
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2098
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &value, Y_STR_ARG, LY_STMT_STATUS));
2099
0
    if (strcmp(value, "current") == 0) {
2100
0
        *flags |= LYS_STATUS_CURR;
2101
0
    } else if (strcmp(value, "deprecated") == 0) {
2102
0
        *flags |= LYS_STATUS_DEPRC;
2103
0
    } else if (strcmp(value, "obsolete") == 0) {
2104
0
        *flags |= LYS_STATUS_OBSLT;
2105
0
    } else {
2106
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS3, value, "value",
2107
0
                "status", "current", "deprecated", "obsolete");
2108
0
        lysdict_remove(ctx->xmlctx->ctx, value);
2109
0
        return LY_EVALID;
2110
0
    }
2111
0
    lysdict_remove(ctx->xmlctx->ctx, value);
2112
2113
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), flags, LY_STMT_STATUS, NULL, exts));
2114
2115
0
    return LY_SUCCESS;
2116
0
}
2117
2118
/**
2119
 * @brief Parse when element.
2120
 *
2121
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2122
 * @param[out] when_p When pointer to parse to.
2123
 * @return LY_ERR values.
2124
 */
2125
static LY_ERR
2126
yin_parse_when(struct lysp_yin_ctx *ctx, struct lysp_when **when_p)
2127
0
{
2128
0
    struct lysp_when *when;
2129
0
    LY_ERR ret = LY_SUCCESS;
2130
2131
0
    when = calloc(1, sizeof *when);
2132
0
    LY_CHECK_ERR_RET(!when, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
2133
2134
0
    ret = lyxml_ctx_next(ctx->xmlctx);
2135
0
    LY_CHECK_ERR_RET(ret, free(when), ret);
2136
2137
0
    ret = yin_parse_attribute(ctx, YIN_ARG_CONDITION, &when->cond, Y_STR_ARG, LY_STMT_WHEN);
2138
0
    LY_CHECK_ERR_RET(ret, free(when), ret);
2139
2140
0
    *when_p = when;
2141
0
    struct yin_subelement subelems[] = {
2142
0
        {LY_STMT_DESCRIPTION, &when->dsc, YIN_SUBELEM_UNIQUE},
2143
0
        {LY_STMT_REFERENCE, &when->ref, YIN_SUBELEM_UNIQUE},
2144
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2145
0
    };
2146
2147
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), when, LY_STMT_WHEN, NULL, &when->exts));
2148
2149
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2150
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, when->exts));
2151
2152
0
    return LY_SUCCESS;
2153
0
}
2154
2155
/**
2156
 * @brief Parse yin-elemenet element.
2157
 *
2158
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2159
 * @param[in] parent Current statement parent.
2160
 * @param[in,out] flags Flags to add to.
2161
 * @param[in,out] exts Extension instances to add to.
2162
 * @return LY_ERR values.
2163
 */
2164
static LY_ERR
2165
yin_parse_yin_element(struct lysp_yin_ctx *ctx, const void *parent, uint16_t *flags, struct lysp_ext_instance **exts)
2166
0
{
2167
0
    const char *temp_val = NULL;
2168
0
    struct yin_subelement subelems[] = {
2169
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2170
0
    };
2171
2172
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2173
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_YIN_ELEMENT));
2174
0
    if (strcmp(temp_val, "true") == 0) {
2175
0
        *flags |= LYS_YINELEM_TRUE;
2176
0
    } else if (strcmp(temp_val, "false") == 0) {
2177
0
        *flags |= LYS_YINELEM_FALSE;
2178
0
    } else {
2179
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS2, temp_val, "value",
2180
0
                "yin-element", "true", "false");
2181
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
2182
0
        return LY_EVALID;
2183
0
    }
2184
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
2185
2186
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_YIN_ELEMENT, NULL, exts));
2187
2188
0
    return LY_SUCCESS;
2189
0
}
2190
2191
/**
2192
 * @brief Parse argument element.
2193
 *
2194
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2195
 * @param[in] parent Current statement parent.
2196
 * @param[in,out] arg_meta Meta information about destination of parsed data.
2197
 * @param[in,out] exts Extension instances to add to.
2198
 * @return LY_ERR values.
2199
 */
2200
static LY_ERR
2201
yin_parse_argument(struct lysp_yin_ctx *ctx, const void *parent, struct yin_argument_meta *arg_meta, struct lysp_ext_instance **exts)
2202
0
{
2203
0
    struct yin_subelement subelems[] = {
2204
0
        {LY_STMT_YIN_ELEMENT, arg_meta->flags, YIN_SUBELEM_UNIQUE},
2205
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2206
0
    };
2207
2208
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2209
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg_meta->argument, Y_IDENTIF_ARG, LY_STMT_ARGUMENT));
2210
2211
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), parent, LY_STMT_ARGUMENT, NULL, exts));
2212
2213
0
    return LY_SUCCESS;
2214
0
}
2215
2216
/**
2217
 * @brief Parse the extension statement.
2218
 *
2219
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2220
 * @param[in,out] extensions Extensions to add to.
2221
 * @return LY_ERR values.
2222
 */
2223
static LY_ERR
2224
yin_parse_extension(struct lysp_yin_ctx *ctx, struct lysp_ext **extensions)
2225
0
{
2226
0
    struct lysp_ext *ex;
2227
2228
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *extensions, ex, LY_EMEM);
2229
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2230
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ex->name, Y_IDENTIF_ARG, LY_STMT_EXTENSION));
2231
2232
0
    struct yin_argument_meta arg_info = {&ex->flags, &ex->argname};
2233
0
    struct yin_subelement subelems[] = {
2234
0
        {LY_STMT_ARGUMENT, &arg_info, YIN_SUBELEM_UNIQUE},
2235
0
        {LY_STMT_DESCRIPTION, &ex->dsc, YIN_SUBELEM_UNIQUE},
2236
0
        {LY_STMT_REFERENCE, &ex->ref, YIN_SUBELEM_UNIQUE},
2237
0
        {LY_STMT_STATUS, &ex->flags, YIN_SUBELEM_UNIQUE},
2238
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2239
0
    };
2240
2241
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), ex, LY_STMT_EXTENSION, NULL, &ex->exts));
2242
2243
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2244
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, ex->exts));
2245
2246
0
    return LY_SUCCESS;
2247
0
}
2248
2249
/**
2250
 * @brief Parse feature element.
2251
 *
2252
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2253
 * @param[in,out] features Features to add to.
2254
 * @return LY_ERR values.
2255
 */
2256
static LY_ERR
2257
yin_parse_feature(struct lysp_yin_ctx *ctx, struct lysp_feature **features)
2258
0
{
2259
0
    struct lysp_feature *feat;
2260
2261
    /* allocate new feature */
2262
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *features, feat, LY_EMEM);
2263
2264
    /* parse argument */
2265
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2266
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &feat->name, Y_IDENTIF_ARG, LY_STMT_FEATURE));
2267
2268
    /* parse content */
2269
0
    struct yin_subelement subelems[] = {
2270
0
        {LY_STMT_DESCRIPTION, &feat->dsc, YIN_SUBELEM_UNIQUE},
2271
0
        {LY_STMT_IF_FEATURE, &feat->iffeatures, 0},
2272
0
        {LY_STMT_REFERENCE, &feat->ref, YIN_SUBELEM_UNIQUE},
2273
0
        {LY_STMT_STATUS, &feat->flags, YIN_SUBELEM_UNIQUE},
2274
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2275
0
    };
2276
2277
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), feat, LY_STMT_FEATURE, NULL, &feat->exts));
2278
2279
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2280
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, feat->exts));
2281
2282
0
    return LY_SUCCESS;
2283
0
}
2284
2285
/**
2286
 * @brief Parse identity element.
2287
 *
2288
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2289
 * @param[in,out] identities Identities to add to.
2290
 * @return LY_ERR values.
2291
 */
2292
static LY_ERR
2293
yin_parse_identity(struct lysp_yin_ctx *ctx, struct lysp_ident **identities)
2294
0
{
2295
0
    struct lysp_ident *ident;
2296
2297
    /* allocate new identity */
2298
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *identities, ident, LY_EMEM);
2299
2300
    /* parse argument */
2301
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2302
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &ident->name, Y_IDENTIF_ARG, LY_STMT_IDENTITY));
2303
2304
    /* parse content */
2305
0
    struct yin_subelement subelems[] = {
2306
0
        {LY_STMT_BASE, &ident->bases, 0},
2307
0
        {LY_STMT_DESCRIPTION, &ident->dsc, YIN_SUBELEM_UNIQUE},
2308
0
        {LY_STMT_IF_FEATURE, &ident->iffeatures, YIN_SUBELEM_VER2},
2309
0
        {LY_STMT_REFERENCE, &ident->ref, YIN_SUBELEM_UNIQUE},
2310
0
        {LY_STMT_STATUS, &ident->flags, YIN_SUBELEM_UNIQUE},
2311
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2312
0
    };
2313
2314
0
    LY_CHECK_RET(yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), ident, LY_STMT_IDENTITY, NULL, &ident->exts));
2315
2316
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2317
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, ident->exts));
2318
2319
0
    return LY_SUCCESS;
2320
0
}
2321
2322
/**
2323
 * @brief Parse list element.
2324
 *
2325
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2326
 * @param[in] node_meta Meta information about parent node and siblings to add to.
2327
 * @return LY_ERR values.
2328
 */
2329
static LY_ERR
2330
yin_parse_list(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
2331
0
{
2332
0
    struct lysp_node_list *list;
2333
0
    LY_ERR ret = LY_SUCCESS;
2334
0
    struct yin_subelement *subelems = NULL;
2335
0
    size_t subelems_size;
2336
2337
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, list, next, LY_EMEM);
2338
0
    list->nodetype = LYS_LIST;
2339
0
    list->parent = node_meta->parent;
2340
2341
    /* parse argument */
2342
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2343
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &list->name, Y_IDENTIF_ARG, LY_STMT_LIST));
2344
2345
    /* parse list content */
2346
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 25, (struct lysp_node *)list, &subelems,
2347
0
            LY_STMT_ACTION, &list->actions, 0,
2348
0
            LY_STMT_ANYDATA, &list->child, 0,
2349
0
            LY_STMT_ANYXML, &list->child, 0,
2350
0
            LY_STMT_CHOICE, &list->child, 0,
2351
0
            LY_STMT_CONFIG, &list->flags, YIN_SUBELEM_UNIQUE,
2352
0
            LY_STMT_CONTAINER, &list->child, 0,
2353
0
            LY_STMT_DESCRIPTION, &list->dsc, YIN_SUBELEM_UNIQUE,
2354
0
            LY_STMT_GROUPING, &list->groupings, 0,
2355
0
            LY_STMT_IF_FEATURE, &list->iffeatures, 0,
2356
0
            LY_STMT_KEY, &list->key, YIN_SUBELEM_UNIQUE,
2357
0
            LY_STMT_LEAF, &list->child, 0,
2358
0
            LY_STMT_LEAF_LIST, &list->child, 0,
2359
0
            LY_STMT_LIST, &list->child, 0,
2360
0
            LY_STMT_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2361
0
            LY_STMT_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE,
2362
0
            LY_STMT_MUST, &list->musts, 0,
2363
0
            LY_STMT_NOTIFICATION, &list->notifs, 0,
2364
0
            LY_STMT_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE,
2365
0
            LY_STMT_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE,
2366
0
            LY_STMT_STATUS, &list->flags, YIN_SUBELEM_UNIQUE,
2367
0
            LY_STMT_TYPEDEF, &list->typedefs, 0,
2368
0
            LY_STMT_UNIQUE, &list->uniques, 0,
2369
0
            LY_STMT_USES, &list->child, 0,
2370
0
            LY_STMT_WHEN, &list->when, YIN_SUBELEM_UNIQUE,
2371
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2372
0
    ret = yin_parse_content(ctx, subelems, subelems_size, list, LY_STMT_LIST, NULL, &list->exts);
2373
0
    subelems_deallocator(subelems_size, subelems);
2374
0
    LY_CHECK_RET(ret);
2375
2376
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2377
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, list->exts));
2378
2379
0
    if (list->max && (list->min > list->max)) {
2380
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_MINMAX, list->min, list->max);
2381
0
        return LY_EVALID;
2382
0
    }
2383
2384
0
    return LY_SUCCESS;
2385
0
}
2386
2387
/**
2388
 * @brief Parse notification element.
2389
 *
2390
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2391
 * @param[in,out] notif_meta Meta information about parent node and notifications to add to.
2392
 * @return LY_ERR values.
2393
 */
2394
static LY_ERR
2395
yin_parse_notification(struct lysp_yin_ctx *ctx, struct tree_node_meta *notif_meta)
2396
0
{
2397
0
    struct lysp_node_notif *notif;
2398
0
    struct lysp_node_notif **notifs = (struct lysp_node_notif **)notif_meta->nodes;
2399
0
    LY_ERR ret = LY_SUCCESS;
2400
0
    struct yin_subelement *subelems = NULL;
2401
0
    size_t subelems_size;
2402
2403
    /* allocate new notification */
2404
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, notifs, notif, next, LY_EMEM);
2405
0
    notif->nodetype = LYS_NOTIF;
2406
0
    notif->parent = notif_meta->parent;
2407
2408
    /* parse argument */
2409
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2410
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &notif->name, Y_IDENTIF_ARG, LY_STMT_NOTIFICATION));
2411
2412
    /* parse notification content */
2413
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, (struct lysp_node *)notif, &subelems,
2414
0
            LY_STMT_ANYDATA, &notif->child, 0,
2415
0
            LY_STMT_ANYXML, &notif->child, 0,
2416
0
            LY_STMT_CHOICE, &notif->child, 0,
2417
0
            LY_STMT_CONTAINER, &notif->child, 0,
2418
0
            LY_STMT_DESCRIPTION, &notif->dsc, YIN_SUBELEM_UNIQUE,
2419
0
            LY_STMT_GROUPING, &notif->groupings, 0,
2420
0
            LY_STMT_IF_FEATURE, &notif->iffeatures, 0,
2421
0
            LY_STMT_LEAF, &notif->child, 0,
2422
0
            LY_STMT_LEAF_LIST, &notif->child, 0,
2423
0
            LY_STMT_LIST, &notif->child, 0,
2424
0
            LY_STMT_MUST, &notif->musts, YIN_SUBELEM_VER2,
2425
0
            LY_STMT_REFERENCE, &notif->ref, YIN_SUBELEM_UNIQUE,
2426
0
            LY_STMT_STATUS, &notif->flags, YIN_SUBELEM_UNIQUE,
2427
0
            LY_STMT_TYPEDEF, &notif->typedefs, 0,
2428
0
            LY_STMT_USES, &notif->child, 0,
2429
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2430
2431
0
    ret = yin_parse_content(ctx, subelems, subelems_size, notif, LY_STMT_NOTIFICATION, NULL, &notif->exts);
2432
0
    subelems_deallocator(subelems_size, subelems);
2433
0
    LY_CHECK_RET(ret);
2434
2435
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2436
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, notif->exts));
2437
2438
0
    return LY_SUCCESS;
2439
0
}
2440
2441
/**
2442
 * @brief Parse grouping element.
2443
 *
2444
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2445
 * @param[in,out] gr_meta Meta information about parent node and groupings to add to.
2446
 * @return LY_ERR values.
2447
 */
2448
static LY_ERR
2449
yin_parse_grouping(struct lysp_yin_ctx *ctx, struct tree_node_meta *gr_meta)
2450
0
{
2451
0
    struct lysp_node_grp *grp;
2452
0
    struct lysp_node_grp **grps = (struct lysp_node_grp **)gr_meta->nodes;
2453
0
    LY_ERR ret = LY_SUCCESS;
2454
0
    struct yin_subelement *subelems = NULL;
2455
0
    size_t subelems_size;
2456
2457
    /* create new grouping */
2458
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, grps, grp, next, LY_EMEM);
2459
0
    grp->nodetype = LYS_GROUPING;
2460
0
    grp->parent = gr_meta->parent;
2461
2462
    /* parse argument */
2463
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2464
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &grp->name, Y_IDENTIF_ARG, LY_STMT_GROUPING));
2465
2466
    /* parse grouping content */
2467
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 16, &grp->node, &subelems,
2468
0
            LY_STMT_ACTION, &grp->actions, 0,
2469
0
            LY_STMT_ANYDATA, &grp->child, 0,
2470
0
            LY_STMT_ANYXML, &grp->child, 0,
2471
0
            LY_STMT_CHOICE, &grp->child, 0,
2472
0
            LY_STMT_CONTAINER, &grp->child, 0,
2473
0
            LY_STMT_DESCRIPTION, &grp->dsc, YIN_SUBELEM_UNIQUE,
2474
0
            LY_STMT_GROUPING, &grp->groupings, 0,
2475
0
            LY_STMT_LEAF, &grp->child, 0,
2476
0
            LY_STMT_LEAF_LIST, &grp->child, 0,
2477
0
            LY_STMT_LIST, &grp->child, 0,
2478
0
            LY_STMT_NOTIFICATION, &grp->notifs, 0,
2479
0
            LY_STMT_REFERENCE, &grp->ref, YIN_SUBELEM_UNIQUE,
2480
0
            LY_STMT_STATUS, &grp->flags, YIN_SUBELEM_UNIQUE,
2481
0
            LY_STMT_TYPEDEF, &grp->typedefs, 0,
2482
0
            LY_STMT_USES, &grp->child, 0,
2483
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2484
0
    ret = yin_parse_content(ctx, subelems, subelems_size, grp, LY_STMT_GROUPING, NULL, &grp->exts);
2485
0
    subelems_deallocator(subelems_size, subelems);
2486
2487
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2488
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, grp->exts));
2489
2490
    /* store data for collision check */
2491
0
    if (!ret && grp->parent) {
2492
0
        LY_CHECK_RET(ly_set_add(&ctx->grps_nodes, grp->parent, 0, NULL));
2493
0
    }
2494
2495
0
    return ret;
2496
0
}
2497
2498
/**
2499
 * @brief Parse container element.
2500
 *
2501
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2502
 * @param[in] node_meta Meta information about parent node and siblings to add to.
2503
 * @return LY_ERR values.
2504
 */
2505
static LY_ERR
2506
yin_parse_container(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
2507
0
{
2508
0
    struct lysp_node_container *cont;
2509
0
    LY_ERR ret = LY_SUCCESS;
2510
0
    struct yin_subelement *subelems = NULL;
2511
0
    size_t subelems_size;
2512
2513
    /* create new container */
2514
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cont, next, LY_EMEM);
2515
0
    cont->nodetype = LYS_CONTAINER;
2516
0
    cont->parent = node_meta->parent;
2517
2518
    /* parse aegument */
2519
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2520
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME,  &cont->name, Y_IDENTIF_ARG, LY_STMT_CONTAINER));
2521
2522
    /* parse container content */
2523
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 21, (struct lysp_node *)cont, &subelems,
2524
0
            LY_STMT_ACTION, &cont->actions, YIN_SUBELEM_VER2,
2525
0
            LY_STMT_ANYDATA, &cont->child, YIN_SUBELEM_VER2,
2526
0
            LY_STMT_ANYXML, &cont->child, 0,
2527
0
            LY_STMT_CHOICE, &cont->child, 0,
2528
0
            LY_STMT_CONFIG, &cont->flags, YIN_SUBELEM_UNIQUE,
2529
0
            LY_STMT_CONTAINER, &cont->child, 0,
2530
0
            LY_STMT_DESCRIPTION, &cont->dsc, YIN_SUBELEM_UNIQUE,
2531
0
            LY_STMT_GROUPING, &cont->groupings, 0,
2532
0
            LY_STMT_IF_FEATURE, &cont->iffeatures, 0,
2533
0
            LY_STMT_LEAF, &cont->child, 0,
2534
0
            LY_STMT_LEAF_LIST, &cont->child, 0,
2535
0
            LY_STMT_LIST, &cont->child, 0,
2536
0
            LY_STMT_MUST, &cont->musts, 0,
2537
0
            LY_STMT_NOTIFICATION, &cont->notifs, YIN_SUBELEM_VER2,
2538
0
            LY_STMT_PRESENCE, &cont->presence, YIN_SUBELEM_UNIQUE,
2539
0
            LY_STMT_REFERENCE, &cont->ref, YIN_SUBELEM_UNIQUE,
2540
0
            LY_STMT_STATUS, &cont->flags, YIN_SUBELEM_UNIQUE,
2541
0
            LY_STMT_TYPEDEF, &cont->typedefs, 0,
2542
0
            LY_STMT_USES, &cont->child, 0,
2543
0
            LY_STMT_WHEN, &cont->when, YIN_SUBELEM_UNIQUE,
2544
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2545
0
    ret = yin_parse_content(ctx, subelems, subelems_size, cont, LY_STMT_CONTAINER, NULL, &cont->exts);
2546
0
    subelems_deallocator(subelems_size, subelems);
2547
0
    LY_CHECK_RET(ret);
2548
2549
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2550
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, cont->exts));
2551
2552
0
    return LY_SUCCESS;
2553
0
}
2554
2555
/**
2556
 * @brief Parse case element.
2557
 *
2558
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2559
 * @param[in] node_meta Meta information about parent node and siblings to add to.
2560
 * @return LY_ERR values.
2561
 */
2562
static LY_ERR
2563
yin_parse_case(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
2564
0
{
2565
0
    struct lysp_node_case *cas;
2566
0
    LY_ERR ret = LY_SUCCESS;
2567
0
    struct yin_subelement *subelems = NULL;
2568
0
    size_t subelems_size;
2569
2570
    /* create new case */
2571
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, cas, next, LY_EMEM);
2572
0
    cas->nodetype = LYS_CASE;
2573
0
    cas->parent = node_meta->parent;
2574
2575
    /* parse argument */
2576
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2577
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &cas->name, Y_IDENTIF_ARG, LY_STMT_CASE));
2578
2579
    /* parse case content */
2580
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 14, (struct lysp_node *)cas, &subelems,
2581
0
            LY_STMT_ANYDATA, &cas->child, YIN_SUBELEM_VER2,
2582
0
            LY_STMT_ANYXML, &cas->child, 0,
2583
0
            LY_STMT_CHOICE, &cas->child, 0,
2584
0
            LY_STMT_CONTAINER, &cas->child, 0,
2585
0
            LY_STMT_DESCRIPTION, &cas->dsc, YIN_SUBELEM_UNIQUE,
2586
0
            LY_STMT_IF_FEATURE, &cas->iffeatures, 0,
2587
0
            LY_STMT_LEAF, &cas->child, 0,
2588
0
            LY_STMT_LEAF_LIST, &cas->child, 0,
2589
0
            LY_STMT_LIST, &cas->child, 0,
2590
0
            LY_STMT_REFERENCE, &cas->ref, YIN_SUBELEM_UNIQUE,
2591
0
            LY_STMT_STATUS, &cas->flags, YIN_SUBELEM_UNIQUE,
2592
0
            LY_STMT_USES, &cas->child, 0,
2593
0
            LY_STMT_WHEN, &cas->when, YIN_SUBELEM_UNIQUE,
2594
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2595
0
    ret = yin_parse_content(ctx, subelems, subelems_size, cas, LY_STMT_CASE, NULL, &cas->exts);
2596
0
    subelems_deallocator(subelems_size, subelems);
2597
0
    LY_CHECK_RET(ret);
2598
2599
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2600
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, cas->exts));
2601
2602
0
    return LY_SUCCESS;
2603
0
}
2604
2605
/**
2606
 * @brief Parse choice element.
2607
 *
2608
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2609
 * @param[in] node_meta Meta information about parent node and siblings to add to.
2610
 * @return LY_ERR values.
2611
 */
2612
LY_ERR
2613
yin_parse_choice(struct lysp_yin_ctx *ctx, struct tree_node_meta *node_meta)
2614
0
{
2615
0
    LY_ERR ret = LY_SUCCESS;
2616
0
    struct yin_subelement *subelems = NULL;
2617
0
    struct lysp_node_choice *choice;
2618
0
    size_t subelems_size;
2619
2620
    /* create new choice */
2621
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, node_meta->nodes, choice, next, LY_EMEM);
2622
2623
0
    choice->nodetype = LYS_CHOICE;
2624
0
    choice->parent = node_meta->parent;
2625
2626
    /* parse argument */
2627
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2628
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &choice->name, Y_IDENTIF_ARG, LY_STMT_CHOICE));
2629
2630
    /* parse choice content */
2631
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)choice, &subelems,
2632
0
            LY_STMT_ANYDATA, &choice->child, YIN_SUBELEM_VER2,
2633
0
            LY_STMT_ANYXML, &choice->child, 0,
2634
0
            LY_STMT_CASE, &choice->child, 0,
2635
0
            LY_STMT_CHOICE, &choice->child, YIN_SUBELEM_VER2,
2636
0
            LY_STMT_CONFIG, &choice->flags, YIN_SUBELEM_UNIQUE,
2637
0
            LY_STMT_CONTAINER, &choice->child, 0,
2638
0
            LY_STMT_DEFAULT, &choice->dflt, YIN_SUBELEM_UNIQUE,
2639
0
            LY_STMT_DESCRIPTION, &choice->dsc, YIN_SUBELEM_UNIQUE,
2640
0
            LY_STMT_IF_FEATURE, &choice->iffeatures, 0,
2641
0
            LY_STMT_LEAF, &choice->child, 0,
2642
0
            LY_STMT_LEAF_LIST, &choice->child, 0,
2643
0
            LY_STMT_LIST, &choice->child, 0,
2644
0
            LY_STMT_MANDATORY, &choice->flags, YIN_SUBELEM_UNIQUE,
2645
0
            LY_STMT_REFERENCE, &choice->ref, YIN_SUBELEM_UNIQUE,
2646
0
            LY_STMT_STATUS, &choice->flags, YIN_SUBELEM_UNIQUE,
2647
0
            LY_STMT_WHEN, &choice->when, YIN_SUBELEM_UNIQUE,
2648
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2649
0
    ret = yin_parse_content(ctx, subelems, subelems_size, choice, LY_STMT_CHOICE, NULL, &choice->exts);
2650
0
    subelems_deallocator(subelems_size, subelems);
2651
0
    LY_CHECK_RET(ret);
2652
2653
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2654
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, choice->exts));
2655
2656
0
    return ret;
2657
0
}
2658
2659
/**
2660
 * @brief Parse input or output element.
2661
 *
2662
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2663
 * @param[in] inout_kw Identification of input/output element.
2664
 * @param[in] inout_meta Meta information about parent node and siblings and input/output pointer to write to.
2665
 * @return LY_ERR values.
2666
 */
2667
static LY_ERR
2668
yin_parse_inout(struct lysp_yin_ctx *ctx, enum ly_stmt inout_kw, struct inout_meta *inout_meta)
2669
0
{
2670
0
    LY_ERR ret = LY_SUCCESS;
2671
0
    struct yin_subelement *subelems = NULL;
2672
0
    size_t subelems_size;
2673
2674
    /* initiate structure */
2675
0
    LY_CHECK_RET(lysdict_insert(PARSER_CTX(ctx), (inout_kw == LY_STMT_INPUT) ? "input" : "output", 0, &inout_meta->inout_p->name));
2676
0
    inout_meta->inout_p->nodetype = (inout_kw == LY_STMT_INPUT) ? LYS_INPUT : LYS_OUTPUT;
2677
0
    inout_meta->inout_p->parent = inout_meta->parent;
2678
2679
    /* check attributes */
2680
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2681
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, NULL, Y_MAYBE_STR_ARG, inout_kw));
2682
2683
    /* parser input/output content */
2684
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 12, (struct lysp_node *)inout_meta->inout_p, &subelems,
2685
0
            LY_STMT_ANYDATA, &inout_meta->inout_p->child, YIN_SUBELEM_VER2,
2686
0
            LY_STMT_ANYXML, &inout_meta->inout_p->child, 0,
2687
0
            LY_STMT_CHOICE, &inout_meta->inout_p->child, 0,
2688
0
            LY_STMT_CONTAINER, &inout_meta->inout_p->child, 0,
2689
0
            LY_STMT_GROUPING, &inout_meta->inout_p->groupings, 0,
2690
0
            LY_STMT_LEAF, &inout_meta->inout_p->child, 0,
2691
0
            LY_STMT_LEAF_LIST, &inout_meta->inout_p->child, 0,
2692
0
            LY_STMT_LIST, &inout_meta->inout_p->child, 0,
2693
0
            LY_STMT_MUST, &inout_meta->inout_p->musts, YIN_SUBELEM_VER2,
2694
0
            LY_STMT_TYPEDEF, &inout_meta->inout_p->typedefs, 0,
2695
0
            LY_STMT_USES, &inout_meta->inout_p->child, 0,
2696
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2697
0
    ret = yin_parse_content(ctx, subelems, subelems_size, inout_meta->inout_p, inout_kw, NULL, &inout_meta->inout_p->exts);
2698
0
    subelems_deallocator(subelems_size, subelems);
2699
0
    LY_CHECK_RET(ret);
2700
2701
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2702
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, inout_meta->inout_p->exts));
2703
2704
0
    if (!inout_meta->inout_p->child) {
2705
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_MISSTMT, "data-def-stmt", lyplg_ext_stmt2str(inout_kw));
2706
0
        return LY_EVALID;
2707
0
    }
2708
2709
0
    return LY_SUCCESS;
2710
0
}
2711
2712
/**
2713
 * @brief Parse action element.
2714
 *
2715
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2716
 * @param[in] act_meta Meta information about parent node and actions to add to.
2717
 * @return LY_ERR values.
2718
 */
2719
static LY_ERR
2720
yin_parse_action(struct lysp_yin_ctx *ctx, struct tree_node_meta *act_meta)
2721
0
{
2722
0
    struct lysp_node_action *act, **acts = (struct lysp_node_action **)act_meta->nodes;
2723
0
    LY_ERR ret = LY_SUCCESS;
2724
0
    struct yin_subelement *subelems = NULL;
2725
0
    size_t subelems_size;
2726
0
    enum ly_stmt kw = act_meta->parent ? LY_STMT_ACTION : LY_STMT_RPC;
2727
2728
    /* create new action */
2729
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, acts, act, next, LY_EMEM);
2730
0
    act->nodetype = act_meta->parent ? LYS_ACTION : LYS_RPC;
2731
0
    act->parent = act_meta->parent;
2732
2733
    /* parse argument */
2734
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2735
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &act->name, Y_IDENTIF_ARG, kw));
2736
2737
    /* parse content */
2738
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 9, (struct lysp_node *)act, &subelems,
2739
0
            LY_STMT_DESCRIPTION, &act->dsc, YIN_SUBELEM_UNIQUE,
2740
0
            LY_STMT_GROUPING, &act->groupings, 0,
2741
0
            LY_STMT_IF_FEATURE, &act->iffeatures, 0,
2742
0
            LY_STMT_INPUT, &act->input, YIN_SUBELEM_UNIQUE,
2743
0
            LY_STMT_OUTPUT, &act->output, YIN_SUBELEM_UNIQUE,
2744
0
            LY_STMT_REFERENCE, &act->ref, YIN_SUBELEM_UNIQUE,
2745
0
            LY_STMT_STATUS, &act->flags, YIN_SUBELEM_UNIQUE,
2746
0
            LY_STMT_TYPEDEF, &act->typedefs, 0,
2747
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2748
0
    ret = (yin_parse_content(ctx, subelems, subelems_size, act, kw, NULL, &act->exts));
2749
0
    subelems_deallocator(subelems_size, subelems);
2750
0
    LY_CHECK_RET(ret);
2751
2752
    /* always initialize inout, they are technically present (needed for later deviations/refines) */
2753
0
    if (!act->input.nodetype) {
2754
0
        act->input.nodetype = LYS_INPUT;
2755
0
        act->input.parent = (struct lysp_node *)act;
2756
0
        LY_CHECK_RET(lysdict_insert(PARSER_CTX(ctx), "input", 0, &act->input.name));
2757
0
    }
2758
0
    if (!act->output.nodetype) {
2759
0
        act->output.nodetype = LYS_OUTPUT;
2760
0
        act->output.parent = (struct lysp_node *)act;
2761
0
        LY_CHECK_RET(lysdict_insert(PARSER_CTX(ctx), "output", 0, &act->output.name));
2762
0
    }
2763
2764
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2765
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, act->exts));
2766
2767
0
    return LY_SUCCESS;
2768
0
}
2769
2770
/**
2771
 * @brief Parse augment element.
2772
 *
2773
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2774
 * @param[in] aug_meta Meta information about parent node and augments to add to.
2775
 * @return LY_ERR values.
2776
 */
2777
static LY_ERR
2778
yin_parse_augment(struct lysp_yin_ctx *ctx, struct tree_node_meta *aug_meta)
2779
0
{
2780
0
    struct lysp_node_augment *aug;
2781
0
    struct lysp_node_augment **augs = (struct lysp_node_augment **)aug_meta->nodes;
2782
0
    LY_ERR ret = LY_SUCCESS;
2783
0
    struct yin_subelement *subelems = NULL;
2784
0
    size_t subelems_size;
2785
2786
    /* create new augment */
2787
0
    LY_LIST_NEW_RET(ctx->xmlctx->ctx, augs, aug, next, LY_EMEM);
2788
0
    aug->nodetype = LYS_AUGMENT;
2789
0
    aug->parent = aug_meta->parent;
2790
2791
    /* parse argument */
2792
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2793
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, LY_STMT_AUGMENT));
2794
0
    CHECK_NONEMPTY((struct lysp_ctx *)ctx, strlen(aug->nodeid), "augment");
2795
2796
    /* parser augment content */
2797
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 17, (struct lysp_node *)aug, &subelems,
2798
0
            LY_STMT_ACTION, &aug->actions, YIN_SUBELEM_VER2,
2799
0
            LY_STMT_ANYDATA, &aug->child, YIN_SUBELEM_VER2,
2800
0
            LY_STMT_ANYXML, &aug->child, 0,
2801
0
            LY_STMT_CASE, &aug->child, 0,
2802
0
            LY_STMT_CHOICE, &aug->child, 0,
2803
0
            LY_STMT_CONTAINER, &aug->child, 0,
2804
0
            LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
2805
0
            LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
2806
0
            LY_STMT_LEAF, &aug->child, 0,
2807
0
            LY_STMT_LEAF_LIST, &aug->child, 0,
2808
0
            LY_STMT_LIST, &aug->child, 0,
2809
0
            LY_STMT_NOTIFICATION, &aug->notifs, YIN_SUBELEM_VER2,
2810
0
            LY_STMT_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE,
2811
0
            LY_STMT_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE,
2812
0
            LY_STMT_USES, &aug->child, 0,
2813
0
            LY_STMT_WHEN, &aug->when, YIN_SUBELEM_UNIQUE,
2814
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
2815
0
    ret = yin_parse_content(ctx, subelems, subelems_size, aug, LY_STMT_AUGMENT, NULL, &aug->exts);
2816
0
    subelems_deallocator(subelems_size, subelems);
2817
0
    LY_CHECK_RET(ret);
2818
2819
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2820
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, aug->exts));
2821
2822
0
    return LY_SUCCESS;
2823
0
}
2824
2825
/**
2826
 * @brief Parse deviate element.
2827
 *
2828
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2829
 * @param[in] deviates Deviates to add to.
2830
 * @return LY_ERR values.
2831
 */
2832
static LY_ERR
2833
yin_parse_deviate(struct lysp_yin_ctx *ctx, struct lysp_deviate **deviates)
2834
0
{
2835
0
    LY_ERR ret = LY_SUCCESS;
2836
0
    uint8_t dev_mod;
2837
0
    const char *temp_val;
2838
0
    struct lysp_deviate *d;
2839
0
    struct lysp_deviate_add *d_add = NULL;
2840
0
    struct lysp_deviate_rpl *d_rpl = NULL;
2841
0
    struct lysp_deviate_del *d_del = NULL;
2842
2843
    /* parse argument */
2844
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
2845
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, LY_STMT_DEVIATE));
2846
2847
0
    if (strcmp(temp_val, "not-supported") == 0) {
2848
0
        dev_mod = LYS_DEV_NOT_SUPPORTED;
2849
0
    } else if (strcmp(temp_val, "add") == 0) {
2850
0
        dev_mod = LYS_DEV_ADD;
2851
0
    } else if (strcmp(temp_val, "replace") == 0) {
2852
0
        dev_mod = LYS_DEV_REPLACE;
2853
0
    } else if (strcmp(temp_val, "delete") == 0) {
2854
0
        dev_mod = LYS_DEV_DELETE;
2855
0
    } else {
2856
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INVAL_YIN VALID_VALS4, temp_val, "value", "deviate",
2857
0
                "not-supported", "add", "replace", "delete");
2858
0
        lysdict_remove(ctx->xmlctx->ctx, temp_val);
2859
0
        return LY_EVALID;
2860
0
    }
2861
0
    lysdict_remove(ctx->xmlctx->ctx, temp_val);
2862
2863
0
    if (dev_mod == LYS_DEV_NOT_SUPPORTED) {
2864
0
        d = calloc(1, sizeof *d);
2865
0
        LY_CHECK_ERR_RET(!d, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
2866
0
        struct yin_subelement subelems[] = {
2867
0
            {LY_STMT_EXTENSION_INSTANCE, NULL, 0}
2868
0
        };
2869
2870
0
        ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
2871
2872
0
    } else if (dev_mod == LYS_DEV_ADD) {
2873
0
        d_add = calloc(1, sizeof *d_add);
2874
0
        LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
2875
0
        d = (struct lysp_deviate *)d_add;
2876
0
        struct minmax_dev_meta min = {&d_add->min, &d_add->flags, &d_add->exts};
2877
0
        struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
2878
0
        struct yin_subelement subelems[] = {
2879
0
            {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
2880
0
            {LY_STMT_DEFAULT, &d_add->dflts, 0},
2881
0
            {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
2882
0
            {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2883
0
            {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2884
0
            {LY_STMT_MUST, &d_add->musts, 0},
2885
0
            {LY_STMT_UNIQUE, &d_add->uniques, 0},
2886
0
            {LY_STMT_UNITS, &d_add->units, YIN_SUBELEM_UNIQUE},
2887
0
            {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2888
0
        };
2889
2890
0
        ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
2891
2892
0
    } else if (dev_mod == LYS_DEV_REPLACE) {
2893
0
        d_rpl = calloc(1, sizeof *d_rpl);
2894
0
        LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
2895
0
        d = (struct lysp_deviate *)d_rpl;
2896
0
        struct minmax_dev_meta min = {&d_rpl->min, &d_rpl->flags, &d_rpl->exts};
2897
0
        struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
2898
0
        struct yin_subelement subelems[] = {
2899
0
            {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2900
0
            {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
2901
0
            {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
2902
0
            {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
2903
0
            {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
2904
0
            {LY_STMT_TYPE, &d_rpl->type, YIN_SUBELEM_UNIQUE},
2905
0
            {LY_STMT_UNITS, &d_rpl->units, YIN_SUBELEM_UNIQUE},
2906
0
            {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2907
0
        };
2908
2909
0
        ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
2910
2911
0
    } else {
2912
0
        d_del = calloc(1, sizeof *d_del);
2913
0
        LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
2914
0
        d = (struct lysp_deviate *)d_del;
2915
0
        struct yin_subelement subelems[] = {
2916
0
            {LY_STMT_DEFAULT, &d_del->dflts, 0},
2917
0
            {LY_STMT_MUST, &d_del->musts, 0},
2918
0
            {LY_STMT_UNIQUE, &d_del->uniques, 0},
2919
0
            {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
2920
0
            {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2921
0
        };
2922
2923
0
        ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), d, LY_STMT_DEVIATE, NULL, &d->exts);
2924
0
    }
2925
0
    LY_CHECK_GOTO(ret, cleanup);
2926
2927
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2928
0
    LY_CHECK_GOTO(ret = yin_unres_exts_add(ctx, d->exts), cleanup);
2929
2930
0
    d->mod = dev_mod;
2931
    /* insert into siblings */
2932
0
    LY_LIST_INSERT(deviates, d, next);
2933
2934
0
    return ret;
2935
2936
0
cleanup:
2937
0
    free(d);
2938
0
    return ret;
2939
0
}
2940
2941
/**
2942
 * @brief Parse deviation element.
2943
 *
2944
 * @param[in,out] ctx YIN parser context for logging and to store current state.
2945
 * @param[in] deviations Deviations to add to.
2946
 * @return LY_ERR values.
2947
 */
2948
static LY_ERR
2949
yin_parse_deviation(struct lysp_yin_ctx *ctx, struct lysp_deviation **deviations)
2950
0
{
2951
0
    LY_ERR ret = LY_SUCCESS;
2952
0
    struct lysp_deviation *dev = NULL;
2953
2954
    /* create new deviation */
2955
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *deviations, dev, LY_EMEM);
2956
2957
    /* parse argument */
2958
0
    LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
2959
0
    LY_CHECK_GOTO(ret = yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, LY_STMT_DEVIATION), cleanup);
2960
0
    CHECK_NONEMPTY((struct lysp_ctx *)ctx, strlen(dev->nodeid), "deviation");
2961
0
    struct yin_subelement subelems[] = {
2962
0
        {LY_STMT_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
2963
0
        {LY_STMT_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
2964
0
        {LY_STMT_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
2965
0
        {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
2966
0
    };
2967
2968
0
    ret = yin_parse_content(ctx, subelems, ly_sizeofarray(subelems), dev, LY_STMT_DEVIATION, NULL, &dev->exts);
2969
0
    LY_CHECK_GOTO(ret, cleanup);
2970
2971
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
2972
0
    LY_CHECK_GOTO(ret = yin_unres_exts_add(ctx, dev->exts), cleanup);
2973
2974
0
cleanup:
2975
0
    if (ret) {
2976
0
        lysp_deviation_free(PARSER_CTX(ctx), dev);
2977
0
        LY_ARRAY_DECREMENT_FREE(*deviations);
2978
0
    }
2979
0
    return ret;
2980
0
}
2981
2982
/**
2983
 * @brief map keyword to keyword-group.
2984
 *
2985
 * @param[in] ctx YIN parser context used for logging.
2986
 * @param[in] kw Keyword that is child of module or submodule.
2987
 * @param[out] group Group of keyword.
2988
 * @return LY_SUCCESS on success LY_EINT if kw can't be mapped to kw_group, should not happen if called correctly.
2989
 */
2990
static LY_ERR
2991
kw2kw_group(struct lysp_yin_ctx *ctx, enum ly_stmt kw, enum yang_module_stmt *group)
2992
0
{
2993
0
    switch (kw) {
2994
    /* module header */
2995
0
    case LY_STMT_NONE:
2996
0
    case LY_STMT_NAMESPACE:
2997
0
    case LY_STMT_PREFIX:
2998
0
    case LY_STMT_BELONGS_TO:
2999
0
    case LY_STMT_YANG_VERSION:
3000
0
        *group = Y_MOD_MODULE_HEADER;
3001
0
        break;
3002
    /* linkage */
3003
0
    case LY_STMT_INCLUDE:
3004
0
    case LY_STMT_IMPORT:
3005
0
        *group = Y_MOD_LINKAGE;
3006
0
        break;
3007
    /* meta */
3008
0
    case LY_STMT_ORGANIZATION:
3009
0
    case LY_STMT_CONTACT:
3010
0
    case LY_STMT_DESCRIPTION:
3011
0
    case LY_STMT_REFERENCE:
3012
0
        *group = Y_MOD_META;
3013
0
        break;
3014
    /* revision */
3015
0
    case LY_STMT_REVISION:
3016
0
        *group = Y_MOD_REVISION;
3017
0
        break;
3018
    /* body */
3019
0
    case LY_STMT_ANYDATA:
3020
0
    case LY_STMT_ANYXML:
3021
0
    case LY_STMT_AUGMENT:
3022
0
    case LY_STMT_CHOICE:
3023
0
    case LY_STMT_CONTAINER:
3024
0
    case LY_STMT_DEVIATION:
3025
0
    case LY_STMT_EXTENSION:
3026
0
    case LY_STMT_FEATURE:
3027
0
    case LY_STMT_GROUPING:
3028
0
    case LY_STMT_IDENTITY:
3029
0
    case LY_STMT_LEAF:
3030
0
    case LY_STMT_LEAF_LIST:
3031
0
    case LY_STMT_LIST:
3032
0
    case LY_STMT_NOTIFICATION:
3033
0
    case LY_STMT_RPC:
3034
0
    case LY_STMT_TYPEDEF:
3035
0
    case LY_STMT_USES:
3036
0
    case LY_STMT_EXTENSION_INSTANCE:
3037
0
        *group = Y_MOD_BODY;
3038
0
        break;
3039
0
    default:
3040
0
        LOGINT(ctx->xmlctx->ctx);
3041
0
        return LY_EINT;
3042
0
    }
3043
3044
0
    return LY_SUCCESS;
3045
0
}
3046
3047
/**
3048
 * @brief Check if relative order of two keywords is valid.
3049
 *
3050
 * @param[in] ctx YIN parser context used for logging.
3051
 * @param[in] cur_stmt Type of current statement.
3052
 * @param[in] next_stmt Type of next statement.
3053
 * @param[in] parent_stmt Type of parent statement, can be se to to LY_STMT_MODULE of LY_STMT_SUBMODULE,
3054
 *            because relative order is required only in module and submodule sub-elements, used for logging.
3055
 * @return LY_SUCCESS on success and LY_EVALID if relative order is invalid.
3056
 */
3057
static LY_ERR
3058
yin_check_relative_order(struct lysp_yin_ctx *ctx, enum ly_stmt cur_stmt, enum ly_stmt next_stmt, enum ly_stmt parent_stmt)
3059
0
{
3060
0
    enum yang_module_stmt gr, next_gr;
3061
3062
0
    assert(parent_stmt == LY_STMT_MODULE || parent_stmt == LY_STMT_SUBMODULE);
3063
3064
0
    if (cur_stmt == LY_STMT_EXTENSION_INSTANCE) {
3065
        /* no order defined */
3066
0
        return LY_SUCCESS;
3067
0
    }
3068
3069
0
    LY_CHECK_RET(kw2kw_group(ctx, cur_stmt, &gr));
3070
0
    LY_CHECK_RET(kw2kw_group(ctx, next_stmt, &next_gr));
3071
3072
0
    if (gr > next_gr) {
3073
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INORDER_YIN, lyplg_ext_stmt2str(parent_stmt), lyplg_ext_stmt2str(next_stmt),
3074
0
                lyplg_ext_stmt2str(cur_stmt));
3075
0
        return LY_EVALID;
3076
0
    }
3077
3078
0
    return LY_SUCCESS;
3079
0
}
3080
3081
/**
3082
 * @brief Parse argument of extension subelement that is classic yang keyword and not another instance of extension.
3083
 *
3084
 * @param[in,out] ctx Yin parser context for logging and to store current state.
3085
 * @param[in] parent_stmt Type of parent statement.
3086
 * @param[out] arg Value to write to.
3087
 * @return LY_ERR values.
3088
 */
3089
static LY_ERR
3090
yin_parse_extension_instance_arg(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, const char **arg)
3091
0
{
3092
0
    enum ly_stmt child;
3093
3094
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3095
3096
0
    switch (parent_stmt) {
3097
0
    case LY_STMT_ACTION:
3098
0
    case LY_STMT_ANYDATA:
3099
0
    case LY_STMT_ANYXML:
3100
0
    case LY_STMT_ARGUMENT:
3101
0
    case LY_STMT_BASE:
3102
0
    case LY_STMT_BIT:
3103
0
    case LY_STMT_CASE:
3104
0
    case LY_STMT_CHOICE:
3105
0
    case LY_STMT_CONTAINER:
3106
0
    case LY_STMT_ENUM:
3107
0
    case LY_STMT_EXTENSION:
3108
0
    case LY_STMT_FEATURE:
3109
0
    case LY_STMT_GROUPING:
3110
0
    case LY_STMT_IDENTITY:
3111
0
    case LY_STMT_IF_FEATURE:
3112
0
    case LY_STMT_LEAF:
3113
0
    case LY_STMT_LEAF_LIST:
3114
0
    case LY_STMT_LIST:
3115
0
    case LY_STMT_MODULE:
3116
0
    case LY_STMT_NOTIFICATION:
3117
0
    case LY_STMT_RPC:
3118
0
    case LY_STMT_SUBMODULE:
3119
0
    case LY_STMT_TYPE:
3120
0
    case LY_STMT_TYPEDEF:
3121
0
    case LY_STMT_UNITS:
3122
0
    case LY_STMT_USES:
3123
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, arg, Y_MAYBE_STR_ARG, parent_stmt));
3124
0
        break;
3125
0
    case LY_STMT_AUGMENT:
3126
0
    case LY_STMT_DEVIATION:
3127
0
    case LY_STMT_REFINE:
3128
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TARGET_NODE, arg, Y_MAYBE_STR_ARG, parent_stmt));
3129
0
        break;
3130
0
    case LY_STMT_CONFIG:
3131
0
    case LY_STMT_DEFAULT:
3132
0
    case LY_STMT_DEVIATE:
3133
0
    case LY_STMT_ERROR_APP_TAG:
3134
0
    case LY_STMT_FRACTION_DIGITS:
3135
0
    case LY_STMT_KEY:
3136
0
    case LY_STMT_LENGTH:
3137
0
    case LY_STMT_MANDATORY:
3138
0
    case LY_STMT_MAX_ELEMENTS:
3139
0
    case LY_STMT_MIN_ELEMENTS:
3140
0
    case LY_STMT_MODIFIER:
3141
0
    case LY_STMT_ORDERED_BY:
3142
0
    case LY_STMT_PATH:
3143
0
    case LY_STMT_PATTERN:
3144
0
    case LY_STMT_POSITION:
3145
0
    case LY_STMT_PREFIX:
3146
0
    case LY_STMT_PRESENCE:
3147
0
    case LY_STMT_RANGE:
3148
0
    case LY_STMT_REQUIRE_INSTANCE:
3149
0
    case LY_STMT_STATUS:
3150
0
    case LY_STMT_VALUE:
3151
0
    case LY_STMT_YANG_VERSION:
3152
0
    case LY_STMT_YIN_ELEMENT:
3153
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_VALUE, arg, Y_MAYBE_STR_ARG, parent_stmt));
3154
0
        break;
3155
0
    case LY_STMT_IMPORT:
3156
0
    case LY_STMT_INCLUDE:
3157
0
    case LY_STMT_BELONGS_TO:
3158
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, arg, Y_MAYBE_STR_ARG, parent_stmt));
3159
0
        break;
3160
0
    case LY_STMT_INPUT:
3161
0
    case LY_STMT_OUTPUT:
3162
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, parent_stmt));
3163
0
        break;
3164
0
    case LY_STMT_MUST:
3165
0
    case LY_STMT_WHEN:
3166
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_CONDITION, arg, Y_MAYBE_STR_ARG, parent_stmt));
3167
0
        break;
3168
0
    case LY_STMT_NAMESPACE:
3169
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_URI, arg, Y_MAYBE_STR_ARG, parent_stmt));
3170
0
        break;
3171
0
    case LY_STMT_REVISION:
3172
0
    case LY_STMT_REVISION_DATE:
3173
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_DATE, arg, Y_MAYBE_STR_ARG, parent_stmt));
3174
0
        break;
3175
0
    case LY_STMT_UNIQUE:
3176
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_TAG, arg, Y_MAYBE_STR_ARG, parent_stmt));
3177
0
        break;
3178
    /* argument is mapped to yin element */
3179
0
    case LY_STMT_CONTACT:
3180
0
    case LY_STMT_DESCRIPTION:
3181
0
    case LY_STMT_ORGANIZATION:
3182
0
    case LY_STMT_REFERENCE:
3183
0
    case LY_STMT_ERROR_MESSAGE:
3184
        /* there shouldn't be any attribute, argument is supposed to be first subelement */
3185
0
        LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NONE, arg, Y_MAYBE_STR_ARG, parent_stmt));
3186
3187
        /* no content */
3188
0
        assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3189
0
        if (ctx->xmlctx->ws_only) {
3190
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3191
0
        }
3192
0
        if (((ctx->xmlctx->status == LYXML_ELEM_CONTENT) && !ctx->xmlctx->ws_only) || (ctx->xmlctx->status != LYXML_ELEMENT)) {
3193
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_FIRT_SUBELEM,
3194
0
                    parent_stmt == LY_STMT_ERROR_MESSAGE ? "value" : "text", lyplg_ext_stmt2str(parent_stmt));
3195
0
            return LY_EVALID;
3196
0
        }
3197
3198
        /* parse child element */
3199
0
        child = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3200
0
                ctx->xmlctx->prefix_len, parent_stmt);
3201
0
        if (((parent_stmt == LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_VALUE)) ||
3202
0
                ((parent_stmt != LY_STMT_ERROR_MESSAGE) && (child != LY_STMT_ARG_TEXT))) {
3203
0
            LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, (int)ctx->xmlctx->name_len, ctx->xmlctx->name,
3204
0
                    lyplg_ext_stmt2str(parent_stmt));
3205
0
            return LY_EVALID;
3206
0
        }
3207
0
        LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3208
3209
        /* no attributes expected */
3210
0
        while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3211
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3212
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3213
0
        }
3214
3215
        /* load and save content */
3216
0
        INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *arg);
3217
0
        LY_CHECK_RET(!*arg, LY_EMEM);
3218
3219
        /* load closing tag of subelement */
3220
0
        LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3221
0
        break;
3222
0
    default:
3223
0
        LOGINT(ctx->xmlctx->ctx);
3224
0
        return LY_EINT;
3225
0
    }
3226
3227
0
    return LY_SUCCESS;
3228
0
}
3229
3230
/**
3231
 * @brief Parse yin element into generic structure.
3232
 *
3233
 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
3234
 * @param[in] parent_stmt Type of parent statement.
3235
 * @param[out] element Where the element structure should be stored.
3236
 * @return LY_ERR values.
3237
 */
3238
LY_ERR
3239
yin_parse_element_generic(struct lysp_yin_ctx *ctx, enum ly_stmt parent_stmt, struct lysp_stmt **element)
3240
0
{
3241
0
    LY_ERR ret = LY_SUCCESS;
3242
0
    struct lysp_stmt *last = NULL, *new = NULL;
3243
0
    char *id;
3244
3245
0
    assert(ctx->xmlctx->status == LYXML_ELEMENT);
3246
3247
    /* allocate new structure for element */
3248
0
    *element = calloc(1, sizeof(**element));
3249
0
    LY_CHECK_ERR_GOTO(!(*element), LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3250
3251
    /* store identifier */
3252
0
    if (ctx->xmlctx->prefix_len) {
3253
0
        if (asprintf(&id, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3254
0
                ctx->xmlctx->name) == -1) {
3255
0
            LOGMEM(ctx->xmlctx->ctx);
3256
0
            ret = LY_EMEM;
3257
0
            goto cleanup;
3258
0
        }
3259
0
        LY_CHECK_GOTO(ret = lysdict_insert_zc(ctx->xmlctx->ctx, id, &(*element)->stmt), cleanup);
3260
3261
        /* store prefix data for the statement */
3262
0
        LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->stmt, strlen((*element)->stmt), LY_VALUE_XML,
3263
0
                &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3264
0
    } else {
3265
0
        LY_CHECK_GOTO(ret = lysdict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &(*element)->stmt), cleanup);
3266
0
    }
3267
3268
0
    (*element)->kw = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3269
0
            ctx->xmlctx->prefix_len, parent_stmt);
3270
3271
0
    last = (*element)->child;
3272
0
    if ((*element)->kw == LY_STMT_NONE) {
3273
        /* unrecognized element */
3274
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, (int)ctx->xmlctx->name_len, ctx->xmlctx->name,
3275
0
                lyplg_ext_stmt2str(parent_stmt));
3276
0
        ret = LY_EVALID;
3277
0
        goto cleanup;
3278
0
    } else if ((*element)->kw != LY_STMT_EXTENSION_INSTANCE) {
3279
        /* element is known yang keyword, which means argument can be parsed correctly. */
3280
0
        ret = yin_parse_extension_instance_arg(ctx, (*element)->kw, &(*element)->arg);
3281
0
        LY_CHECK_GOTO(ret, cleanup);
3282
0
    } else {
3283
0
        LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3284
3285
        /* load attributes in generic way, save all attributes in linked list */
3286
0
        while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3287
0
            new = calloc(1, sizeof(*last));
3288
0
            LY_CHECK_ERR_GOTO(!new, LOGMEM(ctx->xmlctx->ctx); ret = LY_EMEM, cleanup);
3289
0
            if (!(*element)->child) {
3290
                /* save first */
3291
0
                (*element)->child = new;
3292
0
            } else {
3293
0
                last->next = new;
3294
0
            }
3295
0
            last = new;
3296
3297
0
            last->flags |= LYS_YIN_ATTR;
3298
0
            LY_CHECK_GOTO(ret = lysdict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last->stmt), cleanup);
3299
0
            last->kw = LY_STMT_NONE;
3300
            /* attributes with prefix are ignored */
3301
0
            if (!ctx->xmlctx->prefix) {
3302
0
                LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3303
3304
0
                INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, last->arg);
3305
0
                LY_CHECK_ERR_GOTO(!last->arg, ret = LY_EMEM, cleanup);
3306
0
            } else {
3307
0
                LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3308
0
            }
3309
0
            LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3310
0
        }
3311
0
    }
3312
3313
0
    if ((ctx->xmlctx->status != LYXML_ELEM_CONTENT) || ctx->xmlctx->ws_only) {
3314
0
        LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3315
0
        while (ctx->xmlctx->status == LYXML_ELEMENT) {
3316
            /* parse subelements */
3317
0
            ret = yin_parse_element_generic(ctx, (*element)->kw, &new);
3318
0
            LY_CHECK_GOTO(ret, cleanup);
3319
0
            if (!(*element)->child) {
3320
                /* save first */
3321
0
                (*element)->child = new;
3322
0
            } else {
3323
0
                last->next = new;
3324
0
            }
3325
0
            last = new;
3326
3327
0
            assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3328
0
            LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3329
0
        }
3330
0
    } else {
3331
        /* save element content */
3332
0
        if (ctx->xmlctx->value_len) {
3333
0
            INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, (*element)->arg);
3334
0
            LY_CHECK_ERR_GOTO(!(*element)->arg, ret = LY_EMEM, cleanup);
3335
3336
            /* store prefix data for the argument as well */
3337
0
            LY_CHECK_GOTO(ret = ly_store_prefix_data(ctx->xmlctx->ctx, (*element)->arg, strlen((*element)->arg), LY_VALUE_XML,
3338
0
                    &ctx->xmlctx->ns, &(*element)->format, &(*element)->prefix_data), cleanup);
3339
0
        }
3340
3341
        /* read closing tag */
3342
0
        LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3343
0
    }
3344
3345
0
cleanup:
3346
0
    return ret;
3347
0
}
3348
3349
/**
3350
 * @brief Parse instance of extension.
3351
 *
3352
 * @param[in,out] ctx Yin parser context for logging and to store current state.
3353
 * @param[in] parent Current statement parent.
3354
 * @param[in] parent_stmt Type of @p parent statement.
3355
 * @param[in] parent_stmt_index In case of several @p parent_stmt, index of the relevant @p parent statement.
3356
 * @param[in,out] exts Extension instance to add to.
3357
 * @return LY_ERR values.
3358
 */
3359
LY_ERR
3360
yin_parse_extension_instance(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt,
3361
        LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts)
3362
0
{
3363
0
    struct lysp_ext_instance *e;
3364
0
    struct lysp_stmt *last_subelem = NULL, *new_subelem = NULL;
3365
0
    char *ext_name;
3366
3367
0
    assert(ctx->xmlctx->status == LYXML_ELEMENT);
3368
0
    assert(exts);
3369
3370
0
    LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *exts, e, LY_EMEM);
3371
3372
0
    if (!ctx->xmlctx->prefix_len) {
3373
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%*.s\" without the mandatory prefix.",
3374
0
                (int)ctx->xmlctx->name_len, ctx->xmlctx->name);
3375
0
        return LY_EVALID;
3376
0
    }
3377
3378
    /* store prefixed name */
3379
0
    if (asprintf(&ext_name, "%.*s:%.*s", (int)ctx->xmlctx->prefix_len, ctx->xmlctx->prefix, (int)ctx->xmlctx->name_len,
3380
0
            ctx->xmlctx->name) == -1) {
3381
0
        LOGMEM(ctx->xmlctx->ctx);
3382
0
        return LY_EMEM;
3383
0
    }
3384
0
    LY_CHECK_RET(lysdict_insert_zc(ctx->xmlctx->ctx, ext_name, &e->name));
3385
3386
    /* store prefix data for the name */
3387
0
    LY_CHECK_RET(ly_store_prefix_data(ctx->xmlctx->ctx, e->name, strlen(e->name), LY_VALUE_XML, &ctx->xmlctx->ns,
3388
0
            &e->format, &e->prefix_data));
3389
3390
0
    e->parent = (void *)parent;
3391
0
    e->parent_stmt = parent_stmt;
3392
0
    e->parent_stmt_index = parent_stmt_index;
3393
3394
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3395
3396
    /* store attributes as subelements */
3397
0
    while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3398
0
        if (!ctx->xmlctx->prefix) {
3399
0
            new_subelem = calloc(1, sizeof(*new_subelem));
3400
0
            if (!e->child) {
3401
0
                e->child = new_subelem;
3402
0
            } else {
3403
0
                last_subelem->next = new_subelem;
3404
0
            }
3405
0
            last_subelem = new_subelem;
3406
3407
0
            last_subelem->flags |= LYS_YIN_ATTR;
3408
0
            LY_CHECK_RET(lysdict_insert(ctx->xmlctx->ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, &last_subelem->stmt));
3409
0
            LY_CHECK_RET(!last_subelem->stmt, LY_EMEM);
3410
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3411
3412
0
            INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic,
3413
0
                    last_subelem->arg);
3414
0
            LY_CHECK_RET(!last_subelem->arg, LY_EMEM);
3415
0
        } else {
3416
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3417
0
        }
3418
3419
0
        LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3420
0
    }
3421
3422
    /* parse subelements */
3423
0
    assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3424
0
    if (ctx->xmlctx->ws_only) {
3425
0
        LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3426
0
        while (ctx->xmlctx->status == LYXML_ELEMENT) {
3427
            /* BUG nested extensions will not be parsed because we are not able to dinsguish between them
3428
             * and the argument of this extension, in case there is one and its 'yin-element' is 'true'
3429
            stmt = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3430
                    ctx->xmlctx->prefix_len, LY_STMT_EXTENSION_INSTANCE);
3431
            if (stmt == LY_STMT_EXTENSION_INSTANCE) {
3432
                LY_CHECK_RET(yin_parse_extension_instance(ctx, e, LY_STMT_EXTENSION_INSTANCE, 0, &e->exts));
3433
            } else { */
3434
3435
0
            LY_CHECK_RET(yin_parse_element_generic(ctx, LY_STMT_EXTENSION_INSTANCE, &new_subelem));
3436
0
            if (!e->child) {
3437
0
                e->child = new_subelem;
3438
0
            } else {
3439
0
                last_subelem->next = new_subelem;
3440
0
            }
3441
0
            last_subelem = new_subelem;
3442
3443
0
            assert(ctx->xmlctx->status == LYXML_ELEM_CLOSE);
3444
0
            LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3445
0
        }
3446
3447
        /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
3448
0
        LY_CHECK_RET(yin_unres_exts_add(ctx, e->exts));
3449
0
    } else if (ctx->xmlctx->value_len) {
3450
        /* invalid text content */
3451
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Extension instance \"%s\" with unexpected text content \"%.*s\".", ext_name,
3452
0
                (int)ctx->xmlctx->value_len, ctx->xmlctx->value);
3453
0
        return LY_EVALID;
3454
0
    }
3455
3456
0
    e->parsed = NULL;
3457
3458
0
    return LY_SUCCESS;
3459
0
}
3460
3461
/**
3462
 * @brief Generic function for content parsing
3463
 *
3464
 * @param[in,out] ctx Yin parser context for logging and to store current state.
3465
 * @param[in] subelem_info array of valid subelement types and meta information
3466
 * @param[in] subelem_info_size Size of subelem_info array.
3467
 * @param[in] parent Current statement parent.
3468
 * @param[in] parent_stmt Type of @p parent statement.
3469
 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
3470
 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
3471
 * @return LY_ERR values.
3472
 */
3473
LY_ERR
3474
yin_parse_content(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
3475
        const void *parent, enum ly_stmt parent_stmt, const char **text_content, struct lysp_ext_instance **exts)
3476
0
{
3477
0
    LY_ERR ret = LY_SUCCESS;
3478
0
    enum LYXML_PARSER_STATUS next_status;
3479
0
    enum ly_stmt cur_stmt = LY_STMT_NONE, last_stmt = LY_STMT_NONE;
3480
0
    struct yin_subelement *subelem = NULL;
3481
3482
0
    assert(ctx->xmlctx->status == LYXML_ELEM_CONTENT);
3483
3484
0
    if (ctx->xmlctx->ws_only) {
3485
        /* check whether there are any children */
3486
0
        LY_CHECK_GOTO(ret = lyxml_ctx_peek(ctx->xmlctx, &next_status), cleanup);
3487
0
    } else {
3488
        /* we want to parse the value */
3489
0
        next_status = LYXML_ELEM_CLOSE;
3490
0
    }
3491
3492
0
    if (next_status == LYXML_ELEMENT) {
3493
0
        LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3494
3495
        /* current element has subelements as content */
3496
0
        while (ctx->xmlctx->status == LYXML_ELEMENT) {
3497
            /* match keyword */
3498
0
            last_stmt = cur_stmt;
3499
0
            cur_stmt = yin_match_keyword(ctx, ctx->xmlctx->name, ctx->xmlctx->name_len, ctx->xmlctx->prefix,
3500
0
                    ctx->xmlctx->prefix_len, parent_stmt);
3501
3502
            /* check if this element can be child of current element */
3503
0
            subelem = get_record(cur_stmt, subelem_info_size, subelem_info);
3504
0
            if (!subelem) {
3505
0
                if ((parent_stmt == LY_STMT_DEVIATE) && isdevsub(cur_stmt)) {
3506
0
                    LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INDEV_YIN, lyplg_ext_stmt2str(cur_stmt));
3507
0
                } else {
3508
0
                    LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_UNEXP_SUBELEM, (int)ctx->xmlctx->name_len,
3509
0
                            ctx->xmlctx->name, lyplg_ext_stmt2str(parent_stmt));
3510
0
                }
3511
0
                ret = LY_EVALID;
3512
0
                goto cleanup;
3513
0
            }
3514
3515
            /* relative order is required only in module and submodule sub-elements */
3516
0
            if ((parent_stmt == LY_STMT_MODULE) || (parent_stmt == LY_STMT_SUBMODULE)) {
3517
0
                ret = yin_check_relative_order(ctx, last_stmt, cur_stmt, parent_stmt);
3518
0
                LY_CHECK_GOTO(ret, cleanup);
3519
0
            }
3520
3521
            /* flag check */
3522
0
            if ((subelem->flags & YIN_SUBELEM_UNIQUE) && (subelem->flags & YIN_SUBELEM_PARSED)) {
3523
                /* subelement uniquenes */
3524
0
                LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_SUBELEM_REDEF, lyplg_ext_stmt2str(cur_stmt), lyplg_ext_stmt2str(parent_stmt));
3525
0
                return LY_EVALID;
3526
0
            }
3527
0
            if (subelem->flags & YIN_SUBELEM_FIRST) {
3528
                /* subelement is supposed to be defined as first subelement */
3529
0
                ret = yin_check_subelem_first_constraint(ctx, subelem_info, subelem_info_size, parent_stmt, subelem);
3530
0
                LY_CHECK_GOTO(ret, cleanup);
3531
0
            }
3532
0
            if (subelem->flags & YIN_SUBELEM_VER2) {
3533
                /* subelement is supported only in version 1.1 or higher */
3534
0
                if (PARSER_CUR_PMOD(ctx)->version < LYS_VERSION_1_1) {
3535
0
                    LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_INSUBELEM2, lyplg_ext_stmt2str(cur_stmt), lyplg_ext_stmt2str(parent_stmt));
3536
0
                    ret = LY_EVALID;
3537
0
                    goto cleanup;
3538
0
                }
3539
0
            }
3540
            /* note that element was parsed for easy uniqueness check in next iterations */
3541
0
            subelem->flags |= YIN_SUBELEM_PARSED;
3542
3543
0
            switch (cur_stmt) {
3544
            /* call responsible function */
3545
0
            case LY_STMT_EXTENSION_INSTANCE:
3546
0
                ret = yin_parse_extension_instance(ctx, parent, parent_stmt,
3547
0
                        (subelem->dest) ? *((LY_ARRAY_COUNT_TYPE *)subelem->dest) : 0, exts);
3548
0
                break;
3549
0
            case LY_STMT_ACTION:
3550
0
            case LY_STMT_RPC:
3551
0
                ret = yin_parse_action(ctx, (struct tree_node_meta *)subelem->dest);
3552
0
                break;
3553
0
            case LY_STMT_ANYDATA:
3554
0
            case LY_STMT_ANYXML:
3555
0
                ret = yin_parse_any(ctx, cur_stmt, (struct tree_node_meta *)subelem->dest);
3556
0
                break;
3557
0
            case LY_STMT_ARGUMENT:
3558
0
                ret = yin_parse_argument(ctx, parent, (struct yin_argument_meta *)subelem->dest, exts);
3559
0
                break;
3560
0
            case LY_STMT_AUGMENT:
3561
0
                ret = yin_parse_augment(ctx, (struct tree_node_meta *)subelem->dest);
3562
0
                break;
3563
0
            case LY_STMT_BASE:
3564
0
                ret = yin_parse_base(ctx, parent_stmt, subelem->dest, exts);
3565
0
                break;
3566
0
            case LY_STMT_BELONGS_TO:
3567
0
                ret = yin_parse_belongs_to(ctx, (struct lysp_submodule *)subelem->dest, exts);
3568
0
                break;
3569
0
            case LY_STMT_BIT:
3570
0
                ret = yin_parse_bit(ctx, (struct lysp_type *)subelem->dest);
3571
0
                break;
3572
0
            case LY_STMT_CASE:
3573
0
                ret = yin_parse_case(ctx, (struct tree_node_meta *)subelem->dest);
3574
0
                break;
3575
0
            case LY_STMT_CHOICE:
3576
0
                ret = yin_parse_choice(ctx, (struct tree_node_meta *)subelem->dest);
3577
0
                break;
3578
0
            case LY_STMT_CONFIG:
3579
0
                ret = yin_parse_config(ctx, (uint16_t *)subelem->dest, exts);
3580
0
                break;
3581
0
            case LY_STMT_CONTACT:
3582
0
            case LY_STMT_DESCRIPTION:
3583
0
            case LY_STMT_ORGANIZATION:
3584
0
            case LY_STMT_REFERENCE:
3585
0
                ret = yin_parse_meta(ctx, parent, cur_stmt, (const char **)subelem->dest, exts);
3586
0
                break;
3587
0
            case LY_STMT_CONTAINER:
3588
0
                ret = yin_parse_container(ctx, (struct tree_node_meta *)subelem->dest);
3589
0
                break;
3590
0
            case LY_STMT_DEFAULT:
3591
0
                ret = yin_parse_qname(ctx, cur_stmt, subelem, exts);
3592
0
                break;
3593
0
            case LY_STMT_ERROR_APP_TAG:
3594
0
            case LY_STMT_KEY:
3595
0
                ret = yin_parse_simple_elem(ctx, parent, cur_stmt, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3596
0
                break;
3597
0
            case LY_STMT_PRESENCE:
3598
0
                ret = yin_parse_simple_elem(ctx, *(const char **)subelem->dest, cur_stmt, subelem, YIN_ARG_VALUE, Y_STR_ARG, exts);
3599
0
                break;
3600
0
            case LY_STMT_DEVIATE:
3601
0
                ret = yin_parse_deviate(ctx, (struct lysp_deviate **)subelem->dest);
3602
0
                break;
3603
0
            case LY_STMT_DEVIATION:
3604
0
                ret = yin_parse_deviation(ctx, (struct lysp_deviation **)subelem->dest);
3605
0
                break;
3606
0
            case LY_STMT_ENUM:
3607
0
                ret = yin_parse_enum(ctx, (struct lysp_type *)subelem->dest);
3608
0
                break;
3609
0
            case LY_STMT_ERROR_MESSAGE:
3610
0
                ret = yin_parse_err_msg(ctx, parent, (const char **)subelem->dest, exts);
3611
0
                break;
3612
0
            case LY_STMT_EXTENSION:
3613
0
                ret = yin_parse_extension(ctx, (struct lysp_ext **)subelem->dest);
3614
0
                break;
3615
0
            case LY_STMT_FEATURE:
3616
0
                ret = yin_parse_feature(ctx, (struct lysp_feature **)subelem->dest);
3617
0
                break;
3618
0
            case LY_STMT_FRACTION_DIGITS:
3619
0
                ret = yin_parse_fracdigits(ctx, (struct lysp_type *)subelem->dest);
3620
0
                break;
3621
0
            case LY_STMT_GROUPING:
3622
0
                ret = yin_parse_grouping(ctx, (struct tree_node_meta *)subelem->dest);
3623
0
                break;
3624
0
            case LY_STMT_IDENTITY:
3625
0
                ret = yin_parse_identity(ctx, (struct lysp_ident **)subelem->dest);
3626
0
                break;
3627
0
            case LY_STMT_UNIQUE:
3628
0
            case LY_STMT_IF_FEATURE:
3629
0
                ret = yin_parse_qname(ctx, cur_stmt, subelem, exts);
3630
0
                break;
3631
0
            case LY_STMT_UNITS:
3632
0
                ret = yin_parse_simple_elem(ctx, *(const char **)subelem->dest, cur_stmt, subelem, YIN_ARG_NAME, Y_STR_ARG, exts);
3633
0
                break;
3634
0
            case LY_STMT_IMPORT:
3635
0
                ret = yin_parse_import(ctx, (struct import_meta *)subelem->dest);
3636
0
                break;
3637
0
            case LY_STMT_INCLUDE:
3638
0
                if ((parent_stmt == LY_STMT_SUBMODULE) && (PARSER_CUR_PMOD(ctx)->version == LYS_VERSION_1_1)) {
3639
0
                    LOGWRN(PARSER_CTX(ctx), "YANG version 1.1 expects all includes in main module, includes in "
3640
0
                            "submodules (%s) are not necessary.", ((struct lysp_submodule *)PARSER_CUR_PMOD(ctx))->name);
3641
0
                }
3642
0
                ret = yin_parse_include(ctx, (struct include_meta *)subelem->dest);
3643
0
                break;
3644
0
            case LY_STMT_INPUT:
3645
0
            case LY_STMT_OUTPUT:
3646
0
                ret = yin_parse_inout(ctx, cur_stmt, (struct inout_meta *)subelem->dest);
3647
0
                break;
3648
0
            case LY_STMT_LEAF:
3649
0
                ret = yin_parse_leaf(ctx, (struct tree_node_meta *)subelem->dest);
3650
0
                break;
3651
0
            case LY_STMT_LEAF_LIST:
3652
0
                ret = yin_parse_leaflist(ctx, (struct tree_node_meta *)subelem->dest);
3653
0
                break;
3654
0
            case LY_STMT_LENGTH:
3655
0
                ret = yin_parse_length(ctx, (struct lysp_type *)subelem->dest);
3656
0
                break;
3657
0
            case LY_STMT_LIST:
3658
0
                ret = yin_parse_list(ctx, (struct tree_node_meta *)subelem->dest);
3659
0
                break;
3660
0
            case LY_STMT_MANDATORY:
3661
0
                ret = yin_parse_mandatory(ctx, (uint16_t *)subelem->dest, exts);
3662
0
                break;
3663
0
            case LY_STMT_MAX_ELEMENTS:
3664
0
            case LY_STMT_MIN_ELEMENTS:
3665
0
                ret = yin_parse_minmax(ctx, parent_stmt, cur_stmt, subelem->dest);
3666
0
                break;
3667
0
            case LY_STMT_MODIFIER:
3668
0
                ret = yin_parse_modifier(ctx, parent, (const char **)subelem->dest, exts);
3669
0
                break;
3670
0
            case LY_STMT_MUST:
3671
0
                ret = yin_parse_must(ctx, (struct lysp_restr **)subelem->dest);
3672
0
                break;
3673
0
            case LY_STMT_NAMESPACE:
3674
0
                ret = yin_parse_simple_elem(ctx, parent, cur_stmt, subelem, YIN_ARG_URI, Y_STR_ARG, exts);
3675
0
                break;
3676
0
            case LY_STMT_NOTIFICATION:
3677
0
                ret = yin_parse_notification(ctx, (struct tree_node_meta *)subelem->dest);
3678
0
                break;
3679
0
            case LY_STMT_ORDERED_BY:
3680
0
                ret = yin_parse_orderedby(ctx, parent, (uint16_t *)subelem->dest, exts);
3681
0
                break;
3682
0
            case LY_STMT_PATH:
3683
0
                ret = yin_parse_path(ctx, (struct lysp_type *)subelem->dest);
3684
0
                break;
3685
0
            case LY_STMT_PATTERN:
3686
0
                ret = yin_parse_pattern(ctx, (struct lysp_type *)subelem->dest);
3687
0
                break;
3688
0
            case LY_STMT_VALUE:
3689
0
            case LY_STMT_POSITION:
3690
0
                ret = yin_parse_value_pos(ctx, cur_stmt, (struct lysp_type_enum *)subelem->dest);
3691
0
                break;
3692
0
            case LY_STMT_PREFIX:
3693
0
                ret = yin_parse_simple_elem(ctx, *(const char **)subelem->dest, cur_stmt, subelem, YIN_ARG_VALUE,
3694
0
                        Y_IDENTIF_ARG, exts);
3695
0
                break;
3696
0
            case LY_STMT_RANGE:
3697
0
                ret = yin_parse_range(ctx, (struct lysp_type *)subelem->dest);
3698
0
                break;
3699
0
            case LY_STMT_REFINE:
3700
0
                ret = yin_parse_refine(ctx, (struct lysp_refine **)subelem->dest);
3701
0
                break;
3702
0
            case LY_STMT_REQUIRE_INSTANCE:
3703
0
                ret = yin_pasrse_reqinstance(ctx, (struct lysp_type *)subelem->dest);
3704
0
                break;
3705
0
            case LY_STMT_REVISION:
3706
0
                ret = yin_parse_revision(ctx, (struct lysp_revision **)subelem->dest);
3707
0
                break;
3708
0
            case LY_STMT_REVISION_DATE:
3709
0
                ret = yin_parse_revision_date(ctx, (char *)subelem->dest, exts);
3710
0
                break;
3711
0
            case LY_STMT_STATUS:
3712
0
                ret = yin_parse_status(ctx, (uint16_t *)subelem->dest, exts);
3713
0
                break;
3714
0
            case LY_STMT_TYPE:
3715
0
                ret = yin_parse_type(ctx, parent_stmt, subelem);
3716
0
                break;
3717
0
            case LY_STMT_TYPEDEF:
3718
0
                ret = yin_parse_typedef(ctx, (struct tree_node_meta *)subelem->dest);
3719
0
                break;
3720
0
            case LY_STMT_USES:
3721
0
                ret = yin_parse_uses(ctx, (struct tree_node_meta *)subelem->dest);
3722
0
                break;
3723
0
            case LY_STMT_WHEN:
3724
0
                ret = yin_parse_when(ctx, (struct lysp_when **)subelem->dest);
3725
0
                break;
3726
0
            case LY_STMT_YANG_VERSION:
3727
0
                ret = yin_parse_yangversion(ctx, parent, (uint8_t *)subelem->dest, exts);
3728
0
                break;
3729
0
            case LY_STMT_YIN_ELEMENT:
3730
0
                ret = yin_parse_yin_element(ctx, parent, (uint16_t *)subelem->dest, exts);
3731
0
                break;
3732
0
            case LY_STMT_ARG_TEXT:
3733
0
            case LY_STMT_ARG_VALUE:
3734
                /* TODO what to do with content/attributes? */
3735
0
                LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3736
0
                while (ctx->xmlctx->status == LYXML_ATTRIBUTE) {
3737
0
                    LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3738
0
                    LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3739
0
                }
3740
0
                ret = yin_parse_content(ctx, NULL, 0, parent, cur_stmt, (const char **)subelem->dest, NULL);
3741
0
                break;
3742
0
            default:
3743
0
                LOGINT(ctx->xmlctx->ctx);
3744
0
                ret = LY_EINT;
3745
0
            }
3746
0
            LY_CHECK_GOTO(ret, cleanup);
3747
3748
0
            LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3749
0
        }
3750
0
    } else {
3751
0
        LY_CHECK_RET(ret);
3752
        /* elements with text or none content */
3753
        /* save text content, if text_content isn't set, it's just ignored */
3754
        /* no resources are allocated in this branch, no need to use cleanup label */
3755
0
        LY_CHECK_RET(yin_validate_value(ctx, Y_STR_ARG));
3756
0
        if (text_content) {
3757
0
            INSERT_STRING_RET(ctx->xmlctx->ctx, ctx->xmlctx->value, ctx->xmlctx->value_len, ctx->xmlctx->dynamic, *text_content);
3758
0
            LY_CHECK_RET(!*text_content, LY_EMEM);
3759
0
        }
3760
3761
0
        LY_CHECK_GOTO(ret = lyxml_ctx_next(ctx->xmlctx), cleanup);
3762
0
    }
3763
3764
    /* mandatory subelements are checked only after whole element was succesfully parsed */
3765
0
    LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, parent_stmt));
3766
3767
0
cleanup:
3768
0
    return ret;
3769
0
}
3770
3771
/**
3772
 * @brief Parse module element.
3773
 *
3774
 * @param[in,out] ctx Yin parser context for logging and to store current state.
3775
 * @param[out] mod Parsed module structure.
3776
 * @return LY_ERR values.
3777
 */
3778
LY_ERR
3779
yin_parse_mod(struct lysp_yin_ctx *ctx, struct lysp_module *mod)
3780
0
{
3781
0
    LY_ERR ret = LY_SUCCESS;
3782
0
    struct yin_subelement *subelems = NULL;
3783
0
    const struct lysp_submodule *dup;
3784
0
    size_t subelems_size;
3785
3786
0
    mod->is_submod = 0;
3787
3788
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3789
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &mod->mod->name, Y_IDENTIF_ARG, LY_STMT_MODULE));
3790
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 28, NULL, &subelems,
3791
0
            LY_STMT_ANYDATA, &mod->data, YIN_SUBELEM_VER2,
3792
0
            LY_STMT_ANYXML, &mod->data, 0,
3793
0
            LY_STMT_AUGMENT, &mod->augments, 0,
3794
0
            LY_STMT_CHOICE, &mod->data, 0,
3795
0
            LY_STMT_CONTACT, &mod->mod->contact, YIN_SUBELEM_UNIQUE,
3796
0
            LY_STMT_CONTAINER, &mod->data, 0,
3797
0
            LY_STMT_DESCRIPTION, &mod->mod->dsc, YIN_SUBELEM_UNIQUE,
3798
0
            LY_STMT_DEVIATION, &mod->deviations, 0,
3799
0
            LY_STMT_EXTENSION, &mod->extensions, 0,
3800
0
            LY_STMT_FEATURE, &mod->features, 0,
3801
0
            LY_STMT_GROUPING, &mod->groupings, 0,
3802
0
            LY_STMT_IDENTITY, &mod->identities, 0,
3803
0
            LY_STMT_IMPORT, mod->mod->prefix, &mod->imports, 0,
3804
0
            LY_STMT_INCLUDE, mod->mod->name, &mod->includes, 0,
3805
0
            LY_STMT_LEAF, &mod->data, 0,
3806
0
            LY_STMT_LEAF_LIST, &mod->data, 0,
3807
0
            LY_STMT_LIST, &mod->data, 0,
3808
0
            LY_STMT_NAMESPACE, &mod->mod->ns, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3809
0
            LY_STMT_NOTIFICATION, &mod->notifs, 0,
3810
0
            LY_STMT_ORGANIZATION, &mod->mod->org, YIN_SUBELEM_UNIQUE,
3811
0
            LY_STMT_PREFIX, &mod->mod->prefix, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3812
0
            LY_STMT_REFERENCE, &mod->mod->ref, YIN_SUBELEM_UNIQUE,
3813
0
            LY_STMT_REVISION, &mod->revs, 0,
3814
0
            LY_STMT_RPC, &mod->rpcs, 0,
3815
0
            LY_STMT_TYPEDEF, &mod->typedefs, 0,
3816
0
            LY_STMT_USES, &mod->data, 0,
3817
0
            LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
3818
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
3819
3820
0
    ret = yin_parse_content(ctx, subelems, subelems_size, mod, LY_STMT_MODULE, NULL, &mod->exts);
3821
0
    subelems_deallocator(subelems_size, subelems);
3822
0
    LY_CHECK_RET(ret);
3823
3824
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
3825
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, mod->exts));
3826
3827
    /* submodules share the namespace with the module names, so there must not be
3828
     * a submodule of the same name in the context, no need for revision matching */
3829
0
    dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, mod->mod->name);
3830
0
    if (dup) {
3831
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_NAME2_COL, "module", "submodule", mod->mod->name);
3832
0
        return LY_EVALID;
3833
0
    }
3834
3835
0
    return LY_SUCCESS;
3836
0
}
3837
3838
/**
3839
 * @brief Parse submodule element.
3840
 *
3841
 * @param[in,out] ctx Yin parser context for logging and to store current state.
3842
 * @param[in] mod_attrs Attributes of submodule element.
3843
 * @param[out] submod Parsed submodule structure.
3844
 * @return LY_ERR values.
3845
 */
3846
LY_ERR
3847
yin_parse_submod(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod)
3848
0
{
3849
0
    LY_ERR ret = LY_SUCCESS;
3850
0
    struct yin_subelement *subelems = NULL;
3851
0
    const struct lysp_submodule *dup;
3852
0
    size_t subelems_size;
3853
3854
0
    submod->is_submod = 1;
3855
3856
0
    LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
3857
0
    LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_NAME, &submod->name, Y_IDENTIF_ARG, LY_STMT_SUBMODULE));
3858
0
    LY_CHECK_RET(subelems_allocator(ctx, subelems_size = 27, NULL, &subelems,
3859
0
            LY_STMT_ANYDATA, &submod->data, YIN_SUBELEM_VER2,
3860
0
            LY_STMT_ANYXML, &submod->data, 0,
3861
0
            LY_STMT_AUGMENT, &submod->augments, 0,
3862
0
            LY_STMT_BELONGS_TO, submod, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE,
3863
0
            LY_STMT_CHOICE, &submod->data, 0,
3864
0
            LY_STMT_CONTACT, &submod->contact, YIN_SUBELEM_UNIQUE,
3865
0
            LY_STMT_CONTAINER, &submod->data, 0,
3866
0
            LY_STMT_DESCRIPTION, &submod->dsc, YIN_SUBELEM_UNIQUE,
3867
0
            LY_STMT_DEVIATION, &submod->deviations, 0,
3868
0
            LY_STMT_EXTENSION, &submod->extensions, 0,
3869
0
            LY_STMT_FEATURE, &submod->features, 0,
3870
0
            LY_STMT_GROUPING, &submod->groupings, 0,
3871
0
            LY_STMT_IDENTITY, &submod->identities, 0,
3872
0
            LY_STMT_IMPORT, submod->prefix, &submod->imports, 0,
3873
0
            LY_STMT_INCLUDE, submod->name, &submod->includes, 0,
3874
0
            LY_STMT_LEAF, &submod->data, 0,
3875
0
            LY_STMT_LEAF_LIST, &submod->data, 0,
3876
0
            LY_STMT_LIST, &submod->data, 0,
3877
0
            LY_STMT_NOTIFICATION, &submod->notifs, 0,
3878
0
            LY_STMT_ORGANIZATION, &submod->org, YIN_SUBELEM_UNIQUE,
3879
0
            LY_STMT_REFERENCE, &submod->ref, YIN_SUBELEM_UNIQUE,
3880
0
            LY_STMT_REVISION, &submod->revs, 0,
3881
0
            LY_STMT_RPC, &submod->rpcs, 0,
3882
0
            LY_STMT_TYPEDEF, &submod->typedefs, 0,
3883
0
            LY_STMT_USES, &submod->data, 0,
3884
0
            LY_STMT_YANG_VERSION, &submod->version, YIN_SUBELEM_UNIQUE,
3885
0
            LY_STMT_EXTENSION_INSTANCE, NULL, 0));
3886
3887
0
    ret = yin_parse_content(ctx, subelems, subelems_size, submod, LY_STMT_SUBMODULE, NULL, &submod->exts);
3888
0
    subelems_deallocator(subelems_size, subelems);
3889
0
    LY_CHECK_RET(ret);
3890
3891
    /* store extension instance array (no realloc anymore) to find the plugin records and finish parsing */
3892
0
    LY_CHECK_RET(yin_unres_exts_add(ctx, submod->exts));
3893
3894
    /* submodules share the namespace with the module names, so there must not be
3895
     * a submodule of the same name in the context, no need for revision matching */
3896
0
    dup = ly_ctx_get_submodule_latest(ctx->xmlctx->ctx, submod->name);
3897
0
    if (dup && strcmp(dup->mod->name, submod->mod->name)) {
3898
0
        LOGVAL_PARSER((struct lysp_ctx *)ctx, LY_VCODE_NAME_COL, "submodules", dup->name);
3899
0
        return LY_EVALID;
3900
0
    }
3901
3902
0
    return LY_SUCCESS;
3903
0
}
3904
3905
LY_ERR
3906
yin_parse_submodule(struct lysp_yin_ctx **yin_ctx, struct ly_ctx *ctx, struct lysp_ctx *main_ctx,
3907
        struct ly_in *in, struct lysp_submodule **submod)
3908
0
{
3909
0
    enum ly_stmt kw = LY_STMT_NONE;
3910
0
    LY_ERR ret = LY_SUCCESS;
3911
0
    struct lysp_submodule *mod_p = NULL;
3912
3913
0
    assert(yin_ctx && ctx && main_ctx && in && submod);
3914
3915
    /* create context */
3916
0
    *yin_ctx = calloc(1, sizeof **yin_ctx);
3917
0
    LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
3918
0
    (*yin_ctx)->format = LYS_IN_YIN;
3919
0
    (*yin_ctx)->main_ctx = main_ctx;
3920
0
    LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
3921
3922
0
    mod_p = calloc(1, sizeof *mod_p);
3923
0
    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3924
0
    mod_p->mod = PARSER_CUR_PMOD(main_ctx)->mod;
3925
0
    mod_p->parsing = 1;
3926
3927
    /* use main context parsed mods adding the current one */
3928
0
    (*yin_ctx)->parsed_mods = main_ctx->parsed_mods;
3929
0
    ly_set_add((*yin_ctx)->parsed_mods, mod_p, 1, NULL);
3930
3931
    /* check submodule */
3932
0
    kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
3933
0
            (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
3934
0
    if (kw == LY_STMT_MODULE) {
3935
0
        LOGERR(ctx, LY_EDENIED, "Input data contains module when a submodule is expected.");
3936
0
        ret = LY_EINVAL;
3937
0
        goto cleanup;
3938
0
    } else if (kw != LY_STMT_SUBMODULE) {
3939
0
        LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
3940
0
        ret = LY_EVALID;
3941
0
        goto cleanup;
3942
0
    }
3943
3944
0
    ret = yin_parse_submod(*yin_ctx, mod_p);
3945
0
    LY_CHECK_GOTO(ret, cleanup);
3946
3947
    /* skip possible trailing whitespaces at end of the input */
3948
0
    while (isspace(in->current[0])) {
3949
0
        if (in->current[0] == '\n') {
3950
0
            LY_IN_NEW_LINE(in);
3951
0
        }
3952
0
        ly_in_skip(in, 1);
3953
0
    }
3954
0
    if (in->current[0]) {
3955
0
        LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_TRAILING_SUBMOD, 15, in->current,
3956
0
                strlen(in->current) > 15 ? "..." : "");
3957
0
        ret = LY_EVALID;
3958
0
        goto cleanup;
3959
0
    }
3960
3961
0
    mod_p->parsing = 0;
3962
0
    *submod = mod_p;
3963
3964
0
cleanup:
3965
0
    ly_log_location_revert(0, 0, 0, 1);
3966
0
    if (ret) {
3967
0
        lysp_module_free(ctx, (struct lysp_module *)mod_p);
3968
0
        lysp_yin_ctx_free(*yin_ctx);
3969
0
        *yin_ctx = NULL;
3970
0
    }
3971
0
    return ret;
3972
0
}
3973
3974
LY_ERR
3975
yin_parse_module(struct lysp_yin_ctx **yin_ctx, struct ly_in *in, struct lys_module *mod)
3976
0
{
3977
0
    LY_ERR ret = LY_SUCCESS;
3978
0
    enum ly_stmt kw = LY_STMT_NONE;
3979
0
    struct lysp_module *mod_p = NULL;
3980
3981
    /* create context */
3982
0
    *yin_ctx = calloc(1, sizeof **yin_ctx);
3983
0
    LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
3984
0
    (*yin_ctx)->format = LYS_IN_YIN;
3985
0
    LY_CHECK_ERR_RET(ly_set_new(&(*yin_ctx)->parsed_mods), free(*yin_ctx); LOGMEM(mod->ctx), LY_EMEM);
3986
0
    LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
3987
0
    (*yin_ctx)->main_ctx = (struct lysp_ctx *)(*yin_ctx);
3988
3989
0
    mod_p = calloc(1, sizeof *mod_p);
3990
0
    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
3991
0
    mod_p->mod = mod;
3992
0
    ly_set_add((*yin_ctx)->parsed_mods, mod_p, 1, NULL);
3993
3994
    /* check module */
3995
0
    kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
3996
0
            (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
3997
0
    if (kw == LY_STMT_SUBMODULE) {
3998
0
        LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
3999
0
        ret = LY_EINVAL;
4000
0
        goto cleanup;
4001
0
    } else if (kw != LY_STMT_MODULE) {
4002
0
        LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_MOD_SUBOMD, lyplg_ext_stmt2str(kw));
4003
0
        ret = LY_EVALID;
4004
0
        goto cleanup;
4005
0
    }
4006
4007
    /* parse module substatements */
4008
0
    ret = yin_parse_mod(*yin_ctx, mod_p);
4009
0
    LY_CHECK_GOTO(ret, cleanup);
4010
4011
    /* skip possible trailing whitespaces at end of the input */
4012
0
    while (isspace(in->current[0])) {
4013
0
        if (in->current[0] == '\n') {
4014
0
            LY_IN_NEW_LINE(in);
4015
0
        }
4016
0
        ly_in_skip(in, 1);
4017
0
    }
4018
0
    if (in->current[0]) {
4019
0
        LOGVAL_PARSER((struct lysp_ctx *)*yin_ctx, LY_VCODE_TRAILING_MOD, 15, in->current,
4020
0
                strlen(in->current) > 15 ? "..." : "");
4021
0
        ret = LY_EVALID;
4022
0
        goto cleanup;
4023
0
    }
4024
4025
0
    mod->parsed = mod_p;
4026
4027
0
cleanup:
4028
0
    ly_log_location_revert(0, 0, 0, 1);
4029
0
    if (ret) {
4030
0
        lysp_module_free(mod->ctx, mod_p);
4031
0
        lysp_yin_ctx_free(*yin_ctx);
4032
        *yin_ctx = NULL;
4033
0
    }
4034
0
    return ret;
4035
0
}