Coverage Report

Created: 2025-12-05 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyang/src/tree_schema_helpers.c
Line
Count
Source
1
/**
2
 * @file tree_schema_helpers.c
3
 * @author Radek Krejci <rkrejci@cesnet.cz>
4
 * @brief Parsing and validation helper functions for schema trees
5
 *
6
 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7
 *
8
 * This source code is licensed under BSD 3-Clause License (the "License").
9
 * You may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     https://opensource.org/licenses/BSD-3-Clause
13
 */
14
15
#define _GNU_SOURCE
16
17
#include <assert.h>
18
#include <ctype.h>
19
#include <stddef.h>
20
#include <stdint.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <time.h>
24
25
#include "common.h"
26
#include "compat.h"
27
#include "context.h"
28
#include "dict.h"
29
#include "hash_table.h"
30
#include "in.h"
31
#include "in_internal.h"
32
#include "log.h"
33
#include "parser_schema.h"
34
#include "schema_compile.h"
35
#include "schema_features.h"
36
#include "set.h"
37
#include "tree.h"
38
#include "tree_edit.h"
39
#include "tree_schema.h"
40
#include "tree_schema_internal.h"
41
42
LY_ERR
43
lysp_check_prefix(struct lys_parser_ctx *ctx, struct lysp_import *imports, const char *module_prefix, const char **value)
44
0
{
45
0
    struct lysp_import *i;
46
47
0
    if (module_prefix && (&module_prefix != value) && !strcmp(module_prefix, *value)) {
48
0
        LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used as module prefix.", *value);
49
0
        return LY_EEXIST;
50
0
    }
51
0
    LY_ARRAY_FOR(imports, struct lysp_import, i) {
52
0
        if (i->prefix && (&i->prefix != value) && !strcmp(i->prefix, *value)) {
53
0
            LOGVAL_PARSER(ctx, LYVE_REFERENCE, "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
54
0
            return LY_EEXIST;
55
0
        }
56
0
    }
57
0
    return LY_SUCCESS;
58
0
}
59
60
LY_ERR
61
lysp_check_date(struct lys_parser_ctx *ctx, const char *date, uint8_t date_len, const char *stmt)
62
0
{
63
0
    struct tm tm, tm_;
64
0
    char *r;
65
66
0
    LY_CHECK_ARG_RET(ctx ? PARSER_CTX(ctx) : NULL, date, LY_EINVAL);
67
0
    LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx ? PARSER_CTX(ctx) : NULL, date_len), LY_EINVAL);
68
69
    /* check format: YYYY-MM-DD */
70
0
    for (uint8_t i = 0; i < date_len; i++) {
71
0
        if ((i == 4) || (i == 7)) {
72
0
            if (date[i] != '-') {
73
0
                goto error;
74
0
            }
75
0
        } else if (!isdigit(date[i])) {
76
0
            goto error;
77
0
        }
78
0
    }
79
80
    /* check content, e.g. 2018-02-31 */
81
0
    memset(&tm, 0, sizeof tm);
82
0
    r = strptime(date, "%Y-%m-%d", &tm);
83
0
    if (!r || (r != &date[LY_REV_SIZE - 1])) {
84
0
        goto error;
85
0
    }
86
0
    memcpy(&tm_, &tm, sizeof tm);
87
0
    mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
88
0
    if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
89
        /* checking days is enough, since other errors
90
         * have been checked by strptime() */
91
0
        goto error;
92
0
    }
93
94
0
    return LY_SUCCESS;
95
96
0
error:
97
0
    if (stmt) {
98
0
        LOGVAL_PARSER(ctx, LY_VCODE_INVAL, date_len, date, stmt);
99
0
    }
100
0
    return LY_EINVAL;
101
0
}
102
103
void
104
lysp_sort_revisions(struct lysp_revision *revs)
105
0
{
106
0
    LY_ARRAY_COUNT_TYPE i, r;
107
0
    struct lysp_revision rev;
108
109
0
    for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
110
0
        if (strcmp(revs[i].date, revs[r].date) > 0) {
111
0
            r = i;
112
0
        }
113
0
    }
114
115
0
    if (r) {
116
        /* the newest revision is not on position 0, switch them */
117
0
        memcpy(&rev, &revs[0], sizeof rev);
118
0
        memcpy(&revs[0], &revs[r], sizeof rev);
119
0
        memcpy(&revs[r], &rev, sizeof rev);
120
0
    }
121
0
}
122
123
static const struct lysp_tpdf *
124
lysp_type_match(const char *name, struct lysp_node *node)
125
0
{
126
0
    const struct lysp_tpdf *typedefs;
127
0
    LY_ARRAY_COUNT_TYPE u;
128
129
0
    typedefs = lysp_node_typedefs(node);
130
0
    LY_ARRAY_FOR(typedefs, u) {
131
0
        if (!strcmp(name, typedefs[u].name)) {
132
            /* match */
133
0
            return &typedefs[u];
134
0
        }
135
0
    }
136
137
0
    return NULL;
138
0
}
139
140
static LY_DATA_TYPE
141
lysp_type_str2builtin(const char *name, size_t len)
142
0
{
143
0
    if (len >= 4) { /* otherwise it does not match any built-in type */
144
0
        if (name[0] == 'b') {
145
0
            if (name[1] == 'i') {
146
0
                if ((len == 6) && !strncmp(&name[2], "nary", 4)) {
147
0
                    return LY_TYPE_BINARY;
148
0
                } else if ((len == 4) && !strncmp(&name[2], "ts", 2)) {
149
0
                    return LY_TYPE_BITS;
150
0
                }
151
0
            } else if ((len == 7) && !strncmp(&name[1], "oolean", 6)) {
152
0
                return LY_TYPE_BOOL;
153
0
            }
154
0
        } else if (name[0] == 'd') {
155
0
            if ((len == 9) && !strncmp(&name[1], "ecimal64", 8)) {
156
0
                return LY_TYPE_DEC64;
157
0
            }
158
0
        } else if (name[0] == 'e') {
159
0
            if ((len == 5) && !strncmp(&name[1], "mpty", 4)) {
160
0
                return LY_TYPE_EMPTY;
161
0
            } else if ((len == 11) && !strncmp(&name[1], "numeration", 10)) {
162
0
                return LY_TYPE_ENUM;
163
0
            }
164
0
        } else if (name[0] == 'i') {
165
0
            if (name[1] == 'n') {
166
0
                if ((len == 4) && !strncmp(&name[2], "t8", 2)) {
167
0
                    return LY_TYPE_INT8;
168
0
                } else if (len == 5) {
169
0
                    if (!strncmp(&name[2], "t16", 3)) {
170
0
                        return LY_TYPE_INT16;
171
0
                    } else if (!strncmp(&name[2], "t32", 3)) {
172
0
                        return LY_TYPE_INT32;
173
0
                    } else if (!strncmp(&name[2], "t64", 3)) {
174
0
                        return LY_TYPE_INT64;
175
0
                    }
176
0
                } else if ((len == 19) && !strncmp(&name[2], "stance-identifier", 17)) {
177
0
                    return LY_TYPE_INST;
178
0
                }
179
0
            } else if ((len == 11) && !strncmp(&name[1], "dentityref", 10)) {
180
0
                return LY_TYPE_IDENT;
181
0
            }
182
0
        } else if (name[0] == 'l') {
183
0
            if ((len == 7) && !strncmp(&name[1], "eafref", 6)) {
184
0
                return LY_TYPE_LEAFREF;
185
0
            }
186
0
        } else if (name[0] == 's') {
187
0
            if ((len == 6) && !strncmp(&name[1], "tring", 5)) {
188
0
                return LY_TYPE_STRING;
189
0
            }
190
0
        } else if (name[0] == 'u') {
191
0
            if (name[1] == 'n') {
192
0
                if ((len == 5) && !strncmp(&name[2], "ion", 3)) {
193
0
                    return LY_TYPE_UNION;
194
0
                }
195
0
            } else if ((name[1] == 'i') && (name[2] == 'n') && (name[3] == 't')) {
196
0
                if ((len == 5) && (name[4] == '8')) {
197
0
                    return LY_TYPE_UINT8;
198
0
                } else if (len == 6) {
199
0
                    if (!strncmp(&name[4], "16", 2)) {
200
0
                        return LY_TYPE_UINT16;
201
0
                    } else if (!strncmp(&name[4], "32", 2)) {
202
0
                        return LY_TYPE_UINT32;
203
0
                    } else if (!strncmp(&name[4], "64", 2)) {
204
0
                        return LY_TYPE_UINT64;
205
0
                    }
206
0
                }
207
0
            }
208
0
        }
209
0
    }
210
211
0
    return LY_TYPE_UNKNOWN;
212
0
}
213
214
LY_ERR
215
lysp_type_find(const char *id, struct lysp_node *start_node, const struct lysp_module *start_module,
216
        LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node)
