libyang5-sys 0.1.0

Raw FFI bindings for libyang5
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/**
 * @file structure.c
 * @author Michal Vasko <mvasko@cesnet.cz>
 * @brief libyang extension plugin - structure (RFC 8791)
 *
 * Copyright (c) 2022 - 2026 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "compat.h"
#include "libyang.h"
#include "ly_common.h"
#include "plugins_exts.h"
#include "tree_data_internal.h"
#include "xpath.h"

struct lysp_ext_instance_structure {
    struct lysp_restr *musts;
    uint16_t flags;
    const char *dsc;
    const char *ref;
    struct lysp_tpdf *typedefs;
    struct lysp_node_grp *groupings;
    struct lysp_node *child;
};

struct lysc_ext_instance_structure {
    struct lysc_node_container *top_cont;
};

struct lysp_ext_instance_augment_structure {
    uint16_t flags;
    const char *dsc;
    const char *ref;
    struct lysp_node *child;
    struct lysp_node_augment *aug;
};

static void structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext);

/**
 * @brief Parse structure extension instances.
 *
 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
 */
static LY_ERR
structure_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
{
    LY_ERR rc;
    LY_ARRAY_COUNT_TYPE u;
    struct lysp_module *pmod;
    struct lysp_ext_instance_structure *struct_pdata;

    /* structure can appear only at the top level of a YANG module or submodule */
    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
                "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name,
                lyplg_ext_stmt2str(ext->parent_stmt));
        return LY_EVALID;
    }

    pmod = ext->parent;

    /* check for duplication */
    LY_ARRAY_FOR(pmod->exts, u) {
        if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
            /* duplication of the same structure extension in a single module */
            lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
            return LY_EVALID;
        }
    }

    /* allocate the storage */
    struct_pdata = calloc(1, sizeof *struct_pdata);
    if (!struct_pdata) {
        goto emem;
    }
    ext->parsed = struct_pdata;
    LY_ARRAY_CREATE_GOTO(lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx, ext->substmts, 14, rc, emem);

    /* parse substatements */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[0].stmt = LY_STMT_MUST;
    ext->substmts[0].storage_p = (void **)&struct_pdata->musts;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[1].stmt = LY_STMT_STATUS;
    ext->substmts[1].storage_p = (void **)&struct_pdata->flags;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
    ext->substmts[2].storage_p = (void **)&struct_pdata->dsc;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[3].stmt = LY_STMT_REFERENCE;
    ext->substmts[3].storage_p = (void **)&struct_pdata->ref;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[4].stmt = LY_STMT_TYPEDEF;
    ext->substmts[4].storage_p = (void **)&struct_pdata->typedefs;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[5].stmt = LY_STMT_GROUPING;
    ext->substmts[5].storage_p = (void **)&struct_pdata->groupings;

    /* data-def-stmt */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[6].stmt = LY_STMT_CONTAINER;
    ext->substmts[6].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[7].stmt = LY_STMT_LEAF;
    ext->substmts[7].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
    ext->substmts[8].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[9].stmt = LY_STMT_LIST;
    ext->substmts[9].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[10].stmt = LY_STMT_CHOICE;
    ext->substmts[10].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[11].stmt = LY_STMT_ANYDATA;
    ext->substmts[11].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[12].stmt = LY_STMT_ANYXML;
    ext->substmts[12].storage_p = (void **)&struct_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[13].stmt = LY_STMT_USES;
    ext->substmts[13].storage_p = (void **)&struct_pdata->child;

    rc = lyplg_ext_parse_extension_instance(pctx, ext);
    return rc;

emem:
    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
    return LY_EMEM;
}

/**
 * @brief Compile structure extension instances.
 *
 * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
 */
static LY_ERR
structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
{
    LY_ERR rc;
    struct lysc_module *mod_c;
    struct lysc_node *child;
    struct lysc_ext_instance_structure *struct_cdata;
    uint32_t prev_options = *lyplg_ext_compile_get_options(cctx);

