blob: c2dad18952e0cfa264cd43c04f837b8bacc20d43 [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
Michal Vasko6b26e742020-07-17 15:02:10 +020034#define LOGVAL_P(CTX, CUR_NODE, CODE, FORMAT...) ly_vlog(CTX, (CUR_NODE) ? LY_VLOG_LYSC : LY_VLOG_NONE, CUR_NODE, CODE, ##FORMAT)
35
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,
49 uint16_t *tok_idx, uint8_t prefix, uint8_t pred)
Michal Vasko004d3152020-06-11 19:59:22 +020050{
51 struct ly_set *set = NULL;
52 uint32_t i;
53 const char *name;
54 size_t name_len;
55
56 /* '[' */
57 if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) {
58 if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS))
59 && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
60 set = ly_set_new();
61 LY_CHECK_ERR_GOTO(!set, LOGMEM(ctx), error);
62
63 do {
64 /* NameTest is always expected here */
65 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), error);
66
67 /* check prefix based on the options */
68 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
69 if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) {
Michal Vasko6b26e742020-07-17 15:02:10 +020070 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx],
71 exp->expr + exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +020072 goto error;
Michal Vasko8b06a5e2020-08-06 12:13:08 +020073 } else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) {
74 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", exp->tok_len[*tok_idx],
75 exp->expr + exp->tok_pos[*tok_idx]);
76 goto error;
Michal Vasko004d3152020-06-11 19:59:22 +020077 }
78 if (!name) {
79 name = exp->expr + exp->tok_pos[*tok_idx];
80 name_len = exp->tok_len[*tok_idx];
81 } else {
82 ++name;
83 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
84 }
85
86 /* check whether it was not already specified */
87 for (i = 0; i < set->count; ++i) {
88 /* all the keys must be from the same module so this comparison should be fine */
89 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Michal Vasko6b26e742020-07-17 15:02:10 +020090 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", name_len, name);
Michal Vasko004d3152020-06-11 19:59:22 +020091 goto error;
92 }
93 }
94
95 /* add it into the set */
96 ly_set_add(set, (void *)name, LY_SET_OPT_USEASLIST);
97
98 /* NameTest */
99 ++(*tok_idx);
100
101 /* '=' */
102 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), error);
103
104 /* Literal */
105 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), error);
106
107 /* ']' */
108 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error);
109
110 /* '[' */
111 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
112
113 /* '.' */
114 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) {
115 /* '=' */
116 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), error);
117
118 /* Literal */
119 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), error);
120
121 /* ']' */
122 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error);
123
124 /* Number */
125 } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) {
126
127 /* ']' */
128 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error);
129
130 } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) {
131 assert(prefix == LY_PATH_PREFIX_OPTIONAL);
132 set = ly_set_new();
133 LY_CHECK_ERR_GOTO(!set, LOGMEM(ctx), error);
134
135 do {
136 /* NameTest is always expected here */
137 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), error);
138
139 name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
140 if (!name) {
141 name = exp->expr + exp->tok_pos[*tok_idx];
142 name_len = exp->tok_len[*tok_idx];
143 } else {
144 ++name;
145 name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx]));
146 }
147
148 /* check whether it was not already specified */
149 for (i = 0; i < set->count; ++i) {
150 /* all the keys must be from the same module so this comparison should be fine */
151 if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200152 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", name_len, name);
Michal Vasko004d3152020-06-11 19:59:22 +0200153 goto error;
154 }
155 }
156
157 /* add it into the set */
158 ly_set_add(set, (void *)name, LY_SET_OPT_USEASLIST);
159
160 /* NameTest */
161 ++(*tok_idx);
162
163 /* '=' */
164 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), error);
165
166 /* FuncName */
167 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), error);
168 if ((exp->tok_len[*tok_idx] != 7) || strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", 7)) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200169 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.",
170 exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +0200171 goto error;
172 }
173 ++(*tok_idx);
174
175 /* '(' */
176 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), error);
177
178 /* ')' */
179 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), error);
180
181 /* '/' */
182 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), error);
183
184 /* '..' */
185 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), error);
186 do {
187 /* '/' */
188 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), error);
189 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT));
190
191 /* NameTest */
192 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), error);
193
194 /* '/' */
195 while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) {
196 /* NameTest */
197 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), error);
198 }
199
200 /* ']' */
201 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error);
202
203 /* '[' */
204 } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1));
205
206 } else {
Michal Vasko6b26e742020-07-17 15:02:10 +0200207 LOGVAL_P(ctx, cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
208 exp->expr + exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +0200209 goto error;
210 }
211 }
212
213 ly_set_free(set, NULL);
214 return LY_SUCCESS;
215
216error:
217 ly_set_free(set, NULL);
218 return LY_EVALID;
219}
220
221LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200222ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len,
223 uint8_t begin, uint8_t lref, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200224{
225 struct lyxp_expr *exp;
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200226 uint16_t tok_idx, cur_len;
227 const char *cur_node, *prev_prefix = NULL, *ptr;
Michal Vasko004d3152020-06-11 19:59:22 +0200228
229 assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER));
230 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200231 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY)
232 || (prefix == LY_PATH_PREFIX_STRICT_INHERIT));
Michal Vasko004d3152020-06-11 19:59:22 +0200233 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
234
235 /* parse as a generic XPath expression */
236 exp = lyxp_expr_parse(ctx, str_path, path_len, 1);
237 LY_CHECK_GOTO(!exp, error);
238 tok_idx = 0;
239
240 if (begin == LY_PATH_BEGIN_EITHER) {
241 /* is the path relative? */
242 if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) {
243 /* '..' */
244 while ((lref == LY_PATH_LREF_TRUE) && !lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT)) {
245 /* '/' */
246 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), error);
247 }
248 }
249 } else {
250 /* '/' */
251 LY_CHECK_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), error);
252 }
253
254 do {
255 /* NameTest */
256 LY_CHECK_GOTO(lyxp_check_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), error);
257
258 /* check prefix based on the options */
Michal Vasko8b06a5e2020-08-06 12:13:08 +0200259 cur_node = exp->expr + exp->tok_pos[tok_idx];
260 cur_len = exp->tok_len[tok_idx];
261 if (prefix == LY_PATH_PREFIX_MANDATORY) {
262 if (!strnstr(cur_node, ":", cur_len)) {
263 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
264 goto error;
265 }
266 } else if (prefix == LY_PATH_PREFIX_STRICT_INHERIT) {
267 if (!prev_prefix) {
268 /* the first node must have a prefix */
269 if (!strnstr(cur_node, ":", cur_len)) {
270 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
271 goto error;
272 }
273
274 /* remember the first prefix */
275 prev_prefix = cur_node;
276 } else {
277 /* the prefix must be different, if any */
278 ptr = strnstr(cur_node, ":", cur_len);
279 if (ptr) {
280 if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) {
281 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", cur_len, cur_node);
282 goto error;
283 }
284
285 /* remember this next prefix */
286 prev_prefix = cur_node;
287 }
288 }
Michal Vasko004d3152020-06-11 19:59:22 +0200289 }
290
291 ++tok_idx;
292
293 /* Predicate* */
Michal Vasko6b26e742020-07-17 15:02:10 +0200294 LY_CHECK_GOTO(ly_path_check_predicate(ctx, ctx_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200295
296 /* '/' */
297 } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH));
298
299 /* trailing token check */
300 if (exp->used > tok_idx) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200301 LOGVAL_P(ctx, ctx_node, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of path.",
302 exp->expr + exp->tok_pos[tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +0200303 goto error;
304 }
305
306 *expr = exp;
307 return LY_SUCCESS;
308
309error:
310 lyxp_expr_free(ctx, exp);
Radek Krejci8de005f2020-06-25 17:02:07 +0200311 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200312}
313
314LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200315ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path,
316 size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr)
Michal Vasko004d3152020-06-11 19:59:22 +0200317{
318 struct lyxp_expr *exp;
319 uint16_t tok_idx;
320
321 assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY));
322 assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
323
324 /* parse as a generic XPath expression */
325 exp = lyxp_expr_parse(ctx, str_path, path_len, 0);
326 LY_CHECK_GOTO(!exp, error);
327 tok_idx = 0;
328
Michal Vasko6b26e742020-07-17 15:02:10 +0200329 LY_CHECK_GOTO(ly_path_check_predicate(ctx, cur_node, exp, &tok_idx, prefix, pred), error);
Michal Vasko004d3152020-06-11 19:59:22 +0200330
331 /* trailing token check */
332 if (exp->used > tok_idx) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200333 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.",
334 exp->expr + exp->tok_pos[tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +0200335 goto error;
336 }
337
338 *expr = exp;
339 return LY_SUCCESS;
340
341error:
342 lyxp_expr_free(ctx, exp);
Radek Krejci8de005f2020-06-25 17:02:07 +0200343 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200344}
345
346/**
347 * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node.
348 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200349 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200350 * @param[in] cur_node Optional current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +0200351 * @param[in] cur_mod Module of the current (original context) node. Needed for ::LYD_SCHEMA.
352 * @param[in] prev_ctx_node Previous context node. Needed for ::LYD_JSON.
353 * @param[in] expr Parsed path.
354 * @param[in] tok_idx Index in @p expr.
355 * @param[in] lref Lref option.
Michal Vasko004d3152020-06-11 19:59:22 +0200356 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200357 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200358 * @param[out] mod Resolved module.
359 * @param[out] name Parsed node name.
360 * @param[out] name_len Length of @p name.
361 * @return LY_ERR value.
362 */
363static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200364ly_path_compile_prefix(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
365 const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint16_t tok_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200366 uint8_t lref, LY_PREFIX_FORMAT format, void *prefix_data, const struct lys_module **mod,
367 const char **name, size_t *name_len)
Michal Vasko004d3152020-06-11 19:59:22 +0200368{
369 const char *ptr;
370 size_t len;
371
372 assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST);
373
374 /* get prefix */
375 ptr = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]);
376 len = ptr ? ptr - (expr->expr + expr->tok_pos[tok_idx]) : 0;
377
378 /* find next node module */
379 if (ptr) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200380 *mod = ly_resolve_prefix(ctx, expr->expr + expr->tok_pos[tok_idx], len, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200381 if (!*mod) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200382 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Prefix \"%.*s\" not found of a module in path.",
383 len, expr->expr + expr->tok_pos[tok_idx]);
Radek Krejci8de005f2020-06-25 17:02:07 +0200384 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200385 } else if (!(*mod)->implemented) {
386 if (lref == LY_PATH_LREF_FALSE) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200387 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200388 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200389 }
Radek Krejci03fa8e82020-08-12 16:57:25 +0200390 LY_CHECK_RET(lys_set_implemented_internal((struct lys_module *)*mod, ctx->module_set_id));
Michal Vasko004d3152020-06-11 19:59:22 +0200391 }
392 } else {
393 switch (format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200394 case LY_PREF_SCHEMA:
Michal Vasko004d3152020-06-11 19:59:22 +0200395 *mod = cur_mod;
396 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200397 case LY_PREF_JSON:
Michal Vasko004d3152020-06-11 19:59:22 +0200398 if (!prev_ctx_node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200399 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200400 }
401 *mod = prev_ctx_node->module;
402 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200403 case LY_PREF_XML:
Michal Vasko004d3152020-06-11 19:59:22 +0200404 /* not really defined */
Michal Vasko00cbf532020-06-15 13:58:47 +0200405 LOGINT_RET(ctx);
Michal Vasko004d3152020-06-11 19:59:22 +0200406 }
407 }
408
409 /* set name */
410 if (ptr) {
411 *name = ptr + 1;
412 *name_len = expr->tok_len[tok_idx] - len - 1;
413 } else {
414 *name = expr->expr + expr->tok_pos[tok_idx];
415 *name_len = expr->tok_len[tok_idx];
416 }
417
418 return LY_SUCCESS;
419}
420
421LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +0200422ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
423 const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200424 LY_PREFIX_FORMAT format, void *prefix_data, struct ly_path_predicate **predicates,
425 enum ly_path_pred_type *pred_type)
Michal Vasko004d3152020-06-11 19:59:22 +0200426{
427 struct ly_path_predicate *p;
428 const struct lysc_node *key;
429 const struct lys_module *mod;
430 const char *name;
431 size_t name_len, key_count;
432
Michal Vasko00cbf532020-06-15 13:58:47 +0200433 assert(ctx && ctx_node);
434
Michal Vasko004d3152020-06-11 19:59:22 +0200435 *pred_type = 0;
436
437 /* '[' */
438 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
439 /* no predicate */
440 return LY_SUCCESS;
441 }
442
443 if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) {
444 if (ctx_node->nodetype != LYS_LIST) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200445 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
446 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200447 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200448 } else if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200449 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
450 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200451 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200452 }
453
454 do {
455 /* NameTest, find the key */
Michal Vasko6b26e742020-07-17 15:02:10 +0200456 LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, LY_PATH_LREF_FALSE,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200457 format, prefix_data, &mod, &name, &name_len));
Michal Vasko004d3152020-06-11 19:59:22 +0200458 key = lys_find_child(ctx_node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
459 if (!key) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200460 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200461 return LY_ENOTFOUND;
Michal Vasko004d3152020-06-11 19:59:22 +0200462 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200463 LOGVAL_P(ctx, cur_node ? cur_node : key, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
464 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200465 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200466 }
467 ++(*tok_idx);
468
469 /* new predicate */
470 if (!*pred_type) {
471 *pred_type = LY_PATH_PREDTYPE_LIST;
472 }
473 assert(*pred_type == LY_PATH_PREDTYPE_LIST);
Michal Vasko00cbf532020-06-15 13:58:47 +0200474 LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200475 p->key = key;
476
477 /* '=' */
478 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
479 ++(*tok_idx);
480
481 /* Literal */
482 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
483 LY_CHECK_RET(lyd_value_store(&p->value, key, expr->expr + expr->tok_pos[*tok_idx] + 1,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200484 expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data));
Michal Vasko004d3152020-06-11 19:59:22 +0200485 ++(*tok_idx);
486
487 /* ']' */
488 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
489 ++(*tok_idx);
490
491 /* another predicate follows? */
492 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
493
494 /* check that all keys were set */
495 key_count = 0;
496 for (key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next) {
497 ++key_count;
498 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200499 if (LY_ARRAY_COUNT(*predicates) != key_count) {
Michal Vasko004d3152020-06-11 19:59:22 +0200500 /* names (keys) are unique - it was checked when parsing */
Michal Vasko6b26e742020-07-17 15:02:10 +0200501 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.",
502 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Michal Vasko00cbf532020-06-15 13:58:47 +0200503 ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, NULL, *predicates);
Michal Vasko004d3152020-06-11 19:59:22 +0200504 *predicates = NULL;
Radek Krejci8de005f2020-06-25 17:02:07 +0200505 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200506 }
507
508 } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) {
509 if (ctx_node->nodetype != LYS_LEAFLIST) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200510 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.",
511 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200512 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200513 }
514 ++(*tok_idx);
515
516 /* new predicate */
517 *pred_type = LY_PATH_PREDTYPE_LEAFLIST;
Michal Vasko00cbf532020-06-15 13:58:47 +0200518 LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200519
520 /* '=' */
521 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
522 ++(*tok_idx);
523
524 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL);
525 /* store the value */
526 LY_CHECK_RET(lyd_value_store(&p->value, ctx_node, expr->expr + expr->tok_pos[*tok_idx] + 1,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200527 expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data));
Michal Vasko004d3152020-06-11 19:59:22 +0200528 ++(*tok_idx);
529
530 /* ']' */
531 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
532 ++(*tok_idx);
533 } else {
534 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER);
535 if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200536 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.",
537 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200538 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200539 } else if (ctx_node->flags & LYS_CONFIG_W) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200540 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Positional predicate defined for configuration %s \"%s\" in path.",
541 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200542 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200543 }
Michal Vasko004d3152020-06-11 19:59:22 +0200544
545 /* new predicate */
546 *pred_type = LY_PATH_PREDTYPE_POSITION;
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 /* syntax was already checked */
550 p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&name, 10);
Michal Vasko00cbf532020-06-15 13:58:47 +0200551 ++(*tok_idx);
Michal Vasko004d3152020-06-11 19:59:22 +0200552
553 /* ']' */
554 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
555 ++(*tok_idx);
556 }
557
558 return LY_SUCCESS;
559}
560
561/**
562 * @brief Compile leafref predicate. Actually, it is only checked.
563 *
564 * @param[in] ctx_node Context node, node for which the predicate is defined.
565 * @param[in] cur_node Current (original context) node.
566 * @param[in] expr Parsed path.
567 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
Michal Vasko004d3152020-06-11 19:59:22 +0200568 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200569 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200570 * @return LY_ERR value.
571 */
572static LY_ERR
573ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200574 const struct lyxp_expr *expr, uint16_t *tok_idx, LY_PREFIX_FORMAT format, void *prefix_data)
Michal Vasko004d3152020-06-11 19:59:22 +0200575{
576 const struct lysc_node *key, *node, *node2;
577 const struct lys_module *mod;
578 const char *name;
579 size_t name_len;
580
581 /* '[' */
582 if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) {
583 /* no predicate */
584 return LY_SUCCESS;
585 }
586
587 if (ctx_node->nodetype != LYS_LIST) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200588 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.",
589 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200590 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200591 } else if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200592 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.",
593 lys_nodetype2str(ctx_node->nodetype), ctx_node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200594 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200595 }
596
597 do {
598 /* NameTest, find the key */
Michal Vasko6b26e742020-07-17 15:02:10 +0200599 LY_CHECK_RET(ly_path_compile_prefix(cur_node->module->ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200600 LY_PATH_LREF_TRUE, format, prefix_data, &mod, &name, &name_len));
Michal Vasko004d3152020-06-11 19:59:22 +0200601 key = lys_find_child(ctx_node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
602 if (!key) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200603 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 +0200604 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200605 } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200606 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.",
607 lys_nodetype2str(key->nodetype), key->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200608 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200609 }
610 ++(*tok_idx);
611
612 /* we are not actually compiling, throw the key away */
613 (void)key;
614
615 /* '=' */
616 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL);
617 ++(*tok_idx);
618
619 /* FuncName */
620 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME);
621 ++(*tok_idx);
622
623 /* evaluating from the "current()" node */
624 node = cur_node;
625
626 /* '(' */
627 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
628 ++(*tok_idx);
629
630 /* ')' */
631 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
632 ++(*tok_idx);
633
634 do {
635 /* '/' */
636 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
637 ++(*tok_idx);
638
639 /* go to parent */
640 if (!node) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200641 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH, "Too many parent references in path.");
Radek Krejci8de005f2020-06-25 17:02:07 +0200642 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200643 }
644 node = lysc_data_parent(node);
645
646 /* '..' */
647 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT);
648 ++(*tok_idx);
649 } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT);
650
651 do {
652 /* '/' */
653 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH);
654 ++(*tok_idx);
655
656 /* NameTest */
657 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST);
Michal Vasko6b26e742020-07-17 15:02:10 +0200658 LY_CHECK_RET(ly_path_compile_prefix(cur_node->module->ctx, cur_node, cur_node->module, node, expr, *tok_idx,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200659 LY_PATH_LREF_TRUE, format, prefix_data, &mod, &name, &name_len));
Michal Vasko004d3152020-06-11 19:59:22 +0200660 node2 = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
661 if (!node2) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200662 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 +0200663 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200664 }
665 node = node2;
666 ++(*tok_idx);
667 } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST));
668
669 /* check the last target node */
670 if (node->nodetype != LYS_LEAF) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200671 LOGVAL_P(cur_node->module->ctx, cur_node, LYVE_XPATH,
672 "Leaf expected instead of %s \"%s\" in leafref predicate in path.",
673 lys_nodetype2str(node->nodetype), node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200674 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200675 }
676
677 /* we are not actually compiling, throw the rightside node away */
678 (void)node;
679
680 /* ']' */
681 assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
682 ++(*tok_idx);
683
684 /* another predicate follows? */
685 } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1));
686
687 return LY_SUCCESS;
688}
689
690LY_ERR
Michal Vasko00cbf532020-06-15 13:58:47 +0200691ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200692 const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target, LY_PREFIX_FORMAT format,
693 void *prefix_data, struct ly_path **path)
Michal Vasko004d3152020-06-11 19:59:22 +0200694{
695 LY_ERR ret = LY_SUCCESS;
696 uint16_t tok_idx = 0;
697 const struct lys_module *mod;
Michal Vasko6b26e742020-07-17 15:02:10 +0200698 const struct lysc_node *node2, *cur_node, *op;
Michal Vasko00cbf532020-06-15 13:58:47 +0200699 struct ly_path *p = NULL;
700 int getnext_opts;
Michal Vasko004d3152020-06-11 19:59:22 +0200701 const char *name;
702 size_t name_len;
703
Michal Vasko00cbf532020-06-15 13:58:47 +0200704 assert(ctx);
Michal Vasko6b26e742020-07-17 15:02:10 +0200705 assert((lref == LY_PATH_LREF_FALSE) || ctx_node);
Michal Vasko004d3152020-06-11 19:59:22 +0200706 assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE));
Michal Vasko00cbf532020-06-15 13:58:47 +0200707 assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT));
708 assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY));
Michal Vasko004d3152020-06-11 19:59:22 +0200709
Michal Vasko6b26e742020-07-17 15:02:10 +0200710 /* find operation, if we are in any */
711 for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent);
712
713 /* remember original context node */
714 cur_node = ctx_node;
715
Michal Vasko004d3152020-06-11 19:59:22 +0200716 *path = NULL;
717
Michal Vasko00cbf532020-06-15 13:58:47 +0200718 if (oper == LY_PATH_OPER_OUTPUT) {
719 getnext_opts = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_OUTPUT;
720 } else {
721 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
722 }
723
Michal Vasko004d3152020-06-11 19:59:22 +0200724 if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) {
725 /* absolute path */
726 ctx_node = NULL;
727
728 ++tok_idx;
729 } else {
730 /* relative path */
731 while ((lref == LY_PATH_LREF_TRUE) && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) {
732 if (!ctx_node) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200733 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Too many parent references in path.");
Radek Krejci8de005f2020-06-25 17:02:07 +0200734 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200735 }
736
737 /* get parent */
738 ctx_node = lysc_data_parent(ctx_node);
739
740 ++tok_idx;
741
742 assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH);
743 ++tok_idx;
744 }
745
Michal Vasko00cbf532020-06-15 13:58:47 +0200746 /* we are not storing the parent */
747 (void)ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +0200748 }
749
750 do {
Michal Vasko00cbf532020-06-15 13:58:47 +0200751 /* check last compiled inner node, whether it is uniquely identified (even key-less list) */
752 if (p && (lref == LY_PATH_LREF_FALSE) && (p->node->nodetype == LYS_LIST) && !p->predicates) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200753 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
754 lys_nodetype2str(p->node->nodetype), p->node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200755 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200756 }
757
Michal Vasko004d3152020-06-11 19:59:22 +0200758 /* get module and node name */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200759 LY_CHECK_GOTO(ret = ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, lref, format,
760 prefix_data, &mod, &name, &name_len), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200761 ++tok_idx;
762
763 /* find the next node */
Michal Vasko00cbf532020-06-15 13:58:47 +0200764 node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts);
Michal Vasko6b26e742020-07-17 15:02:10 +0200765 if (!node2 || ((node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) {
766 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200767 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +0200768 goto cleanup;
769 }
770 ctx_node = node2;
771
772 /* new path segment */
Michal Vasko00cbf532020-06-15 13:58:47 +0200773 LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200774 p->node = ctx_node;
775
776 /* compile any predicates */
777 if (lref == LY_PATH_LREF_TRUE) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200778 ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200779 } else {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200780 ret = ly_path_compile_predicate(ctx, cur_node, cur_mod, ctx_node, expr, &tok_idx, format, prefix_data,
781 &p->predicates, &p->pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +0200782 }
783 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +0200784 } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH));
785
Michal Vasko00cbf532020-06-15 13:58:47 +0200786 /* check last compiled node */
787 if ((lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE)
788 && (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) {
Michal Vasko6b26e742020-07-17 15:02:10 +0200789 LOGVAL_P(ctx, cur_node, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.",
790 lys_nodetype2str(p->node->nodetype), p->node->name);
Radek Krejci8de005f2020-06-25 17:02:07 +0200791 return LY_EVALID;
Michal Vasko00cbf532020-06-15 13:58:47 +0200792 }
793
Michal Vasko004d3152020-06-11 19:59:22 +0200794cleanup:
795 if (ret) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200796 ly_path_free(ctx, *path);
Michal Vasko004d3152020-06-11 19:59:22 +0200797 *path = NULL;
798 }
799 return ret;
800}
801
802LY_ERR
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200803ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx,
Michal Vasko00cbf532020-06-15 13:58:47 +0200804 struct lyd_node **match)
Michal Vasko004d3152020-06-11 19:59:22 +0200805{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200806 LY_ARRAY_COUNT_TYPE u;
Michal Vasko00cbf532020-06-15 13:58:47 +0200807 struct lyd_node *prev_node = NULL, *node, *target;
Michal Vasko004d3152020-06-11 19:59:22 +0200808 uint64_t pos;
809
810 assert(path && start);
811
812 if (lysc_data_parent(path[0].node)) {
813 /* relative path, start from the parent children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200814 start = lyd_node_children(start, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200815 } else {
816 /* absolute path, start from the first top-level sibling */
817 while (start->parent) {
818 start = (struct lyd_node *)start->parent;
819 }
820 while (start->prev->next) {
821 start = start->prev;
822 }
823 }
824
825 LY_ARRAY_FOR(path, u) {
826 switch (path[u].pred_type) {
827 case LY_PATH_PREDTYPE_POSITION:
828 /* we cannot use hashes and want an instance on a specific position */
829 pos = 1;
Michal Vasko4c583e82020-07-17 12:16:14 +0200830 LYD_LIST_FOR_INST(start, path[u].node, node) {
Michal Vasko004d3152020-06-11 19:59:22 +0200831 if (pos == path[u].predicates[0].position) {
832 break;
833 }
834 ++pos;
835 }
836 break;
837 case LY_PATH_PREDTYPE_LEAFLIST:
838 /* we will use hashes to find one leaf-list instance */
839 LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target));
840 lyd_find_sibling_first(start, target, &node);
841 lyd_free_tree(target);
842 break;
843 case LY_PATH_PREDTYPE_LIST:
844 /* we will use hashes to find one list instance */
845 LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target));
846 lyd_find_sibling_first(start, target, &node);
847 lyd_free_tree(target);
848 break;
849 case LY_PATH_PREDTYPE_NONE:
850 /* we will use hashes to find one any/container/leaf instance */
851 lyd_find_sibling_val(start, path[u].node, NULL, 0, &node);
852 break;
853 }
854
855 if (!node) {
856 /* no matching nodes */
857 break;
858 }
859
Michal Vasko00cbf532020-06-15 13:58:47 +0200860 /* rememeber previous node */
861 prev_node = node;
862
Michal Vasko004d3152020-06-11 19:59:22 +0200863 /* next path segment, if any */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200864 start = lyd_node_children(node, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200865 }
866
Michal Vasko004d3152020-06-11 19:59:22 +0200867 if (node) {
Michal Vasko00cbf532020-06-15 13:58:47 +0200868 /* we have found the full path */
869 if (path_idx) {
870 *path_idx = u;
871 }
872 if (match) {
873 *match = node;
874 }
Michal Vasko004d3152020-06-11 19:59:22 +0200875 return LY_SUCCESS;
Michal Vasko00cbf532020-06-15 13:58:47 +0200876
877 } else if (prev_node) {
878 /* we have found only some partial match */
879 if (path_idx) {
880 *path_idx = u - 1;
881 }
882 if (match) {
883 *match = prev_node;
884 }
885 return LY_EINCOMPLETE;
886 }
887
888 /* we have not found any nodes */
889 if (path_idx) {
890 *path_idx = 0;
891 }
892 if (match) {
893 *match = NULL;
894 }
895 return LY_ENOTFOUND;
896}
897
898LY_ERR
899ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match)
900{
901 LY_ERR ret;
902 struct lyd_node *m;
903
904 ret = ly_path_eval_partial(path, start, NULL, &m);
905
906 if (ret == LY_SUCCESS) {
907 /* last node was found */
908 if (match) {
909 *match = m;
910 }
911 return LY_SUCCESS;
912 }
913
914 /* not a full match */
915 if (match) {
916 *match = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +0200917 }
918 return LY_ENOTFOUND;
919}
920
921LY_ERR
922ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup)
923{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200924 LY_ARRAY_COUNT_TYPE u, v;
Michal Vasko004d3152020-06-11 19:59:22 +0200925
926 if (!path) {
927 return LY_SUCCESS;
928 }
929
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200930 LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200931 LY_ARRAY_FOR(path, u) {
932 LY_ARRAY_INCREMENT(*dup);
933 (*dup)[u].node = path[u].node;
934 if (path[u].predicates) {
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200935 LY_ARRAY_CREATE_RET(ctx, (*dup)[u].predicates, LY_ARRAY_COUNT(path[u].predicates), LY_EMEM);
Michal Vasko004d3152020-06-11 19:59:22 +0200936 (*dup)[u].pred_type = path[u].pred_type;
937 LY_ARRAY_FOR(path[u].predicates, v) {
938 struct ly_path_predicate *pred = &path[u].predicates[v];
939
940 LY_ARRAY_INCREMENT((*dup)[u].predicates);
941 switch (path[u].pred_type) {
942 case LY_PATH_PREDTYPE_POSITION:
943 /* position-predicate */
944 (*dup)[u].predicates[v].position = pred->position;
945 break;
946 case LY_PATH_PREDTYPE_LIST:
947 case LY_PATH_PREDTYPE_LEAFLIST:
948 /* key-predicate or leaf-list-predicate */
949 (*dup)[u].predicates[v].key = pred->key;
950 (*dup)[u].predicates[v].value.realtype = pred->value.realtype;
951 pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value);
952 break;
953 case LY_PATH_PREDTYPE_NONE:
954 break;
955 }
956 }
957 }
958 }
959
960 return LY_SUCCESS;
961}
962
963void
964ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, const struct lysc_node *llist,
965 struct ly_path_predicate *predicates)
966{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200967 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +0200968
969 if (!predicates) {
970 return;
971 }
972
973 LY_ARRAY_FOR(predicates, u) {
974 switch (pred_type) {
975 case LY_PATH_PREDTYPE_POSITION:
976 case LY_PATH_PREDTYPE_NONE:
977 /* nothing to free */
978 break;
979 case LY_PATH_PREDTYPE_LIST:
980 ((struct lysc_node_leaf *)predicates[u].key)->type->plugin->free(ctx, &predicates[u].value);
981 break;
982 case LY_PATH_PREDTYPE_LEAFLIST:
983 ((struct lysc_node_leaflist *)llist)->type->plugin->free(ctx, &predicates[u].value);
984 break;
985 }
986 }
987 LY_ARRAY_FREE(predicates);
988}
989
990void
991ly_path_free(const struct ly_ctx *ctx, struct ly_path *path)
992{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200993 LY_ARRAY_COUNT_TYPE u;
Michal Vasko004d3152020-06-11 19:59:22 +0200994
995 LY_ARRAY_FOR(path, u) {
996 ly_path_predicates_free(ctx, path[u].pred_type, path[u].node, path[u].predicates);
997 }
998 LY_ARRAY_FREE(path);
999}