217
0
{
218
0
    const char *str, *name;
219
0
    struct lysp_tpdf *typedefs;
220
0
    const struct lys_module *mod;
221
0
    const struct lysp_module *local_module;
222
0
    LY_ARRAY_COUNT_TYPE u, v;
223
224
0
    assert(id);
225
0
    assert(start_module);
226
0
    assert(tpdf);
227
0
    assert(node);
228
229
0
    *node = NULL;
230
0
    str = strchr(id, ':');
231
0
    if (str) {
232
0
        mod = ly_resolve_prefix(start_module->mod->ctx, id, str - id, LY_VALUE_SCHEMA, (void *)start_module);
233
0
        local_module = mod ? mod->parsed : NULL;
234
0
        name = str + 1;
235
0
        *type = LY_TYPE_UNKNOWN;
236
0
    } else {
237
0
        local_module = start_module;
238
0
        name = id;
239
240
        /* check for built-in types */
241
0
        *type = lysp_type_str2builtin(name, strlen(name));
242
0
        if (*type) {
243
0
            *tpdf = NULL;
244
0
            return LY_SUCCESS;
245
0
        }
246
0
    }
247
0
    LY_CHECK_RET(!local_module, LY_ENOTFOUND);
248
249
0
    if (start_node && (local_module == start_module)) {
250
        /* search typedefs in parent's nodes */
251
0
        *node = start_node;
252
0
        while (*node) {
253
0
            *tpdf = lysp_type_match(name, *node);
254
0
            if (*tpdf) {
255
                /* match */
256
0
                return LY_SUCCESS;
257
0
            }
258
0
            *node = (*node)->parent;
259
0
        }
260
0
    }
261
262
    /* search in top-level typedefs */
263
0
    if (local_module->typedefs) {
264
0
        LY_ARRAY_FOR(local_module->typedefs, u) {
265
0
            if (!strcmp(name, local_module->typedefs[u].name)) {
266
                /* match */
267
0
                *tpdf = &local_module->typedefs[u];
268
0
                return LY_SUCCESS;
269
0
            }
270
0
        }
271
0
    }
272
273
    /* search in submodules' typedefs */
274
0
    LY_ARRAY_FOR(local_module->includes, u) {
275
0
        typedefs = local_module->includes[u].submodule->typedefs;
276
0
        LY_ARRAY_FOR(typedefs, v) {
277
0
            if (!strcmp(name, typedefs[v].name)) {
278
                /* match */
279
0
                *tpdf = &typedefs[v];
280
0
                return LY_SUCCESS;
281
0
            }
282
0
        }
283
0
    }
284
285
0
    return LY_ENOTFOUND;
286
0
}
287
288
LY_ERR
289
lysp_check_enum_name(struct lys_parser_ctx *ctx, const char *name, size_t name_len)
290
0
{
291
0
    if (!name_len) {
292
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not be zero-length.");
293
0
        return LY_EVALID;
294
0
    } else if (isspace(name[0]) || isspace(name[name_len - 1])) {
295
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Enum name must not have any leading or trailing whitespaces (\"%.*s\").",
296
0
                (int)name_len, name);
297
0
        return LY_EVALID;
298
0
    } else {
299
0
        for (size_t u = 0; u < name_len; ++u) {
300
0
            if (iscntrl(name[u])) {
301
0
                LOGWRN(PARSER_CTX(ctx), "Control characters in enum name should be avoided (\"%.*s\", character number %d).",
302
0
                        (int)name_len, name, u + 1);
303
0
                break;
304
0
            }
305
0
        }
306
0
    }
307
308
0
    return LY_SUCCESS;
309
0
}
310
311
/**
312
 * @brief Check name of a new type to avoid name collisions.
313
 *
314
 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
315
 * @param[in] node Schema node where the type is being defined, NULL in case of a top-level typedef.
316
 * @param[in] tpdf Typedef definition to check.
317
 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
318
 *            typedefs are checked, caller is supposed to free the table.
319
 * @param[in,out] tpdfs_global Initialized hash table to store temporary data between calls. When the module's
320
 *            typedefs are checked, caller is supposed to free the table.
321
 * @return LY_EEXIST in case of collision, LY_SUCCESS otherwise.
322
 */
323
static LY_ERR
324
lysp_check_dup_typedef(struct lys_parser_ctx *ctx, struct lysp_node *node, const struct lysp_tpdf *tpdf,
325
        struct hash_table *tpdfs_global, struct hash_table *tpdfs_scoped)
326
0
{
327
0
    struct lysp_node *parent;
328
0
    uint32_t hash;
329
0
    size_t name_len;
330
0
    const char *name;
331
0
    LY_ARRAY_COUNT_TYPE u;
332
0
    const struct lysp_tpdf *typedefs;
333
334
0
    assert(ctx);
335
0
    assert(tpdf);
336
337
0
    name = tpdf->name;
338
0
    name_len = strlen(name);
339
340
0
    if (lysp_type_str2builtin(name, name_len)) {
341
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with a built-in type.", name);
342
0
        return LY_EEXIST;
343
0
    }
344
345
    /* check locally scoped typedefs (avoid name shadowing) */
346
0
    if (node) {
347
0
        typedefs = lysp_node_typedefs(node);
348
0
        LY_ARRAY_FOR(typedefs, u) {
349
0
            if (&typedefs[u] == tpdf) {
350
0
                break;
351
0
            }
352
0
            if (!strcmp(name, typedefs[u].name)) {
353
0
                LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with sibling type.", name);
354
0
                return LY_EEXIST;
355
0
            }
356
0
        }
357
        /* search typedefs in parent's nodes */
358
0
        for (parent = node->parent; parent; parent = parent->parent) {
359
0
            if (lysp_type_match(name, parent)) {
360
0
                LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another scoped type.", name);
361
0
                return LY_EEXIST;
362
0
            }
363
0
        }
364
0
    }
365
366
    /* check collision with the top-level typedefs */
367
0
    hash = dict_hash(name, name_len);
368
0
    if (node) {
369
0
        lyht_insert(tpdfs_scoped, &name, hash, NULL);
370
0
        if (!lyht_find(tpdfs_global, &name, hash, NULL)) {
371
0
            LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - scoped type collide with a top-level type.", name);
372
0
            return LY_EEXIST;
373
0
        }
374
0
    } else {
375
0
        if (lyht_insert(tpdfs_global, &name, hash, NULL)) {
376
0
            LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid name \"%s\" of typedef - name collision with another top-level type.", name);
377
0
            return LY_EEXIST;
378
0
        }
379
        /* it is not necessary to test collision with the scoped types - in lysp_check_typedefs, all the
380
         * top-level typedefs are inserted into the tables before the scoped typedefs, so the collision
381
         * is detected in the first branch few lines above */
382
0
    }
383
384
0
    return LY_SUCCESS;
385
0
}
386
387
/**
388
 * @brief Compare identifiers.
389
 * Implementation of ::lyht_value_equal_cb.
390
 */
391
static ly_bool
392
lysp_id_cmp(void *val1, void *val2, ly_bool UNUSED(mod), void *UNUSED(cb_data))
393
0
{
394
0
    return strcmp(val1, val2) == 0 ? 1 : 0;
395
0
}
396
397
LY_ERR
398
lysp_check_dup_typedefs(struct lys_parser_ctx *ctx, struct lysp_module *mod)
399
0
{
400
0
    struct hash_table *ids_global;
401
0
    struct hash_table *ids_scoped;
402
0
    const struct lysp_tpdf *typedefs;
403
0
    LY_ARRAY_COUNT_TYPE u, v;
404
0
    uint32_t i;
405
0
    LY_ERR ret = LY_SUCCESS;
406
407
    /* check name collisions - typedefs and groupings */
408
0
    ids_global = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
409
0
    ids_scoped = lyht_new(LYHT_MIN_SIZE, sizeof(char *), lysp_id_cmp, NULL, 1);
410
0
    LY_ARRAY_FOR(mod->typedefs, v) {
411
0
        ret = lysp_check_dup_typedef(ctx, NULL, &mod->typedefs[v], ids_global, ids_scoped);
412
0
        LY_CHECK_GOTO(ret, cleanup);
413
0
    }
414
0
    LY_ARRAY_FOR(mod->includes, v) {
415
0
        LY_ARRAY_FOR(mod->includes[v].submodule->typedefs, u) {
416
0
            ret = lysp_check_dup_typedef(ctx, NULL, &mod->includes[v].submodule->typedefs[u], ids_global, ids_scoped);
417
0
            LY_CHECK_GOTO(ret, cleanup);
418
0
        }
419
0
    }
420
0
    for (i = 0; i < ctx->tpdfs_nodes.count; ++i) {
421
0
        typedefs = lysp_node_typedefs((struct lysp_node *)ctx->tpdfs_nodes.objs[i]);
422
0
        LY_ARRAY_FOR(typedefs, u) {
423
0
            ret = lysp_check_dup_typedef(ctx, (struct lysp_node *)ctx->tpdfs_nodes.objs[i], &typedefs[u], ids_global, ids_scoped);
424
0
            LY_CHECK_GOTO(ret, cleanup);
425
0
        }
426
0
    }
427
428
0
cleanup:
429
0
    lyht_free(ids_global);
430
0
    lyht_free(ids_scoped);
431
0
    return ret;
432
0
}
433
434
static ly_bool
435
ly_ptrequal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
436
0
{
437
0
    void *ptr1 = *((void **)val1_p), *ptr2 = *((void **)val2_p);
438
439
0
    return ptr1 == ptr2 ? 1 : 0;
440
0
}
441
442
LY_ERR
443
lysp_check_dup_features(struct lys_parser_ctx *ctx, struct lysp_module *mod)
444
0
{
445
0
    LY_ARRAY_COUNT_TYPE u;
446
0
    struct hash_table *ht;
447
0
    struct lysp_feature *f;
448
0
    uint32_t hash;
449
0
    LY_ERR ret = LY_SUCCESS, r;
450
451
0
    ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
452
0
    LY_CHECK_RET(!ht, LY_EMEM);
453
454
    /* add all module features into a hash table */
455
0
    LY_ARRAY_FOR(mod->features, struct lysp_feature, f) {
456
0
        hash = dict_hash(f->name, strlen(f->name));
457
0
        r = lyht_insert(ht, &f->name, hash, NULL);
458
0
        if (r == LY_EEXIST) {
459
0
            LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
460
0
            ret = LY_EVALID;
461
0
            goto cleanup;
462
0
        } else if (r) {
463
0
            ret = r;
464
0
            goto cleanup;
465
0
        }
466
0
    }
467
468
    /* add all submodule features into a hash table */
469
0
    LY_ARRAY_FOR(mod->includes, u) {
470
0
        LY_ARRAY_FOR(mod->includes[u].submodule->features, struct lysp_feature, f) {
471
0
            hash = dict_hash(f->name, strlen(f->name));
472
0
            r = lyht_insert(ht, &f->name, hash, NULL);
473
0
            if (r == LY_EEXIST) {
474
0
                LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, f->name, "feature");
475
0
                ret = LY_EVALID;
476
0
                goto cleanup;
477
0
            } else if (r) {
478
0
                ret = r;
479
0
                goto cleanup;
480
0
            }
481
0
        }
482
0
    }
483
484
0
cleanup:
485
0
    lyht_free(ht);
486
0
    return ret;