    mod_c = ext->parent;

    /* check identifier namespace with the compiled nodes */
    LY_LIST_FOR(mod_c->data, child) {
        if (!strcmp(child->name, ext->argument)) {
            /* identifier collision */
            lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,  "Extension %s collides with a %s with the same identifier.",
                    extp->name, lys_nodetype2str(child->nodetype));
            return LY_EVALID;
        }
    }

    /* allocate the storage */
    struct_cdata = calloc(1, sizeof *struct_cdata);
    if (!struct_cdata) {
        goto emem;
    }
    ext->compiled = struct_cdata;

    /* add the top-level container with the extension instance name, connect all the other substatements into it */
    struct_cdata->top_cont = calloc(1, sizeof *struct_cdata->top_cont);
    if (!struct_cdata->top_cont) {
        goto emem;
    }

    struct_cdata->top_cont->name = ext->argument;
    struct_cdata->top_cont->nodetype = LYS_CONTAINER;
    struct_cdata->top_cont->module = (struct lys_module *)lyplg_ext_compile_get_cur_mod(cctx);
    struct_cdata->top_cont->prev = &struct_cdata->top_cont->node;

    /* compile substatements */
    LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 14, rc, emem);
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[0].stmt = LY_STMT_MUST;
    ext->substmts[0].storage_p = (void **)&struct_cdata->top_cont->musts;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[1].stmt = LY_STMT_STATUS;
    ext->substmts[1].storage_p = (void **)&struct_cdata->top_cont->flags;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
    ext->substmts[2].storage_p = (void **)&struct_cdata->top_cont->dsc;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[3].stmt = LY_STMT_REFERENCE;
    ext->substmts[3].storage_p = (void **)&struct_cdata->top_cont->ref;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[4].stmt = LY_STMT_TYPEDEF;
    ext->substmts[4].storage_p = NULL;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[5].stmt = LY_STMT_GROUPING;
    ext->substmts[5].storage_p = NULL;

    /* data-def-stmt */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[6].stmt = LY_STMT_CONTAINER;
    ext->substmts[6].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[7].stmt = LY_STMT_LEAF;
    ext->substmts[7].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
    ext->substmts[8].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[9].stmt = LY_STMT_LIST;
    ext->substmts[9].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[10].stmt = LY_STMT_CHOICE;
    ext->substmts[10].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[11].stmt = LY_STMT_ANYDATA;
    ext->substmts[11].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[12].stmt = LY_STMT_ANYXML;
    ext->substmts[12].storage_p = (void **)&struct_cdata->top_cont->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[13].stmt = LY_STMT_USES;
    ext->substmts[13].storage_p = (void **)&struct_cdata->top_cont->child;

    *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
    rc = lyplg_ext_compile_extension_instance(cctx, extp, ext, (struct lysc_node *)struct_cdata->top_cont);
    *lyplg_ext_compile_get_options(cctx) = prev_options;
    if (rc) {
        return rc;
    }

    /* compile config properly even though it is ignored */
    if (!(struct_cdata->top_cont->flags & LYS_CONFIG_MASK)) {
        struct_cdata->top_cont->flags |= LYS_CONFIG_W;
    }

    /* connect any augments */
    LY_CHECK_RET(lyplg_ext_compiled_node_augments(cctx, ext, (struct lysc_node *)struct_cdata->top_cont));

    return LY_SUCCESS;

emem:
    structure_cfree(lyplg_ext_compile_get_ctx(cctx), ext);
    lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
    return LY_EMEM;
}

/**
 * @brief Structure schema info printer.
 *
 * Implementation of ::lyplg_ext_sprinter_info_clb set as ::lyext_plugin::printer_info
 */
