blob: 2214ed0a0510db35a5b82747427140af1ae5a111 [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 *
Michal Vasko90189962023-02-28 12:10:34 +01006 * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
Michal Vasko004d3152020-06-11 19:59:22 +02007 *
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 */
Michal Vasko90189962023-02-28 12:10:34 +010014
15#define _GNU_SOURCE
16
Michal Vasko004d3152020-06-11 19:59:22 +020017#include "path.h"
18
19#include <assert.h>
Michal Vasko004d3152020-06-11 19:59:22 +020020#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020021#include <string.h>
Michal Vasko004d3152020-06-11 19:59:22 +020022
Michal Vasko5aa44c02020-06-29 11:47:02 +020023#include "compat.h"
Michal Vasko004d3152020-06-11 19:59:22 +020024#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010025#include "ly_common.h"
Michal Vasko004d3152020-06-11 19:59:22 +020026#include "plugins_types.h"
Michal Vasko40c158c2021-04-28 17:01:03 +020027#include "schema_compile.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020028#include "set.h"
Michal Vasko4c583e82020-07-17 12:16:14 +020029#include "tree.h"
Michal Vasko004d3152020-06-11 19:59:22 +020030#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010031#include "tree_edit.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020032#include "tree_schema.h"
Michal Vasko004d3152020-06-11 19:59:22 +020033#include "tree_schema_internal.h"
34#include "xpath.h"
35
Radek Krejcic0c66412020-08-21 13:53:50 +020036#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 +020037
Michal Vasko004d3152020-06-11 19:59:22 +020038/**
39 * @brief Check predicate syntax.
40 *
41 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +020042 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +020043 * @param[in] exp Parsed predicate.
44 * @param[in,out] tok_idx Index in @p exp, is adjusted.
45 * @param[in] prefix Prefix option.
46 * @param[in] pred Predicate option.
47 * @return LY_ERR value.
48 */
49static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +020050ly_path_check_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lyxp_expr *exp,
Michal Vasko32ca49b2023-02-17 15:11:35 +010051 uint32_t *tok_idx, uint16_t prefix, uint16_t pred)
Michal Vasko004d3152020-06-11 19:59:22 +020052{
Radek Krejciba03a5a2020-08-27 14:40:41 +020053 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +020054 struct ly_set *set = NULL;
55 uint32_t i;
56 const char *name;
57 size_t name_len;
58
Michal Vasko7a266772024-01-23 11:02:38 +010059 if (cur_node) {
60 LOG_LOCSET(cur_node, NULL);
61 }
Radek Krejci2efc45b2020-12-22 16:25:44 +010062
Michal Vasko004d3152020-06-11 19:59:22 +020063 if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +020064 /* '[' */
65
Michal Vasko69730152020-10-09 16:30:07 +020066 if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS)) &&
67 !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020068 ret = ly_set_new(&set);
69 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +020070
71 do {
72 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +020073 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +020074
75 /* check prefix based on the options */
76 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
77 if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) {
Michal Vasko21eaa392024-02-20 15:48:42 +010078 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", (int)exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020079 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020080 goto token_error;
Michal Vasko8b06a5e2020-08-06 12:13:08 +020081 } else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) {
Michal Vasko21eaa392024-02-20 15:48:42 +010082 LOGVAL(ctx, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", (int)exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020083 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020084 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +020085 }
86 if (!name) {
87 name = exp->expr + exp->tok_pos[*tok_idx];
88 name_len = exp->tok_len[*tok_idx];
89 } else {
90 ++name;
91 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
92 }
93
94 /* check whether it was not already specified */
95 for (i = 0; i < set->count; ++i) {
96 /* all the keys must be from the same module so this comparison should be fine */
Michal Vasko9ff8d2d2022-09-29 13:41:14 +020097 if (!strncmp(set->objs[i], name, name_len) &&
98 lysp_check_identifierchar(NULL, ((char *)set->objs[i])[name_len], 0, NULL)) {
Radek Krejci422afb12021-03-04 16:38:16 +010099 LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200100 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200101 }
102 }
103
104 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200105 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200106 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200107
108 /* NameTest */
109 ++(*tok_idx);
110
111 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200112 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200113
Michal Vaskob01983e2023-04-24 10:43:13 +0200114 /* fill repeat */
115 exp->repeat[*tok_idx - 2] = calloc(2, sizeof *exp->repeat[*tok_idx]);
116 LY_CHECK_ERR_GOTO(!exp->repeat[*tok_idx - 2], LOGMEM(NULL); ret = LY_EMEM, cleanup);
117 exp->repeat[*tok_idx - 2][0] = LYXP_EXPR_EQUALITY;
118
Michal Vasko90189962023-02-28 12:10:34 +0100119 /* Literal, Number, or VariableReference */
120 if (lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_LITERAL) &&
121 lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER) &&
122 lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_VARREF)) {
123 /* error */
124 lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL);
125 goto token_error;
126 }
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
Radek Krejci0f969882020-08-21 16:56:47 +0200131 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200132 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
133
Michal Vasko004d3152020-06-11 19:59:22 +0200134 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200135 /* '.' */
136
Michal Vasko004d3152020-06-11 19:59:22 +0200137 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200138 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200139
Michal Vaskob01983e2023-04-24 10:43:13 +0200140 /* fill repeat */
141 exp->repeat[*tok_idx - 2] = calloc(2, sizeof *exp->repeat[*tok_idx]);
142 LY_CHECK_ERR_GOTO(!exp->repeat[*tok_idx - 2], LOGMEM(NULL); ret = LY_EMEM, cleanup);
143 exp->repeat[*tok_idx - 2][0] = LYXP_EXPR_EQUALITY;
144
Michal Vasko4911eeb2021-06-28 11:23:05 +0200145 /* Literal or Number */
146 LY_CHECK_GOTO(lyxp_next_token2(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL, LYXP_TOKEN_NUMBER), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200147
148 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200149 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200150
Michal Vasko004d3152020-06-11 19:59:22 +0200151 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200152 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +0200153
Michal Vasko2261dfa2022-09-29 12:29:20 +0200154 /* check for index 0 */
155 if (!atoi(exp->expr + exp->tok_pos[*tok_idx - 1])) {
156 LOGVAL(ctx, LYVE_XPATH, "Invalid positional predicate \"%.*s\".", (int)exp->tok_len[*tok_idx - 1],
157 exp->expr + exp->tok_pos[*tok_idx - 1]);
158 goto token_error;
159 }
160
Michal Vasko004d3152020-06-11 19:59:22 +0200161 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200162 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200163
164 } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
165 assert(prefix == LY_PATH_PREFIX_OPTIONAL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200166 ret = ly_set_new(&set);
167 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200168
169 do {
170 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200171 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200172
173 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
174 if (!name) {
175 name = exp->expr + exp->tok_pos[*tok_idx];
176 name_len = exp->tok_len[*tok_idx];
177 } else {
178 ++name;
179 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
180 }
181
182 /* check whether it was not already specified */
183 for (i = 0; i < set->count; ++i) {
184 /* all the keys must be from the same module so this comparison should be fine */
Michal Vasko9ff8d2d2022-09-29 13:41:14 +0200185 if (!strncmp(set->objs[i], name, name_len) &&
186 lysp_check_identifierchar(NULL, ((char *)set->objs[i])[name_len], 0, NULL)) {
Radek Krejci422afb12021-03-04 16:38:16 +0100187 LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200188 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200189 }
190 }
191
192 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200193 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200194 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200195
196 /* NameTest */
197 ++(*tok_idx);
198
199 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200200 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200201
Michal Vaskob01983e2023-04-24 10:43:13 +0200202 /* fill repeat */
203 exp->repeat[*tok_idx - 2] = calloc(2, sizeof *exp->repeat[*tok_idx]);
204 LY_CHECK_ERR_GOTO(!exp->repeat[*tok_idx - 2], LOGMEM(NULL); ret = LY_EMEM, cleanup);
205 exp->repeat[*tok_idx - 2][0] = LYXP_EXPR_EQUALITY;
206
Michal Vasko004d3152020-06-11 19:59:22 +0200207 /* FuncName */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200208 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), token_error);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100209 if ((exp->tok_len[*tok_idx] != ly_strlen_const("current")) ||
210 strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", ly_strlen_const("current"))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100211 LOGVAL(ctx, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.",
Michal Vasko21eaa392024-02-20 15:48:42 +0100212 (int)exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200213 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200214 }
215 ++(*tok_idx);
216
217 /* '(' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200218 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200219
220 /* ')' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200221 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200222
223 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200224 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200225
226 /* '..' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200227 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200228 do {
229 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200230 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200231 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT));
232
233 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200234 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200235
236 /* '/' */
237 while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) {
238 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200239 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200240 }
241
242 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200243 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200244
Radek Krejci0f969882020-08-21 16:56:47 +0200245 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200246 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
247
Michal Vasko3c19b492022-09-05 08:48:10 +0200248 } else if (lyxp_check_token(ctx, exp, *tok_idx, 0)) {
249 /* unexpected EOF */
250 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200251 } else {
Michal Vasko3c19b492022-09-05 08:48:10 +0200252 /* invalid token */
Michal Vasko49fec8e2022-05-24 10:28:33 +0200253 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200254 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200255 }
256 }
257
Radek Krejciba03a5a2020-08-27 14:40:41 +0200258cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100259 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200260 ly_set_free(set, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200261 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200262
Radek Krejciba03a5a2020-08-27 14:40:41 +0200263token_error:
Michal Vasko7a266772024-01-23 11:02:38 +0100264 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200265 ly_set_free(set, NULL);
266 return LY_EVALID;
267}
268
stewegd8e2fc92023-05-31 09:52:56 +0200269/**
270 * @brief Parse deref XPath function and perform all additional checks.
271 *
272 * @param[in] ctx libyang context.
273 * @param[in] ctx_node Optional context node, used for logging.
274 * @param[in] exp Parsed path.
275 * @param[in,out] tok_idx Index in @p exp, is adjusted.
276 * @return LY_ERR value.
277 */
278static LY_ERR
279ly_path_parse_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const struct lyxp_expr *exp,
280 uint32_t *tok_idx)
281{
282 size_t arg_len;
283 uint32_t begin_token, end_token;
284 struct lyxp_expr *arg_expr = NULL;
285
286 /* mandatory FunctionName */
287 LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_FUNCNAME), LY_EVALID);
288 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
289 LOGVAL(ctx, LYVE_XPATH, "Unexpected XPath function \"%.*s\" in path, expected \"deref(...)\"",
Michal Vasko21eaa392024-02-20 15:48:42 +0100290 (int)exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
stewegd8e2fc92023-05-31 09:52:56 +0200291 return LY_EVALID;
292 }
293
294 /* mandatory '(' */
295 LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), LY_EVALID);
296 begin_token = *tok_idx;
297
298 /* count tokens till ')' */
299 while (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_PAR2) && *tok_idx < exp->used) {
300 /* emebedded functions are not allowed */
301 if (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_FUNCNAME)) {
302 LOGVAL(ctx, LYVE_XPATH, "Embedded function XPath function inside deref function within the path"
303 "is not allowed");
304 return LY_EVALID;
305 }
306
307 (*tok_idx)++;
308 }
309
310 /* mandatory ')' */
311 LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), LY_EVALID);
312 end_token = *tok_idx - 1;
313
314 /* parse the path of deref argument */
315 arg_len = exp->tok_pos[end_token] - exp->tok_pos[begin_token];
316 LY_CHECK_RET(ly_path_parse(ctx, ctx_node, &exp->expr[exp->tok_pos[begin_token]], arg_len, 1,
317 LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &arg_expr), LY_EVALID);
318 lyxp_expr_free(ctx, arg_expr);
319
320 return LY_SUCCESS;
321}
322
Michal Vasko004d3152020-06-11 19:59:22 +0200323LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200324ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len,
Michal Vasko32ca49b2023-02-17 15:11:35 +0100325 ly_bool lref, uint16_t begin, uint16_t prefix, uint16_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200326{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200327 LY_ERR ret = LY_SUCCESS;
328 struct lyxp_expr *exp = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +0200329 uint32_t tok_idx, cur_len;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200330 const char *cur_node, *prev_prefix = NULL, *ptr;
Michal Vasko32ca49b2023-02-17 15:11:35 +0100331 ly_bool is_abs;
Michal Vasko004d3152020-06-11 19:59:22 +0200332
333 assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER));
Michal Vasko69730152020-10-09 16:30:07 +0200334 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) ||
Michal Vasko32ca49b2023-02-17 15:11:35 +0100335 (prefix == LY_PATH_PREFIX_FIRST) || (prefix == LY_PATH_PREFIX_STRICT_INHERIT));
Michal Vasko004d3152020-06-11 19:59:22 +0200336 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
337
Michal Vasko7a266772024-01-23 11:02:38 +0100338 if (ctx_node) {
339 LOG_LOCSET(ctx_node, NULL);
340 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100341
Michal Vaskob01983e2023-04-24 10:43:13 +0200342 /* parse as a generic XPath expression, reparse is performed manually */
343 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200344 tok_idx = 0;
345
Michal Vaskob01983e2023-04-24 10:43:13 +0200346 /* alloc empty repeat (only '=', filled manually) */
347 exp->repeat = calloc(exp->size, sizeof *exp->repeat);
348 LY_CHECK_ERR_GOTO(!exp->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
349
Michal Vasko004d3152020-06-11 19:59:22 +0200350 if (begin == LY_PATH_BEGIN_EITHER) {
351 /* is the path relative? */
352 if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) {
Michal Vaskocb8c6d42020-10-16 11:58:30 +0200353 /* relative path check specific to leafref */
Michal Vaskoed725d72021-06-23 12:03:45 +0200354 if (lref) {
stewegd8e2fc92023-05-31 09:52:56 +0200355 /* optional function 'deref..' */
356 if ((ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_EXTENDED) &&
357 !lyxp_check_token(NULL, exp, tok_idx, LYXP_TOKEN_FUNCNAME)) {
358 LY_CHECK_ERR_GOTO(ly_path_parse_deref(ctx, ctx_node, exp, &tok_idx), ret = LY_EVALID, error);
359
360 /* '/' */
361 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID,
362 error);
363 }
364
Michal Vaskocb8c6d42020-10-16 11:58:30 +0200365 /* mandatory '..' */
366 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error);
367
368 do {
369 /* '/' */
stewegd8e2fc92023-05-31 09:52:56 +0200370 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID,
371 error);
Michal Vaskocb8c6d42020-10-16 11:58:30 +0200372
373 /* optional '..' */
374 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT));
Michal Vasko004d3152020-06-11 19:59:22 +0200375 }
Michal Vasko32ca49b2023-02-17 15:11:35 +0100376
377 is_abs = 0;
378 } else {
379 is_abs = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200380 }
381 } else {
382 /* '/' */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200383 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error);
Michal Vasko32ca49b2023-02-17 15:11:35 +0100384
385 is_abs = 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200386 }
387
388 do {
389 /* NameTest */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200390 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 +0200391
392 /* check prefix based on the options */
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200393 cur_node = exp->expr + exp->tok_pos[tok_idx];
394 cur_len = exp->tok_len[tok_idx];
395 if (prefix == LY_PATH_PREFIX_MANDATORY) {
396 if (!strnstr(cur_node, ":", cur_len)) {
Michal Vasko21eaa392024-02-20 15:48:42 +0100397 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", (int)cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200398 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200399 goto error;
400 }
Michal Vasko32ca49b2023-02-17 15:11:35 +0100401 } else if ((prefix == LY_PATH_PREFIX_FIRST) || (prefix == LY_PATH_PREFIX_STRICT_INHERIT)) {
402 if (!prev_prefix && is_abs) {
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200403 /* the first node must have a prefix */
404 if (!strnstr(cur_node, ":", cur_len)) {
Michal Vasko21eaa392024-02-20 15:48:42 +0100405 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", (int)cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200406 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200407 goto error;
408 }
409
410 /* remember the first prefix */
411 prev_prefix = cur_node;
Michal Vasko32ca49b2023-02-17 15:11:35 +0100412 } else if (prev_prefix && (prefix == LY_PATH_PREFIX_STRICT_INHERIT)) {
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200413 /* the prefix must be different, if any */
414 ptr = strnstr(cur_node, ":", cur_len);
415 if (ptr) {
416 if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) {
Michal Vasko21eaa392024-02-20 15:48:42 +0100417 LOGVAL(ctx, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", (int)cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200418 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200419 goto error;
420 }
421
422 /* remember this next prefix */
423 prev_prefix = cur_node;
424 }
425 }
Michal Vasko004d3152020-06-11 19:59:22 +0200426 }
427
428 ++tok_idx;
429
430 /* Predicate* */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200431 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, ctx_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200432
Radek Krejci0f969882020-08-21 16:56:47 +0200433 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +0200434 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH));
435
436 /* trailing token check */
437 if (exp->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100438 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 +0200439 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200440 goto error;
441 }
442
443 *expr = exp;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100444
Michal Vasko7a266772024-01-23 11:02:38 +0100445 LOG_LOCBACK(ctx_node ? 1 : 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200446 return LY_SUCCESS;
447
448error:
449 lyxp_expr_free(ctx, exp);
Michal Vasko7a266772024-01-23 11:02:38 +0100450 LOG_LOCBACK(ctx_node ? 1 : 0, 0);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200451 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200452}
453
454LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200455ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path,
Michal Vasko32ca49b2023-02-17 15:11:35 +0100456 size_t path_len, uint16_t prefix, uint16_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200457{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200458 LY_ERR ret = LY_SUCCESS;
459 struct lyxp_expr *exp = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +0200460 uint32_t tok_idx;
Michal Vasko004d3152020-06-11 19:59:22 +0200461
462 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY));
463 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
464
Michal Vasko7a266772024-01-23 11:02:38 +0100465 if (cur_node) {
466 LOG_LOCSET(cur_node, NULL);
467 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100468
Michal Vaskob01983e2023-04-24 10:43:13 +0200469 /* parse as a generic XPath expression, reparse is performed manually */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200470 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200471 tok_idx = 0;
472
Michal Vaskob01983e2023-04-24 10:43:13 +0200473 /* alloc empty repeat (only '=', filled manually) */
474 exp->repeat = calloc(exp->size, sizeof *exp->repeat);
475 LY_CHECK_ERR_GOTO(!exp->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
476
Radek Krejcif03a9e22020-09-18 20:09:31 +0200477 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, cur_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200478
479 /* trailing token check */
480 if (exp->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100481 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.",
Michal Vasko69730152020-10-09 16:30:07 +0200482 exp->expr + exp->tok_pos[tok_idx]);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200483 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200484 goto error;
485 }
486
487 *expr = exp;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100488
Michal Vasko7a266772024-01-23 11:02:38 +0100489 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200490 return LY_SUCCESS;
491
492error:
493 lyxp_expr_free(ctx, exp);
Michal Vasko7a266772024-01-23 11:02:38 +0100494 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200495 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200496}
497
498/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200499 * @brief Parse NameTest and get the corresponding schema node.
Michal Vasko004d3152020-06-11 19:59:22 +0200500 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200501 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200502 * @param[in] cur_node Optional current (original context) node.
Radek Krejci84d7fd72021-07-14 18:32:21 +0200503 * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_VALUE_SCHEMA
504 * and ::LY_VALUE_SCHEMA_RESOLVED.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200505 * @param[in] prev_ctx_node Previous context node.
Michal Vasko004d3152020-06-11 19:59:22 +0200506 * @param[in] expr Parsed path.
507 * @param[in] tok_idx Index in @p expr.
Michal Vasko004d3152020-06-11 19:59:22 +0200508 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200509 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko8cc3f662022-03-29 11:25:51 +0200510 * @param[in] top_ext Optional top-level extension to use for searching the schema node.
511 * @param[in] getnext_opts Options to be used for ::lys_getnext() calls.
512 * @param[out] snode Resolved schema node.
513 * @param[out] ext Optional extension instance of @p snode, if any.
Michal Vasko004d3152020-06-11 19:59:22 +0200514 * @return LY_ERR value.
515 */
516static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +0200517ly_path_compile_snode(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Michal Vaskodd528af2022-08-08 14:35:07 +0200518 const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint32_t tok_idx, LY_VALUE_FORMAT format,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200519 void *prefix_data, const struct lysc_ext_instance *top_ext, uint32_t getnext_opts, const struct lysc_node **snode,
520 struct lysc_ext_instance **ext)
Michal Vasko004d3152020-06-11 19:59:22 +0200521{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100522 LY_ERR ret;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200523 const struct lys_module *mod = NULL;
524 struct lysc_ext_instance *e = NULL;
525 const char *pref, *name;
526 size_t len, name_len;
Michal Vasko004d3152020-06-11 19:59:22 +0200527
528 assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST);
529
Michal Vasko8cc3f662022-03-29 11:25:51 +0200530 *snode = NULL;
531 if (ext) {
532 *ext = NULL;
533 }
534
Michal Vasko004d3152020-06-11 19:59:22 +0200535 /* get prefix */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200536 if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) {
537 len = pref - (expr->expr + expr->tok_pos[tok_idx]);
538 pref = expr->expr + expr->tok_pos[tok_idx];
539 } else {
540 len = 0;
541 }
Michal Vasko004d3152020-06-11 19:59:22 +0200542
Michal Vasko8cc3f662022-03-29 11:25:51 +0200543 /* set name */
544 if (pref) {
545 name = pref + len + 1;
546 name_len = expr->tok_len[tok_idx] - len - 1;
547 } else {
548 name = expr->expr + expr->tok_pos[tok_idx];
549 name_len = expr->tok_len[tok_idx];
550 }
551
552 /* find node module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200553 if (pref) {
Michal Vasko7a266772024-01-23 11:02:38 +0100554 if (cur_node) {
555 LOG_LOCSET(cur_node, NULL);
556 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100557
Michal Vasko35b29622022-07-22 14:12:56 +0200558 mod = ly_resolve_prefix(prev_ctx_node ? prev_ctx_node->module->ctx : ctx, pref, len, format, prefix_data);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200559 if ((!mod || !mod->implemented) && prev_ctx_node) {
560 /* check for nested ext data */
561 ret = ly_nested_ext_schema(NULL, prev_ctx_node, pref, len, format, prefix_data, name, name_len, snode, &e);
562 if (!ret) {
563 goto success;
564 } else if (ret != LY_ENOT) {
565 goto error;
566 }
567 }
568
569 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100570 LOGVAL(ctx, LYVE_XPATH, "No module connected with the prefix \"%.*s\" found (prefix format %s).",
Radek Krejci422afb12021-03-04 16:38:16 +0100571 (int)len, pref, ly_format2str(format));
Michal Vasko825a0442021-04-16 16:11:53 +0200572 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 goto error;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200574 } else if (!mod->implemented) {
575 LOGVAL(ctx, LYVE_XPATH, "Not implemented module \"%s\" in path.", mod->name);
Michal Vaskoed725d72021-06-23 12:03:45 +0200576 ret = LY_EVALID;
577 goto error;
Michal Vasko004d3152020-06-11 19:59:22 +0200578 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100579
Michal Vasko7a266772024-01-23 11:02:38 +0100580 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200581 } else {
582 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200583 case LY_VALUE_SCHEMA:
584 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200585 if (!cur_mod) {
586 LOGINT_RET(ctx);
587 }
588 /* use current module */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200589 mod = cur_mod;
Michal Vasko004d3152020-06-11 19:59:22 +0200590 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200591 case LY_VALUE_JSON:
Michal Vasko79228af2021-08-26 14:44:28 +0200592 case LY_VALUE_LYB:
Michal Vasko004d3152020-06-11 19:59:22 +0200593 if (!prev_ctx_node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200594 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200595 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200596 /* inherit module of the previous node */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200597 mod = prev_ctx_node->module;
Michal Vasko004d3152020-06-11 19:59:22 +0200598 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200599 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200600 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100601 case LY_VALUE_STR_NS:
Radek Krejcif9943642021-04-26 10:18:21 +0200602 /* not really defined or accepted */
Michal Vasko00cbf532020-06-15 13:58:47 +0200603 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200604 }
605 }
606
Michal Vasko8cc3f662022-03-29 11:25:51 +0200607 /* find schema node */
608 if (!prev_ctx_node && top_ext) {
609 *snode = lysc_ext_find_node(top_ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko004d3152020-06-11 19:59:22 +0200610 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200611 *snode = lys_find_child(prev_ctx_node, mod, name, name_len, 0, getnext_opts);
612 if (!(*snode) && prev_ctx_node) {
613 ret = ly_nested_ext_schema(NULL, prev_ctx_node, pref, len, format, prefix_data, name, name_len, snode, &e);
614 LY_CHECK_RET(ret && (ret != LY_ENOT), ret);
615 }
616 }
617 if (!(*snode)) {
618 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
619 return LY_ENOTFOUND;
Michal Vasko004d3152020-06-11 19:59:22 +0200620 }
621
Michal Vasko8cc3f662022-03-29 11:25:51 +0200622success:
623 if (ext) {
624 *ext = e;
625 }
Michal Vasko004d3152020-06-11 19:59:22 +0200626 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100627
628error:
Michal Vasko7a266772024-01-23 11:02:38 +0100629 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Michal Vasko825a0442021-04-16 16:11:53 +0200630 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200631}
632
633LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200634ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Michal Vaskodd528af2022-08-08 14:35:07 +0200635 const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint32_t *tok_idx, LY_VALUE_FORMAT format,
Michal Vasko90189962023-02-28 12:10:34 +0100636 void *prefix_data, struct ly_path_predicate **predicates)
Michal Vasko004d3152020-06-11 19:59:22 +0200637{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100638 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +0200639 struct ly_path_predicate *p;
640 const struct lysc_node *key;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200641 const char *val;
642 size_t val_len, key_count;
Michal Vasko004d3152020-06-11 19:59:22 +0200643
Michal Vasko00cbf532020-06-15 13:58:47 +0200644 assert(ctx && ctx_node);
645
Michal Vasko7a266772024-01-23 11:02:38 +0100646 if (cur_node) {
647 LOG_LOCSET(cur_node, NULL);
648 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100649
Michal Vasko90189962023-02-28 12:10:34 +0100650 *predicates = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200651
Michal Vasko004d3152020-06-11 19:59:22 +0200652 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200653 /* '[', no predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 goto cleanup; /* LY_SUCCESS */
Michal Vasko004d3152020-06-11 19:59:22 +0200655 }
656
657 if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) {
658 if (ctx_node->nodetype != LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100659 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200660 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100661 ret = LY_EVALID;
662 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200663 } else if (ctx_node->flags & LYS_KEYLESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100664 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200665 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100666 ret = LY_EVALID;
667 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200668 }
669
670 do {
671 /* NameTest, find the key */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200672 LY_CHECK_RET(ly_path_compile_snode(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, format, prefix_data,
673 NULL, 0, &key, NULL));
674 if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100675 LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", lys_nodetype2str(key->nodetype),
676 key->name);
677 ret = LY_EVALID;
678 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200679 }
680 ++(*tok_idx);
681
Michal Vasko90189962023-02-28 12:10:34 +0100682 /* new predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100683 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200684 p->key = key;
685
686 /* '=' */
687 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
688 ++(*tok_idx);
689
Michal Vasko90189962023-02-28 12:10:34 +0100690 /* Literal, Number, or VariableReference */
691 if (expr->tokens[*tok_idx] == LYXP_TOKEN_VARREF) {
692 /* store the variable name */
693 p->variable = strndup(expr->expr + expr->tok_pos[*tok_idx], expr->tok_len[*tok_idx]);
694 LY_CHECK_ERR_GOTO(!p->variable, LOGMEM(ctx); ret = LY_EMEM, cleanup);
695
696 p->type = LY_PATH_PREDTYPE_LIST_VAR;
697 ++(*tok_idx);
Michal Vasko4911eeb2021-06-28 11:23:05 +0200698 } else {
Michal Vasko90189962023-02-28 12:10:34 +0100699 if (expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL) {
700 /* skip quotes */
701 val = expr->expr + expr->tok_pos[*tok_idx] + 1;
702 val_len = expr->tok_len[*tok_idx] - 2;
703 } else {
704 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER);
705 val = expr->expr + expr->tok_pos[*tok_idx];
706 val_len = expr->tok_len[*tok_idx];
707 }
708
709 /* store the value */
Michal Vasko7a266772024-01-23 11:02:38 +0100710 if (key) {
711 LOG_LOCSET(key, NULL);
712 }
Michal Vasko989cdb42023-10-06 15:32:37 +0200713 ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type, val, val_len, 0, NULL,
714 format, prefix_data, LYD_HINT_DATA, key, NULL);
Michal Vasko7a266772024-01-23 11:02:38 +0100715 LOG_LOCBACK(key ? 1 : 0, 0);
Michal Vasko90189962023-02-28 12:10:34 +0100716 LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup);
717
718 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
719 LY_ATOMIC_INC_BARRIER(((struct lysc_type *)p->value.realtype)->refcount);
720
721 p->type = LY_PATH_PREDTYPE_LIST;
722 ++(*tok_idx);
Michal Vasko4911eeb2021-06-28 11:23:05 +0200723 }
724
Michal Vasko004d3152020-06-11 19:59:22 +0200725 /* ']' */
726 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
727 ++(*tok_idx);
728
729 /* another predicate follows? */
730 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
731
732 /* check that all keys were set */
733 key_count = 0;
Michal Vasko544e58a2021-01-28 14:33:41 +0100734 for (key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +0200735 ++key_count;
736 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200737 if (LY_ARRAY_COUNT(*predicates) != key_count) {
Michal Vasko004d3152020-06-11 19:59:22 +0200738 /* names (keys) are unique - it was checked when parsing */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100739 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200740 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Michal Vasko90189962023-02-28 12:10:34 +0100741 ly_path_predicates_free(ctx, *predicates);
Michal Vasko004d3152020-06-11 19:59:22 +0200742 *predicates = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100743 ret = LY_EVALID;
744 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200745 }
746
747 } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) {
748 if (ctx_node->nodetype != LYS_LEAFLIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100749 LOGVAL(ctx, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200750 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100751 ret = LY_EVALID;
752 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200753 }
754 ++(*tok_idx);
755
756 /* new predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100757 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko90189962023-02-28 12:10:34 +0100758 p->type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko004d3152020-06-11 19:59:22 +0200759
760 /* '=' */
761 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
762 ++(*tok_idx);
763
Michal Vasko4911eeb2021-06-28 11:23:05 +0200764 /* Literal or Number */
765 assert((expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL) || (expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER));
766 if (expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL) {
767 /* skip quotes */
768 val = expr->expr + expr->tok_pos[*tok_idx] + 1;
769 val_len = expr->tok_len[*tok_idx] - 2;
770 } else {
771 val = expr->expr + expr->tok_pos[*tok_idx];
772 val_len = expr->tok_len[*tok_idx];
773 }
774
Michal Vasko004d3152020-06-11 19:59:22 +0200775 /* store the value */
Michal Vasko7a266772024-01-23 11:02:38 +0100776 if (ctx_node) {
777 LOG_LOCSET(ctx_node, NULL);
778 }
Michal Vasko989cdb42023-10-06 15:32:37 +0200779 ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type, val, val_len, 0, NULL,
780 format, prefix_data, LYD_HINT_DATA, ctx_node, NULL);
Michal Vasko7a266772024-01-23 11:02:38 +0100781 LOG_LOCBACK(ctx_node ? 1 : 0, 0);
Michal Vasko55b84812021-05-11 09:23:58 +0200782 LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200783 ++(*tok_idx);
784
Michal Vaskoae875662020-10-21 10:33:17 +0200785 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
Michal Vasko04338d92021-09-01 07:58:14 +0200786 LY_ATOMIC_INC_BARRIER(((struct lysc_type *)p->value.realtype)->refcount);
Michal Vaskoae875662020-10-21 10:33:17 +0200787
Michal Vasko004d3152020-06-11 19:59:22 +0200788 /* ']' */
789 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
790 ++(*tok_idx);
791 } else {
792 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER);
793 if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100794 ret = LY_EVALID;
795 LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200796 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100797 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200798 } else if (ctx_node->flags & LYS_CONFIG_W) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100799 ret = LY_EVALID;
800 LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for configuration %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200801 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100802 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200803 }
Michal Vasko004d3152020-06-11 19:59:22 +0200804
805 /* new predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100806 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko90189962023-02-28 12:10:34 +0100807 p->type = LY_PATH_PREDTYPE_POSITION;
Michal Vasko004d3152020-06-11 19:59:22 +0200808
809 /* syntax was already checked */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200810 p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&val, LY_BASE_DEC);
Michal Vasko00cbf532020-06-15 13:58:47 +0200811 ++(*tok_idx);
Michal Vasko004d3152020-06-11 19:59:22 +0200812
813 /* ']' */
814 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
815 ++(*tok_idx);
816 }
817
Radek Krejci2efc45b2020-12-22 16:25:44 +0100818cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100819 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100820 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200821}
822
823/**
824 * @brief Compile leafref predicate. Actually, it is only checked.
825 *
826 * @param[in] ctx_node Context node, node for which the predicate is defined.
827 * @param[in] cur_node Current (original context) node.
828 * @param[in] expr Parsed path.
829 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
Michal Vasko004d3152020-06-11 19:59:22 +0200830 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200831 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200832 * @return LY_ERR value.
833 */
834static LY_ERR
835ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node,
Michal Vaskodd528af2022-08-08 14:35:07 +0200836 const struct lyxp_expr *expr, uint32_t *tok_idx, LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko004d3152020-06-11 19:59:22 +0200837{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100838 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +0200839 const struct lysc_node *key, *node, *node2;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100840 struct ly_ctx *ctx = cur_node->module->ctx;
841
Michal Vasko004d3152020-06-11 19:59:22 +0200842 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200843 /* '[', no predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100844 goto cleanup; /* LY_SUCCESS */
Michal Vasko004d3152020-06-11 19:59:22 +0200845 }
846
847 if (ctx_node->nodetype != LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100848 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200849 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100850 ret = LY_EVALID;
851 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200852 } else if (ctx_node->flags & LYS_KEYLESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100853 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200854 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100855 ret = LY_EVALID;
856 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200857 }
858
859 do {
860 /* NameTest, find the key */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200861 ret = ly_path_compile_snode(ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx, format, prefix_data,
862 NULL, 0, &key, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100863 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200864 if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100865 LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200866 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100867 ret = LY_EVALID;
868 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200869 }
870 ++(*tok_idx);
871
872 /* we are not actually compiling, throw the key away */
873 (void)key;
874
875 /* '=' */
876 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
877 ++(*tok_idx);
878
879 /* FuncName */
880 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME);
881 ++(*tok_idx);
882
883 /* evaluating from the "current()" node */
884 node = cur_node;
885
886 /* '(' */
887 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
888 ++(*tok_idx);
889
890 /* ')' */
891 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
892 ++(*tok_idx);
893
894 do {
895 /* '/' */
896 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
897 ++(*tok_idx);
898
899 /* go to parent */
900 if (!node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100901 LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path.");
902 ret = LY_EVALID;
903 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200904 }
905 node = lysc_data_parent(node);
906
907 /* '..' */
908 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT);
909 ++(*tok_idx);
910 } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT);
911
912 do {
913 /* '/' */
914 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
915 ++(*tok_idx);
916
917 /* NameTest */
918 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200919 LY_CHECK_RET(ly_path_compile_snode(ctx, cur_node, cur_node->module, node, expr, *tok_idx, format,
920 prefix_data, NULL, 0, &node2, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +0200921 node = node2;
922 ++(*tok_idx);
923 } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST));
924
925 /* check the last target node */
926 if (node->nodetype != LYS_LEAF) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100927 LOGVAL(ctx, LYVE_XPATH, "Leaf expected instead of %s \"%s\" in leafref predicate in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200928 lys_nodetype2str(node->nodetype), node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100929 ret = LY_EVALID;
930 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200931 }
932
933 /* we are not actually compiling, throw the rightside node away */
934 (void)node;
935
936 /* ']' */
937 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
938 ++(*tok_idx);
939
Radek Krejci0f969882020-08-21 16:56:47 +0200940 /* another predicate follows? */
Michal Vasko004d3152020-06-11 19:59:22 +0200941 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
942
Radek Krejci2efc45b2020-12-22 16:25:44 +0100943cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +0200944 return (ret == LY_ENOTFOUND) ? LY_EVALID : ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200945}
946
Michal Vaskoed725d72021-06-23 12:03:45 +0200947/**
stewegd8e2fc92023-05-31 09:52:56 +0200948 * @brief Duplicate ly_path_predicate structure.
949 *
950 * @param[in] ctx libyang context.
951 * @param[in] pred The array of path predicates.
952 * @param[out] dup Duplicated predicates.
953 * @return LY_ERR value.
954 */
955static LY_ERR
956ly_path_dup_predicates(const struct ly_ctx *ctx, const struct ly_path_predicate *pred, struct ly_path_predicate **dup)
957{
958 LY_ARRAY_COUNT_TYPE u;
959
960 if (!pred) {
961 return LY_SUCCESS;
962 }
963
964 LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(pred), LY_EMEM);
965 LY_ARRAY_FOR(pred, u) {
966 LY_ARRAY_INCREMENT(*dup);
967 (*dup)[u].type = pred->type;
968
Michal Vasko5339b4f2023-09-29 14:38:08 +0200969 switch (pred[u].type) {
stewegd8e2fc92023-05-31 09:52:56 +0200970 case LY_PATH_PREDTYPE_POSITION:
971 /* position-predicate */
Michal Vasko5339b4f2023-09-29 14:38:08 +0200972 (*dup)[u].position = pred[u].position;
stewegd8e2fc92023-05-31 09:52:56 +0200973 break;
974 case LY_PATH_PREDTYPE_LIST:
975 case LY_PATH_PREDTYPE_LEAFLIST:
976 /* key-predicate or leaf-list-predicate */
Michal Vasko5339b4f2023-09-29 14:38:08 +0200977 (*dup)[u].key = pred[u].key;
978 pred[u].value.realtype->plugin->duplicate(ctx, &pred[u].value, &(*dup)[u].value);
979 LY_ATOMIC_INC_BARRIER(((struct lysc_type *)pred[u].value.realtype)->refcount);
stewegd8e2fc92023-05-31 09:52:56 +0200980 break;
981 case LY_PATH_PREDTYPE_LIST_VAR:
982 /* key-predicate with a variable */
Michal Vasko5339b4f2023-09-29 14:38:08 +0200983 (*dup)[u].key = pred[u].key;
984 (*dup)[u].variable = strdup(pred[u].variable);
stewegd8e2fc92023-05-31 09:52:56 +0200985 break;
986 }
987 }
988
989 return LY_SUCCESS;
990}
991
992/**
993 * @brief Appends path elements from source to destination array
994 *
995 * @param[in] ctx libyang context.
996 * @param[in] src The source path
997 * @param[in,out] dst The destination path
998 * @return LY_ERR value.
999 */
1000static LY_ERR
1001ly_path_append(const struct ly_ctx *ctx, const struct ly_path *src, struct ly_path **dst)
1002{
1003 LY_ERR ret = LY_SUCCESS;
1004 LY_ARRAY_COUNT_TYPE u;
1005 struct ly_path *p;
1006
1007 if (!src) {
1008 return LY_SUCCESS;
1009 }
1010
1011 LY_ARRAY_CREATE_RET(ctx, *dst, LY_ARRAY_COUNT(src), LY_EMEM);
1012 LY_ARRAY_FOR(src, u) {
Michal Vasko696915b2023-05-31 10:12:11 +02001013 LY_ARRAY_NEW_GOTO(ctx, *dst, p, ret, cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001014 p->node = src[u].node;
1015 p->ext = src[u].ext;
Michal Vasko696915b2023-05-31 10:12:11 +02001016 LY_CHECK_GOTO(ret = ly_path_dup_predicates(ctx, src[u].predicates, &p->predicates), cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001017 }
1018
Michal Vasko696915b2023-05-31 10:12:11 +02001019cleanup:
1020 return ret;
stewegd8e2fc92023-05-31 09:52:56 +02001021}
1022
1023/**
1024 * @brief Compile deref XPath function into ly_path structure.
1025 *
1026 * @param[in] ctx libyang context.
1027 * @param[in] ctx_node Optional context node, mandatory of @p lref.
1028 * @param[in] top_ext Extension instance containing the definition of the data being created. It is used to find
1029 * the top-level node inside the extension instance instead of a module. Note that this is the case not only if
1030 * the @p ctx_node is NULL, but also if the relative path starting in @p ctx_node reaches the document root
1031 * via double dots.
1032 * @param[in] expr Parsed path.
1033 * @param[in] oper Oper option (@ref path_oper_options).
1034 * @param[in] target Target option (@ref path_target_options).
1035 * @param[in] format Format of the path.
1036 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
1037 * @param[in,out] tok_idx Index in @p exp, is adjusted.
1038 * @param[out] path Compiled path.
1039 * @return LY_ERR value.
1040 */
1041static LY_ERR
1042ly_path_compile_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node,
1043 const struct lysc_ext_instance *top_ext, const struct lyxp_expr *expr, uint16_t oper, uint16_t target,
1044 LY_VALUE_FORMAT format, void *prefix_data, uint32_t *tok_idx, struct ly_path **path)
1045{
1046 LY_ERR ret = LY_SUCCESS;
1047 struct lyxp_expr expr2;
1048 struct ly_path *path2 = NULL;
1049 const struct lysc_node *node2;
1050 const struct lysc_node_leaf *deref_leaf_node;
1051 const struct lysc_type_leafref *lref;
1052 uint32_t begin_token;
1053
Michal Vasko696915b2023-05-31 10:12:11 +02001054 *path = NULL;
1055
stewegd8e2fc92023-05-31 09:52:56 +02001056 /* properly parsed path must always starts with 'deref' and '(' */
1057 assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_FUNCNAME));
1058 assert(!strncmp(&expr->expr[expr->tok_pos[*tok_idx]], "deref", 5));
1059 (*tok_idx)++;
1060 assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR1));
1061 (*tok_idx)++;
1062 begin_token = *tok_idx;
1063
1064 /* emebedded functions were already identified count tokens till ')' */
1065 while (lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR2) && (*tok_idx < expr->used)) {
1066 (*tok_idx)++;
1067 }
1068
1069 /* properly parsed path must have ')' within the tokens */
1070 assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR2));
1071
1072 /* prepare expr representing just deref arg */
1073 expr2.tokens = &expr->tokens[begin_token];
1074 expr2.tok_pos = &expr->tok_pos[begin_token];
1075 expr2.tok_len = &expr->tok_len[begin_token];
1076 expr2.repeat = &expr->repeat[begin_token];
1077 expr2.used = *tok_idx - begin_token;
1078 expr2.size = expr->size - begin_token;
1079 expr2.expr = expr->expr;
1080
1081 /* compile just deref arg, append it to the path and find dereferenced lref for next operations */
Michal Vasko696915b2023-05-31 10:12:11 +02001082 LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, ctx_node, top_ext, &expr2, oper, target, format, prefix_data,
1083 &path2), cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001084 node2 = path2[LY_ARRAY_COUNT(path2) - 1].node;
steweg1ac8b412023-10-31 13:25:33 +01001085 if ((node2->nodetype != LYS_LEAF) && (node2->nodetype != LYS_LEAFLIST)) {
1086 LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not leaf nor leaflist", node2->name);
1087 ret = LY_EVALID;
1088 goto cleanup;
1089 }
stewegd8e2fc92023-05-31 09:52:56 +02001090 deref_leaf_node = (const struct lysc_node_leaf *)node2;
steweg1ac8b412023-10-31 13:25:33 +01001091 if (deref_leaf_node->type->basetype != LY_TYPE_LEAFREF) {
1092 LOGVAL(ctx, LYVE_XPATH, "The deref function target node \"%s\" is not leafref", node2->name);
1093 ret = LY_EVALID;
1094 goto cleanup;
1095 }
stewegd8e2fc92023-05-31 09:52:56 +02001096 lref = (const struct lysc_type_leafref *)deref_leaf_node->type;
Michal Vasko696915b2023-05-31 10:12:11 +02001097 LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001098 ly_path_free(ctx, path2);
1099 path2 = NULL;
1100
1101 /* compile dereferenced leafref expression and append it to the path */
Michal Vasko696915b2023-05-31 10:12:11 +02001102 LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, node2, top_ext, lref->path, oper, target, format, prefix_data,
1103 &path2), cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001104 node2 = path2[LY_ARRAY_COUNT(path2) - 1].node;
Michal Vasko696915b2023-05-31 10:12:11 +02001105 LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001106 ly_path_free(ctx, path2);
1107 path2 = NULL;
1108
1109 /* properly parsed path must always continue with ')' and '/' */
1110 assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR2));
1111 (*tok_idx)++;
1112 assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_OPER_PATH));
1113 (*tok_idx)++;
1114
1115 /* prepare expr representing rest of the path after deref */
1116 expr2.tokens = &expr->tokens[*tok_idx];
1117 expr2.tok_pos = &expr->tok_pos[*tok_idx];
1118 expr2.tok_len = &expr->tok_len[*tok_idx];
1119 expr2.repeat = &expr->repeat[*tok_idx];
1120 expr2.used = expr->used - *tok_idx;
1121 expr2.size = expr->size - *tok_idx;
1122 expr2.expr = expr->expr;
1123
1124 /* compile rest of the path and append it to the path */
Michal Vasko696915b2023-05-31 10:12:11 +02001125 LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, node2, top_ext, &expr2, oper, target, format, prefix_data, &path2),
1126 cleanup);
1127 LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup);
stewegd8e2fc92023-05-31 09:52:56 +02001128
1129cleanup:
1130 ly_path_free(ctx, path2);
1131 if (ret) {
1132 ly_path_free(ctx, *path);
1133 *path = NULL;
1134 }
Michal Vasko696915b2023-05-31 10:12:11 +02001135 return ret;
stewegd8e2fc92023-05-31 09:52:56 +02001136}
1137
1138/**
Michal Vaskoed725d72021-06-23 12:03:45 +02001139 * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled.
1140 *
1141 * @param[in] ctx libyang context.
1142 * @param[in] cur_mod Current module of the path (where it was "instantiated"), ignored of @p lref. Used for nodes
Radek Krejci84d7fd72021-07-14 18:32:21 +02001143 * without a prefix for ::LY_VALUE_SCHEMA and ::LY_VALUE_SCHEMA_RESOLVED format.
Michal Vaskoed725d72021-06-23 12:03:45 +02001144 * @param[in] ctx_node Optional context node, mandatory of @p lref.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001145 * @param[in] top_ext Extension instance containing the definition of the data being created. It is used to find the top-level
Michal Vaskoed725d72021-06-23 12:03:45 +02001146 * node inside the extension instance instead of a module. Note that this is the case not only if the @p ctx_node is NULL,
1147 * but also if the relative path starting in @p ctx_node reaches the document root via double dots.
1148 * @param[in] expr Parsed path.
1149 * @param[in] lref Whether leafref is being compiled or not.
1150 * @param[in] oper Oper option (@ref path_oper_options).
1151 * @param[in] target Target option (@ref path_target_options).
Michal Vasko0884d212021-10-14 09:21:46 +02001152 * @param[in] limit_access_tree Whether to limit accessible tree as described in
1153 * [XPath context](https://datatracker.ietf.org/doc/html/rfc7950#section-6.4.1).
Michal Vaskoed725d72021-06-23 12:03:45 +02001154 * @param[in] format Format of the path.
1155 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskoed725d72021-06-23 12:03:45 +02001156 * @param[out] path Compiled path.
1157 * @return LY_ERECOMPILE, only if @p lref.
1158 * @return LY_ERR value.
1159 */
1160static LY_ERR
1161_ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
Michal Vasko32ca49b2023-02-17 15:11:35 +01001162 const struct lysc_ext_instance *top_ext, const struct lyxp_expr *expr, ly_bool lref, uint16_t oper, uint16_t target,
Michal Vasko0884d212021-10-14 09:21:46 +02001163 ly_bool limit_access_tree, LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path)
Michal Vasko004d3152020-06-11 19:59:22 +02001164{
1165 LY_ERR ret = LY_SUCCESS;
Michal Vaskodd528af2022-08-08 14:35:07 +02001166 uint32_t tok_idx = 0, getnext_opts;
Michal Vasko6b26e742020-07-17 15:02:10 +02001167 const struct lysc_node *node2, *cur_node, *op;
Michal Vasko00cbf532020-06-15 13:58:47 +02001168 struct ly_path *p = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001169 struct lysc_ext_instance *ext = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02001170
Michal Vasko00cbf532020-06-15 13:58:47 +02001171 assert(ctx);
Michal Vaskoed725d72021-06-23 12:03:45 +02001172 assert(!lref || ctx_node);
Michal Vasko00cbf532020-06-15 13:58:47 +02001173 assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT));
1174 assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY));
Michal Vasko004d3152020-06-11 19:59:22 +02001175
Michal Vasko0884d212021-10-14 09:21:46 +02001176 if (!limit_access_tree) {
1177 op = NULL;
1178 } else {
1179 /* find operation, if we are in any */
1180 for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
1181 }
Michal Vasko6b26e742020-07-17 15:02:10 +02001182
Radek Krejci2efc45b2020-12-22 16:25:44 +01001183 *path = NULL;
1184
Michal Vasko6b26e742020-07-17 15:02:10 +02001185 /* remember original context node */
1186 cur_node = ctx_node;
Michal Vasko7a266772024-01-23 11:02:38 +01001187 if (cur_node) {
1188 LOG_LOCSET(cur_node, NULL);
1189 }
Michal Vasko004d3152020-06-11 19:59:22 +02001190
Michal Vasko00cbf532020-06-15 13:58:47 +02001191 if (oper == LY_PATH_OPER_OUTPUT) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001192 getnext_opts = LYS_GETNEXT_OUTPUT;
Michal Vasko00cbf532020-06-15 13:58:47 +02001193 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001194 getnext_opts = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +02001195 }
1196
stewegd8e2fc92023-05-31 09:52:56 +02001197 if (lref && (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_EXTENDED) &&
1198 (expr->tokens[tok_idx] == LYXP_TOKEN_FUNCNAME)) {
1199 /* deref function */
1200 ret = ly_path_compile_deref(ctx, ctx_node, top_ext, expr, oper, target, format, prefix_data, &tok_idx, path);
stewegd8e2fc92023-05-31 09:52:56 +02001201 goto cleanup;
1202 } else if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko004d3152020-06-11 19:59:22 +02001203 /* absolute path */
1204 ctx_node = NULL;
1205
1206 ++tok_idx;
1207 } else {
1208 /* relative path */
Michal Vasko80239792021-11-02 11:48:32 +01001209 if (!ctx_node) {
1210 LOGVAL(ctx, LYVE_XPATH, "No initial schema parent for a relative path.");
1211 ret = LY_EVALID;
1212 goto cleanup;
1213 }
1214
1215 /* go up the parents for leafref */
Michal Vaskoed725d72021-06-23 12:03:45 +02001216 while (lref && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) {
Michal Vasko004d3152020-06-11 19:59:22 +02001217 if (!ctx_node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001218 LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path.");
Michal Vasko14424ba2020-12-09 18:09:51 +01001219 ret = LY_EVALID;
1220 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02001221 }
1222
1223 /* get parent */
1224 ctx_node = lysc_data_parent(ctx_node);
1225
1226 ++tok_idx;
1227
1228 assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH);
1229 ++tok_idx;
1230 }
Michal Vasko004d3152020-06-11 19:59:22 +02001231 }
1232
1233 do {
Michal Vasko00cbf532020-06-15 13:58:47 +02001234 /* check last compiled inner node, whether it is uniquely identified (even key-less list) */
Michal Vaskoed725d72021-06-23 12:03:45 +02001235 if (p && !lref && (target == LY_PATH_TARGET_SINGLE) && (p->node->nodetype == LYS_LIST) && !p->predicates) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001236 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +02001237 lys_nodetype2str(p->node->nodetype), p->node->name);
Michal Vasko14424ba2020-12-09 18:09:51 +01001238 ret = LY_EVALID;
1239 goto cleanup;
Michal Vasko00cbf532020-06-15 13:58:47 +02001240 }
1241
Michal Vasko14424ba2020-12-09 18:09:51 +01001242 /* NameTest */
1243 LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, expr, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, cleanup);
1244
Michal Vasko8cc3f662022-03-29 11:25:51 +02001245 /* get schema node */
1246 LY_CHECK_GOTO(ret = ly_path_compile_snode(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, format, prefix_data,
1247 top_ext, getnext_opts, &node2, &ext), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02001248 ++tok_idx;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001249 if ((op && (node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) {
1250 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%s\" in path.", node2->name);
Radek Krejci8de005f2020-06-25 17:02:07 +02001251 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02001252 goto cleanup;
1253 }
1254 ctx_node = node2;
1255
1256 /* new path segment */
Michal Vasko00cbf532020-06-15 13:58:47 +02001257 LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02001258 p->node = ctx_node;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001259 p->ext = ext;
Michal Vasko004d3152020-06-11 19:59:22 +02001260
1261 /* compile any predicates */
Michal Vaskoed725d72021-06-23 12:03:45 +02001262 if (lref) {
Michal Vasko24fc4d12021-07-12 14:41:20 +02001263 ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +02001264 } else {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001265 ret = ly_path_compile_predicate(ctx, cur_node, cur_mod, ctx_node, expr, &tok_idx, format, prefix_data,
Michal Vasko90189962023-02-28 12:10:34 +01001266 &p->predicates);
Michal Vasko004d3152020-06-11 19:59:22 +02001267 }
1268 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02001269 } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH));
1270
Michal Vasko14424ba2020-12-09 18:09:51 +01001271 /* check leftover tokens */
1272 if (tok_idx < expr->used) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001273 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(expr->tokens[tok_idx]), &expr->expr[expr->tok_pos[tok_idx]]);
Michal Vasko14424ba2020-12-09 18:09:51 +01001274 ret = LY_EVALID;
1275 goto cleanup;
1276 }
1277
Michal Vasko00cbf532020-06-15 13:58:47 +02001278 /* check last compiled node */
Michal Vaskoed725d72021-06-23 12:03:45 +02001279 if (!lref && (target == LY_PATH_TARGET_SINGLE) && (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001280 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +02001281 lys_nodetype2str(p->node->nodetype), p->node->name);
Michal Vasko14424ba2020-12-09 18:09:51 +01001282 ret = LY_EVALID;
1283 goto cleanup;
Michal Vasko00cbf532020-06-15 13:58:47 +02001284 }
1285
Michal Vasko004d3152020-06-11 19:59:22 +02001286cleanup:
1287 if (ret) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001288 ly_path_free(ctx, *path);
Michal Vasko004d3152020-06-11 19:59:22 +02001289 *path = NULL;
1290 }
Michal Vasko7a266772024-01-23 11:02:38 +01001291 LOG_LOCBACK(cur_node ? 1 : 0, 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001292 return (ret == LY_ENOTFOUND) ? LY_EVALID : ret;
Michal Vasko004d3152020-06-11 19:59:22 +02001293}
1294
1295LY_ERR
Michal Vaskoed725d72021-06-23 12:03:45 +02001296ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
Michal Vasko32ca49b2023-02-17 15:11:35 +01001297 const struct lysc_ext_instance *top_ext, const struct lyxp_expr *expr, uint16_t oper, uint16_t target,
Michal Vasko0884d212021-10-14 09:21:46 +02001298 ly_bool limit_access_tree, LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path)
Michal Vaskoed725d72021-06-23 12:03:45 +02001299{
Michal Vasko8cc3f662022-03-29 11:25:51 +02001300 return _ly_path_compile(ctx, cur_mod, ctx_node, top_ext, expr, 0, oper, target, limit_access_tree, format,
1301 prefix_data, path);
Michal Vaskoed725d72021-06-23 12:03:45 +02001302}
1303
1304LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001305ly_path_compile_leafref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const struct lysc_ext_instance *top_ext,
Michal Vasko32ca49b2023-02-17 15:11:35 +01001306 const struct lyxp_expr *expr, uint16_t oper, uint16_t target, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko24fc4d12021-07-12 14:41:20 +02001307 struct ly_path **path)
Michal Vaskoed725d72021-06-23 12:03:45 +02001308{
Michal Vasko8cc3f662022-03-29 11:25:51 +02001309 return _ly_path_compile(ctx, ctx_node->module, ctx_node, top_ext, expr, 1, oper, target, 1, format, prefix_data, path);
Michal Vaskoed725d72021-06-23 12:03:45 +02001310}
1311
1312LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01001313ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, const struct lyxp_var *vars,
Michal Vasko838829d2023-10-09 16:06:43 +02001314 ly_bool with_opaq, LY_ARRAY_COUNT_TYPE *path_idx, struct lyd_node **match)
Michal Vasko004d3152020-06-11 19:59:22 +02001315{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001316 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoe43e21f2022-06-07 12:26:36 +02001317 struct lyd_node *prev_node = NULL, *elem, *node = NULL, *target;
Michal Vasko004d3152020-06-11 19:59:22 +02001318 uint64_t pos;
1319
1320 assert(path && start);
1321
1322 if (lysc_data_parent(path[0].node)) {
1323 /* relative path, start from the parent children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001324 start = lyd_child(start);
Michal Vasko004d3152020-06-11 19:59:22 +02001325 } else {
1326 /* absolute path, start from the first top-level sibling */
1327 while (start->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +01001328 start = lyd_parent(start);
Michal Vasko004d3152020-06-11 19:59:22 +02001329 }
1330 while (start->prev->next) {
1331 start = start->prev;
1332 }
1333 }
1334
1335 LY_ARRAY_FOR(path, u) {
Michal Vasko90189962023-02-28 12:10:34 +01001336 if (path[u].predicates) {
1337 switch (path[u].predicates[0].type) {
1338 case LY_PATH_PREDTYPE_POSITION:
1339 /* we cannot use hashes and want an instance on a specific position */
1340 pos = 1;
1341 node = NULL;
1342 LYD_LIST_FOR_INST(start, path[u].node, elem) {
1343 if (pos == path[u].predicates[0].position) {
1344 node = elem;
1345 break;
1346 }
1347 ++pos;
Michal Vasko004d3152020-06-11 19:59:22 +02001348 }
Michal Vasko90189962023-02-28 12:10:34 +01001349 break;
1350 case LY_PATH_PREDTYPE_LEAFLIST:
1351 /* we will use hashes to find one leaf-list instance */
1352 LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target));
1353 lyd_find_sibling_first(start, target, &node);
1354 lyd_free_tree(target);
1355 break;
1356 case LY_PATH_PREDTYPE_LIST_VAR:
1357 case LY_PATH_PREDTYPE_LIST:
1358 /* we will use hashes to find one list instance */
1359 LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, vars, &target));
1360 lyd_find_sibling_first(start, target, &node);
1361 lyd_free_tree(target);
1362 break;
Michal Vasko004d3152020-06-11 19:59:22 +02001363 }
Michal Vasko90189962023-02-28 12:10:34 +01001364 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001365 /* we will use hashes to find one any/container/leaf instance */
Michal Vasko838829d2023-10-09 16:06:43 +02001366 if (lyd_find_sibling_val(start, path[u].node, NULL, 0, &node) && with_opaq) {
1367 if (!lyd_find_sibling_opaq_next(start, path[u].node->name, &node) &&
1368 (lyd_node_module(node) != path[u].node->module)) {
1369 /* non-matching opaque node */
1370 node = NULL;
1371 }
1372 }
Michal Vasko004d3152020-06-11 19:59:22 +02001373 }
1374
1375 if (!node) {
1376 /* no matching nodes */
1377 break;
1378 }
1379
Michal Vasko00cbf532020-06-15 13:58:47 +02001380 /* rememeber previous node */
1381 prev_node = node;
1382
Michal Vasko004d3152020-06-11 19:59:22 +02001383 /* next path segment, if any */
Radek Krejcia1c1e542020-09-29 16:06:52 +02001384 start = lyd_child(node);
Michal Vasko004d3152020-06-11 19:59:22 +02001385 }
1386
Michal Vasko004d3152020-06-11 19:59:22 +02001387 if (node) {
Michal Vasko00cbf532020-06-15 13:58:47 +02001388 /* we have found the full path */
1389 if (path_idx) {
1390 *path_idx = u;
1391 }
1392 if (match) {
1393 *match = node;
1394 }
Michal Vasko004d3152020-06-11 19:59:22 +02001395 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +02001396
1397 } else if (prev_node) {
1398 /* we have found only some partial match */
1399 if (path_idx) {
1400 *path_idx = u - 1;
1401 }
1402 if (match) {
1403 *match = prev_node;
1404 }
1405 return LY_EINCOMPLETE;
1406 }
1407
1408 /* we have not found any nodes */
1409 if (path_idx) {
1410 *path_idx = 0;
1411 }
1412 if (match) {
1413 *match = NULL;
1414 }
1415 return LY_ENOTFOUND;
1416}
1417
1418LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01001419ly_path_eval(const struct ly_path *path, const struct lyd_node *start, const struct lyxp_var *vars, struct lyd_node **match)
Michal Vasko00cbf532020-06-15 13:58:47 +02001420{
1421 LY_ERR ret;
1422 struct lyd_node *m;
1423
Michal Vasko838829d2023-10-09 16:06:43 +02001424 ret = ly_path_eval_partial(path, start, vars, 0, NULL, &m);
Michal Vasko00cbf532020-06-15 13:58:47 +02001425
1426 if (ret == LY_SUCCESS) {
1427 /* last node was found */
1428 if (match) {
1429 *match = m;
1430 }
1431 return LY_SUCCESS;
1432 }
1433
1434 /* not a full match */
1435 if (match) {
1436 *match = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02001437 }
1438 return LY_ENOTFOUND;
1439}
1440
1441LY_ERR
1442ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup)
1443{
stewegd8e2fc92023-05-31 09:52:56 +02001444 LY_ERR ret = LY_SUCCESS;
1445 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001446
1447 if (!path) {
1448 return LY_SUCCESS;
1449 }
1450
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001451 LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02001452 LY_ARRAY_FOR(path, u) {
1453 LY_ARRAY_INCREMENT(*dup);
1454 (*dup)[u].node = path[u].node;
stewegd8e2fc92023-05-31 09:52:56 +02001455 (*dup)[u].ext = path[u].ext;
1456 LY_CHECK_RET(ret = ly_path_dup_predicates(ctx, path[u].predicates, &(*dup)[u].predicates), ret);
Michal Vasko004d3152020-06-11 19:59:22 +02001457 }
1458
1459 return LY_SUCCESS;
1460}
1461
1462void
Michal Vasko90189962023-02-28 12:10:34 +01001463ly_path_predicates_free(const struct ly_ctx *ctx, struct ly_path_predicate *predicates)
Michal Vasko004d3152020-06-11 19:59:22 +02001464{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001465 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoc636ea42022-09-16 10:20:31 +02001466 struct lysf_ctx fctx = {.ctx = (struct ly_ctx *)ctx};
Michal Vasko004d3152020-06-11 19:59:22 +02001467
1468 if (!predicates) {
1469 return;
1470 }
1471
1472 LY_ARRAY_FOR(predicates, u) {
Michal Vasko90189962023-02-28 12:10:34 +01001473 switch (predicates[u].type) {
Michal Vasko004d3152020-06-11 19:59:22 +02001474 case LY_PATH_PREDTYPE_POSITION:
Michal Vasko004d3152020-06-11 19:59:22 +02001475 /* nothing to free */
1476 break;
1477 case LY_PATH_PREDTYPE_LIST:
Michal Vasko004d3152020-06-11 19:59:22 +02001478 case LY_PATH_PREDTYPE_LEAFLIST:
Michal Vaskoae875662020-10-21 10:33:17 +02001479 if (predicates[u].value.realtype) {
1480 predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001481 lysc_type_free(&fctx, (struct lysc_type *)predicates[u].value.realtype);
Michal Vaskoae875662020-10-21 10:33:17 +02001482 }
Michal Vasko004d3152020-06-11 19:59:22 +02001483 break;
Michal Vasko90189962023-02-28 12:10:34 +01001484 case LY_PATH_PREDTYPE_LIST_VAR:
1485 free(predicates[u].variable);
1486 break;
Michal Vasko004d3152020-06-11 19:59:22 +02001487 }
1488 }
1489 LY_ARRAY_FREE(predicates);
1490}
1491
1492void
1493ly_path_free(const struct ly_ctx *ctx, struct ly_path *path)
1494{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001495 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001496
Michal Vasko55b84812021-05-11 09:23:58 +02001497 if (!path) {
1498 return;
1499 }
1500
Michal Vasko004d3152020-06-11 19:59:22 +02001501 LY_ARRAY_FOR(path, u) {
Michal Vasko90189962023-02-28 12:10:34 +01001502 ly_path_predicates_free(ctx, path[u].predicates);
Michal Vasko004d3152020-06-11 19:59:22 +02001503 }
1504 LY_ARRAY_FREE(path);
1505}