forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_schema.c
More file actions
3172 lines (2773 loc) · 106 KB
/
tree_schema.c
File metadata and controls
3172 lines (2773 loc) · 106 KB
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file tree_schema.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief Schema tree implementation
*
* Copyright (c) 2015 - 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
*/
#define _GNU_SOURCE /* asprintf, strdup */
#include "tree_schema.h"
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "compat.h"
#include "context.h"
#include "dict.h"
#include "in.h"
#include "in_internal.h"
#include "log.h"
#include "ly_common.h"
#include "parser_internal.h"
#include "parser_schema.h"
#include "path.h"
#include "plugins_exts.h"
#include "plugins_internal.h"
#include "schema_compile.h"
#include "schema_compile_amend.h"
#include "schema_features.h"
#include "set.h"
#include "tree.h"
#include "tree_edit.h"
#include "tree_schema_free.h"
#include "tree_schema_internal.h"
#include "xml.h"
#include "xpath.h"
const char * const ly_devmod_list[] = {
[LYS_DEV_NOT_SUPPORTED] = "not-supported",
[LYS_DEV_ADD] = "add",
[LYS_DEV_DELETE] = "delete",
[LYS_DEV_REPLACE] = "replace",
};
LIBYANG_API_DEF LY_ERR
lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
{
struct lysc_node *elem, *elem2;
const struct lysc_node_action *action;
const struct lysc_node_notif *notif;
LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
LYSC_TREE_DFS_BEGIN(root, elem) {
/* schema node */
LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
LY_LIST_FOR(lysc_node_actions(elem), action) {
LYSC_TREE_DFS_BEGIN(action, elem2) {
/* action subtree */
LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
LYSC_TREE_DFS_END(action, elem2);
}
}
LY_LIST_FOR(lysc_node_notifs(elem), notif) {
LYSC_TREE_DFS_BEGIN(notif, elem2) {
/* notification subtree */
LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
LYSC_TREE_DFS_END(notif, elem2);
}
}
LYSC_TREE_DFS_END(root, elem);
}
return LY_SUCCESS;
}
LIBYANG_API_DEF LY_ERR
lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
{
const struct lysc_node *root;
LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
/* schema nodes */
LY_LIST_FOR(mod->compiled->data, root) {
LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
}
/* RPCs */
LY_LIST_FOR((const struct lysc_node *)mod->compiled->rpcs, root) {
LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
}
/* notifications */
LY_LIST_FOR((const struct lysc_node *)mod->compiled->notifs, root) {
LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
}
return LY_SUCCESS;
}
/**
* @brief Find import prefix in imports.
*/
static const struct lys_module *
ly_schema_resolve_prefix(const struct ly_ctx *UNUSED(ctx), const char *prefix, uint32_t prefix_len, const void *prefix_data)
{
const struct lysp_module *prefix_mod = prefix_data;
LY_ARRAY_COUNT_TYPE u;
const char *local_prefix;
local_prefix = prefix_mod->is_submod ? ((struct lysp_submodule *)prefix_mod)->prefix : prefix_mod->mod->prefix;
if (!prefix_len || !ly_strncmp(local_prefix, prefix, prefix_len)) {
/* it is the prefix of the module itself */
return prefix_mod->mod;
}
/* search in imports */
LY_ARRAY_FOR(prefix_mod->imports, u) {
if (!ly_strncmp(prefix_mod->imports[u].prefix, prefix, prefix_len)) {
return prefix_mod->imports[u].module;
}
}
return NULL;
}
/**
* @brief Find resolved module for a prefix in prefix - module pairs.
*/
static const struct lys_module *
ly_schema_resolved_resolve_prefix(const struct ly_ctx *UNUSED(ctx), const char *prefix, uint32_t prefix_len,
const void *prefix_data)
{
const struct lysc_prefix *prefixes = prefix_data;
LY_ARRAY_COUNT_TYPE u;
LY_ARRAY_FOR(prefixes, u) {
if ((!prefixes[u].prefix && !prefix_len) || (prefixes[u].prefix && !ly_strncmp(prefixes[u].prefix, prefix, prefix_len))) {
return prefixes[u].mod;
}
}
return NULL;
}
/**
* @brief Find XML namespace prefix in XML namespaces, which are then mapped to modules.
*/
static const struct lys_module *
ly_xml_resolve_prefix(const struct ly_ctx *ctx, const char *prefix, uint32_t prefix_len, const void *prefix_data)
{
const struct lys_module *mod;
const struct lyxml_ns *ns;
const struct ly_set *ns_set = prefix_data;
ns = lyxml_ns_get(ns_set, prefix, prefix_len);
if (!ns) {
return NULL;
}
mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
if (!mod) {
/* for YIN extension prefix resolution */
mod = ly_ctx_get_module_latest_ns(ctx, ns->uri);
}
return mod;
}
/**
* @brief Find module name.
*/
static const struct lys_module *
ly_json_resolve_prefix(const struct ly_ctx *ctx, const char *prefix, uint32_t prefix_len, const void *UNUSED(prefix_data))
{
return ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
}
const struct lys_module *
ly_resolve_prefix(const struct ly_ctx *ctx, const void *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format,
const void *prefix_data)
{
const struct lys_module *mod = NULL;
LY_CHECK_ARG_RET(ctx, prefix, prefix_len, NULL);
switch (format) {
case LY_VALUE_SCHEMA:
mod = ly_schema_resolve_prefix(ctx, prefix, prefix_len, prefix_data);
break;
case LY_VALUE_SCHEMA_RESOLVED:
mod = ly_schema_resolved_resolve_prefix(ctx, prefix, prefix_len, prefix_data);
break;
case LY_VALUE_XML:
case LY_VALUE_STR_NS:
mod = ly_xml_resolve_prefix(ctx, prefix, prefix_len, prefix_data);
break;
case LY_VALUE_CANON:
case LY_VALUE_JSON:
case LY_VALUE_LYB:
mod = ly_json_resolve_prefix(ctx, prefix, prefix_len, prefix_data);
break;
}
return mod;
}
LIBYANG_API_DEF const struct lys_module *
lys_find_module(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *prefix, uint32_t prefix_len,
LY_VALUE_FORMAT format, const void *prefix_data)
{
if (prefix_len) {
return ly_resolve_prefix(ctx, prefix, prefix_len, format, prefix_data);
} else {
switch (format) {
case LY_VALUE_SCHEMA:
/* use local module */
return ly_schema_resolve_prefix(ctx, prefix, prefix_len, prefix_data);
case LY_VALUE_SCHEMA_RESOLVED:
/* use local module */
return ly_schema_resolved_resolve_prefix(ctx, prefix, prefix_len, prefix_data);
case LY_VALUE_CANON:
case LY_VALUE_JSON:
case LY_VALUE_LYB:
case LY_VALUE_STR_NS:
/* use context node module (as specified) */
return ctx_node ? ctx_node->module : NULL;
case LY_VALUE_XML:
/* use the default namespace */
return ly_xml_resolve_prefix(ctx, NULL, 0, prefix_data);
}
}
return NULL;
}
static void
lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
{
for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
if (first_case->child) {
/* there is something to return */
(*next) = first_case->child;
return;
}
}
/* no children in choice's cases, so go to the choice's sibling instead of into it */
(*last) = (*next);
(*next) = (*next)->next;
}
LIBYANG_API_DEF const struct lysc_node *
lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
{
const struct lysc_node *next = NULL;
ly_bool action_flag = 0, notif_flag = 0;
LY_CHECK_ARG_RET(NULL, last || parent || module, NULL);
next:
if (!last) {
/* first call */
/* learn where to start */
if (parent) {
/* schema subtree */
next = last = lysc_node_child(parent);
} else {
/* top level data */
next = last = module->data;
}
if (!next) {
/* try to get action or notification */
goto repeat;
}
/* test if the next can be returned */
goto check;
} else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
action_flag = 1;
next = last->next;
} else if (last->nodetype == LYS_NOTIF) {
action_flag = notif_flag = 1;
next = last->next;
} else {
next = last->next;
}
repeat:
if (!next) {
if (last && (last->parent != parent)) {
/* go back to parent */
last = last->parent;
goto next;
} else if (!action_flag) {
action_flag = 1;
if (parent) {
next = (struct lysc_node *)lysc_node_actions(parent);
} else if (module) {
next = (struct lysc_node *)module->rpcs;
}
} else if (!notif_flag) {
notif_flag = 1;
if (parent) {
next = (struct lysc_node *)lysc_node_notifs(parent);
} else if (module) {
next = (struct lysc_node *)module->notifs;
}
} else {
return NULL;
}
goto repeat;
}
check:
switch (next->nodetype) {
case LYS_RPC:
case LYS_ACTION:
case LYS_NOTIF:
case LYS_LEAF:
case LYS_ANYXML:
case LYS_ANYDATA:
case LYS_LIST:
case LYS_LEAFLIST:
break;
case LYS_CASE:
if (options & LYS_GETNEXT_WITHCASE) {
break;
} else {
/* go into */
lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
}
goto repeat;
case LYS_CONTAINER:
if (!(next->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
if (lysc_node_child(next)) {
/* go into */
next = lysc_node_child(next);
} else {
last = next;
next = next->next;
}
goto repeat;
}
break;
case LYS_CHOICE:
if (options & LYS_GETNEXT_WITHCHOICE) {
break;
} else if ((options & LYS_GETNEXT_NOCHOICE) || !lysc_node_child(next)) {
next = next->next;
} else {
if (options & LYS_GETNEXT_WITHCASE) {
next = lysc_node_child(next);
} else {
/* go into */
lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
}
}
goto repeat;
case LYS_INPUT:
if (options & LYS_GETNEXT_OUTPUT) {
/* skip */
next = next->next;
} else {
/* go into */
next = lysc_node_child(next);
}
goto repeat;
case LYS_OUTPUT:
if (!(options & LYS_GETNEXT_OUTPUT)) {
/* skip */
next = next->next;
} else {
/* go into */
next = lysc_node_child(next);
}
goto repeat;
default:
/* we should not be here */
LOGINT(NULL);
return NULL;
}
return next;
}
/**
* @brief Get schema node in extension instance according to the given parameters.
*
* @param[in] ext Extension instance which top-level schema node is being searched.
* @param[in] parent Parsed parent data node.
* @param[in] sparent Schema parent node.
* @param[in] module Optional parameter to match the extension instance's (and its data) module.
* @param[in] name Name of the schema node to find, if the string is not NULL-terminated, the @p name_len must be set.
* @param[in] name_len Length of the @p name string, use in case the @p name is not NULL-terminated string.
* @param[in] nodetype Allowed [type of the node](@ref schemanodetypes).
* @param[in] is_xpath Set if searching for nodes in an XPath expression.
* @param[out] snode Found schema node, NULL if no suitable was found.
* @return Found schema node if there is some satisfy the provided requirements.
*/
static LY_ERR
lys_ext_find_node(const 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, ly_bool is_xpath, const struct lysc_node **snode)
{
LY_ERR rc = LY_SUCCESS;
struct lyplg_ext *plg_ext;
const struct lys_module *mod;
const struct lysc_node *node;
plg_ext = LYSC_GET_EXT_PLG(ext->def->plugin_ref);
if (!plg_ext) {
return LY_ENOT;
}
/* standard nodes */
if (is_xpath && plg_ext->snode_xpath) {
rc = plg_ext->snode_xpath((struct lysc_ext_instance *)ext, prefix, prefix_len, format, prefix_data, name,
name_len, snode);
} else if (!is_xpath && plg_ext->snode) {
rc = plg_ext->snode((struct lysc_ext_instance *)ext, parent, sparent, prefix, prefix_len, format,
prefix_data, name, name_len, snode);
} else {
lyplg_ext_get_storage(ext, LY_STMT_DATA_NODE_MASK, sizeof node, (const void **)&node);
if (node) {
/* find the module */
mod = lys_find_module((*snode)->module->ctx, parent ? parent->schema : sparent, prefix, prefix_len, format,
prefix_data);
}
if (node && mod && mod->implemented) {
while ((node = lys_getnext(node, node->parent, NULL, 0))) {
if (node->module != mod) {
continue;
}
if (ly_strncmp(node->name, name, name_len)) {
continue;
}
break;
}
if (node) {
/* matching node */
*snode = node;
}
}
if (!*snode) {
rc = LY_ENOT;
}
}
return rc;
}
LY_ERR
lys_find_child_node_ext(const struct ly_ctx *ctx, const struct lys_module *mod, 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, ly_bool is_xpath, const struct lysc_node **snode,
struct lysc_ext_instance **ext)
{
LY_ERR r;
LY_ARRAY_COUNT_TYPE u;
struct lysc_ext_instance *exts;
*snode = NULL;
if (ext) {
*ext = NULL;
}
/* check if there are any nested parent extension instances */
if (parent && parent->schema) {
exts = parent->schema->exts;
} else if (sparent) {
exts = sparent->exts;
} else {
exts = NULL;
}
LY_ARRAY_FOR(exts, u) {
r = lys_ext_find_node(&exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len,
is_xpath, snode);
if (!r) {
if (ext) {
/* schema node found, remember the ext instance */
*ext = &exts[u];
}
return LY_SUCCESS;
} else if (r != LY_ENOT) {
return r;
}
/* data was not from this ext instance, continue */
}
/* check if there are global module ext instances */
if (!mod) {
mod = lys_find_module(ctx, parent ? parent->schema : sparent, prefix, prefix_len, format, prefix_data);
}
if (mod && mod->implemented) {
exts = mod->compiled->exts;
} else {
exts = NULL;
}
LY_ARRAY_FOR(exts, u) {
r = lys_ext_find_node(&exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len,
is_xpath, snode);
if (!r) {
if (ext) {
*ext = &exts[u];
}
return LY_SUCCESS;
} else if (r != LY_ENOT) {
return r;
}
}
/* no extensions or none matched */
return LY_ENOT;
}
LY_ERR
lys_find_child_node(const struct ly_ctx *ctx, const struct lysc_node *parent, const struct lys_module *mod,
const char *prefix, uint32_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name,
uint32_t name_len, uint32_t options, const struct lysc_node **snode, struct lysc_ext_instance **ext)
{
const struct lysc_node *node = NULL;
assert(name);
*snode = NULL;
if (ext) {
*ext = NULL;
}
if (prefix && !prefix_len) {
prefix_len = strlen(prefix);
}
if (!name_len) {
name_len = strlen(name);
}
/* find the module */
if (!mod) {
mod = lys_find_module(ctx, parent, prefix, prefix_len, format, prefix_data);
}
/* look for a standard schema node */
if (mod && mod->implemented) {
while ((node = lys_getnext(node, parent, mod->compiled, options))) {
/* check module */
if (node->module != mod) {
continue;
}
/* check name */
if (!ly_strncmp(node->name, name, name_len)) {
*snode = node;
return LY_SUCCESS;
}
}
}
/* look for an ext instance node */
return lys_find_child_node_ext(ctx, mod, NULL, parent, prefix, prefix_len, format, prefix_data, name, name_len,
0, snode, ext);
}
LIBYANG_API_DEF const struct lysc_node *
lys_find_child(const struct ly_ctx *ctx, const struct lysc_node *parent, const struct lys_module *mod,
const char *mod_name, uint32_t mod_len, const char *name, uint32_t name_len, uint32_t options)
{
const struct lysc_node *snode = NULL;
LY_CHECK_ARG_RET(NULL, ctx || parent || mod, (mod || mod_name) && (!mod || !mod_name), name, NULL);
if (parent) {
ctx = parent->module->ctx;
} else if (mod) {
ctx = mod->ctx;
}
if (mod) {
mod_name = mod->name;
}
if (mod_name && !mod_len) {
mod_len = strlen(mod_name);
}
if (!name_len) {
name_len = strlen(name);
}
lys_find_child_node(ctx, parent, mod, mod_name, mod_len, LY_VALUE_JSON, NULL, name, name_len, options, &snode, NULL);
return snode;
}
LIBYANG_API_DEF LY_ERR
lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
struct ly_set **set)
{
LY_ERR ret = LY_SUCCESS;
struct lyxp_set xp_set = {0};
struct lyxp_expr *exp = NULL;
uint32_t i;
LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(__func__, ctx, ctx_node ? ctx_node->module->ctx : NULL, LY_EINVAL);
if (!(options & LYXP_SCNODE_ALL)) {
options |= LYXP_SCNODE;
}
if (!ctx) {
ctx = ctx_node->module->ctx;
}
/* allocate return set */
ret = ly_set_new(set);
LY_CHECK_GOTO(ret, cleanup);
/* compile expression */
ret = lyxp_expr_parse(ctx, NULL, xpath, 0, 1, &exp);
LY_CHECK_GOTO(ret, cleanup);
/* atomize expression */
ret = lyxp_atomize(ctx, exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, options);
LY_CHECK_GOTO(ret, cleanup);
/* transform into ly_set */
(*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
(*set)->size = xp_set.used;
for (i = 0; i < xp_set.used; ++i) {
if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
cleanup:
lyxp_set_free_content(&xp_set);
lyxp_expr_free(exp);
return ret;
}
LIBYANG_API_DEF LY_ERR
lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
{
LY_ERR ret = LY_SUCCESS;
struct lyxp_set xp_set = {0};
uint32_t i;
LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(__func__, ctx_node ? ctx_node->module->ctx : NULL, cur_mod->ctx, LY_EINVAL);
if (!(options & LYXP_SCNODE_ALL)) {
options = LYXP_SCNODE;
}
/* allocate return set */
ret = ly_set_new(set);
LY_CHECK_GOTO(ret, cleanup);
/* atomize expression */
ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_VALUE_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, ctx_node,
&xp_set, options);
LY_CHECK_GOTO(ret, cleanup);
/* transform into ly_set */
(*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
(*set)->size = xp_set.used;
for (i = 0; i < xp_set.used; ++i) {
if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM_NODE)) {
assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) ||
(xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL) ||
(xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
cleanup:
lyxp_set_free_content(&xp_set);
if (ret) {
ly_set_free(*set, NULL);
*set = NULL;
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
struct ly_set **set)
{
LY_ERR ret = LY_SUCCESS;
struct lyxp_set xp_set = {0};
struct lyxp_expr *exp = NULL;
uint32_t i;
LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(__func__, ctx, ctx_node ? ctx_node->module->ctx : NULL, LY_EINVAL);
if (!(options & LYXP_SCNODE_ALL)) {
options |= LYXP_SCNODE;
}
if (!ctx) {
ctx = ctx_node->module->ctx;
}
/* allocate return set */
ret = ly_set_new(set);
LY_CHECK_GOTO(ret, cleanup);
/* compile expression */
ret = lyxp_expr_parse(ctx, NULL, xpath, 0, 1, &exp);
LY_CHECK_GOTO(ret, cleanup);
/* atomize expression */
ret = lyxp_atomize(ctx, exp, NULL, LY_VALUE_JSON, NULL, ctx_node, ctx_node, &xp_set, options);
LY_CHECK_GOTO(ret, cleanup);
/* transform into ly_set */
(*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
(*set)->size = xp_set.used;
for (i = 0; i < xp_set.used; ++i) {
if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
cleanup:
lyxp_set_free_content(&xp_set);
lyxp_expr_free(exp);
if (ret) {
ly_set_free(*set, NULL);
*set = NULL;
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
{
LY_ERR ret = LY_SUCCESS;
LY_ARRAY_COUNT_TYPE u, v;
LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
/* allocate return set */
LY_CHECK_RET(ly_set_new(set));
LY_ARRAY_FOR(path, u) {
/* add nodes from the path */
LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
LY_ARRAY_FOR(path[u].predicates, v) {
if ((path[u].predicates[v].type == LY_PATH_PREDTYPE_LIST) || (path[u].predicates[v].type == LY_PATH_PREDTYPE_LIST_VAR)) {
/* add all the keys in a predicate */
LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
}
}
}
cleanup:
if (ret) {
ly_set_free(*set, NULL);
*set = NULL;
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
struct ly_set **set)
{
LY_ERR ret = LY_SUCCESS;
uint8_t oper;
struct lyxp_expr *expr = NULL;
struct ly_path *p = NULL;
LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(__func__, ctx, ctx_node ? ctx_node->module->ctx : NULL, LY_EINVAL);
if (!ctx) {
ctx = ctx_node->module->ctx;
}
/* parse */
ret = ly_path_parse(ctx, ctx_node, path, 0, 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST,
LY_PATH_PRED_SIMPLE, &expr);
LY_CHECK_GOTO(ret, cleanup);
/* compile */
oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
ret = ly_path_compile(ctx, ctx_node, expr, oper, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p);
LY_CHECK_GOTO(ret, cleanup);
/* resolve */
ret = lys_find_lypath_atoms(p, set);
cleanup:
ly_path_free(p);
lyxp_expr_free(expr);
return ret;
}
LIBYANG_API_DEF const struct lysc_node *
lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
{
const struct lysc_node *snode = NULL;
struct lyxp_expr *expr = NULL;
struct ly_path *p = NULL;
LY_ERR ret;
uint8_t oper;
LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, NULL);
LY_CHECK_CTX_EQUAL_RET(__func__, ctx, ctx_node ? ctx_node->module->ctx : NULL, NULL);
if (!ctx) {
ctx = ctx_node->module->ctx;
}
/* parse */
ret = ly_path_parse(ctx, ctx_node, path, 0, 0, LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_FIRST,
LY_PATH_PRED_SIMPLE, &expr);
LY_CHECK_GOTO(ret, cleanup);
/* compile */
oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
ret = ly_path_compile(ctx, ctx_node, expr, oper, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p);
LY_CHECK_GOTO(ret, cleanup);
/* get last node */
snode = p[LY_ARRAY_COUNT(p) - 1].node;
cleanup:
ly_path_free(p);
lyxp_expr_free(expr);
return snode;
}
char *
lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
size_t buflen)
{
const struct lysc_node *iter, *par, *key;
char *path = NULL;
int len = 0;
ly_bool skip_schema;
if (buffer) {
LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
buffer[0] = '\0';
}
if ((pathtype == LYSC_PATH_DATA) || (pathtype == LYSC_PATH_DATA_PATTERN)) {
/* skip schema-only nodes */
skip_schema = 1;
} else {
skip_schema = 0;
}
for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
char *s;
const char *slash;
if (skip_schema && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) {
/* schema-only node */
continue;
}
if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) {
char *predicates = NULL;
key = NULL;
while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) {
s = predicates;
/* print key predicate */
if (asprintf(&predicates, "%s[%s='%%s']", s ? s : "", key->name) == -1) {
free(s);
free(path);
return NULL;
}
free(s);
}
s = buffer ? strdup(buffer) : path;
if (buffer) {
len = snprintf(buffer, buflen, "%s%s", predicates ? predicates : "", s ? s : "");
} else {
len = asprintf(&path, "%s%s", predicates ? predicates : "", s ? s : "");
}
free(predicates);
free(s);
if (buffer && (buflen <= (size_t)len)) {
/* not enough space in buffer */
break;
}
}
s = buffer ? strdup(buffer) : path;
if (parent && (iter->parent == parent)) {
slash = "";
} else {
slash = "/";
}
if (skip_schema) {
par = lysc_data_parent(iter);
} else {
par = iter->parent;
}
if (!par || (par->module != iter->module)) {
/* print prefix */
if (buffer) {
len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, iter->name, s ? s : "");
} else {
len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, iter->name, s ? s : "");
}
} else {
/* prefix is the same as in parent */
if (buffer) {
len = snprintf(buffer, buflen, "%s%s%s", slash, iter->name, s ? s : "");
} else {
len = asprintf(&path, "%s%s%s", slash, iter->name, s ? s : "");
}
}
free(s);
if (buffer && (buflen <= (size_t)len)) {
/* not enough space in buffer */
break;
}
}
if (len < 0) {
free(path);
path = NULL;
} else if (len == 0) {
if (buffer) {
strcpy(buffer, "/");
} else {
free(path);
path = strdup("/");
}
}
if (buffer) {
return buffer;
} else {
return path;
}
}
LIBYANG_API_DEF char *
lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
{
return lysc_path_until(node, NULL, pathtype, buffer, buflen);
}
LY_ERR
_lys_set_implemented(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
{
LY_ERR ret = LY_SUCCESS, r;
struct lys_module *mod_iter;
const char **imp_f, *all_f[] = {"*", NULL};
uint32_t i;
if (mod->implemented) {
/* mod is already implemented, set the features */
r = lys_set_features(mod->parsed, features);
if (r == LY_EEXIST) {
/* no changes */
return LY_SUCCESS;
} else if (!r) {
/* mark the module as changed */
mod->to_compile = 1;