static LY_ERR
structure_printer_info(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
{
    lyplg_ext_print_info_extension_instance(ctx, ext, flag);
    return LY_SUCCESS;
}

/**
 * @brief Free parsed structure extension instance data.
 *
 * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree.
 */
static void
structure_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
{
    lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
    free(ext->parsed);
}

/**
 * @brief Free compiled structure extension instance data.
 *
 * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree.
 */
static void
structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
{
    struct lysc_ext_instance_structure *struct_cdata = ext->compiled;

    lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
    if (struct_cdata) {
        free(struct_cdata->top_cont);
        free(struct_cdata);
    }
}

static int
structure_compiled_size(const struct lysc_ext_instance *ext, struct ly_ht *addr_ht)
{
    struct lysc_ext_instance_structure *struct_cdata;
    int size = 0;

    struct_cdata = ext->compiled;

    size += LY_CTXP_MEM_SIZE(sizeof *struct_cdata);
    size += lyplg_ext_compiled_stmts_storage_size(ext->substmts, addr_ht);
    size += LY_CTXP_MEM_SIZE(sizeof *struct_cdata->top_cont);

    return size;
}

static LY_ERR
structure_compiled_print(const struct lysc_ext_instance *orig_ext, struct lysc_ext_instance *ext, struct ly_ht *addr_ht,
        struct ly_set *ptr_set, void **mem)
{
    LY_ERR r;
    struct lysc_ext_instance_structure *struct_cdata, *orig_cdata;

    orig_cdata = orig_ext->compiled;

    /* ext structure */
    struct_cdata = ext->compiled = *mem;
    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *struct_cdata);
    memset(struct_cdata, 0, sizeof *struct_cdata);

    /* top_cont */
    struct_cdata->top_cont = *mem;
    *mem = (char *)*mem + LY_CTXP_MEM_SIZE(sizeof *struct_cdata->top_cont);
    lyplg_ext_compiled_print_add_addr(addr_ht, orig_cdata->top_cont, struct_cdata->top_cont);
    memset(struct_cdata->top_cont, 0, sizeof *struct_cdata->top_cont);

    /* substatements */
    ext->substmts[0].storage_p = (void **)&struct_cdata->top_cont->musts;
    ext->substmts[1].storage_p = (void **)&struct_cdata->top_cont->flags;
    ext->substmts[2].storage_p = (void **)&struct_cdata->top_cont->dsc;
    ext->substmts[3].storage_p = (void **)&struct_cdata->top_cont->ref;

    ext->substmts[6].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[7].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[8].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[9].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[10].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[11].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[12].storage_p = (void **)&struct_cdata->top_cont->child;
    ext->substmts[13].storage_p = (void **)&struct_cdata->top_cont->child;

    r = lyplg_ext_compiled_stmts_storage_print(orig_ext->substmts, ext->substmts, addr_ht, ptr_set, mem);
    if (r) {
        return r;
    }

    /* top_cont substatements that are now in addr_ht */
    struct_cdata->top_cont->name = lyplg_ext_compiled_print_get_addr(addr_ht, orig_cdata->top_cont->name);
    assert(struct_cdata->top_cont->name);
    struct_cdata->top_cont->nodetype = orig_cdata->top_cont->nodetype;
    struct_cdata->top_cont->module = lyplg_ext_compiled_print_get_addr(addr_ht, orig_cdata->top_cont->module);
    assert(struct_cdata->top_cont->module);
    struct_cdata->top_cont->prev = lyplg_ext_compiled_print_get_addr(addr_ht, orig_cdata->top_cont->prev);
    assert(struct_cdata->top_cont->prev);

    return LY_SUCCESS;
}

/**
 * @brief Parse augment-structure extension instances.
 *
 * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
 */