487
0
}
488
489
LY_ERR
490
lysp_check_dup_identities(struct lys_parser_ctx *ctx, struct lysp_module *mod)
491
0
{
492
0
    LY_ARRAY_COUNT_TYPE u;
493
0
    struct hash_table *ht;
494
0
    struct lysp_ident *i;
495
0
    uint32_t hash;
496
0
    LY_ERR ret = LY_SUCCESS, r;
497
498
0
    ht = lyht_new(1, sizeof(void *), ly_ptrequal_cb, NULL, 1);
499
0
    LY_CHECK_RET(!ht, LY_EMEM);
500
501
    /* add all module identities into a hash table */
502
0
    LY_ARRAY_FOR(mod->identities, struct lysp_ident, i) {
503
0
        hash = dict_hash(i->name, strlen(i->name));
504
0
        r = lyht_insert(ht, &i->name, hash, NULL);
505
0
        if (r == LY_EEXIST) {
506
0
            LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
507
0
            ret = LY_EVALID;
508
0
            goto cleanup;
509
0
        } else if (r) {
510
0
            ret = r;
511
0
            goto cleanup;
512
0
        }
513
0
    }
514
515
    /* add all submodule identities into a hash table */
516
0
    LY_ARRAY_FOR(mod->includes, u) {
517
0
        LY_ARRAY_FOR(mod->includes[u].submodule->identities, struct lysp_ident, i) {
518
0
            hash = dict_hash(i->name, strlen(i->name));
519
0
            r = lyht_insert(ht, &i->name, hash, NULL);
520
0
            if (r == LY_EEXIST) {
521
0
                LOGVAL_PARSER(ctx, LY_VCODE_DUPIDENT, i->name, "identity");
522
0
                ret = LY_EVALID;
523
0
                goto cleanup;
524
0
            } else if (r) {
525
0
                ret = r;
526
0
                goto cleanup;
527
0
            }
528
0
        }
529
0
    }
530
531
0
cleanup:
532
0
    lyht_free(ht);
533
0
    return ret;
534
0
}
535
536
struct lysp_load_module_check_data {
537
    const char *name;
538
    const char *revision;
539
    const char *path;
540
    const char *submoduleof;
541
};
542
543
static LY_ERR
544
lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data)
545
0
{
546
0
    struct lysp_load_module_check_data *info = data;
547
0
    const char *filename, *dot, *rev, *name;
548
0
    uint8_t latest_revision;
549
0
    size_t len;
550
0
    struct lysp_revision *revs;
551
552
0
    name = mod ? mod->mod->name : submod->name;
553
0
    revs = mod ? mod->revs : submod->revs;
554
0
    latest_revision = mod ? mod->mod->latest_revision : submod->latest_revision;
555
556
0
    if (info->name) {
557
        /* check name of the parsed model */
558
0
        if (strcmp(info->name, name)) {
559
0
            LOGERR(ctx, LY_EINVAL, "Unexpected module \"%s\" parsed instead of \"%s\").", name, info->name);
560
0
            return LY_EINVAL;
561
0
        }
562
0
    }
563
0
    if (info->revision) {
564
        /* check revision of the parsed model */
565
0
        if (!revs || strcmp(info->revision, revs[0].date)) {
566
0
            LOGERR(ctx, LY_EINVAL, "Module \"%s\" parsed with the wrong revision (\"%s\" instead \"%s\").", name,
567
0
                    revs ? revs[0].date : "none", info->revision);
568
0
            return LY_EINVAL;
569
0
        }
570
0
    } else if (!latest_revision) {
571
        /* do not log, we just need to drop the schema and use the latest revision from the context */
572
0
        return LY_EEXIST;
573
0
    }
574
0
    if (submod) {
575
0
        assert(info->submoduleof);
576
577
        /* check that the submodule belongs-to our module */
578
0
        if (strcmp(info->submoduleof, submod->mod->name)) {
579
0
            LOGVAL(ctx, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
580
0
                    submod->name, info->submoduleof, submod->mod->name);
581
0
            return LY_EVALID;
582
0
        }
583
        /* check circular dependency */
584
0
        if (submod->parsing) {
585
0
            LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".", submod->name);
586
0
            return LY_EVALID;
587
0
        }
588
0
    }
589
0
    if (info->path) {
590
        /* check that name and revision match filename */
591
0
        filename = strrchr(info->path, '/');
592
0
        if (!filename) {
593
0
            filename = info->path;
594
0
        } else {
595
0
            filename++;
596
0
        }
597
        /* name */
598
0
        len = strlen(name);
599
0
        rev = strchr(filename, '@');
600
0
        dot = strrchr(info->path, '.');
601
0
        if (strncmp(filename, name, len) ||
602
0
                ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
603
0
            LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, name);
604
0
        }
605
        /* revision */
606
0
        if (rev) {
607
0
            len = dot - ++rev;
608
0
            if (!revs || (len != LY_REV_SIZE - 1) || strncmp(revs[0].date, rev, len)) {
609
0
                LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
610
0
                        revs ? revs[0].date : "none");
611
0
            }
612
0
        }
613
0
    }
614
0
    return LY_SUCCESS;
615
0
}
616
617
/**
618
 * @brief Load the (sub)module into the context.
619
 *
620
 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
621
 *
622
 * module_name and submodule_name are alternatives - only one of the
623
 *
624
 * @param[in] ctx libyang context where to work.
625
 * @param[in] name Name of the (sub)module to load.
626
 * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded.
627
 * @param[in] features Array of enabled features ended with NULL.
628
 * @param[in] need_implemented Whether the (sub)module is needed implemented or not.
629
 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
630
 * @param[in] main_name Main module name in case of loading submodule.
631
 * @param[in] required Module is required so error (even if the input file not found) are important. If 0, there is some
632
 * backup and it is actually ok if the input data are not found. However, parser reports errors even in this case.
633
 * @param[in,out] unres Global unres structure for newly implemented modules.
634
 * @param[out] result Parsed YANG schema tree of the requested module (struct lys_module*) or submodule (struct lysp_submodule*).
635
 * If it is a module, it is already in the context!
636
 * @return LY_ERR value, in case of LY_SUCCESS, the \arg result is always provided.
637
 */
638
static LY_ERR
639
lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, const char **features,
640
        ly_bool need_implemented, struct lys_parser_ctx *main_ctx, const char *main_name, ly_bool required,
641
        struct lys_glob_unres *unres, void **result)
642
0
{
643
0
    struct ly_in *in;
644
0
    char *filepath = NULL;
645
0
    LYS_INFORMAT format;
646
0
    void *mod = NULL;
647
0
    LY_ERR ret = LY_SUCCESS;
648
0
    struct lysp_load_module_check_data check_data = {0};
649
650
0
    LY_CHECK_RET(lys_search_localfile(ly_ctx_get_searchdirs(ctx), !(ctx->flags & LY_CTX_DISABLE_SEARCHDIR_CWD), name, revision,
651
0
            &filepath, &format));
652
0
    if (!filepath) {
653
0
        if (required) {
654
0
            LOGERR(ctx, LY_ENOTFOUND, "Data model \"%s%s%s\" not found in local searchdirs.", name, revision ? "@" : "",
655
0
                    revision ? revision : "");
656
0
        }
657
0
        return LY_ENOTFOUND;
658
0
    }
659
660
0
    LOGVRB("Loading schema from \"%s\" file.", filepath);
661
662
    /* get the (sub)module */
663
0
    LY_CHECK_ERR_GOTO(ret = ly_in_new_filepath(filepath, 0, &in),
664
0
            LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", filepath), cleanup);
665
0
    check_data.name = name;
666
0
    check_data.revision = revision;
667
0
    check_data.path = filepath;
668
0
    check_data.submoduleof = main_name;
669
0
    if (main_ctx) {
670
0
        ret = lys_parse_submodule(ctx, in, format, main_ctx, lysp_load_module_check, &check_data,
671
0
                (struct lysp_submodule **)&mod);
672
0
    } else {
673
0
        ret = lys_create_module(ctx, in, format, need_implemented, lysp_load_module_check, &check_data, features, unres,
674
0
                (struct lys_module **)&mod);
675
676
0
    }
677
0
    ly_in_free(in, 1);
678
0
    LY_CHECK_GOTO(ret, cleanup);
679
680
0
    *result = mod;
681
682
    /* success */
683
684
0
cleanup:
685
0
    free(filepath);
686
0
    return ret;
687
0
}
688
689
LY_ERR
690
lys_load_module(struct ly_ctx *ctx, const char *name, const char *revision, ly_bool need_implemented,
691
        const char **features, struct lys_glob_unres *unres, struct lys_module **mod)
692
0
{
693
0
    const char *module_data = NULL;
694
0
    LYS_INFORMAT format = LYS_IN_UNKNOWN;
695
696
0
    void (*module_data_free)(void *module_data, void *user_data) = NULL;
697
0
    struct lysp_load_module_check_data check_data = {0};
698
0
    struct lys_module *ctx_latest = NULL, *m;
699
0
    struct ly_in *in;
700
0
    LY_ERR ret;
701
0
    ly_bool implement;
702
703
0
    assert(mod && unres);
704
705
0
    if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
706
0
        implement = 1;
707
0
    } else {
708
0
        implement = need_implemented;
709
0
    }
710
711
    /*
712
     * try to get the module from the context
713
     */
714
0
    if (!*mod) {
715
0
        if (revision) {
716
            /* get the specific revision */
717
0
            *mod = (struct lys_module *)ly_ctx_get_module(ctx, name, revision);
718
0
        } else {
719
0
            if (implement) {
720
                /* prefer the implemented module instead of the latest one */
721
0
                *mod = (struct lys_module *)ly_ctx_get_module_implemented(ctx, name);
722
0
            }
723
0
            if (!*mod) {
724
                /* get the requested module of the latest revision in the context */
725
0
                *mod = (struct lys_module *)ly_ctx_get_module_latest(ctx, name);
726
0
                if (*mod && ((*mod)->latest_revision == 1)) {
727
                    /* let us now search with callback and searchpaths to check if there is newer revision outside the context */
728
0
                    ctx_latest = *mod;
729
0
                    *mod = NULL;
730
0
                }
731
0
            }
732
0
        }
733
0
    }
734
735
    /* check collision with other implemented revision */
736
0
    if (implement) {
737
0
        m = ly_ctx_get_module_implemented(ctx, name);
738
0
        if (m && (!*mod || (*mod && (m != *mod)))) {
739
0
            LOGVAL(ctx, LYVE_REFERENCE, "Module \"%s\" is already present in other implemented revision.", name);
740
0
            *mod = NULL;
741
0
            return LY_EDENIED;
742
0
        }
743
0
    }
744
745
    /*
746
     * no suitable module in the context, try to load it
747
     */
748
0
    if (!*mod) {
749
        /* module not present in the context, get the input data and parse it */
750
0
        if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
751
0
search_clb:
752
0
            if (ctx->imp_clb) {
753
0
                if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
754
0
                        &format, &module_data, &module_data_free) == LY_SUCCESS) {
755
0
                    LY_CHECK_RET(ly_in_new_memory(module_data, &in));
756
0
                    check_data.name = name;
757
0
                    check_data.revision = revision;
758
0
                    lys_create_module(ctx, in, format, implement, lysp_load_module_check, &check_data, features, unres, mod);
759
0
                    ly_in_free(in, 0);
760
0
                    if (module_data_free) {
761
0
                        module_data_free((void *)module_data, ctx->imp_clb_data);
762
0
                    }
763
0
                }
764
0
            }
765
0
            if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
