blob: 293c1e50fccef55f9a17a0badda11f9e0e3e4999 [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"
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,
Radek Krejci0f969882020-08-21 16:56:47 +020051 uint16_t *tok_idx, uint8_t prefix, uint8_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
Radek Krejciddace2c2021-01-08 11:30:56 +010059 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +010060
Michal Vasko004d3152020-06-11 19:59:22 +020061 if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +020062 /* '[' */
63
Michal Vasko69730152020-10-09 16:30:07 +020064 if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS)) &&
65 !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020066 ret = ly_set_new(&set);
67 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +020068
69 do {
70 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +020071 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +020072
73 /* check prefix based on the options */
74 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
75 if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010076 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020077 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020078 goto token_error;
Michal Vasko8b06a5e2020-08-06 12:13:08 +020079 } else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) {
Radek Krejci2efc45b2020-12-22 16:25:44 +010080 LOGVAL(ctx, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020081 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020082 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +020083 }
84 if (!name) {
85 name = exp->expr + exp->tok_pos[*tok_idx];
86 name_len = exp->tok_len[*tok_idx];
87 } else {
88 ++name;
89 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
90 }
91
92 /* check whether it was not already specified */
93 for (i = 0; i < set->count; ++i) {
94 /* all the keys must be from the same module so this comparison should be fine */
95 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Radek Krejci422afb12021-03-04 16:38:16 +010096 LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +020097 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +020098 }
99 }
100
101 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200102 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200103 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200104
105 /* NameTest */
106 ++(*tok_idx);
107
108 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200109 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200110
111 /* Literal */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200112 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200113
114 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200115 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200116
Radek Krejci0f969882020-08-21 16:56:47 +0200117 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200118 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
119
Michal Vasko004d3152020-06-11 19:59:22 +0200120 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200121 /* '.' */
122
Michal Vasko004d3152020-06-11 19:59:22 +0200123 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200124 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200125
126 /* Literal */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200127 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200128
129 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200130 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200131
Michal Vasko004d3152020-06-11 19:59:22 +0200132 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200133 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +0200134
135 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200136 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200137
138 } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
139 assert(prefix == LY_PATH_PREFIX_OPTIONAL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200140 ret = ly_set_new(&set);
141 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200142
143 do {
144 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200145 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200146
147 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
148 if (!name) {
149 name = exp->expr + exp->tok_pos[*tok_idx];
150 name_len = exp->tok_len[*tok_idx];
151 } else {
152 ++name;
153 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
154 }
155
156 /* check whether it was not already specified */
157 for (i = 0; i < set->count; ++i) {
158 /* all the keys must be from the same module so this comparison should be fine */
159 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Radek Krejci422afb12021-03-04 16:38:16 +0100160 LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200161 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200162 }
163 }
164
165 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200166 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200167 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200168
169 /* NameTest */
170 ++(*tok_idx);
171
172 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200173 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200174
175 /* FuncName */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200176 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), token_error);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100177 if ((exp->tok_len[*tok_idx] != ly_strlen_const("current")) ||
178 strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", ly_strlen_const("current"))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100179 LOGVAL(ctx, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200180 exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200181 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200182 }
183 ++(*tok_idx);
184
185 /* '(' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200186 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200187
188 /* ')' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200189 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200190
191 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200192 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200193
194 /* '..' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200195 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200196 do {
197 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200198 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200199 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT));
200
201 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200202 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200203
204 /* '/' */
205 while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) {
206 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200207 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200208 }
209
210 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200211 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200212
Radek Krejci0f969882020-08-21 16:56:47 +0200213 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200214 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
215
216 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100217 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 +0200218 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200219 }
220 }
221
Radek Krejciba03a5a2020-08-27 14:40:41 +0200222cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100223 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200224 ly_set_free(set, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200225 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200226
Radek Krejciba03a5a2020-08-27 14:40:41 +0200227token_error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100228 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200229 ly_set_free(set, NULL);
230 return LY_EVALID;
231}
232
233LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200234ly_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 +0200235 uint8_t begin, uint8_t lref, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200236{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200237 LY_ERR ret = LY_SUCCESS;
238 struct lyxp_expr *exp = NULL;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200239 uint16_t tok_idx, cur_len;
240 const char *cur_node, *prev_prefix = NULL, *ptr;
Michal Vasko004d3152020-06-11 19:59:22 +0200241
242 assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER));
243 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko69730152020-10-09 16:30:07 +0200244 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) ||
245 (prefix == LY_PATH_PREFIX_STRICT_INHERIT));
Michal Vasko004d3152020-06-11 19:59:22 +0200246 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
247
Radek Krejciddace2c2021-01-08 11:30:56 +0100248 LOG_LOCSET(ctx_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100249
Michal Vasko004d3152020-06-11 19:59:22 +0200250 /* parse as a generic XPath expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200251 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 1, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200252 tok_idx = 0;
253
254 if (begin == LY_PATH_BEGIN_EITHER) {
255 /* is the path relative? */
256 if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) {
Michal Vaskocb8c6d42020-10-16 11:58:30 +0200257 /* relative path check specific to leafref */
258 if (lref == LY_PATH_LREF_TRUE) {
259 /* mandatory '..' */
260 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error);
261
262 do {
263 /* '/' */
264 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error);
265
266 /* optional '..' */
267 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT));
Michal Vasko004d3152020-06-11 19:59:22 +0200268 }
269 }
270 } else {
271 /* '/' */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200272 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 +0200273 }
274
275 do {
276 /* NameTest */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200277 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 +0200278
279 /* check prefix based on the options */
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200280 cur_node = exp->expr + exp->tok_pos[tok_idx];
281 cur_len = exp->tok_len[tok_idx];
282 if (prefix == LY_PATH_PREFIX_MANDATORY) {
283 if (!strnstr(cur_node, ":", cur_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100284 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200285 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200286 goto error;
287 }
288 } else if (prefix == LY_PATH_PREFIX_STRICT_INHERIT) {
289 if (!prev_prefix) {
290 /* the first node must have a prefix */
291 if (!strnstr(cur_node, ":", cur_len)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100292 LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200293 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200294 goto error;
295 }
296
297 /* remember the first prefix */
298 prev_prefix = cur_node;
299 } else {
300 /* the prefix must be different, if any */
301 ptr = strnstr(cur_node, ":", cur_len);
302 if (ptr) {
303 if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100304 LOGVAL(ctx, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200305 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200306 goto error;
307 }
308
309 /* remember this next prefix */
310 prev_prefix = cur_node;
311 }
312 }
Michal Vasko004d3152020-06-11 19:59:22 +0200313 }
314
315 ++tok_idx;
316
317 /* Predicate* */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200318 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, ctx_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200319
Radek Krejci0f969882020-08-21 16:56:47 +0200320 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +0200321 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH));
322
323 /* trailing token check */
324 if (exp->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100325 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 +0200326 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200327 goto error;
328 }
329
330 *expr = exp;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100331
Radek Krejciddace2c2021-01-08 11:30:56 +0100332 LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200333 return LY_SUCCESS;
334
335error:
336 lyxp_expr_free(ctx, exp);
Radek Krejciddace2c2021-01-08 11:30:56 +0100337 LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200338 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200339}
340
341LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200342ly_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 +0200343 size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200344{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200345 LY_ERR ret = LY_SUCCESS;
346 struct lyxp_expr *exp = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200347 uint16_t tok_idx;
348
349 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY));
350 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
351
Radek Krejciddace2c2021-01-08 11:30:56 +0100352 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100353
Michal Vasko004d3152020-06-11 19:59:22 +0200354 /* parse as a generic XPath expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200355 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200356 tok_idx = 0;
357
Radek Krejcif03a9e22020-09-18 20:09:31 +0200358 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, cur_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200359
360 /* trailing token check */
361 if (exp->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100362 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.",
Michal Vasko69730152020-10-09 16:30:07 +0200363 exp->expr + exp->tok_pos[tok_idx]);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200364 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200365 goto error;
366 }
367
368 *expr = exp;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100369
Radek Krejciddace2c2021-01-08 11:30:56 +0100370 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200371 return LY_SUCCESS;
372
373error:
374 lyxp_expr_free(ctx, exp);
Radek Krejciddace2c2021-01-08 11:30:56 +0100375 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200376 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200377}
378
379/**
380 * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node.
381 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200382 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200383 * @param[in] cur_node Optional current (original context) node.
Radek Krejci09c77442021-04-26 11:10:34 +0200384 * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_VALUE_SCHEMA*.
385 * @param[in] prev_ctx_node Previous context node. Needed for ::LY_VALUE_JSON.
Michal Vasko004d3152020-06-11 19:59:22 +0200386 * @param[in] expr Parsed path.
387 * @param[in] tok_idx Index in @p expr.
388 * @param[in] lref Lref option.
Michal Vasko004d3152020-06-11 19:59:22 +0200389 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200390 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskoa9388b52021-06-15 11:57:42 +0200391 * @param[in,out] unres Global unres to use.
Michal Vasko004d3152020-06-11 19:59:22 +0200392 * @param[out] mod Resolved module.
393 * @param[out] name Parsed node name.
394 * @param[out] name_len Length of @p name.
395 * @return LY_ERR value.
396 */
397static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200398ly_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 +0200399 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 +0200400 LY_VALUE_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, const struct lys_module **mod,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100401 const char **name, size_t *name_len)
Michal Vasko004d3152020-06-11 19:59:22 +0200402{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100403 LY_ERR ret;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200404 const char *pref;
Michal Vasko004d3152020-06-11 19:59:22 +0200405 size_t len;
406
407 assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST);
408
409 /* get prefix */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200410 if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) {
411 len = pref - (expr->expr + expr->tok_pos[tok_idx]);
412 pref = expr->expr + expr->tok_pos[tok_idx];
413 } else {
414 len = 0;
415 }
Michal Vasko004d3152020-06-11 19:59:22 +0200416
417 /* find next node module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200418 if (pref) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100419 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100420
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200421 *mod = ly_resolve_prefix(ctx, pref, len, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200422 if (!*mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100423 LOGVAL(ctx, LYVE_XPATH, "No module connected with the prefix \"%.*s\" found (prefix format %s).",
Radek Krejci422afb12021-03-04 16:38:16 +0100424 (int)len, pref, ly_format2str(format));
Michal Vasko825a0442021-04-16 16:11:53 +0200425 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100426 goto error;
Michal Vasko004d3152020-06-11 19:59:22 +0200427 } else if (!(*mod)->implemented) {
428 if (lref == LY_PATH_LREF_FALSE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100429 LOGVAL(ctx, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name);
Michal Vasko825a0442021-04-16 16:11:53 +0200430 ret = LY_EVALID;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100431 goto error;
Michal Vasko004d3152020-06-11 19:59:22 +0200432 }
Michal Vasko825a0442021-04-16 16:11:53 +0200433
Michal Vaskob3e22532021-02-03 10:50:04 +0100434 assert(unres);
Michal Vasko4e205e82021-06-08 14:01:47 +0200435 LY_CHECK_GOTO(ret = lys_implement((struct lys_module *)*mod, NULL, unres), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200436 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100437
Radek Krejciddace2c2021-01-08 11:30:56 +0100438 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200439 } else {
440 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200441 case LY_VALUE_SCHEMA:
442 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200443 if (!cur_mod) {
444 LOGINT_RET(ctx);
445 }
446 /* use current module */
Michal Vasko004d3152020-06-11 19:59:22 +0200447 *mod = cur_mod;
448 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200449 case LY_VALUE_JSON:
Michal Vasko004d3152020-06-11 19:59:22 +0200450 if (!prev_ctx_node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200451 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200452 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200453 /* inherit module of the previous node */
Michal Vasko004d3152020-06-11 19:59:22 +0200454 *mod = prev_ctx_node->module;
455 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200456 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200457 case LY_VALUE_XML:
Radek Krejcif9943642021-04-26 10:18:21 +0200458 case LY_VALUE_LYB:
459 /* not really defined or accepted */
Michal Vasko00cbf532020-06-15 13:58:47 +0200460 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200461 }
462 }
463
464 /* set name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200465 if (pref) {
466 *name = pref + len + 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200467 *name_len = expr->tok_len[tok_idx] - len - 1;
468 } else {
469 *name = expr->expr + expr->tok_pos[tok_idx];
470 *name_len = expr->tok_len[tok_idx];
471 }
472
473 return LY_SUCCESS;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100474
475error:
Radek Krejciddace2c2021-01-08 11:30:56 +0100476 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Michal Vasko825a0442021-04-16 16:11:53 +0200477 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200478}
479
480LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200481ly_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 +0200482 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 +0200483 void *prefix_data, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vasko004d3152020-06-11 19:59:22 +0200484{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100485 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +0200486 struct ly_path_predicate *p;
487 const struct lysc_node *key;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100488 const struct lys_module *mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200489 const char *name;
490 size_t name_len, key_count;
491
Michal Vasko00cbf532020-06-15 13:58:47 +0200492 assert(ctx && ctx_node);
493
Radek Krejciddace2c2021-01-08 11:30:56 +0100494 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100495
Michal Vasko004d3152020-06-11 19:59:22 +0200496 *pred_type = 0;
497
Michal Vasko004d3152020-06-11 19:59:22 +0200498 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200499 /* '[', no predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100500 goto cleanup; /* LY_SUCCESS */
Michal Vasko004d3152020-06-11 19:59:22 +0200501 }
502
503 if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) {
504 if (ctx_node->nodetype != LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100505 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200506 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100507 ret = LY_EVALID;
508 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200509 } else if (ctx_node->flags & LYS_KEYLESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100510 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200511 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100512 ret = LY_EVALID;
513 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200514 }
515
516 do {
517 /* NameTest, find the key */
Michal Vasko6b26e742020-07-17 15:02:10 +0200518 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 +0100519 format, prefix_data, NULL, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100520 key = lys_find_child(ctx_node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200521 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +0100522 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100523 ret = LY_ENOTFOUND;
524 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200525 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100526 LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", lys_nodetype2str(key->nodetype),
527 key->name);
528 ret = LY_EVALID;
529 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200530 }
531 ++(*tok_idx);
532
Michal Vasko004d3152020-06-11 19:59:22 +0200533 if (!*pred_type) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200534 /* new predicate */
Michal Vasko004d3152020-06-11 19:59:22 +0200535 *pred_type = LY_PATH_PREDTYPE_LIST;
536 }
537 assert(*pred_type == LY_PATH_PREDTYPE_LIST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100538 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200539 p->key = key;
540
541 /* '=' */
542 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
543 ++(*tok_idx);
544
545 /* Literal */
546 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100547 LOG_LOCSET(key, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100548 ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200549 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 +0100550 LYD_HINT_DATA, key, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100551 LOG_LOCBACK(key ? 1 : 0, 0, 0, 0);
Michal Vasko55b84812021-05-11 09:23:58 +0200552 LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200553 ++(*tok_idx);
554
Michal Vaskoae875662020-10-21 10:33:17 +0200555 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
556 ++((struct lysc_type *)p->value.realtype)->refcount;
557
Michal Vasko004d3152020-06-11 19:59:22 +0200558 /* ']' */
559 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
560 ++(*tok_idx);
561
562 /* another predicate follows? */
563 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
564
565 /* check that all keys were set */
566 key_count = 0;
Michal Vasko544e58a2021-01-28 14:33:41 +0100567 for (key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +0200568 ++key_count;
569 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200570 if (LY_ARRAY_COUNT(*predicates) != key_count) {
Michal Vasko004d3152020-06-11 19:59:22 +0200571 /* names (keys) are unique - it was checked when parsing */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100572 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200573 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200574 ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, *predicates);
Michal Vasko004d3152020-06-11 19:59:22 +0200575 *predicates = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100576 ret = LY_EVALID;
577 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200578 }
579
580 } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) {
581 if (ctx_node->nodetype != LYS_LEAFLIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582 LOGVAL(ctx, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200583 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100584 ret = LY_EVALID;
585 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200586 }
587 ++(*tok_idx);
588
589 /* new predicate */
590 *pred_type = LY_PATH_PREDTYPE_LEAFLIST;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100591 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200592
593 /* '=' */
594 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
595 ++(*tok_idx);
596
597 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
598 /* store the value */
Radek Krejciddace2c2021-01-08 11:30:56 +0100599 LOG_LOCSET(ctx_node, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100600 ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200601 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 +0100602 LYD_HINT_DATA, ctx_node, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100603 LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0);
Michal Vasko55b84812021-05-11 09:23:58 +0200604 LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200605 ++(*tok_idx);
606
Michal Vaskoae875662020-10-21 10:33:17 +0200607 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
608 ++((struct lysc_type *)p->value.realtype)->refcount;
609
Michal Vasko004d3152020-06-11 19:59:22 +0200610 /* ']' */
611 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
612 ++(*tok_idx);
613 } else {
614 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER);
615 if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100616 ret = LY_EVALID;
617 LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200618 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100619 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200620 } else if (ctx_node->flags & LYS_CONFIG_W) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100621 ret = LY_EVALID;
622 LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for configuration %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200623 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100624 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200625 }
Michal Vasko004d3152020-06-11 19:59:22 +0200626
627 /* new predicate */
628 *pred_type = LY_PATH_PREDTYPE_POSITION;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100629 LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200630
631 /* syntax was already checked */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100632 p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&name, LY_BASE_DEC);
Michal Vasko00cbf532020-06-15 13:58:47 +0200633 ++(*tok_idx);
Michal Vasko004d3152020-06-11 19:59:22 +0200634
635 /* ']' */
636 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
637 ++(*tok_idx);
638 }
639
Radek Krejci2efc45b2020-12-22 16:25:44 +0100640cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100641 LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100642 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200643}
644
645/**
646 * @brief Compile leafref predicate. Actually, it is only checked.
647 *
648 * @param[in] ctx_node Context node, node for which the predicate is defined.
649 * @param[in] cur_node Current (original context) node.
650 * @param[in] expr Parsed path.
651 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
Michal Vasko004d3152020-06-11 19:59:22 +0200652 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200653 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko405cc9e2020-12-01 12:01:27 +0100654 * @param[in,out] unres Global unres structure for newly implemented modules.
Michal Vasko004d3152020-06-11 19:59:22 +0200655 * @return LY_ERR value.
656 */
657static LY_ERR
658ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node,
Radek Krejci8df109d2021-04-23 12:19:08 +0200659 const struct lyxp_expr *expr, uint16_t *tok_idx, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100660 struct lys_glob_unres *unres)
Michal Vasko004d3152020-06-11 19:59:22 +0200661{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100662 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +0200663 const struct lysc_node *key, *node, *node2;
664 const struct lys_module *mod;
665 const char *name;
666 size_t name_len;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100667 struct ly_ctx *ctx = cur_node->module->ctx;
668
Radek Krejciddace2c2021-01-08 11:30:56 +0100669 LOG_LOCSET(cur_node, NULL, NULL, NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200670
Michal Vasko004d3152020-06-11 19:59:22 +0200671 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200672 /* '[', no predicate */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100673 goto cleanup; /* LY_SUCCESS */
Michal Vasko004d3152020-06-11 19:59:22 +0200674 }
675
676 if (ctx_node->nodetype != LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100677 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200678 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100679 ret = LY_EVALID;
680 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200681 } else if (ctx_node->flags & LYS_KEYLESS) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100682 LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200683 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100684 ret = LY_EVALID;
685 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200686 }
687
688 do {
689 /* NameTest, find the key */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100690 ret = ly_path_compile_prefix(ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx,
691 LY_PATH_LREF_TRUE, format, prefix_data, unres, &mod, &name, &name_len);
692 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100693 key = lys_find_child(ctx_node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200694 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +0100695 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696 ret = LY_EVALID;
697 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200698 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100699 LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200700 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100701 ret = LY_EVALID;
702 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200703 }
704 ++(*tok_idx);
705
706 /* we are not actually compiling, throw the key away */
707 (void)key;
708
709 /* '=' */
710 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
711 ++(*tok_idx);
712
713 /* FuncName */
714 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME);
715 ++(*tok_idx);
716
717 /* evaluating from the "current()" node */
718 node = cur_node;
719
720 /* '(' */
721 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
722 ++(*tok_idx);
723
724 /* ')' */
725 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
726 ++(*tok_idx);
727
728 do {
729 /* '/' */
730 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
731 ++(*tok_idx);
732
733 /* go to parent */
734 if (!node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100735 LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path.");
736 ret = LY_EVALID;
737 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200738 }
739 node = lysc_data_parent(node);
740
741 /* '..' */
742 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT);
743 ++(*tok_idx);
744 } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT);
745
746 do {
747 /* '/' */
748 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
749 ++(*tok_idx);
750
751 /* NameTest */
752 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100753 LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_node->module, node, expr, *tok_idx,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100754 LY_PATH_LREF_TRUE, format, prefix_data, unres, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100755 node2 = lys_find_child(node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200756 if (!node2) {
Radek Krejci422afb12021-03-04 16:38:16 +0100757 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100758 ret = LY_EVALID;
759 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200760 }
761 node = node2;
762 ++(*tok_idx);
763 } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST));
764
765 /* check the last target node */
766 if (node->nodetype != LYS_LEAF) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100767 LOGVAL(ctx, LYVE_XPATH, "Leaf expected instead of %s \"%s\" in leafref predicate in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200768 lys_nodetype2str(node->nodetype), node->name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100769 ret = LY_EVALID;
770 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200771 }
772
773 /* we are not actually compiling, throw the rightside node away */
774 (void)node;
775
776 /* ']' */
777 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
778 ++(*tok_idx);
779
Radek Krejci0f969882020-08-21 16:56:47 +0200780 /* another predicate follows? */
Michal Vasko004d3152020-06-11 19:59:22 +0200781 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
782
Radek Krejci2efc45b2020-12-22 16:25:44 +0100783cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100784 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100785 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200786}
787
788LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200789ly_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 +0100790 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 +0200791 LY_VALUE_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, struct ly_path **path)
Michal Vasko004d3152020-06-11 19:59:22 +0200792{
793 LY_ERR ret = LY_SUCCESS;
794 uint16_t tok_idx = 0;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100795 const struct lys_module *mod = NULL;
Michal Vasko6b26e742020-07-17 15:02:10 +0200796 const struct lysc_node *node2, *cur_node, *op;
Michal Vasko00cbf532020-06-15 13:58:47 +0200797 struct ly_path *p = NULL;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200798 uint32_t getnext_opts;
Michal Vasko004d3152020-06-11 19:59:22 +0200799 const char *name;
800 size_t name_len;
801
Michal Vasko00cbf532020-06-15 13:58:47 +0200802 assert(ctx);
Michal Vasko6b26e742020-07-17 15:02:10 +0200803 assert((lref == LY_PATH_LREF_FALSE) || ctx_node);
Michal Vasko004d3152020-06-11 19:59:22 +0200804 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko00cbf532020-06-15 13:58:47 +0200805 assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT));
806 assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY));
Michal Vasko004d3152020-06-11 19:59:22 +0200807
Michal Vasko6b26e742020-07-17 15:02:10 +0200808 /* find operation, if we are in any */
Radek Krejci1e008d22020-08-17 11:37:37 +0200809 for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +0200810
Radek Krejci2efc45b2020-12-22 16:25:44 +0100811 *path = NULL;
812
Michal Vasko6b26e742020-07-17 15:02:10 +0200813 /* remember original context node */
814 cur_node = ctx_node;
Radek Krejciddace2c2021-01-08 11:30:56 +0100815 LOG_LOCINIT(cur_node, NULL, NULL, NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200816
Michal Vasko00cbf532020-06-15 13:58:47 +0200817 if (oper == LY_PATH_OPER_OUTPUT) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100818 getnext_opts = LYS_GETNEXT_OUTPUT;
Michal Vasko00cbf532020-06-15 13:58:47 +0200819 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100820 getnext_opts = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +0200821 }
822
Michal Vasko004d3152020-06-11 19:59:22 +0200823 if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) {
824 /* absolute path */
825 ctx_node = NULL;
826
827 ++tok_idx;
828 } else {
829 /* relative path */
830 while ((lref == LY_PATH_LREF_TRUE) && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) {
831 if (!ctx_node) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100832 LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path.");
Michal Vasko14424ba2020-12-09 18:09:51 +0100833 ret = LY_EVALID;
834 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +0200835 }
836
837 /* get parent */
838 ctx_node = lysc_data_parent(ctx_node);
839
840 ++tok_idx;
841
842 assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH);
843 ++tok_idx;
844 }
845
Michal Vasko00cbf532020-06-15 13:58:47 +0200846 /* we are not storing the parent */
847 (void)ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +0200848 }
849
850 do {
Michal Vasko00cbf532020-06-15 13:58:47 +0200851 /* check last compiled inner node, whether it is uniquely identified (even key-less list) */
Michal Vaskod456cf62020-11-23 16:48:43 +0100852 if (p && (lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) &&
853 (p->node->nodetype == LYS_LIST) && !p->predicates) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100854 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200855 lys_nodetype2str(p->node->nodetype), p->node->name);
Michal Vasko14424ba2020-12-09 18:09:51 +0100856 ret = LY_EVALID;
857 goto cleanup;
Michal Vasko00cbf532020-06-15 13:58:47 +0200858 }
859
Michal Vasko14424ba2020-12-09 18:09:51 +0100860 /* NameTest */
861 LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, expr, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, cleanup);
862
Michal Vasko004d3152020-06-11 19:59:22 +0200863 /* get module and node name */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200864 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 +0100865 prefix_data, unres, &mod, &name, &name_len), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200866 ++tok_idx;
867
868 /* find the next node */
Radek Krejcid5d37432021-03-12 13:46:40 +0100869 if (!ctx_node && ext) {
870 node2 = lysc_ext_find_node(ext, mod, name, name_len, 0, getnext_opts);
871 } else {
872 node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts);
873 }
Radek Krejci25fe3dd2020-11-10 22:02:26 +0100874 if (!node2 || (op && (node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) {
Radek Krejci422afb12021-03-04 16:38:16 +0100875 LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200876 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200877 goto cleanup;
878 }
879 ctx_node = node2;
880
881 /* new path segment */
Michal Vasko00cbf532020-06-15 13:58:47 +0200882 LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200883 p->node = ctx_node;
884
885 /* compile any predicates */
886 if (lref == LY_PATH_LREF_TRUE) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100887 ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data, unres);
Michal Vasko004d3152020-06-11 19:59:22 +0200888 } else {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200889 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 +0200890 &p->predicates, &p->pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +0200891 }
892 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200893 } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH));
894
Michal Vasko14424ba2020-12-09 18:09:51 +0100895 /* check leftover tokens */
896 if (tok_idx < expr->used) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100897 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 +0100898 ret = LY_EVALID;
899 goto cleanup;
900 }
901
Michal Vasko00cbf532020-06-15 13:58:47 +0200902 /* check last compiled node */
Michal Vasko69730152020-10-09 16:30:07 +0200903 if ((lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) &&
904 (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100905 LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200906 lys_nodetype2str(p->node->nodetype), p->node->name);
Michal Vasko14424ba2020-12-09 18:09:51 +0100907 ret = LY_EVALID;
908 goto cleanup;
Michal Vasko00cbf532020-06-15 13:58:47 +0200909 }
910
Michal Vasko004d3152020-06-11 19:59:22 +0200911cleanup:
912 if (ret) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200913 ly_path_free(ctx, *path);
Michal Vasko004d3152020-06-11 19:59:22 +0200914 *path = NULL;
915 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100916 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200917 return ret;
918}
919
920LY_ERR
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200921ly_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 +0200922 struct lyd_node **match)
Michal Vasko004d3152020-06-11 19:59:22 +0200923{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200924 LY_ARRAY_COUNT_TYPE u;
Michal Vaskoacfc9282021-02-04 12:08:23 +0100925 struct lyd_node *prev_node = NULL, *node = NULL, *target;
Michal Vasko004d3152020-06-11 19:59:22 +0200926 uint64_t pos;
927
928 assert(path && start);
929
930 if (lysc_data_parent(path[0].node)) {
931 /* relative path, start from the parent children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200932 start = lyd_child(start);
Michal Vasko004d3152020-06-11 19:59:22 +0200933 } else {
934 /* absolute path, start from the first top-level sibling */
935 while (start->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +0100936 start = lyd_parent(start);
Michal Vasko004d3152020-06-11 19:59:22 +0200937 }
938 while (start->prev->next) {
939 start = start->prev;
940 }
941 }
942
943 LY_ARRAY_FOR(path, u) {
944 switch (path[u].pred_type) {
945 case LY_PATH_PREDTYPE_POSITION:
946 /* we cannot use hashes and want an instance on a specific position */
947 pos = 1;
Michal Vasko4c583e82020-07-17 12:16:14 +0200948 LYD_LIST_FOR_INST(start, path[u].node, node) {
Michal Vasko004d3152020-06-11 19:59:22 +0200949 if (pos == path[u].predicates[0].position) {
950 break;
951 }
952 ++pos;
953 }
954 break;
955 case LY_PATH_PREDTYPE_LEAFLIST:
956 /* we will use hashes to find one leaf-list instance */
957 LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target));
958 lyd_find_sibling_first(start, target, &node);
959 lyd_free_tree(target);
960 break;
961 case LY_PATH_PREDTYPE_LIST:
962 /* we will use hashes to find one list instance */
963 LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target));
964 lyd_find_sibling_first(start, target, &node);
965 lyd_free_tree(target);
966 break;
967 case LY_PATH_PREDTYPE_NONE:
968 /* we will use hashes to find one any/container/leaf instance */
969 lyd_find_sibling_val(start, path[u].node, NULL, 0, &node);
970 break;
971 }
972
973 if (!node) {
974 /* no matching nodes */
975 break;
976 }
977
Michal Vasko00cbf532020-06-15 13:58:47 +0200978 /* rememeber previous node */
979 prev_node = node;
980
Michal Vasko004d3152020-06-11 19:59:22 +0200981 /* next path segment, if any */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200982 start = lyd_child(node);
Michal Vasko004d3152020-06-11 19:59:22 +0200983 }
984
Michal Vasko004d3152020-06-11 19:59:22 +0200985 if (node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200986 /* we have found the full path */
987 if (path_idx) {
988 *path_idx = u;
989 }
990 if (match) {
991 *match = node;
992 }
Michal Vasko004d3152020-06-11 19:59:22 +0200993 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200994
995 } else if (prev_node) {
996 /* we have found only some partial match */
997 if (path_idx) {
998 *path_idx = u - 1;
999 }
1000 if (match) {
1001 *match = prev_node;
1002 }
1003 return LY_EINCOMPLETE;
1004 }
1005
1006 /* we have not found any nodes */
1007 if (path_idx) {
1008 *path_idx = 0;
1009 }
1010 if (match) {
1011 *match = NULL;
1012 }
1013 return LY_ENOTFOUND;
1014}
1015
1016LY_ERR
1017ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match)
1018{
1019 LY_ERR ret;
1020 struct lyd_node *m;
1021
1022 ret = ly_path_eval_partial(path, start, NULL, &m);
1023
1024 if (ret == LY_SUCCESS) {
1025 /* last node was found */
1026 if (match) {
1027 *match = m;
1028 }
1029 return LY_SUCCESS;
1030 }
1031
1032 /* not a full match */
1033 if (match) {
1034 *match = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02001035 }
1036 return LY_ENOTFOUND;
1037}
1038
1039LY_ERR
1040ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup)
1041{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001042 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko004d3152020-06-11 19:59:22 +02001043
1044 if (!path) {
1045 return LY_SUCCESS;
1046 }
1047
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001048 LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02001049 LY_ARRAY_FOR(path, u) {
1050 LY_ARRAY_INCREMENT(*dup);
1051 (*dup)[u].node = path[u].node;
1052 if (path[u].predicates) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001053 LY_ARRAY_CREATE_RET(ctx, (*dup)[u].predicates, LY_ARRAY_COUNT(path[u].predicates), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +02001054 (*dup)[u].pred_type = path[u].pred_type;
1055 LY_ARRAY_FOR(path[u].predicates, v) {
1056 struct ly_path_predicate *pred = &path[u].predicates[v];
1057
1058 LY_ARRAY_INCREMENT((*dup)[u].predicates);
1059 switch (path[u].pred_type) {
1060 case LY_PATH_PREDTYPE_POSITION:
1061 /* position-predicate */
1062 (*dup)[u].predicates[v].position = pred->position;
1063 break;
1064 case LY_PATH_PREDTYPE_LIST:
1065 case LY_PATH_PREDTYPE_LEAFLIST:
1066 /* key-predicate or leaf-list-predicate */
1067 (*dup)[u].predicates[v].key = pred->key;
Michal Vasko004d3152020-06-11 19:59:22 +02001068 pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value);
Michal Vaskoae875662020-10-21 10:33:17 +02001069 ++((struct lysc_type *)pred->value.realtype)->refcount;
Michal Vasko004d3152020-06-11 19:59:22 +02001070 break;
1071 case LY_PATH_PREDTYPE_NONE:
1072 break;
1073 }
1074 }
1075 }
1076 }
1077
1078 return LY_SUCCESS;
1079}
1080
1081void
Michal Vaskof7e16e22020-10-21 09:24:39 +02001082ly_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 +02001083{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001084 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001085
1086 if (!predicates) {
1087 return;
1088 }
1089
1090 LY_ARRAY_FOR(predicates, u) {
1091 switch (pred_type) {
1092 case LY_PATH_PREDTYPE_POSITION:
1093 case LY_PATH_PREDTYPE_NONE:
1094 /* nothing to free */
1095 break;
1096 case LY_PATH_PREDTYPE_LIST:
Michal Vasko004d3152020-06-11 19:59:22 +02001097 case LY_PATH_PREDTYPE_LEAFLIST:
Michal Vaskoae875662020-10-21 10:33:17 +02001098 if (predicates[u].value.realtype) {
1099 predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
1100 lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype);
1101 }
Michal Vasko004d3152020-06-11 19:59:22 +02001102 break;
1103 }
1104 }
1105 LY_ARRAY_FREE(predicates);
1106}
1107
1108void
1109ly_path_free(const struct ly_ctx *ctx, struct ly_path *path)
1110{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001111 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001112
Michal Vasko55b84812021-05-11 09:23:58 +02001113 if (!path) {
1114 return;
1115 }
1116
Michal Vasko004d3152020-06-11 19:59:22 +02001117 LY_ARRAY_FOR(path, u) {
Michal Vaskof7e16e22020-10-21 09:24:39 +02001118 ly_path_predicates_free(ctx, path[u].pred_type, path[u].predicates);
Michal Vasko004d3152020-06-11 19:59:22 +02001119 }
1120 LY_ARRAY_FREE(path);
1121}