blob: 38cb92ca2d1920286cbfc09731c8e911b466b080 [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
2 * @file path.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Path functions
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _ISOC99_SOURCE /* strtoull */
15
16#include "path.h"
17
18#include <assert.h>
19#include <ctype.h>
20#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020021#include <string.h>
Michal Vasko004d3152020-06-11 19:59:22 +020022
23#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020024#include "compat.h"
Michal Vasko004d3152020-06-11 19:59:22 +020025#include "log.h"
26#include "plugins_types.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020027#include "set.h"
Michal Vasko4c583e82020-07-17 12:16:14 +020028#include "tree.h"
Michal Vasko004d3152020-06-11 19:59:22 +020029#include "tree_data_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "tree_schema.h"
Michal Vasko004d3152020-06-11 19:59:22 +020031#include "tree_schema_internal.h"
32#include "xpath.h"
33
Radek Krejcic0c66412020-08-21 13:53:50 +020034#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 +020035
Michal Vasko004d3152020-06-11 19:59:22 +020036/**
37 * @brief Check predicate syntax.
38 *
39 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +020040 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +020041 * @param[in] exp Parsed predicate.
42 * @param[in,out] tok_idx Index in @p exp, is adjusted.
43 * @param[in] prefix Prefix option.
44 * @param[in] pred Predicate option.
45 * @return LY_ERR value.
46 */
47static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +020048ly_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 +020049 uint16_t *tok_idx, uint8_t prefix, uint8_t pred)
Michal Vasko004d3152020-06-11 19:59:22 +020050{
Radek Krejciba03a5a2020-08-27 14:40:41 +020051 LY_ERR ret = LY_SUCCESS;
Michal Vasko004d3152020-06-11 19:59:22 +020052 struct ly_set *set = NULL;
53 uint32_t i;
54 const char *name;
55 size_t name_len;
56
Michal Vasko004d3152020-06-11 19:59:22 +020057 if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +020058 /* '[' */
59
Michal Vasko69730152020-10-09 16:30:07 +020060 if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS)) &&
61 !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +020062 ret = ly_set_new(&set);
63 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +020064
65 do {
66 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +020067 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +020068
69 /* check prefix based on the options */
70 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
71 if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) {
Michal Vasko6b26e742020-07-17 15:02:10 +020072 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +020073 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +020074 goto token_error;
Michal Vasko8b06a5e2020-08-06 12:13:08 +020075 } else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) {
76 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Redundant prefix 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 Vasko004d3152020-06-11 19:59:22 +020079 }
80 if (!name) {
81 name = exp->expr + exp->tok_pos[*tok_idx];
82 name_len = exp->tok_len[*tok_idx];
83 } else {
84 ++name;
85 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
86 }
87
88 /* check whether it was not already specified */
89 for (i = 0; i < set->count; ++i) {
90 /* all the keys must be from the same module so this comparison should be fine */
91 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Michal Vasko6b26e742020-07-17 15:02:10 +020092 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +020093 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +020094 }
95 }
96
97 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +020098 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +020099 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200100
101 /* NameTest */
102 ++(*tok_idx);
103
104 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200105 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200106
107 /* Literal */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200108 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200109
110 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200111 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200112
Radek Krejci0f969882020-08-21 16:56:47 +0200113 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200114 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
115
Michal Vasko004d3152020-06-11 19:59:22 +0200116 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200117 /* '.' */
118
Michal Vasko004d3152020-06-11 19:59:22 +0200119 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200120 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200121
122 /* Literal */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200123 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200124
125 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200126 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200127
Michal Vasko004d3152020-06-11 19:59:22 +0200128 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200129 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +0200130
131 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200132 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200133
134 } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
135 assert(prefix == LY_PATH_PREFIX_OPTIONAL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200136 ret = ly_set_new(&set);
137 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200138
139 do {
140 /* NameTest is always expected here */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200141 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200142
143 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
144 if (!name) {
145 name = exp->expr + exp->tok_pos[*tok_idx];
146 name_len = exp->tok_len[*tok_idx];
147 } else {
148 ++name;
149 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
150 }
151
152 /* check whether it was not already specified */
153 for (i = 0; i < set->count; ++i) {
154 /* all the keys must be from the same module so this comparison should be fine */
155 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200156 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", name_len, name);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200157 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200158 }
159 }
160
161 /* add it into the set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200162 ret = ly_set_add(set, (void *)name, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200163 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200164
165 /* NameTest */
166 ++(*tok_idx);
167
168 /* '=' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200169 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200170
171 /* FuncName */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200172 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200173 if ((exp->tok_len[*tok_idx] != 7) || strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", 7)) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200174 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200175 exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200176 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200177 }
178 ++(*tok_idx);
179
180 /* '(' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200181 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200182
183 /* ')' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200184 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200185
186 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200187 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200188
189 /* '..' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200190 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200191 do {
192 /* '/' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200193 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200194 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT));
195
196 /* NameTest */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200197 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200198
199 /* '/' */
200 while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) {
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 /* ']' */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200206 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error);
Michal Vasko004d3152020-06-11 19:59:22 +0200207
Radek Krejci0f969882020-08-21 16:56:47 +0200208 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +0200209 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
210
211 } else {
Michal Vasko6b26e742020-07-17 15:02:10 +0200212 LOGVAL_P(ctx, cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
Michal Vasko69730152020-10-09 16:30:07 +0200213 exp->expr + exp->tok_pos[*tok_idx]);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200214 goto token_error;
Michal Vasko004d3152020-06-11 19:59:22 +0200215 }
216 }
217
Radek Krejciba03a5a2020-08-27 14:40:41 +0200218cleanup:
Michal Vasko004d3152020-06-11 19:59:22 +0200219 ly_set_free(set, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200220 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200221
Radek Krejciba03a5a2020-08-27 14:40:41 +0200222token_error:
Michal Vasko004d3152020-06-11 19:59:22 +0200223 ly_set_free(set, NULL);
224 return LY_EVALID;
225}
226
227LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200228ly_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 +0200229 uint8_t begin, uint8_t lref, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200230{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200231 LY_ERR ret = LY_SUCCESS;
232 struct lyxp_expr *exp = NULL;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200233 uint16_t tok_idx, cur_len;
234 const char *cur_node, *prev_prefix = NULL, *ptr;
Michal Vasko004d3152020-06-11 19:59:22 +0200235
236 assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER));
237 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko69730152020-10-09 16:30:07 +0200238 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) ||
239 (prefix == LY_PATH_PREFIX_STRICT_INHERIT));
Michal Vasko004d3152020-06-11 19:59:22 +0200240 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
241
242 /* parse as a generic XPath expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200243 LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 1, &exp), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200244 tok_idx = 0;
245
246 if (begin == LY_PATH_BEGIN_EITHER) {
247 /* is the path relative? */
248 if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) {
Michal Vaskocb8c6d42020-10-16 11:58:30 +0200249 /* relative path check specific to leafref */
250 if (lref == LY_PATH_LREF_TRUE) {
251 /* mandatory '..' */
252 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error);
253
254 do {
255 /* '/' */
256 LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error);
257
258 /* optional '..' */
259 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT));
Michal Vasko004d3152020-06-11 19:59:22 +0200260 }
261 }
262 } else {
263 /* '/' */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200264 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 +0200265 }
266
267 do {
268 /* NameTest */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200269 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 +0200270
271 /* check prefix based on the options */
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200272 cur_node = exp->expr + exp->tok_pos[tok_idx];
273 cur_len = exp->tok_len[tok_idx];
274 if (prefix == LY_PATH_PREFIX_MANDATORY) {
275 if (!strnstr(cur_node, ":", cur_len)) {
276 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200277 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200278 goto error;
279 }
280 } else if (prefix == LY_PATH_PREFIX_STRICT_INHERIT) {
281 if (!prev_prefix) {
282 /* the first node must have a prefix */
283 if (!strnstr(cur_node, ":", cur_len)) {
284 LOGVAL_P(ctx, ctx_node, 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
289 /* remember the first prefix */
290 prev_prefix = cur_node;
291 } else {
292 /* the prefix must be different, if any */
293 ptr = strnstr(cur_node, ":", cur_len);
294 if (ptr) {
295 if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) {
296 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", cur_len, cur_node);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200297 ret = LY_EVALID;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200298 goto error;
299 }
300
301 /* remember this next prefix */
302 prev_prefix = cur_node;
303 }
304 }
Michal Vasko004d3152020-06-11 19:59:22 +0200305 }
306
307 ++tok_idx;
308
309 /* Predicate* */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200310 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, ctx_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200311
Radek Krejci0f969882020-08-21 16:56:47 +0200312 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +0200313 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH));
314
315 /* trailing token check */
316 if (exp->used > tok_idx) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200317 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of path.",
Michal Vasko69730152020-10-09 16:30:07 +0200318 exp->expr + exp->tok_pos[tok_idx]);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200319 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200320 goto error;
321 }
322
323 *expr = exp;
324 return LY_SUCCESS;
325
326error:
327 lyxp_expr_free(ctx, exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200328 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200329}
330
331LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200332ly_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 +0200333 size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200334{
Radek Krejcif03a9e22020-09-18 20:09:31 +0200335 LY_ERR ret = LY_SUCCESS;
336 struct lyxp_expr *exp = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200337 uint16_t tok_idx;
338
339 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY));
340 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
341
342 /* parse as a generic XPath expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200343 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
Radek Krejcif03a9e22020-09-18 20:09:31 +0200346 LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, cur_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200347
348 /* trailing token check */
349 if (exp->used > tok_idx) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200350 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.",
Michal Vasko69730152020-10-09 16:30:07 +0200351 exp->expr + exp->tok_pos[tok_idx]);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200352 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200353 goto error;
354 }
355
356 *expr = exp;
357 return LY_SUCCESS;
358
359error:
360 lyxp_expr_free(ctx, exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200361 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +0200362}
363
364/**
365 * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node.
366 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200367 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200368 * @param[in] cur_node Optional current (original context) node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200369 * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_PREF_SCHEMA*.
370 * @param[in] prev_ctx_node Previous context node. Needed for ::LY_PREF_JSON.
Michal Vasko004d3152020-06-11 19:59:22 +0200371 * @param[in] expr Parsed path.
372 * @param[in] tok_idx Index in @p expr.
373 * @param[in] lref Lref option.
Michal Vasko004d3152020-06-11 19:59:22 +0200374 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200375 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200376 * @param[out] mod Resolved module.
377 * @param[out] name Parsed node name.
378 * @param[out] name_len Length of @p name.
379 * @return LY_ERR value.
380 */
381static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200382ly_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 +0200383 const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint16_t tok_idx, uint8_t lref,
384 LY_PREFIX_FORMAT format, void *prefix_data, const struct lys_module **mod, const char **name, size_t *name_len)
Michal Vasko004d3152020-06-11 19:59:22 +0200385{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200386 const char *pref;
Michal Vasko004d3152020-06-11 19:59:22 +0200387 size_t len;
388
389 assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST);
390
391 /* get prefix */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200392 if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) {
393 len = pref - (expr->expr + expr->tok_pos[tok_idx]);
394 pref = expr->expr + expr->tok_pos[tok_idx];
395 } else {
396 len = 0;
397 }
Michal Vasko004d3152020-06-11 19:59:22 +0200398
399 /* find next node module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200400 if (pref) {
401 *mod = ly_resolve_prefix(ctx, pref, len, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200402 if (!*mod) {
Radek Krejcic271ff92020-11-10 22:01:52 +0100403 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "No module connected with the prefix \"%.*s\" found (prefix format %d).",
404 len, pref, format);
Radek Krejci8de005f2020-06-25 17:02:07 +0200405 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200406 } else if (!(*mod)->implemented) {
407 if (lref == LY_PATH_LREF_FALSE) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200408 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200409 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200410 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100411 LY_CHECK_RET(lys_set_implemented((struct lys_module *)*mod, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +0200412 }
413 } else {
414 switch (format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200415 case LY_PREF_SCHEMA:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200416 case LY_PREF_SCHEMA_RESOLVED:
417 if (!cur_mod) {
418 LOGINT_RET(ctx);
419 }
420 /* use current module */
Michal Vasko004d3152020-06-11 19:59:22 +0200421 *mod = cur_mod;
422 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200423 case LY_PREF_JSON:
Michal Vasko004d3152020-06-11 19:59:22 +0200424 if (!prev_ctx_node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200425 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200426 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200427 /* inherit module of the previous node */
Michal Vasko004d3152020-06-11 19:59:22 +0200428 *mod = prev_ctx_node->module;
429 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200430 case LY_PREF_XML:
Michal Vasko004d3152020-06-11 19:59:22 +0200431 /* not really defined */
Michal Vasko00cbf532020-06-15 13:58:47 +0200432 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200433 }
434 }
435
436 /* set name */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200437 if (pref) {
438 *name = pref + len + 1;
Michal Vasko004d3152020-06-11 19:59:22 +0200439 *name_len = expr->tok_len[tok_idx] - len - 1;
440 } else {
441 *name = expr->expr + expr->tok_pos[tok_idx];
442 *name_len = expr->tok_len[tok_idx];
443 }
444
445 return LY_SUCCESS;
446}
447
448LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200449ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200450 const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx, LY_PREFIX_FORMAT format,
451 void *prefix_data, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vasko004d3152020-06-11 19:59:22 +0200452{
453 struct ly_path_predicate *p;
454 const struct lysc_node *key;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100455 const struct lys_module *mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200456 const char *name;
457 size_t name_len, key_count;
458
Michal Vasko00cbf532020-06-15 13:58:47 +0200459 assert(ctx && ctx_node);
460
Michal Vasko004d3152020-06-11 19:59:22 +0200461 *pred_type = 0;
462
Michal Vasko004d3152020-06-11 19:59:22 +0200463 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200464 /* '[', no predicate */
Michal Vasko004d3152020-06-11 19:59:22 +0200465 return LY_SUCCESS;
466 }
467
468 if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) {
469 if (ctx_node->nodetype != LYS_LIST) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200470 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200471 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200472 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200473 } else if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200474 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200475 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200476 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200477 }
478
479 do {
480 /* NameTest, find the key */
Michal Vasko6b26e742020-07-17 15:02:10 +0200481 LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, LY_PATH_LREF_FALSE,
Michal Vasko69730152020-10-09 16:30:07 +0200482 format, prefix_data, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100483 key = lys_find_child(ctx_node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200484 if (!key) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200485 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200486 return LY_ENOTFOUND;
Michal Vasko004d3152020-06-11 19:59:22 +0200487 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200488 LOGVAL_P(ctx, cur_node ? cur_node : key, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200489 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200490 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200491 }
492 ++(*tok_idx);
493
Michal Vasko004d3152020-06-11 19:59:22 +0200494 if (!*pred_type) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200495 /* new predicate */
Michal Vasko004d3152020-06-11 19:59:22 +0200496 *pred_type = LY_PATH_PREDTYPE_LIST;
497 }
498 assert(*pred_type == LY_PATH_PREDTYPE_LIST);
Michal Vasko00cbf532020-06-15 13:58:47 +0200499 LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200500 p->key = key;
501
502 /* '=' */
503 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
504 ++(*tok_idx);
505
506 /* Literal */
507 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200508 LY_CHECK_RET(lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type,
509 expr->expr + expr->tok_pos[*tok_idx] + 1, expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data,
510 LYD_HINT_DATA, key, NULL, LY_VLOG_LYSC, key));
Michal Vasko004d3152020-06-11 19:59:22 +0200511 ++(*tok_idx);
512
Michal Vaskoae875662020-10-21 10:33:17 +0200513 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
514 ++((struct lysc_type *)p->value.realtype)->refcount;
515
Michal Vasko004d3152020-06-11 19:59:22 +0200516 /* ']' */
517 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
518 ++(*tok_idx);
519
520 /* another predicate follows? */
521 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
522
523 /* check that all keys were set */
524 key_count = 0;
525 for (key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next) {
526 ++key_count;
527 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200528 if (LY_ARRAY_COUNT(*predicates) != key_count) {
Michal Vasko004d3152020-06-11 19:59:22 +0200529 /* names (keys) are unique - it was checked when parsing */
Michal Vasko6b26e742020-07-17 15:02:10 +0200530 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200531 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Michal Vaskof7e16e22020-10-21 09:24:39 +0200532 ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, *predicates);
Michal Vasko004d3152020-06-11 19:59:22 +0200533 *predicates = NULL;
Radek Krejci8de005f2020-06-25 17:02:07 +0200534 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200535 }
536
537 } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) {
538 if (ctx_node->nodetype != LYS_LEAFLIST) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200539 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200540 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200541 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200542 }
543 ++(*tok_idx);
544
545 /* new predicate */
546 *pred_type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko00cbf532020-06-15 13:58:47 +0200547 LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200548
549 /* '=' */
550 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
551 ++(*tok_idx);
552
553 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
554 /* store the value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200555 LY_CHECK_RET(lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type,
556 expr->expr + expr->tok_pos[*tok_idx] + 1, expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data,
557 LYD_HINT_DATA, ctx_node, NULL, LY_VLOG_LYSC, ctx_node));
Michal Vasko004d3152020-06-11 19:59:22 +0200558 ++(*tok_idx);
559
Michal Vaskoae875662020-10-21 10:33:17 +0200560 /* "allocate" the type to avoid problems when freeing the value after the type was freed */
561 ++((struct lysc_type *)p->value.realtype)->refcount;
562
Michal Vasko004d3152020-06-11 19:59:22 +0200563 /* ']' */
564 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
565 ++(*tok_idx);
566 } else {
567 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER);
568 if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200569 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200570 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200571 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200572 } else if (ctx_node->flags & LYS_CONFIG_W) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200573 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Positional predicate defined for configuration %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200574 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200575 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200576 }
Michal Vasko004d3152020-06-11 19:59:22 +0200577
578 /* new predicate */
579 *pred_type = LY_PATH_PREDTYPE_POSITION;
Michal Vasko00cbf532020-06-15 13:58:47 +0200580 LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200581
582 /* syntax was already checked */
583 p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&name, 10);
Michal Vasko00cbf532020-06-15 13:58:47 +0200584 ++(*tok_idx);
Michal Vasko004d3152020-06-11 19:59:22 +0200585
586 /* ']' */
587 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
588 ++(*tok_idx);
589 }
590
591 return LY_SUCCESS;
592}
593
594/**
595 * @brief Compile leafref predicate. Actually, it is only checked.
596 *
597 * @param[in] ctx_node Context node, node for which the predicate is defined.
598 * @param[in] cur_node Current (original context) node.
599 * @param[in] expr Parsed path.
600 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
Michal Vasko004d3152020-06-11 19:59:22 +0200601 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200602 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200603 * @return LY_ERR value.
604 */
605static LY_ERR
606ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node,
Radek Krejci0f969882020-08-21 16:56:47 +0200607 const struct lyxp_expr *expr, uint16_t *tok_idx, LY_PREFIX_FORMAT format, void *prefix_data)
Michal Vasko004d3152020-06-11 19:59:22 +0200608{
609 const struct lysc_node *key, *node, *node2;
610 const struct lys_module *mod;
611 const char *name;
612 size_t name_len;
613
Michal Vasko004d3152020-06-11 19:59:22 +0200614 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
Radek Krejcif6a11002020-08-21 13:29:07 +0200615 /* '[', no predicate */
Michal Vasko004d3152020-06-11 19:59:22 +0200616 return LY_SUCCESS;
617 }
618
619 if (ctx_node->nodetype != LYS_LIST) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200620 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200621 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200622 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200623 } else if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200624 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200625 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200626 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200627 }
628
629 do {
630 /* NameTest, find the key */
Michal Vasko6b26e742020-07-17 15:02:10 +0200631 LY_CHECK_RET(ly_path_compile_prefix(cur_node->module->ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx,
Michal Vasko69730152020-10-09 16:30:07 +0200632 LY_PATH_LREF_TRUE, format, prefix_data, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100633 key = lys_find_child(ctx_node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200634 if (!key) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200635 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200636 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200637 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200638 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200639 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200640 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200641 }
642 ++(*tok_idx);
643
644 /* we are not actually compiling, throw the key away */
645 (void)key;
646
647 /* '=' */
648 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
649 ++(*tok_idx);
650
651 /* FuncName */
652 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME);
653 ++(*tok_idx);
654
655 /* evaluating from the "current()" node */
656 node = cur_node;
657
658 /* '(' */
659 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
660 ++(*tok_idx);
661
662 /* ')' */
663 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
664 ++(*tok_idx);
665
666 do {
667 /* '/' */
668 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
669 ++(*tok_idx);
670
671 /* go to parent */
672 if (!node) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200673 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "Too many parent references in path.");
Radek Krejci8de005f2020-06-25 17:02:07 +0200674 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200675 }
676 node = lysc_data_parent(node);
677
678 /* '..' */
679 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT);
680 ++(*tok_idx);
681 } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT);
682
683 do {
684 /* '/' */
685 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
686 ++(*tok_idx);
687
688 /* NameTest */
689 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST);
Michal Vasko6b26e742020-07-17 15:02:10 +0200690 LY_CHECK_RET(ly_path_compile_prefix(cur_node->module->ctx, cur_node, cur_node->module, node, expr, *tok_idx,
Michal Vasko69730152020-10-09 16:30:07 +0200691 LY_PATH_LREF_TRUE, format, prefix_data, &mod, &name, &name_len));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100692 node2 = lys_find_child(node, mod, name, name_len, 0, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200693 if (!node2) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200694 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200695 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200696 }
697 node = node2;
698 ++(*tok_idx);
699 } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST));
700
701 /* check the last target node */
702 if (node->nodetype != LYS_LEAF) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200703 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH,
Michal Vasko69730152020-10-09 16:30:07 +0200704 "Leaf expected instead of %s \"%s\" in leafref predicate in path.",
705 lys_nodetype2str(node->nodetype), node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200706 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200707 }
708
709 /* we are not actually compiling, throw the rightside node away */
710 (void)node;
711
712 /* ']' */
713 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
714 ++(*tok_idx);
715
Radek Krejci0f969882020-08-21 16:56:47 +0200716 /* another predicate follows? */
Michal Vasko004d3152020-06-11 19:59:22 +0200717 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
718
719 return LY_SUCCESS;
720}
721
722LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200723ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
Radek Krejci0f969882020-08-21 16:56:47 +0200724 const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target, LY_PREFIX_FORMAT format,
725 void *prefix_data, struct ly_path **path)
Michal Vasko004d3152020-06-11 19:59:22 +0200726{
727 LY_ERR ret = LY_SUCCESS;
728 uint16_t tok_idx = 0;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100729 const struct lys_module *mod = NULL;
Michal Vasko6b26e742020-07-17 15:02:10 +0200730 const struct lysc_node *node2, *cur_node, *op;
Michal Vasko00cbf532020-06-15 13:58:47 +0200731 struct ly_path *p = NULL;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200732 uint32_t getnext_opts;
Michal Vasko004d3152020-06-11 19:59:22 +0200733 const char *name;
734 size_t name_len;
735
Michal Vasko00cbf532020-06-15 13:58:47 +0200736 assert(ctx);
Michal Vasko6b26e742020-07-17 15:02:10 +0200737 assert((lref == LY_PATH_LREF_FALSE) || ctx_node);
Michal Vasko004d3152020-06-11 19:59:22 +0200738 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko00cbf532020-06-15 13:58:47 +0200739 assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT));
740 assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY));
Michal Vasko004d3152020-06-11 19:59:22 +0200741
Michal Vasko6b26e742020-07-17 15:02:10 +0200742 /* find operation, if we are in any */
Radek Krejci1e008d22020-08-17 11:37:37 +0200743 for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +0200744
745 /* remember original context node */
746 cur_node = ctx_node;
747
Michal Vasko004d3152020-06-11 19:59:22 +0200748 *path = NULL;
749
Michal Vasko00cbf532020-06-15 13:58:47 +0200750 if (oper == LY_PATH_OPER_OUTPUT) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100751 getnext_opts = LYS_GETNEXT_OUTPUT;
Michal Vasko00cbf532020-06-15 13:58:47 +0200752 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100753 getnext_opts = 0;
Michal Vasko00cbf532020-06-15 13:58:47 +0200754 }
755
Michal Vasko004d3152020-06-11 19:59:22 +0200756 if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) {
757 /* absolute path */
758 ctx_node = NULL;
759
760 ++tok_idx;
761 } else {
762 /* relative path */
763 while ((lref == LY_PATH_LREF_TRUE) && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) {
764 if (!ctx_node) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200765 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Too many parent references in path.");
Radek Krejci8de005f2020-06-25 17:02:07 +0200766 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200767 }
768
769 /* get parent */
770 ctx_node = lysc_data_parent(ctx_node);
771
772 ++tok_idx;
773
774 assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH);
775 ++tok_idx;
776 }
777
Michal Vasko00cbf532020-06-15 13:58:47 +0200778 /* we are not storing the parent */
779 (void)ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +0200780 }
781
782 do {
Michal Vasko00cbf532020-06-15 13:58:47 +0200783 /* check last compiled inner node, whether it is uniquely identified (even key-less list) */
784 if (p && (lref == LY_PATH_LREF_FALSE) && (p->node->nodetype == LYS_LIST) && !p->predicates) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200785 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200786 lys_nodetype2str(p->node->nodetype), p->node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200787 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200788 }
789
Michal Vasko004d3152020-06-11 19:59:22 +0200790 /* get module and node name */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200791 LY_CHECK_GOTO(ret = ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, lref, format,
Michal Vasko69730152020-10-09 16:30:07 +0200792 prefix_data, &mod, &name, &name_len), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200793 ++tok_idx;
794
795 /* find the next node */
Michal Vasko00cbf532020-06-15 13:58:47 +0200796 node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts);
Radek Krejci25fe3dd2020-11-10 22:02:26 +0100797 if (!node2 || (op && (node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200798 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200799 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200800 goto cleanup;
801 }
802 ctx_node = node2;
803
804 /* new path segment */
Michal Vasko00cbf532020-06-15 13:58:47 +0200805 LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200806 p->node = ctx_node;
807
808 /* compile any predicates */
809 if (lref == LY_PATH_LREF_TRUE) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200810 ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200811 } else {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200812 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 +0200813 &p->predicates, &p->pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +0200814 }
815 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200816 } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH));
817
Michal Vasko00cbf532020-06-15 13:58:47 +0200818 /* check last compiled node */
Michal Vasko69730152020-10-09 16:30:07 +0200819 if ((lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) &&
820 (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200821 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
Michal Vasko69730152020-10-09 16:30:07 +0200822 lys_nodetype2str(p->node->nodetype), p->node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200823 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200824 }
825
Michal Vasko004d3152020-06-11 19:59:22 +0200826cleanup:
827 if (ret) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200828 ly_path_free(ctx, *path);
Michal Vasko004d3152020-06-11 19:59:22 +0200829 *path = NULL;
830 }
831 return ret;
832}
833
834LY_ERR
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200835ly_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 +0200836 struct lyd_node **match)
Michal Vasko004d3152020-06-11 19:59:22 +0200837{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200838 LY_ARRAY_COUNT_TYPE u;
Michal Vasko00cbf532020-06-15 13:58:47 +0200839 struct lyd_node *prev_node = NULL, *node, *target;
Michal Vasko004d3152020-06-11 19:59:22 +0200840 uint64_t pos;
841
842 assert(path && start);
843
844 if (lysc_data_parent(path[0].node)) {
845 /* relative path, start from the parent children */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200846 start = lyd_child(start);
Michal Vasko004d3152020-06-11 19:59:22 +0200847 } else {
848 /* absolute path, start from the first top-level sibling */
849 while (start->parent) {
850 start = (struct lyd_node *)start->parent;
851 }
852 while (start->prev->next) {
853 start = start->prev;
854 }
855 }
856
857 LY_ARRAY_FOR(path, u) {
858 switch (path[u].pred_type) {
859 case LY_PATH_PREDTYPE_POSITION:
860 /* we cannot use hashes and want an instance on a specific position */
861 pos = 1;
Michal Vasko4c583e82020-07-17 12:16:14 +0200862 LYD_LIST_FOR_INST(start, path[u].node, node) {
Michal Vasko004d3152020-06-11 19:59:22 +0200863 if (pos == path[u].predicates[0].position) {
864 break;
865 }
866 ++pos;
867 }
868 break;
869 case LY_PATH_PREDTYPE_LEAFLIST:
870 /* we will use hashes to find one leaf-list instance */
871 LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target));
872 lyd_find_sibling_first(start, target, &node);
873 lyd_free_tree(target);
874 break;
875 case LY_PATH_PREDTYPE_LIST:
876 /* we will use hashes to find one list instance */
877 LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target));
878 lyd_find_sibling_first(start, target, &node);
879 lyd_free_tree(target);
880 break;
881 case LY_PATH_PREDTYPE_NONE:
882 /* we will use hashes to find one any/container/leaf instance */
883 lyd_find_sibling_val(start, path[u].node, NULL, 0, &node);
884 break;
885 }
886
887 if (!node) {
888 /* no matching nodes */
889 break;
890 }
891
Michal Vasko00cbf532020-06-15 13:58:47 +0200892 /* rememeber previous node */
893 prev_node = node;
894
Michal Vasko004d3152020-06-11 19:59:22 +0200895 /* next path segment, if any */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200896 start = lyd_child(node);
Michal Vasko004d3152020-06-11 19:59:22 +0200897 }
898
Michal Vasko004d3152020-06-11 19:59:22 +0200899 if (node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200900 /* we have found the full path */
901 if (path_idx) {
902 *path_idx = u;
903 }
904 if (match) {
905 *match = node;
906 }
Michal Vasko004d3152020-06-11 19:59:22 +0200907 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200908
909 } else if (prev_node) {
910 /* we have found only some partial match */
911 if (path_idx) {
912 *path_idx = u - 1;
913 }
914 if (match) {
915 *match = prev_node;
916 }
917 return LY_EINCOMPLETE;
918 }
919
920 /* we have not found any nodes */
921 if (path_idx) {
922 *path_idx = 0;
923 }
924 if (match) {
925 *match = NULL;
926 }
927 return LY_ENOTFOUND;
928}
929
930LY_ERR
931ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match)
932{
933 LY_ERR ret;
934 struct lyd_node *m;
935
936 ret = ly_path_eval_partial(path, start, NULL, &m);
937
938 if (ret == LY_SUCCESS) {
939 /* last node was found */
940 if (match) {
941 *match = m;
942 }
943 return LY_SUCCESS;
944 }
945
946 /* not a full match */
947 if (match) {
948 *match = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200949 }
950 return LY_ENOTFOUND;
951}
952
953LY_ERR
954ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup)
955{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200956 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko004d3152020-06-11 19:59:22 +0200957
958 if (!path) {
959 return LY_SUCCESS;
960 }
961
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200962 LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200963 LY_ARRAY_FOR(path, u) {
964 LY_ARRAY_INCREMENT(*dup);
965 (*dup)[u].node = path[u].node;
966 if (path[u].predicates) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200967 LY_ARRAY_CREATE_RET(ctx, (*dup)[u].predicates, LY_ARRAY_COUNT(path[u].predicates), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200968 (*dup)[u].pred_type = path[u].pred_type;
969 LY_ARRAY_FOR(path[u].predicates, v) {
970 struct ly_path_predicate *pred = &path[u].predicates[v];
971
972 LY_ARRAY_INCREMENT((*dup)[u].predicates);
973 switch (path[u].pred_type) {
974 case LY_PATH_PREDTYPE_POSITION:
975 /* position-predicate */
976 (*dup)[u].predicates[v].position = pred->position;
977 break;
978 case LY_PATH_PREDTYPE_LIST:
979 case LY_PATH_PREDTYPE_LEAFLIST:
980 /* key-predicate or leaf-list-predicate */
981 (*dup)[u].predicates[v].key = pred->key;
Michal Vasko004d3152020-06-11 19:59:22 +0200982 pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value);
Michal Vaskoae875662020-10-21 10:33:17 +0200983 ++((struct lysc_type *)pred->value.realtype)->refcount;
Michal Vasko004d3152020-06-11 19:59:22 +0200984 break;
985 case LY_PATH_PREDTYPE_NONE:
986 break;
987 }
988 }
989 }
990 }
991
992 return LY_SUCCESS;
993}
994
995void
Michal Vaskof7e16e22020-10-21 09:24:39 +0200996ly_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 +0200997{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200998 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +0200999
1000 if (!predicates) {
1001 return;
1002 }
1003
1004 LY_ARRAY_FOR(predicates, u) {
1005 switch (pred_type) {
1006 case LY_PATH_PREDTYPE_POSITION:
1007 case LY_PATH_PREDTYPE_NONE:
1008 /* nothing to free */
1009 break;
1010 case LY_PATH_PREDTYPE_LIST:
Michal Vasko004d3152020-06-11 19:59:22 +02001011 case LY_PATH_PREDTYPE_LEAFLIST:
Michal Vaskoae875662020-10-21 10:33:17 +02001012 if (predicates[u].value.realtype) {
1013 predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
1014 lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype);
1015 }
Michal Vasko004d3152020-06-11 19:59:22 +02001016 break;
1017 }
1018 }
1019 LY_ARRAY_FREE(predicates);
1020}
1021
1022void
1023ly_path_free(const struct ly_ctx *ctx, struct ly_path *path)
1024{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001025 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +02001026
1027 LY_ARRAY_FOR(path, u) {
Michal Vaskof7e16e22020-10-21 09:24:39 +02001028 ly_path_predicates_free(ctx, path[u].pred_type, path[u].predicates);
Michal Vasko004d3152020-06-11 19:59:22 +02001029 }
1030 LY_ARRAY_FREE(path);
1031}