766
0
                goto search_file;
767
0
            }
768
0
        } else {
769
0
search_file:
770
0
            if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
771
                /* module was not received from the callback or there is no callback set */
772
0
                lys_module_localfile(ctx, name, revision, features, implement, NULL, NULL, ctx_latest ? 0 : 1, unres,
773
0
                        (void **)mod);
774
0
            }
775
0
            if (!*mod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
776
0
                goto search_clb;
777
0
            }
778
0
        }
779
780
        /* update the latest_revision flag - here we have selected the latest available schema,
781
         * consider that even the callback provides correct latest revision */
782
0
        if (!*mod && ctx_latest) {
783
0
            LOGVRB("Newer revision than \"%s@%s\" not found, using this as the latest revision.", ctx_latest->name,
784
0
                    ctx_latest->revision);
785
0
            ctx_latest->latest_revision = 2;
786
0
            *mod = ctx_latest;
787
0
        } else if (*mod && !revision && ((*mod)->latest_revision == 1)) {
788
0
            (*mod)->latest_revision = 2;
789
0
        }
790
791
0
        if (!*mod) {
792
0
            LOGVAL(ctx, LYVE_REFERENCE, "%s \"%s\" module failed.", implement ? "Loading" : "Importing", name);
793
0
            return LY_EVALID;
794
0
        }
795
0
    } else {
796
        /* we have module from the current context, circular check */
797
0
        if ((*mod)->parsed->parsing) {
798
0
            LOGVAL(ctx, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
799
0
            *mod = NULL;
800
0
            return LY_EVALID;
801
0
        }
802
0
    }
803
804
    /*
805
     * module found, make sure it is implemented if should be
806
     */
807
0
    if (implement) {
808
0
        if (!(*mod)->implemented) {
809
            /* implement */
810
0
            ret = lys_set_implemented_r(*mod, features, unres);
811
0
            if (ret) {
812
0
                *mod = NULL;
813
0
                return ret;
814
0
            }
815
0
        } else if (features) {
816
            /* set features if different */
817
0
            ret = lys_set_features((*mod)->parsed, features);
818
0
            if (!ret) {
819
                /* context need to be recompiled so that feature changes are properly applied */
820
0
                unres->recompile = 1;
821
0
            } else if (ret != LY_EEXIST) {
822
                /* error */
823
0
                return ret;
824
0
            } /* else no feature changes */
825
0
        }
826
0
    }
827
828
0
    return LY_SUCCESS;
829
0
}
830
831
LY_ERR
832
lysp_check_stringchar(struct lys_parser_ctx *ctx, uint32_t c)
833
0
{
834
0
    if (!is_yangutf8char(c)) {
835
0
        LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
836
0
        return LY_EVALID;
837
0
    }
838
0
    return LY_SUCCESS;
839
0
}
840
841
LY_ERR
842
lysp_check_identifierchar(struct lys_parser_ctx *ctx, uint32_t c, ly_bool first, uint8_t *prefix)
843
0
{
844
0
    if (first || (prefix && ((*prefix) == 1))) {
845
0
        if (!is_yangidentstartchar(c)) {
846
0
            if (isprint(c)) {
847
0
                LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
848
0
            } else {
849
0
                LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
850
0
            }
851
0
            return LY_EVALID;
852
0
        }
853
0
        if (prefix) {
854
0
            if (first) {
855
0
                (*prefix) = 0;
856
0
            } else {
857
0
                (*prefix) = 2;
858
0
            }
859
0
        }
860
0
    } else if ((c == ':') && prefix && ((*prefix) == 0)) {
861
0
        (*prefix) = 1;
862
0
    } else if (!is_yangidentchar(c)) {
863
0
        LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
864
0
        return LY_EVALID;
865
0
    }
866
867
0
    return LY_SUCCESS;
868
0
}
869
870
/**
871
 * @brief Try to find the parsed submodule in main module for the given include record.
872
 *
873
 * @param[in] pctx main parser context
874
 * @param[in] inc The include record with missing parsed submodule. According to include info try to find
875
 * the corresponding parsed submodule in main module's includes.
876
 * @return LY_SUCCESS - the parsed submodule was found and inserted into the @p inc record
877
 * @return LY_ENOT - the parsed module was not found.
878
 * @return LY_EVALID - YANG rule violation
879
 */
880
static LY_ERR
881
lysp_get_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
882
0
{
883
0
    LY_ARRAY_COUNT_TYPE i;
884
0
    struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
885
886
0
    LY_ARRAY_FOR(main_pmod->includes, i) {
887
0
        if (strcmp(main_pmod->includes[i].name, inc->name)) {
888
0
            continue;
889
0
        }
890
891
0
        if (inc->rev[0] && strncmp(inc->rev, main_pmod->includes[i].rev, LY_REV_SIZE)) {
892
0
            LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
893
0
                    "Submodule %s includes different revision (%s) of the submodule %s:%s included by the main module %s.",
894
0
                    ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->rev,
895
0
                    main_pmod->includes[i].name, main_pmod->includes[i].rev, main_pmod->mod->name);
896
0
            return LY_EVALID;
897
0
        }
898
899
0
        inc->submodule = main_pmod->includes[i].submodule;
900
0
        return inc->submodule ? LY_SUCCESS : LY_ENOT;
901
0
    }
902
903
0
    if (main_pmod->version == LYS_VERSION_1_1) {
904
0
        LOGVAL(PARSER_CTX(pctx), LYVE_REFERENCE,
905
0
                "YANG 1.1 requires all submodules to be included from main module. "
906
0
                "But submodule \"%s\" includes submodule \"%s\" which is not included by main module \"%s\".",
907
0
                ((struct lysp_submodule *)pctx->parsed_mod)->name, inc->name, main_pmod->mod->name);
908
0
        return LY_EVALID;
909
0
    } else {
910
0
        return LY_ENOT;
911
0
    }
912
0
}
913
914
/**
915
 * @brief Make the copy of the given include record into the main module.
916
 *
917
 * YANG 1.0 does not require the main module to include all the submodules. Therefore, parsing submodules can cause
918
 * reallocating and extending the includes array in the main module by the submodules included only in submodules.
919
 *
920
 * @param[in] pctx main parser context
921
 * @param[in] inc Include record to copy into main module taken from @p pctx.
922
 * @return LY_ERR value.
923
 */
924
static LY_ERR
925
lysp_inject_submodule(struct lys_parser_ctx *pctx, struct lysp_include *inc)
926
0
{
927
0
    LY_ARRAY_COUNT_TYPE i;
928
0
    struct lysp_include *inc_new, *inc_tofill = NULL;
929
0
    struct lysp_module *main_pmod = pctx->parsed_mod->mod->parsed;
930
931
    /* first, try to find the corresponding record with missing parsed submodule */
932
0
    LY_ARRAY_FOR(main_pmod->includes, i) {
933
0
        if (strcmp(main_pmod->includes[i].name, inc->name)) {
934
0
            continue;
935
0
        }
936
0
        inc_tofill = &main_pmod->includes[i];
937
0
        break;
938
0
    }
939
940
0
    if (inc_tofill) {
941
0
        inc_tofill->submodule = inc->submodule;
942
0
    } else {
943
0
        LY_ARRAY_NEW_RET(PARSER_CTX(pctx), main_pmod->includes, inc_new, LY_EMEM);
944
945
0
        inc_new->submodule = inc->submodule;
946
0
        DUP_STRING_RET(PARSER_CTX(pctx), inc->name, inc_new->name);
947
0
        DUP_STRING_RET(PARSER_CTX(pctx), inc->dsc, inc_new->dsc);
948
0
        DUP_STRING_RET(PARSER_CTX(pctx), inc->ref, inc_new->ref);
949
        /* TODO duplicate extensions */
950
0
        memcpy(inc_new->rev, inc->rev, LY_REV_SIZE);
951
0
        inc_new->injected = 1;
952
0
    }
953
0
    return LY_SUCCESS;
954
0
}
955
956
LY_ERR
957
lysp_load_submodules(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
958
0
{
959
0
    LY_ARRAY_COUNT_TYPE u;
960
0
    struct ly_ctx *ctx = PARSER_CTX(pctx);
961
962
0
    LY_ARRAY_FOR(pmod->includes, u) {
963
0
        LY_ERR ret = LY_SUCCESS;
964
0
        struct lysp_submodule *submod = NULL;
965
0
        struct lysp_include *inc = &pmod->includes[u];
966
967
0
        if (inc->submodule) {
968
0
            continue;
969
0
        }
970
971
0
        if (pmod->is_submod) {
972
            /* try to find the submodule in the main module or its submodules */
973
0
            ret = lysp_get_submodule(pctx, inc);
974
0
            LY_CHECK_RET(ret && ret != LY_ENOT, ret);
975
0
            LY_CHECK_RET(ret == LY_SUCCESS, LY_SUCCESS); /* submodule found in linked with the inc */
976
0
        }
977
978
        /* submodule not present in the main module, get the input data and parse it */
979
0
        if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
980
0
search_clb:
981
0
            if (ctx->imp_clb) {
982
0
                const char *submodule_data = NULL;
983
0
                LYS_INFORMAT format = LYS_IN_UNKNOWN;
984
0
                void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
985
0
                struct lysp_load_module_check_data check_data = {0};
986
0
                struct ly_in *in;
987
988
0
                if (ctx->imp_clb(pctx->parsed_mod->mod->name, NULL, inc->name,
989
0
                        inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
990
0
                        &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
991
0
                    LY_CHECK_RET(ly_in_new_memory(submodule_data, &in));
992
0
                    check_data.name = inc->name;
993
0
                    check_data.revision = inc->rev[0] ? inc->rev : NULL;
994
0
                    check_data.submoduleof = pctx->parsed_mod->mod->name;
995
0
                    lys_parse_submodule(ctx, in, format, pctx, lysp_load_module_check, &check_data, &submod);
996
997
                    /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
998
                     * submodule's include into main module, where it is missing */
999
0
                    inc = &pmod->includes[u];
1000
1001
0
                    ly_in_free(in, 0);
1002
0
                    if (submodule_data_free) {
1003
0
                        submodule_data_free((void *)submodule_data, ctx->imp_clb_data);
1004
0
                    }
1005
0
                }
1006
0
            }
1007
0
            if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1008
0
                goto search_file;
1009
0
            }
1010
0
        } else {
1011
0
search_file:
1012
0
            if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
1013
                /* submodule was not received from the callback or there is no callback set */
1014
0
                lys_module_localfile(ctx, inc->name,
1015
0
                        inc->rev[0] ? inc->rev : NULL, NULL, 0, pctx,
1016
0
                        pctx->parsed_mod->mod->name, 1, NULL, (void **)&submod);
1017
1018
                /* update inc pointer - parsing another (YANG 1.0) submodule can cause injecting
1019
                 * submodule's include into main module, where it is missing */
1020
0
                inc = &pmod->includes[u];
1021
0
            }
1022
0
            if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
1023
0
                goto search_clb;
1024
0
            }
1025
0
        }