static LY_ERR
structure_aug_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
{
    LY_ERR rc;
    struct lysp_stmt *stmt;
    struct lysp_ext_instance_augment_structure *aug_pdata;
    const struct ly_ctx *ctx = lyplg_ext_parse_get_cur_pmod(pctx)->mod->ctx;

    /* augment-structure can appear only at the top level of a YANG module or submodule */
    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
                "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name,
                lyplg_ext_stmt2str(ext->parent_stmt));
        return LY_EVALID;
    }

    /* augment-structure must define some data-def-stmt */
    LY_LIST_FOR(ext->child, stmt) {
        if (stmt->kw & (LY_STMT_CONTAINER | LY_STMT_LEAF | LY_STMT_LEAF_LIST | LY_STMT_LIST | LY_STMT_CHOICE |
                LY_STMT_ANYDATA | LY_STMT_ANYXML | LY_STMT_USES)) {
            break;
        }
    }
    if (!stmt) {
        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s does not define any data-def-stmt statements.",
                ext->name);
        return LY_EVALID;
    }

    /* allocate the storage */
    aug_pdata = calloc(1, sizeof *aug_pdata);
    if (!aug_pdata) {
        goto emem;
    }
    ext->parsed = aug_pdata;
    LY_ARRAY_CREATE_GOTO(ctx, ext->substmts, 13, rc, emem);

    /* parse substatements */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[0].stmt = LY_STMT_STATUS;
    ext->substmts[0].storage_p = (void **)&aug_pdata->flags;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[1].stmt = LY_STMT_DESCRIPTION;
    ext->substmts[1].storage_p = (void **)&aug_pdata->dsc;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[2].stmt = LY_STMT_REFERENCE;
    ext->substmts[2].storage_p = (void **)&aug_pdata->ref;

    /* data-def-stmt */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[3].stmt = LY_STMT_CONTAINER;
    ext->substmts[3].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[4].stmt = LY_STMT_LEAF;
    ext->substmts[4].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[5].stmt = LY_STMT_LEAF_LIST;
    ext->substmts[5].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[6].stmt = LY_STMT_LIST;
    ext->substmts[6].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[7].stmt = LY_STMT_CHOICE;
    ext->substmts[7].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[8].stmt = LY_STMT_ANYDATA;
    ext->substmts[8].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[9].stmt = LY_STMT_ANYXML;
    ext->substmts[9].storage_p = (void **)&aug_pdata->child;

    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[10].stmt = LY_STMT_USES;
    ext->substmts[10].storage_p = (void **)&aug_pdata->child;

    /* case */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[11].stmt = LY_STMT_CASE;
    ext->substmts[11].storage_p = (void **)&aug_pdata->child;

    if ((rc = lyplg_ext_parse_extension_instance(pctx, ext))) {
        return rc;
    }

    /* add fake parsed augment node */
    LY_ARRAY_INCREMENT(ext->substmts);
    ext->substmts[12].stmt = LY_STMT_AUGMENT;
    ext->substmts[12].storage_p = (void **)&aug_pdata->aug;

    aug_pdata->aug = calloc(1, sizeof *aug_pdata->aug);
    if (!aug_pdata->aug) {
        goto emem;
    }
    aug_pdata->aug->nodetype = LYS_AUGMENT;
    aug_pdata->aug->flags = aug_pdata->flags;
    if (lysdict_insert(ctx, ext->argument, 0, &aug_pdata->aug->nodeid)) {
        goto emem;
    }
    aug_pdata->aug->child = aug_pdata->child;
    /* avoid double free */
    aug_pdata->child = NULL;

    return LY_SUCCESS;

emem:
    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
    return LY_EMEM;
}

/**
 * @brief Node xpath callback for structure.
 */
static void
structure_node_xpath(struct lysc_ext_instance *ext, const struct lyd_node *cur_node, const struct lyd_node **node)
{
    *node = NULL;

    if (!cur_node) {
        return;
    }

    while (cur_node && !(cur_node->flags & LYD_EXT)) {
        cur_node = cur_node->parent;
    }
    assert(cur_node);

    /* virtual top-level container expected */
    assert(!strcmp(ext->argument, LYD_NAME(cur_node)));
    (void)ext;

    /* return the child */
    *node = lyd_child(cur_node);
}

/**
 * @brief Snode xpath callback for structure.
 */
