blob: 7ca8616435bb8f805f6d97cbca211d9e4e376135 [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
2 * @file path.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Path functions
5 *
6 * Copyright (c) 2020 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#define _ISOC99_SOURCE /* strtoull */
15
16#include "path.h"
17
18#include <assert.h>
19#include <ctype.h>
20#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020021#include <string.h>
Michal Vasko004d3152020-06-11 19:59:22 +020022
23#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020024#include "compat.h"
Michal Vasko004d3152020-06-11 19:59:22 +020025#include "log.h"
26#include "plugins_types.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020027#include "set.h"
Michal Vasko4c583e82020-07-17 12:16:14 +020028#include "tree.h"
Michal Vasko004d3152020-06-11 19:59:22 +020029#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010030#include "tree_edit.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020031#include "tree_schema.h"
Michal Vasko004d3152020-06-11 19:59:22 +020032#include "tree_schema_internal.h"
33#include "xpath.h"
34
Radek Krejcic0c66412020-08-21 13:53:50 +020035#define LOGVAL_P(CTX, CUR_NODE, CODE, ...) ly_vlog(CTX, (CUR_NODE) ? LY_VLOG_LYSC : LY_VLOG_NONE, CUR_NODE, CODE, ##__VA_ARGS__)
Michal Vasko6b26e742020-07-17 15:02:10 +020036
Michal Vasko004d3152020-06-11 19:59:22 +020037/**
38 * @brief Check predicate syntax.
39 *
40 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +020041 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +020042 * @param[in] exp Parsed predicate.
43 * @param[in,out] tok_idx Index in @p exp, is adjusted.
44 * @param[in] prefix Prefix option.
45 * @param[in] pred Predicate option.
46 * @return LY_ERR value.
47 */
48static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +020049ly_path_check_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lyxp_expr *exp,
Radek Krejci0f969882020-08-21 16:56:47 +020050 uint16_t *tok_idx, uint8_t prefix, uint8_t pred)
Michal Vasko004d3152020-06-11 19:59:22 +020051{
Radek Krejciba03a5a2020-08-27 14:40:41 +020052 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +020053 struct ly_set *set = NULL;
54 uint32_t i;
55 const char *name;
56 size_t name_len;
57
Radek Krejciddace2c2021-01-08 11:30:56 +010058 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +010059
Michal Vasko004d3152020-06-11 19:59:22 +020060 if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +020061 /* '[' */
62
Michal Vasko69730152020-10-09 16:30:07 +020063 if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS)) &&
64 !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020065 ret = ly_set_new(&set);
66 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +020067
68 do {
69 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +020070 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +020071
72 /* check prefix based on the options */
73 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
74 if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010075 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020076 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020077 goto token_error;
Michal Vasko8b06a5e2020-08-06 12:13:08 +020078 } else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010079 LOGVAL(ctx, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020080 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020081 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +020082 }
83 if (!name) {
84 name = exp->expr + exp->tok_pos[*tok_idx];
85 name_len = exp->tok_len[*tok_idx];
86 } else {
87 ++name;
88 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
89 }
90
91 /* check whether it was not already specified */
92 for (i = 0; i < set->count; ++i) {
93 /* all the keys must be from the same module so this comparison should be fine */
94 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Radek Krejci422afb12021-03-04 16:38:16 +010095 LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +020096 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +020097 }
98 }
99
100 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200101 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200102 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200103
104 /* NameTest */
105 ++(*tok_idx);
106
107 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200108 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200109
110 /* Literal */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200111 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200112
113 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200114 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200115
Radek Krejci0f969882020-08-21 16:56:47 +0200116 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200117 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
118
Michal Vasko004d3152020-06-11 19:59:22 +0200119 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200120 /* '.' */
121
Michal Vasko004d3152020-06-11 19:59:22 +0200122 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200123 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200124
125 /* Literal */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200126 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200127
128 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200129 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200130
Michal Vasko004d3152020-06-11 19:59:22 +0200131 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200132 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +0200133
134 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200135 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200136
137 } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
138 assert(prefix == LY_PATH_PREFIX_OPTIONAL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200139 ret = ly_set_new(&set);
140 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200141
142 do {
143 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200144 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200145
146 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
147 if (!name) {
148 name = exp->expr + exp->tok_pos[*tok_idx];
149 name_len = exp->tok_len[*tok_idx];
150 } else {
151 ++name;
152 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
153 }
154
155 /* check whether it was not already specified */
156 for (i = 0; i < set->count; ++i) {
157 /* all the keys must be from the same module so this comparison should be fine */
158 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Radek Krejci422afb12021-03-04 16:38:16 +0100159 LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200160 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200161 }
162 }
163
164 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200165 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200166 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200167
168 /* NameTest */
169 ++(*tok_idx);
170
171 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200172 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200173
174 /* FuncName */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200175 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), token_error);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100176 if ((exp->tok_len[*tok_idx] != ly_strlen_const("current")) ||
177 strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", ly_strlen_const("current"))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100178 LOGVAL(ctx, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200179 exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200180 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200181 }
182 ++(*tok_idx);
183
184 /* '(' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200185 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200186
187 /* ')' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200188 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200189
190 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200191 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200192
193 /* '..' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200194 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200195 do {
196 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200197 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200198 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT));
199
200 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200201 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200202
203 /* '/' */
204 while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) {
205 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200206 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200207 }
208
209 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200210 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200211
Radek Krejci0f969882020-08-21 16:56:47 +0200212 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200213 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
214
215 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100216 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200217 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200218 }
219 }
220
Radek Krejciba03a5a2020-08-27 14:40:41 +0200221cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100222 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200223 ly_set_free(set, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200224 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200225
Radek Krejciba03a5a2020-08-27 14:40:41 +0200226token_error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100227 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200228 ly_set_free(set, NULL);
229 return LY_EVALID;
230}
231
232LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200233ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200234 uint8_t begin, uint8_t lref, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200235{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200236 LY_ERR ret = LY_SUCCESS;
237 struct lyxp_expr *exp = NULL;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200238 uint16_t tok_idx, cur_len;
239 const char *cur_node, *prev_prefix = NULL, *ptr;
Michal Vasko004d3152020-06-11 19:59:22 +0200240
241 assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER));
242 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko69730152020-10-09 16:30:07 +0200243 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) ||
244 (prefix == LY_PATH_PREFIX_STRICT_INHERIT));
Michal Vasko004d3152020-06-11 19:59:22 +0200245 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
246
Radek Krejciddace2c2021-01-08 11:30:56 +0100247 LOG_LOCSET(ctx_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100248
Michal Vasko004d3152020-06-11 19:59:22 +0200249 /* parse as a generic XPath expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200250 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 1, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200251 tok_idx = 0;
252
253 if (begin == LY_PATH_BEGIN_EITHER) {
254 /* is the path relative? */
255 if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) {
Michal Vaskocb8c6d42020-10-16 11:58:30 +0200256 /* relative path check specific to leafref */
257 if (lref == LY_PATH_LREF_TRUE) {
258 /* mandatory '..' */
259 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error);
260
261 do {
262 /* '/' */
263 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error);
264
265 /* optional '..' */
266 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT));
Michal Vasko004d3152020-06-11 19:59:22 +0200267 }
268 }
269 } else {
270 /* '/' */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200271 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error);
Michal Vasko004d3152020-06-11 19:59:22 +0200272 }
273
274 do {
275 /* NameTest */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200276 LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, error);
Michal Vasko004d3152020-06-11 19:59:22 +0200277
278 /* check prefix based on the options */
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200279 cur_node = exp->expr + exp->tok_pos[tok_idx];
280 cur_len = exp->tok_len[tok_idx];
281 if (prefix == LY_PATH_PREFIX_MANDATORY) {
282 if (!strnstr(cur_node, ":", cur_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100283 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200284 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200285 goto error;
286 }
287 } else if (prefix == LY_PATH_PREFIX_STRICT_INHERIT) {
288 if (!prev_prefix) {
289 /* the first node must have a prefix */
290 if (!strnstr(cur_node, ":", cur_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100291 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200292 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200293 goto error;
294 }
295
296 /* remember the first prefix */
297 prev_prefix = cur_node;
298 } else {
299 /* the prefix must be different, if any */
300 ptr = strnstr(cur_node, ":", cur_len);
301 if (ptr) {
302 if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100303 LOGVAL(ctx, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200304 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200305 goto error;
306 }
307
308 /* remember this next prefix */
309 prev_prefix = cur_node;
310 }
311 }
Michal Vasko004d3152020-06-11 19:59:22 +0200312 }
313
314 ++tok_idx;
315
316 /* Predicate* */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200317 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, ctx_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200318
Radek Krejci0f969882020-08-21 16:56:47 +0200319 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +0200320 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH));
321
322 /* trailing token check */
323 if (exp->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100324 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of path.", exp->expr + exp->tok_pos[tok_idx]);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200325 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200326 goto error;
327 }
328
329 *expr = exp;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100330
Radek Krejciddace2c2021-01-08 11:30:56 +0100331 LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200332 return LY_SUCCESS;
333
334error:
335 lyxp_expr_free(ctx, exp);
Radek Krejciddace2c2021-01-08 11:30:56 +0100336 LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200337 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200338}
339
340LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200341ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path,
Radek Krejci0f969882020-08-21 16:56:47 +0200342 size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200343{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200344 LY_ERR ret = LY_SUCCESS;
345 struct lyxp_expr *exp = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200346 uint16_t tok_idx;
347
348 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY));
349 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
350
Radek Krejciddace2c2021-01-08 11:30:56 +0100351 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100352
Michal Vasko004d3152020-06-11 19:59:22 +0200353 /* parse as a generic XPath expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200354 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200355 tok_idx = 0;
356
Radek Krejcif03a9e22020-09-18 20:09:31 +0200357 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, cur_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200358
359 /* trailing token check */
360 if (exp->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100361 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.",
Michal Vasko69730152020-10-09 16:30:07 +0200362 exp->expr + exp->tok_pos[tok_idx]);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200363 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200364 goto error;
365 }
366
367 *expr = exp;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100368
Radek Krejciddace2c2021-01-08 11:30:56 +0100369 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200370 return LY_SUCCESS;
371
372error:
373 lyxp_expr_free(ctx, exp);
Radek Krejciddace2c2021-01-08 11:30:56 +0100374 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200375 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200376}
377
378/**
379 * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node.
380 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200381 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200382 * @param[in] cur_node Optional current (original context) node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200383 * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_PREF_SCHEMA*.
384 * @param[in] prev_ctx_node Previous context node. Needed for ::LY_PREF_JSON.
Michal Vasko004d3152020-06-11 19:59:22 +0200385 * @param[in] expr Parsed path.
386 * @param[in] tok_idx Index in @p expr.
387 * @param[in] lref Lref option.
Michal Vasko004d3152020-06-11 19:59:22 +0200388 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200389 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200390 * @param[out] mod Resolved module.
391 * @param[out] name Parsed node name.
392 * @param[out] name_len Length of @p name.
393 * @return LY_ERR value.
394 */
395static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200396ly_path_compile_prefix(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200397 const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint16_t tok_idx, uint8_t lref,
Radek Krejci8df109d2021-04-23 12:19:08 +0200398 LY_VALUE_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, const struct lys_module **mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100399 const char **name, size_t *name_len)
Michal Vasko004d3152020-06-11 19:59:22 +0200400{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100401 LY_ERR ret;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200402 const char *pref;
Michal Vasko004d3152020-06-11 19:59:22 +0200403 size_t len;
404
405 assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST);
406
407 /* get prefix */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200408 if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) {
409 len = pref - (expr->expr + expr->tok_pos[tok_idx]);
410 pref = expr->expr + expr->tok_pos[tok_idx];
411 } else {
412 len = 0;
413 }
Michal Vasko004d3152020-06-11 19:59:22 +0200414
415 /* find next node module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200416 if (pref) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100417 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100418
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200419 *mod = ly_resolve_prefix(ctx, pref, len, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200420 if (!*mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100421 LOGVAL(ctx, LYVE_XPATH, "No module connected with the prefix \"%.*s\" found (prefix format %s).",
Radek Krejci422afb12021-03-04 16:38:16 +0100422 (int)len, pref, ly_format2str(format));
Michal Vasko825a0442021-04-16 16:11:53 +0200423 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100424 goto error;
Michal Vasko004d3152020-06-11 19:59:22 +0200425 } else if (!(*mod)->implemented) {
426 if (lref == LY_PATH_LREF_FALSE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100427 LOGVAL(ctx, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name);
Michal Vasko825a0442021-04-16 16:11:53 +0200428 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100429 goto error;
Michal Vasko004d3152020-06-11 19:59:22 +0200430 }
Michal Vasko825a0442021-04-16 16:11:53 +0200431
Michal Vaskob3e22532021-02-03 10:50:04 +0100432 assert(unres);
Michal Vasko69827502021-04-22 09:15:23 +0200433 LY_CHECK_GOTO(ret = lys_set_implemented_r((struct lys_module *)*mod, NULL, unres), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200434 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100435
Radek Krejciddace2c2021-01-08 11:30:56 +0100436 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200437 } else {
438 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200439 case LY_VALUE_SCHEMA:
440 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200441 if (!cur_mod) {
442 LOGINT_RET(ctx);
443 }
444 /* use current module */
Michal Vasko004d3152020-06-11 19:59:22 +0200445 *mod = cur_mod;
446 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200447 case LY_VALUE_JSON:
Michal Vasko004d3152020-06-11 19:59:22 +0200448 if (!prev_ctx_node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200449 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200450 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200451 /* inherit module of the previous node */
Michal Vasko004d3152020-06-11 19:59:22 +0200452 *mod = prev_ctx_node->module;
453 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200454 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200455 case LY_VALUE_XML:
Michal Vasko004d3152020-06-11 19:59:22 +0200456 /* not really defined */
Michal Vasko00cbf532020-06-15 13:58:47 +0200457 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200458 }
459 }
460
461 /* set name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200462 if (pref) {
463 *name = pref + len + 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200464 *name_len = expr->tok_len[tok_idx] - len - 1;
465 } else {
466 *name = expr->expr + expr->tok_pos[tok_idx];
467 *name_len = expr->tok_len[tok_idx];
468 }
469
470 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100471
472error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100473 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko825a0442021-04-16 16:11:53 +0200474 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200475}
476
477LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200478ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +0200479 const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx, LY_VALUE_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200480 void *prefix_data, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vasko004d3152020-06-11 19:59:22 +0200481{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100482 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +0200483 struct ly_path_predicate *p;
484 const struct lysc_node *key;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100485 const struct lys_module *mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200486 const char *name;
487 size_t name_len, key_count;
488
Michal Vasko00cbf532020-06-15 13:58:47 +0200489 assert(ctx && ctx_node);
490
Radek Krejciddace2c2021-01-08 11:30:56 +0100491 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100492
Michal Vasko004d3152020-06-11 19:59:22 +0200493 *pred_type = 0;
494
Michal Vasko004d3152020-06-11 19:59:22 +0200495 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200496 /* '[', no predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100497 goto cleanup; /* LY_SUCCESS */
Michal Vasko004d3152020-06-11 19:59:22 +0200498 }
499
500 if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) {
501 if (ctx_node->nodetype != LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100502 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200503 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100504 ret = LY_EVALID;
505 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200506 } else if (ctx_node->flags & LYS_KEYLESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100507 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200508 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100509 ret = LY_EVALID;
510 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200511 }
512
513 do {
514 /* NameTest, find the key */
Michal Vasko6b26e742020-07-17 15:02:10 +0200515 LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, LY_PATH_LREF_FALSE,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100516 format, prefix_data, NULL, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100517 key = lys_find_child(ctx_node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200518 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +0100519 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100520 ret = LY_ENOTFOUND;
521 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200522 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100523 LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", lys_nodetype2str(key->nodetype),
524 key->name);
525 ret = LY_EVALID;
526 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200527 }
528 ++(*tok_idx);
529
Michal Vasko004d3152020-06-11 19:59:22 +0200530 if (!*pred_type) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200531 /* new predicate */
Michal Vasko004d3152020-06-11 19:59:22 +0200532 *pred_type = LY_PATH_PREDTYPE_LIST;
533 }
534 assert(*pred_type == LY_PATH_PREDTYPE_LIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100535 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200536 p->key = key;
537
538 /* '=' */
539 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
540 ++(*tok_idx);
541
542 /* Literal */
543 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100544 LOG_LOCSET(key, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100545 ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200546 expr->expr + expr->tok_pos[*tok_idx] + 1, expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100547 LYD_HINT_DATA, key, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100548 LOG_LOCBACK(key ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100549 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200550 ++(*tok_idx);
551
Michal Vaskoae875662020-10-21 10:33:17 +0200552 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
553 ++((struct lysc_type *)p->value.realtype)->refcount;
554
Michal Vasko004d3152020-06-11 19:59:22 +0200555 /* ']' */
556 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
557 ++(*tok_idx);
558
559 /* another predicate follows? */
560 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
561
562 /* check that all keys were set */
563 key_count = 0;
Michal Vasko544e58a2021-01-28 14:33:41 +0100564 for (key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +0200565 ++key_count;
566 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200567 if (LY_ARRAY_COUNT(*predicates) != key_count) {
Michal Vasko004d3152020-06-11 19:59:22 +0200568 /* names (keys) are unique - it was checked when parsing */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100569 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200570 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200571 ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, *predicates);
Michal Vasko004d3152020-06-11 19:59:22 +0200572 *predicates = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 ret = LY_EVALID;
574 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200575 }
576
577 } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) {
578 if (ctx_node->nodetype != LYS_LEAFLIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100579 LOGVAL(ctx, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200580 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100581 ret = LY_EVALID;
582 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200583 }
584 ++(*tok_idx);
585
586 /* new predicate */
587 *pred_type = LY_PATH_PREDTYPE_LEAFLIST;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100588 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200589
590 /* '=' */
591 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
592 ++(*tok_idx);
593
594 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
595 /* store the value */
Radek Krejciddace2c2021-01-08 11:30:56 +0100596 LOG_LOCSET(ctx_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100597 ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200598 expr->expr + expr->tok_pos[*tok_idx] + 1, expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100599 LYD_HINT_DATA, ctx_node, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100600 LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100601 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200602 ++(*tok_idx);
603
Michal Vaskoae875662020-10-21 10:33:17 +0200604 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
605 ++((struct lysc_type *)p->value.realtype)->refcount;
606
Michal Vasko004d3152020-06-11 19:59:22 +0200607 /* ']' */
608 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
609 ++(*tok_idx);
610 } else {
611 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER);
612 if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100613 ret = LY_EVALID;
614 LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200615 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100616 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200617 } else if (ctx_node->flags & LYS_CONFIG_W) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100618 ret = LY_EVALID;
619 LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for configuration %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200620 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100621 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200622 }
Michal Vasko004d3152020-06-11 19:59:22 +0200623
624 /* new predicate */
625 *pred_type = LY_PATH_PREDTYPE_POSITION;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100626 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200627
628 /* syntax was already checked */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100629 p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&name, LY_BASE_DEC);
Michal Vasko00cbf532020-06-15 13:58:47 +0200630 ++(*tok_idx);
Michal Vasko004d3152020-06-11 19:59:22 +0200631
632 /* ']' */
633 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
634 ++(*tok_idx);
635 }
636
Radek Krejci2efc45b2020-12-22 16:25:44 +0100637cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100638 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200640}
641
642/**
643 * @brief Compile leafref predicate. Actually, it is only checked.
644 *
645 * @param[in] ctx_node Context node, node for which the predicate is defined.
646 * @param[in] cur_node Current (original context) node.
647 * @param[in] expr Parsed path.
648 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
Michal Vasko004d3152020-06-11 19:59:22 +0200649 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200650 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko405cc9e2020-12-01 12:01:27 +0100651 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko004d3152020-06-11 19:59:22 +0200652 * @return LY_ERR value.
653 */
654static LY_ERR
655ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node,
Radek Krejci8df109d2021-04-23 12:19:08 +0200656 const struct lyxp_expr *expr, uint16_t *tok_idx, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100657 struct lys_glob_unres *unres)
Michal Vasko004d3152020-06-11 19:59:22 +0200658{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100659 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +0200660 const struct lysc_node *key, *node, *node2;
661 const struct lys_module *mod;
662 const char *name;
663 size_t name_len;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100664 struct ly_ctx *ctx = cur_node->module->ctx;
665
Radek Krejciddace2c2021-01-08 11:30:56 +0100666 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200667
Michal Vasko004d3152020-06-11 19:59:22 +0200668 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200669 /* '[', no predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100670 goto cleanup; /* LY_SUCCESS */
Michal Vasko004d3152020-06-11 19:59:22 +0200671 }
672
673 if (ctx_node->nodetype != LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100674 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200675 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100676 ret = LY_EVALID;
677 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200678 } else if (ctx_node->flags & LYS_KEYLESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100679 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200680 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100681 ret = LY_EVALID;
682 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200683 }
684
685 do {
686 /* NameTest, find the key */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100687 ret = ly_path_compile_prefix(ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx,
688 LY_PATH_LREF_TRUE, format, prefix_data, unres, &mod, &name, &name_len);
689 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100690 key = lys_find_child(ctx_node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200691 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +0100692 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100693 ret = LY_EVALID;
694 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200695 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696 LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200697 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100698 ret = LY_EVALID;
699 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200700 }
701 ++(*tok_idx);
702
703 /* we are not actually compiling, throw the key away */
704 (void)key;
705
706 /* '=' */
707 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
708 ++(*tok_idx);
709
710 /* FuncName */
711 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME);
712 ++(*tok_idx);
713
714 /* evaluating from the "current()" node */
715 node = cur_node;
716
717 /* '(' */
718 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
719 ++(*tok_idx);
720
721 /* ')' */
722 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
723 ++(*tok_idx);
724
725 do {
726 /* '/' */
727 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
728 ++(*tok_idx);
729
730 /* go to parent */
731 if (!node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100732 LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path.");
733 ret = LY_EVALID;
734 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200735 }
736 node = lysc_data_parent(node);
737
738 /* '..' */
739 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT);
740 ++(*tok_idx);
741 } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT);
742
743 do {
744 /* '/' */
745 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
746 ++(*tok_idx);
747
748 /* NameTest */
749 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100750 LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_node->module, node, expr, *tok_idx,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100751 LY_PATH_LREF_TRUE, format, prefix_data, unres, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100752 node2 = lys_find_child(node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200753 if (!node2) {
Radek Krejci422afb12021-03-04 16:38:16 +0100754 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100755 ret = LY_EVALID;
756 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200757 }
758 node = node2;
759 ++(*tok_idx);
760 } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST));
761
762 /* check the last target node */
763 if (node->nodetype != LYS_LEAF) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100764 LOGVAL(ctx, LYVE_XPATH, "Leaf expected instead of %s \"%s\" in leafref predicate in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200765 lys_nodetype2str(node->nodetype), node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100766 ret = LY_EVALID;
767 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200768 }
769
770 /* we are not actually compiling, throw the rightside node away */
771 (void)node;
772
773 /* ']' */
774 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
775 ++(*tok_idx);
776
Radek Krejci0f969882020-08-21 16:56:47 +0200777 /* another predicate follows? */
Michal Vasko004d3152020-06-11 19:59:22 +0200778 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
779
Radek Krejci2efc45b2020-12-22 16:25:44 +0100780cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100781 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100782 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200783}
784
785LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200786ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
Radek Krejcid5d37432021-03-12 13:46:40 +0100787 const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target,
Radek Krejci8df109d2021-04-23 12:19:08 +0200788 LY_VALUE_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, struct ly_path **path)
Michal Vasko004d3152020-06-11 19:59:22 +0200789{
790 LY_ERR ret = LY_SUCCESS;
791 uint16_t tok_idx = 0;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100792 const struct lys_module *mod = NULL;
Michal Vasko6b26e742020-07-17 15:02:10 +0200793 const struct lysc_node *node2, *cur_node, *op;
Michal Vasko00cbf532020-06-15 13:58:47 +0200794 struct ly_path *p = NULL;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200795 uint32_t getnext_opts;
Michal Vasko004d3152020-06-11 19:59:22 +0200796 const char *name;
797 size_t name_len;
798
Michal Vasko00cbf532020-06-15 13:58:47 +0200799 assert(ctx);
Michal Vasko6b26e742020-07-17 15:02:10 +0200800 assert((lref == LY_PATH_LREF_FALSE) || ctx_node);
Michal Vasko004d3152020-06-11 19:59:22 +0200801 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko00cbf532020-06-15 13:58:47 +0200802 assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT));
803 assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY));
Michal Vasko004d3152020-06-11 19:59:22 +0200804
Michal Vasko6b26e742020-07-17 15:02:10 +0200805 /* find operation, if we are in any */
Radek Krejci1e008d22020-08-17 11:37:37 +0200806 for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +0200807
Radek Krejci2efc45b2020-12-22 16:25:44 +0100808 *path = NULL;
809
Michal Vasko6b26e742020-07-17 15:02:10 +0200810 /* remember original context node */
811 cur_node = ctx_node;
Radek Krejciddace2c2021-01-08 11:30:56 +0100812 LOG_LOCINIT(cur_node, NULL, NULL, NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200813
Michal Vasko00cbf532020-06-15 13:58:47 +0200814 if (oper == LY_PATH_OPER_OUTPUT) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100815 getnext_opts = LYS_GETNEXT_OUTPUT;
Michal Vasko00cbf532020-06-15 13:58:47 +0200816 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100817 getnext_opts = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +0200818 }
819
Michal Vasko004d3152020-06-11 19:59:22 +0200820 if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) {
821 /* absolute path */
822 ctx_node = NULL;
823
824 ++tok_idx;
825 } else {
826 /* relative path */
827 while ((lref == LY_PATH_LREF_TRUE) && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) {
828 if (!ctx_node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100829 LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path.");
Michal Vasko14424ba2020-12-09 18:09:51 +0100830 ret = LY_EVALID;
831 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200832 }
833
834 /* get parent */
835 ctx_node = lysc_data_parent(ctx_node);
836
837 ++tok_idx;
838
839 assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH);
840 ++tok_idx;
841 }
842
Michal Vasko00cbf532020-06-15 13:58:47 +0200843 /* we are not storing the parent */
844 (void)ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +0200845 }
846
847 do {
Michal Vasko00cbf532020-06-15 13:58:47 +0200848 /* check last compiled inner node, whether it is uniquely identified (even key-less list) */
Michal Vaskod456cf62020-11-23 16:48:43 +0100849 if (p && (lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) &&
850 (p->node->nodetype == LYS_LIST) && !p->predicates) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100851 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200852 lys_nodetype2str(p->node->nodetype), p->node->name);
Michal Vasko14424ba2020-12-09 18:09:51 +0100853 ret = LY_EVALID;
854 goto cleanup;
Michal Vasko00cbf532020-06-15 13:58:47 +0200855 }
856
Michal Vasko14424ba2020-12-09 18:09:51 +0100857 /* NameTest */
858 LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, expr, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, cleanup);
859
Michal Vasko004d3152020-06-11 19:59:22 +0200860 /* get module and node name */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200861 LY_CHECK_GOTO(ret = ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, lref, format,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100862 prefix_data, unres, &mod, &name, &name_len), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200863 ++tok_idx;
864
865 /* find the next node */
Radek Krejcid5d37432021-03-12 13:46:40 +0100866 if (!ctx_node && ext) {
867 node2 = lysc_ext_find_node(ext, mod, name, name_len, 0, getnext_opts);
868 } else {
869 node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts);
870 }
Radek Krejci25fe3dd2020-11-10 22:02:26 +0100871 if (!node2 || (op && (node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) {
Radek Krejci422afb12021-03-04 16:38:16 +0100872 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200873 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200874 goto cleanup;
875 }
876 ctx_node = node2;
877
878 /* new path segment */
Michal Vasko00cbf532020-06-15 13:58:47 +0200879 LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200880 p->node = ctx_node;
881
882 /* compile any predicates */
883 if (lref == LY_PATH_LREF_TRUE) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100884 ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data, unres);
Michal Vasko004d3152020-06-11 19:59:22 +0200885 } else {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200886 ret = ly_path_compile_predicate(ctx, cur_node, cur_mod, ctx_node, expr, &tok_idx, format, prefix_data,
Michal Vasko69730152020-10-09 16:30:07 +0200887 &p->predicates, &p->pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +0200888 }
889 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200890 } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH));
891
Michal Vasko14424ba2020-12-09 18:09:51 +0100892 /* check leftover tokens */
893 if (tok_idx < expr->used) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100894 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(expr->tokens[tok_idx]), &expr->expr[expr->tok_pos[tok_idx]]);
Michal Vasko14424ba2020-12-09 18:09:51 +0100895 ret = LY_EVALID;
896 goto cleanup;
897 }
898
Michal Vasko00cbf532020-06-15 13:58:47 +0200899 /* check last compiled node */
Michal Vasko69730152020-10-09 16:30:07 +0200900 if ((lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) &&
901 (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100902 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200903 lys_nodetype2str(p->node->nodetype), p->node->name);
Michal Vasko14424ba2020-12-09 18:09:51 +0100904 ret = LY_EVALID;
905 goto cleanup;
Michal Vasko00cbf532020-06-15 13:58:47 +0200906 }
907
Michal Vasko004d3152020-06-11 19:59:22 +0200908cleanup:
909 if (ret) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200910 ly_path_free(ctx, *path);
Michal Vasko004d3152020-06-11 19:59:22 +0200911 *path = NULL;
912 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100913 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200914 return ret;
915}
916
917LY_ERR
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200918ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx,
Radek Krejci0f969882020-08-21 16:56:47 +0200919 struct lyd_node **match)
Michal Vasko004d3152020-06-11 19:59:22 +0200920{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200921 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoacfc9282021-02-04 12:08:23 +0100922 struct lyd_node *prev_node = NULL, *node = NULL, *target;
Michal Vasko004d3152020-06-11 19:59:22 +0200923 uint64_t pos;
924
925 assert(path && start);
926
927 if (lysc_data_parent(path[0].node)) {
928 /* relative path, start from the parent children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200929 start = lyd_child(start);
Michal Vasko004d3152020-06-11 19:59:22 +0200930 } else {
931 /* absolute path, start from the first top-level sibling */
932 while (start->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100933 start = lyd_parent(start);
Michal Vasko004d3152020-06-11 19:59:22 +0200934 }
935 while (start->prev->next) {
936 start = start->prev;
937 }
938 }
939
940 LY_ARRAY_FOR(path, u) {
941 switch (path[u].pred_type) {
942 case LY_PATH_PREDTYPE_POSITION:
943 /* we cannot use hashes and want an instance on a specific position */
944 pos = 1;
Michal Vasko4c583e82020-07-17 12:16:14 +0200945 LYD_LIST_FOR_INST(start, path[u].node, node) {
Michal Vasko004d3152020-06-11 19:59:22 +0200946 if (pos == path[u].predicates[0].position) {
947 break;
948 }
949 ++pos;
950 }
951 break;
952 case LY_PATH_PREDTYPE_LEAFLIST:
953 /* we will use hashes to find one leaf-list instance */
954 LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target));
955 lyd_find_sibling_first(start, target, &node);
956 lyd_free_tree(target);
957 break;
958 case LY_PATH_PREDTYPE_LIST:
959 /* we will use hashes to find one list instance */
960 LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target));
961 lyd_find_sibling_first(start, target, &node);
962 lyd_free_tree(target);
963 break;
964 case LY_PATH_PREDTYPE_NONE:
965 /* we will use hashes to find one any/container/leaf instance */
966 lyd_find_sibling_val(start, path[u].node, NULL, 0, &node);
967 break;
968 }
969
970 if (!node) {
971 /* no matching nodes */
972 break;
973 }
974
Michal Vasko00cbf532020-06-15 13:58:47 +0200975 /* rememeber previous node */
976 prev_node = node;
977
Michal Vasko004d3152020-06-11 19:59:22 +0200978 /* next path segment, if any */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200979 start = lyd_child(node);
Michal Vasko004d3152020-06-11 19:59:22 +0200980 }
981
Michal Vasko004d3152020-06-11 19:59:22 +0200982 if (node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200983 /* we have found the full path */
984 if (path_idx) {
985 *path_idx = u;
986 }
987 if (match) {
988 *match = node;
989 }
Michal Vasko004d3152020-06-11 19:59:22 +0200990 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200991
992 } else if (prev_node) {
993 /* we have found only some partial match */
994 if (path_idx) {
995 *path_idx = u - 1;
996 }
997 if (match) {
998 *match = prev_node;
999 }
1000 return LY_EINCOMPLETE;
1001 }
1002
1003 /* we have not found any nodes */
1004 if (path_idx) {
1005 *path_idx = 0;
1006 }
1007 if (match) {
1008 *match = NULL;
1009 }
1010 return LY_ENOTFOUND;
1011}
1012
1013LY_ERR
1014ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match)
1015{
1016 LY_ERR ret;
1017 struct lyd_node *m;
1018
1019 ret = ly_path_eval_partial(path, start, NULL, &m);
1020
1021 if (ret == LY_SUCCESS) {
1022 /* last node was found */
1023 if (match) {
1024 *match = m;
1025 }
1026 return LY_SUCCESS;
1027 }
1028
1029 /* not a full match */
1030 if (match) {
1031 *match = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02001032 }
1033 return LY_ENOTFOUND;
1034}
1035
1036LY_ERR
1037ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup)
1038{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001039 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko004d3152020-06-11 19:59:22 +02001040
1041 if (!path) {
1042 return LY_SUCCESS;
1043 }
1044
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001045 LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02001046 LY_ARRAY_FOR(path, u) {
1047 LY_ARRAY_INCREMENT(*dup);
1048 (*dup)[u].node = path[u].node;
1049 if (path[u].predicates) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001050 LY_ARRAY_CREATE_RET(ctx, (*dup)[u].predicates, LY_ARRAY_COUNT(path[u].predicates), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02001051 (*dup)[u].pred_type = path[u].pred_type;
1052 LY_ARRAY_FOR(path[u].predicates, v) {
1053 struct ly_path_predicate *pred = &path[u].predicates[v];
1054
1055 LY_ARRAY_INCREMENT((*dup)[u].predicates);
1056 switch (path[u].pred_type) {
1057 case LY_PATH_PREDTYPE_POSITION:
1058 /* position-predicate */
1059 (*dup)[u].predicates[v].position = pred->position;
1060 break;
1061 case LY_PATH_PREDTYPE_LIST:
1062 case LY_PATH_PREDTYPE_LEAFLIST:
1063 /* key-predicate or leaf-list-predicate */
1064 (*dup)[u].predicates[v].key = pred->key;
Michal Vasko004d3152020-06-11 19:59:22 +02001065 pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value);
Michal Vaskoae875662020-10-21 10:33:17 +02001066 ++((struct lysc_type *)pred->value.realtype)->refcount;
Michal Vasko004d3152020-06-11 19:59:22 +02001067 break;
1068 case LY_PATH_PREDTYPE_NONE:
1069 break;
1070 }
1071 }
1072 }
1073 }
1074
1075 return LY_SUCCESS;
1076}
1077
1078void
Michal Vaskof7e16e22020-10-21 09:24:39 +02001079ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, struct ly_path_predicate *predicates)
Michal Vasko004d3152020-06-11 19:59:22 +02001080{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001081 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001082
1083 if (!predicates) {
1084 return;
1085 }
1086
1087 LY_ARRAY_FOR(predicates, u) {
1088 switch (pred_type) {
1089 case LY_PATH_PREDTYPE_POSITION:
1090 case LY_PATH_PREDTYPE_NONE:
1091 /* nothing to free */
1092 break;
1093 case LY_PATH_PREDTYPE_LIST:
Michal Vasko004d3152020-06-11 19:59:22 +02001094 case LY_PATH_PREDTYPE_LEAFLIST:
Michal Vaskoae875662020-10-21 10:33:17 +02001095 if (predicates[u].value.realtype) {
1096 predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
1097 lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype);
1098 }
Michal Vasko004d3152020-06-11 19:59:22 +02001099 break;
1100 }
1101 }
1102 LY_ARRAY_FREE(predicates);
1103}
1104
1105void
1106ly_path_free(const struct ly_ctx *ctx, struct ly_path *path)
1107{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001108 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001109
1110 LY_ARRAY_FOR(path, u) {
Michal Vaskof7e16e22020-10-21 09:24:39 +02001111 ly_path_predicates_free(ctx, path[u].pred_type, path[u].predicates);
Michal Vasko004d3152020-06-11 19:59:22 +02001112 }
1113 LY_ARRAY_FREE(path);
1114}