1026
0
        if (submod) {
1027
0
            if (!inc->rev[0] && (submod->latest_revision == 1)) {
1028
                /* update the latest_revision flag - here we have selected the latest available schema,
1029
                 * consider that even the callback provides correct latest revision */
1030
0
                submod->latest_revision = 2;
1031
0
            }
1032
1033
0
            inc->submodule = submod;
1034
0
            if (ret == LY_ENOT) {
1035
                /* the submodule include is not present in YANG 1.0 main module - add it there */
1036
0
                LY_CHECK_RET(lysp_inject_submodule(pctx, &pmod->includes[u]));
1037
0
            }
1038
0
        }
1039
0
        if (!inc->submodule) {
1040
0
            LOGVAL(ctx, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name,
1041
0
                    pctx->parsed_mod->is_submod ? ((struct lysp_submodule *)pctx->parsed_mod)->name : pctx->parsed_mod->mod->name);
1042
0
            return LY_EVALID;
1043
0
        }
1044
0
    }
1045
1046
0
    return LY_SUCCESS;
1047
0
}
1048
1049
API const struct lysc_when *
1050
lysc_has_when(const struct lysc_node *node)
1051
0
{
1052
0
    struct lysc_when **when;
1053
1054
0
    if (!node) {
1055
0
        return NULL;
1056
0
    }
1057
1058
0
    do {
1059
0
        when = lysc_node_when(node);
1060
0
        if (when) {
1061
0
            return when[0];
1062
0
        }
1063
0
        node = node->parent;
1064
0
    } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
1065
1066
0
    return NULL;
1067
0
}
1068
1069
API const char *
1070
lys_nodetype2str(uint16_t nodetype)
1071
0
{
1072
0
    switch (nodetype) {
1073
0
    case LYS_CONTAINER:
1074
0
        return "container";
1075
0
    case LYS_CHOICE:
1076
0
        return "choice";
1077
0
    case LYS_LEAF:
1078
0
        return "leaf";
1079
0
    case LYS_LEAFLIST:
1080
0
        return "leaf-list";
1081
0
    case LYS_LIST:
1082
0
        return "list";
1083
0
    case LYS_ANYXML:
1084
0
        return "anyxml";
1085
0
    case LYS_ANYDATA:
1086
0
        return "anydata";
1087
0
    case LYS_CASE:
1088
0
        return "case";
1089
0
    case LYS_RPC:
1090
0
        return "RPC";
1091
0
    case LYS_ACTION:
1092
0
        return "action";
1093
0
    case LYS_NOTIF:
1094
0
        return "notification";
1095
0
    case LYS_USES:
1096
0
        return "uses";
1097
0
    default:
1098
0
        return "unknown";
1099
0
    }
1100
0
}
1101
1102
API enum ly_stmt
1103
lys_nodetype2stmt(uint16_t nodetype)
1104
0
{
1105
0
    switch (nodetype) {
1106
0
    case LYS_CONTAINER:
1107
0
        return LY_STMT_CONTAINER;
1108
0
    case LYS_CHOICE:
1109
0
        return LY_STMT_CHOICE;
1110
0
    case LYS_LEAF:
1111
0
        return LY_STMT_LEAF;
1112
0
    case LYS_LEAFLIST:
1113
0
        return LY_STMT_LEAF_LIST;
1114
0
    case LYS_LIST:
1115
0
        return LY_STMT_LIST;
1116
0
    case LYS_ANYXML:
1117
0
        return LY_STMT_ANYXML;
1118
0
    case LYS_ANYDATA:
1119
0
        return LY_STMT_ANYDATA;
1120
0
    case LYS_CASE:
1121
0
        return LY_STMT_CASE;
1122
0
    case LYS_RPC:
1123
0
        return LY_STMT_RPC;
1124
0
    case LYS_ACTION:
1125
0
        return LY_STMT_ACTION;
1126
0
    case LYS_NOTIF:
1127
0
        return LY_STMT_NOTIFICATION;
1128
0
    case LYS_USES:
1129
0
        return LY_STMT_USES;
1130
0
    case LYS_INPUT:
1131
0
        return LY_STMT_INPUT;
1132
0
    case LYS_OUTPUT:
1133
0
        return LY_STMT_OUTPUT;
1134
0
    default:
1135
0
        return LY_STMT_NONE;
1136
0
    }
1137
0
}
1138
1139
const char *
1140
lys_datatype2str(LY_DATA_TYPE basetype)
1141
0
{
1142
0
    switch (basetype) {
1143
0
    case LY_TYPE_BINARY:
1144
0
        return "binary";
1145
0
    case LY_TYPE_UINT8:
1146
0
        return "uint8";
1147
0
    case LY_TYPE_UINT16:
1148
0
        return "uint16";
1149
0
    case LY_TYPE_UINT32:
1150
0
        return "uint32";
1151
0
    case LY_TYPE_UINT64:
1152
0
        return "uint64";
1153
0
    case LY_TYPE_STRING:
1154
0
        return "string";
1155
0
    case LY_TYPE_BITS:
1156
0
        return "bits";
1157
0
    case LY_TYPE_BOOL:
1158
0
        return "boolean";
1159
0
    case LY_TYPE_DEC64:
1160
0
        return "decimal64";
1161
0
    case LY_TYPE_EMPTY:
1162
0
        return "empty";
1163
0
    case LY_TYPE_ENUM:
1164
0
        return "enumeration";
1165
0
    case LY_TYPE_IDENT:
1166
0
        return "identityref";
1167
0
    case LY_TYPE_INST:
1168
0
        return "instance-identifier";
1169
0
    case LY_TYPE_LEAFREF:
1170
0
        return "leafref";
1171
0
    case LY_TYPE_UNION:
1172
0
        return "union";
1173
0
    case LY_TYPE_INT8:
1174
0
        return "int8";
1175
0
    case LY_TYPE_INT16:
1176
0
        return "int16";
1177
0
    case LY_TYPE_INT32:
1178
0
        return "int32";
1179
0
    case LY_TYPE_INT64:
1180
0
        return "int64";
1181
0
    default:
1182
0
        return "unknown";
1183
0
    }
1184
0
}
1185
1186
API const struct lysp_tpdf *
1187
lysp_node_typedefs(const struct lysp_node *node)
1188
0
{
1189
0
    switch (node->nodetype) {
1190
0
    case LYS_CONTAINER:
1191
0
        return ((struct lysp_node_container *)node)->typedefs;
1192
0
    case LYS_LIST:
1193
0
        return ((struct lysp_node_list *)node)->typedefs;
1194
0
    case LYS_GROUPING:
1195
0
        return ((struct lysp_node_grp *)node)->typedefs;
1196
0
    case LYS_RPC:
1197
0
    case LYS_ACTION:
1198
0
        return ((struct lysp_node_action *)node)->typedefs;
1199
0
    case LYS_INPUT:
1200
0
    case LYS_OUTPUT:
1201
0
        return ((struct lysp_node_action_inout *)node)->typedefs;
1202
0
    case LYS_NOTIF:
1203
0
        return ((struct lysp_node_notif *)node)->typedefs;
1204
0
    default:
1205
0
        return NULL;
1206
0
    }
1207
0
}
1208
1209
API const struct lysp_node_grp *
1210
lysp_node_groupings(const struct lysp_node *node)
1211
0
{
1212
0
    switch (node->nodetype) {
1213
0
    case LYS_CONTAINER:
1214
0
        return ((struct lysp_node_container *)node)->groupings;
1215
0
    case LYS_LIST:
1216
0
        return ((struct lysp_node_list *)node)->groupings;
1217
0
    case LYS_GROUPING:
1218
0
        return ((struct lysp_node_grp *)node)->groupings;
1219
0
    case LYS_RPC:
1220
0
    case LYS_ACTION:
1221
0
        return ((struct lysp_node_action *)node)->groupings;
1222
0
    case LYS_INPUT:
1223
0
    case LYS_OUTPUT:
1224
0
        return ((struct lysp_node_action_inout *)node)->groupings;
1225
0
    case LYS_NOTIF:
1226
0
        return ((struct lysp_node_notif *)node)->groupings;
1227
0
    default:
1228
0
        return NULL;
1229
0
    }
1230
0
}
1231
1232
struct lysp_node_action **
1233
lysp_node_actions_p(struct lysp_node *node)
1234
0
{
1235
0
    assert(node);
1236
1237
0
    switch (node->nodetype) {
1238
0
    case LYS_CONTAINER:
1239
0
        return &((struct lysp_node_container *)node)->actions;
1240
0
    case LYS_LIST:
1241
0
        return &((struct lysp_node_list *)node)->actions;
1242
0
    case LYS_GROUPING:
1243
0
        return &((struct lysp_node_grp *)node)->actions;
1244
0
    case LYS_AUGMENT:
1245
0
        return &((struct lysp_node_augment *)node)->actions;
1246
0
    default:
1247
0
        return NULL;
1248
0
    }
1249
0
}
1250
1251
API const struct lysp_node_action *
1252
lysp_node_actions(const struct lysp_node *node)
1253
0
{
1254
0
    struct lysp_node_action **actions;
1255
1256
0
    actions = lysp_node_actions_p((struct lysp_node *)node);
1257
0
    if (actions) {
1258
0
        return *actions;
1259
0
    } else {
1260
0
        return NULL;
1261
0
    }
1262
0
}
1263
1264
struct lysp_node_notif **
1265
lysp_node_notifs_p(struct lysp_node *node)
1266
0
{
1267
0
    assert(node);
1268
0
    switch (node->nodetype) {
1269
0
    case LYS_CONTAINER:
1270
0
        return &((struct lysp_node_container *)node)->notifs;
1271
0
    case LYS_LIST:
1272
0
        return &((struct lysp_node_list *)node)->notifs;
1273
0
    case LYS_GROUPING:
1274
0
        return &((struct lysp_node_grp *)node)->notifs;
1275
0
    case LYS_AUGMENT:
1276
0
        return &((struct lysp_node_augment *)node)->notifs;
1277
0
    default:
1278
0
        return NULL;
1279
0
    }
1280
0
}
1281
1282
API const struct lysp_node_notif *
1283
lysp_node_notifs(const struct lysp_node *node)
1284
0
{
1285
0
    struct lysp_node_notif **notifs;
1286
1287
0
    notifs = lysp_node_notifs_p((struct lysp_node *)node);
1288
0
    if (notifs) {
1289
0
        return *notifs;
1290
0
    } else {
1291
0
        return NULL;
1292
0
    }
1293
0
}
1294
1295
struct lysp_node **
1296
lysp_node_child_p(struct lysp_node *node)
1297
0
{
1298
0
    assert(node);
1299
0
    switch (node->nodetype) {
1300
0
    case LYS_CONTAINER:
1301
0
        return &((struct lysp_node_container *)node)->child;
1302
0
    case LYS_CHOICE:
1303
0
        return &((struct lysp_node_choice *)node)->child;
1304
0
    case LYS_LIST:
1305
0
        return &((struct lysp_node_list *)node)->child;
1306
0
    case LYS_CASE:
1307
0
        return &((struct lysp_node_case *)node)->child;
1308
0
    case LYS_GROUPING:
1309
0
        return &((struct lysp_node_grp *)node)->child;
1310
0
    case LYS_AUGMENT:
1311
0
        return &((struct lysp_node_augment *)node)->child;
1312
0
    case LYS_INPUT:
1313
0
    case LYS_OUTPUT:
1314
0
        return &((struct lysp_node_action_inout *)node)->child;
1315
0
    case LYS_NOTIF:
1316
0
        return &((struct lysp_node_notif *)node)->child;
1317
0
    default:
1318
0
        return NULL;
1319
0
    }
1320
0
}
1321
1322
API const struct lysp_node *
1323
lysp_node_child(const struct lysp_node *node)
1324
0
{
1325
0
    struct lysp_node **child;
1326
1327
0
    if (!node) {
1328
0
        return NULL;
1329
0
    }
1330
1331
0
    child = lysp_node_child_p((struct lysp_node *)node);
1332
0
    if (child) {
1333
0
        return *child;
1334
0
    } else {
1335
0
        return NULL;
1336
0
    }
1337
0
}
1338
1339
struct lysp_restr **
1340
lysp_node_musts_p(const struct lysp_node *node)
1341
0
{
1342
0
    if (!node) {
1343
0
        return NULL;
1344
0
    }
1345
1346
0
    switch (node->nodetype) {
1347
0
    case LYS_CONTAINER:
1348
0
        return &((struct lysp_node_container *)node)->musts;
1349
0
    case LYS_LEAF:
1350
0
        return &((struct lysp_node_leaf *)node)->musts;
1351
0
    case LYS_LEAFLIST:
1352
0
        return &((struct lysp_node_leaflist *)node)->musts;
1353
0
    case LYS_LIST:
1354
0
        return &((struct lysp_node_list *)node)->musts;
1355
0
    case LYS_ANYXML:
1356
0
    case LYS_ANYDATA:
1357
0
        return &((struct lysp_node_anydata *)node)->musts;
1358
0
    case LYS_NOTIF:
1359
0
        return &((struct lysp_node_notif *)node)->musts;
1360
0
    case LYS_INPUT:
1361
0
    case LYS_OUTPUT:
1362
0
        return &((struct lysp_node_action_inout *)node)->musts;
1363
0
    default:
1364
0
        return NULL;
1365
0
    }
1366
0
}
1367
1368
struct lysp_restr *
1369
lysp_node_musts(const struct lysp_node *node)
1370
0
{
1371
0
    struct lysp_restr **musts;
1372
1373
0
    musts = lysp_node_musts_p(node);
1374
0
    if (musts) {
1375
0
        return *musts;
1376
0
    } else {
1377
0
        return NULL;
1378
0
    }
1379
0
}
1380
1381
struct lysp_when **
1382
lysp_node_when_p(const struct lysp_node *node)
1383
0
{
1384
0
    if (!node) {
1385
0
        return NULL;
1386
0
    }
1387
1388
0
    switch (node->nodetype) {
1389
0
    case LYS_CONTAINER:
1390
0
        return &((struct lysp_node_container *)node)->when;
1391
0
    case LYS_CHOICE:
1392
0
        return &((struct lysp_node_choice *)node)->when;
1393
0
    case LYS_LEAF:
1394
0
        return &((struct lysp_node_leaf *)node)->when;
1395
0
    case LYS_LEAFLIST:
1396
0
        return &((struct lysp_node_leaflist *)node)->when;
1397
0
    case LYS_LIST:
1398
0
        return &((struct lysp_node_list *)node)->when;
1399
0
    case LYS_ANYXML:
1400
0
    case LYS_ANYDATA:
1401
0
        return &((struct lysp_node_anydata *)node)->when;
1402
0
    case LYS_CASE:
1403
0
        return &((struct lysp_node_case *)node)->when;
1404
0
    case LYS_USES:
1405
0
        return &((struct lysp_node_uses *)node)->when;
1406
0
    case LYS_AUGMENT:
1407
0
        return &((struct lysp_node_augment *)node)->when;
1408
0
    default:
1409
0
        return NULL;
1410
0
    }
1411
0
}
1412
1413
struct lysp_when *
1414
lysp_node_when(const struct lysp_node *node)
1415
0
{
1416
0
    struct lysp_when **when;
1417
1418
0
    when = lysp_node_when_p(node);
1419
0
    if (when) {
1420
0
        return *when;
1421
0
    } else {
1422
0
        return NULL;
1423
0
    }
1424
0
}
1425
1426
struct lysc_node_action **
1427
lysc_node_actions_p(struct lysc_node *node)
1428
0
{
1429
0
    assert(node);
1430
0
    switch (node->nodetype) {
1431
0
    case LYS_CONTAINER:
1432
0
        return &((struct lysc_node_container *)node)->actions;
1433
0
    case LYS_LIST:
1434
0
        return &((struct lysc_node_list *)node)->actions;
1435
0
    default:
1436
0
        return NULL;
1437
0
    }
1438
0
}
1439
1440
API const struct lysc_node_action *
1441
lysc_node_actions(const struct lysc_node *node)
1442
0
{
1443
0
    struct lysc_node_action **actions;
1444
1445
0
    actions = lysc_node_actions_p((struct lysc_node *)node);
1446
0
    if (actions) {
1447
0
        return *actions;
1448
0
    } else {
1449
0
        return NULL;
1450
0
    }
1451
0
}
1452
1453
struct lysc_node_notif **
1454
lysc_node_notifs_p(struct lysc_node *node)
1455
0
{
1456
0
    assert(node);
1457
0
    switch (node->nodetype) {
1458
0
    case LYS_CONTAINER:
1459
0
        return &((struct lysc_node_container *)node)->notifs;
1460
0
    case LYS_LIST:
1461
0
        return &((struct lysc_node_list *)node)->notifs;
1462
0
    default:
1463
0
        return NULL;
1464
0
    }
1465
0
}
1466
1467
API const struct lysc_node_notif *
1468
lysc_node_notifs(const struct lysc_node *node)
1469
0
{
1470
0
    struct lysc_node_notif **notifs;
1471
1472
0
    notifs = lysc_node_notifs_p((struct lysc_node *)node);
1473
0
    if (notifs) {
1474
0
        return *notifs;
1475
0
    } else {
1476
0
        return NULL;
1477
0
    }
1478
0
}
1479
1480
struct lysc_node **
1481
lysc_node_child_p(const struct lysc_node *node)
1482
0
{
1483
0
    assert(node && !(node->nodetype & (LYS_RPC | LYS_ACTION)));
1484
1485
0
    switch (node->nodetype) {
1486
0
    case LYS_CONTAINER:
1487
0
        return &((struct lysc_node_container *)node)->child;
1488
0
    case LYS_CHOICE:
1489
0
        return (struct lysc_node **)&((struct lysc_node_choice *)node)->cases;
1490
0
    case LYS_CASE:
1491
0
        return &((struct lysc_node_case *)node)->child;
1492
0
    case LYS_LIST:
1493
0
        return &((struct lysc_node_list *)node)->child;
1494
0
    case LYS_INPUT:
1495
0
    case LYS_OUTPUT:
1496
0
        return &((struct lysc_node_action_inout *)node)->child;
1497
0
    case LYS_NOTIF:
1498
0
        return &((struct lysc_node_notif *)node)->child;
1499
0
    default:
1500
0
        return NULL;
1501
0
    }
1502
0
}
1503
1504
API const struct lysc_node *
1505
lysc_node_child(const struct lysc_node *node)
1506
0
{
1507
0
    struct lysc_node **child;
1508
1509
0
    if (!node) {
1510
0
        return NULL;
1511
0
    }
1512
1513
0
    if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
1514
0
        return &((struct lysc_node_action *)node)->input.node;
1515
0
    } else {
1516
0
        child = lysc_node_child_p(node);
1517
0
        if (child) {
1518
0
            return *child;
1519
0
        }
1520
0
    }