static LY_ERR
structure_snode_xpath(struct lysc_ext_instance *ext, const char *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format,
        void *prefix_data, const char *name, uint32_t name_len, const struct lysc_node **snode)
{
    struct lysc_ext_instance_structure *struct_cdata = ext->compiled;
    const struct lysc_node *schema = NULL;
    const struct lys_module *mod;

    if (prefix && prefix_len) {
        /* check module */
        mod = lys_find_module(ext->module->ctx, NULL, prefix, prefix_len, format, prefix_data);
        if (!mod || (ext->module != mod)) {
            return LY_ENOT;
        }
    }

    /* find the schema node in the substatements */
    while ((schema = lys_getnext(schema, &struct_cdata->top_cont->node, NULL, 0))) {
        if (!ly_strncmp(schema->name, name, name_len)) {
            *snode = schema;
            return LY_SUCCESS;
        }
    }

    return LY_ENOT;
}

/**
 * @brief Snode callback for structure.
 */
static LY_ERR
structure_snode(struct lysc_ext_instance *ext, const struct lyd_node *parent, const struct lysc_node *sparent,
        const char *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name,
        uint32_t name_len, const struct lysc_node **snode)
{
    struct lysc_ext_instance_structure *struct_cdata = ext->compiled;
    const struct lys_module *mod;

    /* may be anywhere in the data. in top-level or nested */
    (void)parent;
    (void)sparent;

    if (prefix && prefix_len) {
        /* check module */
        mod = lys_find_module(ext->module->ctx, NULL, prefix, prefix_len, format, prefix_data);
        if (!mod || (ext->module != mod)) {
            return LY_ENOT;
        }
    }

    /* check name */
    if (name && name_len) {
        if (ly_strncmp(struct_cdata->top_cont->name, name, name_len)) {
            return LY_ENOT;
        }
    }

    /* data tree start at the top-level virtual container */
    *snode = &struct_cdata->top_cont->node;

    return LY_SUCCESS;
}

/**
 * @brief Validate callback for structure.
 */
static LY_ERR
structure_validate(struct lysc_ext_instance *ext, struct lyd_node *node, const struct lyd_node *UNUSED(dep_tree),
        enum lyd_type data_type, uint32_t val_opts, struct lyd_node **diff)
{
    if (data_type != LYD_TYPE_DATA_YANG) {
        /* not supported */
        return LY_ENOT;
    }

    /* validate all the modules with data */
    return lyd_validate_ext(&node, ext, val_opts, diff);
}

/**
 * @brief Plugin descriptions for the structure extension
 *
 * Note that external plugins are supposed to use:
 *
 *   LYPLG_EXTENSIONS = {
 */
const struct lyplg_ext_record plugins_structure[] = {
    {
        .module = "ietf-yang-structure-ext",
        .revision = "2020-06-17",
        .name = "structure",

        .plugin.id = "ly2 structure",
        .plugin.parse = structure_parse,
        .plugin.compile = structure_compile,
        .plugin.printer_info = structure_printer_info,
        .plugin.node_xpath = structure_node_xpath,
        .plugin.snode_xpath = structure_snode_xpath,
        .plugin.snode = structure_snode,
        .plugin.validate = structure_validate,
        .plugin.pfree = structure_pfree,
        .plugin.cfree = structure_cfree,
        .plugin.compiled_size = structure_compiled_size,
        .plugin.compiled_print = structure_compiled_print
    },
    {
        .module = "ietf-yang-structure-ext",
        .revision = "2020-06-17",
        .name = "augment-structure",

        .plugin.id = "ly2 structure",
        .plugin.parse = structure_aug_parse,
        .plugin.compile = NULL,
        .plugin.printer_info = NULL,
        .plugin.node_xpath = NULL,
        .plugin.snode_xpath = NULL,
        .plugin.snode = NULL,
        .plugin.validate = NULL,
        .plugin.pfree = structure_pfree,
        .plugin.cfree = NULL,
        .plugin.compiled_size = NULL,
        .plugin.compiled_print = NULL
    },
    {0}     /* terminating zeroed record */
};