1521
1522
0
    return NULL;
1523
0
}
1524
1525
struct lysc_must **
1526
lysc_node_musts_p(const struct lysc_node *node)
1527
0
{
1528
0
    if (!node) {
1529
0
        return NULL;
1530
0
    }
1531
1532
0
    switch (node->nodetype) {
1533
0
    case LYS_CONTAINER:
1534
0
        return &((struct lysc_node_container *)node)->musts;
1535
0
    case LYS_LEAF:
1536
0
        return &((struct lysc_node_leaf *)node)->musts;
1537
0
    case LYS_LEAFLIST:
1538
0
        return &((struct lysc_node_leaflist *)node)->musts;
1539
0
    case LYS_LIST:
1540
0
        return &((struct lysc_node_list *)node)->musts;
1541
0
    case LYS_ANYXML:
1542
0
    case LYS_ANYDATA:
1543
0
        return &((struct lysc_node_anydata *)node)->musts;
1544
0
    case LYS_NOTIF:
1545
0
        return &((struct lysc_node_notif *)node)->musts;
1546
0
    case LYS_INPUT:
1547
0
    case LYS_OUTPUT:
1548
0
        return &((struct lysc_node_action_inout *)node)->musts;
1549
0
    default:
1550
0
        return NULL;
1551
0
    }
1552
0
}
1553
1554
API struct lysc_must *
1555
lysc_node_musts(const struct lysc_node *node)
1556
0
{
1557
0
    struct lysc_must **must_p;
1558
1559
0
    must_p = lysc_node_musts_p(node);
1560
0
    if (must_p) {
1561
0
        return *must_p;
1562
0
    } else {
1563
0
        return NULL;
1564
0
    }
1565
0
}
1566
1567
struct lysc_when ***
1568
lysc_node_when_p(const struct lysc_node *node)
1569
0
{
1570
0
    if (!node) {
1571
0
        return NULL;
1572
0
    }
1573
1574
0
    switch (node->nodetype) {
1575
0
    case LYS_CONTAINER:
1576
0
        return &((struct lysc_node_container *)node)->when;
1577
0
    case LYS_CHOICE:
1578
0
        return &((struct lysc_node_choice *)node)->when;
1579
0
    case LYS_LEAF:
1580
0
        return &((struct lysc_node_leaf *)node)->when;
1581
0
    case LYS_LEAFLIST:
1582
0
        return &((struct lysc_node_leaflist *)node)->when;
1583
0
    case LYS_LIST:
1584
0
        return &((struct lysc_node_list *)node)->when;
1585
0
    case LYS_ANYXML:
1586
0
    case LYS_ANYDATA:
1587
0
        return &((struct lysc_node_anydata *)node)->when;
1588
0
    case LYS_CASE:
1589
0
        return &((struct lysc_node_case *)node)->when;
1590
0
    case LYS_NOTIF:
1591
0
        return &((struct lysc_node_notif *)node)->when;
1592
0
    case LYS_RPC:
1593
0
    case LYS_ACTION:
1594
0
        return &((struct lysc_node_action *)node)->when;
1595
0
    default:
1596
0
        return NULL;
1597
0
    }
1598
0
}
1599
1600
API struct lysc_when **
1601
lysc_node_when(const struct lysc_node *node)
1602
0
{
1603
0
    struct lysc_when ***when_p;
1604
1605
0
    when_p = lysc_node_when_p(node);
1606
0
    if (when_p) {
1607
0
        return *when_p;
1608
0
    } else {
1609
0
        return NULL;
1610
0
    }
1611
0
}
1612
1613
struct lys_module *
1614
lysp_find_module(struct ly_ctx *ctx, const struct lysp_module *mod)
1615
0
{
1616
0
    for (uint32_t u = 0; u < ctx->list.count; ++u) {
1617
0
        if (((struct lys_module *)ctx->list.objs[u])->parsed == mod) {
1618
0
            return (struct lys_module *)ctx->list.objs[u];
1619
0
        }
1620
0
    }
1621
0
    return NULL;
1622
0
}
1623
1624
enum ly_stmt
1625
lysp_match_kw(struct ly_in *in, uint64_t *indent)
1626
0
{
1627
/**
1628
 * @brief Move the input by COUNT items. Also updates the indent value in yang parser context
1629
 * @param[in] COUNT number of items for which the DATA pointer is supposed to move on.
1630
 *
1631
 * *INDENT-OFF*
1632
 */
1633
0
#define MOVE_IN(COUNT) \
1634
0
    ly_in_skip(in, COUNT); \
1635
0
    if (indent) { \
1636
0
        (*indent)+=COUNT; \
1637
0
    }
1638
0
#define IF_KW(STR, LEN, STMT) \
1639
0
    if (!strncmp(in->current, STR, LEN)) { \
1640
0
        MOVE_IN(LEN); \
1641
0
        (*kw)=STMT; \
1642
0
    }
1643
0
#define IF_KW_PREFIX(STR, LEN) \
1644
0
    if (!strncmp(in->current, STR, LEN)) { \
1645
0
        MOVE_IN(LEN);
1646
0
#define IF_KW_PREFIX_END \
1647
0
    }
1648
1649
0
    const char *start = in->current;
1650
0
    enum ly_stmt result = LY_STMT_NONE;
1651
0
    enum ly_stmt *kw = &result;
1652
    /* read the keyword itself */
1653
0
    switch (in->current[0]) {
1654
0
    case 'a':
1655
0
        MOVE_IN(1);
1656
0
        IF_KW("rgument", 7, LY_STMT_ARGUMENT)
1657
0
        else IF_KW("ugment", 6, LY_STMT_AUGMENT)
1658
0
        else IF_KW("ction", 5, LY_STMT_ACTION)
1659
0
        else IF_KW_PREFIX("ny", 2)
1660
0
            IF_KW("data", 4, LY_STMT_ANYDATA)
1661
0
            else IF_KW("xml", 3, LY_STMT_ANYXML)
1662
0
        IF_KW_PREFIX_END
1663
0
        break;
1664
0
    case 'b':
1665
0
        MOVE_IN(1);
1666
0
        IF_KW("ase", 3, LY_STMT_BASE)
1667
0
        else IF_KW("elongs-to", 9, LY_STMT_BELONGS_TO)
1668
0
        else IF_KW("it", 2, LY_STMT_BIT)
1669
0
        break;
1670
0
    case 'c':
1671
0
        MOVE_IN(1);
1672
0
        IF_KW("ase", 3, LY_STMT_CASE)
1673
0
        else IF_KW("hoice", 5, LY_STMT_CHOICE)
1674
0
        else IF_KW_PREFIX("on", 2)
1675
0
            IF_KW("fig", 3, LY_STMT_CONFIG)
1676
0
            else IF_KW_PREFIX("ta", 2)
1677
0
                IF_KW("ct", 2, LY_STMT_CONTACT)
1678
0
                else IF_KW("iner", 4, LY_STMT_CONTAINER)
1679
0
            IF_KW_PREFIX_END
1680
0
        IF_KW_PREFIX_END
1681
0
        break;
1682
0
    case 'd':
1683
0
        MOVE_IN(1);
1684
0
        IF_KW_PREFIX("e", 1)
1685
0
            IF_KW("fault", 5, LY_STMT_DEFAULT)
1686
0
            else IF_KW("scription", 9, LY_STMT_DESCRIPTION)
1687
0
            else IF_KW_PREFIX("viat", 4)
1688
0
                IF_KW("e", 1, LY_STMT_DEVIATE)
1689
0
                else IF_KW("ion", 3, LY_STMT_DEVIATION)
1690
0
            IF_KW_PREFIX_END
1691
0
        IF_KW_PREFIX_END
1692
0
        break;
1693
0
    case 'e':
1694
0
        MOVE_IN(1);
1695
0
        IF_KW("num", 3, LY_STMT_ENUM)
1696
0
        else IF_KW_PREFIX("rror-", 5)
1697
0
            IF_KW("app-tag", 7, LY_STMT_ERROR_APP_TAG)
1698
0
            else IF_KW("message", 7, LY_STMT_ERROR_MESSAGE)
1699
0
        IF_KW_PREFIX_END
1700
0
        else IF_KW("xtension", 8, LY_STMT_EXTENSION)
1701
0
        break;
1702
0
    case 'f':
1703
0
        MOVE_IN(1);
1704
0
        IF_KW("eature", 6, LY_STMT_FEATURE)
1705
0
        else IF_KW("raction-digits", 14, LY_STMT_FRACTION_DIGITS)
1706
0
        break;
1707
0
    case 'g':
1708
0
        MOVE_IN(1);
1709
0
        IF_KW("rouping", 7, LY_STMT_GROUPING)
1710
0
        break;
1711
0
    case 'i':
1712
0
        MOVE_IN(1);
1713
0
        IF_KW("dentity", 7, LY_STMT_IDENTITY)
1714
0
        else IF_KW("f-feature", 9, LY_STMT_IF_FEATURE)
1715
0
        else IF_KW("mport", 5, LY_STMT_IMPORT)
1716
0
        else IF_KW_PREFIX("n", 1)
1717
0
            IF_KW("clude", 5, LY_STMT_INCLUDE)
1718
0
            else IF_KW("put", 3, LY_STMT_INPUT)
1719
0
        IF_KW_PREFIX_END
1720
0
        break;
1721
0
    case 'k':
1722
0
        MOVE_IN(1);
1723
0
        IF_KW("ey", 2, LY_STMT_KEY)
1724
0
        break;
1725
0
    case 'l':
1726
0
        MOVE_IN(1);
1727
0
        IF_KW_PREFIX("e", 1)
1728
0
            IF_KW("af-list", 7, LY_STMT_LEAF_LIST)
1729
0
            else IF_KW("af", 2, LY_STMT_LEAF)
1730
0
            else IF_KW("ngth", 4, LY_STMT_LENGTH)
1731
0
        IF_KW_PREFIX_END
1732
0
        else IF_KW("ist", 3, LY_STMT_LIST)
1733
0
        break;
1734
0
    case 'm':
1735
0
        MOVE_IN(1);
1736
0
        IF_KW_PREFIX("a", 1)
1737
0
            IF_KW("ndatory", 7, LY_STMT_MANDATORY)
1738
0
            else IF_KW("x-elements", 10, LY_STMT_MAX_ELEMENTS)
1739
0
        IF_KW_PREFIX_END
1740
0
        else IF_KW("in-elements", 11, LY_STMT_MIN_ELEMENTS)
1741
0
        else IF_KW("ust", 3, LY_STMT_MUST)
1742
0
        else IF_KW_PREFIX("od", 2)
1743
0
            IF_KW("ule", 3, LY_STMT_MODULE)
1744
0
            else IF_KW("ifier", 5, LY_STMT_MODIFIER)
1745
0
        IF_KW_PREFIX_END
1746
0
        break;
1747
0
    case 'n':
1748
0
        MOVE_IN(1);
1749
0
        IF_KW("amespace", 8, LY_STMT_NAMESPACE)
1750
0
        else IF_KW("otification", 11, LY_STMT_NOTIFICATION)
1751
0
        break;
1752
0
    case 'o':
1753
0
        MOVE_IN(1);
1754
0
        IF_KW_PREFIX("r", 1)
1755
0
            IF_KW("dered-by", 8, LY_STMT_ORDERED_BY)
1756
0
            else IF_KW("ganization", 10, LY_STMT_ORGANIZATION)
1757
0
        IF_KW_PREFIX_END
1758
0
        else IF_KW("utput", 5, LY_STMT_OUTPUT)
1759
0
        break;
1760
0
    case 'p':
1761
0
        MOVE_IN(1);
1762
0
        IF_KW("ath", 3, LY_STMT_PATH)
1763
0
        else IF_KW("attern", 6, LY_STMT_PATTERN)
1764
0
        else IF_KW("osition", 7, LY_STMT_POSITION)
1765
0
        else IF_KW_PREFIX("re", 2)
1766
0
            IF_KW("fix", 3, LY_STMT_PREFIX)
1767
0
            else IF_KW("sence", 5, LY_STMT_PRESENCE)
1768
0
        IF_KW_PREFIX_END
1769
0
        break;
1770
0
    case 'r':
1771
0
        MOVE_IN(1);
1772
0
        IF_KW("ange", 4, LY_STMT_RANGE)
1773
0
        else IF_KW_PREFIX("e", 1)
1774
0
            IF_KW_PREFIX("f", 1)
1775
0
                IF_KW("erence", 6, LY_STMT_REFERENCE)
1776
0
                else IF_KW("ine", 3, LY_STMT_REFINE)
1777
0
            IF_KW_PREFIX_END
1778
0
            else IF_KW("quire-instance", 14, LY_STMT_REQUIRE_INSTANCE)
1779
0
            else IF_KW("vision-date", 11, LY_STMT_REVISION_DATE)
1780
0
            else IF_KW("vision", 6, LY_STMT_REVISION)
1781
0
        IF_KW_PREFIX_END
1782
0
        else IF_KW("pc", 2, LY_STMT_RPC)
1783
0
        break;
1784
0
    case 's':
1785
0
        MOVE_IN(1);
1786
0
        IF_KW("tatus", 5, LY_STMT_STATUS)
1787
0
        else IF_KW("ubmodule", 8, LY_STMT_SUBMODULE)
1788
0
        break;
1789
0
    case 't':
1790
0
        MOVE_IN(1);
1791
0
        IF_KW("ypedef", 6, LY_STMT_TYPEDEF)
1792
0
        else IF_KW("ype", 3, LY_STMT_TYPE)
1793
0
        break;
1794
0
    case 'u':
1795
0
        MOVE_IN(1);
1796
0
        IF_KW_PREFIX("ni", 2)
1797
0
            IF_KW("que", 3, LY_STMT_UNIQUE)
1798
0
            else IF_KW("ts", 2, LY_STMT_UNITS)
1799
0
        IF_KW_PREFIX_END
1800
0
        else IF_KW("ses", 3, LY_STMT_USES)
1801
0
        break;
1802
0
    case 'v':
1803
0
        MOVE_IN(1);
1804
0
        IF_KW("alue", 4, LY_STMT_VALUE)
1805
0
        break;
1806
0
    case 'w':
1807
0
        MOVE_IN(1);
1808
0
        IF_KW("hen", 3, LY_STMT_WHEN)
1809
0
        break;
1810
0
    case 'y':
1811
0
        MOVE_IN(1);
1812
0
        IF_KW("ang-version", 11, LY_STMT_YANG_VERSION)
1813
0
        else IF_KW("in-element", 10, LY_STMT_YIN_ELEMENT)
1814
0
        break;
1815
0
    default:
1816
        /* if indent is not NULL we are matching keyword from YANG data */
1817
0
        if (indent) {
1818
0
            if (in->current[0] == ';') {
1819
0
                MOVE_IN(1);
1820
0
                *kw = LY_STMT_SYNTAX_SEMICOLON;
1821
0
            } else if (in->current[0] == '{') {
1822
0
                MOVE_IN(1);
1823
0
                *kw = LY_STMT_SYNTAX_LEFT_BRACE;
1824
0
            } else if (in->current[0] == '}') {
1825
0
                MOVE_IN(1);
1826
0
                *kw = LY_STMT_SYNTAX_RIGHT_BRACE;
1827
0
            }
1828
0
        }
1829
0
        break;
1830
0
    }
1831
1832
0
    if ((*kw < LY_STMT_SYNTAX_SEMICOLON) && isalnum(in->current[0])) {
1833
        /* the keyword is not terminated */
1834
0
        *kw = LY_STMT_NONE;
1835
0
        in->current = start;
1836
0
    }
1837
1838
0
#undef IF_KW
1839
0
#undef IF_KW_PREFIX
1840
0
#undef IF_KW_PREFIX_END
1841
0
#undef MOVE_IN
1842
    /* *INDENT-ON* */
1843
1844
0
    return result;
1845
0
}
1846
1847
LY_ERR
1848
lysp_ext_find_definition(const struct ly_ctx *ctx, const struct lysp_ext_instance *ext, const struct lys_module **ext_mod,
1849
        struct lysp_ext **ext_def)
1850
0
{
1851
0
    const char *tmp, *name, *prefix;
1852
0
    size_t pref_len, name_len;
1853
0
    LY_ARRAY_COUNT_TYPE v;
1854
0
    const struct lys_module *mod = NULL;
1855
1856
0
    assert(ext_def);
1857
1858
0
    *ext_def = NULL;
1859
0
    if (ext_mod) {
1860
0
        *ext_mod = NULL;
1861
0
    }
1862
1863
    /* parse the prefix, the nodeid was previously already parsed and checked */
1864
0
    tmp = ext->name;
1865
0
    ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
1866
1867
    /* get module where the extension definition should be placed */
1868
0
    mod = ly_resolve_prefix(ctx, prefix, pref_len, ext->format, ext->prefix_data);
1869
0
    if (!mod) {
1870
0
        LOGVAL(ctx, LYVE_REFERENCE, "Invalid prefix \"%.*s\" used for extension instance identifier.", (int)pref_len, prefix);
1871
0
        return LY_EVALID;
1872
0
    } else if (!mod->parsed->extensions) {
1873
0
        LOGVAL(ctx, LYVE_REFERENCE, "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
1874
0
                ext->name, mod->name);
1875
0
        return LY_EVALID;
1876
0
    }
1877
1878
    /* find the parsed extension definition there */
1879
0
    LY_ARRAY_FOR(mod->parsed->extensions, v) {
1880
0
        if (!strcmp(name, mod->parsed->extensions[v].name)) {
1881
0
            *ext_def = &mod->parsed->extensions[v];
1882
0
            break;
1883
0
        }
1884
0
    }
1885
1886
0
    if (!(*ext_def)) {
1887
0
        LOGVAL(ctx, LYVE_REFERENCE, "Extension definition of extension instance \"%s\" not found.", ext->name);
1888
0
        return LY_EVALID;
1889
0
    }
1890
1891
0
    if (ext_mod) {
1892
0
        *ext_mod = mod;
1893
0
    }
1894
0
    return LY_SUCCESS;
1895
0
}
1896
1897
LY_ERR
1898
lysp_ext_instance_resolve_argument(struct ly_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysp_ext *ext_def)
1899
0
{
1900
0
    if (!ext_def->argname || ext_p->argument) {
1901
        /* nothing to do */
1902
0
        return LY_SUCCESS;
1903
0
    }
1904
1905
0
    if (ext_p->format == LY_VALUE_XML) {
1906
        /* Schema was parsed from YIN and an argument is expected, ... */
1907
0
        struct lysp_stmt *stmt = NULL;
1908
1909
0
        if (ext_def->flags & LYS_YINELEM_TRUE) {
1910
            /* ... argument was the first XML child element */
1911
0
            for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {}
1912
0
            if (stmt) {
1913
0
                const char *arg, *ext, *name_arg, *name_ext, *prefix_arg, *prefix_ext;
1914
0
                size_t name_arg_len, name_ext_len, prefix_arg_len, prefix_ext_len;
1915
1916
0
                stmt = ext_p->child;
1917
1918
0
                arg = stmt->stmt;
1919
0
                ly_parse_nodeid(&arg, &prefix_arg, &prefix_arg_len, &name_arg, &name_arg_len);
1920
0
                if (ly_strncmp(ext_def->argname, name_arg, name_arg_len)) {
1921
0
                    LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" expects argument element \"%s\" as its first XML child, "
1922
0
                            "but \"%.*s\" element found.", ext_p->name, ext_def->argname, (int)name_arg_len, name_arg);
1923
0
                    return LY_EVALID;
1924
0
                }
1925
1926
                /* check namespace - all the extension instances must be qualified and argument element is expected in the same
1927
                 * namespace. Do not check just prefixes, there can be different prefixes pointing to the same namespace */
1928
0
                ext = ext_p->name; /* include prefix */
1929
0
                ly_parse_nodeid(&ext, &prefix_ext, &prefix_ext_len, &name_ext, &name_ext_len);
1930
1931
0
                if (ly_resolve_prefix(ctx, prefix_ext, prefix_ext_len, ext_p->format, ext_p->prefix_data) !=
1932
0
                        ly_resolve_prefix(ctx, prefix_arg, prefix_arg_len, stmt->format, stmt->prefix_data)) {
1933
0
                    LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" element and its argument element \"%s\" are "
1934
0
                            "expected in the same namespace, but they differ.", ext_p->name, ext_def->argname);
1935
0
                    return LY_EVALID;
1936
0
                }
1937
0
            }
1938
0
        } else {
1939
            /* ... argument was one of the XML attributes which are represented as child stmt
1940
             * with LYS_YIN_ATTR flag */
1941
0
            for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
1942
0
                if (!strcmp(stmt->stmt, ext_def->argname)) {
1943
                    /* this is the extension's argument */
1944
0
                    break;
1945
0
                }
1946
0
            }
1947
0
        }
1948
1949
0
        if (stmt) {
1950
0
            LY_CHECK_RET(lydict_insert(ctx, stmt->arg, 0, &ext_p->argument));
1951
0
            stmt->flags |= LYS_YIN_ARGUMENT;
1952
0
        }
1953
0
    }
1954
1955
0
    if (!ext_p->argument) {
1956
        /* missing extension's argument */
1957
0
        LOGVAL(ctx, LYVE_SEMANTICS, "Extension instance \"%s\" misses argument %s\"%s\".",
1958
0
                ext_p->name, (ext_def->flags & LYS_YINELEM_TRUE) ? "element " : "", ext_def->argname);
1959
0
        return LY_EVALID;
1960
0
    }
1961
1962
0
    return LY_SUCCESS;
1963
0
}
1964
1965
LY_ARRAY_COUNT_TYPE
1966
lysp_ext_instance_iter(struct lysp_ext_instance *ext, LY_ARRAY_COUNT_TYPE index, enum ly_stmt substmt)
1967
0
{
1968
0
    LY_CHECK_ARG_RET(NULL, ext, LY_EINVAL);
1969
1970
0
    for ( ; index < LY_ARRAY_COUNT(ext); index++) {
1971
0
        if (ext[index].parent_stmt == substmt) {
1972
0
            return index;
1973
0
        }
1974
0
    }
1975
1976
0
    return LY_ARRAY_COUNT(ext);
1977
0
}
1978
1979
const struct lysc_node *
1980
lysc_data_node(const struct lysc_node *schema)
1981
0
{
1982
0
    const struct lysc_node *parent;
1983
1984
0
    parent = schema;
1985
0
    while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC |
1986
0
            LYS_ACTION | LYS_NOTIF))) {
1987
0
        parent = parent->parent;
1988
0
    }
1989
1990
0
    return parent;
1991
0
}